ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
| 34 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 35 | // Zone scopes are in one of two modes. Either they delete the zone |
| 36 | // on exit or they do not. |
| 37 | enum ZoneScopeMode { |
| 38 | DELETE_ON_EXIT, |
| 39 | DONT_DELETE_ON_EXIT |
| 40 | }; |
| 41 | |
| 42 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | // The Zone supports very fast allocation of small chunks of |
| 44 | // memory. The chunks cannot be deallocated individually, but instead |
| 45 | // the Zone supports deallocating all chunks in one fast |
| 46 | // operation. The Zone is used to hold temporary data structures like |
| 47 | // the abstract syntax tree, which is deallocated after compilation. |
| 48 | |
| 49 | // Note: There is no need to initialize the Zone; the first time an |
| 50 | // allocation is attempted, a segment of memory will be requested |
| 51 | // through a call to malloc(). |
| 52 | |
| 53 | // Note: The implementation is inherently not thread safe. Do not use |
| 54 | // from multi-threaded code. |
| 55 | |
| 56 | class Zone { |
| 57 | public: |
| 58 | // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
| 59 | // allocating new segments of memory on demand using malloc(). |
| 60 | static inline void* New(int size); |
| 61 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 62 | template <typename T> |
| 63 | static inline T* NewArray(int length); |
| 64 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | // Delete all objects and free all memory allocated in the Zone. |
| 66 | static void DeleteAll(); |
| 67 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 68 | // Returns true if more memory has been allocated in zones than |
| 69 | // the limit allows. |
| 70 | static inline bool excess_allocation(); |
| 71 | |
| 72 | static inline void adjust_segment_bytes_allocated(int delta); |
| 73 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | private: |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 75 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | // All pointers returned from New() have this alignment. |
| 77 | static const int kAlignment = kPointerSize; |
| 78 | |
| 79 | // Never allocate segments smaller than this size in bytes. |
| 80 | static const int kMinimumSegmentSize = 8 * KB; |
| 81 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 82 | // Never allocate segments larger than this size in bytes. |
| 83 | static const int kMaximumSegmentSize = 1 * MB; |
| 84 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | // Never keep segments larger than this size in bytes around. |
| 86 | static const int kMaximumKeptSegmentSize = 64 * KB; |
| 87 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 88 | // Report zone excess when allocation exceeds this limit. |
| 89 | static int zone_excess_limit_; |
| 90 | |
| 91 | // The number of bytes allocated in segments. Note that this number |
| 92 | // includes memory allocated from the OS but not yet allocated from |
| 93 | // the zone. |
| 94 | static int segment_bytes_allocated_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | |
| 96 | // The Zone is intentionally a singleton; you should not try to |
| 97 | // allocate instances of the class. |
| 98 | Zone() { UNREACHABLE(); } |
| 99 | |
| 100 | |
| 101 | // Expand the Zone to hold at least 'size' more bytes and allocate |
| 102 | // the bytes. Returns the address of the newly allocated chunk of |
| 103 | // memory in the Zone. Should only be called if there isn't enough |
| 104 | // room in the Zone already. |
| 105 | static Address NewExpand(int size); |
| 106 | |
| 107 | |
| 108 | // The free region in the current (front) segment is represented as |
| 109 | // the half-open interval [position, limit). The 'position' variable |
| 110 | // is guaranteed to be aligned as dictated by kAlignment. |
| 111 | static Address position_; |
| 112 | static Address limit_; |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | // ZoneObject is an abstraction that helps define classes of objects |
| 117 | // allocated in the Zone. Use it as a base class; see ast.h. |
| 118 | class ZoneObject { |
| 119 | public: |
| 120 | // Allocate a new ZoneObject of 'size' bytes in the Zone. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 121 | void* operator new(size_t size) { return Zone::New(static_cast<int>(size)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | |
| 123 | // Ideally, the delete operator should be private instead of |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 124 | // public, but unfortunately the compiler sometimes synthesizes |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | // (unused) destructors for classes derived from ZoneObject, which |
| 126 | // require the operator to be visible. MSVC requires the delete |
| 127 | // operator to be public. |
| 128 | |
| 129 | // ZoneObjects should never be deleted individually; use |
| 130 | // Zone::DeleteAll() to delete all zone objects in one go. |
| 131 | void operator delete(void*, size_t) { UNREACHABLE(); } |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | class AssertNoZoneAllocation { |
| 136 | public: |
| 137 | AssertNoZoneAllocation() : prev_(allow_allocation_) { |
| 138 | allow_allocation_ = false; |
| 139 | } |
| 140 | ~AssertNoZoneAllocation() { allow_allocation_ = prev_; } |
| 141 | static bool allow_allocation() { return allow_allocation_; } |
| 142 | private: |
| 143 | bool prev_; |
| 144 | static bool allow_allocation_; |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | // The ZoneListAllocationPolicy is used to specialize the GenericList |
| 149 | // implementation to allocate ZoneLists and their elements in the |
| 150 | // Zone. |
| 151 | class ZoneListAllocationPolicy { |
| 152 | public: |
| 153 | // Allocate 'size' bytes of memory in the zone. |
| 154 | static void* New(int size) { return Zone::New(size); } |
| 155 | |
| 156 | // De-allocation attempts are silently ignored. |
| 157 | static void Delete(void* p) { } |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | // ZoneLists are growable lists with constant-time access to the |
| 162 | // elements. The list itself and all its elements are allocated in the |
| 163 | // Zone. ZoneLists cannot be deleted individually; you can delete all |
| 164 | // objects in the Zone by calling Zone::DeleteAll(). |
| 165 | template<typename T> |
| 166 | class ZoneList: public List<T, ZoneListAllocationPolicy> { |
| 167 | public: |
| 168 | // Construct a new ZoneList with the given capacity; the length is |
| 169 | // always zero. The capacity must be non-negative. |
| 170 | explicit ZoneList(int capacity) |
| 171 | : List<T, ZoneListAllocationPolicy>(capacity) { } |
| 172 | }; |
| 173 | |
| 174 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 175 | // ZoneScopes keep track of the current parsing and compilation |
| 176 | // nesting and cleans up generated ASTs in the Zone when exiting the |
| 177 | // outer-most scope. |
| 178 | class ZoneScope BASE_EMBEDDED { |
| 179 | public: |
| 180 | explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) { |
| 181 | nesting_++; |
| 182 | } |
| 183 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 184 | virtual ~ZoneScope() { |
| 185 | if (ShouldDeleteOnExit()) Zone::DeleteAll(); |
| 186 | --nesting_; |
| 187 | } |
| 188 | |
| 189 | bool ShouldDeleteOnExit() { |
| 190 | return nesting_ == 1 && mode_ == DELETE_ON_EXIT; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // For ZoneScopes that do not delete on exit by default, call this |
| 194 | // method to request deletion on exit. |
| 195 | void DeleteOnExit() { |
| 196 | mode_ = DELETE_ON_EXIT; |
| 197 | } |
| 198 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 199 | static int nesting() { return nesting_; } |
| 200 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 201 | private: |
| 202 | ZoneScopeMode mode_; |
| 203 | static int nesting_; |
| 204 | }; |
| 205 | |
| 206 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 207 | // A zone splay tree. The config type parameter encapsulates the |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 208 | // different configurations of a concrete splay tree (see splay-tree.h). |
| 209 | // The tree itself and all its elements are allocated in the Zone. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 210 | template <typename Config> |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 211 | class ZoneSplayTree: public SplayTree<Config, ZoneListAllocationPolicy> { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 212 | public: |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 213 | ZoneSplayTree() |
| 214 | : SplayTree<Config, ZoneListAllocationPolicy>() {} |
| 215 | ~ZoneSplayTree(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | } } // namespace v8::internal |
| 220 | |
| 221 | #endif // V8_ZONE_H_ |