Changeset 108010 in webkit for trunk/Source/JavaScriptCore/heap/Weak.h
- Timestamp:
- Feb 16, 2012, 5:56:13 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/heap/Weak.h
r96465 r108010 1 1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved.2 * Copyright (C) 2009, 2012 Apple Inc. All rights reserved. 3 3 * 4 4 * Redistribution and use in source and binary forms, with or without … … 31 31 #include "HandleHeap.h" 32 32 #include "JSGlobalData.h" 33 #include "PassWeak.h" 33 34 34 35 namespace JSC { … … 36 37 // A weakly referenced handle that becomes 0 when the value it points to is garbage collected. 37 38 template <typename T> class Weak : public Handle<T> { 39 WTF_MAKE_NONCOPYABLE(Weak); 40 38 41 using Handle<T>::slot; 39 42 using Handle<T>::setSlot; … … 47 50 } 48 51 49 Weak(JSGlobalData& globalData, ExternalType value = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0) 52 Weak(std::nullptr_t) 53 : Handle<T>() 54 { 55 } 56 57 Weak(JSGlobalData& globalData, ExternalType externalType = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0) 50 58 : Handle<T>(globalData.heap.handleHeap()->allocate()) 51 59 { 52 60 HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context); 53 set(value); 61 JSValue value = HandleTypes<T>::toJSValue(externalType); 62 HandleHeap::heapFor(slot())->writeBarrier(slot(), value); 63 *slot() = value; 54 64 } 55 65 … … 60 70 validateCell(get()); 61 71 } 62 63 Weak(const Weak& other)64 : Handle<T>()65 {66 if (!other.slot())67 return;68 setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot()));69 }70 72 71 template <typename U> Weak(const Weak<U>& other)72 : Handle<T>()73 {74 if (!other.slot())75 return;76 setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot()));77 }78 79 73 enum HashTableDeletedValueTag { HashTableDeletedValue }; 80 74 bool isHashTableDeletedValue() const { return slot() == hashTableDeletedValue(); } 81 75 Weak(HashTableDeletedValueTag) 82 76 : Handle<T>(hashTableDeletedValue()) 77 { 78 } 79 80 template<typename U> Weak(const PassWeak<U>& other) 81 : Handle<T>(other.leakHandle()) 83 82 { 84 83 } … … 94 93 } 95 94 95 Weak& operator=(const PassWeak<T>&); 96 96 97 ExternalType get() const { return HandleTypes<T>::getFromSlot(slot()); } 97 98 99 PassWeak<T> release() { PassWeak<T> tmp = adoptWeak<T>(slot()); setSlot(0); return tmp; } 100 98 101 void clear() 99 102 { … … 102 105 HandleHeap::heapFor(slot())->deallocate(slot()); 103 106 setSlot(0); 104 }105 106 void set(JSGlobalData& globalData, ExternalType value, WeakHandleOwner* weakOwner = 0, void* context = 0)107 {108 if (!slot()) {109 setSlot(globalData.heap.handleHeap()->allocate());110 HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context);111 }112 ASSERT(HandleHeap::heapFor(slot())->hasWeakOwner(slot(), weakOwner));113 set(value);114 }115 116 template <typename U> Weak& operator=(const Weak<U>& other)117 {118 clear();119 if (other.slot())120 setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot()));121 return *this;122 }123 124 Weak& operator=(const Weak& other)125 {126 clear();127 if (other.slot())128 setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot()));129 return *this;130 107 } 131 108 … … 140 117 private: 141 118 static HandleSlot hashTableDeletedValue() { return reinterpret_cast<HandleSlot>(-1); } 142 143 void set(ExternalType externalType)144 {145 ASSERT(slot());146 JSValue value = HandleTypes<T>::toJSValue(externalType);147 HandleHeap::heapFor(slot())->writeBarrier(slot(), value);148 *slot() = value;149 }150 119 }; 151 120 … … 153 122 { 154 123 a.swap(b); 124 } 125 126 template<typename T> inline Weak<T>& Weak<T>::operator=(const PassWeak<T>& o) 127 { 128 clear(); 129 setSlot(o.leakHandle()); 130 return *this; 155 131 } 156 132 … … 163 139 }; 164 140 165 template<typename P> struct HashTraits<JSC::Weak<P> > : SimpleClassHashTraits<JSC::Weak<P> > { }; 141 template<typename T> struct HashTraits<JSC::Weak<T> > : SimpleClassHashTraits<JSC::Weak<T> > { 142 typedef JSC::Weak<T> StorageType; 143 144 typedef std::nullptr_t EmptyValueType; 145 static EmptyValueType emptyValue() { return EmptyValueType(); } 146 147 typedef JSC::PassWeak<T> PassInType; 148 static void store(PassInType value, StorageType& storage) { storage = value; } 149 150 typedef JSC::PassWeak<T> PassOutType; 151 static PassOutType passOut(StorageType& value) { return value.release(); } 152 static PassOutType passOut(EmptyValueType) { return PassOutType(); } 153 154 typedef typename StorageType::ExternalType PeekType; 155 static PeekType peek(const StorageType& value) { return value.get(); } 156 static PeekType peek(EmptyValueType) { return PeekType(); } 157 }; 166 158 167 159 }
Note:
See TracChangeset
for help on using the changeset viewer.