v8  3.14.5(node0.10.28)
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 
42 // Zone scopes are in one of two modes. Either they delete the zone
43 // on exit or they do not.
47 };
48 
49 class Segment;
50 class Isolate;
51 
52 // The Zone supports very fast allocation of small chunks of
53 // memory. The chunks cannot be deallocated individually, but instead
54 // the Zone supports deallocating all chunks in one fast
55 // operation. The Zone is used to hold temporary data structures like
56 // the abstract syntax tree, which is deallocated after compilation.
57 
58 // Note: There is no need to initialize the Zone; the first time an
59 // allocation is attempted, a segment of memory will be requested
60 // through a call to malloc().
61 
62 // Note: The implementation is inherently not thread safe. Do not use
63 // from multi-threaded code.
64 
65 class Zone {
66  public:
67  explicit Zone(Isolate* isolate);
69  // Allocate 'size' bytes of memory in the Zone; expands the Zone by
70  // allocating new segments of memory on demand using malloc().
71  inline void* New(int size);
72 
73  template <typename T>
74  inline T* NewArray(int length);
75 
76  // Deletes all objects and free all memory allocated in the Zone. Keeps one
77  // small (size <= kMaximumKeptSegmentSize) segment around if it finds one.
78  void DeleteAll();
79 
80  // Deletes the last small segment kept around by DeleteAll().
81  void DeleteKeptSegment();
82 
83  // Returns true if more memory has been allocated in zones than
84  // the limit allows.
85  inline bool excess_allocation();
86 
87  inline void adjust_segment_bytes_allocated(int delta);
88 
89  inline Isolate* isolate() { return isolate_; }
90 
91  static unsigned allocation_size_;
92 
93  private:
94  friend class Isolate;
95  friend class ZoneScope;
96 
97  // All pointers returned from New() have this alignment. In addition, if the
98  // object being allocated has a size that is divisible by 8 then its alignment
99  // will be 8.
100  static const int kAlignment = kPointerSize;
101 
102  // Never allocate segments smaller than this size in bytes.
103  static const int kMinimumSegmentSize = 8 * KB;
104 
105  // Never allocate segments larger than this size in bytes.
106  static const int kMaximumSegmentSize = 1 * MB;
107 
108  // Never keep segments larger than this size in bytes around.
109  static const int kMaximumKeptSegmentSize = 64 * KB;
110 
111  // Report zone excess when allocation exceeds this limit.
112  int zone_excess_limit_;
113 
114  // The number of bytes allocated in segments. Note that this number
115  // includes memory allocated from the OS but not yet allocated from
116  // the zone.
117  int segment_bytes_allocated_;
118 
119  // Expand the Zone to hold at least 'size' more bytes and allocate
120  // the bytes. Returns the address of the newly allocated chunk of
121  // memory in the Zone. Should only be called if there isn't enough
122  // room in the Zone already.
123  Address NewExpand(int size);
124 
125  // Creates a new segment, sets it size, and pushes it to the front
126  // of the segment chain. Returns the new segment.
127  Segment* NewSegment(int size);
128 
129  // Deletes the given segment. Does not touch the segment chain.
130  void DeleteSegment(Segment* segment, int size);
131 
132  // The free region in the current (front) segment is represented as
133  // the half-open interval [position, limit). The 'position' variable
134  // is guaranteed to be aligned as dictated by kAlignment.
135  Address position_;
136  Address limit_;
137 
138  int scope_nesting_;
139 
140  Segment* segment_head_;
141  Isolate* isolate_;
142 };
143 
144 
145 // ZoneObject is an abstraction that helps define classes of objects
146 // allocated in the Zone. Use it as a base class; see ast.h.
147 class ZoneObject {
148  public:
149  // Allocate a new ZoneObject of 'size' bytes in the Zone.
150  INLINE(void* operator new(size_t size, Zone* zone));
151 
152  // Ideally, the delete operator should be private instead of
153  // public, but unfortunately the compiler sometimes synthesizes
154  // (unused) destructors for classes derived from ZoneObject, which
155  // require the operator to be visible. MSVC requires the delete
156  // operator to be public.
157 
158  // ZoneObjects should never be deleted individually; use
159  // Zone::DeleteAll() to delete all zone objects in one go.
160  void operator delete(void*, size_t) { UNREACHABLE(); }
161  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
162 };
163 
164 
165 // The ZoneAllocationPolicy is used to specialize generic data
166 // structures to allocate themselves and their elements in the Zone.
168  public:
169  explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
170  INLINE(void* New(size_t size));
171  INLINE(static void Delete(void *pointer)) { }
172 
173  private:
174  Zone* zone_;
175 };
176 
177 
178 // ZoneLists are growable lists with constant-time access to the
179 // elements. The list itself and all its elements are allocated in the
180 // Zone. ZoneLists cannot be deleted individually; you can delete all
181 // objects in the Zone by calling Zone::DeleteAll().
182 template<typename T>
183 class ZoneList: public List<T, ZoneAllocationPolicy> {
184  public:
185  // Construct a new ZoneList with the given capacity; the length is
186  // always zero. The capacity must be non-negative.
187  ZoneList(int capacity, Zone* zone)
188  : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { }
189 
190  INLINE(void* operator new(size_t size, Zone* zone));
191 
192  // Construct a new ZoneList by copying the elements of the given ZoneList.
193  ZoneList(const ZoneList<T>& other, Zone* zone)
194  : List<T, ZoneAllocationPolicy>(other.length(),
195  ZoneAllocationPolicy(zone)) {
196  AddAll(other, ZoneAllocationPolicy(zone));
197  }
198 
199  // We add some convenience wrappers so that we can pass in a Zone
200  // instead of a (less convenient) ZoneAllocationPolicy.
201  INLINE(void Add(const T& element, Zone* zone)) {
203  }
205  Zone* zone)) {
207  }
208  INLINE(void AddAll(const Vector<T>& other, Zone* zone)) {
210  }
211  INLINE(void InsertAt(int index, const T& element, Zone* zone)) {
213  ZoneAllocationPolicy(zone));
214  }
215  INLINE(Vector<T> AddBlock(T value, int count, Zone* zone)) {
216  return List<T, ZoneAllocationPolicy>::AddBlock(value, count,
217  ZoneAllocationPolicy(zone));
218  }
219  INLINE(void Allocate(int length, Zone* zone)) {
221  }
222  INLINE(void Initialize(int capacity, Zone* zone)) {
224  ZoneAllocationPolicy(zone));
225  }
226 
227  void operator delete(void* pointer) { UNREACHABLE(); }
228  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
229 };
230 
231 
232 // ZoneScopes keep track of the current parsing and compilation
233 // nesting and cleans up generated ASTs in the Zone when exiting the
234 // outer-most scope.
235 class ZoneScope BASE_EMBEDDED {
236  public:
237  INLINE(ZoneScope(Zone* zone, ZoneScopeMode mode));
238 
239  virtual ~ZoneScope();
240 
241  inline bool ShouldDeleteOnExit();
242 
243  // For ZoneScopes that do not delete on exit by default, call this
244  // method to request deletion on exit.
245  void DeleteOnExit() {
246  mode_ = DELETE_ON_EXIT;
247  }
248 
249  inline static int nesting();
250 
251  private:
252  Zone* zone_;
253  ZoneScopeMode mode_;
254 };
255 
256 
257 // A zone splay tree. The config type parameter encapsulates the
258 // different configurations of a concrete splay tree (see splay-tree.h).
259 // The tree itself and all its elements are allocated in the Zone.
260 template <typename Config>
261 class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> {
262  public:
263  explicit ZoneSplayTree(Zone* zone)
265  ~ZoneSplayTree();
266 };
267 
268 
270 
271 } } // namespace v8::internal
272 
273 #endif // V8_ZONE_H_
byte * Address
Definition: globals.h:157
void InsertAt(int index, const T &element, AllocationPolicy allocator=AllocationPolicy())
INLINE(void *New(size_t size))
bool excess_allocation()
Definition: zone-inl.h:77
const int KB
Definition: globals.h:207
INLINE(static void Delete(void *pointer))
Definition: zone.h:171
INLINE(void AddAll(const Vector< T > &other, Zone *zone))
Definition: zone.h:208
INLINE(void *operator new(size_t size, Zone *zone))
INLINE(void InsertAt(int index, const T &element, Zone *zone))
Definition: zone.h:211
INLINE(Vector< T > AddBlock(T value, int count, Zone *zone))
Definition: zone.h:215
void DeleteAll()
Definition: zone.cc:107
Isolate * isolate()
Definition: zone.h:89
Zone(Isolate *isolate)
Definition: zone.cc:70
INLINE(void Allocate(int length, Zone *zone))
Definition: zone.h:219
#define UNREACHABLE()
Definition: checks.h:50
INLINE(void AddAll(const List< T, ZoneAllocationPolicy > &other, Zone *zone))
Definition: zone.h:204
const int kPointerSize
Definition: globals.h:220
INLINE(void Add(const T &element, Zone *zone))
Definition: zone.h:201
static unsigned allocation_size_
Definition: zone.h:91
friend class ZoneScope
Definition: zone.h:95
void adjust_segment_bytes_allocated(int delta)
Definition: zone-inl.h:82
void DeleteKeptSegment()
Definition: zone.cc:159
#define BASE_EMBEDDED
Definition: allocation.h:68
T * NewArray(int length)
Definition: zone-inl.h:72
ZoneList(const ZoneList< T > &other, Zone *zone)
Definition: zone.h:193
#define T(name, string, precedence)
Definition: token.cc:48
INLINE(void *operator new(size_t size, Zone *zone))
INLINE(static HeapObject *EnsureDoubleAligned(Heap *heap, HeapObject *object, int size))
ZoneScopeMode
Definition: zone.h:44
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
ZoneAllocationPolicy(Zone *zone)
Definition: zone.h:169
void * New(int size)
Definition: zone-inl.h:42
TemplateHashMapImpl< ZoneAllocationPolicy > ZoneHashMap
Definition: zone.h:269
INLINE(void Initialize(int capacity, Zone *zone))
Definition: zone.h:222
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:263
ZoneList(int capacity, Zone *zone)
Definition: zone.h:187
const int MB
Definition: globals.h:208