blob: 753e2035d3ac612060cb1391c33f29db5df9110f [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_ZONE_H_
6#define V8_ZONE_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include <limits>
9
10#include "src/allocation.h"
11#include "src/base/logging.h"
12#include "src/globals.h"
13#include "src/hashmap.h"
14#include "src/list.h"
15#include "src/splay-tree.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000016
Steve Blocka7e24c12009-10-30 11:49:00 +000017namespace v8 {
18namespace internal {
19
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020// Forward declarations.
Steve Block44f0eee2011-05-26 01:26:41 +010021class Segment;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022
Steve Blocka7e24c12009-10-30 11:49:00 +000023
24// The Zone supports very fast allocation of small chunks of
25// memory. The chunks cannot be deallocated individually, but instead
26// the Zone supports deallocating all chunks in one fast
27// operation. The Zone is used to hold temporary data structures like
28// the abstract syntax tree, which is deallocated after compilation.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029//
Steve Blocka7e24c12009-10-30 11:49:00 +000030// Note: There is no need to initialize the Zone; the first time an
31// allocation is attempted, a segment of memory will be requested
32// through a call to malloc().
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033//
Steve Blocka7e24c12009-10-30 11:49:00 +000034// Note: The implementation is inherently not thread safe. Do not use
35// from multi-threaded code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036class Zone final {
Steve Blocka7e24c12009-10-30 11:49:00 +000037 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 Zone();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 ~Zone();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040
Steve Blocka7e24c12009-10-30 11:49:00 +000041 // Allocate 'size' bytes of memory in the Zone; expands the Zone by
42 // allocating new segments of memory on demand using malloc().
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 void* New(size_t size);
Steve Blocka7e24c12009-10-30 11:49:00 +000044
45 template <typename T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 T* NewArray(size_t length) {
47 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 return static_cast<T*>(New(length * sizeof(T)));
49 }
Steve Blocka7e24c12009-10-30 11:49:00 +000050
Ben Murdoch69a99ed2011-11-30 16:03:39 +000051 // Deletes all objects and free all memory allocated in the Zone. Keeps one
52 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one.
Steve Block44f0eee2011-05-26 01:26:41 +010053 void DeleteAll();
Steve Blocka7e24c12009-10-30 11:49:00 +000054
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 // Deletes the last small segment kept around by DeleteAll(). You
56 // may no longer allocate in the Zone after a call to this method.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000057 void DeleteKeptSegment();
58
Steve Blocka7e24c12009-10-30 11:49:00 +000059 // Returns true if more memory has been allocated in zones than
60 // the limit allows.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 bool excess_allocation() const {
62 return segment_bytes_allocated_ > kExcessLimit;
63 }
Steve Blocka7e24c12009-10-30 11:49:00 +000064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 size_t allocation_size() const { return allocation_size_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +010066
Steve Blocka7e24c12009-10-30 11:49:00 +000067 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +010068 // All pointers returned from New() have this alignment. In addition, if the
69 // object being allocated has a size that is divisible by 8 then its alignment
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 // will be 8. ASan requires 8-byte alignment.
71#ifdef V8_USE_ADDRESS_SANITIZER
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 static const size_t kAlignment = 8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 STATIC_ASSERT(kPointerSize <= 8);
74#else
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 static const size_t kAlignment = kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000077
78 // Never allocate segments smaller than this size in bytes.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 static const size_t kMinimumSegmentSize = 8 * KB;
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81 // Never allocate segments larger than this size in bytes.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 static const size_t kMaximumSegmentSize = 1 * MB;
Steve Blocka7e24c12009-10-30 11:49:00 +000083
84 // Never keep segments larger than this size in bytes around.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 static const size_t kMaximumKeptSegmentSize = 64 * KB;
Steve Blocka7e24c12009-10-30 11:49:00 +000086
87 // Report zone excess when allocation exceeds this limit.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 static const size_t kExcessLimit = 256 * MB;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089
90 // The number of bytes allocated in this zone so far.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091 size_t allocation_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +000092
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.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 size_t segment_bytes_allocated_;
Steve Blocka7e24c12009-10-30 11:49:00 +000097
Steve Blocka7e24c12009-10-30 11:49:00 +000098 // Expand the Zone to hold at least 'size' more bytes and allocate
99 // the bytes. Returns the address of the newly allocated chunk of
100 // memory in the Zone. Should only be called if there isn't enough
101 // room in the Zone already.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 Address NewExpand(size_t size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000103
Steve Block44f0eee2011-05-26 01:26:41 +0100104 // Creates a new segment, sets it size, and pushes it to the front
105 // of the segment chain. Returns the new segment.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 inline Segment* NewSegment(size_t size);
Steve Block44f0eee2011-05-26 01:26:41 +0100107
108 // Deletes the given segment. Does not touch the segment chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 inline void DeleteSegment(Segment* segment, size_t size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000110
111 // The free region in the current (front) segment is represented as
112 // the half-open interval [position, limit). The 'position' variable
113 // is guaranteed to be aligned as dictated by kAlignment.
Steve Block44f0eee2011-05-26 01:26:41 +0100114 Address position_;
115 Address limit_;
116
Steve Block44f0eee2011-05-26 01:26:41 +0100117 Segment* segment_head_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000118};
119
120
121// ZoneObject is an abstraction that helps define classes of objects
122// allocated in the Zone. Use it as a base class; see ast.h.
123class ZoneObject {
124 public:
125 // Allocate a new ZoneObject of 'size' bytes in the Zone.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000127
128 // Ideally, the delete operator should be private instead of
129 // public, but unfortunately the compiler sometimes synthesizes
130 // (unused) destructors for classes derived from ZoneObject, which
131 // require the operator to be visible. MSVC requires the delete
132 // operator to be public.
133
134 // ZoneObjects should never be deleted individually; use
135 // Zone::DeleteAll() to delete all zone objects in one go.
136 void operator delete(void*, size_t) { UNREACHABLE(); }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000137 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000138};
139
140
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141// The ZoneScope is used to automatically call DeleteAll() on a
142// Zone when the ZoneScope is destroyed (i.e. goes out of scope)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143class ZoneScope final {
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 explicit ZoneScope(Zone* zone) : zone_(zone) { }
146 ~ZoneScope() { zone_->DeleteAll(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000147
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148 Zone* zone() const { return zone_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149
150 private:
151 Zone* zone_;
152};
153
154
155// The ZoneAllocationPolicy is used to specialize generic data
156// structures to allocate themselves and their elements in the Zone.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157class ZoneAllocationPolicy final {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 public:
159 explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160 void* New(size_t size) { return zone()->New(size); }
161 static void Delete(void* pointer) {}
162 Zone* zone() const { return zone_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163
164 private:
165 Zone* zone_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000166};
167
168
169// ZoneLists are growable lists with constant-time access to the
170// elements. The list itself and all its elements are allocated in the
171// Zone. ZoneLists cannot be deleted individually; you can delete all
172// objects in the Zone by calling Zone::DeleteAll().
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173template <typename T>
174class ZoneList final : public List<T, ZoneAllocationPolicy> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 public:
176 // Construct a new ZoneList with the given capacity; the length is
177 // always zero. The capacity must be non-negative.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 ZoneList(int capacity, Zone* zone)
179 : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { }
180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100182
183 // Construct a new ZoneList by copying the elements of the given ZoneList.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 ZoneList(const ZoneList<T>& other, Zone* zone)
185 : List<T, ZoneAllocationPolicy>(other.length(),
186 ZoneAllocationPolicy(zone)) {
187 AddAll(other, zone);
188 }
189
190 // We add some convenience wrappers so that we can pass in a Zone
191 // instead of a (less convenient) ZoneAllocationPolicy.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 void Add(const T& element, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone));
194 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
197 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000198 void AddAll(const Vector<T>& other, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone));
200 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000201 void InsertAt(int index, const T& element, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 List<T, ZoneAllocationPolicy>::InsertAt(index, element,
203 ZoneAllocationPolicy(zone));
204 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000205 Vector<T> AddBlock(T value, int count, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 return List<T, ZoneAllocationPolicy>::AddBlock(value, count,
207 ZoneAllocationPolicy(zone));
208 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209 void Allocate(int length, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone));
211 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000212 void Initialize(int capacity, Zone* zone) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 List<T, ZoneAllocationPolicy>::Initialize(capacity,
214 ZoneAllocationPolicy(zone));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100215 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000216
217 void operator delete(void* pointer) { UNREACHABLE(); }
218 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000219};
220
221
Steve Blocka7e24c12009-10-30 11:49:00 +0000222// A zone splay tree. The config type parameter encapsulates the
Steve Block6ded16b2010-05-10 14:33:55 +0100223// different configurations of a concrete splay tree (see splay-tree.h).
224// The tree itself and all its elements are allocated in the Zone.
Steve Blocka7e24c12009-10-30 11:49:00 +0000225template <typename Config>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226class ZoneSplayTree final : public SplayTree<Config, ZoneAllocationPolicy> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000228 explicit ZoneSplayTree(Zone* zone)
229 : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000230 ~ZoneSplayTree() {
231 // Reset the root to avoid unneeded iteration over all tree nodes
232 // in the destructor. For a zone-allocated tree, nodes will be
233 // freed by the Zone.
234 SplayTree<Config, ZoneAllocationPolicy>::ResetRoot();
235 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000237 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238
239 void operator delete(void* pointer) { UNREACHABLE(); }
240 void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000241};
242
243
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100245
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000246} // namespace internal
247} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000248
249#endif // V8_ZONE_H_