OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
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 | |
31 #ifndef WTF_PartitionAllocator_h | |
32 #define WTF_PartitionAllocator_h | |
33 | |
34 // This is the allocator that is used for allocations that are not on the | |
35 // traced, garbage collected heap. It uses FastMalloc for collections, | |
36 // but uses the partition allocator for the backing store of the collections. | |
37 | |
38 #include "wtf/Allocator.h" | |
39 #include "wtf/Assertions.h" | |
40 #include "wtf/allocator/PartitionAlloc.h" | |
41 #include "wtf/allocator/Partitions.h" | |
42 | |
43 #include <string.h> | |
44 | |
45 namespace WTF { | |
46 | |
47 class PartitionAllocatorDummyVisitor { | |
48 DISALLOW_NEW(); | |
49 }; | |
50 | |
51 class WTF_EXPORT PartitionAllocator { | |
52 public: | |
53 typedef PartitionAllocatorDummyVisitor Visitor; | |
54 static const bool isGarbageCollected = false; | |
55 | |
56 template <typename T> | |
57 static size_t quantizedSize(size_t count) { | |
58 RELEASE_ASSERT(count <= kGenericMaxDirectMapped / sizeof(T)); | |
59 return partitionAllocActualSize(Partitions::bufferPartition(), | |
60 count * sizeof(T)); | |
61 } | |
62 template <typename T> | |
63 static T* allocateVectorBacking(size_t size) { | |
64 return reinterpret_cast<T*>( | |
65 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | |
66 } | |
67 template <typename T> | |
68 static T* allocateExpandedVectorBacking(size_t size) { | |
69 return reinterpret_cast<T*>( | |
70 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | |
71 } | |
72 static void freeVectorBacking(void* address); | |
73 static inline bool expandVectorBacking(void*, size_t) { return false; } | |
74 static inline bool shrinkVectorBacking(void* address, | |
75 size_t quantizedCurrentSize, | |
76 size_t quantizedShrunkSize) { | |
77 // Optimization: if we're downsizing inside the same allocator bucket, | |
78 // we can skip reallocation. | |
79 return quantizedCurrentSize == quantizedShrunkSize; | |
80 } | |
81 template <typename T> | |
82 static T* allocateInlineVectorBacking(size_t size) { | |
83 return allocateVectorBacking<T>(size); | |
84 } | |
85 static inline void freeInlineVectorBacking(void* address) { | |
86 freeVectorBacking(address); | |
87 } | |
88 static inline bool expandInlineVectorBacking(void*, size_t) { return false; } | |
89 static inline bool shrinkInlineVectorBacking(void* address, | |
90 size_t quantizedCurrentSize, | |
91 size_t quantizedShrunkSize) { | |
92 return shrinkVectorBacking(address, quantizedCurrentSize, | |
93 quantizedShrunkSize); | |
94 } | |
95 | |
96 template <typename T, typename HashTable> | |
97 static T* allocateHashTableBacking(size_t size) { | |
98 return reinterpret_cast<T*>( | |
99 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | |
100 } | |
101 template <typename T, typename HashTable> | |
102 static T* allocateZeroedHashTableBacking(size_t size) { | |
103 void* result = allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T)); | |
104 memset(result, 0, size); | |
105 return reinterpret_cast<T*>(result); | |
106 } | |
107 static void freeHashTableBacking(void* address); | |
108 | |
109 template <typename Return, typename Metadata> | |
110 static Return malloc(size_t size, const char* typeName) { | |
111 return reinterpret_cast<Return>(Partitions::fastMalloc(size, typeName)); | |
112 } | |
113 | |
114 static inline bool expandHashTableBacking(void*, size_t) { return false; } | |
115 static void free(void* address) { Partitions::fastFree(address); } | |
116 template <typename T> | |
117 static void* newArray(size_t bytes) { | |
118 return malloc<void*, void>(bytes, WTF_HEAP_PROFILER_TYPE_NAME(T)); | |
119 } | |
120 static void deleteArray(void* ptr) { | |
121 free(ptr); // Not the system free, the one from this class. | |
122 } | |
123 | |
124 static bool isAllocationAllowed() { return true; } | |
125 | |
126 static void enterGCForbiddenScope() {} | |
127 static void leaveGCForbiddenScope() {} | |
128 | |
129 private: | |
130 static void* allocateBacking(size_t, const char* typeName); | |
131 }; | |
132 | |
133 // 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 | |
135 // heap.) | |
136 template <> | |
137 WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t); | |
138 template <> | |
139 WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>( | |
140 size_t); | |
141 | |
142 } // namespace WTF | |
143 | |
144 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \ | |
145 public: \ | |
146 void* operator new(size_t size) { \ | |
147 return Allocator::template malloc<void*, ClassName>( \ | |
148 size, WTF_HEAP_PROFILER_TYPE_NAME(ClassName)); \ | |
149 } \ | |
150 void operator delete(void* p) { Allocator::free(p); } \ | |
151 void* operator new[](size_t size) { \ | |
152 return Allocator::template newArray<ClassName>(size); \ | |
153 } \ | |
154 void operator delete[](void* p) { Allocator::deleteArray(p); } \ | |
155 void* operator new(size_t, NotNullTag, void* location) { \ | |
156 ASSERT(location); \ | |
157 return location; \ | |
158 } \ | |
159 void* operator new(size_t, void* location) { return location; } \ | |
160 \ | |
161 private: \ | |
162 typedef int __thisIsHereToForceASemicolonAfterThisMacro | |
163 | |
164 using WTF::PartitionAllocator; | |
165 | |
166 #endif // WTF_PartitionAllocator_h | |
OLD | NEW |