Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 32 | #include "checks.h" |
| 33 | #include "hashmap.h" |
| 34 | #include "globals.h" |
| 35 | #include "list.h" |
| 36 | #include "splay-tree.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 37 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
| 44 | enum ZoneScopeMode { |
| 45 | DELETE_ON_EXIT, |
| 46 | DONT_DELETE_ON_EXIT |
| 47 | }; |
| 48 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 49 | class Segment; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 50 | class Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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(). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 69 | inline void* New(int size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | |
| 71 | template <typename T> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 72 | inline T* NewArray(int length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 74 | // Deletes all objects and free all memory allocated in the Zone. Keeps one |
| 75 | // small (size <= kMaximumKeptSegmentSize) segment around if it finds one. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 76 | void DeleteAll(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 78 | // Deletes the last small segment kept around by DeleteAll(). |
| 79 | void DeleteKeptSegment(); |
| 80 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | // Returns true if more memory has been allocated in zones than |
| 82 | // the limit allows. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 83 | inline bool excess_allocation(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 85 | inline void adjust_segment_bytes_allocated(int delta); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 87 | inline Isolate* isolate() { return isolate_; } |
| 88 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 89 | static unsigned allocation_size_; |
| 90 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 92 | friend class Isolate; |
| 93 | friend class ZoneScope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 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. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 110 | int zone_excess_limit_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 115 | int segment_bytes_allocated_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 117 | // Each isolate gets its own zone. |
| 118 | Zone(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 124 | Address NewExpand(int size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 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); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 136 | Address position_; |
| 137 | Address limit_; |
| 138 | |
| 139 | int scope_nesting_; |
| 140 | |
| 141 | Segment* segment_head_; |
| 142 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 151 | INLINE(void* operator new(size_t size)); |
| 152 | INLINE(void* operator new(size_t size, Zone* zone)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | |
| 154 | // Ideally, the delete operator should be private instead of |
| 155 | // public, but unfortunately the compiler sometimes synthesizes |
| 156 | // (unused) destructors for classes derived from ZoneObject, which |
| 157 | // require the operator to be visible. MSVC requires the delete |
| 158 | // operator to be public. |
| 159 | |
| 160 | // ZoneObjects should never be deleted individually; use |
| 161 | // Zone::DeleteAll() to delete all zone objects in one go. |
| 162 | void operator delete(void*, size_t) { UNREACHABLE(); } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 163 | void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | // The ZoneListAllocationPolicy is used to specialize the GenericList |
| 168 | // implementation to allocate ZoneLists and their elements in the |
| 169 | // Zone. |
| 170 | class ZoneListAllocationPolicy { |
| 171 | public: |
| 172 | // Allocate 'size' bytes of memory in the zone. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 173 | static void* New(int size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | |
| 175 | // De-allocation attempts are silently ignored. |
| 176 | static void Delete(void* p) { } |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | // ZoneLists are growable lists with constant-time access to the |
| 181 | // elements. The list itself and all its elements are allocated in the |
| 182 | // Zone. ZoneLists cannot be deleted individually; you can delete all |
| 183 | // objects in the Zone by calling Zone::DeleteAll(). |
| 184 | template<typename T> |
| 185 | class ZoneList: public List<T, ZoneListAllocationPolicy> { |
| 186 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 187 | INLINE(void* operator new(size_t size)); |
| 188 | INLINE(void* operator new(size_t size, Zone* zone)); |
| 189 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | // Construct a new ZoneList with the given capacity; the length is |
| 191 | // always zero. The capacity must be non-negative. |
| 192 | explicit ZoneList(int capacity) |
| 193 | : List<T, ZoneListAllocationPolicy>(capacity) { } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 194 | |
| 195 | // Construct a new ZoneList by copying the elements of the given ZoneList. |
| 196 | explicit ZoneList(const ZoneList<T>& other) |
| 197 | : List<T, ZoneListAllocationPolicy>(other.length()) { |
| 198 | AddAll(other); |
| 199 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 200 | |
| 201 | void operator delete(void* pointer) { UNREACHABLE(); } |
| 202 | void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | |
| 206 | // ZoneScopes keep track of the current parsing and compilation |
| 207 | // nesting and cleans up generated ASTs in the Zone when exiting the |
| 208 | // outer-most scope. |
| 209 | class ZoneScope BASE_EMBEDDED { |
| 210 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 211 | INLINE(ZoneScope(Isolate* isolate, ZoneScopeMode mode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 213 | virtual ~ZoneScope(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 214 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 215 | inline bool ShouldDeleteOnExit(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | |
| 217 | // For ZoneScopes that do not delete on exit by default, call this |
| 218 | // method to request deletion on exit. |
| 219 | void DeleteOnExit() { |
| 220 | mode_ = DELETE_ON_EXIT; |
| 221 | } |
| 222 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 223 | inline static int nesting(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | |
| 225 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 226 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 227 | ZoneScopeMode mode_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | |
| 231 | // A zone splay tree. The config type parameter encapsulates the |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 232 | // different configurations of a concrete splay tree (see splay-tree.h). |
| 233 | // The tree itself and all its elements are allocated in the Zone. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | template <typename Config> |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 235 | class ZoneSplayTree: public SplayTree<Config, ZoneListAllocationPolicy> { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | public: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 237 | ZoneSplayTree() |
| 238 | : SplayTree<Config, ZoneListAllocationPolicy>() {} |
| 239 | ~ZoneSplayTree(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 243 | typedef TemplateHashMapImpl<ZoneListAllocationPolicy> ZoneHashMap; |
| 244 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | } } // namespace v8::internal |
| 246 | |
| 247 | #endif // V8_ZONE_H_ |