| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef HeapAllocator_h | 5 #ifndef HeapAllocator_h |
| 6 #define HeapAllocator_h | 6 #define HeapAllocator_h |
| 7 | 7 |
| 8 #include "platform/heap/Heap.h" | 8 #include "platform/heap/Heap.h" |
| 9 #include "platform/heap/Persistent.h" | 9 #include "platform/heap/Persistent.h" |
| 10 #include "platform/heap/TraceTraits.h" | 10 #include "platform/heap/TraceTraits.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // allocated. However see also HeapListHashSetAllocator. | 46 // allocated. However see also HeapListHashSetAllocator. |
| 47 class PLATFORM_EXPORT HeapAllocator { | 47 class PLATFORM_EXPORT HeapAllocator { |
| 48 STATIC_ONLY(HeapAllocator); | 48 STATIC_ONLY(HeapAllocator); |
| 49 | 49 |
| 50 public: | 50 public: |
| 51 using Visitor = blink::Visitor; | 51 using Visitor = blink::Visitor; |
| 52 static const bool isGarbageCollected = true; | 52 static const bool isGarbageCollected = true; |
| 53 | 53 |
| 54 template <typename T> | 54 template <typename T> |
| 55 static size_t quantizedSize(size_t count) { | 55 static size_t quantizedSize(size_t count) { |
| 56 RELEASE_ASSERT(count <= maxHeapObjectSize / sizeof(T)); | 56 CHECK(count <= maxHeapObjectSize / sizeof(T)); |
| 57 return ThreadHeap::allocationSizeFromSize(count * sizeof(T)) - | 57 return ThreadHeap::allocationSizeFromSize(count * sizeof(T)) - |
| 58 sizeof(HeapObjectHeader); | 58 sizeof(HeapObjectHeader); |
| 59 } | 59 } |
| 60 template <typename T> | 60 template <typename T> |
| 61 static T* allocateVectorBacking(size_t size) { | 61 static T* allocateVectorBacking(size_t size) { |
| 62 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(); | 62 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(); |
| 63 ASSERT(state->isAllocationAllowed()); | 63 DCHECK(state->isAllocationAllowed()); |
| 64 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T>>::index(); | 64 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T>>::index(); |
| 65 NormalPageArena* arena = | 65 NormalPageArena* arena = |
| 66 static_cast<NormalPageArena*>(state->vectorBackingArena(gcInfoIndex)); | 66 static_cast<NormalPageArena*>(state->vectorBackingArena(gcInfoIndex)); |
| 67 return reinterpret_cast<T*>(arena->allocateObject( | 67 return reinterpret_cast<T*>(arena->allocateObject( |
| 68 ThreadHeap::allocationSizeFromSize(size), gcInfoIndex)); | 68 ThreadHeap::allocationSizeFromSize(size), gcInfoIndex)); |
| 69 } | 69 } |
| 70 template <typename T> | 70 template <typename T> |
| 71 static T* allocateExpandedVectorBacking(size_t size) { | 71 static T* allocateExpandedVectorBacking(size_t size) { |
| 72 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(); | 72 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(); |
| 73 ASSERT(state->isAllocationAllowed()); | 73 DCHECK(state->isAllocationAllowed()); |
| 74 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T>>::index(); | 74 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T>>::index(); |
| 75 NormalPageArena* arena = static_cast<NormalPageArena*>( | 75 NormalPageArena* arena = static_cast<NormalPageArena*>( |
| 76 state->expandedVectorBackingArena(gcInfoIndex)); | 76 state->expandedVectorBackingArena(gcInfoIndex)); |
| 77 return reinterpret_cast<T*>(arena->allocateObject( | 77 return reinterpret_cast<T*>(arena->allocateObject( |
| 78 ThreadHeap::allocationSizeFromSize(size), gcInfoIndex)); | 78 ThreadHeap::allocationSizeFromSize(size), gcInfoIndex)); |
| 79 } | 79 } |
| 80 static void freeVectorBacking(void*); | 80 static void freeVectorBacking(void*); |
| 81 static bool expandVectorBacking(void*, size_t); | 81 static bool expandVectorBacking(void*, size_t); |
| 82 static bool shrinkVectorBacking(void* address, | 82 static bool shrinkVectorBacking(void* address, |
| 83 size_t quantizedCurrentSize, | 83 size_t quantizedCurrentSize, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // provide a version that asserts and fails at run-time if | 123 // provide a version that asserts and fails at run-time if |
| 124 // used. | 124 // used. |
| 125 // Elsewhere we expect compilation to fail if 'delete' is | 125 // Elsewhere we expect compilation to fail if 'delete' is |
| 126 // attempted used and instantiated with a HeapAllocator-based | 126 // attempted used and instantiated with a HeapAllocator-based |
| 127 // object, as HeapAllocator::free is not provided. | 127 // object, as HeapAllocator::free is not provided. |
| 128 static void free(void*) { NOTREACHED(); } | 128 static void free(void*) { NOTREACHED(); } |
| 129 #endif | 129 #endif |
| 130 | 130 |
| 131 template <typename T> | 131 template <typename T> |
| 132 static void* newArray(size_t bytes) { | 132 static void* newArray(size_t bytes) { |
| 133 ASSERT_NOT_REACHED(); | 133 NOTREACHED(); |
| 134 return 0; | 134 return 0; |
| 135 } | 135 } |
| 136 | 136 |
| 137 static void deleteArray(void* ptr) { ASSERT_NOT_REACHED(); } | 137 static void deleteArray(void* ptr) { NOTREACHED(); } |
| 138 | 138 |
| 139 static bool isAllocationAllowed() { | 139 static bool isAllocationAllowed() { |
| 140 return ThreadState::current()->isAllocationAllowed(); | 140 return ThreadState::current()->isAllocationAllowed(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 template <typename T> | 143 template <typename T> |
| 144 static bool isHeapObjectAlive(T* object) { | 144 static bool isHeapObjectAlive(T* object) { |
| 145 return ThreadHeap::isHeapObjectAlive(object); | 145 return ThreadHeap::isHeapObjectAlive(object); |
| 146 } | 146 } |
| 147 | 147 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 173 | 173 |
| 174 template <typename VisitorDispatcher> | 174 template <typename VisitorDispatcher> |
| 175 static void registerWeakTable(VisitorDispatcher visitor, | 175 static void registerWeakTable(VisitorDispatcher visitor, |
| 176 const void* closure, | 176 const void* closure, |
| 177 EphemeronCallback iterationCallback, | 177 EphemeronCallback iterationCallback, |
| 178 EphemeronCallback iterationDoneCallback) { | 178 EphemeronCallback iterationDoneCallback) { |
| 179 visitor->registerWeakTable(closure, iterationCallback, | 179 visitor->registerWeakTable(closure, iterationCallback, |
| 180 iterationDoneCallback); | 180 iterationDoneCallback); |
| 181 } | 181 } |
| 182 | 182 |
| 183 #if ENABLE(ASSERT) | 183 #if DCHECK_IS_ON() |
| 184 template <typename VisitorDispatcher> | 184 template <typename VisitorDispatcher> |
| 185 static bool weakTableRegistered(VisitorDispatcher visitor, | 185 static bool weakTableRegistered(VisitorDispatcher visitor, |
| 186 const void* closure) { | 186 const void* closure) { |
| 187 return visitor->weakTableRegistered(closure); | 187 return visitor->weakTableRegistered(closure); |
| 188 } | 188 } |
| 189 #endif | 189 #endif |
| 190 | 190 |
| 191 template <typename T, typename VisitorDispatcher> | 191 template <typename T, typename VisitorDispatcher> |
| 192 static void registerBackingStoreReference(VisitorDispatcher visitor, | 192 static void registerBackingStoreReference(VisitorDispatcher visitor, |
| 193 T** slot) { | 193 T** slot) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 void HeapVectorBacking<T, Traits>::finalize(void* pointer) { | 300 void HeapVectorBacking<T, Traits>::finalize(void* pointer) { |
| 301 static_assert(Traits::needsDestruction, | 301 static_assert(Traits::needsDestruction, |
| 302 "Only vector buffers with items requiring destruction should " | 302 "Only vector buffers with items requiring destruction should " |
| 303 "be finalized"); | 303 "be finalized"); |
| 304 // See the comment in HeapVectorBacking::trace. | 304 // See the comment in HeapVectorBacking::trace. |
| 305 static_assert( | 305 static_assert( |
| 306 Traits::canClearUnusedSlotsWithMemset || std::is_polymorphic<T>::value, | 306 Traits::canClearUnusedSlotsWithMemset || std::is_polymorphic<T>::value, |
| 307 "HeapVectorBacking doesn't support objects that cannot be cleared as " | 307 "HeapVectorBacking doesn't support objects that cannot be cleared as " |
| 308 "unused with memset or don't have a vtable"); | 308 "unused with memset or don't have a vtable"); |
| 309 | 309 |
| 310 ASSERT(!WTF::IsTriviallyDestructible<T>::value); | 310 DCHECK(!WTF::IsTriviallyDestructible<T>::value); |
| 311 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer); | 311 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer); |
| 312 ASSERT(header->checkHeader()); | 312 DCHECK(header->checkHeader()); |
| 313 // Use the payload size as recorded by the heap to determine how many | 313 // Use the payload size as recorded by the heap to determine how many |
| 314 // elements to finalize. | 314 // elements to finalize. |
| 315 size_t length = header->payloadSize() / sizeof(T); | 315 size_t length = header->payloadSize() / sizeof(T); |
| 316 T* buffer = reinterpret_cast<T*>(pointer); | 316 T* buffer = reinterpret_cast<T*>(pointer); |
| 317 #ifdef ANNOTATE_CONTIGUOUS_CONTAINER | 317 #ifdef ANNOTATE_CONTIGUOUS_CONTAINER |
| 318 // As commented above, HeapVectorBacking calls finalizers for unused slots | 318 // As commented above, HeapVectorBacking calls finalizers for unused slots |
| 319 // (which are already zeroed out). | 319 // (which are already zeroed out). |
| 320 ANNOTATE_CHANGE_SIZE(buffer, length, 0, length); | 320 ANNOTATE_CHANGE_SIZE(buffer, length, 0, length); |
| 321 #endif | 321 #endif |
| 322 if (std::is_polymorphic<T>::value) { | 322 if (std::is_polymorphic<T>::value) { |
| 323 char* pointer = reinterpret_cast<char*>(buffer); | 323 char* pointer = reinterpret_cast<char*>(buffer); |
| 324 for (unsigned i = 0; i < length; ++i) { | 324 for (unsigned i = 0; i < length; ++i) { |
| 325 char* element = pointer + i * sizeof(T); | 325 char* element = pointer + i * sizeof(T); |
| 326 if (blink::vTableInitialized(element)) | 326 if (blink::vTableInitialized(element)) |
| 327 reinterpret_cast<T*>(element)->~T(); | 327 reinterpret_cast<T*>(element)->~T(); |
| 328 } | 328 } |
| 329 } else { | 329 } else { |
| 330 for (unsigned i = 0; i < length; ++i) { | 330 for (unsigned i = 0; i < length; ++i) { |
| 331 buffer[i].~T(); | 331 buffer[i].~T(); |
| 332 } | 332 } |
| 333 } | 333 } |
| 334 } | 334 } |
| 335 | 335 |
| 336 template <typename Table> | 336 template <typename Table> |
| 337 void HeapHashTableBacking<Table>::finalize(void* pointer) { | 337 void HeapHashTableBacking<Table>::finalize(void* pointer) { |
| 338 using Value = typename Table::ValueType; | 338 using Value = typename Table::ValueType; |
| 339 ASSERT(!WTF::IsTriviallyDestructible<Value>::value); | 339 DCHECK(!WTF::IsTriviallyDestructible<Value>::value); |
| 340 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer); | 340 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer); |
| 341 ASSERT(header->checkHeader()); | 341 DCHECK(header->checkHeader()); |
| 342 // Use the payload size as recorded by the heap to determine how many | 342 // Use the payload size as recorded by the heap to determine how many |
| 343 // elements to finalize. | 343 // elements to finalize. |
| 344 size_t length = header->payloadSize() / sizeof(Value); | 344 size_t length = header->payloadSize() / sizeof(Value); |
| 345 Value* table = reinterpret_cast<Value*>(pointer); | 345 Value* table = reinterpret_cast<Value*>(pointer); |
| 346 for (unsigned i = 0; i < length; ++i) { | 346 for (unsigned i = 0; i < length; ++i) { |
| 347 if (!Table::isEmptyOrDeletedBucket(table[i])) | 347 if (!Table::isEmptyOrDeletedBucket(table[i])) |
| 348 table[i].~Value(); | 348 table[i].~Value(); |
| 349 } | 349 } |
| 350 } | 350 } |
| 351 | 351 |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 const blink::HeapHashCountedSet<Value, HashFunctions, Traits>& set, | 832 const blink::HeapHashCountedSet<Value, HashFunctions, Traits>& set, |
| 833 VectorType& vector) { | 833 VectorType& vector) { |
| 834 copyToVector(static_cast<const HashCountedSet<Value, HashFunctions, Traits, | 834 copyToVector(static_cast<const HashCountedSet<Value, HashFunctions, Traits, |
| 835 blink::HeapAllocator>&>(set), | 835 blink::HeapAllocator>&>(set), |
| 836 vector); | 836 vector); |
| 837 } | 837 } |
| 838 | 838 |
| 839 } // namespace WTF | 839 } // namespace WTF |
| 840 | 840 |
| 841 #endif | 841 #endif |
| OLD | NEW |