| OLD | NEW |
| 1 /* | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * | 3 // found in the LICENSE file. |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | 4 |
| 31 #ifndef WTF_PartitionAllocator_h | 5 #ifndef WTF_PartitionAllocator_h |
| 32 #define WTF_PartitionAllocator_h | 6 #define WTF_PartitionAllocator_h |
| 33 | 7 |
| 34 // This is the allocator that is used for allocations that are not on the | 8 // This is the allocator that is used for allocations that are not on the |
| 35 // traced, garbage collected heap. It uses FastMalloc for collections, | 9 // traced, garbage collected heap. It uses FastMalloc for collections, |
| 36 // but uses the partition allocator for the backing store of the collections. | 10 // but uses the partition allocator for the backing store of the collections. |
| 37 | 11 |
| 38 #include "wtf/Allocator.h" | 12 #include "base/allocator/partition_allocator/partition_alloc.h" |
| 39 #include "wtf/Assertions.h" | 13 #include "third_party/WebKit/Source/wtf/Allocator.h" |
| 40 #include "wtf/allocator/PartitionAlloc.h" | 14 #include "wtf/TypeTraits.h" |
| 41 #include "wtf/allocator/Partitions.h" | 15 #include "wtf/WTFExport.h" |
| 42 | |
| 43 #include <string.h> | 16 #include <string.h> |
| 44 | 17 |
| 45 namespace WTF { | 18 namespace WTF { |
| 46 | 19 |
| 47 class PartitionAllocatorDummyVisitor { | 20 class PartitionAllocatorDummyVisitor { |
| 48 DISALLOW_NEW(); | 21 DISALLOW_NEW(); |
| 49 }; | 22 }; |
| 50 | 23 |
| 51 class WTF_EXPORT PartitionAllocator { | 24 class WTF_EXPORT PartitionAllocator { |
| 52 public: | 25 public: |
| 53 typedef PartitionAllocatorDummyVisitor Visitor; | 26 typedef PartitionAllocatorDummyVisitor Visitor; |
| 54 static const bool isGarbageCollected = false; | 27 static const bool isGarbageCollected = false; |
| 55 | 28 |
| 56 template <typename T> | 29 template <typename T> |
| 57 static size_t quantizedSize(size_t count) { | 30 static size_t quantizedSize(size_t count) { |
| 58 RELEASE_ASSERT(count <= kGenericMaxDirectMapped / sizeof(T)); | 31 CHECK(count <= base::kGenericMaxDirectMapped / sizeof(T)); |
| 59 return partitionAllocActualSize(Partitions::bufferPartition(), | 32 return partitionAllocActualSize(WTF::Partitions::bufferPartition(), |
| 60 count * sizeof(T)); | 33 count * sizeof(T)); |
| 61 } | 34 } |
| 62 template <typename T> | 35 template <typename T> |
| 63 static T* allocateVectorBacking(size_t size) { | 36 static T* allocateVectorBacking(size_t size) { |
| 64 return reinterpret_cast<T*>( | 37 return reinterpret_cast<T*>( |
| 65 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | 38 allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T))); |
| 66 } | 39 } |
| 67 template <typename T> | 40 template <typename T> |
| 68 static T* allocateExpandedVectorBacking(size_t size) { | 41 static T* allocateExpandedVectorBacking(size_t size) { |
| 69 return reinterpret_cast<T*>( | 42 return reinterpret_cast<T*>( |
| 70 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | 43 allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T))); |
| 71 } | 44 } |
| 72 static void freeVectorBacking(void* address); | 45 static void freeVectorBacking(void* address); |
| 73 static inline bool expandVectorBacking(void*, size_t) { return false; } | 46 static inline bool expandVectorBacking(void*, size_t) { return false; } |
| 74 static inline bool shrinkVectorBacking(void* address, | 47 static inline bool shrinkVectorBacking(void* address, |
| 75 size_t quantizedCurrentSize, | 48 size_t quantizedCurrentSize, |
| 76 size_t quantizedShrunkSize) { | 49 size_t quantizedShrunkSize) { |
| 77 // Optimization: if we're downsizing inside the same allocator bucket, | 50 // Optimization: if we're downsizing inside the same allocator bucket, |
| 78 // we can skip reallocation. | 51 // we can skip reallocation. |
| 79 return quantizedCurrentSize == quantizedShrunkSize; | 52 return quantizedCurrentSize == quantizedShrunkSize; |
| 80 } | 53 } |
| 81 template <typename T> | 54 template <typename T> |
| 82 static T* allocateInlineVectorBacking(size_t size) { | 55 static T* allocateInlineVectorBacking(size_t size) { |
| 83 return allocateVectorBacking<T>(size); | 56 return allocateVectorBacking<T>(size); |
| 84 } | 57 } |
| 85 static inline void freeInlineVectorBacking(void* address) { | 58 static inline void freeInlineVectorBacking(void* address) { |
| 86 freeVectorBacking(address); | 59 freeVectorBacking(address); |
| 87 } | 60 } |
| 88 static inline bool expandInlineVectorBacking(void*, size_t) { return false; } | 61 static inline bool expandInlineVectorBacking(void*, size_t) { return false; } |
| 89 static inline bool shrinkInlineVectorBacking(void* address, | 62 static inline bool shrinkInlineVectorBacking(void* address, |
| 90 size_t quantizedCurrentSize, | 63 size_t quantizedCurrentSize, |
| 91 size_t quantizedShrunkSize) { | 64 size_t quantizedShrunkSize) { |
| 92 return shrinkVectorBacking(address, quantizedCurrentSize, | 65 return shrinkVectorBacking(address, quantizedCurrentSize, |
| 93 quantizedShrunkSize); | 66 quantizedShrunkSize); |
| 94 } | 67 } |
| 95 | 68 |
| 96 template <typename T, typename HashTable> | 69 template <typename T, typename HashTable> |
| 97 static T* allocateHashTableBacking(size_t size) { | 70 static T* allocateHashTableBacking(size_t size) { |
| 98 return reinterpret_cast<T*>( | 71 return reinterpret_cast<T*>( |
| 99 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | 72 allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T))); |
| 100 } | 73 } |
| 101 template <typename T, typename HashTable> | 74 template <typename T, typename HashTable> |
| 102 static T* allocateZeroedHashTableBacking(size_t size) { | 75 static T* allocateZeroedHashTableBacking(size_t size) { |
| 103 void* result = allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T)); | 76 void* result = allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T)); |
| 104 memset(result, 0, size); | 77 memset(result, 0, size); |
| 105 return reinterpret_cast<T*>(result); | 78 return reinterpret_cast<T*>(result); |
| 106 } | 79 } |
| 107 static void freeHashTableBacking(void* address); | 80 static void freeHashTableBacking(void* address); |
| 108 | 81 |
| 109 template <typename Return, typename Metadata> | 82 template <typename Return, typename Metadata> |
| 110 static Return malloc(size_t size, const char* typeName) { | 83 static Return malloc(size_t size, const char* typeName) { |
| 111 return reinterpret_cast<Return>(Partitions::fastMalloc(size, typeName)); | 84 return reinterpret_cast<Return>( |
| 85 WTF::Partitions::fastMalloc(size, typeName)); |
| 112 } | 86 } |
| 113 | 87 |
| 114 static inline bool expandHashTableBacking(void*, size_t) { return false; } | 88 static inline bool expandHashTableBacking(void*, size_t) { return false; } |
| 115 static void free(void* address) { Partitions::fastFree(address); } | 89 static void free(void* address) { WTF::Partitions::fastFree(address); } |
| 116 template <typename T> | 90 template <typename T> |
| 117 static void* newArray(size_t bytes) { | 91 static void* newArray(size_t bytes) { |
| 118 return malloc<void*, void>(bytes, WTF_HEAP_PROFILER_TYPE_NAME(T)); | 92 return malloc<void*, void>(bytes, PARTITION_HEAP_PROFILER_TYPE_NAME(T)); |
| 119 } | 93 } |
| 120 static void deleteArray(void* ptr) { | 94 static void deleteArray(void* ptr) { |
| 121 free(ptr); // Not the system free, the one from this class. | 95 free(ptr); // Not the system free, the one from this class. |
| 122 } | 96 } |
| 123 | 97 |
| 124 static bool isAllocationAllowed() { return true; } | 98 static bool isAllocationAllowed() { return true; } |
| 125 | 99 |
| 126 static void enterGCForbiddenScope() {} | 100 static void enterGCForbiddenScope() {} |
| 127 static void leaveGCForbiddenScope() {} | 101 static void leaveGCForbiddenScope() {} |
| 128 | 102 |
| 129 private: | 103 private: |
| 130 static void* allocateBacking(size_t, const char* typeName); | 104 static void* allocateBacking(size_t, const char* typeName); |
| 131 }; | 105 }; |
| 132 | 106 |
| 133 // Specializations for heap profiling, so type profiling of |char| is possible | 107 // Specializations for heap profiling, so type profiling of |char| is possible |
| 134 // even in official builds (because |char| makes up a large portion of the | 108 // even in official builds (because |char| makes up a large portion of the |
| 135 // heap.) | 109 // heap.) |
| 136 template <> | 110 template <> |
| 137 WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t); | 111 WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t); |
| 138 template <> | 112 template <> |
| 139 WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>( | 113 WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>( |
| 140 size_t); | 114 size_t); |
| 141 | 115 |
| 142 } // namespace WTF | 116 } // namespace WTF |
| 143 | 117 |
| 144 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \ | 118 #define USE_ALLOCATOR(ClassName, Allocator) \ |
| 145 public: \ | 119 public: \ |
| 146 void* operator new(size_t size) { \ | 120 void* operator new(size_t size) { \ |
| 147 return Allocator::template malloc<void*, ClassName>( \ | 121 return Allocator::template malloc<void*, ClassName>( \ |
| 148 size, WTF_HEAP_PROFILER_TYPE_NAME(ClassName)); \ | 122 size, PARTITION_HEAP_PROFILER_TYPE_NAME(ClassName)); \ |
| 149 } \ | 123 } \ |
| 150 void operator delete(void* p) { Allocator::free(p); } \ | 124 void operator delete(void* p) { Allocator::free(p); } \ |
| 151 void* operator new[](size_t size) { \ | 125 void* operator new[](size_t size) { \ |
| 152 return Allocator::template newArray<ClassName>(size); \ | 126 return Allocator::template newArray<ClassName>(size); \ |
| 153 } \ | 127 } \ |
| 154 void operator delete[](void* p) { Allocator::deleteArray(p); } \ | 128 void operator delete[](void* p) { Allocator::deleteArray(p); } \ |
| 155 void* operator new(size_t, NotNullTag, void* location) { \ | 129 void* operator new(size_t, NotNullTag, void* location) { \ |
| 156 ASSERT(location); \ | 130 ASSERT(location); \ |
| 157 return location; \ | 131 return location; \ |
| 158 } \ | 132 } \ |
| 159 void* operator new(size_t, void* location) { return location; } \ | 133 void* operator new(size_t, void* location) { return location; } \ |
| 160 \ | 134 \ |
| 161 private: \ | 135 private: \ |
| 162 typedef int __thisIsHereToForceASemicolonAfterThisMacro | 136 typedef int __thisIsHereToForceASemicolonAfterThisMacro |
| 163 | 137 |
| 164 using WTF::PartitionAllocator; | |
| 165 | |
| 166 #endif // WTF_PartitionAllocator_h | 138 #endif // WTF_PartitionAllocator_h |
| OLD | NEW |