v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
zone.h
Go to the documentation of this file.
1 // Copyright 2012 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 
31 #include "allocation.h"
32 #include "checks.h"
33 #include "hashmap.h"
34 #include "globals.h"
35 #include "list.h"
36 #include "splay-tree.h"
37 
38 namespace v8 {
39 namespace internal {
40 
41 #if defined(__has_feature)
42  #if __has_feature(address_sanitizer)
43  #define V8_USE_ADDRESS_SANITIZER
44  #endif
45 #endif
46 
47 class Segment;
48 class Isolate;
49 
50 // The Zone supports very fast allocation of small chunks of
51 // memory. The chunks cannot be deallocated individually, but instead
52 // the Zone supports deallocating all chunks in one fast
53 // operation. The Zone is used to hold temporary data structures like
54 // the abstract syntax tree, which is deallocated after compilation.
55 
56 // Note: There is no need to initialize the Zone; the first time an
57 // allocation is attempted, a segment of memory will be requested
58 // through a call to malloc().
59 
60 // Note: The implementation is inherently not thread safe. Do not use
61 // from multi-threaded code.
62 
63 class Zone {
64  public:
65  explicit Zone(Isolate* isolate);
66  ~Zone();
67  // Allocate 'size' bytes of memory in the Zone; expands the Zone by
68  // allocating new segments of memory on demand using malloc().
69  inline void* New(int size);
70 
71  template <typename T>
72  inline T* NewArray(int length);
73 
74  // Deletes all objects and free all memory allocated in the Zone. Keeps one
75  // small (size <= kMaximumKeptSegmentSize) segment around if it finds one.
76  void DeleteAll();
77 
78  // Deletes the last small segment kept around by DeleteAll(). You
79  // may no longer allocate in the Zone after a call to this method.
80  void DeleteKeptSegment();
81 
82  // Returns true if more memory has been allocated in zones than
83  // the limit allows.
84  inline bool excess_allocation();
85 
86  inline void adjust_segment_bytes_allocated(int delta);
87 
88  inline unsigned allocation_size() { return allocation_size_; }
89 
90  inline Isolate* isolate() { return isolate_; }
91 
92  private:
93  friend class Isolate;
94 
95  // All pointers returned from New() have this alignment. In addition, if the
96  // object being allocated has a size that is divisible by 8 then its alignment
97  // will be 8. ASan requires 8-byte alignment.
98 #ifdef V8_USE_ADDRESS_SANITIZER
99  static const int kAlignment = 8;
101 #else
102  static const int kAlignment = kPointerSize;
103 #endif
104 
105  // Never allocate segments smaller than this size in bytes.
106  static const int kMinimumSegmentSize = 8 * KB;
107 
108  // Never allocate segments larger than this size in bytes.
109  static const int kMaximumSegmentSize = 1 * MB;
110 
111  // Never keep segments larger than this size in bytes around.
112  static const int kMaximumKeptSegmentSize = 64 * KB;
113 
114  // Report zone excess when allocation exceeds this limit.
115  static const int kExcessLimit = 256 * MB;
116 
117  // The number of bytes allocated in this zone so far.
118  unsigned allocation_size_;
119 
120  // The number of bytes allocated in segments. Note that this number
121  // includes memory allocated from the OS but not yet allocated from
122  // the zone.
123  int segment_bytes_allocated_;
124 
125  // Expand the Zone to hold at least 'size' more bytes and allocate
126  // the bytes. Returns the address of the newly allocated chunk of
127  // memory in the Zone. Should only be called if there isn't enough
128  // room in the Zone already.
129  Address NewExpand(int size);
130 
131  // Creates a new segment, sets it size, and pushes it to the front
132  // of the segment chain. Returns the new segment.
133  INLINE(Segment* NewSegment(int size));
134 
135  // Deletes the given segment. Does not touch the segment chain.
136  INLINE(void DeleteSegment(Segment* segment, int size));
137 
138  // The free region in the current (front) segment is represented as
139  // the half-open interval [position, limit). The 'position' variable
140  // is guaranteed to be aligned as dictated by kAlignment.
141  Address position_;
142  Address limit_;
143 
144  Segment* segment_head_;
145  Isolate* isolate_;
146 };
147 
148 
149 // ZoneObject is an abstraction that helps define classes of objects
150 // allocated in the Zone. Use it as a base class; see ast.h.
151 class ZoneObject {
152  public:
153  // Allocate a new ZoneObject of 'size' bytes in the Zone.
154  INLINE(void* operator new(size_t size, Zone* zone));
155 
156  // Ideally, the delete operator should be private instead of
157  // public, but unfortunately the compiler sometimes synthesizes
158  // (unused) destructors for classes derived from ZoneObject, which
159  // require the operator to be visible. MSVC requires the delete
160  // operator to be public.
161 
162  // ZoneObjects should never be deleted individually; use
163  // Zone::DeleteAll() to delete all zone objects in one go.
164  void operator delete(void*, size_t) { UNREACHABLE(); }
165  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
166 };
167 
168 
169 // The ZoneScope is used to automatically call DeleteAll() on a
170 // Zone when the ZoneScope is destroyed (i.e. goes out of scope)
171 struct ZoneScope {
172  public:
173  explicit ZoneScope(Zone* zone) : zone_(zone) { }
174  ~ZoneScope() { zone_->DeleteAll(); }
175 
176  Zone* zone() { return zone_; }
177 
178  private:
179  Zone* zone_;
180 };
181 
182 
183 // The ZoneAllocationPolicy is used to specialize generic data
184 // structures to allocate themselves and their elements in the Zone.
186  public:
187  explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
188  INLINE(void* New(size_t size));
189  INLINE(static void Delete(void *pointer)) { }
190  Zone* zone() { return zone_; }
191 
192  private:
193  Zone* zone_;
194 };
195 
196 
197 // ZoneLists are growable lists with constant-time access to the
198 // elements. The list itself and all its elements are allocated in the
199 // Zone. ZoneLists cannot be deleted individually; you can delete all
200 // objects in the Zone by calling Zone::DeleteAll().
201 template<typename T>
202 class ZoneList: public List<T, ZoneAllocationPolicy> {
203  public:
204  // Construct a new ZoneList with the given capacity; the length is
205  // always zero. The capacity must be non-negative.
206  ZoneList(int capacity, Zone* zone)
207  : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { }
208 
209  INLINE(void* operator new(size_t size, Zone* zone));
210 
211  // Construct a new ZoneList by copying the elements of the given ZoneList.
212  ZoneList(const ZoneList<T>& other, Zone* zone)
213  : List<T, ZoneAllocationPolicy>(other.length(),
214  ZoneAllocationPolicy(zone)) {
215  AddAll(other, zone);
216  }
217 
218  // We add some convenience wrappers so that we can pass in a Zone
219  // instead of a (less convenient) ZoneAllocationPolicy.
220  INLINE(void Add(const T& element, Zone* zone)) {
222  }
223  INLINE(void AddAll(const List<T, ZoneAllocationPolicy>& other, Zone* zone)) {
225  }
226  INLINE(void AddAll(const Vector<T>& other, Zone* zone)) {
228  }
229  INLINE(void InsertAt(int index, const T& element, Zone* zone)) {
231  ZoneAllocationPolicy(zone));
232  }
233  INLINE(Vector<T> AddBlock(T value, int count, Zone* zone)) {
234  return List<T, ZoneAllocationPolicy>::AddBlock(value, count,
235  ZoneAllocationPolicy(zone));
236  }
237  INLINE(void Allocate(int length, Zone* zone)) {
239  }
240  INLINE(void Initialize(int capacity, Zone* zone)) {
242  ZoneAllocationPolicy(zone));
243  }
244 
245  void operator delete(void* pointer) { UNREACHABLE(); }
246  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
247 };
248 
249 
250 // A zone splay tree. The config type parameter encapsulates the
251 // different configurations of a concrete splay tree (see splay-tree.h).
252 // The tree itself and all its elements are allocated in the Zone.
253 template <typename Config>
254 class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> {
255  public:
256  explicit ZoneSplayTree(Zone* zone)
258  ~ZoneSplayTree();
259 
260  INLINE(void* operator new(size_t size, Zone* zone));
261 
262  void operator delete(void* pointer) { UNREACHABLE(); }
263  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
264 };
265 
266 
268 
269 } } // namespace v8::internal
270 
271 #endif // V8_ZONE_H_
byte * Address
Definition: globals.h:186
void InsertAt(int index, const T &element, AllocationPolicy allocator=AllocationPolicy())
INLINE(void *New(size_t size))
bool excess_allocation()
Definition: zone-inl.h:99
ZoneScope(Zone *zone)
Definition: zone.h:173
const int KB
Definition: globals.h:245
INLINE(static void Delete(void *pointer))
Definition: zone.h:189
INLINE(void AddAll(const Vector< T > &other, Zone *zone))
Definition: zone.h:226
INLINE(void *operator new(size_t size, Zone *zone))
INLINE(void InsertAt(int index, const T &element, Zone *zone))
Definition: zone.h:229
INLINE(Vector< T > AddBlock(T value, int count, Zone *zone))
Definition: zone.h:233
void DeleteAll()
Definition: zone.cc:88
Isolate * isolate()
Definition: zone.h:90
Zone(Isolate *isolate)
Definition: zone.cc:70
INLINE(void Allocate(int length, Zone *zone))
Definition: zone.h:237
INLINE(void *operator new(size_t size, Zone *zone))
#define UNREACHABLE()
Definition: checks.h:52
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object size
Definition: flags.cc:211
STATIC_ASSERT(sizeof(CPURegister)==sizeof(Register))
INLINE(void AddAll(const List< T, ZoneAllocationPolicy > &other, Zone *zone))
Definition: zone.h:223
const int kPointerSize
Definition: globals.h:268
INLINE(void Add(const T &element, Zone *zone))
Definition: zone.h:220
void adjust_segment_bytes_allocated(int delta)
Definition: zone-inl.h:104
void DeleteKeptSegment()
Definition: zone.cc:140
T * NewArray(int length)
Definition: zone-inl.h:93
ZoneList(const ZoneList< T > &other, Zone *zone)
Definition: zone.h:212
#define T(name, string, precedence)
Definition: token.cc:48
INLINE(void *operator new(size_t size, Zone *zone))
#define INLINE(declarator)
Definition: globals.h:376
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
unsigned allocation_size()
Definition: zone.h:88
ZoneAllocationPolicy(Zone *zone)
Definition: zone.h:187
void * New(int size)
Definition: zone-inl.h:51
TemplateHashMapImpl< ZoneAllocationPolicy > ZoneHashMap
Definition: zone.h:267
INLINE(void Initialize(int capacity, Zone *zone))
Definition: zone.h:240
Vector< T > AddBlock(T value, int count, AllocationPolicy allocator=AllocationPolicy())
void AddAll(const List< T, AllocationPolicy > &other, AllocationPolicy allocator=AllocationPolicy())
ZoneSplayTree(Zone *zone)
Definition: zone.h:256
ZoneList(int capacity, Zone *zone)
Definition: zone.h:206
const int MB
Definition: globals.h:246