blob: dde722f67581453310df9a0790970ee02826acc6 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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 {
32namespace internal {
33
34
35// Zone scopes are in one of two modes. Either they delete the zone
36// on exit or they do not.
37enum ZoneScopeMode {
38 DELETE_ON_EXIT,
39 DONT_DELETE_ON_EXIT
40};
41
42
43// The Zone supports very fast allocation of small chunks of
44// memory. The chunks cannot be deallocated individually, but instead
45// the Zone supports deallocating all chunks in one fast
46// operation. The Zone is used to hold temporary data structures like
47// the abstract syntax tree, which is deallocated after compilation.
48
49// Note: There is no need to initialize the Zone; the first time an
50// allocation is attempted, a segment of memory will be requested
51// through a call to malloc().
52
53// Note: The implementation is inherently not thread safe. Do not use
54// from multi-threaded code.
55
56class Zone {
57 public:
58 // Allocate 'size' bytes of memory in the Zone; expands the Zone by
59 // allocating new segments of memory on demand using malloc().
60 static inline void* New(int size);
61
62 template <typename T>
63 static inline T* NewArray(int length);
64
65 // Delete all objects and free all memory allocated in the Zone.
66 static void DeleteAll();
67
68 // Returns true if more memory has been allocated in zones than
69 // the limit allows.
70 static inline bool excess_allocation();
71
72 static inline void adjust_segment_bytes_allocated(int delta);
73
74 private:
75
76 // All pointers returned from New() have this alignment.
77 static const int kAlignment = kPointerSize;
78
79 // Never allocate segments smaller than this size in bytes.
80 static const int kMinimumSegmentSize = 8 * KB;
81
82 // Never allocate segments larger than this size in bytes.
83 static const int kMaximumSegmentSize = 1 * MB;
84
85 // Never keep segments larger than this size in bytes around.
86 static const int kMaximumKeptSegmentSize = 64 * KB;
87
88 // Report zone excess when allocation exceeds this limit.
89 static int zone_excess_limit_;
90
91 // The number of bytes allocated in segments. Note that this number
92 // includes memory allocated from the OS but not yet allocated from
93 // the zone.
94 static int segment_bytes_allocated_;
95
96 // The Zone is intentionally a singleton; you should not try to
97 // allocate instances of the class.
98 Zone() { UNREACHABLE(); }
99
100
101 // Expand the Zone to hold at least 'size' more bytes and allocate
102 // the bytes. Returns the address of the newly allocated chunk of
103 // memory in the Zone. Should only be called if there isn't enough
104 // room in the Zone already.
105 static Address NewExpand(int size);
106
107
108 // The free region in the current (front) segment is represented as
109 // the half-open interval [position, limit). The 'position' variable
110 // is guaranteed to be aligned as dictated by kAlignment.
111 static Address position_;
112 static Address limit_;
113};
114
115
116// ZoneObject is an abstraction that helps define classes of objects
117// allocated in the Zone. Use it as a base class; see ast.h.
118class ZoneObject {
119 public:
120 // Allocate a new ZoneObject of 'size' bytes in the Zone.
Steve Blockd0582a62009-12-15 09:54:21 +0000121 void* operator new(size_t size) { return Zone::New(static_cast<int>(size)); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
123 // Ideally, the delete operator should be private instead of
124 // public, but unfortunately the compiler sometimes synthesizes
125 // (unused) destructors for classes derived from ZoneObject, which
126 // require the operator to be visible. MSVC requires the delete
127 // operator to be public.
128
129 // ZoneObjects should never be deleted individually; use
130 // Zone::DeleteAll() to delete all zone objects in one go.
131 void operator delete(void*, size_t) { UNREACHABLE(); }
132};
133
134
135class AssertNoZoneAllocation {
136 public:
137 AssertNoZoneAllocation() : prev_(allow_allocation_) {
138 allow_allocation_ = false;
139 }
140 ~AssertNoZoneAllocation() { allow_allocation_ = prev_; }
141 static bool allow_allocation() { return allow_allocation_; }
142 private:
143 bool prev_;
144 static bool allow_allocation_;
145};
146
147
148// The ZoneListAllocationPolicy is used to specialize the GenericList
149// implementation to allocate ZoneLists and their elements in the
150// Zone.
151class ZoneListAllocationPolicy {
152 public:
153 // Allocate 'size' bytes of memory in the zone.
154 static void* New(int size) { return Zone::New(size); }
155
156 // De-allocation attempts are silently ignored.
157 static void Delete(void* p) { }
158};
159
160
161// ZoneLists are growable lists with constant-time access to the
162// elements. The list itself and all its elements are allocated in the
163// Zone. ZoneLists cannot be deleted individually; you can delete all
164// objects in the Zone by calling Zone::DeleteAll().
165template<typename T>
166class ZoneList: public List<T, ZoneListAllocationPolicy> {
167 public:
168 // Construct a new ZoneList with the given capacity; the length is
169 // always zero. The capacity must be non-negative.
170 explicit ZoneList(int capacity)
171 : List<T, ZoneListAllocationPolicy>(capacity) { }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100172
173 // Construct a new ZoneList by copying the elements of the given ZoneList.
174 explicit ZoneList(const ZoneList<T>& other)
175 : List<T, ZoneListAllocationPolicy>(other.length()) {
176 AddAll(other);
177 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000178};
179
180
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181// Introduce a convenience type for zone lists of map handles.
182typedef ZoneList<Handle<Map> > ZoneMapList;
183
184
Steve Blocka7e24c12009-10-30 11:49:00 +0000185// ZoneScopes keep track of the current parsing and compilation
186// nesting and cleans up generated ASTs in the Zone when exiting the
187// outer-most scope.
188class ZoneScope BASE_EMBEDDED {
189 public:
190 explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) {
191 nesting_++;
192 }
193
194 virtual ~ZoneScope() {
195 if (ShouldDeleteOnExit()) Zone::DeleteAll();
196 --nesting_;
197 }
198
199 bool ShouldDeleteOnExit() {
200 return nesting_ == 1 && mode_ == DELETE_ON_EXIT;
201 }
202
203 // For ZoneScopes that do not delete on exit by default, call this
204 // method to request deletion on exit.
205 void DeleteOnExit() {
206 mode_ = DELETE_ON_EXIT;
207 }
208
209 static int nesting() { return nesting_; }
210
211 private:
212 ZoneScopeMode mode_;
213 static int nesting_;
214};
215
216
217// A zone splay tree. The config type parameter encapsulates the
Steve Block6ded16b2010-05-10 14:33:55 +0100218// different configurations of a concrete splay tree (see splay-tree.h).
219// The tree itself and all its elements are allocated in the Zone.
Steve Blocka7e24c12009-10-30 11:49:00 +0000220template <typename Config>
Steve Block6ded16b2010-05-10 14:33:55 +0100221class ZoneSplayTree: public SplayTree<Config, ZoneListAllocationPolicy> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 public:
Steve Block6ded16b2010-05-10 14:33:55 +0100223 ZoneSplayTree()
224 : SplayTree<Config, ZoneListAllocationPolicy>() {}
225 ~ZoneSplayTree();
Steve Blocka7e24c12009-10-30 11:49:00 +0000226};
227
228
229} } // namespace v8::internal
230
231#endif // V8_ZONE_H_