blob: 01e887e779d70377db2c8a80e653e94b53d7f2e9 [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_ZONE_H_
29#define V8_ZONE_H_
30
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
danno@chromium.orgfa458e42012-02-01 10:48:36 +000032#include "checks.h"
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000033#include "hashmap.h"
danno@chromium.orgfa458e42012-02-01 10:48:36 +000034#include "globals.h"
35#include "list.h"
36#include "splay-tree.h"
lrn@chromium.org1c092762011-05-09 09:42:16 +000037
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000042// Zone scopes are in one of two modes. Either they delete the zone
43// on exit or they do not.
44enum ZoneScopeMode {
45 DELETE_ON_EXIT,
46 DONT_DELETE_ON_EXIT
47};
48
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049class Segment;
danno@chromium.orgfa458e42012-02-01 10:48:36 +000050class Isolate;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000051
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052// The Zone supports very fast allocation of small chunks of
53// memory. The chunks cannot be deallocated individually, but instead
54// the Zone supports deallocating all chunks in one fast
55// operation. The Zone is used to hold temporary data structures like
56// the abstract syntax tree, which is deallocated after compilation.
57
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
60// through a call to malloc().
61
62// Note: The implementation is inherently not thread safe. Do not use
63// from multi-threaded code.
64
65class Zone {
66 public:
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000067 explicit Zone(Isolate* isolate);
68 ~Zone() { DeleteKeptSegment(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069 // Allocate 'size' bytes of memory in the Zone; expands the Zone by
70 // allocating new segments of memory on demand using malloc().
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000071 inline void* New(int size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000072
ager@chromium.org41826e72009-03-30 13:30:57 +000073 template <typename T>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 inline T* NewArray(int length);
ager@chromium.org41826e72009-03-30 13:30:57 +000075
danno@chromium.orgb6451162011-08-17 14:33:23 +000076 // Deletes all objects and free all memory allocated in the Zone. Keeps one
77 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000078 void DeleteAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
danno@chromium.orgb6451162011-08-17 14:33:23 +000080 // Deletes the last small segment kept around by DeleteAll().
81 void DeleteKeptSegment();
82
ager@chromium.orga74f0da2008-12-03 16:05:52 +000083 // Returns true if more memory has been allocated in zones than
84 // the limit allows.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000085 inline bool excess_allocation();
ager@chromium.orga74f0da2008-12-03 16:05:52 +000086
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000087 inline void adjust_segment_bytes_allocated(int delta);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000088
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000089 inline Isolate* isolate() { return isolate_; }
90
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000091 static unsigned allocation_size_;
92
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000094 friend class Isolate;
95 friend class ZoneScope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000096
danno@chromium.org50d79f32011-11-11 12:01:18 +000097 // 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
99 // will be 8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 static const int kAlignment = kPointerSize;
101
102 // Never allocate segments smaller than this size in bytes.
103 static const int kMinimumSegmentSize = 8 * KB;
104
ager@chromium.org8bb60582008-12-11 12:02:20 +0000105 // Never allocate segments larger than this size in bytes.
106 static const int kMaximumSegmentSize = 1 * MB;
107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 // Never keep segments larger than this size in bytes around.
109 static const int kMaximumKeptSegmentSize = 64 * KB;
110
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000111 // Report zone excess when allocation exceeds this limit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000112 int zone_excess_limit_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000113
114 // The number of bytes allocated in segments. Note that this number
115 // includes memory allocated from the OS but not yet allocated from
116 // the zone.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 int segment_bytes_allocated_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 // Expand the Zone to hold at least 'size' more bytes and allocate
120 // the bytes. Returns the address of the newly allocated chunk of
121 // memory in the Zone. Should only be called if there isn't enough
122 // room in the Zone already.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000123 Address NewExpand(int size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125 // Creates a new segment, sets it size, and pushes it to the front
126 // of the segment chain. Returns the new segment.
127 Segment* NewSegment(int size);
128
129 // Deletes the given segment. Does not touch the segment chain.
130 void DeleteSegment(Segment* segment, int size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131
132 // The free region in the current (front) segment is represented as
133 // the half-open interval [position, limit). The 'position' variable
134 // is guaranteed to be aligned as dictated by kAlignment.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000135 Address position_;
136 Address limit_;
137
138 int scope_nesting_;
139
140 Segment* segment_head_;
141 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142};
143
144
145// ZoneObject is an abstraction that helps define classes of objects
146// allocated in the Zone. Use it as a base class; see ast.h.
147class ZoneObject {
148 public:
149 // Allocate a new ZoneObject of 'size' bytes in the Zone.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000150 INLINE(void* operator new(size_t size, Zone* zone));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151
152 // Ideally, the delete operator should be private instead of
ager@chromium.org32912102009-01-16 10:38:43 +0000153 // public, but unfortunately the compiler sometimes synthesizes
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 // (unused) destructors for classes derived from ZoneObject, which
155 // require the operator to be visible. MSVC requires the delete
156 // operator to be public.
157
158 // ZoneObjects should never be deleted individually; use
159 // Zone::DeleteAll() to delete all zone objects in one go.
160 void operator delete(void*, size_t) { UNREACHABLE(); }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000161 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162};
163
164
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000165// The ZoneAllocationPolicy is used to specialize generic data
166// structures to allocate themselves and their elements in the Zone.
167struct ZoneAllocationPolicy {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000169 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000170 INLINE(void* New(size_t size));
171 INLINE(static void Delete(void *pointer)) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000173 private:
174 Zone* zone_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175};
176
177
178// ZoneLists are growable lists with constant-time access to the
179// elements. The list itself and all its elements are allocated in the
180// Zone. ZoneLists cannot be deleted individually; you can delete all
181// objects in the Zone by calling Zone::DeleteAll().
182template<typename T>
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000183class ZoneList: public List<T, ZoneAllocationPolicy> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 public:
185 // Construct a new ZoneList with the given capacity; the length is
186 // always zero. The capacity must be non-negative.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000187 ZoneList(int capacity, Zone* zone)
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000188 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { }
189
190 INLINE(void* operator new(size_t size, Zone* zone));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000191
192 // Construct a new ZoneList by copying the elements of the given ZoneList.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000193 ZoneList(const ZoneList<T>& other, Zone* zone)
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000194 : List<T, ZoneAllocationPolicy>(other.length(),
195 ZoneAllocationPolicy(zone)) {
196 AddAll(other, ZoneAllocationPolicy(zone));
197 }
198
199 // We add some convenience wrappers so that we can pass in a Zone
200 // instead of a (less convenient) ZoneAllocationPolicy.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000201 INLINE(void Add(const T& element, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000202 List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone));
203 }
204 INLINE(void AddAll(const List<T, ZoneAllocationPolicy>& other,
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000205 Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000206 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
207 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000208 INLINE(void AddAll(const Vector<T>& other, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000209 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
210 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000211 INLINE(void InsertAt(int index, const T& element, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000212 List<T, ZoneAllocationPolicy>::InsertAt(index, element,
213 ZoneAllocationPolicy(zone));
214 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000215 INLINE(Vector<T> AddBlock(T value, int count, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000216 return List<T, ZoneAllocationPolicy>::AddBlock(value, count,
217 ZoneAllocationPolicy(zone));
218 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000219 INLINE(void Allocate(int length, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000220 List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone));
221 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000222 INLINE(void Initialize(int capacity, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000223 List<T, ZoneAllocationPolicy>::Initialize(capacity,
224 ZoneAllocationPolicy(zone));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000225 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000226
227 void operator delete(void* pointer) { UNREACHABLE(); }
228 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229};
230
231
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000232// ZoneScopes keep track of the current parsing and compilation
233// nesting and cleans up generated ASTs in the Zone when exiting the
234// outer-most scope.
235class ZoneScope BASE_EMBEDDED {
236 public:
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000237 INLINE(ZoneScope(Zone* zone, ZoneScopeMode mode));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000238
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000239 virtual ~ZoneScope();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000240
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000241 inline bool ShouldDeleteOnExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000242
243 // For ZoneScopes that do not delete on exit by default, call this
244 // method to request deletion on exit.
245 void DeleteOnExit() {
246 mode_ = DELETE_ON_EXIT;
247 }
248
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000249 inline static int nesting();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000250
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000251 private:
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000252 Zone* zone_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000253 ZoneScopeMode mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000254};
255
256
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000257// A zone splay tree. The config type parameter encapsulates the
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000258// different configurations of a concrete splay tree (see splay-tree.h).
259// The tree itself and all its elements are allocated in the Zone.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000260template <typename Config>
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000261class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000262 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000263 explicit ZoneSplayTree(Zone* zone)
264 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {}
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000265 ~ZoneSplayTree();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000266};
267
268
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000269typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000270
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271} } // namespace v8::internal
272
273#endif // V8_ZONE_H_