diff options
Diffstat (limited to 'JavaScriptCore/runtime/WeakGCPtr.h')
| -rw-r--r-- | JavaScriptCore/runtime/WeakGCPtr.h | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/JavaScriptCore/runtime/WeakGCPtr.h b/JavaScriptCore/runtime/WeakGCPtr.h index 3ed4645..ac77cf3 100644 --- a/JavaScriptCore/runtime/WeakGCPtr.h +++ b/JavaScriptCore/runtime/WeakGCPtr.h @@ -27,6 +27,7 @@ #define WeakGCPtr_h #include "Collector.h" +#include "GCHandle.h" #include <wtf/Noncopyable.h> namespace JSC { @@ -34,20 +35,34 @@ namespace JSC { // A smart pointer whose get() function returns 0 for cells awaiting destruction. template <typename T> class WeakGCPtr : Noncopyable { public: - WeakGCPtr() : m_ptr(0) { } + WeakGCPtr() + : m_ptr(0) + { + } + WeakGCPtr(T* ptr) { assign(ptr); } + ~WeakGCPtr() + { + if (m_ptr) + m_ptr->pool()->free(m_ptr); + } + T* get() const { - if (!m_ptr || !Heap::isCellMarked(m_ptr)) - return 0; - return m_ptr; + if (m_ptr && m_ptr->isValidPtr()) + return static_cast<T*>(m_ptr->get()); + return 0; } - void clear(JSCell* ptr) + bool clear(JSCell* p) { - if (ptr == m_ptr) - m_ptr = 0; + if (!m_ptr || m_ptr->get() != p) + return false; + + m_ptr->pool()->free(m_ptr); + m_ptr = 0; + return true; } T& operator*() const { return *get(); } @@ -59,21 +74,27 @@ public: #if COMPILER(WINSCW) operator bool() const { return m_ptr; } #else - typedef T* WeakGCPtr::*UnspecifiedBoolType; + typedef WeakGCHandle* WeakGCPtr::*UnspecifiedBoolType; operator UnspecifiedBoolType() const { return get() ? &WeakGCPtr::m_ptr : 0; } #endif WeakGCPtr& operator=(T*); +#if !ASSERT_DISABLED + bool hasDeadObject() const { return !!m_ptr; } +#endif + private: - void assign(T* ptr) + void assign(JSCell* ptr) { - if (ptr) - Heap::markCell(ptr); - m_ptr = ptr; + ASSERT(ptr); + if (m_ptr) + m_ptr->set(ptr); + else + m_ptr = Heap::heap(ptr)->addWeakGCHandle(ptr); } - T* m_ptr; + WeakGCHandle* m_ptr; }; template <typename T> inline WeakGCPtr<T>& WeakGCPtr<T>::operator=(T* optr) @@ -122,7 +143,7 @@ template <typename T, typename U> inline WeakGCPtr<T> const_pointer_cast(const W return WeakGCPtr<T>(const_cast<T*>(p.get())); } -template <typename T> inline T* getPtr(const WeakGCPtr<T>& p) +template <typename T> inline T* get(const WeakGCPtr<T>& p) { return p.get(); } |
