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_SRC_ZONE_ZONE_H_ |
marja
2016/09/19 08:05:11
Ditto
heimbuef
2016/09/19 10:47:52
Done.
| |
6 #define V8_ZONE_H_ | 6 #define V8_SRC_ZONE_ZONE_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "src/base/accounting-allocator.h" | |
11 #include "src/base/hashmap.h" | 10 #include "src/base/hashmap.h" |
12 #include "src/base/logging.h" | 11 #include "src/base/logging.h" |
13 #include "src/globals.h" | 12 #include "src/globals.h" |
14 #include "src/list.h" | 13 #include "src/list.h" |
15 #include "src/splay-tree.h" | 14 #include "src/splay-tree.h" |
15 #include "src/zone/accounting-allocator.h" | |
16 | 16 |
17 namespace v8 { | 17 namespace v8 { |
18 namespace internal { | 18 namespace internal { |
19 | 19 |
20 // Forward declarations. | |
21 class Segment; | |
22 | |
23 | |
24 // The Zone supports very fast allocation of small chunks of | 20 // The Zone supports very fast allocation of small chunks of |
25 // memory. The chunks cannot be deallocated individually, but instead | 21 // memory. The chunks cannot be deallocated individually, but instead |
26 // the Zone supports deallocating all chunks in one fast | 22 // the Zone supports deallocating all chunks in one fast |
27 // operation. The Zone is used to hold temporary data structures like | 23 // operation. The Zone is used to hold temporary data structures like |
28 // the abstract syntax tree, which is deallocated after compilation. | 24 // the abstract syntax tree, which is deallocated after compilation. |
29 // | 25 // |
30 // Note: There is no need to initialize the Zone; the first time an | 26 // Note: There is no need to initialize the Zone; the first time an |
31 // allocation is attempted, a segment of memory will be requested | 27 // allocation is attempted, a segment of memory will be requested |
32 // through a call to malloc(). | 28 // through a call to malloc(). |
33 // | 29 // |
34 // Note: The implementation is inherently not thread safe. Do not use | 30 // Note: The implementation is inherently not thread safe. Do not use |
35 // from multi-threaded code. | 31 // from multi-threaded code. |
36 class Zone final { | 32 class Zone final { |
37 public: | 33 public: |
38 explicit Zone(base::AccountingAllocator* allocator); | 34 explicit Zone(AccountingAllocator* allocator); |
39 ~Zone(); | 35 ~Zone(); |
40 | 36 |
41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 37 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
42 // allocating new segments of memory on demand using malloc(). | 38 // allocating new segments of memory on demand using malloc(). |
43 void* New(size_t size); | 39 void* New(size_t size); |
44 | 40 |
45 template <typename T> | 41 template <typename T> |
46 T* NewArray(size_t length) { | 42 T* NewArray(size_t length) { |
47 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); | 43 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); |
48 return static_cast<T*>(New(length * sizeof(T))); | 44 return static_cast<T*>(New(length * sizeof(T))); |
49 } | 45 } |
50 | 46 |
51 // Deletes all objects and free all memory allocated in the Zone. Keeps one | 47 // Deletes all objects and free all memory allocated in the Zone. Keeps one |
52 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | 48 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. |
53 void DeleteAll(); | 49 void DeleteAll(); |
54 | 50 |
55 // Deletes the last small segment kept around by DeleteAll(). You | 51 // Deletes the last small segment kept around by DeleteAll(). You |
56 // may no longer allocate in the Zone after a call to this method. | 52 // may no longer allocate in the Zone after a call to this method. |
57 void DeleteKeptSegment(); | 53 void DeleteKeptSegment(); |
58 | 54 |
59 // Returns true if more memory has been allocated in zones than | 55 // Returns true if more memory has been allocated in zones than |
60 // the limit allows. | 56 // the limit allows. |
61 bool excess_allocation() const { | 57 bool excess_allocation() const { |
62 return segment_bytes_allocated_ > kExcessLimit; | 58 return segment_bytes_allocated_ > kExcessLimit; |
63 } | 59 } |
64 | 60 |
65 size_t allocation_size() const { return allocation_size_; } | 61 size_t allocation_size() const { return allocation_size_; } |
66 | 62 |
67 base::AccountingAllocator* allocator() const { return allocator_; } | 63 AccountingAllocator* allocator() const { return allocator_; } |
68 | 64 |
69 private: | 65 private: |
70 // All pointers returned from New() have this alignment. In addition, if the | 66 friend class SegmentPool; |
marja
2016/09/19 08:05:11
Hmm, what? Is this CL based on some other CL and c
heimbuef
2016/09/19 10:47:52
Removed.
| |
71 // object being allocated has a size that is divisible by 8 then its alignment | 67 // All pointers returned from New() have this alignment. In addition, if the |
72 // will be 8. ASan requires 8-byte alignment. | 68 // object being allocated has a size that is divisible by 8 then its alignment |
69 // will be 8. ASan requires 8-byte alignment. | |
73 #ifdef V8_USE_ADDRESS_SANITIZER | 70 #ifdef V8_USE_ADDRESS_SANITIZER |
74 static const size_t kAlignment = 8; | 71 static const size_t kAlignment = 8; |
75 STATIC_ASSERT(kPointerSize <= 8); | 72 STATIC_ASSERT(kPointerSize <= 8); |
76 #else | 73 #else |
77 static const size_t kAlignment = kPointerSize; | 74 static const size_t kAlignment = kPointerSize; |
78 #endif | 75 #endif |
79 | 76 |
80 // Never allocate segments smaller than this size in bytes. | 77 // Never allocate segments smaller than this size in bytes. |
81 static const size_t kMinimumSegmentSize = 8 * KB; | 78 static const size_t kMinimumSegmentSize = 8 * KB; |
82 | 79 |
(...skipping 17 matching lines...) Expand all Loading... | |
100 // Expand the Zone to hold at least 'size' more bytes and allocate | 97 // Expand the Zone to hold at least 'size' more bytes and allocate |
101 // the bytes. Returns the address of the newly allocated chunk of | 98 // the bytes. Returns the address of the newly allocated chunk of |
102 // memory in the Zone. Should only be called if there isn't enough | 99 // memory in the Zone. Should only be called if there isn't enough |
103 // room in the Zone already. | 100 // room in the Zone already. |
104 Address NewExpand(size_t size); | 101 Address NewExpand(size_t size); |
105 | 102 |
106 // Creates a new segment, sets it size, and pushes it to the front | 103 // Creates a new segment, sets it size, and pushes it to the front |
107 // of the segment chain. Returns the new segment. | 104 // of the segment chain. Returns the new segment. |
108 inline Segment* NewSegment(size_t size); | 105 inline Segment* NewSegment(size_t size); |
109 | 106 |
110 // Deletes the given segment. Does not touch the segment chain. | |
111 inline void DeleteSegment(Segment* segment, size_t size); | |
112 | |
113 // The free region in the current (front) segment is represented as | 107 // The free region in the current (front) segment is represented as |
114 // the half-open interval [position, limit). The 'position' variable | 108 // the half-open interval [position, limit). The 'position' variable |
115 // is guaranteed to be aligned as dictated by kAlignment. | 109 // is guaranteed to be aligned as dictated by kAlignment. |
116 Address position_; | 110 Address position_; |
117 Address limit_; | 111 Address limit_; |
118 | 112 |
119 base::AccountingAllocator* allocator_; | 113 AccountingAllocator* allocator_; |
120 | 114 |
121 Segment* segment_head_; | 115 Segment* segment_head_; |
122 }; | 116 }; |
123 | 117 |
124 | |
125 // ZoneObject is an abstraction that helps define classes of objects | 118 // ZoneObject is an abstraction that helps define classes of objects |
126 // allocated in the Zone. Use it as a base class; see ast.h. | 119 // allocated in the Zone. Use it as a base class; see ast.h. |
127 class ZoneObject { | 120 class ZoneObject { |
128 public: | 121 public: |
129 // Allocate a new ZoneObject of 'size' bytes in the Zone. | 122 // Allocate a new ZoneObject of 'size' bytes in the Zone. |
130 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 123 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
131 | 124 |
132 // Ideally, the delete operator should be private instead of | 125 // Ideally, the delete operator should be private instead of |
133 // public, but unfortunately the compiler sometimes synthesizes | 126 // public, but unfortunately the compiler sometimes synthesizes |
134 // (unused) destructors for classes derived from ZoneObject, which | 127 // (unused) destructors for classes derived from ZoneObject, which |
135 // require the operator to be visible. MSVC requires the delete | 128 // require the operator to be visible. MSVC requires the delete |
136 // operator to be public. | 129 // operator to be public. |
137 | 130 |
138 // ZoneObjects should never be deleted individually; use | 131 // ZoneObjects should never be deleted individually; use |
139 // Zone::DeleteAll() to delete all zone objects in one go. | 132 // Zone::DeleteAll() to delete all zone objects in one go. |
140 void operator delete(void*, size_t) { UNREACHABLE(); } | 133 void operator delete(void*, size_t) { UNREACHABLE(); } |
141 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 134 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
142 }; | 135 }; |
143 | 136 |
144 | |
145 // The ZoneScope is used to automatically call DeleteAll() on a | 137 // The ZoneScope is used to automatically call DeleteAll() on a |
146 // Zone when the ZoneScope is destroyed (i.e. goes out of scope) | 138 // Zone when the ZoneScope is destroyed (i.e. goes out of scope) |
147 class ZoneScope final { | 139 class ZoneScope final { |
148 public: | 140 public: |
149 explicit ZoneScope(Zone* zone) : zone_(zone) { } | 141 explicit ZoneScope(Zone* zone) : zone_(zone) {} |
150 ~ZoneScope() { zone_->DeleteAll(); } | 142 ~ZoneScope() { zone_->DeleteAll(); } |
151 | 143 |
152 Zone* zone() const { return zone_; } | 144 Zone* zone() const { return zone_; } |
153 | 145 |
154 private: | 146 private: |
155 Zone* zone_; | 147 Zone* zone_; |
156 }; | 148 }; |
157 | 149 |
158 | |
159 // The ZoneAllocationPolicy is used to specialize generic data | 150 // The ZoneAllocationPolicy is used to specialize generic data |
160 // structures to allocate themselves and their elements in the Zone. | 151 // structures to allocate themselves and their elements in the Zone. |
161 class ZoneAllocationPolicy final { | 152 class ZoneAllocationPolicy final { |
162 public: | 153 public: |
163 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } | 154 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) {} |
164 void* New(size_t size) { return zone()->New(size); } | 155 void* New(size_t size) { return zone()->New(size); } |
165 static void Delete(void* pointer) {} | 156 static void Delete(void* pointer) {} |
166 Zone* zone() const { return zone_; } | 157 Zone* zone() const { return zone_; } |
167 | 158 |
168 private: | 159 private: |
169 Zone* zone_; | 160 Zone* zone_; |
170 }; | 161 }; |
171 | 162 |
172 | |
173 // ZoneLists are growable lists with constant-time access to the | 163 // ZoneLists are growable lists with constant-time access to the |
174 // elements. The list itself and all its elements are allocated in the | 164 // elements. The list itself and all its elements are allocated in the |
175 // Zone. ZoneLists cannot be deleted individually; you can delete all | 165 // Zone. ZoneLists cannot be deleted individually; you can delete all |
176 // objects in the Zone by calling Zone::DeleteAll(). | 166 // objects in the Zone by calling Zone::DeleteAll(). |
177 template <typename T> | 167 template <typename T> |
178 class ZoneList final : public List<T, ZoneAllocationPolicy> { | 168 class ZoneList final : public List<T, ZoneAllocationPolicy> { |
179 public: | 169 public: |
180 // Construct a new ZoneList with the given capacity; the length is | 170 // Construct a new ZoneList with the given capacity; the length is |
181 // always zero. The capacity must be non-negative. | 171 // always zero. The capacity must be non-negative. |
182 ZoneList(int capacity, Zone* zone) | 172 ZoneList(int capacity, Zone* zone) |
183 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } | 173 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) {} |
184 | 174 |
185 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 175 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
186 | 176 |
187 // Construct a new ZoneList by copying the elements of the given ZoneList. | 177 // Construct a new ZoneList by copying the elements of the given ZoneList. |
188 ZoneList(const ZoneList<T>& other, Zone* zone) | 178 ZoneList(const ZoneList<T>& other, Zone* zone) |
189 : List<T, ZoneAllocationPolicy>(other.length(), | 179 : List<T, ZoneAllocationPolicy>(other.length(), |
190 ZoneAllocationPolicy(zone)) { | 180 ZoneAllocationPolicy(zone)) { |
191 AddAll(other, zone); | 181 AddAll(other, zone); |
192 } | 182 } |
193 | 183 |
(...skipping 21 matching lines...) Expand all Loading... | |
215 } | 205 } |
216 void Initialize(int capacity, Zone* zone) { | 206 void Initialize(int capacity, Zone* zone) { |
217 List<T, ZoneAllocationPolicy>::Initialize(capacity, | 207 List<T, ZoneAllocationPolicy>::Initialize(capacity, |
218 ZoneAllocationPolicy(zone)); | 208 ZoneAllocationPolicy(zone)); |
219 } | 209 } |
220 | 210 |
221 void operator delete(void* pointer) { UNREACHABLE(); } | 211 void operator delete(void* pointer) { UNREACHABLE(); } |
222 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 212 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
223 }; | 213 }; |
224 | 214 |
225 | |
226 // A zone splay tree. The config type parameter encapsulates the | 215 // A zone splay tree. The config type parameter encapsulates the |
227 // different configurations of a concrete splay tree (see splay-tree.h). | 216 // different configurations of a concrete splay tree (see splay-tree.h). |
228 // The tree itself and all its elements are allocated in the Zone. | 217 // The tree itself and all its elements are allocated in the Zone. |
229 template <typename Config> | 218 template <typename Config> |
230 class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> { | 219 class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> { |
231 public: | 220 public: |
232 explicit ZoneSplayTree(Zone* zone) | 221 explicit ZoneSplayTree(Zone* zone) |
233 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} | 222 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} |
234 ~ZoneSplayTree() { | 223 ~ZoneSplayTree() { |
235 // Reset the root to avoid unneeded iteration over all tree nodes | 224 // Reset the root to avoid unneeded iteration over all tree nodes |
236 // in the destructor. For a zone-allocated tree, nodes will be | 225 // in the destructor. For a zone-allocated tree, nodes will be |
237 // freed by the Zone. | 226 // freed by the Zone. |
238 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot(); | 227 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot(); |
239 } | 228 } |
240 | 229 |
241 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 230 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
242 | 231 |
243 void operator delete(void* pointer) { UNREACHABLE(); } | 232 void operator delete(void* pointer) { UNREACHABLE(); } |
244 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 233 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
245 }; | 234 }; |
246 | 235 |
247 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 236 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
248 | 237 |
249 } // namespace internal | 238 } // namespace internal |
250 } // namespace v8 | 239 } // namespace v8 |
251 | 240 |
252 #endif // V8_ZONE_H_ | 241 #endif // V8_ZONE_H_ |
OLD | NEW |