blob: 612819e5425bc40b16f5666b62b019b9ce52b2c8 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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
31namespace v8 { namespace internal {
32
33
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000034// Zone scopes are in one of two modes. Either they delete the zone
35// on exit or they do not.
36enum ZoneScopeMode {
37 DELETE_ON_EXIT,
38 DONT_DELETE_ON_EXIT
39};
40
41
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042// The Zone supports very fast allocation of small chunks of
43// memory. The chunks cannot be deallocated individually, but instead
44// the Zone supports deallocating all chunks in one fast
45// operation. The Zone is used to hold temporary data structures like
46// the abstract syntax tree, which is deallocated after compilation.
47
48// Note: There is no need to initialize the Zone; the first time an
49// allocation is attempted, a segment of memory will be requested
50// through a call to malloc().
51
52// Note: The implementation is inherently not thread safe. Do not use
53// from multi-threaded code.
54
55class Zone {
56 public:
57 // Allocate 'size' bytes of memory in the Zone; expands the Zone by
58 // allocating new segments of memory on demand using malloc().
59 static inline void* New(int size);
60
61 // Delete all objects and free all memory allocated in the Zone.
62 static void DeleteAll();
63
ager@chromium.orga74f0da2008-12-03 16:05:52 +000064 // Returns true if more memory has been allocated in zones than
65 // the limit allows.
66 static inline bool excess_allocation();
67
68 static inline void adjust_segment_bytes_allocated(int delta);
69
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070 private:
ager@chromium.orga74f0da2008-12-03 16:05:52 +000071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000072 // All pointers returned from New() have this alignment.
73 static const int kAlignment = kPointerSize;
74
75 // Never allocate segments smaller than this size in bytes.
76 static const int kMinimumSegmentSize = 8 * KB;
77
78 // Never keep segments larger than this size in bytes around.
79 static const int kMaximumKeptSegmentSize = 64 * KB;
80
ager@chromium.orga74f0da2008-12-03 16:05:52 +000081 // Report zone excess when allocation exceeds this limit.
82 static int zone_excess_limit_;
83
84 // The number of bytes allocated in segments. Note that this number
85 // includes memory allocated from the OS but not yet allocated from
86 // the zone.
87 static int segment_bytes_allocated_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088
89 // The Zone is intentionally a singleton; you should not try to
90 // allocate instances of the class.
91 Zone() { UNREACHABLE(); }
92
93
94 // Expand the Zone to hold at least 'size' more bytes and allocate
95 // the bytes. Returns the address of the newly allocated chunk of
96 // memory in the Zone. Should only be called if there isn't enough
97 // room in the Zone already.
98 static Address NewExpand(int size);
99
100
101 // The free region in the current (front) segment is represented as
102 // the half-open interval [position, limit). The 'position' variable
103 // is guaranteed to be aligned as dictated by kAlignment.
104 static Address position_;
105 static Address limit_;
106};
107
108
109// ZoneObject is an abstraction that helps define classes of objects
110// allocated in the Zone. Use it as a base class; see ast.h.
111class ZoneObject {
112 public:
113 // Allocate a new ZoneObject of 'size' bytes in the Zone.
114 void* operator new(size_t size) { return Zone::New(size); }
115
116 // Ideally, the delete operator should be private instead of
117 // public, but unfortuately the compiler sometimes synthesizes
118 // (unused) destructors for classes derived from ZoneObject, which
119 // require the operator to be visible. MSVC requires the delete
120 // operator to be public.
121
122 // ZoneObjects should never be deleted individually; use
123 // Zone::DeleteAll() to delete all zone objects in one go.
124 void operator delete(void*, size_t) { UNREACHABLE(); }
125};
126
127
128class AssertNoZoneAllocation {
129 public:
130 AssertNoZoneAllocation() : prev_(allow_allocation_) {
131 allow_allocation_ = false;
132 }
133 ~AssertNoZoneAllocation() { allow_allocation_ = prev_; }
134 static bool allow_allocation() { return allow_allocation_; }
135 private:
136 bool prev_;
137 static bool allow_allocation_;
138};
139
140
141// The ZoneListAllocationPolicy is used to specialize the GenericList
142// implementation to allocate ZoneLists and their elements in the
143// Zone.
144class ZoneListAllocationPolicy {
145 public:
146 // Allocate 'size' bytes of memory in the zone.
147 static void* New(int size) { return Zone::New(size); }
148
149 // De-allocation attempts are silently ignored.
150 static void Delete(void* p) { }
151};
152
153
154// ZoneLists are growable lists with constant-time access to the
155// elements. The list itself and all its elements are allocated in the
156// Zone. ZoneLists cannot be deleted individually; you can delete all
157// objects in the Zone by calling Zone::DeleteAll().
158template<typename T>
159class ZoneList: public List<T, ZoneListAllocationPolicy> {
160 public:
161 // Construct a new ZoneList with the given capacity; the length is
162 // always zero. The capacity must be non-negative.
163 explicit ZoneList(int capacity)
164 : List<T, ZoneListAllocationPolicy>(capacity) { }
165};
166
167
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000168// ZoneScopes keep track of the current parsing and compilation
169// nesting and cleans up generated ASTs in the Zone when exiting the
170// outer-most scope.
171class ZoneScope BASE_EMBEDDED {
172 public:
173 explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) {
174 nesting_++;
175 }
176
177 ~ZoneScope() {
178 if (--nesting_ == 0 && mode_ == DELETE_ON_EXIT) Zone::DeleteAll();
179 }
180
181 // For ZoneScopes that do not delete on exit by default, call this
182 // method to request deletion on exit.
183 void DeleteOnExit() {
184 mode_ = DELETE_ON_EXIT;
185 }
186
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000187 static int nesting() { return nesting_; }
188
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000189 private:
190 ZoneScopeMode mode_;
191 static int nesting_;
192};
193
194
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195} } // namespace v8::internal
196
197#endif // V8_ZONE_H_