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