OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project 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 V8_ZONE_H_ | 5 #ifndef V8_ZONE_H_ |
6 #define V8_ZONE_H_ | 6 #define V8_ZONE_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // the Zone supports deallocating all chunks in one fast | 26 // the Zone supports deallocating all chunks in one fast |
27 // operation. The Zone is used to hold temporary data structures like | 27 // operation. The Zone is used to hold temporary data structures like |
28 // the abstract syntax tree, which is deallocated after compilation. | 28 // the abstract syntax tree, which is deallocated after compilation. |
29 // | 29 // |
30 // Note: There is no need to initialize the Zone; the first time an | 30 // Note: There is no need to initialize the Zone; the first time an |
31 // allocation is attempted, a segment of memory will be requested | 31 // allocation is attempted, a segment of memory will be requested |
32 // through a call to malloc(). | 32 // through a call to malloc(). |
33 // | 33 // |
34 // Note: The implementation is inherently not thread safe. Do not use | 34 // Note: The implementation is inherently not thread safe. Do not use |
35 // from multi-threaded code. | 35 // from multi-threaded code. |
36 class Zone FINAL { | 36 class Zone final { |
37 public: | 37 public: |
38 Zone(); | 38 Zone(); |
39 ~Zone(); | 39 ~Zone(); |
40 | 40 |
41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
42 // allocating new segments of memory on demand using malloc(). | 42 // allocating new segments of memory on demand using malloc(). |
43 void* New(size_t size); | 43 void* New(size_t size); |
44 | 44 |
45 template <typename T> | 45 template <typename T> |
46 T* NewArray(size_t length) { | 46 T* NewArray(size_t length) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 133 |
134 // ZoneObjects should never be deleted individually; use | 134 // ZoneObjects should never be deleted individually; use |
135 // Zone::DeleteAll() to delete all zone objects in one go. | 135 // Zone::DeleteAll() to delete all zone objects in one go. |
136 void operator delete(void*, size_t) { UNREACHABLE(); } | 136 void operator delete(void*, size_t) { UNREACHABLE(); } |
137 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 137 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
138 }; | 138 }; |
139 | 139 |
140 | 140 |
141 // The ZoneScope is used to automatically call DeleteAll() on a | 141 // The ZoneScope is used to automatically call DeleteAll() on a |
142 // Zone when the ZoneScope is destroyed (i.e. goes out of scope) | 142 // Zone when the ZoneScope is destroyed (i.e. goes out of scope) |
143 class ZoneScope FINAL { | 143 class ZoneScope final { |
144 public: | 144 public: |
145 explicit ZoneScope(Zone* zone) : zone_(zone) { } | 145 explicit ZoneScope(Zone* zone) : zone_(zone) { } |
146 ~ZoneScope() { zone_->DeleteAll(); } | 146 ~ZoneScope() { zone_->DeleteAll(); } |
147 | 147 |
148 Zone* zone() const { return zone_; } | 148 Zone* zone() const { return zone_; } |
149 | 149 |
150 private: | 150 private: |
151 Zone* zone_; | 151 Zone* zone_; |
152 }; | 152 }; |
153 | 153 |
154 | 154 |
155 // The ZoneAllocationPolicy is used to specialize generic data | 155 // The ZoneAllocationPolicy is used to specialize generic data |
156 // structures to allocate themselves and their elements in the Zone. | 156 // structures to allocate themselves and their elements in the Zone. |
157 class ZoneAllocationPolicy FINAL { | 157 class ZoneAllocationPolicy final { |
158 public: | 158 public: |
159 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } | 159 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } |
160 void* New(size_t size) { return zone()->New(size); } | 160 void* New(size_t size) { return zone()->New(size); } |
161 static void Delete(void* pointer) {} | 161 static void Delete(void* pointer) {} |
162 Zone* zone() const { return zone_; } | 162 Zone* zone() const { return zone_; } |
163 | 163 |
164 private: | 164 private: |
165 Zone* zone_; | 165 Zone* zone_; |
166 }; | 166 }; |
167 | 167 |
168 | 168 |
169 // ZoneLists are growable lists with constant-time access to the | 169 // ZoneLists are growable lists with constant-time access to the |
170 // elements. The list itself and all its elements are allocated in the | 170 // elements. The list itself and all its elements are allocated in the |
171 // Zone. ZoneLists cannot be deleted individually; you can delete all | 171 // Zone. ZoneLists cannot be deleted individually; you can delete all |
172 // objects in the Zone by calling Zone::DeleteAll(). | 172 // objects in the Zone by calling Zone::DeleteAll(). |
173 template <typename T> | 173 template <typename T> |
174 class ZoneList FINAL : public List<T, ZoneAllocationPolicy> { | 174 class ZoneList final : public List<T, ZoneAllocationPolicy> { |
175 public: | 175 public: |
176 // Construct a new ZoneList with the given capacity; the length is | 176 // Construct a new ZoneList with the given capacity; the length is |
177 // always zero. The capacity must be non-negative. | 177 // always zero. The capacity must be non-negative. |
178 ZoneList(int capacity, Zone* zone) | 178 ZoneList(int capacity, Zone* zone) |
179 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } | 179 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } |
180 | 180 |
181 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 181 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
182 | 182 |
183 // Construct a new ZoneList by copying the elements of the given ZoneList. | 183 // Construct a new ZoneList by copying the elements of the given ZoneList. |
184 ZoneList(const ZoneList<T>& other, Zone* zone) | 184 ZoneList(const ZoneList<T>& other, Zone* zone) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 216 |
217 void operator delete(void* pointer) { UNREACHABLE(); } | 217 void operator delete(void* pointer) { UNREACHABLE(); } |
218 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 218 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
219 }; | 219 }; |
220 | 220 |
221 | 221 |
222 // A zone splay tree. The config type parameter encapsulates the | 222 // A zone splay tree. The config type parameter encapsulates the |
223 // different configurations of a concrete splay tree (see splay-tree.h). | 223 // different configurations of a concrete splay tree (see splay-tree.h). |
224 // The tree itself and all its elements are allocated in the Zone. | 224 // The tree itself and all its elements are allocated in the Zone. |
225 template <typename Config> | 225 template <typename Config> |
226 class ZoneSplayTree FINAL : public SplayTree<Config, ZoneAllocationPolicy> { | 226 class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> { |
227 public: | 227 public: |
228 explicit ZoneSplayTree(Zone* zone) | 228 explicit ZoneSplayTree(Zone* zone) |
229 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} | 229 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} |
230 ~ZoneSplayTree() { | 230 ~ZoneSplayTree() { |
231 // Reset the root to avoid unneeded iteration over all tree nodes | 231 // Reset the root to avoid unneeded iteration over all tree nodes |
232 // in the destructor. For a zone-allocated tree, nodes will be | 232 // in the destructor. For a zone-allocated tree, nodes will be |
233 // freed by the Zone. | 233 // freed by the Zone. |
234 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot(); | 234 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot(); |
235 } | 235 } |
236 | 236 |
237 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 237 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
238 | 238 |
239 void operator delete(void* pointer) { UNREACHABLE(); } | 239 void operator delete(void* pointer) { UNREACHABLE(); } |
240 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 240 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
241 }; | 241 }; |
242 | 242 |
243 | 243 |
244 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 244 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
245 | 245 |
246 } // namespace internal | 246 } // namespace internal |
247 } // namespace v8 | 247 } // namespace v8 |
248 | 248 |
249 #endif // V8_ZONE_H_ | 249 #endif // V8_ZONE_H_ |
OLD | NEW |