| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 WTF_PartitionAllocator_h | 5 #include "platform/wtf/allocator/PartitionAllocator.h" |
| 6 #define WTF_PartitionAllocator_h | |
| 7 | 6 |
| 8 // This is the allocator that is used for allocations that are not on the | 7 // The contents of this header was moved to platform/wtf as part of |
| 9 // traced, garbage collected heap. It uses FastMalloc for collections, | 8 // WTF migration project. See the following post for details: |
| 10 // but uses the partition allocator for the backing store of the collections. | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 11 | |
| 12 #include "base/allocator/partition_allocator/partition_alloc.h" | |
| 13 #include "third_party/WebKit/Source/wtf/Allocator.h" | |
| 14 #include "wtf/Assertions.h" | |
| 15 #include "wtf/TypeTraits.h" | |
| 16 #include "wtf/WTFExport.h" | |
| 17 #include <string.h> | |
| 18 | |
| 19 namespace WTF { | |
| 20 | |
| 21 class PartitionAllocatorDummyVisitor { | |
| 22 DISALLOW_NEW(); | |
| 23 }; | |
| 24 | |
| 25 class WTF_EXPORT PartitionAllocator { | |
| 26 public: | |
| 27 typedef PartitionAllocatorDummyVisitor Visitor; | |
| 28 static const bool isGarbageCollected = false; | |
| 29 | |
| 30 template<typename T> | |
| 31 static size_t maxElementCountInBackingStore() { | |
| 32 return base::kGenericMaxDirectMapped / sizeof(T); | |
| 33 } | |
| 34 | |
| 35 template <typename T> | |
| 36 static size_t quantizedSize(size_t count) { | |
| 37 CHECK_LE(count, maxElementCountInBackingStore<T>()); | |
| 38 return PartitionAllocActualSize(WTF::Partitions::bufferPartition(), | |
| 39 count * sizeof(T)); | |
| 40 } | |
| 41 template <typename T> | |
| 42 static T* allocateVectorBacking(size_t size) { | |
| 43 return reinterpret_cast<T*>( | |
| 44 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | |
| 45 } | |
| 46 template <typename T> | |
| 47 static T* allocateExpandedVectorBacking(size_t size) { | |
| 48 return reinterpret_cast<T*>( | |
| 49 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | |
| 50 } | |
| 51 static void freeVectorBacking(void* address); | |
| 52 static inline bool expandVectorBacking(void*, size_t) { return false; } | |
| 53 static inline bool shrinkVectorBacking(void* address, | |
| 54 size_t quantizedCurrentSize, | |
| 55 size_t quantizedShrunkSize) { | |
| 56 // Optimization: if we're downsizing inside the same allocator bucket, | |
| 57 // we can skip reallocation. | |
| 58 return quantizedCurrentSize == quantizedShrunkSize; | |
| 59 } | |
| 60 template <typename T> | |
| 61 static T* allocateInlineVectorBacking(size_t size) { | |
| 62 return allocateVectorBacking<T>(size); | |
| 63 } | |
| 64 static inline void freeInlineVectorBacking(void* address) { | |
| 65 freeVectorBacking(address); | |
| 66 } | |
| 67 static inline bool expandInlineVectorBacking(void*, size_t) { return false; } | |
| 68 static inline bool shrinkInlineVectorBacking(void* address, | |
| 69 size_t quantizedCurrentSize, | |
| 70 size_t quantizedShrunkSize) { | |
| 71 return shrinkVectorBacking(address, quantizedCurrentSize, | |
| 72 quantizedShrunkSize); | |
| 73 } | |
| 74 | |
| 75 template <typename T, typename HashTable> | |
| 76 static T* allocateHashTableBacking(size_t size) { | |
| 77 return reinterpret_cast<T*>( | |
| 78 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | |
| 79 } | |
| 80 template <typename T, typename HashTable> | |
| 81 static T* allocateZeroedHashTableBacking(size_t size) { | |
| 82 void* result = allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T)); | |
| 83 memset(result, 0, size); | |
| 84 return reinterpret_cast<T*>(result); | |
| 85 } | |
| 86 static void freeHashTableBacking(void* address); | |
| 87 | |
| 88 template <typename Return, typename Metadata> | |
| 89 static Return malloc(size_t size, const char* typeName) { | |
| 90 return reinterpret_cast<Return>( | |
| 91 WTF::Partitions::fastMalloc(size, typeName)); | |
| 92 } | |
| 93 | |
| 94 static inline bool expandHashTableBacking(void*, size_t) { return false; } | |
| 95 static void free(void* address) { WTF::Partitions::fastFree(address); } | |
| 96 template <typename T> | |
| 97 static void* newArray(size_t bytes) { | |
| 98 return malloc<void*, void>(bytes, WTF_HEAP_PROFILER_TYPE_NAME(T)); | |
| 99 } | |
| 100 static void deleteArray(void* ptr) { | |
| 101 free(ptr); // Not the system free, the one from this class. | |
| 102 } | |
| 103 | |
| 104 static bool isAllocationAllowed() { return true; } | |
| 105 | |
| 106 static void enterGCForbiddenScope() {} | |
| 107 static void leaveGCForbiddenScope() {} | |
| 108 | |
| 109 private: | |
| 110 static void* allocateBacking(size_t, const char* typeName); | |
| 111 }; | |
| 112 | |
| 113 // Specializations for heap profiling, so type profiling of |char| is possible | |
| 114 // even in official builds (because |char| makes up a large portion of the | |
| 115 // heap.) | |
| 116 template <> | |
| 117 WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t); | |
| 118 template <> | |
| 119 WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>( | |
| 120 size_t); | |
| 121 | |
| 122 } // namespace WTF | |
| 123 | |
| 124 #define USE_ALLOCATOR(ClassName, Allocator) \ | |
| 125 public: \ | |
| 126 void* operator new(size_t size) { \ | |
| 127 return Allocator::template malloc<void*, ClassName>( \ | |
| 128 size, WTF_HEAP_PROFILER_TYPE_NAME(ClassName)); \ | |
| 129 } \ | |
| 130 void operator delete(void* p) { Allocator::free(p); } \ | |
| 131 void* operator new[](size_t size) { \ | |
| 132 return Allocator::template newArray<ClassName>(size); \ | |
| 133 } \ | |
| 134 void operator delete[](void* p) { Allocator::deleteArray(p); } \ | |
| 135 void* operator new(size_t, NotNullTag, void* location) { \ | |
| 136 DCHECK(location); \ | |
| 137 return location; \ | |
| 138 } \ | |
| 139 void* operator new(size_t, void* location) { return location; } \ | |
| 140 \ | |
| 141 private: \ | |
| 142 typedef int __thisIsHereToForceASemicolonAfterThisMacro | |
| 143 | |
| 144 #endif // WTF_PartitionAllocator_h | |
| OLD | NEW |