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/base/accounting-allocator.h" | 10 #include "src/base/accounting-allocator.h" |
11 #include "src/base/hashmap.h" | 11 #include "src/base/hashmap.h" |
12 #include "src/base/logging.h" | 12 #include "src/base/logging.h" |
13 #include "src/base/platform/platform.h" | |
13 #include "src/globals.h" | 14 #include "src/globals.h" |
14 #include "src/list.h" | 15 #include "src/list.h" |
15 #include "src/splay-tree.h" | 16 #include "src/splay-tree.h" |
16 | 17 |
17 namespace v8 { | 18 namespace v8 { |
18 namespace internal { | 19 namespace internal { |
19 | 20 |
20 // Forward declarations. | 21 // Forward declarations. |
21 class Segment; | 22 class Segment; |
22 | 23 |
23 | 24 |
24 // The Zone supports very fast allocation of small chunks of | 25 // The Zone supports very fast allocation of small chunks of |
25 // memory. The chunks cannot be deallocated individually, but instead | 26 // memory. The chunks cannot be deallocated individually, but instead |
26 // the Zone supports deallocating all chunks in one fast | 27 // the Zone supports deallocating all chunks in one fast |
27 // operation. The Zone is used to hold temporary data structures like | 28 // operation. The Zone is used to hold temporary data structures like |
28 // the abstract syntax tree, which is deallocated after compilation. | 29 // the abstract syntax tree, which is deallocated after compilation. |
29 // | 30 // |
30 // Note: There is no need to initialize the Zone; the first time an | 31 // Note: There is no need to initialize the Zone; the first time an |
31 // allocation is attempted, a segment of memory will be requested | 32 // allocation is attempted, a segment of memory will be requested |
32 // through a call to malloc(). | 33 // through a call to malloc(). |
33 // | 34 // |
34 // Note: The implementation is inherently not thread safe. Do not use | 35 // Note: The implementation is inherently not thread safe. Do not use |
35 // from multi-threaded code. | 36 // from multi-threaded code. |
36 class Zone final { | 37 class Zone final { |
38 friend class ZoneObject; | |
39 friend class Segment; | |
40 | |
37 public: | 41 public: |
38 explicit Zone(base::AccountingAllocator* allocator); | 42 explicit Zone(base::AccountingAllocator* allocator); |
39 ~Zone(); | 43 ~Zone(); |
40 | 44 |
41 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 45 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
42 // allocating new segments of memory on demand using malloc(). | 46 // allocating new segments of memory on demand using malloc(). |
43 void* New(size_t size); | 47 void* New(size_t size); |
44 | 48 |
45 template <typename T> | 49 template <typename T> |
46 T* NewArray(size_t length) { | 50 T* NewArray(size_t length) { |
(...skipping 12 matching lines...) Loading... | |
59 // Returns true if more memory has been allocated in zones than | 63 // Returns true if more memory has been allocated in zones than |
60 // the limit allows. | 64 // the limit allows. |
61 bool excess_allocation() const { | 65 bool excess_allocation() const { |
62 return segment_bytes_allocated_ > kExcessLimit; | 66 return segment_bytes_allocated_ > kExcessLimit; |
63 } | 67 } |
64 | 68 |
65 size_t allocation_size() const { return allocation_size_; } | 69 size_t allocation_size() const { return allocation_size_; } |
66 | 70 |
67 base::AccountingAllocator* allocator() const { return allocator_; } | 71 base::AccountingAllocator* allocator() const { return allocator_; } |
68 | 72 |
73 // Returns the zone the pointer belongs to. Only works in case the pointer | |
74 // actually lies within a zone segment. | |
75 static Zone* GetZoneFromPointer(const void* ptr); | |
76 | |
69 private: | 77 private: |
70 // All pointers returned from New() have this alignment. In addition, if the | 78 // All pointers returned from New() have this alignment. In addition, if the |
71 // object being allocated has a size that is divisible by 8 then its alignment | 79 // object being allocated has a size that is divisible by 8 then its alignment |
72 // will be 8. ASan requires 8-byte alignment. | 80 // will be 8. ASan requires 8-byte alignment. |
73 #ifdef V8_USE_ADDRESS_SANITIZER | 81 #ifdef V8_USE_ADDRESS_SANITIZER |
74 static const size_t kAlignment = 8; | 82 static const size_t kAlignment = 8; |
75 STATIC_ASSERT(kPointerSize <= 8); | 83 STATIC_ASSERT(kPointerSize <= 8); |
76 #else | 84 #else |
77 static const size_t kAlignment = kPointerSize; | 85 static const size_t kAlignment = kPointerSize; |
78 #endif | 86 #endif |
79 | 87 |
80 // Never allocate segments smaller than this size in bytes. | 88 // Never allocate segments smaller than this size in bytes. |
81 static const size_t kMinimumSegmentSize = 8 * KB; | 89 static const size_t kMinimumSegmentSize = 8 * KB; |
82 | 90 |
83 // Never allocate segments larger than this size in bytes. | 91 // Never allocate segments larger than this size in bytes. |
84 static const size_t kMaximumSegmentSize = 1 * MB; | 92 static const size_t kMaximumSegmentSize = 1 * MB; |
85 | 93 |
94 static const uint8_t kSegmentAlignmentBits = 20; | |
95 | |
96 // Always align new segments to this size | |
jochen (gone - plz use gerrit)
2016/09/01 12:04:52
nit. comments should be full sentences, so here it
| |
97 static const size_t kSegmentAlignmentSize = 1 << kSegmentAlignmentBits; | |
98 | |
99 static const size_t kSegmentAlignmentMask = | |
100 ~((1 << kSegmentAlignmentBits) - 1); | |
101 | |
102 STATIC_ASSERT(kMaximumSegmentSize <= kSegmentAlignmentSize); | |
103 | |
86 // Never keep segments larger than this size in bytes around. | 104 // Never keep segments larger than this size in bytes around. |
87 static const size_t kMaximumKeptSegmentSize = 64 * KB; | 105 static const size_t kMaximumKeptSegmentSize = 64 * KB; |
88 | 106 |
89 // Report zone excess when allocation exceeds this limit. | 107 // Report zone excess when allocation exceeds this limit. |
90 static const size_t kExcessLimit = 256 * MB; | 108 static const size_t kExcessLimit = 256 * MB; |
91 | 109 |
110 static Segment* GetZoneSegmentFromPointer(const void* ptr); | |
111 | |
92 // The number of bytes allocated in this zone so far. | 112 // The number of bytes allocated in this zone so far. |
93 size_t allocation_size_; | 113 size_t allocation_size_; |
94 | 114 |
95 // The number of bytes allocated in segments. Note that this number | 115 // The number of bytes allocated in segments. Note that this number |
96 // includes memory allocated from the OS but not yet allocated from | 116 // includes memory allocated from the OS but not yet allocated from |
97 // the zone. | 117 // the zone. |
98 size_t segment_bytes_allocated_; | 118 size_t segment_bytes_allocated_; |
99 | 119 |
100 // Expand the Zone to hold at least 'size' more bytes and allocate | 120 Address NewNormalSegment(size_t size); |
101 // 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 | |
103 // room in the Zone already. | |
104 Address NewExpand(size_t size); | |
105 | 121 |
106 // Creates a new segment, sets it size, and pushes it to the front | 122 Address NewLargeObjectSegment(size_t size); |
107 // of the segment chain. Returns the new segment. | 123 |
124 size_t CalculateSegmentSize(const size_t requested); | |
125 | |
126 // Creates a new segment, initializes it. Returns the new segment. | |
108 inline Segment* NewSegment(size_t size); | 127 inline Segment* NewSegment(size_t size); |
109 | 128 |
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 | 129 // The free region in the current (front) segment is represented as |
114 // the half-open interval [position, limit). The 'position' variable | 130 // the half-open interval [position, limit). The 'position' variable |
115 // is guaranteed to be aligned as dictated by kAlignment. | 131 // is guaranteed to be aligned as dictated by kAlignment. |
116 Address position_; | 132 Address position_; |
117 Address limit_; | 133 Address limit_; |
118 | 134 |
119 base::AccountingAllocator* allocator_; | 135 base::AccountingAllocator* allocator_; |
120 | 136 |
121 Segment* segment_head_; | 137 Segment* segment_head_; |
122 }; | 138 }; |
123 | 139 |
124 | |
125 // ZoneObject is an abstraction that helps define classes of objects | 140 // ZoneObject is an abstraction that helps define classes of objects |
126 // allocated in the Zone. Use it as a base class; see ast.h. | 141 // allocated in the Zone. Use it as a base class; see ast.h. |
127 class ZoneObject { | 142 class ZoneObject { |
128 public: | 143 public: |
129 // Allocate a new ZoneObject of 'size' bytes in the Zone. | 144 // Allocate a new ZoneObject of 'size' bytes in the Zone. |
130 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 145 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
131 | 146 |
147 Zone* zone() const { return Zone::GetZoneFromPointer(this); } | |
148 | |
132 // Ideally, the delete operator should be private instead of | 149 // Ideally, the delete operator should be private instead of |
133 // public, but unfortunately the compiler sometimes synthesizes | 150 // public, but unfortunately the compiler sometimes synthesizes |
134 // (unused) destructors for classes derived from ZoneObject, which | 151 // (unused) destructors for classes derived from ZoneObject, which |
135 // require the operator to be visible. MSVC requires the delete | 152 // require the operator to be visible. MSVC requires the delete |
136 // operator to be public. | 153 // operator to be public. |
137 | 154 |
138 // ZoneObjects should never be deleted individually; use | 155 // ZoneObjects should never be deleted individually; use |
139 // Zone::DeleteAll() to delete all zone objects in one go. | 156 // Zone::DeleteAll() to delete all zone objects in one go. |
140 void operator delete(void*, size_t) { UNREACHABLE(); } | 157 void operator delete(void*, size_t) { UNREACHABLE(); } |
141 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 158 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
(...skipping 30 matching lines...) Loading... | |
172 | 189 |
173 // ZoneLists are growable lists with constant-time access to the | 190 // ZoneLists are growable lists with constant-time access to the |
174 // elements. The list itself and all its elements are allocated in the | 191 // elements. The list itself and all its elements are allocated in the |
175 // Zone. ZoneLists cannot be deleted individually; you can delete all | 192 // Zone. ZoneLists cannot be deleted individually; you can delete all |
176 // objects in the Zone by calling Zone::DeleteAll(). | 193 // objects in the Zone by calling Zone::DeleteAll(). |
177 template <typename T> | 194 template <typename T> |
178 class ZoneList final : public List<T, ZoneAllocationPolicy> { | 195 class ZoneList final : public List<T, ZoneAllocationPolicy> { |
179 public: | 196 public: |
180 // Construct a new ZoneList with the given capacity; the length is | 197 // Construct a new ZoneList with the given capacity; the length is |
181 // always zero. The capacity must be non-negative. | 198 // always zero. The capacity must be non-negative. |
199 // The lists storage will be placed in the given zone. | |
182 ZoneList(int capacity, Zone* zone) | 200 ZoneList(int capacity, Zone* zone) |
183 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } | 201 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) {} |
184 | 202 |
185 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 203 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
186 | 204 |
187 // Construct a new ZoneList by copying the elements of the given ZoneList. | 205 // Construct a new ZoneList by copying the elements of the given ZoneList. |
188 ZoneList(const ZoneList<T>& other, Zone* zone) | 206 ZoneList(const ZoneList<T>& other, Zone* zone) |
189 : List<T, ZoneAllocationPolicy>(other.length(), | 207 : List<T, ZoneAllocationPolicy>(other.length(), |
190 ZoneAllocationPolicy(zone)) { | 208 ZoneAllocationPolicy(zone)) { |
191 AddAll(other, zone); | 209 AddAll(other, zone); |
192 } | 210 } |
193 | 211 |
194 // We add some convenience wrappers so that we can pass in a Zone | |
195 // instead of a (less convenient) ZoneAllocationPolicy. | |
196 void Add(const T& element, Zone* zone) { | 212 void Add(const T& element, Zone* zone) { |
213 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); | |
197 List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone)); | 214 List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone)); |
198 } | 215 } |
216 | |
199 void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone) { | 217 void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone) { |
218 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); | |
200 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); | 219 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); |
201 } | 220 } |
221 | |
202 void AddAll(const Vector<T>& other, Zone* zone) { | 222 void AddAll(const Vector<T>& other, Zone* zone) { |
223 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); | |
203 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); | 224 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); |
204 } | 225 } |
226 | |
205 void InsertAt(int index, const T& element, Zone* zone) { | 227 void InsertAt(int index, const T& element, Zone* zone) { |
228 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); | |
206 List<T, ZoneAllocationPolicy>::InsertAt(index, element, | 229 List<T, ZoneAllocationPolicy>::InsertAt(index, element, |
207 ZoneAllocationPolicy(zone)); | 230 ZoneAllocationPolicy(zone)); |
208 } | 231 } |
232 | |
209 Vector<T> AddBlock(T value, int count, Zone* zone) { | 233 Vector<T> AddBlock(T value, int count, Zone* zone) { |
234 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); | |
210 return List<T, ZoneAllocationPolicy>::AddBlock(value, count, | 235 return List<T, ZoneAllocationPolicy>::AddBlock(value, count, |
211 ZoneAllocationPolicy(zone)); | 236 ZoneAllocationPolicy(zone)); |
212 } | 237 } |
238 | |
213 void Allocate(int length, Zone* zone) { | 239 void Allocate(int length, Zone* zone) { |
240 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); | |
214 List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone)); | 241 List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone)); |
215 } | 242 } |
243 | |
216 void Initialize(int capacity, Zone* zone) { | 244 void Initialize(int capacity, Zone* zone) { |
245 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); | |
217 List<T, ZoneAllocationPolicy>::Initialize(capacity, | 246 List<T, ZoneAllocationPolicy>::Initialize(capacity, |
218 ZoneAllocationPolicy(zone)); | 247 ZoneAllocationPolicy(zone)); |
219 } | 248 } |
220 | 249 |
250 bool has_storage_zone() const { return this->capacity() > 0; } | |
251 | |
252 // Returns the zone the storage is located in | |
253 Zone* storage_zone() const { | |
254 DCHECK(this->has_storage_zone()); | |
255 // ZoneList storage lives in a zone, so this works. | |
256 return Zone::GetZoneFromPointer(this->data()); | |
257 } | |
258 | |
221 void operator delete(void* pointer) { UNREACHABLE(); } | 259 void operator delete(void* pointer) { UNREACHABLE(); } |
222 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 260 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
223 }; | 261 }; |
224 | 262 |
225 | 263 |
226 // A zone splay tree. The config type parameter encapsulates the | 264 // A zone splay tree. The config type parameter encapsulates the |
227 // different configurations of a concrete splay tree (see splay-tree.h). | 265 // different configurations of a concrete splay tree (see splay-tree.h). |
228 // The tree itself and all its elements are allocated in the Zone. | 266 // The tree itself and all its elements are allocated in the Zone. |
229 template <typename Config> | 267 template <typename Config> |
230 class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> { | 268 class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> { |
(...skipping 12 matching lines...) Loading... | |
243 void operator delete(void* pointer) { UNREACHABLE(); } | 281 void operator delete(void* pointer) { UNREACHABLE(); } |
244 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 282 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
245 }; | 283 }; |
246 | 284 |
247 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 285 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
248 | 286 |
249 } // namespace internal | 287 } // namespace internal |
250 } // namespace v8 | 288 } // namespace v8 |
251 | 289 |
252 #endif // V8_ZONE_H_ | 290 #endif // V8_ZONE_H_ |
OLD | NEW |