The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / zone.h @ 40c0f755

History | View | Annotate | Download (6.96 KB)

1
// Copyright 2006-2008 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
namespace v8 { namespace internal {
32

    
33

    
34
// Zone scopes are in one of two modes.  Either they delete the zone
35
// on exit or they do not.
36
enum ZoneScopeMode {
37
  DELETE_ON_EXIT,
38
  DONT_DELETE_ON_EXIT
39
};
40

    
41

    
42
// The Zone supports very fast allocation of small chunks of
43
// memory. The chunks cannot be deallocated individually, but instead
44
// the Zone supports deallocating all chunks in one fast
45
// operation. The Zone is used to hold temporary data structures like
46
// the abstract syntax tree, which is deallocated after compilation.
47

    
48
// Note: There is no need to initialize the Zone; the first time an
49
// allocation is attempted, a segment of memory will be requested
50
// through a call to malloc().
51

    
52
// Note: The implementation is inherently not thread safe. Do not use
53
// from multi-threaded code.
54

    
55
class Zone {
56
 public:
57
  // Allocate 'size' bytes of memory in the Zone; expands the Zone by
58
  // allocating new segments of memory on demand using malloc().
59
  static inline void* New(int size);
60

    
61
  template <typename T>
62
  static inline T* NewArray(int length);
63

    
64
  // Delete all objects and free all memory allocated in the Zone.
65
  static void DeleteAll();
66

    
67
  // Returns true if more memory has been allocated in zones than
68
  // the limit allows.
69
  static inline bool excess_allocation();
70

    
71
  static inline void adjust_segment_bytes_allocated(int delta);
72

    
73
 private:
74

    
75
  // All pointers returned from New() have this alignment.
76
  static const int kAlignment = kPointerSize;
77

    
78
  // Never allocate segments smaller than this size in bytes.
79
  static const int kMinimumSegmentSize = 8 * KB;
80

    
81
  // Never allocate segments larger than this size in bytes.
82
  static const int kMaximumSegmentSize = 1 * MB;
83

    
84
  // Never keep segments larger than this size in bytes around.
85
  static const int kMaximumKeptSegmentSize = 64 * KB;
86

    
87
  // Report zone excess when allocation exceeds this limit.
88
  static int zone_excess_limit_;
89

    
90
  // The number of bytes allocated in segments.  Note that this number
91
  // includes memory allocated from the OS but not yet allocated from
92
  // the zone.
93
  static int segment_bytes_allocated_;
94

    
95
  // The Zone is intentionally a singleton; you should not try to
96
  // allocate instances of the class.
97
  Zone() { UNREACHABLE(); }
98

    
99

    
100
  // Expand the Zone to hold at least 'size' more bytes and allocate
101
  // the bytes. Returns the address of the newly allocated chunk of
102
  // memory in the Zone. Should only be called if there isn't enough
103
  // room in the Zone already.
104
  static Address NewExpand(int size);
105

    
106

    
107
  // The free region in the current (front) segment is represented as
108
  // the half-open interval [position, limit). The 'position' variable
109
  // is guaranteed to be aligned as dictated by kAlignment.
110
  static Address position_;
111
  static Address limit_;
112
};
113

    
114

    
115
// ZoneObject is an abstraction that helps define classes of objects
116
// allocated in the Zone. Use it as a base class; see ast.h.
117
class ZoneObject {
118
 public:
119
  // Allocate a new ZoneObject of 'size' bytes in the Zone.
120
  void* operator new(size_t size) { return Zone::New(size); }
121

    
122
  // Ideally, the delete operator should be private instead of
123
  // public, but unfortunately the compiler sometimes synthesizes
124
  // (unused) destructors for classes derived from ZoneObject, which
125
  // require the operator to be visible. MSVC requires the delete
126
  // operator to be public.
127

    
128
  // ZoneObjects should never be deleted individually; use
129
  // Zone::DeleteAll() to delete all zone objects in one go.
130
  void operator delete(void*, size_t) { UNREACHABLE(); }
131
};
132

    
133

    
134
class AssertNoZoneAllocation {
135
 public:
136
  AssertNoZoneAllocation() : prev_(allow_allocation_) {
137
    allow_allocation_ = false;
138
  }
139
  ~AssertNoZoneAllocation() { allow_allocation_ = prev_; }
140
  static bool allow_allocation() { return allow_allocation_; }
141
 private:
142
  bool prev_;
143
  static bool allow_allocation_;
144
};
145

    
146

    
147
// The ZoneListAllocationPolicy is used to specialize the GenericList
148
// implementation to allocate ZoneLists and their elements in the
149
// Zone.
150
class ZoneListAllocationPolicy {
151
 public:
152
  // Allocate 'size' bytes of memory in the zone.
153
  static void* New(int size) {  return Zone::New(size); }
154

    
155
  // De-allocation attempts are silently ignored.
156
  static void Delete(void* p) { }
157
};
158

    
159

    
160
// ZoneLists are growable lists with constant-time access to the
161
// elements. The list itself and all its elements are allocated in the
162
// Zone. ZoneLists cannot be deleted individually; you can delete all
163
// objects in the Zone by calling Zone::DeleteAll().
164
template<typename T>
165
class ZoneList: public List<T, ZoneListAllocationPolicy> {
166
 public:
167
  // Construct a new ZoneList with the given capacity; the length is
168
  // always zero. The capacity must be non-negative.
169
  explicit ZoneList(int capacity)
170
      : List<T, ZoneListAllocationPolicy>(capacity) { }
171
};
172

    
173

    
174
// ZoneScopes keep track of the current parsing and compilation
175
// nesting and cleans up generated ASTs in the Zone when exiting the
176
// outer-most scope.
177
class ZoneScope BASE_EMBEDDED {
178
 public:
179
  explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) {
180
    nesting_++;
181
  }
182

    
183
  ~ZoneScope() {
184
    if (--nesting_ == 0 && mode_ == DELETE_ON_EXIT) Zone::DeleteAll();
185
  }
186

    
187
  // For ZoneScopes that do not delete on exit by default, call this
188
  // method to request deletion on exit.
189
  void DeleteOnExit() {
190
    mode_ = DELETE_ON_EXIT;
191
  }
192

    
193
  static int nesting() { return nesting_; }
194

    
195
 private:
196
  ZoneScopeMode mode_;
197
  static int nesting_;
198
};
199

    
200

    
201
} }  // namespace v8::internal
202

    
203
#endif  // V8_ZONE_H_