blob: e299f158a8f03d341a04f8854be87ea07f1edef4 [file] [log] [blame]
Ben Murdochb8e0da22011-05-16 14:20:40 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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 {
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
Ben Murdochb8e0da22011-05-16 14:20:40 +010074 static unsigned allocation_size_;
75
Steve Blocka7e24c12009-10-30 11:49:00 +000076 private:
77
78 // All pointers returned from New() have this alignment.
79 static const int kAlignment = kPointerSize;
80
81 // Never allocate segments smaller than this size in bytes.
82 static const int kMinimumSegmentSize = 8 * KB;
83
84 // Never allocate segments larger than this size in bytes.
85 static const int kMaximumSegmentSize = 1 * MB;
86
87 // Never keep segments larger than this size in bytes around.
88 static const int kMaximumKeptSegmentSize = 64 * KB;
89
90 // Report zone excess when allocation exceeds this limit.
91 static int zone_excess_limit_;
92
93 // The number of bytes allocated in segments. Note that this number
94 // includes memory allocated from the OS but not yet allocated from
95 // the zone.
96 static int segment_bytes_allocated_;
97
98 // The Zone is intentionally a singleton; you should not try to
99 // allocate instances of the class.
100 Zone() { UNREACHABLE(); }
101
102
103 // Expand the Zone to hold at least 'size' more bytes and allocate
104 // the bytes. Returns the address of the newly allocated chunk of
105 // memory in the Zone. Should only be called if there isn't enough
106 // room in the Zone already.
107 static Address NewExpand(int size);
108
109
110 // The free region in the current (front) segment is represented as
111 // the half-open interval [position, limit). The 'position' variable
112 // is guaranteed to be aligned as dictated by kAlignment.
113 static Address position_;
114 static Address limit_;
115};
116
117
118// ZoneObject is an abstraction that helps define classes of objects
119// allocated in the Zone. Use it as a base class; see ast.h.
120class ZoneObject {
121 public:
122 // Allocate a new ZoneObject of 'size' bytes in the Zone.
Steve Blockd0582a62009-12-15 09:54:21 +0000123 void* operator new(size_t size) { return Zone::New(static_cast<int>(size)); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
125 // Ideally, the delete operator should be private instead of
126 // public, but unfortunately the compiler sometimes synthesizes
127 // (unused) destructors for classes derived from ZoneObject, which
128 // require the operator to be visible. MSVC requires the delete
129 // operator to be public.
130
131 // ZoneObjects should never be deleted individually; use
132 // Zone::DeleteAll() to delete all zone objects in one go.
133 void operator delete(void*, size_t) { UNREACHABLE(); }
134};
135
136
137class AssertNoZoneAllocation {
138 public:
139 AssertNoZoneAllocation() : prev_(allow_allocation_) {
140 allow_allocation_ = false;
141 }
142 ~AssertNoZoneAllocation() { allow_allocation_ = prev_; }
143 static bool allow_allocation() { return allow_allocation_; }
144 private:
145 bool prev_;
146 static bool allow_allocation_;
147};
148
149
150// The ZoneListAllocationPolicy is used to specialize the GenericList
151// implementation to allocate ZoneLists and their elements in the
152// Zone.
153class ZoneListAllocationPolicy {
154 public:
155 // Allocate 'size' bytes of memory in the zone.
156 static void* New(int size) { return Zone::New(size); }
157
158 // De-allocation attempts are silently ignored.
159 static void Delete(void* p) { }
160};
161
162
163// ZoneLists are growable lists with constant-time access to the
164// elements. The list itself and all its elements are allocated in the
165// Zone. ZoneLists cannot be deleted individually; you can delete all
166// objects in the Zone by calling Zone::DeleteAll().
167template<typename T>
168class ZoneList: public List<T, ZoneListAllocationPolicy> {
169 public:
170 // Construct a new ZoneList with the given capacity; the length is
171 // always zero. The capacity must be non-negative.
172 explicit ZoneList(int capacity)
173 : List<T, ZoneListAllocationPolicy>(capacity) { }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100174
175 // Construct a new ZoneList by copying the elements of the given ZoneList.
176 explicit ZoneList(const ZoneList<T>& other)
177 : List<T, ZoneListAllocationPolicy>(other.length()) {
178 AddAll(other);
179 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000180};
181
182
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183// Introduce a convenience type for zone lists of map handles.
184typedef ZoneList<Handle<Map> > ZoneMapList;
185
186
Steve Blocka7e24c12009-10-30 11:49:00 +0000187// ZoneScopes keep track of the current parsing and compilation
188// nesting and cleans up generated ASTs in the Zone when exiting the
189// outer-most scope.
190class ZoneScope BASE_EMBEDDED {
191 public:
192 explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) {
193 nesting_++;
194 }
195
196 virtual ~ZoneScope() {
197 if (ShouldDeleteOnExit()) Zone::DeleteAll();
198 --nesting_;
199 }
200
201 bool ShouldDeleteOnExit() {
202 return nesting_ == 1 && mode_ == DELETE_ON_EXIT;
203 }
204
205 // For ZoneScopes that do not delete on exit by default, call this
206 // method to request deletion on exit.
207 void DeleteOnExit() {
208 mode_ = DELETE_ON_EXIT;
209 }
210
211 static int nesting() { return nesting_; }
212
213 private:
214 ZoneScopeMode mode_;
215 static int nesting_;
216};
217
218
219// A zone splay tree. The config type parameter encapsulates the
Steve Block6ded16b2010-05-10 14:33:55 +0100220// different configurations of a concrete splay tree (see splay-tree.h).
221// The tree itself and all its elements are allocated in the Zone.
Steve Blocka7e24c12009-10-30 11:49:00 +0000222template <typename Config>
Steve Block6ded16b2010-05-10 14:33:55 +0100223class ZoneSplayTree: public SplayTree<Config, ZoneListAllocationPolicy> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 public:
Steve Block6ded16b2010-05-10 14:33:55 +0100225 ZoneSplayTree()
226 : SplayTree<Config, ZoneListAllocationPolicy>() {}
227 ~ZoneSplayTree();
Steve Blocka7e24c12009-10-30 11:49:00 +0000228};
229
230
231} } // namespace v8::internal
232
233#endif // V8_ZONE_H_