Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: third_party/WebKit/Source/platform/heap/HeapAllocator.h

Issue 1985473002: Simplify HeapVectorBacking<> usage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/TraceTraits.h" 9 #include "platform/heap/TraceTraits.h"
10 #include "wtf/Allocator.h" 10 #include "wtf/Allocator.h"
11 #include "wtf/Assertions.h" 11 #include "wtf/Assertions.h"
12 #include "wtf/Deque.h" 12 #include "wtf/Deque.h"
13 #include "wtf/HashCountedSet.h" 13 #include "wtf/HashCountedSet.h"
14 #include "wtf/HashMap.h" 14 #include "wtf/HashMap.h"
15 #include "wtf/HashSet.h" 15 #include "wtf/HashSet.h"
16 #include "wtf/HashTable.h" 16 #include "wtf/HashTable.h"
17 #include "wtf/LinkedHashSet.h" 17 #include "wtf/LinkedHashSet.h"
18 #include "wtf/ListHashSet.h" 18 #include "wtf/ListHashSet.h"
19 #include "wtf/TypeTraits.h" 19 #include "wtf/TypeTraits.h"
20 #include "wtf/Vector.h" 20 #include "wtf/Vector.h"
21 21
22 namespace blink { 22 namespace blink {
23 23
24 template<typename T, typename Traits = WTF::VectorTraits<T>> class HeapVectorBac king {
25 DISALLOW_NEW();
26 public:
27 static void finalize(void* pointer);
28 void finalizeGarbageCollectedObject() { finalize(this); }
29 };
30
31 template<typename Table> class HeapHashTableBacking {
32 DISALLOW_NEW();
33 public:
34 static void finalize(void* pointer);
35 void finalizeGarbageCollectedObject() { finalize(this); }
36 };
37
24 // This is a static-only class used as a trait on collections to make them heap 38 // This is a static-only class used as a trait on collections to make them heap
25 // allocated. However see also HeapListHashSetAllocator. 39 // allocated. However see also HeapListHashSetAllocator.
26 class PLATFORM_EXPORT HeapAllocator { 40 class PLATFORM_EXPORT HeapAllocator {
27 STATIC_ONLY(HeapAllocator); 41 STATIC_ONLY(HeapAllocator);
28 public: 42 public:
29 using Visitor = blink::Visitor; 43 using Visitor = blink::Visitor;
30 static const bool isGarbageCollected = true; 44 static const bool isGarbageCollected = true;
31 45
32 template<typename T> 46 template<typename T>
33 static size_t quantizedSize(size_t count) 47 static size_t quantizedSize(size_t count)
34 { 48 {
35 RELEASE_ASSERT(count <= maxHeapObjectSize / sizeof(T)); 49 RELEASE_ASSERT(count <= maxHeapObjectSize / sizeof(T));
36 return ThreadHeap::allocationSizeFromSize(count * sizeof(T)) - sizeof(He apObjectHeader); 50 return ThreadHeap::allocationSizeFromSize(count * sizeof(T)) - sizeof(He apObjectHeader);
37 } 51 }
38 template <typename T> 52 template <typename T>
39 static T* allocateVectorBacking(size_t size) 53 static T* allocateVectorBacking(size_t size)
40 { 54 {
41 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state( ); 55 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state( );
42 ASSERT(state->isAllocationAllowed()); 56 ASSERT(state->isAllocationAllowed());
43 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T, VectorTraits<T>>>: :index(); 57 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T>>::index();
44 NormalPageArena* arena = static_cast<NormalPageArena*>(state->vectorBack ingArena(gcInfoIndex)); 58 NormalPageArena* arena = static_cast<NormalPageArena*>(state->vectorBack ingArena(gcInfoIndex));
45 return reinterpret_cast<T*>(arena->allocateObject(ThreadHeap::allocation SizeFromSize(size), gcInfoIndex)); 59 return reinterpret_cast<T*>(arena->allocateObject(ThreadHeap::allocation SizeFromSize(size), gcInfoIndex));
46 } 60 }
47 template <typename T> 61 template <typename T>
48 static T* allocateExpandedVectorBacking(size_t size) 62 static T* allocateExpandedVectorBacking(size_t size)
49 { 63 {
50 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state( ); 64 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state( );
51 ASSERT(state->isAllocationAllowed()); 65 ASSERT(state->isAllocationAllowed());
52 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T, VectorTraits<T>>>: :index(); 66 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T>>::index();
53 NormalPageArena* arena = static_cast<NormalPageArena*>(state->expandedVe ctorBackingArena(gcInfoIndex)); 67 NormalPageArena* arena = static_cast<NormalPageArena*>(state->expandedVe ctorBackingArena(gcInfoIndex));
54 return reinterpret_cast<T*>(arena->allocateObject(ThreadHeap::allocation SizeFromSize(size), gcInfoIndex)); 68 return reinterpret_cast<T*>(arena->allocateObject(ThreadHeap::allocation SizeFromSize(size), gcInfoIndex));
55 } 69 }
56 static void freeVectorBacking(void*); 70 static void freeVectorBacking(void*);
57 static bool expandVectorBacking(void*, size_t); 71 static bool expandVectorBacking(void*, size_t);
58 static bool shrinkVectorBacking(void* address, size_t quantizedCurrentSize, size_t quantizedShrunkSize); 72 static bool shrinkVectorBacking(void* address, size_t quantizedCurrentSize, size_t quantizedShrunkSize);
59 template <typename T> 73 template <typename T>
60 static T* allocateInlineVectorBacking(size_t size) 74 static T* allocateInlineVectorBacking(size_t size)
61 { 75 {
62 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T, VectorTraits<T>>>: :index(); 76 size_t gcInfoIndex = GCInfoTrait<HeapVectorBacking<T>>::index();
63 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state( ); 77 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state( );
64 #define COMMA , 78 const char* typeName = WTF_HEAP_PROFILER_TYPE_NAME(HeapVectorBacking<T>) ;
65 const char* typeName = WTF_HEAP_PROFILER_TYPE_NAME(HeapVectorBacking<T C OMMA VectorTraits<T>>);
66 #undef COMMA
67 return reinterpret_cast<T*>(ThreadHeap::allocateOnArenaIndex(state, size , BlinkGC::InlineVectorArenaIndex, gcInfoIndex, typeName)); 79 return reinterpret_cast<T*>(ThreadHeap::allocateOnArenaIndex(state, size , BlinkGC::InlineVectorArenaIndex, gcInfoIndex, typeName));
68 } 80 }
69 static void freeInlineVectorBacking(void*); 81 static void freeInlineVectorBacking(void*);
70 static bool expandInlineVectorBacking(void*, size_t); 82 static bool expandInlineVectorBacking(void*, size_t);
71 static bool shrinkInlineVectorBacking(void* address, size_t quantizedCurrent Size, size_t quantizedShrunkSize); 83 static bool shrinkInlineVectorBacking(void* address, size_t quantizedCurrent Size, size_t quantizedShrunkSize);
72 84
73 template <typename T, typename HashTable> 85 template <typename T, typename HashTable>
74 static T* allocateHashTableBacking(size_t size) 86 static T* allocateHashTableBacking(size_t size)
75 { 87 {
76 size_t gcInfoIndex = GCInfoTrait<HeapHashTableBacking<HashTable>>::index (); 88 size_t gcInfoIndex = GCInfoTrait<HeapHashTableBacking<HashTable>>::index ();
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 return malloc<void*, Node>(sizeof(Node), nullptr /* Oilpan does not use the heap profiler at the moment. */); 251 return malloc<void*, Node>(sizeof(Node), nullptr /* Oilpan does not use the heap profiler at the moment. */);
240 } 252 }
241 253
242 template<typename VisitorDispatcher> 254 template<typename VisitorDispatcher>
243 static void traceValue(VisitorDispatcher visitor, Node* node) 255 static void traceValue(VisitorDispatcher visitor, Node* node)
244 { 256 {
245 traceListHashSetValue(visitor, node->m_value); 257 traceListHashSetValue(visitor, node->m_value);
246 } 258 }
247 }; 259 };
248 260
249 template<typename T, typename Traits = WTF::VectorTraits<T>> class HeapVectorBac king {
250 DISALLOW_NEW();
251 public:
252 static void finalize(void* pointer);
253 void finalizeGarbageCollectedObject() { finalize(this); }
254 };
255
256 template<typename T, typename Traits> 261 template<typename T, typename Traits>
257 void HeapVectorBacking<T, Traits>::finalize(void* pointer) 262 void HeapVectorBacking<T, Traits>::finalize(void* pointer)
258 { 263 {
259 static_assert(Traits::needsDestruction, "Only vector buffers with items requ iring destruction should be finalized"); 264 static_assert(Traits::needsDestruction, "Only vector buffers with items requ iring destruction should be finalized");
260 // See the comment in HeapVectorBacking::trace. 265 // See the comment in HeapVectorBacking::trace.
261 static_assert(Traits::canClearUnusedSlotsWithMemset || std::is_polymorphic<T >::value, "HeapVectorBacking doesn't support objects that cannot be cleared as u nused with memset or don't have a vtable"); 266 static_assert(Traits::canClearUnusedSlotsWithMemset || std::is_polymorphic<T >::value, "HeapVectorBacking doesn't support objects that cannot be cleared as u nused with memset or don't have a vtable");
262 267
263 ASSERT(!WTF::IsTriviallyDestructible<T>::value); 268 ASSERT(!WTF::IsTriviallyDestructible<T>::value);
264 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer); 269 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer);
265 ASSERT(header->checkHeader()); 270 ASSERT(header->checkHeader());
(...skipping 11 matching lines...) Expand all
277 if (blink::vTableInitialized(&buffer[i])) 282 if (blink::vTableInitialized(&buffer[i]))
278 buffer[i].~T(); 283 buffer[i].~T();
279 } 284 }
280 } else { 285 } else {
281 for (unsigned i = 0; i < length; ++i) { 286 for (unsigned i = 0; i < length; ++i) {
282 buffer[i].~T(); 287 buffer[i].~T();
283 } 288 }
284 } 289 }
285 } 290 }
286 291
287 template<typename Table> class HeapHashTableBacking {
288 DISALLOW_NEW();
289 public:
290 static void finalize(void* pointer);
291 void finalizeGarbageCollectedObject() { finalize(this); }
292 };
293
294 template<typename Table> 292 template<typename Table>
295 void HeapHashTableBacking<Table>::finalize(void* pointer) 293 void HeapHashTableBacking<Table>::finalize(void* pointer)
296 { 294 {
297 using Value = typename Table::ValueType; 295 using Value = typename Table::ValueType;
298 ASSERT(!WTF::IsTriviallyDestructible<Value>::value); 296 ASSERT(!WTF::IsTriviallyDestructible<Value>::value);
299 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer); 297 HeapObjectHeader* header = HeapObjectHeader::fromPayload(pointer);
300 ASSERT(header->checkHeader()); 298 ASSERT(header->checkHeader());
301 // Use the payload size as recorded by the heap to determine how many 299 // Use the payload size as recorded by the heap to determine how many
302 // elements to finalize. 300 // elements to finalize.
303 size_t length = header->payloadSize() / sizeof(Value); 301 size_t length = header->payloadSize() / sizeof(Value);
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 static_assert(sizeof(T), "T must be fully defined"); 566 static_assert(sizeof(T), "T must be fully defined");
569 // All heap allocated node pointers need visiting to keep the nodes alive, 567 // All heap allocated node pointers need visiting to keep the nodes alive,
570 // regardless of whether they contain pointers to other heap allocated 568 // regardless of whether they contain pointers to other heap allocated
571 // objects. 569 // objects.
572 static const bool value = true; 570 static const bool value = true;
573 }; 571 };
574 572
575 } // namespace WTF 573 } // namespace WTF
576 574
577 #endif 575 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698