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_DefaultAllocator_h | |
32 #define WTF_DefaultAllocator_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/PartitionAlloc.h" | |
41 #include "wtf/Partitions.h" | |
42 | |
43 #include <string.h> | |
44 | |
45 namespace WTF { | |
46 | |
47 class DefaultAllocatorDummyVisitor; | |
48 | |
49 class WTF_EXPORT DefaultAllocator { | |
50 public: | |
51 typedef DefaultAllocatorDummyVisitor Visitor; | |
52 static const bool isGarbageCollected = false; | |
53 | |
54 template<typename T> | |
55 static size_t quantizedSize(size_t count) | |
56 { | |
57 RELEASE_ASSERT(count <= kGenericMaxDirectMapped / sizeof(T)); | |
58 return partitionAllocActualSize(Partitions::bufferPartition(), count * s
izeof(T)); | |
59 } | |
60 template <typename T> | |
61 static T* allocateVectorBacking(size_t size) | |
62 { | |
63 return reinterpret_cast<T*>(allocateBacking(size)); | |
64 } | |
65 template <typename T> | |
66 static T* allocateExpandedVectorBacking(size_t size) | |
67 { | |
68 return reinterpret_cast<T*>(allocateBacking(size)); | |
69 } | |
70 static void freeVectorBacking(void* address); | |
71 static inline bool expandVectorBacking(void*, size_t) | |
72 { | |
73 return false; | |
74 } | |
75 static inline bool shrinkVectorBacking(void* address, size_t quantizedCurren
tSize, size_t quantizedShrunkSize) | |
76 { | |
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) { return allocateVectorBa
cking<T>(size); } | |
83 static inline void freeInlineVectorBacking(void* address) { freeVectorBackin
g(address); } | |
84 static inline bool expandInlineVectorBacking(void*, size_t) { return false;
} | |
85 static inline bool shrinkInlineVectorBacking(void* address, size_t quantized
CurrentSize, size_t quantizedShrunkSize) { return shrinkVectorBacking(address, q
uantizedCurrentSize, quantizedShrunkSize); } | |
86 | |
87 template <typename T, typename HashTable> | |
88 static T* allocateHashTableBacking(size_t size) | |
89 { | |
90 return reinterpret_cast<T*>(allocateBacking(size)); | |
91 } | |
92 template <typename T, typename HashTable> | |
93 static T* allocateZeroedHashTableBacking(size_t size) | |
94 { | |
95 void* result = allocateBacking(size); | |
96 memset(result, 0, size); | |
97 return reinterpret_cast<T*>(result); | |
98 } | |
99 static void freeHashTableBacking(void* address); | |
100 | |
101 template <typename Return, typename Metadata> | |
102 static Return malloc(size_t size) | |
103 { | |
104 return reinterpret_cast<Return>(Partitions::fastMalloc(size)); | |
105 } | |
106 | |
107 static inline bool expandHashTableBacking(void*, size_t) | |
108 { | |
109 return false; | |
110 } | |
111 static void free(void* address) | |
112 { | |
113 Partitions::fastFree(address); | |
114 } | |
115 template<typename T> | |
116 static void* newArray(size_t bytes) | |
117 { | |
118 return malloc<void*, void>(bytes); | |
119 } | |
120 static void | |
121 deleteArray(void* ptr) | |
122 { | |
123 free(ptr); // Not the system free, the one from this class. | |
124 } | |
125 | |
126 static bool isAllocationAllowed() { return true; } | |
127 template<typename T> | |
128 static bool isHeapObjectAlive(T* object) | |
129 { | |
130 ASSERT_NOT_REACHED(); | |
131 return false; | |
132 } | |
133 | |
134 static void markNoTracing(...) | |
135 { | |
136 ASSERT_NOT_REACHED(); | |
137 } | |
138 | |
139 static void registerDelayedMarkNoTracing(...) | |
140 { | |
141 ASSERT_NOT_REACHED(); | |
142 } | |
143 | |
144 static void registerWeakMembers(...) | |
145 { | |
146 ASSERT_NOT_REACHED(); | |
147 } | |
148 | |
149 static void registerWeakTable(...) | |
150 { | |
151 ASSERT_NOT_REACHED(); | |
152 } | |
153 | |
154 #if ENABLE(ASSERT) | |
155 static bool weakTableRegistered(...) | |
156 { | |
157 ASSERT_NOT_REACHED(); | |
158 return false; | |
159 } | |
160 #endif | |
161 | |
162 template<typename T, typename Traits> | |
163 static void trace(...) | |
164 { | |
165 ASSERT_NOT_REACHED(); | |
166 } | |
167 | |
168 template<typename T> | |
169 struct OtherType { | |
170 typedef T* Type; | |
171 }; | |
172 | |
173 template<typename T> | |
174 static T& getOther(T* other) | |
175 { | |
176 return *other; | |
177 } | |
178 | |
179 static void enterGCForbiddenScope() { } | |
180 static void leaveGCForbiddenScope() { } | |
181 | |
182 private: | |
183 static void* allocateBacking(size_t); | |
184 }; | |
185 | |
186 // The Windows compiler seems to be very eager to instantiate things it won't | |
187 // need, so unless we have this class we get compile errors. | |
188 class DefaultAllocatorDummyVisitor { | |
189 public: | |
190 template<typename T> inline bool isHeapObjectAlive(T obj) | |
191 { | |
192 ASSERT_NOT_REACHED(); | |
193 return false; | |
194 } | |
195 }; | |
196 | |
197 } // namespace WTF | |
198 | |
199 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \ | |
200 public: \ | |
201 void* operator new(size_t size) \ | |
202 { \ | |
203 return Allocator::template malloc<void*, ClassName>(size); \ | |
204 } \ | |
205 void operator delete(void* p) { Allocator::free(p); } \ | |
206 void* operator new[](size_t size) { return Allocator::template newArray<Clas
sName>(size); } \ | |
207 void operator delete[](void* p) { Allocator::deleteArray(p); } \ | |
208 void* operator new(size_t, NotNullTag, void* location) \ | |
209 { \ | |
210 ASSERT(location); \ | |
211 return location; \ | |
212 } \ | |
213 private: \ | |
214 typedef int __thisIsHereToForceASemicolonAfterThisMacro | |
215 | |
216 using WTF::DefaultAllocator; | |
217 | |
218 #endif // WTF_DefaultAllocator_h | |
OLD | NEW |