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 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // Returns true if more memory has been allocated in zones than | 60 // Returns true if more memory has been allocated in zones than |
60 // the limit allows. | 61 // the limit allows. |
61 bool excess_allocation() const { | 62 bool excess_allocation() const { |
62 return segment_bytes_allocated_ > kExcessLimit; | 63 return segment_bytes_allocated_ > kExcessLimit; |
63 } | 64 } |
64 | 65 |
65 size_t allocation_size() const { return allocation_size_; } | 66 size_t allocation_size() const { return allocation_size_; } |
66 | 67 |
67 base::AccountingAllocator* allocator() const { return allocator_; } | 68 base::AccountingAllocator* allocator() const { return allocator_; } |
68 | 69 |
| 70 // Returns the zone the pointer belongs to. Only works in case the pointer |
| 71 // actually lies within a zone segment. |
| 72 static Zone* GetZoneFromPointer(const void* ptr); |
| 73 |
69 private: | 74 private: |
| 75 friend class ZoneObject; |
| 76 friend class Segment; |
70 // All pointers returned from New() have this alignment. In addition, if the | 77 // 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 | 78 // object being allocated has a size that is divisible by 8 then its alignment |
72 // will be 8. ASan requires 8-byte alignment. | 79 // will be 8. ASan requires 8-byte alignment. |
73 #ifdef V8_USE_ADDRESS_SANITIZER | 80 #ifdef V8_USE_ADDRESS_SANITIZER |
74 static const size_t kAlignment = 8; | 81 static const size_t kAlignment = 8; |
75 STATIC_ASSERT(kPointerSize <= 8); | 82 STATIC_ASSERT(kPointerSize <= 8); |
76 #else | 83 #else |
77 static const size_t kAlignment = kPointerSize; | 84 static const size_t kAlignment = kPointerSize; |
78 #endif | 85 #endif |
79 | 86 |
80 // Never allocate segments smaller than this size in bytes. | 87 // Never allocate segments smaller than this size in bytes. |
81 static const size_t kMinimumSegmentSize = 8 * KB; | 88 static const size_t kMinimumSegmentSize = 8 * KB; |
82 | 89 |
83 // Never allocate segments larger than this size in bytes. | 90 // Never allocate segments larger than this size in bytes. |
84 static const size_t kMaximumSegmentSize = 1 * MB; | 91 static const size_t kMaximumSegmentSize = 1 * MB; |
85 | 92 |
| 93 static const uint8_t kSegmentAlignmentBits = 20; |
| 94 |
| 95 // Always align new segments to this size. |
| 96 static const size_t kSegmentAlignmentSize = 1 << kSegmentAlignmentBits; |
| 97 |
| 98 static const size_t kSegmentAlignmentMask = |
| 99 ~((1 << kSegmentAlignmentBits) - 1); |
| 100 |
| 101 STATIC_ASSERT(kMaximumSegmentSize <= kSegmentAlignmentSize); |
| 102 |
86 // Never keep segments larger than this size in bytes around. | 103 // Never keep segments larger than this size in bytes around. |
87 static const size_t kMaximumKeptSegmentSize = 64 * KB; | 104 static const size_t kMaximumKeptSegmentSize = 64 * KB; |
88 | 105 |
89 // Report zone excess when allocation exceeds this limit. | 106 // Report zone excess when allocation exceeds this limit. |
90 static const size_t kExcessLimit = 256 * MB; | 107 static const size_t kExcessLimit = 256 * MB; |
91 | 108 |
| 109 static Segment* GetZoneSegmentFromPointer(const void* ptr); |
| 110 |
92 // The number of bytes allocated in this zone so far. | 111 // The number of bytes allocated in this zone so far. |
93 size_t allocation_size_; | 112 size_t allocation_size_; |
94 | 113 |
95 // The number of bytes allocated in segments. Note that this number | 114 // The number of bytes allocated in segments. Note that this number |
96 // includes memory allocated from the OS but not yet allocated from | 115 // includes memory allocated from the OS but not yet allocated from |
97 // the zone. | 116 // the zone. |
98 size_t segment_bytes_allocated_; | 117 size_t segment_bytes_allocated_; |
99 | 118 |
100 // Expand the Zone to hold at least 'size' more bytes and allocate | 119 // Creates a new normal segment, that can be used to quickly allocate memory |
101 // the bytes. Returns the address of the newly allocated chunk of | 120 // for lots of smaller objects. |
102 // memory in the Zone. Should only be called if there isn't enough | 121 Address NewNormalSegment(size_t size); |
103 // room in the Zone already. | |
104 Address NewExpand(size_t size); | |
105 | 122 |
106 // Creates a new segment, sets it size, and pushes it to the front | 123 // Creates a large object segment, that is created to exactly fit one large |
107 // of the segment chain. Returns the new segment. | 124 // object. |
| 125 Address NewLargeObjectSegment(size_t size); |
| 126 |
| 127 size_t CalculateSegmentSize(const size_t requested); |
| 128 |
| 129 // Creates a new segment of the requested size and initializes it. Returns the |
| 130 // new segment. |
108 inline Segment* NewSegment(size_t size); | 131 inline Segment* NewSegment(size_t size); |
109 | 132 |
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 | 133 // The free region in the current (front) segment is represented as |
114 // the half-open interval [position, limit). The 'position' variable | 134 // the half-open interval [position, limit). The 'position' variable |
115 // is guaranteed to be aligned as dictated by kAlignment. | 135 // is guaranteed to be aligned as dictated by kAlignment. |
116 Address position_; | 136 Address position_; |
117 Address limit_; | 137 Address limit_; |
118 | 138 |
119 base::AccountingAllocator* allocator_; | 139 base::AccountingAllocator* allocator_; |
120 | 140 |
121 Segment* segment_head_; | 141 Segment* segment_head_; |
122 }; | 142 }; |
123 | 143 |
124 | |
125 // ZoneObject is an abstraction that helps define classes of objects | 144 // ZoneObject is an abstraction that helps define classes of objects |
126 // allocated in the Zone. Use it as a base class; see ast.h. | 145 // allocated in the Zone. Use it as a base class; see ast.h. |
127 class ZoneObject { | 146 class ZoneObject { |
128 public: | 147 public: |
129 // Allocate a new ZoneObject of 'size' bytes in the Zone. | 148 // Allocate a new ZoneObject of 'size' bytes in the Zone. |
130 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 149 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
131 | 150 |
| 151 Zone* zone() const { return Zone::GetZoneFromPointer(this); } |
| 152 |
132 // Ideally, the delete operator should be private instead of | 153 // Ideally, the delete operator should be private instead of |
133 // public, but unfortunately the compiler sometimes synthesizes | 154 // public, but unfortunately the compiler sometimes synthesizes |
134 // (unused) destructors for classes derived from ZoneObject, which | 155 // (unused) destructors for classes derived from ZoneObject, which |
135 // require the operator to be visible. MSVC requires the delete | 156 // require the operator to be visible. MSVC requires the delete |
136 // operator to be public. | 157 // operator to be public. |
137 | 158 |
138 // ZoneObjects should never be deleted individually; use | 159 // ZoneObjects should never be deleted individually; use |
139 // Zone::DeleteAll() to delete all zone objects in one go. | 160 // Zone::DeleteAll() to delete all zone objects in one go. |
140 void operator delete(void*, size_t) { UNREACHABLE(); } | 161 void operator delete(void*, size_t) { UNREACHABLE(); } |
141 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 162 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
(...skipping 13 matching lines...) Expand all Loading... |
155 Zone* zone_; | 176 Zone* zone_; |
156 }; | 177 }; |
157 | 178 |
158 | 179 |
159 // The ZoneAllocationPolicy is used to specialize generic data | 180 // The ZoneAllocationPolicy is used to specialize generic data |
160 // structures to allocate themselves and their elements in the Zone. | 181 // structures to allocate themselves and their elements in the Zone. |
161 class ZoneAllocationPolicy final { | 182 class ZoneAllocationPolicy final { |
162 public: | 183 public: |
163 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } | 184 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } |
164 void* New(size_t size) { return zone()->New(size); } | 185 void* New(size_t size) { return zone()->New(size); } |
165 static void Delete(void* pointer) {} | 186 static void Delete(void* pointer) { |
| 187 DCHECK_IMPLIES(pointer != nullptr, Zone::GetZoneFromPointer(pointer)); |
| 188 } |
166 Zone* zone() const { return zone_; } | 189 Zone* zone() const { return zone_; } |
167 | 190 |
168 private: | 191 private: |
169 Zone* zone_; | 192 Zone* zone_; |
170 }; | 193 }; |
171 | 194 |
172 | 195 |
173 // ZoneLists are growable lists with constant-time access to the | 196 // ZoneLists are growable lists with constant-time access to the |
174 // elements. The list itself and all its elements are allocated in the | 197 // elements. The list itself and all its elements are allocated in the |
175 // Zone. ZoneLists cannot be deleted individually; you can delete all | 198 // Zone. ZoneLists cannot be deleted individually; you can delete all |
176 // objects in the Zone by calling Zone::DeleteAll(). | 199 // objects in the Zone by calling Zone::DeleteAll(). |
177 template <typename T> | 200 template <typename T> |
178 class ZoneList final : public List<T, ZoneAllocationPolicy> { | 201 class ZoneList final : public List<T, ZoneAllocationPolicy> { |
179 public: | 202 public: |
180 // Construct a new ZoneList with the given capacity; the length is | 203 // Construct a new ZoneList with the given capacity; the length is |
181 // always zero. The capacity must be non-negative. | 204 // always zero. The capacity must be non-negative. |
| 205 // The lists storage will be placed in the given zone. |
182 ZoneList(int capacity, Zone* zone) | 206 ZoneList(int capacity, Zone* zone) |
183 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } | 207 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) {} |
184 | 208 |
185 void* operator new(size_t size, Zone* zone) { return zone->New(size); } | 209 void* operator new(size_t size, Zone* zone) { return zone->New(size); } |
186 | 210 |
187 // Construct a new ZoneList by copying the elements of the given ZoneList. | 211 // Construct a new ZoneList by copying the elements of the given ZoneList. |
188 ZoneList(const ZoneList<T>& other, Zone* zone) | 212 ZoneList(const ZoneList<T>& other, Zone* zone) |
189 : List<T, ZoneAllocationPolicy>(other.length(), | 213 : List<T, ZoneAllocationPolicy>(other.length(), |
190 ZoneAllocationPolicy(zone)) { | 214 ZoneAllocationPolicy(zone)) { |
191 AddAll(other, zone); | 215 AddAll(other, zone); |
192 } | 216 } |
193 | 217 |
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) { | 218 void Add(const T& element, Zone* zone) { |
| 219 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); |
197 List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone)); | 220 List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone)); |
198 } | 221 } |
| 222 |
199 void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone) { | 223 void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone) { |
| 224 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); |
200 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); | 225 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); |
201 } | 226 } |
| 227 |
202 void AddAll(const Vector<T>& other, Zone* zone) { | 228 void AddAll(const Vector<T>& other, Zone* zone) { |
| 229 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); |
203 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); | 230 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); |
204 } | 231 } |
| 232 |
205 void InsertAt(int index, const T& element, Zone* zone) { | 233 void InsertAt(int index, const T& element, Zone* zone) { |
| 234 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); |
206 List<T, ZoneAllocationPolicy>::InsertAt(index, element, | 235 List<T, ZoneAllocationPolicy>::InsertAt(index, element, |
207 ZoneAllocationPolicy(zone)); | 236 ZoneAllocationPolicy(zone)); |
208 } | 237 } |
| 238 |
209 Vector<T> AddBlock(T value, int count, Zone* zone) { | 239 Vector<T> AddBlock(T value, int count, Zone* zone) { |
| 240 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); |
210 return List<T, ZoneAllocationPolicy>::AddBlock(value, count, | 241 return List<T, ZoneAllocationPolicy>::AddBlock(value, count, |
211 ZoneAllocationPolicy(zone)); | 242 ZoneAllocationPolicy(zone)); |
212 } | 243 } |
| 244 |
213 void Allocate(int length, Zone* zone) { | 245 void Allocate(int length, Zone* zone) { |
| 246 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); |
214 List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone)); | 247 List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone)); |
215 } | 248 } |
| 249 |
216 void Initialize(int capacity, Zone* zone) { | 250 void Initialize(int capacity, Zone* zone) { |
| 251 DCHECK_IMPLIES(this->has_storage_zone(), this->storage_zone() == zone); |
217 List<T, ZoneAllocationPolicy>::Initialize(capacity, | 252 List<T, ZoneAllocationPolicy>::Initialize(capacity, |
218 ZoneAllocationPolicy(zone)); | 253 ZoneAllocationPolicy(zone)); |
219 } | 254 } |
220 | 255 |
| 256 bool has_storage_zone() const { return this->capacity() > 0; } |
| 257 |
| 258 // Returns the zone the storage is located in |
| 259 Zone* storage_zone() const { |
| 260 DCHECK(this->has_storage_zone()); |
| 261 // ZoneList storage lives in a zone, so this works. |
| 262 return Zone::GetZoneFromPointer(this->data()); |
| 263 } |
| 264 |
221 void operator delete(void* pointer) { UNREACHABLE(); } | 265 void operator delete(void* pointer) { UNREACHABLE(); } |
222 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 266 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
223 }; | 267 }; |
224 | 268 |
225 | 269 |
226 // A zone splay tree. The config type parameter encapsulates the | 270 // A zone splay tree. The config type parameter encapsulates the |
227 // different configurations of a concrete splay tree (see splay-tree.h). | 271 // different configurations of a concrete splay tree (see splay-tree.h). |
228 // The tree itself and all its elements are allocated in the Zone. | 272 // The tree itself and all its elements are allocated in the Zone. |
229 template <typename Config> | 273 template <typename Config> |
230 class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> { | 274 class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> { |
(...skipping 12 matching lines...) Expand all Loading... |
243 void operator delete(void* pointer) { UNREACHABLE(); } | 287 void operator delete(void* pointer) { UNREACHABLE(); } |
244 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 288 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
245 }; | 289 }; |
246 | 290 |
247 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 291 typedef base::TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
248 | 292 |
249 } // namespace internal | 293 } // namespace internal |
250 } // namespace v8 | 294 } // namespace v8 |
251 | 295 |
252 #endif // V8_ZONE_H_ | 296 #endif // V8_ZONE_H_ |
OLD | NEW |