blob: 13b55c4c7f3a11d1223f2cd7c0c900392f137147 [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
Steve Block44f0eee2011-05-26 01:26:41 +010042class Segment;
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44// The Zone supports very fast allocation of small chunks of
45// memory. The chunks cannot be deallocated individually, but instead
46// the Zone supports deallocating all chunks in one fast
47// operation. The Zone is used to hold temporary data structures like
48// the abstract syntax tree, which is deallocated after compilation.
49
50// Note: There is no need to initialize the Zone; the first time an
51// allocation is attempted, a segment of memory will be requested
52// through a call to malloc().
53
54// Note: The implementation is inherently not thread safe. Do not use
55// from multi-threaded code.
56
57class Zone {
58 public:
59 // Allocate 'size' bytes of memory in the Zone; expands the Zone by
60 // allocating new segments of memory on demand using malloc().
Steve Block44f0eee2011-05-26 01:26:41 +010061 inline void* New(int size);
Steve Blocka7e24c12009-10-30 11:49:00 +000062
63 template <typename T>
Steve Block44f0eee2011-05-26 01:26:41 +010064 inline T* NewArray(int length);
Steve Blocka7e24c12009-10-30 11:49:00 +000065
66 // Delete all objects and free all memory allocated in the Zone.
Steve Block44f0eee2011-05-26 01:26:41 +010067 void DeleteAll();
Steve Blocka7e24c12009-10-30 11:49:00 +000068
69 // Returns true if more memory has been allocated in zones than
70 // the limit allows.
Steve Block44f0eee2011-05-26 01:26:41 +010071 inline bool excess_allocation();
Steve Blocka7e24c12009-10-30 11:49:00 +000072
Steve Block44f0eee2011-05-26 01:26:41 +010073 inline void adjust_segment_bytes_allocated(int delta);
Steve Blocka7e24c12009-10-30 11:49:00 +000074
Ben Murdochb8e0da22011-05-16 14:20:40 +010075 static unsigned allocation_size_;
76
Steve Blocka7e24c12009-10-30 11:49:00 +000077 private:
Steve Block44f0eee2011-05-26 01:26:41 +010078 friend class Isolate;
79 friend class ZoneScope;
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81 // All pointers returned from New() have this alignment.
82 static const int kAlignment = kPointerSize;
83
84 // Never allocate segments smaller than this size in bytes.
85 static const int kMinimumSegmentSize = 8 * KB;
86
87 // Never allocate segments larger than this size in bytes.
88 static const int kMaximumSegmentSize = 1 * MB;
89
90 // Never keep segments larger than this size in bytes around.
91 static const int kMaximumKeptSegmentSize = 64 * KB;
92
93 // Report zone excess when allocation exceeds this limit.
Steve Block44f0eee2011-05-26 01:26:41 +010094 int zone_excess_limit_;
Steve Blocka7e24c12009-10-30 11:49:00 +000095
96 // The number of bytes allocated in segments. Note that this number
97 // includes memory allocated from the OS but not yet allocated from
98 // the zone.
Steve Block44f0eee2011-05-26 01:26:41 +010099 int segment_bytes_allocated_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000100
Steve Block44f0eee2011-05-26 01:26:41 +0100101 // Each isolate gets its own zone.
102 Zone();
Steve Blocka7e24c12009-10-30 11:49:00 +0000103
104 // Expand the Zone to hold at least 'size' more bytes and allocate
105 // the bytes. Returns the address of the newly allocated chunk of
106 // memory in the Zone. Should only be called if there isn't enough
107 // room in the Zone already.
Steve Block44f0eee2011-05-26 01:26:41 +0100108 Address NewExpand(int size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
Steve Block44f0eee2011-05-26 01:26:41 +0100110 // Creates a new segment, sets it size, and pushes it to the front
111 // of the segment chain. Returns the new segment.
112 Segment* NewSegment(int size);
113
114 // Deletes the given segment. Does not touch the segment chain.
115 void DeleteSegment(Segment* segment, int size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000116
117 // The free region in the current (front) segment is represented as
118 // the half-open interval [position, limit). The 'position' variable
119 // is guaranteed to be aligned as dictated by kAlignment.
Steve Block44f0eee2011-05-26 01:26:41 +0100120 Address position_;
121 Address limit_;
122
123 int scope_nesting_;
124
125 Segment* segment_head_;
126 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000127};
128
129
130// ZoneObject is an abstraction that helps define classes of objects
131// allocated in the Zone. Use it as a base class; see ast.h.
132class ZoneObject {
133 public:
134 // Allocate a new ZoneObject of 'size' bytes in the Zone.
Steve Block44f0eee2011-05-26 01:26:41 +0100135 inline void* operator new(size_t size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
137 // Ideally, the delete operator should be private instead of
138 // public, but unfortunately the compiler sometimes synthesizes
139 // (unused) destructors for classes derived from ZoneObject, which
140 // require the operator to be visible. MSVC requires the delete
141 // operator to be public.
142
143 // ZoneObjects should never be deleted individually; use
144 // Zone::DeleteAll() to delete all zone objects in one go.
145 void operator delete(void*, size_t) { UNREACHABLE(); }
146};
147
148
149class AssertNoZoneAllocation {
150 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100151 inline AssertNoZoneAllocation();
152 inline ~AssertNoZoneAllocation();
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 private:
154 bool prev_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000155};
156
157
158// The ZoneListAllocationPolicy is used to specialize the GenericList
159// implementation to allocate ZoneLists and their elements in the
160// Zone.
161class ZoneListAllocationPolicy {
162 public:
163 // Allocate 'size' bytes of memory in the zone.
Steve Block44f0eee2011-05-26 01:26:41 +0100164 static inline void* New(int size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
166 // De-allocation attempts are silently ignored.
167 static void Delete(void* p) { }
168};
169
170
171// ZoneLists are growable lists with constant-time access to the
172// elements. The list itself and all its elements are allocated in the
173// Zone. ZoneLists cannot be deleted individually; you can delete all
174// objects in the Zone by calling Zone::DeleteAll().
175template<typename T>
176class ZoneList: public List<T, ZoneListAllocationPolicy> {
177 public:
178 // Construct a new ZoneList with the given capacity; the length is
179 // always zero. The capacity must be non-negative.
180 explicit ZoneList(int capacity)
181 : List<T, ZoneListAllocationPolicy>(capacity) { }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100182
183 // Construct a new ZoneList by copying the elements of the given ZoneList.
184 explicit ZoneList(const ZoneList<T>& other)
185 : List<T, ZoneListAllocationPolicy>(other.length()) {
186 AddAll(other);
187 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000188};
189
190
Ben Murdochb0fe1622011-05-05 13:52:32 +0100191// Introduce a convenience type for zone lists of map handles.
192typedef ZoneList<Handle<Map> > ZoneMapList;
193
194
Steve Blocka7e24c12009-10-30 11:49:00 +0000195// ZoneScopes keep track of the current parsing and compilation
196// nesting and cleans up generated ASTs in the Zone when exiting the
197// outer-most scope.
198class ZoneScope BASE_EMBEDDED {
199 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100200 // TODO(isolates): pass isolate pointer here.
201 inline explicit ZoneScope(ZoneScopeMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
Steve Block44f0eee2011-05-26 01:26:41 +0100203 virtual ~ZoneScope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000204
Steve Block44f0eee2011-05-26 01:26:41 +0100205 inline bool ShouldDeleteOnExit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000206
207 // For ZoneScopes that do not delete on exit by default, call this
208 // method to request deletion on exit.
209 void DeleteOnExit() {
210 mode_ = DELETE_ON_EXIT;
211 }
212
Steve Block44f0eee2011-05-26 01:26:41 +0100213 inline static int nesting();
Steve Blocka7e24c12009-10-30 11:49:00 +0000214
215 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100216 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 ZoneScopeMode mode_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000218};
219
220
221// A zone splay tree. The config type parameter encapsulates the
Steve Block6ded16b2010-05-10 14:33:55 +0100222// different configurations of a concrete splay tree (see splay-tree.h).
223// The tree itself and all its elements are allocated in the Zone.
Steve Blocka7e24c12009-10-30 11:49:00 +0000224template <typename Config>
Steve Block6ded16b2010-05-10 14:33:55 +0100225class ZoneSplayTree: public SplayTree<Config, ZoneListAllocationPolicy> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 public:
Steve Block6ded16b2010-05-10 14:33:55 +0100227 ZoneSplayTree()
228 : SplayTree<Config, ZoneListAllocationPolicy>() {}
229 ~ZoneSplayTree();
Steve Blocka7e24c12009-10-30 11:49:00 +0000230};
231
232
233} } // namespace v8::internal
234
235#endif // V8_ZONE_H_