blob: 5545e3cede6dc6b80ed3a2a517b771256a5f6790 [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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000042class Segment;
danno@chromium.orgfa458e42012-02-01 10:48:36 +000043class Isolate;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045// The Zone supports very fast allocation of small chunks of
46// memory. The chunks cannot be deallocated individually, but instead
47// the Zone supports deallocating all chunks in one fast
48// operation. The Zone is used to hold temporary data structures like
49// the abstract syntax tree, which is deallocated after compilation.
50
51// Note: There is no need to initialize the Zone; the first time an
52// allocation is attempted, a segment of memory will be requested
53// through a call to malloc().
54
55// Note: The implementation is inherently not thread safe. Do not use
56// from multi-threaded code.
57
58class Zone {
59 public:
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000060 explicit Zone(Isolate* isolate);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000061 ~Zone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062 // Allocate 'size' bytes of memory in the Zone; expands the Zone by
63 // allocating new segments of memory on demand using malloc().
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000064 inline void* New(int size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065
ager@chromium.org41826e72009-03-30 13:30:57 +000066 template <typename T>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000067 inline T* NewArray(int length);
ager@chromium.org41826e72009-03-30 13:30:57 +000068
ager@chromium.orga74f0da2008-12-03 16:05:52 +000069 // Returns true if more memory has been allocated in zones than
70 // the limit allows.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000071 inline bool excess_allocation();
ager@chromium.orga74f0da2008-12-03 16:05:52 +000072
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 inline void adjust_segment_bytes_allocated(int delta);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000074
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000075 inline unsigned allocation_size() { return allocation_size_; }
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000076
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000077 inline Isolate* isolate() { return isolate_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000080 friend class Isolate;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000081
danno@chromium.org50d79f32011-11-11 12:01:18 +000082 // All pointers returned from New() have this alignment. In addition, if the
83 // object being allocated has a size that is divisible by 8 then its alignment
84 // will be 8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085 static const int kAlignment = kPointerSize;
86
87 // Never allocate segments smaller than this size in bytes.
88 static const int kMinimumSegmentSize = 8 * KB;
89
ager@chromium.org8bb60582008-12-11 12:02:20 +000090 // Never allocate segments larger than this size in bytes.
91 static const int kMaximumSegmentSize = 1 * MB;
92
ager@chromium.orga74f0da2008-12-03 16:05:52 +000093 // Report zone excess when allocation exceeds this limit.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000094 static const int kExcessLimit = 256 * MB;
95
96 // The number of bytes allocated in this zone so far.
97 unsigned allocation_size_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000098
99 // The number of bytes allocated in segments. Note that this number
100 // includes memory allocated from the OS but not yet allocated from
101 // the zone.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000102 int segment_bytes_allocated_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 // 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.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000108 Address NewExpand(int size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000110 // 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);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000120 Address position_;
121 Address limit_;
122
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000123 Segment* segment_head_;
124 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125};
126
127
128// ZoneObject is an abstraction that helps define classes of objects
129// allocated in the Zone. Use it as a base class; see ast.h.
130class ZoneObject {
131 public:
132 // Allocate a new ZoneObject of 'size' bytes in the Zone.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000133 INLINE(void* operator new(size_t size, Zone* zone));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134
135 // Ideally, the delete operator should be private instead of
ager@chromium.org32912102009-01-16 10:38:43 +0000136 // public, but unfortunately the compiler sometimes synthesizes
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 // (unused) destructors for classes derived from ZoneObject, which
138 // require the operator to be visible. MSVC requires the delete
139 // operator to be public.
140
141 // ZoneObjects should never be deleted individually; use
142 // Zone::DeleteAll() to delete all zone objects in one go.
143 void operator delete(void*, size_t) { UNREACHABLE(); }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000144 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145};
146
147
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000148// The ZoneAllocationPolicy is used to specialize generic data
149// structures to allocate themselves and their elements in the Zone.
150struct ZoneAllocationPolicy {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000152 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000153 INLINE(void* New(size_t size));
154 INLINE(static void Delete(void *pointer)) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000156 private:
157 Zone* zone_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158};
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>
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000166class ZoneList: public List<T, ZoneAllocationPolicy> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 public:
168 // Construct a new ZoneList with the given capacity; the length is
169 // always zero. The capacity must be non-negative.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000170 ZoneList(int capacity, Zone* zone)
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000171 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { }
172
173 INLINE(void* operator new(size_t size, Zone* zone));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000174
175 // Construct a new ZoneList by copying the elements of the given ZoneList.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000176 ZoneList(const ZoneList<T>& other, Zone* zone)
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000177 : List<T, ZoneAllocationPolicy>(other.length(),
178 ZoneAllocationPolicy(zone)) {
179 AddAll(other, ZoneAllocationPolicy(zone));
180 }
181
182 // We add some convenience wrappers so that we can pass in a Zone
183 // instead of a (less convenient) ZoneAllocationPolicy.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000184 INLINE(void Add(const T& element, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000185 List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone));
186 }
187 INLINE(void AddAll(const List<T, ZoneAllocationPolicy>& other,
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000188 Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000189 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
190 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000191 INLINE(void AddAll(const Vector<T>& other, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000192 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
193 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000194 INLINE(void InsertAt(int index, const T& element, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000195 List<T, ZoneAllocationPolicy>::InsertAt(index, element,
196 ZoneAllocationPolicy(zone));
197 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000198 INLINE(Vector<T> AddBlock(T value, int count, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000199 return List<T, ZoneAllocationPolicy>::AddBlock(value, count,
200 ZoneAllocationPolicy(zone));
201 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000202 INLINE(void Allocate(int length, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000203 List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone));
204 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000205 INLINE(void Initialize(int capacity, Zone* zone)) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000206 List<T, ZoneAllocationPolicy>::Initialize(capacity,
207 ZoneAllocationPolicy(zone));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000208 }
ricow@chromium.org4668a2c2011-08-29 10:41:00 +0000209
210 void operator delete(void* pointer) { UNREACHABLE(); }
211 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212};
213
214
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000215// A zone splay tree. The config type parameter encapsulates the
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000216// different configurations of a concrete splay tree (see splay-tree.h).
217// The tree itself and all its elements are allocated in the Zone.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000218template <typename Config>
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000219class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000220 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000221 explicit ZoneSplayTree(Zone* zone)
222 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {}
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000223 ~ZoneSplayTree();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000224};
225
226
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000227typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000228
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229} } // namespace v8::internal
230
231#endif // V8_ZONE_H_