danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1 | // Copyright 2012 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 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 32 | #include "checks.h" |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 33 | #include "hashmap.h" |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 34 | #include "globals.h" |
| 35 | #include "list.h" |
| 36 | #include "splay-tree.h" |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 37 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 38 | namespace v8 { |
| 39 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
| 41 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 42 | class Segment; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 43 | class Isolate; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 44 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | // The Zone supports very fast allocation of small chunks of |
| 46 | // memory. The chunks cannot be deallocated individually, but instead |
| 47 | // the Zone supports deallocating all chunks in one fast |
| 48 | // operation. The Zone is used to hold temporary data structures like |
| 49 | // the abstract syntax tree, which is deallocated after compilation. |
| 50 | |
| 51 | // Note: There is no need to initialize the Zone; the first time an |
| 52 | // allocation is attempted, a segment of memory will be requested |
| 53 | // through a call to malloc(). |
| 54 | |
| 55 | // Note: The implementation is inherently not thread safe. Do not use |
| 56 | // from multi-threaded code. |
| 57 | |
| 58 | class Zone { |
| 59 | public: |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 60 | explicit Zone(Isolate* isolate); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 61 | ~Zone(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | // Allocate 'size' bytes of memory in the Zone; expands the Zone by |
| 63 | // allocating new segments of memory on demand using malloc(). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 64 | inline void* New(int size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 66 | template <typename T> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | inline T* NewArray(int length); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 68 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 69 | // Returns true if more memory has been allocated in zones than |
| 70 | // the limit allows. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 71 | inline bool excess_allocation(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 72 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 73 | inline void adjust_segment_bytes_allocated(int delta); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 74 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 75 | inline unsigned allocation_size() { return allocation_size_; } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 76 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 77 | inline Isolate* isolate() { return isolate_; } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 78 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 80 | friend class Isolate; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 81 | |
danno@chromium.org | 50d79f3 | 2011-11-11 12:01:18 +0000 | [diff] [blame] | 82 | // All pointers returned from New() have this alignment. In addition, if the |
| 83 | // object being allocated has a size that is divisible by 8 then its alignment |
| 84 | // will be 8. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | static const int kAlignment = kPointerSize; |
| 86 | |
| 87 | // Never allocate segments smaller than this size in bytes. |
| 88 | static const int kMinimumSegmentSize = 8 * KB; |
| 89 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 90 | // Never allocate segments larger than this size in bytes. |
| 91 | static const int kMaximumSegmentSize = 1 * MB; |
| 92 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 93 | // Report zone excess when allocation exceeds this limit. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 94 | static const int kExcessLimit = 256 * MB; |
| 95 | |
| 96 | // The number of bytes allocated in this zone so far. |
| 97 | unsigned allocation_size_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 98 | |
| 99 | // The number of bytes allocated in segments. Note that this number |
| 100 | // includes memory allocated from the OS but not yet allocated from |
| 101 | // the zone. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 102 | int segment_bytes_allocated_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | // Expand the Zone to hold at least 'size' more bytes and allocate |
| 105 | // the bytes. Returns the address of the newly allocated chunk of |
| 106 | // memory in the Zone. Should only be called if there isn't enough |
| 107 | // room in the Zone already. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 108 | Address NewExpand(int size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 110 | // Creates a new segment, sets it size, and pushes it to the front |
| 111 | // of the segment chain. Returns the new segment. |
| 112 | Segment* NewSegment(int size); |
| 113 | |
| 114 | // Deletes the given segment. Does not touch the segment chain. |
| 115 | void DeleteSegment(Segment* segment, int size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | |
| 117 | // The free region in the current (front) segment is represented as |
| 118 | // the half-open interval [position, limit). The 'position' variable |
| 119 | // is guaranteed to be aligned as dictated by kAlignment. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 120 | Address position_; |
| 121 | Address limit_; |
| 122 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 123 | Segment* segment_head_; |
| 124 | Isolate* isolate_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | |
| 128 | // ZoneObject is an abstraction that helps define classes of objects |
| 129 | // allocated in the Zone. Use it as a base class; see ast.h. |
| 130 | class ZoneObject { |
| 131 | public: |
| 132 | // Allocate a new ZoneObject of 'size' bytes in the Zone. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 133 | INLINE(void* operator new(size_t size, Zone* zone)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | |
| 135 | // Ideally, the delete operator should be private instead of |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 136 | // public, but unfortunately the compiler sometimes synthesizes |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | // (unused) destructors for classes derived from ZoneObject, which |
| 138 | // require the operator to be visible. MSVC requires the delete |
| 139 | // operator to be public. |
| 140 | |
| 141 | // ZoneObjects should never be deleted individually; use |
| 142 | // Zone::DeleteAll() to delete all zone objects in one go. |
| 143 | void operator delete(void*, size_t) { UNREACHABLE(); } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 144 | void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 148 | // The ZoneAllocationPolicy is used to specialize generic data |
| 149 | // structures to allocate themselves and their elements in the Zone. |
| 150 | struct ZoneAllocationPolicy { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 152 | explicit ZoneAllocationPolicy(Zone* zone) : zone_(zone) { } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 153 | INLINE(void* New(size_t size)); |
| 154 | INLINE(static void Delete(void *pointer)) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 156 | private: |
| 157 | Zone* zone_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 166 | class ZoneList: public List<T, ZoneAllocationPolicy> { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | public: |
| 168 | // Construct a new ZoneList with the given capacity; the length is |
| 169 | // always zero. The capacity must be non-negative. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 170 | ZoneList(int capacity, Zone* zone) |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 171 | : List<T, ZoneAllocationPolicy>(capacity, ZoneAllocationPolicy(zone)) { } |
| 172 | |
| 173 | INLINE(void* operator new(size_t size, Zone* zone)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 174 | |
| 175 | // Construct a new ZoneList by copying the elements of the given ZoneList. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 176 | ZoneList(const ZoneList<T>& other, Zone* zone) |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 177 | : List<T, ZoneAllocationPolicy>(other.length(), |
| 178 | ZoneAllocationPolicy(zone)) { |
| 179 | AddAll(other, ZoneAllocationPolicy(zone)); |
| 180 | } |
| 181 | |
| 182 | // We add some convenience wrappers so that we can pass in a Zone |
| 183 | // instead of a (less convenient) ZoneAllocationPolicy. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 184 | INLINE(void Add(const T& element, Zone* zone)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 185 | List<T, ZoneAllocationPolicy>::Add(element, ZoneAllocationPolicy(zone)); |
| 186 | } |
| 187 | INLINE(void AddAll(const List<T, ZoneAllocationPolicy>& other, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 188 | Zone* zone)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 189 | List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); |
| 190 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 191 | INLINE(void AddAll(const Vector<T>& other, Zone* zone)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 192 | List<T, ZoneAllocationPolicy>::AddAll(other, ZoneAllocationPolicy(zone)); |
| 193 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 194 | INLINE(void InsertAt(int index, const T& element, Zone* zone)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 195 | List<T, ZoneAllocationPolicy>::InsertAt(index, element, |
| 196 | ZoneAllocationPolicy(zone)); |
| 197 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 198 | INLINE(Vector<T> AddBlock(T value, int count, Zone* zone)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 199 | return List<T, ZoneAllocationPolicy>::AddBlock(value, count, |
| 200 | ZoneAllocationPolicy(zone)); |
| 201 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 202 | INLINE(void Allocate(int length, Zone* zone)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 203 | List<T, ZoneAllocationPolicy>::Allocate(length, ZoneAllocationPolicy(zone)); |
| 204 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 205 | INLINE(void Initialize(int capacity, Zone* zone)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 206 | List<T, ZoneAllocationPolicy>::Initialize(capacity, |
| 207 | ZoneAllocationPolicy(zone)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 208 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 209 | |
| 210 | void operator delete(void* pointer) { UNREACHABLE(); } |
| 211 | void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 215 | // A zone splay tree. The config type parameter encapsulates the |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 216 | // different configurations of a concrete splay tree (see splay-tree.h). |
| 217 | // 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] | 218 | template <typename Config> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 219 | class ZoneSplayTree: public SplayTree<Config, ZoneAllocationPolicy> { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 220 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 221 | explicit ZoneSplayTree(Zone* zone) |
| 222 | : SplayTree<Config, ZoneAllocationPolicy>(ZoneAllocationPolicy(zone)) {} |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 223 | ~ZoneSplayTree(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 227 | typedef TemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 228 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 229 | } } // namespace v8::internal |
| 230 | |
| 231 | #endif // V8_ZONE_H_ |