X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/14957cd040308e3eeec43d26bae5d76da13fcd85..refs/heads/master:/heap/Weak.h?ds=sidebyside diff --git a/heap/Weak.h b/heap/Weak.h index a235a57..80cdbd8 100644 --- a/heap/Weak.h +++ b/heap/Weak.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Apple Inc. All rights reserved. + * Copyright (C) 2009, 2012, 2013 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -26,145 +26,72 @@ #ifndef Weak_h #define Weak_h -#include "Assertions.h" -#include "Handle.h" -#include "HandleHeap.h" -#include "JSGlobalData.h" +#include +#include namespace JSC { -// A weakly referenced handle that becomes 0 when the value it points to is garbage collected. -template class Weak : public Handle { - using Handle::slot; - using Handle::setSlot; +class WeakImpl; +class WeakHandleOwner; -public: - typedef typename Handle::ExternalType ExternalType; +// This is a free function rather than a Weak member function so we can put it in Weak.cpp. +JS_EXPORT_PRIVATE void weakClearSlowCase(WeakImpl*&); +template class Weak { + WTF_MAKE_NONCOPYABLE(Weak); +public: Weak() - : Handle() + : m_impl(0) { } - Weak(JSGlobalData& globalData, ExternalType value = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0) - : Handle(globalData.allocateGlobalHandle()) + Weak(std::nullptr_t) + : m_impl(0) { - HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context); - set(value); } - enum AdoptTag { Adopt }; - template Weak(AdoptTag, Handle handle) - : Handle(handle.slot()) - { - validateCell(get()); - } - - Weak(const Weak& other) - : Handle() - { - if (!other.slot()) - return; - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - } + Weak(T*, WeakHandleOwner* = 0, void* context = 0); - template Weak(const Weak& other) - : Handle() - { - if (!other.slot()) - return; - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - } - enum HashTableDeletedValueTag { HashTableDeletedValue }; - bool isHashTableDeletedValue() const { return slot() == hashTableDeletedValue(); } - Weak(HashTableDeletedValueTag) - : Handle(hashTableDeletedValue()) - { - } + bool isHashTableDeletedValue() const; + Weak(HashTableDeletedValueTag); + + Weak(Weak&&); ~Weak() { clear(); } - void swap(Weak& other) - { - Handle::swap(other); - } + void swap(Weak&); - ExternalType get() const { return HandleTypes::getFromSlot(slot()); } - - void clear() - { - if (!slot()) - return; - HandleHeap::heapFor(slot())->deallocate(slot()); - setSlot(0); - } - - void set(JSGlobalData& globalData, ExternalType value, WeakHandleOwner* weakOwner = 0, void* context = 0) - { - if (!slot()) { - setSlot(globalData.allocateGlobalHandle()); - HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context); - } - ASSERT(HandleHeap::heapFor(slot())->hasWeakOwner(slot(), weakOwner)); - set(value); - } + Weak& operator=(Weak&&); - template Weak& operator=(const Weak& other) - { - clear(); - if (other.slot()) - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - return *this; - } + bool operator!() const; + T* operator->() const; + T& operator*() const; + T* get() const; - Weak& operator=(const Weak& other) + bool was(T*) const; + + // This conversion operator allows implicit conversion to bool but not to other integer types. + typedef void* (Weak::*UnspecifiedBoolType); + operator UnspecifiedBoolType*() const; + + WeakImpl* leakImpl() WARN_UNUSED_RETURN; + void clear() { - clear(); - if (other.slot()) - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - return *this; + if (!m_impl) + return; + weakClearSlowCase(m_impl); } - HandleSlot leakHandle() - { - ASSERT(HandleHeap::heapFor(slot())->hasFinalizer(slot())); - HandleSlot result = slot(); - setSlot(0); - return result; - } - private: - static HandleSlot hashTableDeletedValue() { return reinterpret_cast(-1); } + static WeakImpl* hashTableDeletedValue(); - void set(ExternalType externalType) - { - ASSERT(slot()); - JSValue value = HandleTypes::toJSValue(externalType); - ASSERT(!value || !value.isCell() || Heap::isMarked(value.asCell())); - HandleHeap::heapFor(slot())->writeBarrier(slot(), value); - *slot() = value; - } + WeakImpl* m_impl; }; -template inline void swap(Weak& a, Weak& b) -{ - a.swap(b); -} - } // namespace JSC -namespace WTF { - -template struct VectorTraits > : SimpleClassVectorTraits { - static const bool canCompareWithMemcmp = false; -}; - -template struct HashTraits > : SimpleClassHashTraits > { }; - -} - #endif // Weak_h