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 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 28 | #include <string.h> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 29 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 30 | #include "v8.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include "zone-inl.h" |
| 32 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
| 36 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | // Segments represent chunks of memory: They have starting address |
| 38 | // (encoded in the this pointer) and a size in bytes. Segments are |
| 39 | // chained together forming a LIFO structure with the newest segment |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 40 | // available as segment_head_. Segments are allocated using malloc() |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | // and de-allocated using free(). |
| 42 | |
| 43 | class Segment { |
| 44 | public: |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 45 | void Initialize(Segment* next, int size) { |
| 46 | next_ = next; |
| 47 | size_ = size; |
| 48 | } |
| 49 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | Segment* next() const { return next_; } |
| 51 | void clear_next() { next_ = NULL; } |
| 52 | |
| 53 | int size() const { return size_; } |
| 54 | int capacity() const { return size_ - sizeof(Segment); } |
| 55 | |
| 56 | Address start() const { return address(sizeof(Segment)); } |
| 57 | Address end() const { return address(size_); } |
| 58 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | private: |
| 60 | // Computes the address of the nth byte in this segment. |
| 61 | Address address(int n) const { |
| 62 | return Address(this) + n; |
| 63 | } |
| 64 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | Segment* next_; |
| 66 | int size_; |
| 67 | }; |
| 68 | |
| 69 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 70 | Zone::Zone(Isolate* isolate) |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 71 | : allocation_size_(0), |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 72 | segment_bytes_allocated_(0), |
| 73 | position_(0), |
| 74 | limit_(0), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 75 | segment_head_(NULL), |
| 76 | isolate_(isolate) { |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 77 | } |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 78 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 79 | |
| 80 | Zone::~Zone() { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 81 | DeleteAll(); |
| 82 | DeleteKeptSegment(); |
| 83 | |
| 84 | ASSERT(segment_bytes_allocated_ == 0); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void Zone::DeleteAll() { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 89 | #ifdef DEBUG |
| 90 | // Constant byte value used for zapping dead memory in debug mode. |
| 91 | static const unsigned char kZapDeadByte = 0xcd; |
| 92 | #endif |
| 93 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 94 | // Find a segment with a suitable size to keep around. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 95 | Segment* keep = NULL; |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 96 | // Traverse the chained list of segments, zapping (in debug mode) |
| 97 | // and freeing every segment except the one we wish to keep. |
| 98 | for (Segment* current = segment_head_; current != NULL; ) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 99 | Segment* next = current->next(); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 100 | if (keep == NULL && current->size() <= kMaximumKeptSegmentSize) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 101 | // Unlink the segment we wish to keep from the list. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 102 | keep = current; |
| 103 | keep->clear_next(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 104 | } else { |
| 105 | int size = current->size(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 106 | #ifdef DEBUG |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 107 | // Zap the entire current segment (including the header). |
| 108 | memset(current, kZapDeadByte, size); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 109 | #endif |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 110 | DeleteSegment(current, size); |
| 111 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 112 | current = next; |
| 113 | } |
| 114 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 115 | // If we have found a segment we want to keep, we must recompute the |
| 116 | // variables 'position' and 'limit' to prepare for future allocate |
| 117 | // attempts. Otherwise, we must clear the position and limit to |
| 118 | // force a new segment to be allocated on demand. |
| 119 | if (keep != NULL) { |
| 120 | Address start = keep->start(); |
| 121 | position_ = RoundUp(start, kAlignment); |
| 122 | limit_ = keep->end(); |
| 123 | #ifdef DEBUG |
| 124 | // Zap the contents of the kept segment (but not the header). |
| 125 | memset(start, kZapDeadByte, keep->capacity()); |
| 126 | #endif |
| 127 | } else { |
| 128 | position_ = limit_ = 0; |
| 129 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 130 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 131 | // Update the head segment to be the kept segment (if any). |
| 132 | segment_head_ = keep; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void Zone::DeleteKeptSegment() { |
| 137 | #ifdef DEBUG |
| 138 | // Constant byte value used for zapping dead memory in debug mode. |
| 139 | static const unsigned char kZapDeadByte = 0xcd; |
| 140 | #endif |
| 141 | |
| 142 | ASSERT(segment_head_ == NULL || segment_head_->next() == NULL); |
| 143 | if (segment_head_ != NULL) { |
| 144 | int size = segment_head_->size(); |
| 145 | #ifdef DEBUG |
| 146 | // Zap the entire kept segment (including the header). |
| 147 | memset(segment_head_, kZapDeadByte, size); |
| 148 | #endif |
| 149 | DeleteSegment(segment_head_, size); |
| 150 | segment_head_ = NULL; |
| 151 | } |
| 152 | |
| 153 | ASSERT(segment_bytes_allocated_ == 0); |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 157 | // Creates a new segment, sets it size, and pushes it to the front |
| 158 | // of the segment chain. Returns the new segment. |
| 159 | Segment* Zone::NewSegment(int size) { |
| 160 | Segment* result = reinterpret_cast<Segment*>(Malloced::New(size)); |
| 161 | adjust_segment_bytes_allocated(size); |
| 162 | if (result != NULL) { |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 163 | result->Initialize(segment_head_, size); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 164 | segment_head_ = result; |
| 165 | } |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | // Deletes the given segment. Does not touch the segment chain. |
| 171 | void Zone::DeleteSegment(Segment* segment, int size) { |
| 172 | adjust_segment_bytes_allocated(-size); |
| 173 | Malloced::Delete(segment); |
| 174 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | |
| 176 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | Address Zone::NewExpand(int size) { |
| 178 | // Make sure the requested size is already properly aligned and that |
| 179 | // there isn't enough room in the Zone to satisfy the request. |
| 180 | ASSERT(size == RoundDown(size, kAlignment)); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 181 | ASSERT(size > limit_ - position_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | |
| 183 | // Compute the new segment size. We use a 'high water mark' |
| 184 | // strategy, where we increase the segment size every time we expand |
| 185 | // except that we employ a maximum segment size when we delete. This |
| 186 | // is to avoid excessive malloc() and free() overhead. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 187 | Segment* head = segment_head_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | int old_size = (head == NULL) ? 0 : head->size(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 189 | static const int kSegmentOverhead = sizeof(Segment) + kAlignment; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 190 | int new_size_no_overhead = size + (old_size << 1); |
| 191 | int new_size = kSegmentOverhead + new_size_no_overhead; |
| 192 | // Guard against integer overflow. |
| 193 | if (new_size_no_overhead < size || new_size < kSegmentOverhead) { |
| 194 | V8::FatalProcessOutOfMemory("Zone"); |
| 195 | return NULL; |
| 196 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 197 | if (new_size < kMinimumSegmentSize) { |
| 198 | new_size = kMinimumSegmentSize; |
| 199 | } else if (new_size > kMaximumSegmentSize) { |
| 200 | // Limit the size of new segments to avoid growing the segment size |
| 201 | // exponentially, thus putting pressure on contiguous virtual address space. |
| 202 | // All the while making sure to allocate a segment large enough to hold the |
| 203 | // requested size. |
| 204 | new_size = Max(kSegmentOverhead + size, kMaximumSegmentSize); |
| 205 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 206 | Segment* segment = NewSegment(new_size); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 207 | if (segment == NULL) { |
| 208 | V8::FatalProcessOutOfMemory("Zone"); |
| 209 | return NULL; |
| 210 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | |
| 212 | // Recompute 'top' and 'limit' based on the new segment. |
| 213 | Address result = RoundUp(segment->start(), kAlignment); |
| 214 | position_ = result + size; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 215 | // Check for address overflow. |
| 216 | if (position_ < result) { |
| 217 | V8::FatalProcessOutOfMemory("Zone"); |
| 218 | return NULL; |
| 219 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 220 | limit_ = segment->end(); |
| 221 | ASSERT(position_ <= limit_); |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | } } // namespace v8::internal |