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 21 matching lines...) Expand all Loading... |
32 #include "checks.h" | 32 #include "checks.h" |
33 #include "hashmap.h" | 33 #include "hashmap.h" |
34 #include "globals.h" | 34 #include "globals.h" |
35 #include "list.h" | 35 #include "list.h" |
36 #include "splay-tree.h" | 36 #include "splay-tree.h" |
37 | 37 |
38 namespace v8 { | 38 namespace v8 { |
39 namespace internal { | 39 namespace internal { |
40 | 40 |
41 | 41 |
42 // Zone scopes are in one of two modes. Either they delete the zone | |
43 // on exit or they do not. | |
44 enum ZoneScopeMode { | |
45 DELETE_ON_EXIT, | |
46 DONT_DELETE_ON_EXIT | |
47 }; | |
48 | |
49 class Segment; | 42 class Segment; |
50 class Isolate; | 43 class Isolate; |
51 | 44 |
52 // The Zone supports very fast allocation of small chunks of | 45 // The Zone supports very fast allocation of small chunks of |
53 // memory. The chunks cannot be deallocated individually, but instead | 46 // memory. The chunks cannot be deallocated individually, but instead |
54 // the Zone supports deallocating all chunks in one fast | 47 // the Zone supports deallocating all chunks in one fast |
55 // operation. The Zone is used to hold temporary data structures like | 48 // operation. The Zone is used to hold temporary data structures like |
56 // the abstract syntax tree, which is deallocated after compilation. | 49 // the abstract syntax tree, which is deallocated after compilation. |
57 | 50 |
58 // Note: There is no need to initialize the Zone; the first time an | 51 // Note: There is no need to initialize the Zone; the first time an |
59 // allocation is attempted, a segment of memory will be requested | 52 // allocation is attempted, a segment of memory will be requested |
60 // through a call to malloc(). | 53 // through a call to malloc(). |
61 | 54 |
62 // Note: The implementation is inherently not thread safe. Do not use | 55 // Note: The implementation is inherently not thread safe. Do not use |
63 // from multi-threaded code. | 56 // from multi-threaded code. |
64 | 57 |
65 class Zone { | 58 class Zone { |
66 public: | 59 public: |
67 explicit Zone(Isolate* isolate); | 60 explicit Zone(Isolate* isolate); |
68 ~Zone() { DeleteKeptSegment(); } | 61 ~Zone(); |
69 // Allocate 'size' bytes of memory in the Zone; expands the Zone by | 62 // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
70 // allocating new segments of memory on demand using malloc(). | 63 // allocating new segments of memory on demand using malloc(). |
71 inline void* New(int size); | 64 inline void* New(int size); |
72 | 65 |
73 template <typename T> | 66 template <typename T> |
74 inline T* NewArray(int length); | 67 inline T* NewArray(int length); |
75 | 68 |
76 // Deletes all objects and free all memory allocated in the Zone. Keeps one | |
77 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. | |
78 void DeleteAll(); | |
79 | |
80 // Deletes the last small segment kept around by DeleteAll(). | |
81 void DeleteKeptSegment(); | |
82 | |
83 // Returns true if more memory has been allocated in zones than | 69 // Returns true if more memory has been allocated in zones than |
84 // the limit allows. | 70 // the limit allows. |
85 inline bool excess_allocation(); | 71 inline bool excess_allocation(); |
86 | 72 |
87 inline void adjust_segment_bytes_allocated(int delta); | 73 inline void adjust_segment_bytes_allocated(int delta); |
88 | 74 |
89 inline unsigned allocation_size() { return allocation_size_; } | 75 inline unsigned allocation_size() { return allocation_size_; } |
90 | 76 |
91 inline Isolate* isolate() { return isolate_; } | 77 inline Isolate* isolate() { return isolate_; } |
92 | 78 |
93 private: | 79 private: |
94 friend class Isolate; | 80 friend class Isolate; |
95 friend class ZoneScope; | |
96 | 81 |
97 // All pointers returned from New() have this alignment. In addition, if the | 82 // All pointers returned from New() have this alignment. In addition, if the |
98 // object being allocated has a size that is divisible by 8 then its alignment | 83 // object being allocated has a size that is divisible by 8 then its alignment |
99 // will be 8. | 84 // will be 8. |
100 static const int kAlignment = kPointerSize; | 85 static const int kAlignment = kPointerSize; |
101 | 86 |
102 // Never allocate segments smaller than this size in bytes. | 87 // Never allocate segments smaller than this size in bytes. |
103 static const int kMinimumSegmentSize = 8 * KB; | 88 static const int kMinimumSegmentSize = 8 * KB; |
104 | 89 |
105 // Never allocate segments larger than this size in bytes. | 90 // Never allocate segments larger than this size in bytes. |
106 static const int kMaximumSegmentSize = 1 * MB; | 91 static const int kMaximumSegmentSize = 1 * MB; |
107 | 92 |
108 // Never keep segments larger than this size in bytes around. | |
109 static const int kMaximumKeptSegmentSize = 64 * KB; | |
110 | |
111 // Report zone excess when allocation exceeds this limit. | 93 // Report zone excess when allocation exceeds this limit. |
112 int zone_excess_limit_; | 94 int zone_excess_limit_; |
113 | 95 |
114 // The number of bytes allocated in this zone so far. | 96 // The number of bytes allocated in this zone so far. |
115 unsigned allocation_size_; | 97 unsigned allocation_size_; |
116 | 98 |
117 // The number of bytes allocated in segments. Note that this number | 99 // The number of bytes allocated in segments. Note that this number |
118 // includes memory allocated from the OS but not yet allocated from | 100 // includes memory allocated from the OS but not yet allocated from |
119 // the zone. | 101 // the zone. |
120 int segment_bytes_allocated_; | 102 int segment_bytes_allocated_; |
(...skipping 10 matching lines...) Expand all Loading... |
131 | 113 |
132 // Deletes the given segment. Does not touch the segment chain. | 114 // Deletes the given segment. Does not touch the segment chain. |
133 void DeleteSegment(Segment* segment, int size); | 115 void DeleteSegment(Segment* segment, int size); |
134 | 116 |
135 // The free region in the current (front) segment is represented as | 117 // The free region in the current (front) segment is represented as |
136 // the half-open interval [position, limit). The 'position' variable | 118 // the half-open interval [position, limit). The 'position' variable |
137 // is guaranteed to be aligned as dictated by kAlignment. | 119 // is guaranteed to be aligned as dictated by kAlignment. |
138 Address position_; | 120 Address position_; |
139 Address limit_; | 121 Address limit_; |
140 | 122 |
141 int scope_nesting_; | |
142 | |
143 Segment* segment_head_; | 123 Segment* segment_head_; |
144 Isolate* isolate_; | 124 Isolate* isolate_; |
145 }; | 125 }; |
146 | 126 |
147 | 127 |
148 // ZoneObject is an abstraction that helps define classes of objects | 128 // ZoneObject is an abstraction that helps define classes of objects |
149 // allocated in the Zone. Use it as a base class; see ast.h. | 129 // allocated in the Zone. Use it as a base class; see ast.h. |
150 class ZoneObject { | 130 class ZoneObject { |
151 public: | 131 public: |
152 // Allocate a new ZoneObject of 'size' bytes in the Zone. | 132 // Allocate a new ZoneObject of 'size' bytes in the Zone. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 INLINE(void Initialize(int capacity, Zone* zone)) { | 205 INLINE(void Initialize(int capacity, Zone* zone)) { |
226 List<T, ZoneAllocationPolicy>::Initialize(capacity, | 206 List<T, ZoneAllocationPolicy>::Initialize(capacity, |
227 ZoneAllocationPolicy(zone)); | 207 ZoneAllocationPolicy(zone)); |
228 } | 208 } |
229 | 209 |
230 void operator delete(void* pointer) { UNREACHABLE(); } | 210 void operator delete(void* pointer) { UNREACHABLE(); } |
231 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } | 211 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
232 }; | 212 }; |
233 | 213 |
234 | 214 |
235 // ZoneScopes keep track of the current parsing and compilation | |
236 // nesting and cleans up generated ASTs in the Zone when exiting the | |
237 // outer-most scope. | |
238 class ZoneScope BASE_EMBEDDED { | |
239 public: | |
240 INLINE(ZoneScope(Zone* zone, ZoneScopeMode mode)); | |
241 | |
242 virtual ~ZoneScope(); | |
243 | |
244 Zone* zone() const { return zone_; } | |
245 | |
246 inline bool ShouldDeleteOnExit(); | |
247 | |
248 // For ZoneScopes that do not delete on exit by default, call this | |
249 // method to request deletion on exit. | |
250 void DeleteOnExit() { | |
251 mode_ = DELETE_ON_EXIT; | |
252 } | |
253 | |
254 inline static int nesting(); | |
255 | |
256 private: | |
257 Zone* zone_; | |
258 ZoneScopeMode mode_; | |
259 }; | |
260 | |
261 | |
262 // A zone splay tree. The config type parameter encapsulates the | 215 // A zone splay tree. The config type parameter encapsulates the |
263 // different configurations of a concrete splay tree (see splay-tree.h). | 216 // different configurations of a concrete splay tree (see splay-tree.h). |
264 // 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. |
265 template <typename Config> | 218 template <typename Config> |
266 class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> { | 219 class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> { |
267 public: | 220 public: |
268 explicit ZoneSplayTree(Zone* zone) | 221 explicit ZoneSplayTree(Zone* zone) |
269 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} | 222 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} |
270 ~ZoneSplayTree(); | 223 ~ZoneSplayTree(); |
271 }; | 224 }; |
272 | 225 |
273 | 226 |
274 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; | 227 typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
275 | 228 |
276 } } // namespace v8::internal | 229 } } // namespace v8::internal |
277 | 230 |
278 #endif // V8_ZONE_H_ | 231 #endif // V8_ZONE_H_ |
OLD | NEW |