v8  3.11.10(node0.8.26)
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  // 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().
79  void DeleteKeptSegment();
80 
81  // Returns true if more memory has been allocated in zones than
82  // the limit allows.
83  inline bool excess_allocation();
84 
85  inline void adjust_segment_bytes_allocated(int delta);
86 
87  inline Isolate* isolate() { return isolate_; }
88 
89  static unsigned allocation_size_;
90 
91  private:
92  friend class Isolate;
93  friend class ZoneScope;
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.
98  static const int kAlignment = kPointerSize;
99 
100  // Never allocate segments smaller than this size in bytes.
101  static const int kMinimumSegmentSize = 8 * KB;
102 
103  // Never allocate segments larger than this size in bytes.
104  static const int kMaximumSegmentSize = 1 * MB;
105 
106  // Never keep segments larger than this size in bytes around.
107  static const int kMaximumKeptSegmentSize = 64 * KB;
108 
109  // Report zone excess when allocation exceeds this limit.
110  int zone_excess_limit_;
111 
112  // The number of bytes allocated in segments. Note that this number
113  // includes memory allocated from the OS but not yet allocated from
114  // the zone.
115  int segment_bytes_allocated_;
116 
117  // Each isolate gets its own zone.
118  Zone();
119 
120  // Expand the Zone to hold at least 'size' more bytes and allocate
121  // the bytes. Returns the address of the newly allocated chunk of
122  // memory in the Zone. Should only be called if there isn't enough
123  // room in the Zone already.
124  Address NewExpand(int size);
125 
126  // Creates a new segment, sets it size, and pushes it to the front
127  // of the segment chain. Returns the new segment.
128  Segment* NewSegment(int size);
129 
130  // Deletes the given segment. Does not touch the segment chain.
131  void DeleteSegment(Segment* segment, int size);
132 
133  // The free region in the current (front) segment is represented as
134  // the half-open interval [position, limit). The 'position' variable
135  // is guaranteed to be aligned as dictated by kAlignment.
136  Address position_;
137  Address limit_;
138 
139  int scope_nesting_;
140 
141  Segment* segment_head_;
142  Isolate* isolate_;
143 };
144 
145 
146 // ZoneObject is an abstraction that helps define classes of objects
147 // allocated in the Zone. Use it as a base class; see ast.h.
148 class ZoneObject {
149  public:
150  // Allocate a new ZoneObject of 'size' bytes in the Zone.
151  INLINE(void* operator new(size_t size, Zone* zone));
152 
153  // Ideally, the delete operator should be private instead of
154  // public, but unfortunately the compiler sometimes synthesizes
155  // (unused) destructors for classes derived from ZoneObject, which
156  // require the operator to be visible. MSVC requires the delete
157  // operator to be public.
158 
159  // ZoneObjects should never be deleted individually; use
160  // Zone::DeleteAll() to delete all zone objects in one go.
161  void operator delete(void*, size_t) { UNREACHABLE(); }
162  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
163 };
164 
165 
166 // The ZoneAllocationPolicy is used to specialize generic data
167 // structures to allocate themselves and their elements in the Zone.
169  public:
170  explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { }
171  INLINE(void* New(size_t size));
172  INLINE(static void Delete(void *pointer)) { }
173 
174  private:
175  Zone* zone_;
176 };
177 
178 
179 // ZoneLists are growable lists with constant-time access to the
180 // elements. The list itself and all its elements are allocated in the
181 // Zone. ZoneLists cannot be deleted individually; you can delete all
182 // objects in the Zone by calling Zone::DeleteAll().
183 template<typename T>
184 class ZoneList: public List<T, ZoneAllocationPolicy> {
185  public:
186  // Construct a new ZoneList with the given capacity; the length is
187  // always zero. The capacity must be non-negative.
188  ZoneList(int capacity, Zone* zone)
189  : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { }
190 
191  INLINE(void* operator new(size_t size, Zone* zone));
192 
193  // Construct a new ZoneList by copying the elements of the given ZoneList.
194  ZoneList(const ZoneList<T>& other, Zone* zone)
195  : List<T, ZoneAllocationPolicy>(other.length(),
196  ZoneAllocationPolicy(zone)) {
197  AddAll(other, ZoneAllocationPolicy(zone));
198  }
199 
200  // We add some convenience wrappers so that we can pass in a Zone
201  // instead of a (less convenient) ZoneAllocationPolicy.
202  INLINE(void Add(const T& element, Zone* zone)) {
204  }
206  Zone* zone)) {
208  }
209  INLINE(void AddAll(const Vector<T>& other, Zone* zone)) {
211  }
212  INLINE(void InsertAt(int index, const T& element, Zone* zone)) {
214  ZoneAllocationPolicy(zone));
215  }
216  INLINE(Vector<T> AddBlock(T value, int count, Zone* zone)) {
217  return List<T, ZoneAllocationPolicy>::AddBlock(value, count,
218  ZoneAllocationPolicy(zone));
219  }
220  INLINE(void Allocate(int length, Zone* zone)) {
222  }
223  INLINE(void Initialize(int capacity, Zone* zone)) {
225  ZoneAllocationPolicy(zone));
226  }
227 
228  void operator delete(void* pointer) { UNREACHABLE(); }
229  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
230 };
231 
232 
233 // ZoneScopes keep track of the current parsing and compilation
234 // nesting and cleans up generated ASTs in the Zone when exiting the
235 // outer-most scope.
236 class ZoneScope BASE_EMBEDDED {
237  public:
238  INLINE(ZoneScope(Isolate* isolate, ZoneScopeMode mode));
239 
240  virtual ~ZoneScope();
241 
242  inline bool ShouldDeleteOnExit();
243 
244  // For ZoneScopes that do not delete on exit by default, call this
245  // method to request deletion on exit.
246  void DeleteOnExit() {
247  mode_ = DELETE_ON_EXIT;
248  }
249 
250  inline static int nesting();
251 
252  private:
253  Isolate* isolate_;
254  ZoneScopeMode mode_;
255 };
256 
257 
258 // A zone splay tree. The config type parameter encapsulates the
259 // different configurations of a concrete splay tree (see splay-tree.h).
260 // The tree itself and all its elements are allocated in the Zone.
261 template <typename Config>
262 class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> {
263  public:
264  explicit ZoneSplayTree(Zone* zone)
266  ~ZoneSplayTree();
267 };
268 
269 
271 
272 } } // namespace v8::internal
273 
274 #endif // V8_ZONE_H_
byte * Address
Definition: globals.h:172
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:221
INLINE(static void Delete(void *pointer))
Definition: zone.h:172
INLINE(void AddAll(const Vector< T > &other, Zone *zone))
Definition: zone.h:209
INLINE(void *operator new(size_t size, Zone *zone))
INLINE(void InsertAt(int index, const T &element, Zone *zone))
Definition: zone.h:212
INLINE(Vector< T > AddBlock(T value, int count, Zone *zone))
Definition: zone.h:216
void DeleteAll()
Definition: zone.cc:107
Isolate * isolate()
Definition: zone.h:87
INLINE(void Allocate(int length, Zone *zone))
Definition: zone.h:220
#define UNREACHABLE()
Definition: checks.h:50
INLINE(void AddAll(const List< T, ZoneAllocationPolicy > &other, Zone *zone))
Definition: zone.h:205
const int kPointerSize
Definition: globals.h:234
INLINE(void Add(const T &element, Zone *zone))
Definition: zone.h:202
static unsigned allocation_size_
Definition: zone.h:89
friend class ZoneScope
Definition: zone.h:93
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:194
#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:170
void * New(int size)
Definition: zone-inl.h:42
TemplateHashMapImpl< ZoneAllocationPolicy > ZoneHashMap
Definition: zone.h:270
INLINE(void Initialize(int capacity, Zone *zone))
Definition: zone.h:223
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:264
ZoneList(int capacity, Zone *zone)
Definition: zone.h:188
const int MB
Definition: globals.h:222