OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 | 57 |
58 // Note: There is no need to initialize the Zone; the first time an | 58 // Note: There is no need to initialize the Zone; the first time an |
59 // allocation is attempted, a segment of memory will be requested | 59 // allocation is attempted, a segment of memory will be requested |
60 // through a call to malloc(). | 60 // through a call to malloc(). |
61 | 61 |
62 // Note: The implementation is inherently not thread safe. Do not use | 62 // Note: The implementation is inherently not thread safe. Do not use |
63 // from multi-threaded code. | 63 // from multi-threaded code. |
64 | 64 |
65 class Zone { | 65 class Zone { |
66 public: | 66 public: |
67 explicit Zone(Isolate* isolate); | |
67 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 68 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
68 // allocating new segments of memory on demand using malloc(). | 69 // allocating new segments of memory on demand using malloc(). |
69 inline void* New(int size); | 70 inline void* New(int size); |
70 | 71 |
71 template <typename T> | 72 template <typename T> |
72 inline T* NewArray(int length); | 73 inline T* NewArray(int length); |
73 | 74 |
74 // Deletes all objects and free all memory allocated in the Zone. Keeps one | 75 // This method dispatches to either DeleteAll or to DeleteAllButOne |
75 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | 76 // depending on whether this is the singleton Zone in the isolate or |
76 void DeleteAll(); | 77 // not. |
78 void Destroy(); | |
danno
2012/06/14 14:22:19
Why not just make sure that the destructor of the
sanjoy
2012/06/15 09:24:31
Yes, that is much cleaner. Done.
| |
77 | 79 |
78 // Deletes the last small segment kept around by DeleteAll(). | 80 // Deletes the last small segment kept around by DeleteAll(). |
79 void DeleteKeptSegment(); | 81 void DeleteKeptSegment(); |
80 | 82 |
81 // Returns true if more memory has been allocated in zones than | 83 // Returns true if more memory has been allocated in zones than |
82 // the limit allows. | 84 // the limit allows. |
83 inline bool excess_allocation(); | 85 inline bool excess_allocation(); |
84 | 86 |
85 inline void adjust_segment_bytes_allocated(int delta); | 87 inline void adjust_segment_bytes_allocated(int delta); |
86 | 88 |
87 inline Isolate* isolate() { return isolate_; } | 89 inline Isolate* isolate() { return isolate_; } |
88 | 90 |
89 static unsigned allocation_size_; | 91 static unsigned allocation_size_; |
90 | 92 |
91 private: | 93 private: |
92 friend class Isolate; | 94 friend class Isolate; |
93 friend class ZoneScope; | 95 friend class ZoneScope; |
94 | 96 |
97 // Frees all memory allocated to this Zone. | |
98 void DeleteAll(); | |
99 | |
100 // Deletes all objects and free all memory allocated in the Zone. Keeps one | |
101 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | |
102 void DeleteAllButOne(); | |
103 | |
95 // All pointers returned from New() have this alignment. In addition, if the | 104 // All pointers returned from New() have this alignment. In addition, if the |
96 // object being allocated has a size that is divisible by 8 then its alignment | 105 // object being allocated has a size that is divisible by 8 then its alignment |
97 // will be 8. | 106 // will be 8. |
98 static const int kAlignment = kPointerSize; | 107 static const int kAlignment = kPointerSize; |
99 | 108 |
100 // Never allocate segments smaller than this size in bytes. | 109 // Never allocate segments smaller than this size in bytes. |
101 static const int kMinimumSegmentSize = 8 * KB; | 110 static const int kMinimumSegmentSize = 8 * KB; |
102 | 111 |
103 // Never allocate segments larger than this size in bytes. | 112 // Never allocate segments larger than this size in bytes. |
104 static const int kMaximumSegmentSize = 1 * MB; | 113 static const int kMaximumSegmentSize = 1 * MB; |
105 | 114 |
106 // Never keep segments larger than this size in bytes around. | 115 // Never keep segments larger than this size in bytes around. |
107 static const int kMaximumKeptSegmentSize = 64 * KB; | 116 static const int kMaximumKeptSegmentSize = 64 * KB; |
108 | 117 |
109 // Report zone excess when allocation exceeds this limit. | 118 // Report zone excess when allocation exceeds this limit. |
110 int zone_excess_limit_; | 119 int zone_excess_limit_; |
111 | 120 |
112 // The number of bytes allocated in segments. Note that this number | 121 // The number of bytes allocated in segments. Note that this number |
113 // includes memory allocated from the OS but not yet allocated from | 122 // includes memory allocated from the OS but not yet allocated from |
114 // the zone. | 123 // the zone. |
115 int segment_bytes_allocated_; | 124 int segment_bytes_allocated_; |
116 | 125 |
117 // Each isolate gets its own zone. | |
118 Zone(); | |
119 | |
120 // Expand the Zone to hold at least 'size' more bytes and allocate | 126 // Expand the Zone to hold at least 'size' more bytes and allocate |
121 // the bytes. Returns the address of the newly allocated chunk of | 127 // the bytes. Returns the address of the newly allocated chunk of |
122 // memory in the Zone. Should only be called if there isn't enough | 128 // memory in the Zone. Should only be called if there isn't enough |
123 // room in the Zone already. | 129 // room in the Zone already. |
124 Address NewExpand(int size); | 130 Address NewExpand(int size); |
125 | 131 |
126 // Creates a new segment, sets it size, and pushes it to the front | 132 // Creates a new segment, sets it size, and pushes it to the front |
127 // of the segment chain. Returns the new segment. | 133 // of the segment chain. Returns the new segment. |
128 Segment* NewSegment(int size); | 134 Segment* NewSegment(int size); |
129 | 135 |
(...skipping 20 matching lines...) Expand all Loading... | |
150 // Allocate a new ZoneObject of 'size' bytes in the Zone. | 156 // Allocate a new ZoneObject of 'size' bytes in the Zone. |
151 INLINE(void* operator new(size_t size, Zone* zone)); | 157 INLINE(void* operator new(size_t size, Zone* zone)); |
152 | 158 |
153 // Ideally, the delete operator should be private instead of | 159 // Ideally, the delete operator should be private instead of |
154 // public, but unfortunately the compiler sometimes synthesizes | 160 // public, but unfortunately the compiler sometimes synthesizes |
155 // (unused) destructors for classes derived from ZoneObject, which | 161 // (unused) destructors for classes derived from ZoneObject, which |
156 // require the operator to be visible. MSVC requires the delete | 162 // require the operator to be visible. MSVC requires the delete |
157 // operator to be public. | 163 // operator to be public. |
158 | 164 |
159 // ZoneObjects should never be deleted individually; use | 165 // ZoneObjects should never be deleted individually; use |
160 // Zone::DeleteAll() to delete all zone objects in one go. | 166 // Zone::Destroy() to delete all zone objects in one go. |
161 void operator delete(void*, size_t) { UNREACHABLE(); } | 167 void operator delete(void*, size_t) { UNREACHABLE(); } |
162 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 168 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
163 }; | 169 }; |
164 | 170 |
165 | 171 |
166 // The ZoneAllocationPolicy is used to specialize generic data | 172 // The ZoneAllocationPolicy is used to specialize generic data |
167 // structures to allocate themselves and their elements in the Zone. | 173 // structures to allocate themselves and their elements in the Zone. |
168 struct ZoneAllocationPolicy { | 174 struct ZoneAllocationPolicy { |
169 public: | 175 public: |
170 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } | 176 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } |
171 INLINE(void* New(size_t size)); | 177 INLINE(void* New(size_t size)); |
172 INLINE(static void Delete(void *pointer)) { } | 178 INLINE(static void Delete(void *pointer)) { } |
173 | 179 |
174 private: | 180 private: |
175 Zone* zone_; | 181 Zone* zone_; |
176 }; | 182 }; |
177 | 183 |
178 | 184 |
179 // ZoneLists are growable lists with constant-time access to the | 185 // ZoneLists are growable lists with constant-time access to the |
180 // elements. The list itself and all its elements are allocated in the | 186 // elements. The list itself and all its elements are allocated in the |
181 // Zone. ZoneLists cannot be deleted individually; you can delete all | 187 // Zone. ZoneLists cannot be deleted individually; you can delete all |
182 // objects in the Zone by calling Zone::DeleteAll(). | 188 // objects in the Zone by calling Zone::Destroy(). |
183 template<typename T> | 189 template<typename T> |
184 class ZoneList: public List<T, ZoneAllocationPolicy> { | 190 class ZoneList: public List<T, ZoneAllocationPolicy> { |
185 public: | 191 public: |
186 // Construct a new ZoneList with the given capacity; the length is | 192 // Construct a new ZoneList with the given capacity; the length is |
187 // always zero. The capacity must be non-negative. | 193 // always zero. The capacity must be non-negative. |
188 ZoneList(int capacity, Zone* zone) | 194 ZoneList(int capacity, Zone* zone) |
189 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } | 195 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } |
190 | 196 |
191 INLINE(void* operator new(size_t size, Zone* zone)); | 197 INLINE(void* operator new(size_t size, Zone* zone)); |
192 | 198 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 void operator delete(void* pointer) { UNREACHABLE(); } | 234 void operator delete(void* pointer) { UNREACHABLE(); } |
229 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 235 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
230 }; | 236 }; |
231 | 237 |
232 | 238 |
233 // ZoneScopes keep track of the current parsing and compilation | 239 // ZoneScopes keep track of the current parsing and compilation |
234 // nesting and cleans up generated ASTs in the Zone when exiting the | 240 // nesting and cleans up generated ASTs in the Zone when exiting the |
235 // outer-most scope. | 241 // outer-most scope. |
236 class ZoneScope BASE_EMBEDDED { | 242 class ZoneScope BASE_EMBEDDED { |
237 public: | 243 public: |
238 INLINE(ZoneScope(Isolate* isolate, ZoneScopeMode mode)); | 244 INLINE(ZoneScope(Zone* zone, ZoneScopeMode mode)); |
239 | 245 |
240 virtual ~ZoneScope(); | 246 virtual ~ZoneScope(); |
241 | 247 |
242 inline bool ShouldDeleteOnExit(); | 248 inline bool ShouldDeleteOnExit(); |
243 | 249 |
244 // For ZoneScopes that do not delete on exit by default, call this | 250 // For ZoneScopes that do not delete on exit by default, call this |
245 // method to request deletion on exit. | 251 // method to request deletion on exit. |
246 void DeleteOnExit() { | 252 void DeleteOnExit() { |
247 mode_ = DELETE_ON_EXIT; | 253 mode_ = DELETE_ON_EXIT; |
248 } | 254 } |
249 | 255 |
250 inline static int nesting(); | 256 inline static int nesting(); |
251 | 257 |
252 private: | 258 private: |
253 Isolate* isolate_; | 259 Zone* zone_; |
254 ZoneScopeMode mode_; | 260 ZoneScopeMode mode_; |
255 }; | 261 }; |
256 | 262 |
257 | 263 |
258 // A zone splay tree. The config type parameter encapsulates the | 264 // A zone splay tree. The config type parameter encapsulates the |
259 // different configurations of a concrete splay tree (see splay-tree.h). | 265 // different configurations of a concrete splay tree (see splay-tree.h). |
260 // 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. |
261 template <typename Config> | 267 template <typename Config> |
262 class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> { | 268 class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> { |
263 public: | 269 public: |
264 explicit ZoneSplayTree(Zone* zone) | 270 explicit ZoneSplayTree(Zone* zone) |
265 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} | 271 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} |
266 ~ZoneSplayTree(); | 272 ~ZoneSplayTree(); |
267 }; | 273 }; |
268 | 274 |
269 | 275 |
270 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 276 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
271 | 277 |
272 } } // namespace v8::internal | 278 } } // namespace v8::internal |
273 | 279 |
274 #endif // V8_ZONE_H_ | 280 #endif // V8_ZONE_H_ |
OLD | NEW |