ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1 | // Copyright 2011 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 | #include "v8.h" |
| 29 | |
| 30 | #include "macro-assembler.h" |
| 31 | #include "mark-compact.h" |
| 32 | #include "platform.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- |
| 39 | // HeapObjectIterator |
| 40 | |
| 41 | HeapObjectIterator::HeapObjectIterator(PagedSpace* space) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 42 | // You can't actually iterate over the anchor page. It is not a real page, |
| 43 | // just an anchor for the double linked page list. Initialize as if we have |
| 44 | // reached the end of the anchor page, then the first iteration will move on |
| 45 | // to the first page. |
| 46 | Initialize(space, |
| 47 | NULL, |
| 48 | NULL, |
| 49 | kAllPagesInSpace, |
| 50 | NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
| 54 | HeapObjectIterator::HeapObjectIterator(PagedSpace* space, |
| 55 | HeapObjectCallback size_func) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 56 | // You can't actually iterate over the anchor page. It is not a real page, |
| 57 | // just an anchor for the double linked page list. Initialize the current |
| 58 | // address and end as NULL, then the first iteration will move on |
| 59 | // to the first page. |
| 60 | Initialize(space, |
| 61 | NULL, |
| 62 | NULL, |
| 63 | kAllPagesInSpace, |
| 64 | size_func); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 68 | HeapObjectIterator::HeapObjectIterator(Page* page, |
| 69 | HeapObjectCallback size_func) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 70 | Space* owner = page->owner(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 71 | ASSERT(owner == page->heap()->old_pointer_space() || |
| 72 | owner == page->heap()->old_data_space() || |
| 73 | owner == page->heap()->map_space() || |
| 74 | owner == page->heap()->cell_space() || |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 75 | owner == page->heap()->property_cell_space() || |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 76 | owner == page->heap()->code_space()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 77 | Initialize(reinterpret_cast<PagedSpace*>(owner), |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 78 | page->area_start(), |
| 79 | page->area_end(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 80 | kOnePageOnly, |
| 81 | size_func); |
| 82 | ASSERT(page->WasSweptPrecisely()); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 86 | void HeapObjectIterator::Initialize(PagedSpace* space, |
| 87 | Address cur, Address end, |
| 88 | HeapObjectIterator::PageMode mode, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | HeapObjectCallback size_f) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 90 | // Check that we actually can iterate this space. |
| 91 | ASSERT(!space->was_swept_conservatively()); |
| 92 | |
| 93 | space_ = space; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | cur_addr_ = cur; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 95 | cur_end_ = end; |
| 96 | page_mode_ = mode; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | size_func_ = size_f; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 101 | // We have hit the end of the page and should advance to the next block of |
| 102 | // objects. This happens at the end of the page. |
| 103 | bool HeapObjectIterator::AdvanceToNextPage() { |
| 104 | ASSERT(cur_addr_ == cur_end_); |
| 105 | if (page_mode_ == kOnePageOnly) return false; |
| 106 | Page* cur_page; |
| 107 | if (cur_addr_ == NULL) { |
| 108 | cur_page = space_->anchor(); |
| 109 | } else { |
| 110 | cur_page = Page::FromAddress(cur_addr_ - 1); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 111 | ASSERT(cur_addr_ == cur_page->area_end()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 112 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | cur_page = cur_page->next_page(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 114 | if (cur_page == space_->anchor()) return false; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 115 | cur_addr_ = cur_page->area_start(); |
| 116 | cur_end_ = cur_page->area_end(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 117 | ASSERT(cur_page->WasSweptPrecisely()); |
| 118 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | // ----------------------------------------------------------------------------- |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 123 | // CodeRange |
| 124 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 125 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 126 | CodeRange::CodeRange(Isolate* isolate) |
| 127 | : isolate_(isolate), |
| 128 | code_range_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 129 | free_list_(0), |
| 130 | allocation_list_(0), |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 131 | current_allocation_block_index_(0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 132 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 133 | |
| 134 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 135 | bool CodeRange::SetUp(const size_t requested) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 136 | ASSERT(code_range_ == NULL); |
| 137 | |
| 138 | code_range_ = new VirtualMemory(requested); |
| 139 | CHECK(code_range_ != NULL); |
| 140 | if (!code_range_->IsReserved()) { |
| 141 | delete code_range_; |
| 142 | code_range_ = NULL; |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | // We are sure that we have mapped a block of requested addresses. |
| 147 | ASSERT(code_range_->size() == requested); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 148 | LOG(isolate_, NewEvent("CodeRange", code_range_->address(), requested)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 149 | Address base = reinterpret_cast<Address>(code_range_->address()); |
| 150 | Address aligned_base = |
| 151 | RoundUp(reinterpret_cast<Address>(code_range_->address()), |
| 152 | MemoryChunk::kAlignment); |
| 153 | size_t size = code_range_->size() - (aligned_base - base); |
| 154 | allocation_list_.Add(FreeBlock(aligned_base, size)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 155 | current_allocation_block_index_ = 0; |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | int CodeRange::CompareFreeBlockAddress(const FreeBlock* left, |
| 161 | const FreeBlock* right) { |
| 162 | // The entire point of CodeRange is that the difference between two |
| 163 | // addresses in the range can be represented as a signed 32-bit int, |
| 164 | // so the cast is semantically correct. |
| 165 | return static_cast<int>(left->start - right->start); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void CodeRange::GetNextAllocationBlock(size_t requested) { |
| 170 | for (current_allocation_block_index_++; |
| 171 | current_allocation_block_index_ < allocation_list_.length(); |
| 172 | current_allocation_block_index_++) { |
| 173 | if (requested <= allocation_list_[current_allocation_block_index_].size) { |
| 174 | return; // Found a large enough allocation block. |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Sort and merge the free blocks on the free list and the allocation list. |
| 179 | free_list_.AddAll(allocation_list_); |
| 180 | allocation_list_.Clear(); |
| 181 | free_list_.Sort(&CompareFreeBlockAddress); |
| 182 | for (int i = 0; i < free_list_.length();) { |
| 183 | FreeBlock merged = free_list_[i]; |
| 184 | i++; |
| 185 | // Add adjacent free blocks to the current merged block. |
| 186 | while (i < free_list_.length() && |
| 187 | free_list_[i].start == merged.start + merged.size) { |
| 188 | merged.size += free_list_[i].size; |
| 189 | i++; |
| 190 | } |
| 191 | if (merged.size > 0) { |
| 192 | allocation_list_.Add(merged); |
| 193 | } |
| 194 | } |
| 195 | free_list_.Clear(); |
| 196 | |
| 197 | for (current_allocation_block_index_ = 0; |
| 198 | current_allocation_block_index_ < allocation_list_.length(); |
| 199 | current_allocation_block_index_++) { |
| 200 | if (requested <= allocation_list_[current_allocation_block_index_].size) { |
| 201 | return; // Found a large enough allocation block. |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Code range is full or too fragmented. |
| 206 | V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock"); |
| 207 | } |
| 208 | |
| 209 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 210 | Address CodeRange::AllocateRawMemory(const size_t requested_size, |
| 211 | const size_t commit_size, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 212 | size_t* allocated) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 213 | ASSERT(commit_size <= requested_size); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 214 | ASSERT(current_allocation_block_index_ < allocation_list_.length()); |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 215 | if (requested_size > allocation_list_[current_allocation_block_index_].size) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 216 | // Find an allocation block large enough. This function call may |
| 217 | // call V8::FatalProcessOutOfMemory if it cannot find a large enough block. |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 218 | GetNextAllocationBlock(requested_size); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 219 | } |
| 220 | // Commit the requested memory at the start of the current allocation block. |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 221 | size_t aligned_requested = RoundUp(requested_size, MemoryChunk::kAlignment); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 222 | FreeBlock current = allocation_list_[current_allocation_block_index_]; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 223 | if (aligned_requested >= (current.size - Page::kPageSize)) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 224 | // Don't leave a small free block, useless for a large object or chunk. |
| 225 | *allocated = current.size; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 226 | } else { |
| 227 | *allocated = aligned_requested; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 228 | } |
| 229 | ASSERT(*allocated <= current.size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 230 | ASSERT(IsAddressAligned(current.start, MemoryChunk::kAlignment)); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 231 | if (!isolate_->memory_allocator()->CommitExecutableMemory(code_range_, |
| 232 | current.start, |
| 233 | commit_size, |
| 234 | *allocated)) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 235 | *allocated = 0; |
| 236 | return NULL; |
| 237 | } |
| 238 | allocation_list_[current_allocation_block_index_].start += *allocated; |
| 239 | allocation_list_[current_allocation_block_index_].size -= *allocated; |
| 240 | if (*allocated == current.size) { |
| 241 | GetNextAllocationBlock(0); // This block is used up, get the next one. |
| 242 | } |
| 243 | return current.start; |
| 244 | } |
| 245 | |
| 246 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 247 | bool CodeRange::CommitRawMemory(Address start, size_t length) { |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 248 | return isolate_->memory_allocator()->CommitMemory(start, length, EXECUTABLE); |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
| 252 | bool CodeRange::UncommitRawMemory(Address start, size_t length) { |
| 253 | return code_range_->Uncommit(start, length); |
| 254 | } |
| 255 | |
| 256 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 257 | void CodeRange::FreeRawMemory(Address address, size_t length) { |
| 258 | ASSERT(IsAddressAligned(address, MemoryChunk::kAlignment)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 259 | free_list_.Add(FreeBlock(address, length)); |
| 260 | code_range_->Uncommit(address, length); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | void CodeRange::TearDown() { |
| 265 | delete code_range_; // Frees all memory in the virtual memory range. |
| 266 | code_range_ = NULL; |
| 267 | free_list_.Free(); |
| 268 | allocation_list_.Free(); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | // ----------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | // MemoryAllocator |
| 274 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 276 | MemoryAllocator::MemoryAllocator(Isolate* isolate) |
| 277 | : isolate_(isolate), |
| 278 | capacity_(0), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 279 | capacity_executable_(0), |
| 280 | size_(0), |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 281 | size_executable_(0), |
| 282 | lowest_ever_allocated_(reinterpret_cast<void*>(-1)), |
| 283 | highest_ever_allocated_(reinterpret_cast<void*>(0)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 287 | bool MemoryAllocator::SetUp(intptr_t capacity, intptr_t capacity_executable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 288 | capacity_ = RoundUp(capacity, Page::kPageSize); |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 289 | capacity_executable_ = RoundUp(capacity_executable, Page::kPageSize); |
| 290 | ASSERT_GE(capacity_, capacity_executable_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 291 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 292 | size_ = 0; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 293 | size_executable_ = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 294 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | void MemoryAllocator::TearDown() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 300 | // Check that spaces were torn down before MemoryAllocator. |
| 301 | ASSERT(size_ == 0); |
| 302 | // TODO(gc) this will be true again when we fix FreeMemory. |
| 303 | // ASSERT(size_executable_ == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | capacity_ = 0; |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 305 | capacity_executable_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 309 | bool MemoryAllocator::CommitMemory(Address base, |
| 310 | size_t size, |
| 311 | Executability executable) { |
| 312 | if (!VirtualMemory::CommitRegion(base, size, executable == EXECUTABLE)) { |
| 313 | return false; |
| 314 | } |
| 315 | UpdateAllocatedSpaceLimits(base, base + size); |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 320 | void MemoryAllocator::FreeMemory(VirtualMemory* reservation, |
| 321 | Executability executable) { |
| 322 | // TODO(gc) make code_range part of memory allocator? |
| 323 | ASSERT(reservation->IsReserved()); |
| 324 | size_t size = reservation->size(); |
| 325 | ASSERT(size_ >= size); |
| 326 | size_ -= size; |
| 327 | |
| 328 | isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size)); |
| 329 | |
| 330 | if (executable == EXECUTABLE) { |
| 331 | ASSERT(size_executable_ >= size); |
| 332 | size_executable_ -= size; |
| 333 | } |
| 334 | // Code which is part of the code-range does not have its own VirtualMemory. |
| 335 | ASSERT(!isolate_->code_range()->contains( |
| 336 | static_cast<Address>(reservation->address()))); |
| 337 | ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists()); |
| 338 | reservation->Release(); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | void MemoryAllocator::FreeMemory(Address base, |
| 343 | size_t size, |
| 344 | Executability executable) { |
| 345 | // TODO(gc) make code_range part of memory allocator? |
| 346 | ASSERT(size_ >= size); |
| 347 | size_ -= size; |
| 348 | |
| 349 | isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size)); |
| 350 | |
| 351 | if (executable == EXECUTABLE) { |
| 352 | ASSERT(size_executable_ >= size); |
| 353 | size_executable_ -= size; |
| 354 | } |
| 355 | if (isolate_->code_range()->contains(static_cast<Address>(base))) { |
| 356 | ASSERT(executable == EXECUTABLE); |
| 357 | isolate_->code_range()->FreeRawMemory(base, size); |
| 358 | } else { |
| 359 | ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists()); |
| 360 | bool result = VirtualMemory::ReleaseRegion(base, size); |
| 361 | USE(result); |
| 362 | ASSERT(result); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | |
| 367 | Address MemoryAllocator::ReserveAlignedMemory(size_t size, |
| 368 | size_t alignment, |
| 369 | VirtualMemory* controller) { |
| 370 | VirtualMemory reservation(size, alignment); |
| 371 | |
| 372 | if (!reservation.IsReserved()) return NULL; |
| 373 | size_ += reservation.size(); |
| 374 | Address base = RoundUp(static_cast<Address>(reservation.address()), |
| 375 | alignment); |
| 376 | controller->TakeControl(&reservation); |
| 377 | return base; |
| 378 | } |
| 379 | |
| 380 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 381 | Address MemoryAllocator::AllocateAlignedMemory(size_t reserve_size, |
| 382 | size_t commit_size, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 383 | size_t alignment, |
| 384 | Executability executable, |
| 385 | VirtualMemory* controller) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 386 | ASSERT(commit_size <= reserve_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 387 | VirtualMemory reservation; |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 388 | Address base = ReserveAlignedMemory(reserve_size, alignment, &reservation); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 389 | if (base == NULL) return NULL; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 390 | |
| 391 | if (executable == EXECUTABLE) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 392 | if (!CommitExecutableMemory(&reservation, |
| 393 | base, |
| 394 | commit_size, |
| 395 | reserve_size)) { |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 396 | base = NULL; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 397 | } |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 398 | } else { |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 399 | if (reservation.Commit(base, commit_size, false)) { |
| 400 | UpdateAllocatedSpaceLimits(base, base + commit_size); |
| 401 | } else { |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 402 | base = NULL; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (base == NULL) { |
| 407 | // Failed to commit the body. Release the mapping and any partially |
| 408 | // commited regions inside it. |
| 409 | reservation.Release(); |
| 410 | return NULL; |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 411 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 412 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 413 | controller->TakeControl(&reservation); |
| 414 | return base; |
| 415 | } |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 416 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 417 | |
| 418 | void Page::InitializeAsAnchor(PagedSpace* owner) { |
| 419 | set_owner(owner); |
| 420 | set_prev_page(this); |
| 421 | set_next_page(this); |
| 422 | } |
| 423 | |
| 424 | |
| 425 | NewSpacePage* NewSpacePage::Initialize(Heap* heap, |
| 426 | Address start, |
| 427 | SemiSpace* semi_space) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 428 | Address area_start = start + NewSpacePage::kObjectStartOffset; |
| 429 | Address area_end = start + Page::kPageSize; |
| 430 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 431 | MemoryChunk* chunk = MemoryChunk::Initialize(heap, |
| 432 | start, |
| 433 | Page::kPageSize, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 434 | area_start, |
| 435 | area_end, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 436 | NOT_EXECUTABLE, |
| 437 | semi_space); |
| 438 | chunk->set_next_chunk(NULL); |
| 439 | chunk->set_prev_chunk(NULL); |
| 440 | chunk->initialize_scan_on_scavenge(true); |
| 441 | bool in_to_space = (semi_space->id() != kFromSpace); |
| 442 | chunk->SetFlag(in_to_space ? MemoryChunk::IN_TO_SPACE |
| 443 | : MemoryChunk::IN_FROM_SPACE); |
| 444 | ASSERT(!chunk->IsFlagSet(in_to_space ? MemoryChunk::IN_FROM_SPACE |
| 445 | : MemoryChunk::IN_TO_SPACE)); |
| 446 | NewSpacePage* page = static_cast<NewSpacePage*>(chunk); |
| 447 | heap->incremental_marking()->SetNewSpacePageFlags(page); |
| 448 | return page; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | void NewSpacePage::InitializeAsAnchor(SemiSpace* semi_space) { |
| 453 | set_owner(semi_space); |
| 454 | set_next_chunk(this); |
| 455 | set_prev_chunk(this); |
| 456 | // Flags marks this invalid page as not being in new-space. |
| 457 | // All real new-space pages will be in new-space. |
| 458 | SetFlags(0, ~0); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | MemoryChunk* MemoryChunk::Initialize(Heap* heap, |
| 463 | Address base, |
| 464 | size_t size, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 465 | Address area_start, |
| 466 | Address area_end, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 467 | Executability executable, |
| 468 | Space* owner) { |
| 469 | MemoryChunk* chunk = FromAddress(base); |
| 470 | |
| 471 | ASSERT(base == chunk->address()); |
| 472 | |
| 473 | chunk->heap_ = heap; |
| 474 | chunk->size_ = size; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 475 | chunk->area_start_ = area_start; |
| 476 | chunk->area_end_ = area_end; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 477 | chunk->flags_ = 0; |
| 478 | chunk->set_owner(owner); |
| 479 | chunk->InitializeReservedMemory(); |
| 480 | chunk->slots_buffer_ = NULL; |
| 481 | chunk->skip_list_ = NULL; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 482 | chunk->write_barrier_counter_ = kWriteBarrierCounterGranularity; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 483 | chunk->progress_bar_ = 0; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 484 | chunk->high_water_mark_ = static_cast<int>(area_start - base); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 485 | chunk->parallel_sweeping_ = 0; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 486 | chunk->available_in_small_free_list_ = 0; |
| 487 | chunk->available_in_medium_free_list_ = 0; |
| 488 | chunk->available_in_large_free_list_ = 0; |
| 489 | chunk->available_in_huge_free_list_ = 0; |
| 490 | chunk->non_available_small_blocks_ = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 491 | chunk->ResetLiveBytes(); |
| 492 | Bitmap::Clear(chunk); |
| 493 | chunk->initialize_scan_on_scavenge(false); |
| 494 | chunk->SetFlag(WAS_SWEPT_PRECISELY); |
| 495 | |
| 496 | ASSERT(OFFSET_OF(MemoryChunk, flags_) == kFlagsOffset); |
| 497 | ASSERT(OFFSET_OF(MemoryChunk, live_byte_count_) == kLiveBytesOffset); |
| 498 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 499 | if (executable == EXECUTABLE) { |
| 500 | chunk->SetFlag(IS_EXECUTABLE); |
| 501 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 502 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 503 | if (owner == heap->old_data_space()) { |
| 504 | chunk->SetFlag(CONTAINS_ONLY_DATA); |
| 505 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 506 | |
| 507 | return chunk; |
| 508 | } |
| 509 | |
| 510 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 511 | // Commit MemoryChunk area to the requested size. |
| 512 | bool MemoryChunk::CommitArea(size_t requested) { |
| 513 | size_t guard_size = IsFlagSet(IS_EXECUTABLE) ? |
| 514 | MemoryAllocator::CodePageGuardSize() : 0; |
| 515 | size_t header_size = area_start() - address() - guard_size; |
| 516 | size_t commit_size = RoundUp(header_size + requested, OS::CommitPageSize()); |
| 517 | size_t committed_size = RoundUp(header_size + (area_end() - area_start()), |
| 518 | OS::CommitPageSize()); |
| 519 | |
| 520 | if (commit_size > committed_size) { |
| 521 | // Commit size should be less or equal than the reserved size. |
| 522 | ASSERT(commit_size <= size() - 2 * guard_size); |
| 523 | // Append the committed area. |
| 524 | Address start = address() + committed_size + guard_size; |
| 525 | size_t length = commit_size - committed_size; |
| 526 | if (reservation_.IsReserved()) { |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 527 | Executability executable = IsFlagSet(IS_EXECUTABLE) |
| 528 | ? EXECUTABLE : NOT_EXECUTABLE; |
| 529 | if (!heap()->isolate()->memory_allocator()->CommitMemory( |
| 530 | start, length, executable)) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 531 | return false; |
| 532 | } |
| 533 | } else { |
| 534 | CodeRange* code_range = heap_->isolate()->code_range(); |
| 535 | ASSERT(code_range->exists() && IsFlagSet(IS_EXECUTABLE)); |
| 536 | if (!code_range->CommitRawMemory(start, length)) return false; |
| 537 | } |
| 538 | |
| 539 | if (Heap::ShouldZapGarbage()) { |
| 540 | heap_->isolate()->memory_allocator()->ZapBlock(start, length); |
| 541 | } |
| 542 | } else if (commit_size < committed_size) { |
| 543 | ASSERT(commit_size > 0); |
| 544 | // Shrink the committed area. |
| 545 | size_t length = committed_size - commit_size; |
| 546 | Address start = address() + committed_size + guard_size - length; |
| 547 | if (reservation_.IsReserved()) { |
| 548 | if (!reservation_.Uncommit(start, length)) return false; |
| 549 | } else { |
| 550 | CodeRange* code_range = heap_->isolate()->code_range(); |
| 551 | ASSERT(code_range->exists() && IsFlagSet(IS_EXECUTABLE)); |
| 552 | if (!code_range->UncommitRawMemory(start, length)) return false; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | area_end_ = area_start_ + requested; |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 561 | void MemoryChunk::InsertAfter(MemoryChunk* other) { |
| 562 | next_chunk_ = other->next_chunk_; |
| 563 | prev_chunk_ = other; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 564 | |
| 565 | // This memory barrier is needed since concurrent sweeper threads may iterate |
| 566 | // over the list of pages while a new page is inserted. |
| 567 | // TODO(hpayer): find a cleaner way to guarantee that the page list can be |
| 568 | // expanded concurrently |
| 569 | MemoryBarrier(); |
| 570 | |
| 571 | // The following two write operations can take effect in arbitrary order |
| 572 | // since pages are always iterated by the sweeper threads in LIFO order, i.e, |
| 573 | // the inserted page becomes visible for the sweeper threads after |
| 574 | // other->next_chunk_ = this; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 575 | other->next_chunk_->prev_chunk_ = this; |
| 576 | other->next_chunk_ = this; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | void MemoryChunk::Unlink() { |
| 581 | if (!InNewSpace() && IsFlagSet(SCAN_ON_SCAVENGE)) { |
| 582 | heap_->decrement_scan_on_scavenge_pages(); |
| 583 | ClearFlag(SCAN_ON_SCAVENGE); |
| 584 | } |
| 585 | next_chunk_->prev_chunk_ = prev_chunk_; |
| 586 | prev_chunk_->next_chunk_ = next_chunk_; |
| 587 | prev_chunk_ = NULL; |
| 588 | next_chunk_ = NULL; |
| 589 | } |
| 590 | |
| 591 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 592 | MemoryChunk* MemoryAllocator::AllocateChunk(intptr_t reserve_area_size, |
| 593 | intptr_t commit_area_size, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 594 | Executability executable, |
| 595 | Space* owner) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 596 | ASSERT(commit_area_size <= reserve_area_size); |
| 597 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 598 | size_t chunk_size; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 599 | Heap* heap = isolate_->heap(); |
| 600 | Address base = NULL; |
| 601 | VirtualMemory reservation; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 602 | Address area_start = NULL; |
| 603 | Address area_end = NULL; |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 604 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 605 | // |
| 606 | // MemoryChunk layout: |
| 607 | // |
| 608 | // Executable |
| 609 | // +----------------------------+<- base aligned with MemoryChunk::kAlignment |
| 610 | // | Header | |
| 611 | // +----------------------------+<- base + CodePageGuardStartOffset |
| 612 | // | Guard | |
| 613 | // +----------------------------+<- area_start_ |
| 614 | // | Area | |
| 615 | // +----------------------------+<- area_end_ (area_start + commit_area_size) |
| 616 | // | Committed but not used | |
| 617 | // +----------------------------+<- aligned at OS page boundary |
| 618 | // | Reserved but not committed | |
| 619 | // +----------------------------+<- aligned at OS page boundary |
| 620 | // | Guard | |
| 621 | // +----------------------------+<- base + chunk_size |
| 622 | // |
| 623 | // Non-executable |
| 624 | // +----------------------------+<- base aligned with MemoryChunk::kAlignment |
| 625 | // | Header | |
| 626 | // +----------------------------+<- area_start_ (base + kObjectStartOffset) |
| 627 | // | Area | |
| 628 | // +----------------------------+<- area_end_ (area_start + commit_area_size) |
| 629 | // | Committed but not used | |
| 630 | // +----------------------------+<- aligned at OS page boundary |
| 631 | // | Reserved but not committed | |
| 632 | // +----------------------------+<- base + chunk_size |
| 633 | // |
| 634 | |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 635 | if (executable == EXECUTABLE) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 636 | chunk_size = RoundUp(CodePageAreaStartOffset() + reserve_area_size, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 637 | OS::CommitPageSize()) + CodePageGuardSize(); |
| 638 | |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 639 | // Check executable memory limit. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 640 | if (size_executable_ + chunk_size > capacity_executable_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 641 | LOG(isolate_, |
| 642 | StringEvent("MemoryAllocator::AllocateRawMemory", |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 643 | "V8 Executable Allocation capacity exceeded")); |
| 644 | return NULL; |
| 645 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 646 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 647 | // Size of header (not executable) plus area (executable). |
| 648 | size_t commit_size = RoundUp(CodePageGuardStartOffset() + commit_area_size, |
| 649 | OS::CommitPageSize()); |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 650 | // Allocate executable memory either from code range or from the |
| 651 | // OS. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 652 | if (isolate_->code_range()->exists()) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 653 | base = isolate_->code_range()->AllocateRawMemory(chunk_size, |
| 654 | commit_size, |
| 655 | &chunk_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 656 | ASSERT(IsAligned(reinterpret_cast<intptr_t>(base), |
| 657 | MemoryChunk::kAlignment)); |
| 658 | if (base == NULL) return NULL; |
| 659 | size_ += chunk_size; |
| 660 | // Update executable memory size. |
| 661 | size_executable_ += chunk_size; |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 662 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 663 | base = AllocateAlignedMemory(chunk_size, |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 664 | commit_size, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 665 | MemoryChunk::kAlignment, |
| 666 | executable, |
| 667 | &reservation); |
| 668 | if (base == NULL) return NULL; |
| 669 | // Update executable memory size. |
| 670 | size_executable_ += reservation.size(); |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 671 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 672 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 673 | if (Heap::ShouldZapGarbage()) { |
| 674 | ZapBlock(base, CodePageGuardStartOffset()); |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 675 | ZapBlock(base + CodePageAreaStartOffset(), commit_area_size); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 676 | } |
| 677 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 678 | area_start = base + CodePageAreaStartOffset(); |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 679 | area_end = area_start + commit_area_size; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 680 | } else { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 681 | chunk_size = RoundUp(MemoryChunk::kObjectStartOffset + reserve_area_size, |
| 682 | OS::CommitPageSize()); |
| 683 | size_t commit_size = RoundUp(MemoryChunk::kObjectStartOffset + |
| 684 | commit_area_size, OS::CommitPageSize()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 685 | base = AllocateAlignedMemory(chunk_size, |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 686 | commit_size, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 687 | MemoryChunk::kAlignment, |
| 688 | executable, |
| 689 | &reservation); |
| 690 | |
| 691 | if (base == NULL) return NULL; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 692 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 693 | if (Heap::ShouldZapGarbage()) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 694 | ZapBlock(base, Page::kObjectStartOffset + commit_area_size); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 695 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 696 | |
| 697 | area_start = base + Page::kObjectStartOffset; |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 698 | area_end = area_start + commit_area_size; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 699 | } |
| 700 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 701 | // Use chunk_size for statistics and callbacks because we assume that they |
| 702 | // treat reserved but not-yet committed memory regions of chunks as allocated. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 703 | isolate_->counters()->memory_allocated()-> |
| 704 | Increment(static_cast<int>(chunk_size)); |
| 705 | |
| 706 | LOG(isolate_, NewEvent("MemoryChunk", base, chunk_size)); |
| 707 | if (owner != NULL) { |
| 708 | ObjectSpace space = static_cast<ObjectSpace>(1 << owner->identity()); |
| 709 | PerformAllocationCallback(space, kAllocationActionAllocate, chunk_size); |
| 710 | } |
| 711 | |
| 712 | MemoryChunk* result = MemoryChunk::Initialize(heap, |
| 713 | base, |
| 714 | chunk_size, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 715 | area_start, |
| 716 | area_end, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 717 | executable, |
| 718 | owner); |
| 719 | result->set_reserved_memory(&reservation); |
| 720 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 724 | void Page::ResetFreeListStatistics() { |
| 725 | non_available_small_blocks_ = 0; |
| 726 | available_in_small_free_list_ = 0; |
| 727 | available_in_medium_free_list_ = 0; |
| 728 | available_in_large_free_list_ = 0; |
| 729 | available_in_huge_free_list_ = 0; |
| 730 | } |
| 731 | |
| 732 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 733 | Page* MemoryAllocator::AllocatePage(intptr_t size, |
| 734 | PagedSpace* owner, |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 735 | Executability executable) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 736 | MemoryChunk* chunk = AllocateChunk(size, size, executable, owner); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 737 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 738 | if (chunk == NULL) return NULL; |
| 739 | |
| 740 | return Page::Initialize(isolate_->heap(), chunk, executable, owner); |
| 741 | } |
| 742 | |
| 743 | |
| 744 | LargePage* MemoryAllocator::AllocateLargePage(intptr_t object_size, |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 745 | Space* owner, |
| 746 | Executability executable) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 747 | MemoryChunk* chunk = AllocateChunk(object_size, |
| 748 | object_size, |
| 749 | executable, |
| 750 | owner); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 751 | if (chunk == NULL) return NULL; |
| 752 | return LargePage::Initialize(isolate_->heap(), chunk); |
| 753 | } |
| 754 | |
| 755 | |
| 756 | void MemoryAllocator::Free(MemoryChunk* chunk) { |
| 757 | LOG(isolate_, DeleteEvent("MemoryChunk", chunk)); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 758 | if (chunk->owner() != NULL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 759 | ObjectSpace space = |
| 760 | static_cast<ObjectSpace>(1 << chunk->owner()->identity()); |
| 761 | PerformAllocationCallback(space, kAllocationActionFree, chunk->size()); |
| 762 | } |
| 763 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 764 | isolate_->heap()->RememberUnmappedPage( |
| 765 | reinterpret_cast<Address>(chunk), chunk->IsEvacuationCandidate()); |
| 766 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 767 | delete chunk->slots_buffer(); |
| 768 | delete chunk->skip_list(); |
| 769 | |
| 770 | VirtualMemory* reservation = chunk->reserved_memory(); |
| 771 | if (reservation->IsReserved()) { |
| 772 | FreeMemory(reservation, chunk->executable()); |
| 773 | } else { |
| 774 | FreeMemory(chunk->address(), |
| 775 | chunk->size(), |
| 776 | chunk->executable()); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | |
| 781 | bool MemoryAllocator::CommitBlock(Address start, |
| 782 | size_t size, |
| 783 | Executability executable) { |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 784 | if (!CommitMemory(start, size, executable)) return false; |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 785 | |
| 786 | if (Heap::ShouldZapGarbage()) { |
| 787 | ZapBlock(start, size); |
| 788 | } |
| 789 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 790 | isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size)); |
| 791 | return true; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | bool MemoryAllocator::UncommitBlock(Address start, size_t size) { |
| 796 | if (!VirtualMemory::UncommitRegion(start, size)) return false; |
| 797 | isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size)); |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | void MemoryAllocator::ZapBlock(Address start, size_t size) { |
| 803 | for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) { |
| 804 | Memory::Address_at(start + s) = kZapValue; |
| 805 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 809 | void MemoryAllocator::PerformAllocationCallback(ObjectSpace space, |
| 810 | AllocationAction action, |
| 811 | size_t size) { |
| 812 | for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) { |
| 813 | MemoryAllocationCallbackRegistration registration = |
| 814 | memory_allocation_callbacks_[i]; |
| 815 | if ((registration.space & space) == space && |
| 816 | (registration.action & action) == action) |
| 817 | registration.callback(space, action, static_cast<int>(size)); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
| 822 | bool MemoryAllocator::MemoryAllocationCallbackRegistered( |
| 823 | MemoryAllocationCallback callback) { |
| 824 | for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) { |
| 825 | if (memory_allocation_callbacks_[i].callback == callback) return true; |
| 826 | } |
| 827 | return false; |
| 828 | } |
| 829 | |
| 830 | |
| 831 | void MemoryAllocator::AddMemoryAllocationCallback( |
| 832 | MemoryAllocationCallback callback, |
| 833 | ObjectSpace space, |
| 834 | AllocationAction action) { |
| 835 | ASSERT(callback != NULL); |
| 836 | MemoryAllocationCallbackRegistration registration(callback, space, action); |
| 837 | ASSERT(!MemoryAllocator::MemoryAllocationCallbackRegistered(callback)); |
| 838 | return memory_allocation_callbacks_.Add(registration); |
| 839 | } |
| 840 | |
| 841 | |
| 842 | void MemoryAllocator::RemoveMemoryAllocationCallback( |
| 843 | MemoryAllocationCallback callback) { |
| 844 | ASSERT(callback != NULL); |
| 845 | for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) { |
| 846 | if (memory_allocation_callbacks_[i].callback == callback) { |
| 847 | memory_allocation_callbacks_.Remove(i); |
| 848 | return; |
| 849 | } |
| 850 | } |
| 851 | UNREACHABLE(); |
| 852 | } |
| 853 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 854 | |
| 855 | #ifdef DEBUG |
| 856 | void MemoryAllocator::ReportStatistics() { |
| 857 | float pct = static_cast<float>(capacity_ - size_) / capacity_; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 858 | PrintF(" capacity: %" V8_PTR_PREFIX "d" |
| 859 | ", used: %" V8_PTR_PREFIX "d" |
| 860 | ", available: %%%d\n\n", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 861 | capacity_, size_, static_cast<int>(pct*100)); |
| 862 | } |
| 863 | #endif |
| 864 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 865 | |
| 866 | int MemoryAllocator::CodePageGuardStartOffset() { |
| 867 | // We are guarding code pages: the first OS page after the header |
| 868 | // will be protected as non-writable. |
| 869 | return RoundUp(Page::kObjectStartOffset, OS::CommitPageSize()); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | int MemoryAllocator::CodePageGuardSize() { |
| 874 | return static_cast<int>(OS::CommitPageSize()); |
| 875 | } |
| 876 | |
| 877 | |
| 878 | int MemoryAllocator::CodePageAreaStartOffset() { |
| 879 | // We are guarding code pages: the first OS page after the header |
| 880 | // will be protected as non-writable. |
| 881 | return CodePageGuardStartOffset() + CodePageGuardSize(); |
| 882 | } |
| 883 | |
| 884 | |
| 885 | int MemoryAllocator::CodePageAreaEndOffset() { |
| 886 | // We are guarding code pages: the last OS page will be protected as |
| 887 | // non-writable. |
| 888 | return Page::kPageSize - static_cast<int>(OS::CommitPageSize()); |
| 889 | } |
| 890 | |
| 891 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 892 | bool MemoryAllocator::CommitExecutableMemory(VirtualMemory* vm, |
| 893 | Address start, |
| 894 | size_t commit_size, |
| 895 | size_t reserved_size) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 896 | // Commit page header (not executable). |
| 897 | if (!vm->Commit(start, |
| 898 | CodePageGuardStartOffset(), |
| 899 | false)) { |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | // Create guard page after the header. |
| 904 | if (!vm->Guard(start + CodePageGuardStartOffset())) { |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | // Commit page body (executable). |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 909 | if (!vm->Commit(start + CodePageAreaStartOffset(), |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 910 | commit_size - CodePageGuardStartOffset(), |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 911 | true)) { |
| 912 | return false; |
| 913 | } |
| 914 | |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 915 | // Create guard page before the end. |
| 916 | if (!vm->Guard(start + reserved_size - CodePageGuardSize())) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 917 | return false; |
| 918 | } |
| 919 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 920 | UpdateAllocatedSpaceLimits(start, |
| 921 | start + CodePageAreaStartOffset() + |
| 922 | commit_size - CodePageGuardStartOffset()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 923 | return true; |
| 924 | } |
| 925 | |
| 926 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 927 | // ----------------------------------------------------------------------------- |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 928 | // MemoryChunk implementation |
| 929 | |
| 930 | void MemoryChunk::IncrementLiveBytesFromMutator(Address address, int by) { |
| 931 | MemoryChunk* chunk = MemoryChunk::FromAddress(address); |
| 932 | if (!chunk->InNewSpace() && !static_cast<Page*>(chunk)->WasSwept()) { |
| 933 | static_cast<PagedSpace*>(chunk->owner())->IncrementUnsweptFreeBytes(-by); |
| 934 | } |
| 935 | chunk->IncrementLiveBytes(by); |
| 936 | } |
| 937 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 938 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 939 | // ----------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 940 | // PagedSpace implementation |
| 941 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 942 | PagedSpace::PagedSpace(Heap* heap, |
| 943 | intptr_t max_capacity, |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 944 | AllocationSpace id, |
| 945 | Executability executable) |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 946 | : Space(heap, id, executable), |
| 947 | free_list_(this), |
| 948 | was_swept_conservatively_(false), |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 949 | first_unswept_page_(Page::FromAddress(NULL)), |
| 950 | unswept_free_bytes_(0) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 951 | if (id == CODE_SPACE) { |
| 952 | area_size_ = heap->isolate()->memory_allocator()-> |
| 953 | CodePageAreaSize(); |
| 954 | } else { |
| 955 | area_size_ = Page::kPageSize - Page::kObjectStartOffset; |
| 956 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 957 | max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize) |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 958 | * AreaSize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 959 | accounting_stats_.Clear(); |
| 960 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 961 | allocation_info_.top = NULL; |
| 962 | allocation_info_.limit = NULL; |
| 963 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 964 | anchor_.InitializeAsAnchor(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 968 | bool PagedSpace::SetUp() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 969 | return true; |
| 970 | } |
| 971 | |
| 972 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 973 | bool PagedSpace::HasBeenSetUp() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 974 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | |
| 978 | void PagedSpace::TearDown() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 979 | PageIterator iterator(this); |
| 980 | while (iterator.has_next()) { |
| 981 | heap()->isolate()->memory_allocator()->Free(iterator.next()); |
| 982 | } |
| 983 | anchor_.set_next_page(&anchor_); |
| 984 | anchor_.set_prev_page(&anchor_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | accounting_stats_.Clear(); |
| 986 | } |
| 987 | |
| 988 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 989 | size_t PagedSpace::CommittedPhysicalMemory() { |
| 990 | if (!VirtualMemory::HasLazyCommits()) return CommittedMemory(); |
| 991 | MemoryChunk::UpdateHighWaterMark(allocation_info_.top); |
| 992 | size_t size = 0; |
| 993 | PageIterator it(this); |
| 994 | while (it.has_next()) { |
| 995 | size += it.next()->CommittedPhysicalMemory(); |
| 996 | } |
| 997 | return size; |
| 998 | } |
| 999 | |
| 1000 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1001 | MaybeObject* PagedSpace::FindObject(Address addr) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1002 | // Note: this function can only be called on precisely swept spaces. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1003 | ASSERT(!heap()->mark_compact_collector()->in_use()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (!Contains(addr)) return Failure::Exception(); |
| 1006 | |
| 1007 | Page* p = Page::FromAddress(addr); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1008 | HeapObjectIterator it(p, NULL); |
| 1009 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
| 1010 | Address cur = obj->address(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1011 | Address next = cur + obj->Size(); |
| 1012 | if ((cur <= addr) && (addr < next)) return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1015 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | return Failure::Exception(); |
| 1017 | } |
| 1018 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1019 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1020 | bool PagedSpace::CanExpand() { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1021 | ASSERT(max_capacity_ % AreaSize() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1022 | |
| 1023 | if (Capacity() == max_capacity_) return false; |
| 1024 | |
| 1025 | ASSERT(Capacity() < max_capacity_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1026 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1027 | // Are we going to exceed capacity for this space? |
| 1028 | if ((Capacity() + Page::kPageSize) > max_capacity_) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1029 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1030 | return true; |
| 1031 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1032 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 1033 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1034 | bool PagedSpace::Expand() { |
| 1035 | if (!CanExpand()) return false; |
| 1036 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1037 | intptr_t size = AreaSize(); |
| 1038 | |
| 1039 | if (anchor_.next_page() == &anchor_) { |
| 1040 | size = SizeOfFirstPage(); |
| 1041 | } |
| 1042 | |
| 1043 | Page* p = heap()->isolate()->memory_allocator()->AllocatePage( |
| 1044 | size, this, executable()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1045 | if (p == NULL) return false; |
| 1046 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1047 | ASSERT(Capacity() <= max_capacity_); |
| 1048 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1049 | p->InsertAfter(anchor_.prev_page()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1050 | |
| 1051 | return true; |
| 1052 | } |
| 1053 | |
| 1054 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1055 | intptr_t PagedSpace::SizeOfFirstPage() { |
| 1056 | int size = 0; |
| 1057 | switch (identity()) { |
| 1058 | case OLD_POINTER_SPACE: |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 1059 | size = 72 * kPointerSize * KB; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | case OLD_DATA_SPACE: |
| 1062 | size = 192 * KB; |
| 1063 | break; |
| 1064 | case MAP_SPACE: |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 1065 | size = 16 * kPointerSize * KB; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1066 | break; |
| 1067 | case CELL_SPACE: |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 1068 | size = 16 * kPointerSize * KB; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1069 | break; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1070 | case PROPERTY_CELL_SPACE: |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1071 | size = 8 * kPointerSize * KB; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1072 | break; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1073 | case CODE_SPACE: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1074 | if (heap()->isolate()->code_range()->exists()) { |
| 1075 | // When code range exists, code pages are allocated in a special way |
| 1076 | // (from the reserved code range). That part of the code is not yet |
| 1077 | // upgraded to handle small pages. |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1078 | size = AreaSize(); |
| 1079 | } else { |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 1080 | #if V8_TARGET_ARCH_MIPS |
| 1081 | // On MIPS, code stubs seem to be quite a bit larger. |
| 1082 | // TODO(olivf/MIPS folks): Can we do anything about this? Does it |
| 1083 | // indicate the presence of a bug? |
| 1084 | size = 464 * KB; |
| 1085 | #else |
| 1086 | size = 416 * KB; |
| 1087 | #endif |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1088 | } |
| 1089 | break; |
| 1090 | default: |
| 1091 | UNREACHABLE(); |
| 1092 | } |
| 1093 | return Min(size, AreaSize()); |
| 1094 | } |
| 1095 | |
| 1096 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1097 | int PagedSpace::CountTotalPages() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1098 | PageIterator it(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1099 | int count = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1100 | while (it.has_next()) { |
| 1101 | it.next(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1102 | count++; |
| 1103 | } |
| 1104 | return count; |
| 1105 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1106 | |
| 1107 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1108 | void PagedSpace::ObtainFreeListStatistics(Page* page, SizeStats* sizes) { |
| 1109 | sizes->huge_size_ = page->available_in_huge_free_list(); |
| 1110 | sizes->small_size_ = page->available_in_small_free_list(); |
| 1111 | sizes->medium_size_ = page->available_in_medium_free_list(); |
| 1112 | sizes->large_size_ = page->available_in_large_free_list(); |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | void PagedSpace::ResetFreeListStatistics() { |
| 1117 | PageIterator page_iterator(this); |
| 1118 | while (page_iterator.has_next()) { |
| 1119 | Page* page = page_iterator.next(); |
| 1120 | page->ResetFreeListStatistics(); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 1125 | void PagedSpace::ReleasePage(Page* page, bool unlink) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1126 | ASSERT(page->LiveBytes() == 0); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1127 | ASSERT(AreaSize() == page->area_size()); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 1128 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1129 | // Adjust list of unswept pages if the page is the head of the list. |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 1130 | if (first_unswept_page_ == page) { |
| 1131 | first_unswept_page_ = page->next_page(); |
| 1132 | if (first_unswept_page_ == anchor()) { |
| 1133 | first_unswept_page_ = Page::FromAddress(NULL); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (page->WasSwept()) { |
| 1138 | intptr_t size = free_list_.EvictFreeListItems(page); |
| 1139 | accounting_stats_.AllocateBytes(size); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1140 | ASSERT_EQ(AreaSize(), static_cast<int>(size)); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1141 | } else { |
| 1142 | DecreaseUnsweptFreeBytes(page); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1145 | if (Page::FromAllocationTop(allocation_info_.top) == page) { |
| 1146 | allocation_info_.top = allocation_info_.limit = NULL; |
| 1147 | } |
| 1148 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 1149 | if (unlink) { |
| 1150 | page->Unlink(); |
| 1151 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1152 | if (page->IsFlagSet(MemoryChunk::CONTAINS_ONLY_DATA)) { |
| 1153 | heap()->isolate()->memory_allocator()->Free(page); |
| 1154 | } else { |
| 1155 | heap()->QueueMemoryChunkForFree(page); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1158 | ASSERT(Capacity() > 0); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1159 | accounting_stats_.ShrinkSpace(AreaSize()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1163 | #ifdef DEBUG |
| 1164 | void PagedSpace::Print() { } |
| 1165 | #endif |
| 1166 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1167 | #ifdef VERIFY_HEAP |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1168 | void PagedSpace::Verify(ObjectVisitor* visitor) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1169 | // We can only iterate over the pages if they were swept precisely. |
| 1170 | if (was_swept_conservatively_) return; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1171 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1172 | bool allocation_pointer_found_in_space = |
| 1173 | (allocation_info_.top == allocation_info_.limit); |
| 1174 | PageIterator page_iterator(this); |
| 1175 | while (page_iterator.has_next()) { |
| 1176 | Page* page = page_iterator.next(); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1177 | CHECK(page->owner() == this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1178 | if (page == Page::FromAllocationTop(allocation_info_.top)) { |
| 1179 | allocation_pointer_found_in_space = true; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1180 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1181 | CHECK(page->WasSweptPrecisely()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1182 | HeapObjectIterator it(page, NULL); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1183 | Address end_of_previous_object = page->area_start(); |
| 1184 | Address top = page->area_end(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1185 | int black_size = 0; |
| 1186 | for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1187 | CHECK(end_of_previous_object <= object->address()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1188 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1189 | // The first word should be a map, and we expect all map pointers to |
| 1190 | // be in map space. |
| 1191 | Map* map = object->map(); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1192 | CHECK(map->IsMap()); |
| 1193 | CHECK(heap()->map_space()->Contains(map)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Perform space-specific object verification. |
| 1196 | VerifyObject(object); |
| 1197 | |
| 1198 | // The object itself should look OK. |
| 1199 | object->Verify(); |
| 1200 | |
| 1201 | // All the interior pointers should be contained in the heap. |
| 1202 | int size = object->Size(); |
| 1203 | object->IterateBody(map->instance_type(), size, visitor); |
| 1204 | if (Marking::IsBlack(Marking::MarkBitFrom(object))) { |
| 1205 | black_size += size; |
| 1206 | } |
| 1207 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1208 | CHECK(object->address() + size <= top); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1209 | end_of_previous_object = object->address() + size; |
| 1210 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1211 | CHECK_LE(black_size, page->LiveBytes()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1212 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1213 | CHECK(allocation_pointer_found_in_space); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1214 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1215 | #endif // VERIFY_HEAP |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1216 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1217 | // ----------------------------------------------------------------------------- |
| 1218 | // NewSpace implementation |
| 1219 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1220 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1221 | bool NewSpace::SetUp(int reserved_semispace_capacity, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1222 | int maximum_semispace_capacity) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1223 | // Set up new space based on the preallocated memory block defined by |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1224 | // start and size. The provided space is divided into two semi-spaces. |
| 1225 | // To support fast containment testing in the new space, the size of |
| 1226 | // this chunk must be a power of two and it must be aligned to its size. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1227 | int initial_semispace_capacity = heap()->InitialSemiSpaceSize(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1228 | |
| 1229 | size_t size = 2 * reserved_semispace_capacity; |
| 1230 | Address base = |
| 1231 | heap()->isolate()->memory_allocator()->ReserveAlignedMemory( |
| 1232 | size, size, &reservation_); |
| 1233 | if (base == NULL) return false; |
| 1234 | |
| 1235 | chunk_base_ = base; |
| 1236 | chunk_size_ = static_cast<uintptr_t>(size); |
| 1237 | LOG(heap()->isolate(), NewEvent("InitialChunk", chunk_base_, chunk_size_)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1238 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1239 | ASSERT(initial_semispace_capacity <= maximum_semispace_capacity); |
| 1240 | ASSERT(IsPowerOf2(maximum_semispace_capacity)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1241 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1242 | // Allocate and set up the histogram arrays if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1243 | allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1); |
| 1244 | promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1); |
| 1245 | |
| 1246 | #define SET_NAME(name) allocated_histogram_[name].set_name(#name); \ |
| 1247 | promoted_histogram_[name].set_name(#name); |
| 1248 | INSTANCE_TYPE_LIST(SET_NAME) |
| 1249 | #undef SET_NAME |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1250 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1251 | ASSERT(reserved_semispace_capacity == heap()->ReservedSemiSpaceSize()); |
| 1252 | ASSERT(static_cast<intptr_t>(chunk_size_) >= |
| 1253 | 2 * heap()->ReservedSemiSpaceSize()); |
| 1254 | ASSERT(IsAddressAligned(chunk_base_, 2 * reserved_semispace_capacity, 0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1255 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1256 | to_space_.SetUp(chunk_base_, |
| 1257 | initial_semispace_capacity, |
| 1258 | maximum_semispace_capacity); |
| 1259 | from_space_.SetUp(chunk_base_ + reserved_semispace_capacity, |
| 1260 | initial_semispace_capacity, |
| 1261 | maximum_semispace_capacity); |
| 1262 | if (!to_space_.Commit()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1263 | return false; |
| 1264 | } |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1265 | ASSERT(!from_space_.is_committed()); // No need to use memory yet. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1266 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1267 | start_ = chunk_base_; |
| 1268 | address_mask_ = ~(2 * reserved_semispace_capacity - 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1269 | object_mask_ = address_mask_ | kHeapObjectTagMask; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1270 | object_expected_ = reinterpret_cast<uintptr_t>(start_) | kHeapObjectTag; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1271 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1272 | ResetAllocationInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1273 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1274 | return true; |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | void NewSpace::TearDown() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1279 | if (allocated_histogram_) { |
| 1280 | DeleteArray(allocated_histogram_); |
| 1281 | allocated_histogram_ = NULL; |
| 1282 | } |
| 1283 | if (promoted_histogram_) { |
| 1284 | DeleteArray(promoted_histogram_); |
| 1285 | promoted_histogram_ = NULL; |
| 1286 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | |
| 1288 | start_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1289 | allocation_info_.top = NULL; |
| 1290 | allocation_info_.limit = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1291 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1292 | to_space_.TearDown(); |
| 1293 | from_space_.TearDown(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1294 | |
| 1295 | LOG(heap()->isolate(), DeleteEvent("InitialChunk", chunk_base_)); |
| 1296 | |
| 1297 | ASSERT(reservation_.IsReserved()); |
| 1298 | heap()->isolate()->memory_allocator()->FreeMemory(&reservation_, |
| 1299 | NOT_EXECUTABLE); |
| 1300 | chunk_base_ = NULL; |
| 1301 | chunk_size_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | void NewSpace::Flip() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1306 | SemiSpace::Swap(&from_space_, &to_space_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1310 | void NewSpace::Grow() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1311 | // Double the semispace size but only up to maximum capacity. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1312 | ASSERT(Capacity() < MaximumCapacity()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1313 | int new_capacity = Min(MaximumCapacity(), 2 * static_cast<int>(Capacity())); |
| 1314 | if (to_space_.GrowTo(new_capacity)) { |
| 1315 | // Only grow from space if we managed to grow to-space. |
| 1316 | if (!from_space_.GrowTo(new_capacity)) { |
| 1317 | // If we managed to grow to-space but couldn't grow from-space, |
| 1318 | // attempt to shrink to-space. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1319 | if (!to_space_.ShrinkTo(from_space_.Capacity())) { |
| 1320 | // We are in an inconsistent state because we could not |
| 1321 | // commit/uncommit memory from new space. |
| 1322 | V8::FatalProcessOutOfMemory("Failed to grow new space."); |
| 1323 | } |
| 1324 | } |
| 1325 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1326 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | void NewSpace::Shrink() { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1331 | int new_capacity = Max(InitialCapacity(), 2 * SizeAsInt()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1332 | int rounded_new_capacity = RoundUp(new_capacity, Page::kPageSize); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1333 | if (rounded_new_capacity < Capacity() && |
| 1334 | to_space_.ShrinkTo(rounded_new_capacity)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1335 | // Only shrink from-space if we managed to shrink to-space. |
| 1336 | from_space_.Reset(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1337 | if (!from_space_.ShrinkTo(rounded_new_capacity)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1338 | // If we managed to shrink to-space but couldn't shrink from |
| 1339 | // space, attempt to grow to-space again. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1340 | if (!to_space_.GrowTo(from_space_.Capacity())) { |
| 1341 | // We are in an inconsistent state because we could not |
| 1342 | // commit/uncommit memory from new space. |
| 1343 | V8::FatalProcessOutOfMemory("Failed to shrink new space."); |
| 1344 | } |
| 1345 | } |
| 1346 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1347 | allocation_info_.limit = to_space_.page_high(); |
| 1348 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | void NewSpace::UpdateAllocationInfo() { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 1353 | MemoryChunk::UpdateHighWaterMark(allocation_info_.top); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1354 | allocation_info_.top = to_space_.page_low(); |
| 1355 | allocation_info_.limit = to_space_.page_high(); |
| 1356 | |
| 1357 | // Lower limit during incremental marking. |
| 1358 | if (heap()->incremental_marking()->IsMarking() && |
| 1359 | inline_allocation_limit_step() != 0) { |
| 1360 | Address new_limit = |
| 1361 | allocation_info_.top + inline_allocation_limit_step(); |
| 1362 | allocation_info_.limit = Min(new_limit, allocation_info_.limit); |
| 1363 | } |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1364 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | void NewSpace::ResetAllocationInfo() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1369 | to_space_.Reset(); |
| 1370 | UpdateAllocationInfo(); |
| 1371 | pages_used_ = 0; |
| 1372 | // Clear all mark-bits in the to-space. |
| 1373 | NewSpacePageIterator it(&to_space_); |
| 1374 | while (it.has_next()) { |
| 1375 | Bitmap::Clear(it.next()); |
| 1376 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1380 | bool NewSpace::AddFreshPage() { |
| 1381 | Address top = allocation_info_.top; |
| 1382 | if (NewSpacePage::IsAtStart(top)) { |
| 1383 | // The current page is already empty. Don't try to make another. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1384 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1385 | // We should only get here if someone asks to allocate more |
| 1386 | // than what can be stored in a single page. |
| 1387 | // TODO(gc): Change the limit on new-space allocation to prevent this |
| 1388 | // from happening (all such allocations should go directly to LOSpace). |
| 1389 | return false; |
| 1390 | } |
| 1391 | if (!to_space_.AdvancePage()) { |
| 1392 | // Failed to get a new page in to-space. |
| 1393 | return false; |
| 1394 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1395 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1396 | // Clear remainder of current page. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1397 | Address limit = NewSpacePage::FromLimit(top)->area_end(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1398 | if (heap()->gc_state() == Heap::SCAVENGE) { |
| 1399 | heap()->promotion_queue()->SetNewLimit(limit); |
| 1400 | heap()->promotion_queue()->ActivateGuardIfOnTheSamePage(); |
| 1401 | } |
| 1402 | |
| 1403 | int remaining_in_page = static_cast<int>(limit - top); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1404 | heap()->CreateFillerObjectAt(top, remaining_in_page); |
| 1405 | pages_used_++; |
| 1406 | UpdateAllocationInfo(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1407 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1408 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1412 | MaybeObject* NewSpace::SlowAllocateRaw(int size_in_bytes) { |
| 1413 | Address old_top = allocation_info_.top; |
| 1414 | Address new_top = old_top + size_in_bytes; |
| 1415 | Address high = to_space_.page_high(); |
| 1416 | if (allocation_info_.limit < high) { |
| 1417 | // Incremental marking has lowered the limit to get a |
| 1418 | // chance to do a step. |
| 1419 | allocation_info_.limit = Min( |
| 1420 | allocation_info_.limit + inline_allocation_limit_step_, |
| 1421 | high); |
| 1422 | int bytes_allocated = static_cast<int>(new_top - top_on_previous_step_); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1423 | heap()->incremental_marking()->Step( |
| 1424 | bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1425 | top_on_previous_step_ = new_top; |
| 1426 | return AllocateRaw(size_in_bytes); |
| 1427 | } else if (AddFreshPage()) { |
| 1428 | // Switched to new page. Try allocating again. |
| 1429 | int bytes_allocated = static_cast<int>(old_top - top_on_previous_step_); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1430 | heap()->incremental_marking()->Step( |
| 1431 | bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1432 | top_on_previous_step_ = to_space_.page_low(); |
| 1433 | return AllocateRaw(size_in_bytes); |
| 1434 | } else { |
| 1435 | return Failure::RetryAfterGC(); |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1440 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1441 | // We do not use the SemiSpaceIterator because verification doesn't assume |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1442 | // that it works (it depends on the invariants we are checking). |
| 1443 | void NewSpace::Verify() { |
| 1444 | // The allocation pointer should be in the space or at the very end. |
| 1445 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1446 | |
| 1447 | // There should be objects packed in from the low address up to the |
| 1448 | // allocation pointer. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1449 | Address current = to_space_.first_page()->area_start(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1450 | CHECK_EQ(current, to_space_.space_start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1451 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1452 | while (current != top()) { |
| 1453 | if (!NewSpacePage::IsAtEnd(current)) { |
| 1454 | // The allocation pointer should not be in the middle of an object. |
| 1455 | CHECK(!NewSpacePage::FromLimit(current)->ContainsLimit(top()) || |
| 1456 | current < top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1458 | HeapObject* object = HeapObject::FromAddress(current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1459 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1460 | // The first word should be a map, and we expect all map pointers to |
| 1461 | // be in map space. |
| 1462 | Map* map = object->map(); |
| 1463 | CHECK(map->IsMap()); |
| 1464 | CHECK(heap()->map_space()->Contains(map)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1465 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1466 | // The object should not be code or a map. |
| 1467 | CHECK(!object->IsMap()); |
| 1468 | CHECK(!object->IsCode()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1469 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1470 | // The object itself should look OK. |
| 1471 | object->Verify(); |
| 1472 | |
| 1473 | // All the interior pointers should be contained in the heap. |
| 1474 | VerifyPointersVisitor visitor; |
| 1475 | int size = object->Size(); |
| 1476 | object->IterateBody(map->instance_type(), size, &visitor); |
| 1477 | |
| 1478 | current += size; |
| 1479 | } else { |
| 1480 | // At end of page, switch to next page. |
| 1481 | NewSpacePage* page = NewSpacePage::FromLimit(current)->next_page(); |
| 1482 | // Next page should be valid. |
| 1483 | CHECK(!page->is_anchor()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1484 | current = page->area_start(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1485 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1488 | // Check semi-spaces. |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1489 | CHECK_EQ(from_space_.id(), kFromSpace); |
| 1490 | CHECK_EQ(to_space_.id(), kToSpace); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1491 | from_space_.Verify(); |
| 1492 | to_space_.Verify(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1493 | } |
| 1494 | #endif |
| 1495 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1496 | // ----------------------------------------------------------------------------- |
| 1497 | // SemiSpace implementation |
| 1498 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1499 | void SemiSpace::SetUp(Address start, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1500 | int initial_capacity, |
| 1501 | int maximum_capacity) { |
| 1502 | // Creates a space in the young generation. The constructor does not |
| 1503 | // allocate memory from the OS. A SemiSpace is given a contiguous chunk of |
| 1504 | // memory of size 'capacity' when set up, and does not grow or shrink |
| 1505 | // otherwise. In the mark-compact collector, the memory region of the from |
| 1506 | // space is used as the marking stack. It requires contiguous memory |
| 1507 | // addresses. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1508 | ASSERT(maximum_capacity >= Page::kPageSize); |
| 1509 | initial_capacity_ = RoundDown(initial_capacity, Page::kPageSize); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1510 | capacity_ = initial_capacity; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1511 | maximum_capacity_ = RoundDown(maximum_capacity, Page::kPageSize); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 1512 | committed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1513 | start_ = start; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1514 | address_mask_ = ~(maximum_capacity - 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1515 | object_mask_ = address_mask_ | kHeapObjectTagMask; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1516 | object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1517 | age_mark_ = start_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | void SemiSpace::TearDown() { |
| 1522 | start_ = NULL; |
| 1523 | capacity_ = 0; |
| 1524 | } |
| 1525 | |
| 1526 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1527 | bool SemiSpace::Commit() { |
| 1528 | ASSERT(!is_committed()); |
| 1529 | int pages = capacity_ / Page::kPageSize; |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1530 | if (!heap()->isolate()->memory_allocator()->CommitBlock(start_, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1531 | capacity_, |
| 1532 | executable())) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1533 | return false; |
| 1534 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1535 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1536 | NewSpacePage* current = anchor(); |
| 1537 | for (int i = 0; i < pages; i++) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1538 | NewSpacePage* new_page = |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1539 | NewSpacePage::Initialize(heap(), start_ + i * Page::kPageSize, this); |
| 1540 | new_page->InsertAfter(current); |
| 1541 | current = new_page; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | committed_ = true; |
| 1545 | Reset(); |
| 1546 | return true; |
| 1547 | } |
| 1548 | |
| 1549 | |
| 1550 | bool SemiSpace::Uncommit() { |
| 1551 | ASSERT(is_committed()); |
| 1552 | Address start = start_ + maximum_capacity_ - capacity_; |
| 1553 | if (!heap()->isolate()->memory_allocator()->UncommitBlock(start, capacity_)) { |
| 1554 | return false; |
| 1555 | } |
| 1556 | anchor()->set_next_page(anchor()); |
| 1557 | anchor()->set_prev_page(anchor()); |
| 1558 | |
| 1559 | committed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1560 | return true; |
| 1561 | } |
| 1562 | |
| 1563 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 1564 | size_t SemiSpace::CommittedPhysicalMemory() { |
| 1565 | if (!is_committed()) return 0; |
| 1566 | size_t size = 0; |
| 1567 | NewSpacePageIterator it(this); |
| 1568 | while (it.has_next()) { |
| 1569 | size += it.next()->CommittedPhysicalMemory(); |
| 1570 | } |
| 1571 | return size; |
| 1572 | } |
| 1573 | |
| 1574 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1575 | bool SemiSpace::GrowTo(int new_capacity) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1576 | if (!is_committed()) { |
| 1577 | if (!Commit()) return false; |
| 1578 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1579 | ASSERT((new_capacity & Page::kPageAlignmentMask) == 0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1580 | ASSERT(new_capacity <= maximum_capacity_); |
| 1581 | ASSERT(new_capacity > capacity_); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1582 | int pages_before = capacity_ / Page::kPageSize; |
| 1583 | int pages_after = new_capacity / Page::kPageSize; |
| 1584 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1585 | size_t delta = new_capacity - capacity_; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1586 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1587 | ASSERT(IsAligned(delta, OS::AllocateAlignment())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1588 | if (!heap()->isolate()->memory_allocator()->CommitBlock( |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1589 | start_ + capacity_, delta, executable())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1590 | return false; |
| 1591 | } |
| 1592 | capacity_ = new_capacity; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1593 | NewSpacePage* last_page = anchor()->prev_page(); |
| 1594 | ASSERT(last_page != anchor()); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1595 | for (int i = pages_before; i < pages_after; i++) { |
| 1596 | Address page_address = start_ + i * Page::kPageSize; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1597 | NewSpacePage* new_page = NewSpacePage::Initialize(heap(), |
| 1598 | page_address, |
| 1599 | this); |
| 1600 | new_page->InsertAfter(last_page); |
| 1601 | Bitmap::Clear(new_page); |
| 1602 | // Duplicate the flags that was set on the old page. |
| 1603 | new_page->SetFlags(last_page->GetFlags(), |
| 1604 | NewSpacePage::kCopyOnFlipFlagsMask); |
| 1605 | last_page = new_page; |
| 1606 | } |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1607 | return true; |
| 1608 | } |
| 1609 | |
| 1610 | |
| 1611 | bool SemiSpace::ShrinkTo(int new_capacity) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1612 | ASSERT((new_capacity & Page::kPageAlignmentMask) == 0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1613 | ASSERT(new_capacity >= initial_capacity_); |
| 1614 | ASSERT(new_capacity < capacity_); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1615 | if (is_committed()) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1616 | size_t delta = capacity_ - new_capacity; |
| 1617 | ASSERT(IsAligned(delta, OS::AllocateAlignment())); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1618 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1619 | MemoryAllocator* allocator = heap()->isolate()->memory_allocator(); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1620 | if (!allocator->UncommitBlock(start_ + new_capacity, delta)) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1621 | return false; |
| 1622 | } |
| 1623 | |
| 1624 | int pages_after = new_capacity / Page::kPageSize; |
| 1625 | NewSpacePage* new_last_page = |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1626 | NewSpacePage::FromAddress(start_ + (pages_after - 1) * Page::kPageSize); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1627 | new_last_page->set_next_page(anchor()); |
| 1628 | anchor()->set_prev_page(new_last_page); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1629 | ASSERT((current_page_ >= first_page()) && (current_page_ <= new_last_page)); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | capacity_ = new_capacity; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1633 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1634 | return true; |
| 1635 | } |
| 1636 | |
| 1637 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1638 | void SemiSpace::FlipPages(intptr_t flags, intptr_t mask) { |
| 1639 | anchor_.set_owner(this); |
| 1640 | // Fixup back-pointers to anchor. Address of anchor changes |
| 1641 | // when we swap. |
| 1642 | anchor_.prev_page()->set_next_page(&anchor_); |
| 1643 | anchor_.next_page()->set_prev_page(&anchor_); |
| 1644 | |
| 1645 | bool becomes_to_space = (id_ == kFromSpace); |
| 1646 | id_ = becomes_to_space ? kToSpace : kFromSpace; |
| 1647 | NewSpacePage* page = anchor_.next_page(); |
| 1648 | while (page != &anchor_) { |
| 1649 | page->set_owner(this); |
| 1650 | page->SetFlags(flags, mask); |
| 1651 | if (becomes_to_space) { |
| 1652 | page->ClearFlag(MemoryChunk::IN_FROM_SPACE); |
| 1653 | page->SetFlag(MemoryChunk::IN_TO_SPACE); |
| 1654 | page->ClearFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK); |
| 1655 | page->ResetLiveBytes(); |
| 1656 | } else { |
| 1657 | page->SetFlag(MemoryChunk::IN_FROM_SPACE); |
| 1658 | page->ClearFlag(MemoryChunk::IN_TO_SPACE); |
| 1659 | } |
| 1660 | ASSERT(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE)); |
| 1661 | ASSERT(page->IsFlagSet(MemoryChunk::IN_TO_SPACE) || |
| 1662 | page->IsFlagSet(MemoryChunk::IN_FROM_SPACE)); |
| 1663 | page = page->next_page(); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | |
| 1668 | void SemiSpace::Reset() { |
| 1669 | ASSERT(anchor_.next_page() != &anchor_); |
| 1670 | current_page_ = anchor_.next_page(); |
| 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | void SemiSpace::Swap(SemiSpace* from, SemiSpace* to) { |
| 1675 | // We won't be swapping semispaces without data in them. |
| 1676 | ASSERT(from->anchor_.next_page() != &from->anchor_); |
| 1677 | ASSERT(to->anchor_.next_page() != &to->anchor_); |
| 1678 | |
| 1679 | // Swap bits. |
| 1680 | SemiSpace tmp = *from; |
| 1681 | *from = *to; |
| 1682 | *to = tmp; |
| 1683 | |
| 1684 | // Fixup back-pointers to the page list anchor now that its address |
| 1685 | // has changed. |
| 1686 | // Swap to/from-space bits on pages. |
| 1687 | // Copy GC flags from old active space (from-space) to new (to-space). |
| 1688 | intptr_t flags = from->current_page()->GetFlags(); |
| 1689 | to->FlipPages(flags, NewSpacePage::kCopyOnFlipFlagsMask); |
| 1690 | |
| 1691 | from->FlipPages(0, 0); |
| 1692 | } |
| 1693 | |
| 1694 | |
| 1695 | void SemiSpace::set_age_mark(Address mark) { |
| 1696 | ASSERT(NewSpacePage::FromLimit(mark)->semi_space() == this); |
| 1697 | age_mark_ = mark; |
| 1698 | // Mark all pages up to the one containing mark. |
| 1699 | NewSpacePageIterator it(space_start(), mark); |
| 1700 | while (it.has_next()) { |
| 1701 | it.next()->SetFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK); |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1706 | #ifdef DEBUG |
| 1707 | void SemiSpace::Print() { } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1708 | #endif |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1709 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1710 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1711 | void SemiSpace::Verify() { |
| 1712 | bool is_from_space = (id_ == kFromSpace); |
| 1713 | NewSpacePage* page = anchor_.next_page(); |
| 1714 | CHECK(anchor_.semi_space() == this); |
| 1715 | while (page != &anchor_) { |
| 1716 | CHECK(page->semi_space() == this); |
| 1717 | CHECK(page->InNewSpace()); |
| 1718 | CHECK(page->IsFlagSet(is_from_space ? MemoryChunk::IN_FROM_SPACE |
| 1719 | : MemoryChunk::IN_TO_SPACE)); |
| 1720 | CHECK(!page->IsFlagSet(is_from_space ? MemoryChunk::IN_TO_SPACE |
| 1721 | : MemoryChunk::IN_FROM_SPACE)); |
| 1722 | CHECK(page->IsFlagSet(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING)); |
| 1723 | if (!is_from_space) { |
| 1724 | // The pointers-from-here-are-interesting flag isn't updated dynamically |
| 1725 | // on from-space pages, so it might be out of sync with the marking state. |
| 1726 | if (page->heap()->incremental_marking()->IsMarking()) { |
| 1727 | CHECK(page->IsFlagSet(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING)); |
| 1728 | } else { |
| 1729 | CHECK(!page->IsFlagSet( |
| 1730 | MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING)); |
| 1731 | } |
| 1732 | // TODO(gc): Check that the live_bytes_count_ field matches the |
| 1733 | // black marking on the page (if we make it match in new-space). |
| 1734 | } |
| 1735 | CHECK(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE)); |
| 1736 | CHECK(page->prev_page()->next_page() == page); |
| 1737 | page = page->next_page(); |
| 1738 | } |
| 1739 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1740 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1741 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 1742 | #ifdef DEBUG |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1743 | void SemiSpace::AssertValidRange(Address start, Address end) { |
| 1744 | // Addresses belong to same semi-space |
| 1745 | NewSpacePage* page = NewSpacePage::FromLimit(start); |
| 1746 | NewSpacePage* end_page = NewSpacePage::FromLimit(end); |
| 1747 | SemiSpace* space = page->semi_space(); |
| 1748 | CHECK_EQ(space, end_page->semi_space()); |
| 1749 | // Start address is before end address, either on same page, |
| 1750 | // or end address is on a later page in the linked list of |
| 1751 | // semi-space pages. |
| 1752 | if (page == end_page) { |
| 1753 | CHECK(start <= end); |
| 1754 | } else { |
| 1755 | while (page != end_page) { |
| 1756 | page = page->next_page(); |
| 1757 | CHECK_NE(page, space->anchor()); |
| 1758 | } |
| 1759 | } |
| 1760 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1761 | #endif |
| 1762 | |
| 1763 | |
| 1764 | // ----------------------------------------------------------------------------- |
| 1765 | // SemiSpaceIterator implementation. |
| 1766 | SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1767 | Initialize(space->bottom(), space->top(), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | |
| 1771 | SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, |
| 1772 | HeapObjectCallback size_func) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1773 | Initialize(space->bottom(), space->top(), size_func); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1778 | Initialize(start, space->top(), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1782 | SemiSpaceIterator::SemiSpaceIterator(Address from, Address to) { |
| 1783 | Initialize(from, to, NULL); |
| 1784 | } |
| 1785 | |
| 1786 | |
| 1787 | void SemiSpaceIterator::Initialize(Address start, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1788 | Address end, |
| 1789 | HeapObjectCallback size_func) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1790 | SemiSpace::AssertValidRange(start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1791 | current_ = start; |
| 1792 | limit_ = end; |
| 1793 | size_func_ = size_func; |
| 1794 | } |
| 1795 | |
| 1796 | |
| 1797 | #ifdef DEBUG |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | // heap_histograms is shared, always clear it before using it. |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1799 | static void ClearHistograms(Isolate* isolate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1800 | // We reset the name each time, though it hasn't changed. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1801 | #define DEF_TYPE_NAME(name) isolate->heap_histograms()[name].set_name(#name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | INSTANCE_TYPE_LIST(DEF_TYPE_NAME) |
| 1803 | #undef DEF_TYPE_NAME |
| 1804 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1805 | #define CLEAR_HISTOGRAM(name) isolate->heap_histograms()[name].clear(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1806 | INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM) |
| 1807 | #undef CLEAR_HISTOGRAM |
| 1808 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1809 | isolate->js_spill_information()->Clear(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1813 | static void ClearCodeKindStatistics(int* code_kind_statistics) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1814 | for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1815 | code_kind_statistics[i] = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1820 | static void ReportCodeKindStatistics(int* code_kind_statistics) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1821 | PrintF("\n Code kind histograms: \n"); |
| 1822 | for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1823 | if (code_kind_statistics[i] > 0) { |
| 1824 | PrintF(" %-20s: %10d bytes\n", |
| 1825 | Code::Kind2String(static_cast<Code::Kind>(i)), |
| 1826 | code_kind_statistics[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | PrintF("\n"); |
| 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | static int CollectHistogramInfo(HeapObject* obj) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1834 | Isolate* isolate = obj->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1835 | InstanceType type = obj->map()->instance_type(); |
| 1836 | ASSERT(0 <= type && type <= LAST_TYPE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1837 | ASSERT(isolate->heap_histograms()[type].name() != NULL); |
| 1838 | isolate->heap_histograms()[type].increment_number(1); |
| 1839 | isolate->heap_histograms()[type].increment_bytes(obj->Size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1840 | |
| 1841 | if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1842 | JSObject::cast(obj)->IncrementSpillStatistics( |
| 1843 | isolate->js_spill_information()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | return obj->Size(); |
| 1847 | } |
| 1848 | |
| 1849 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1850 | static void ReportHistogram(Isolate* isolate, bool print_spill) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1851 | PrintF("\n Object Histogram:\n"); |
| 1852 | for (int i = 0; i <= LAST_TYPE; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1853 | if (isolate->heap_histograms()[i].number() > 0) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1854 | PrintF(" %-34s%10d (%10d bytes)\n", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1855 | isolate->heap_histograms()[i].name(), |
| 1856 | isolate->heap_histograms()[i].number(), |
| 1857 | isolate->heap_histograms()[i].bytes()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | PrintF("\n"); |
| 1861 | |
| 1862 | // Summarize string types. |
| 1863 | int string_number = 0; |
| 1864 | int string_bytes = 0; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1865 | #define INCREMENT(type, size, name, camel_name) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1866 | string_number += isolate->heap_histograms()[type].number(); \ |
| 1867 | string_bytes += isolate->heap_histograms()[type].bytes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | STRING_TYPE_LIST(INCREMENT) |
| 1869 | #undef INCREMENT |
| 1870 | if (string_number > 0) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1871 | PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1872 | string_bytes); |
| 1873 | } |
| 1874 | |
| 1875 | if (FLAG_collect_heap_spill_statistics && print_spill) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1876 | isolate->js_spill_information()->Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1877 | } |
| 1878 | } |
| 1879 | #endif // DEBUG |
| 1880 | |
| 1881 | |
| 1882 | // Support for statistics gathering for --heap-stats and --log-gc. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1883 | void NewSpace::ClearHistograms() { |
| 1884 | for (int i = 0; i <= LAST_TYPE; i++) { |
| 1885 | allocated_histogram_[i].clear(); |
| 1886 | promoted_histogram_[i].clear(); |
| 1887 | } |
| 1888 | } |
| 1889 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1890 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1891 | // Because the copying collector does not touch garbage objects, we iterate |
| 1892 | // the new space before a collection to get a histogram of allocated objects. |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 1893 | // This only happens when --log-gc flag is set. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1894 | void NewSpace::CollectStatistics() { |
| 1895 | ClearHistograms(); |
| 1896 | SemiSpaceIterator it(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1897 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1898 | RecordAllocation(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1902 | static void DoReportStatistics(Isolate* isolate, |
| 1903 | HistogramInfo* info, const char* description) { |
| 1904 | LOG(isolate, HeapSampleBeginEvent("NewSpace", description)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | // Lump all the string types together. |
| 1906 | int string_number = 0; |
| 1907 | int string_bytes = 0; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1908 | #define INCREMENT(type, size, name, camel_name) \ |
| 1909 | string_number += info[type].number(); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1910 | string_bytes += info[type].bytes(); |
| 1911 | STRING_TYPE_LIST(INCREMENT) |
| 1912 | #undef INCREMENT |
| 1913 | if (string_number > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1914 | LOG(isolate, |
| 1915 | HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | // Then do the other types. |
| 1919 | for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) { |
| 1920 | if (info[i].number() > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1921 | LOG(isolate, |
| 1922 | HeapSampleItemEvent(info[i].name(), info[i].number(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | info[i].bytes())); |
| 1924 | } |
| 1925 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1926 | LOG(isolate, HeapSampleEndEvent("NewSpace", description)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1928 | |
| 1929 | |
| 1930 | void NewSpace::ReportStatistics() { |
| 1931 | #ifdef DEBUG |
| 1932 | if (FLAG_heap_stats) { |
| 1933 | float pct = static_cast<float>(Available()) / Capacity(); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1934 | PrintF(" capacity: %" V8_PTR_PREFIX "d" |
| 1935 | ", available: %" V8_PTR_PREFIX "d, %%%d\n", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | Capacity(), Available(), static_cast<int>(pct*100)); |
| 1937 | PrintF("\n Object Histogram:\n"); |
| 1938 | for (int i = 0; i <= LAST_TYPE; i++) { |
| 1939 | if (allocated_histogram_[i].number() > 0) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1940 | PrintF(" %-34s%10d (%10d bytes)\n", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1941 | allocated_histogram_[i].name(), |
| 1942 | allocated_histogram_[i].number(), |
| 1943 | allocated_histogram_[i].bytes()); |
| 1944 | } |
| 1945 | } |
| 1946 | PrintF("\n"); |
| 1947 | } |
| 1948 | #endif // DEBUG |
| 1949 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | if (FLAG_log_gc) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 1951 | Isolate* isolate = heap()->isolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1952 | DoReportStatistics(isolate, allocated_histogram_, "allocated"); |
| 1953 | DoReportStatistics(isolate, promoted_histogram_, "promoted"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1954 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | |
| 1958 | void NewSpace::RecordAllocation(HeapObject* obj) { |
| 1959 | InstanceType type = obj->map()->instance_type(); |
| 1960 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 1961 | allocated_histogram_[type].increment_number(1); |
| 1962 | allocated_histogram_[type].increment_bytes(obj->Size()); |
| 1963 | } |
| 1964 | |
| 1965 | |
| 1966 | void NewSpace::RecordPromotion(HeapObject* obj) { |
| 1967 | InstanceType type = obj->map()->instance_type(); |
| 1968 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 1969 | promoted_histogram_[type].increment_number(1); |
| 1970 | promoted_histogram_[type].increment_bytes(obj->Size()); |
| 1971 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1972 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 1973 | |
| 1974 | size_t NewSpace::CommittedPhysicalMemory() { |
| 1975 | if (!VirtualMemory::HasLazyCommits()) return CommittedMemory(); |
| 1976 | MemoryChunk::UpdateHighWaterMark(allocation_info_.top); |
| 1977 | size_t size = to_space_.CommittedPhysicalMemory(); |
| 1978 | if (from_space_.is_committed()) { |
| 1979 | size += from_space_.CommittedPhysicalMemory(); |
| 1980 | } |
| 1981 | return size; |
| 1982 | } |
| 1983 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1984 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | // ----------------------------------------------------------------------------- |
| 1986 | // Free lists for old object spaces implementation |
| 1987 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1988 | void FreeListNode::set_size(Heap* heap, int size_in_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1989 | ASSERT(size_in_bytes > 0); |
| 1990 | ASSERT(IsAligned(size_in_bytes, kPointerSize)); |
| 1991 | |
| 1992 | // We write a map and possibly size information to the block. If the block |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1993 | // is big enough to be a FreeSpace with at least one extra word (the next |
| 1994 | // pointer), we set its map to be the free space map and its size to an |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1995 | // appropriate array length for the desired size from HeapObject::Size(). |
| 1996 | // If the block is too small (eg, one or two words), to hold both a size |
| 1997 | // field and a next pointer, we give it a filler map that gives it the |
| 1998 | // correct size. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1999 | if (size_in_bytes > FreeSpace::kHeaderSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2000 | set_map_no_write_barrier(heap->raw_unchecked_free_space_map()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2001 | // Can't use FreeSpace::cast because it fails during deserialization. |
| 2002 | FreeSpace* this_as_free_space = reinterpret_cast<FreeSpace*>(this); |
| 2003 | this_as_free_space->set_size(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2004 | } else if (size_in_bytes == kPointerSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2005 | set_map_no_write_barrier(heap->raw_unchecked_one_pointer_filler_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2006 | } else if (size_in_bytes == 2 * kPointerSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2007 | set_map_no_write_barrier(heap->raw_unchecked_two_pointer_filler_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2008 | } else { |
| 2009 | UNREACHABLE(); |
| 2010 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2011 | // We would like to ASSERT(Size() == size_in_bytes) but this would fail during |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2012 | // deserialization because the free space map is not done yet. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2016 | FreeListNode* FreeListNode::next() { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2017 | ASSERT(IsFreeListNode(this)); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2018 | if (map() == GetHeap()->raw_unchecked_free_space_map()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2019 | ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize); |
| 2020 | return reinterpret_cast<FreeListNode*>( |
| 2021 | Memory::Address_at(address() + kNextOffset)); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2022 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2023 | return reinterpret_cast<FreeListNode*>( |
| 2024 | Memory::Address_at(address() + kPointerSize)); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2025 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2029 | FreeListNode** FreeListNode::next_address() { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2030 | ASSERT(IsFreeListNode(this)); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2031 | if (map() == GetHeap()->raw_unchecked_free_space_map()) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2032 | ASSERT(Size() >= kNextOffset + kPointerSize); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2033 | return reinterpret_cast<FreeListNode**>(address() + kNextOffset); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2034 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2035 | return reinterpret_cast<FreeListNode**>(address() + kPointerSize); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2036 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2040 | void FreeListNode::set_next(FreeListNode* next) { |
| 2041 | ASSERT(IsFreeListNode(this)); |
| 2042 | // While we are booting the VM the free space map will actually be null. So |
| 2043 | // we have to make sure that we don't try to use it for anything at that |
| 2044 | // stage. |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2045 | if (map() == GetHeap()->raw_unchecked_free_space_map()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2046 | ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize); |
| 2047 | Memory::Address_at(address() + kNextOffset) = |
| 2048 | reinterpret_cast<Address>(next); |
| 2049 | } else { |
| 2050 | Memory::Address_at(address() + kPointerSize) = |
| 2051 | reinterpret_cast<Address>(next); |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2056 | intptr_t FreeListCategory::Concatenate(FreeListCategory* category) { |
| 2057 | intptr_t free_bytes = 0; |
| 2058 | if (category->top_ != NULL) { |
| 2059 | ASSERT(category->end_ != NULL); |
| 2060 | // This is safe (not going to deadlock) since Concatenate operations |
| 2061 | // are never performed on the same free lists at the same time in |
| 2062 | // reverse order. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 2063 | LockGuard<Mutex> target_lock_guard(mutex()); |
| 2064 | LockGuard<Mutex> source_lock_guard(category->mutex()); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2065 | free_bytes = category->available(); |
| 2066 | if (end_ == NULL) { |
| 2067 | end_ = category->end(); |
| 2068 | } else { |
| 2069 | category->end()->set_next(top_); |
| 2070 | } |
| 2071 | top_ = category->top(); |
| 2072 | available_ += category->available(); |
| 2073 | category->Reset(); |
| 2074 | } |
| 2075 | return free_bytes; |
| 2076 | } |
| 2077 | |
| 2078 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2079 | void FreeListCategory::Reset() { |
| 2080 | top_ = NULL; |
| 2081 | end_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2082 | available_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2086 | intptr_t FreeListCategory::EvictFreeListItemsInList(Page* p) { |
| 2087 | int sum = 0; |
| 2088 | FreeListNode** n = &top_; |
| 2089 | while (*n != NULL) { |
| 2090 | if (Page::FromAddress((*n)->address()) == p) { |
| 2091 | FreeSpace* free_space = reinterpret_cast<FreeSpace*>(*n); |
| 2092 | sum += free_space->Size(); |
| 2093 | *n = (*n)->next(); |
| 2094 | } else { |
| 2095 | n = (*n)->next_address(); |
| 2096 | } |
| 2097 | } |
| 2098 | if (top_ == NULL) { |
| 2099 | end_ = NULL; |
| 2100 | } |
| 2101 | available_ -= sum; |
| 2102 | return sum; |
| 2103 | } |
| 2104 | |
| 2105 | |
| 2106 | FreeListNode* FreeListCategory::PickNodeFromList(int *node_size) { |
| 2107 | FreeListNode* node = top_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2108 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2109 | if (node == NULL) return NULL; |
| 2110 | |
| 2111 | while (node != NULL && |
| 2112 | Page::FromAddress(node->address())->IsEvacuationCandidate()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2113 | available_ -= reinterpret_cast<FreeSpace*>(node)->Size(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2114 | node = node->next(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2115 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2116 | |
| 2117 | if (node != NULL) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2118 | set_top(node->next()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2119 | *node_size = reinterpret_cast<FreeSpace*>(node)->Size(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2120 | available_ -= *node_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2121 | } else { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2122 | set_top(NULL); |
| 2123 | } |
| 2124 | |
| 2125 | if (top() == NULL) { |
| 2126 | set_end(NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2129 | return node; |
| 2130 | } |
| 2131 | |
| 2132 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2133 | FreeListNode* FreeListCategory::PickNodeFromList(int size_in_bytes, |
| 2134 | int *node_size) { |
| 2135 | FreeListNode* node = PickNodeFromList(node_size); |
| 2136 | if (node != NULL && *node_size < size_in_bytes) { |
| 2137 | Free(node, *node_size); |
| 2138 | *node_size = 0; |
| 2139 | return NULL; |
| 2140 | } |
| 2141 | return node; |
| 2142 | } |
| 2143 | |
| 2144 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2145 | void FreeListCategory::Free(FreeListNode* node, int size_in_bytes) { |
| 2146 | node->set_next(top_); |
| 2147 | top_ = node; |
| 2148 | if (end_ == NULL) { |
| 2149 | end_ = node; |
| 2150 | } |
| 2151 | available_ += size_in_bytes; |
| 2152 | } |
| 2153 | |
| 2154 | |
| 2155 | void FreeListCategory::RepairFreeList(Heap* heap) { |
| 2156 | FreeListNode* n = top_; |
| 2157 | while (n != NULL) { |
| 2158 | Map** map_location = reinterpret_cast<Map**>(n->address()); |
| 2159 | if (*map_location == NULL) { |
| 2160 | *map_location = heap->free_space_map(); |
| 2161 | } else { |
| 2162 | ASSERT(*map_location == heap->free_space_map()); |
| 2163 | } |
| 2164 | n = n->next(); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | |
| 2169 | FreeList::FreeList(PagedSpace* owner) |
| 2170 | : owner_(owner), heap_(owner->heap()) { |
| 2171 | Reset(); |
| 2172 | } |
| 2173 | |
| 2174 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2175 | intptr_t FreeList::Concatenate(FreeList* free_list) { |
| 2176 | intptr_t free_bytes = 0; |
| 2177 | free_bytes += small_list_.Concatenate(free_list->small_list()); |
| 2178 | free_bytes += medium_list_.Concatenate(free_list->medium_list()); |
| 2179 | free_bytes += large_list_.Concatenate(free_list->large_list()); |
| 2180 | free_bytes += huge_list_.Concatenate(free_list->huge_list()); |
| 2181 | return free_bytes; |
| 2182 | } |
| 2183 | |
| 2184 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2185 | void FreeList::Reset() { |
| 2186 | small_list_.Reset(); |
| 2187 | medium_list_.Reset(); |
| 2188 | large_list_.Reset(); |
| 2189 | huge_list_.Reset(); |
| 2190 | } |
| 2191 | |
| 2192 | |
| 2193 | int FreeList::Free(Address start, int size_in_bytes) { |
| 2194 | if (size_in_bytes == 0) return 0; |
| 2195 | |
| 2196 | FreeListNode* node = FreeListNode::FromAddress(start); |
| 2197 | node->set_size(heap_, size_in_bytes); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2198 | Page* page = Page::FromAddress(start); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2199 | |
| 2200 | // Early return to drop too-small blocks on the floor. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2201 | if (size_in_bytes < kSmallListMin) { |
| 2202 | page->add_non_available_small_blocks(size_in_bytes); |
| 2203 | return size_in_bytes; |
| 2204 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2205 | |
| 2206 | // Insert other blocks at the head of a free list of the appropriate |
| 2207 | // magnitude. |
| 2208 | if (size_in_bytes <= kSmallListMax) { |
| 2209 | small_list_.Free(node, size_in_bytes); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2210 | page->add_available_in_small_free_list(size_in_bytes); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2211 | } else if (size_in_bytes <= kMediumListMax) { |
| 2212 | medium_list_.Free(node, size_in_bytes); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2213 | page->add_available_in_medium_free_list(size_in_bytes); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2214 | } else if (size_in_bytes <= kLargeListMax) { |
| 2215 | large_list_.Free(node, size_in_bytes); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2216 | page->add_available_in_large_free_list(size_in_bytes); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2217 | } else { |
| 2218 | huge_list_.Free(node, size_in_bytes); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2219 | page->add_available_in_huge_free_list(size_in_bytes); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | ASSERT(IsVeryLong() || available() == SumFreeLists()); |
| 2223 | return 0; |
| 2224 | } |
| 2225 | |
| 2226 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2227 | FreeListNode* FreeList::FindNodeFor(int size_in_bytes, int* node_size) { |
| 2228 | FreeListNode* node = NULL; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2229 | Page* page = NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2230 | |
| 2231 | if (size_in_bytes <= kSmallAllocationMax) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2232 | node = small_list_.PickNodeFromList(node_size); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2233 | if (node != NULL) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2234 | ASSERT(size_in_bytes <= *node_size); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2235 | page = Page::FromAddress(node->address()); |
| 2236 | page->add_available_in_small_free_list(-(*node_size)); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2237 | ASSERT(IsVeryLong() || available() == SumFreeLists()); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2238 | return node; |
| 2239 | } |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2240 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2241 | |
| 2242 | if (size_in_bytes <= kMediumAllocationMax) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2243 | node = medium_list_.PickNodeFromList(node_size); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2244 | if (node != NULL) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2245 | ASSERT(size_in_bytes <= *node_size); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2246 | page = Page::FromAddress(node->address()); |
| 2247 | page->add_available_in_medium_free_list(-(*node_size)); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2248 | ASSERT(IsVeryLong() || available() == SumFreeLists()); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2249 | return node; |
| 2250 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | if (size_in_bytes <= kLargeAllocationMax) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2254 | node = large_list_.PickNodeFromList(node_size); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2255 | if (node != NULL) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2256 | ASSERT(size_in_bytes <= *node_size); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2257 | page = Page::FromAddress(node->address()); |
| 2258 | page->add_available_in_large_free_list(-(*node_size)); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2259 | ASSERT(IsVeryLong() || available() == SumFreeLists()); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2260 | return node; |
| 2261 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2264 | int huge_list_available = huge_list_.available(); |
| 2265 | for (FreeListNode** cur = huge_list_.GetTopAddress(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2266 | *cur != NULL; |
| 2267 | cur = (*cur)->next_address()) { |
| 2268 | FreeListNode* cur_node = *cur; |
| 2269 | while (cur_node != NULL && |
| 2270 | Page::FromAddress(cur_node->address())->IsEvacuationCandidate()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2271 | int size = reinterpret_cast<FreeSpace*>(cur_node)->Size(); |
| 2272 | huge_list_available -= size; |
| 2273 | page = Page::FromAddress(cur_node->address()); |
| 2274 | page->add_available_in_huge_free_list(-size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2275 | cur_node = cur_node->next(); |
| 2276 | } |
| 2277 | |
| 2278 | *cur = cur_node; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2279 | if (cur_node == NULL) { |
| 2280 | huge_list_.set_end(NULL); |
| 2281 | break; |
| 2282 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2283 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2284 | ASSERT((*cur)->map() == heap_->raw_unchecked_free_space_map()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2285 | FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(*cur); |
| 2286 | int size = cur_as_free_space->Size(); |
| 2287 | if (size >= size_in_bytes) { |
| 2288 | // Large enough node found. Unlink it from the list. |
| 2289 | node = *cur; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2290 | *cur = node->next(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2291 | *node_size = size; |
| 2292 | huge_list_available -= size; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2293 | page = Page::FromAddress(node->address()); |
| 2294 | page->add_available_in_huge_free_list(-size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2295 | break; |
| 2296 | } |
| 2297 | } |
| 2298 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2299 | if (huge_list_.top() == NULL) { |
| 2300 | huge_list_.set_end(NULL); |
| 2301 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2302 | huge_list_.set_available(huge_list_available); |
danno@chromium.org | fe57867 | 2013-06-15 14:38:35 +0000 | [diff] [blame] | 2303 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2304 | if (node != NULL) { |
| 2305 | ASSERT(IsVeryLong() || available() == SumFreeLists()); |
| 2306 | return node; |
| 2307 | } |
| 2308 | |
| 2309 | if (size_in_bytes <= kSmallListMax) { |
| 2310 | node = small_list_.PickNodeFromList(size_in_bytes, node_size); |
| 2311 | if (node != NULL) { |
| 2312 | ASSERT(size_in_bytes <= *node_size); |
| 2313 | page = Page::FromAddress(node->address()); |
| 2314 | page->add_available_in_small_free_list(-(*node_size)); |
| 2315 | } |
| 2316 | } else if (size_in_bytes <= kMediumListMax) { |
| 2317 | node = medium_list_.PickNodeFromList(size_in_bytes, node_size); |
| 2318 | if (node != NULL) { |
| 2319 | ASSERT(size_in_bytes <= *node_size); |
| 2320 | page = Page::FromAddress(node->address()); |
| 2321 | page->add_available_in_medium_free_list(-(*node_size)); |
| 2322 | } |
| 2323 | } else if (size_in_bytes <= kLargeListMax) { |
| 2324 | node = large_list_.PickNodeFromList(size_in_bytes, node_size); |
| 2325 | if (node != NULL) { |
| 2326 | ASSERT(size_in_bytes <= *node_size); |
| 2327 | page = Page::FromAddress(node->address()); |
| 2328 | page->add_available_in_large_free_list(-(*node_size)); |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | ASSERT(IsVeryLong() || available() == SumFreeLists()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2333 | return node; |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2337 | // Allocation on the old space free list. If it succeeds then a new linear |
| 2338 | // allocation space has been set up with the top and limit of the space. If |
| 2339 | // the allocation fails then NULL is returned, and the caller can perform a GC |
| 2340 | // or allocate a new page before retrying. |
| 2341 | HeapObject* FreeList::Allocate(int size_in_bytes) { |
| 2342 | ASSERT(0 < size_in_bytes); |
| 2343 | ASSERT(size_in_bytes <= kMaxBlockSize); |
| 2344 | ASSERT(IsAligned(size_in_bytes, kPointerSize)); |
| 2345 | // Don't free list allocate if there is linear space available. |
| 2346 | ASSERT(owner_->limit() - owner_->top() < size_in_bytes); |
| 2347 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2348 | int old_linear_size = static_cast<int>(owner_->limit() - owner_->top()); |
| 2349 | // Mark the old linear allocation area with a free space map so it can be |
| 2350 | // skipped when scanning the heap. This also puts it back in the free list |
| 2351 | // if it is big enough. |
| 2352 | owner_->Free(owner_->top(), old_linear_size); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2353 | |
danno@chromium.org | eb83146 | 2012-08-24 11:57:08 +0000 | [diff] [blame] | 2354 | owner_->heap()->incremental_marking()->OldSpaceStep( |
| 2355 | size_in_bytes - old_linear_size); |
| 2356 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2357 | int new_node_size = 0; |
| 2358 | FreeListNode* new_node = FindNodeFor(size_in_bytes, &new_node_size); |
| 2359 | if (new_node == NULL) { |
| 2360 | owner_->SetTop(NULL, NULL); |
| 2361 | return NULL; |
| 2362 | } |
| 2363 | |
| 2364 | int bytes_left = new_node_size - size_in_bytes; |
| 2365 | ASSERT(bytes_left >= 0); |
| 2366 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2367 | #ifdef DEBUG |
| 2368 | for (int i = 0; i < size_in_bytes / kPointerSize; i++) { |
| 2369 | reinterpret_cast<Object**>(new_node->address())[i] = |
| 2370 | Smi::FromInt(kCodeZapValue); |
| 2371 | } |
| 2372 | #endif |
| 2373 | |
ricow@chromium.org | fa52deb | 2011-10-11 19:09:42 +0000 | [diff] [blame] | 2374 | // The old-space-step might have finished sweeping and restarted marking. |
| 2375 | // Verify that it did not turn the page of the new node into an evacuation |
| 2376 | // candidate. |
| 2377 | ASSERT(!MarkCompactCollector::IsOnEvacuationCandidate(new_node)); |
| 2378 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2379 | const int kThreshold = IncrementalMarking::kAllocatedThreshold; |
| 2380 | |
| 2381 | // Memory in the linear allocation area is counted as allocated. We may free |
| 2382 | // a little of this again immediately - see below. |
| 2383 | owner_->Allocate(new_node_size); |
| 2384 | |
| 2385 | if (bytes_left > kThreshold && |
| 2386 | owner_->heap()->incremental_marking()->IsMarkingIncomplete() && |
| 2387 | FLAG_incremental_marking_steps) { |
| 2388 | int linear_size = owner_->RoundSizeDownToObjectAlignment(kThreshold); |
| 2389 | // We don't want to give too large linear areas to the allocator while |
| 2390 | // incremental marking is going on, because we won't check again whether |
| 2391 | // we want to do another increment until the linear area is used up. |
| 2392 | owner_->Free(new_node->address() + size_in_bytes + linear_size, |
| 2393 | new_node_size - size_in_bytes - linear_size); |
| 2394 | owner_->SetTop(new_node->address() + size_in_bytes, |
| 2395 | new_node->address() + size_in_bytes + linear_size); |
| 2396 | } else if (bytes_left > 0) { |
| 2397 | // Normally we give the rest of the node to the allocator as its new |
| 2398 | // linear allocation area. |
| 2399 | owner_->SetTop(new_node->address() + size_in_bytes, |
| 2400 | new_node->address() + new_node_size); |
| 2401 | } else { |
| 2402 | // TODO(gc) Try not freeing linear allocation region when bytes_left |
| 2403 | // are zero. |
| 2404 | owner_->SetTop(NULL, NULL); |
| 2405 | } |
| 2406 | |
| 2407 | return new_node; |
| 2408 | } |
| 2409 | |
| 2410 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2411 | intptr_t FreeList::EvictFreeListItems(Page* p) { |
| 2412 | intptr_t sum = huge_list_.EvictFreeListItemsInList(p); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2413 | p->set_available_in_huge_free_list(0); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2414 | |
| 2415 | if (sum < p->area_size()) { |
| 2416 | sum += small_list_.EvictFreeListItemsInList(p) + |
| 2417 | medium_list_.EvictFreeListItemsInList(p) + |
| 2418 | large_list_.EvictFreeListItemsInList(p); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2419 | p->set_available_in_small_free_list(0); |
| 2420 | p->set_available_in_medium_free_list(0); |
| 2421 | p->set_available_in_large_free_list(0); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 2422 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2423 | |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 2424 | return sum; |
| 2425 | } |
| 2426 | |
| 2427 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2428 | void FreeList::RepairLists(Heap* heap) { |
| 2429 | small_list_.RepairFreeList(heap); |
| 2430 | medium_list_.RepairFreeList(heap); |
| 2431 | large_list_.RepairFreeList(heap); |
| 2432 | huge_list_.RepairFreeList(heap); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2436 | #ifdef DEBUG |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2437 | intptr_t FreeListCategory::SumFreeList() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2438 | intptr_t sum = 0; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2439 | FreeListNode* cur = top_; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2440 | while (cur != NULL) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2441 | ASSERT(cur->map() == cur->GetHeap()->raw_unchecked_free_space_map()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2442 | FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(cur); |
| 2443 | sum += cur_as_free_space->Size(); |
| 2444 | cur = cur->next(); |
| 2445 | } |
| 2446 | return sum; |
| 2447 | } |
| 2448 | |
| 2449 | |
| 2450 | static const int kVeryLongFreeList = 500; |
| 2451 | |
| 2452 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2453 | int FreeListCategory::FreeListLength() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2454 | int length = 0; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2455 | FreeListNode* cur = top_; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2456 | while (cur != NULL) { |
| 2457 | length++; |
| 2458 | cur = cur->next(); |
| 2459 | if (length == kVeryLongFreeList) return length; |
| 2460 | } |
| 2461 | return length; |
| 2462 | } |
| 2463 | |
| 2464 | |
| 2465 | bool FreeList::IsVeryLong() { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2466 | if (small_list_.FreeListLength() == kVeryLongFreeList) return true; |
| 2467 | if (medium_list_.FreeListLength() == kVeryLongFreeList) return true; |
| 2468 | if (large_list_.FreeListLength() == kVeryLongFreeList) return true; |
| 2469 | if (huge_list_.FreeListLength() == kVeryLongFreeList) return true; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2470 | return false; |
| 2471 | } |
| 2472 | |
| 2473 | |
| 2474 | // This can take a very long time because it is linear in the number of entries |
| 2475 | // on the free list, so it should not be called if FreeListLength returns |
| 2476 | // kVeryLongFreeList. |
| 2477 | intptr_t FreeList::SumFreeLists() { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 2478 | intptr_t sum = small_list_.SumFreeList(); |
| 2479 | sum += medium_list_.SumFreeList(); |
| 2480 | sum += large_list_.SumFreeList(); |
| 2481 | sum += huge_list_.SumFreeList(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2482 | return sum; |
| 2483 | } |
| 2484 | #endif |
| 2485 | |
| 2486 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2487 | // ----------------------------------------------------------------------------- |
| 2488 | // OldSpace implementation |
| 2489 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2490 | bool NewSpace::ReserveSpace(int bytes) { |
| 2491 | // We can't reliably unpack a partial snapshot that needs more new space |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2492 | // space than the minimum NewSpace size. The limit can be set lower than |
| 2493 | // the end of new space either because there is more space on the next page |
| 2494 | // or because we have lowered the limit in order to get periodic incremental |
| 2495 | // marking. The most reliable way to ensure that there is linear space is |
| 2496 | // to do the allocation, then rewind the limit. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2497 | ASSERT(bytes <= InitialCapacity()); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2498 | MaybeObject* maybe = AllocateRaw(bytes); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2499 | Object* object = NULL; |
| 2500 | if (!maybe->ToObject(&object)) return false; |
| 2501 | HeapObject* allocation = HeapObject::cast(object); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2502 | Address top = allocation_info_.top; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2503 | if ((top - bytes) == allocation->address()) { |
| 2504 | allocation_info_.top = allocation->address(); |
| 2505 | return true; |
| 2506 | } |
| 2507 | // There may be a borderline case here where the allocation succeeded, but |
| 2508 | // the limit and top have moved on to a new page. In that case we try again. |
| 2509 | return ReserveSpace(bytes); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2513 | void PagedSpace::PrepareForMarkCompact() { |
| 2514 | // We don't have a linear allocation area while sweeping. It will be restored |
| 2515 | // on the first allocation after the sweep. |
| 2516 | // Mark the old linear allocation area with a free space map so it can be |
| 2517 | // skipped when scanning the heap. |
| 2518 | int old_linear_size = static_cast<int>(limit() - top()); |
| 2519 | Free(top(), old_linear_size); |
| 2520 | SetTop(NULL, NULL); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2521 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2522 | // Stop lazy sweeping and clear marking bits for unswept pages. |
| 2523 | if (first_unswept_page_ != NULL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2524 | Page* p = first_unswept_page_; |
| 2525 | do { |
| 2526 | // Do not use ShouldBeSweptLazily predicate here. |
| 2527 | // New evacuation candidates were selected but they still have |
| 2528 | // to be swept before collection starts. |
| 2529 | if (!p->WasSwept()) { |
| 2530 | Bitmap::Clear(p); |
| 2531 | if (FLAG_gc_verbose) { |
| 2532 | PrintF("Sweeping 0x%" V8PRIxPTR " lazily abandoned.\n", |
| 2533 | reinterpret_cast<intptr_t>(p)); |
| 2534 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2535 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2536 | p = p->next_page(); |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 2537 | } while (p != anchor()); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2538 | } |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 2539 | first_unswept_page_ = Page::FromAddress(NULL); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2540 | unswept_free_bytes_ = 0; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2541 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2542 | // Clear the free list before a full GC---it will be rebuilt afterward. |
| 2543 | free_list_.Reset(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2547 | bool PagedSpace::ReserveSpace(int size_in_bytes) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 2548 | ASSERT(size_in_bytes <= AreaSize()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2549 | ASSERT(size_in_bytes == RoundSizeDownToObjectAlignment(size_in_bytes)); |
| 2550 | Address current_top = allocation_info_.top; |
| 2551 | Address new_top = current_top + size_in_bytes; |
| 2552 | if (new_top <= allocation_info_.limit) return true; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2553 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2554 | HeapObject* new_area = free_list_.Allocate(size_in_bytes); |
| 2555 | if (new_area == NULL) new_area = SlowAllocateRaw(size_in_bytes); |
| 2556 | if (new_area == NULL) return false; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2557 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2558 | int old_linear_size = static_cast<int>(limit() - top()); |
| 2559 | // Mark the old linear allocation area with a free space so it can be |
| 2560 | // skipped when scanning the heap. This also puts it back in the free list |
| 2561 | // if it is big enough. |
| 2562 | Free(top(), old_linear_size); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2563 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2564 | SetTop(new_area->address(), new_area->address() + size_in_bytes); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2565 | return true; |
| 2566 | } |
| 2567 | |
| 2568 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2569 | intptr_t PagedSpace::SizeOfObjects() { |
| 2570 | ASSERT(!heap()->IsSweepingComplete() || (unswept_free_bytes_ == 0)); |
| 2571 | return Size() - unswept_free_bytes_ - (limit() - top()); |
| 2572 | } |
| 2573 | |
| 2574 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 2575 | // After we have booted, we have created a map which represents free space |
| 2576 | // on the heap. If there was already a free list then the elements on it |
| 2577 | // were created with the wrong FreeSpaceMap (normally NULL), so we need to |
| 2578 | // fix them. |
| 2579 | void PagedSpace::RepairFreeListsAfterBoot() { |
| 2580 | free_list_.RepairLists(heap()); |
| 2581 | } |
| 2582 | |
| 2583 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2584 | // You have to call this last, since the implementation from PagedSpace |
| 2585 | // doesn't know that memory was 'promised' to large object space. |
| 2586 | bool LargeObjectSpace::ReserveSpace(int bytes) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 2587 | return heap()->OldGenerationCapacityAvailable() >= bytes && |
| 2588 | (!heap()->incremental_marking()->IsStopped() || |
| 2589 | heap()->OldGenerationSpaceAvailable() >= bytes); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2593 | bool PagedSpace::AdvanceSweeper(intptr_t bytes_to_sweep) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 2594 | if (IsLazySweepingComplete()) return true; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2595 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2596 | intptr_t freed_bytes = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2597 | Page* p = first_unswept_page_; |
| 2598 | do { |
| 2599 | Page* next_page = p->next_page(); |
| 2600 | if (ShouldBeSweptLazily(p)) { |
| 2601 | if (FLAG_gc_verbose) { |
| 2602 | PrintF("Sweeping 0x%" V8PRIxPTR " lazily advanced.\n", |
| 2603 | reinterpret_cast<intptr_t>(p)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2604 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 2605 | DecreaseUnsweptFreeBytes(p); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2606 | freed_bytes += |
| 2607 | MarkCompactCollector:: |
| 2608 | SweepConservatively<MarkCompactCollector::SWEEP_SEQUENTIALLY>( |
| 2609 | this, NULL, p); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2610 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2611 | p = next_page; |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 2612 | } while (p != anchor() && freed_bytes < bytes_to_sweep); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2613 | |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 2614 | if (p == anchor()) { |
| 2615 | first_unswept_page_ = Page::FromAddress(NULL); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2616 | } else { |
| 2617 | first_unswept_page_ = p; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2620 | heap()->FreeQueuedChunks(); |
| 2621 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 2622 | return IsLazySweepingComplete(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | |
| 2626 | void PagedSpace::EvictEvacuationCandidatesFromFreeLists() { |
| 2627 | if (allocation_info_.top >= allocation_info_.limit) return; |
| 2628 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2629 | if (Page::FromAllocationTop(allocation_info_.top)->IsEvacuationCandidate()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2630 | // Create filler object to keep page iterable if it was iterable. |
| 2631 | int remaining = |
| 2632 | static_cast<int>(allocation_info_.limit - allocation_info_.top); |
| 2633 | heap()->CreateFillerObjectAt(allocation_info_.top, remaining); |
| 2634 | |
| 2635 | allocation_info_.top = NULL; |
| 2636 | allocation_info_.limit = NULL; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2641 | bool PagedSpace::EnsureSweeperProgress(intptr_t size_in_bytes) { |
| 2642 | MarkCompactCollector* collector = heap()->mark_compact_collector(); |
| 2643 | if (collector->AreSweeperThreadsActivated()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2644 | if (collector->IsConcurrentSweepingInProgress()) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 2645 | if (collector->StealMemoryFromSweeperThreads(this) < size_in_bytes) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2646 | if (!collector->sequential_sweeping()) { |
| 2647 | collector->WaitUntilSweepingCompleted(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2648 | return true; |
| 2649 | } |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 2650 | } |
| 2651 | return false; |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2652 | } |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 2653 | return true; |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2654 | } else { |
| 2655 | return AdvanceSweeper(size_in_bytes); |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2660 | HeapObject* PagedSpace::SlowAllocateRaw(int size_in_bytes) { |
| 2661 | // Allocation in this space has failed. |
| 2662 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 2663 | // If there are unswept pages advance lazy sweeper a bounded number of times |
| 2664 | // until we find a size_in_bytes contiguous piece of memory |
| 2665 | const int kMaxSweepingTries = 5; |
| 2666 | bool sweeping_complete = false; |
| 2667 | |
| 2668 | for (int i = 0; i < kMaxSweepingTries && !sweeping_complete; i++) { |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2669 | sweeping_complete = EnsureSweeperProgress(size_in_bytes); |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 2670 | |
| 2671 | // Retry the free list allocation. |
| 2672 | HeapObject* object = free_list_.Allocate(size_in_bytes); |
| 2673 | if (object != NULL) return object; |
| 2674 | } |
| 2675 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2676 | // Free list allocation failed and there is no next page. Fail if we have |
| 2677 | // hit the old generation size limit that should cause a garbage |
| 2678 | // collection. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2679 | if (!heap()->always_allocate() && |
| 2680 | heap()->OldGenerationAllocationLimitReached()) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2681 | return NULL; |
| 2682 | } |
| 2683 | |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 2684 | // Try to expand the space and allocate in the new next page. |
| 2685 | if (Expand()) { |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 2686 | ASSERT(CountTotalPages() > 1 || size_in_bytes <= free_list_.available()); |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 2687 | return free_list_.Allocate(size_in_bytes); |
| 2688 | } |
| 2689 | |
| 2690 | // Last ditch, sweep all the remaining pages to try to find space. This may |
| 2691 | // cause a pause. |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 2692 | if (!IsLazySweepingComplete()) { |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2693 | EnsureSweeperProgress(kMaxInt); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2694 | |
| 2695 | // Retry the free list allocation. |
| 2696 | HeapObject* object = free_list_.Allocate(size_in_bytes); |
| 2697 | if (object != NULL) return object; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | // Finally, fail. |
| 2701 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2705 | #ifdef DEBUG |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 2706 | void PagedSpace::ReportCodeStatistics(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2707 | CommentStatistic* comments_statistics = |
| 2708 | isolate->paged_space_comments_statistics(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2709 | ReportCodeKindStatistics(isolate->code_kind_statistics()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2710 | PrintF("Code comment statistics (\" [ comment-txt : size/ " |
| 2711 | "count (average)\"):\n"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2712 | for (int i = 0; i <= CommentStatistic::kMaxComments; i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2713 | const CommentStatistic& cs = comments_statistics[i]; |
| 2714 | if (cs.size > 0) { |
| 2715 | PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count, |
| 2716 | cs.size/cs.count); |
| 2717 | } |
| 2718 | } |
| 2719 | PrintF("\n"); |
| 2720 | } |
| 2721 | |
| 2722 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 2723 | void PagedSpace::ResetCodeStatistics(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2724 | CommentStatistic* comments_statistics = |
| 2725 | isolate->paged_space_comments_statistics(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2726 | ClearCodeKindStatistics(isolate->code_kind_statistics()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2727 | for (int i = 0; i < CommentStatistic::kMaxComments; i++) { |
| 2728 | comments_statistics[i].Clear(); |
| 2729 | } |
| 2730 | comments_statistics[CommentStatistic::kMaxComments].comment = "Unknown"; |
| 2731 | comments_statistics[CommentStatistic::kMaxComments].size = 0; |
| 2732 | comments_statistics[CommentStatistic::kMaxComments].count = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2736 | // Adds comment to 'comment_statistics' table. Performance OK as long as |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2737 | // 'kMaxComments' is small |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2738 | static void EnterComment(Isolate* isolate, const char* comment, int delta) { |
| 2739 | CommentStatistic* comments_statistics = |
| 2740 | isolate->paged_space_comments_statistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2741 | // Do not count empty comments |
| 2742 | if (delta <= 0) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2743 | CommentStatistic* cs = &comments_statistics[CommentStatistic::kMaxComments]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2744 | // Search for a free or matching entry in 'comments_statistics': 'cs' |
| 2745 | // points to result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2746 | for (int i = 0; i < CommentStatistic::kMaxComments; i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2747 | if (comments_statistics[i].comment == NULL) { |
| 2748 | cs = &comments_statistics[i]; |
| 2749 | cs->comment = comment; |
| 2750 | break; |
| 2751 | } else if (strcmp(comments_statistics[i].comment, comment) == 0) { |
| 2752 | cs = &comments_statistics[i]; |
| 2753 | break; |
| 2754 | } |
| 2755 | } |
| 2756 | // Update entry for 'comment' |
| 2757 | cs->size += delta; |
| 2758 | cs->count += 1; |
| 2759 | } |
| 2760 | |
| 2761 | |
| 2762 | // Call for each nested comment start (start marked with '[ xxx', end marked |
| 2763 | // with ']'. RelocIterator 'it' must point to a comment reloc info. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2764 | static void CollectCommentStatistics(Isolate* isolate, RelocIterator* it) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2765 | ASSERT(!it->done()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2766 | ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2767 | const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data()); |
| 2768 | if (tmp[0] != '[') { |
| 2769 | // Not a nested comment; skip |
| 2770 | return; |
| 2771 | } |
| 2772 | |
| 2773 | // Search for end of nested comment or a new nested comment |
| 2774 | const char* const comment_txt = |
| 2775 | reinterpret_cast<const char*>(it->rinfo()->data()); |
| 2776 | const byte* prev_pc = it->rinfo()->pc(); |
| 2777 | int flat_delta = 0; |
| 2778 | it->next(); |
| 2779 | while (true) { |
| 2780 | // All nested comments must be terminated properly, and therefore exit |
| 2781 | // from loop. |
| 2782 | ASSERT(!it->done()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2783 | if (it->rinfo()->rmode() == RelocInfo::COMMENT) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2784 | const char* const txt = |
| 2785 | reinterpret_cast<const char*>(it->rinfo()->data()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2786 | flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2787 | if (txt[0] == ']') break; // End of nested comment |
| 2788 | // A new comment |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2789 | CollectCommentStatistics(isolate, it); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2790 | // Skip code that was covered with previous comment |
| 2791 | prev_pc = it->rinfo()->pc(); |
| 2792 | } |
| 2793 | it->next(); |
| 2794 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2795 | EnterComment(isolate, comment_txt, flat_delta); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | |
| 2799 | // Collects code size statistics: |
| 2800 | // - by code kind |
| 2801 | // - by code comment |
| 2802 | void PagedSpace::CollectCodeStatistics() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2803 | Isolate* isolate = heap()->isolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2804 | HeapObjectIterator obj_it(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2805 | for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2806 | if (obj->IsCode()) { |
| 2807 | Code* code = Code::cast(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2808 | isolate->code_kind_statistics()[code->kind()] += code->Size(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2809 | RelocIterator it(code); |
| 2810 | int delta = 0; |
| 2811 | const byte* prev_pc = code->instruction_start(); |
| 2812 | while (!it.done()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2813 | if (it.rinfo()->rmode() == RelocInfo::COMMENT) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2814 | delta += static_cast<int>(it.rinfo()->pc() - prev_pc); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2815 | CollectCommentStatistics(isolate, &it); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2816 | prev_pc = it.rinfo()->pc(); |
| 2817 | } |
| 2818 | it.next(); |
| 2819 | } |
| 2820 | |
| 2821 | ASSERT(code->instruction_start() <= prev_pc && |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2822 | prev_pc <= code->instruction_end()); |
| 2823 | delta += static_cast<int>(code->instruction_end() - prev_pc); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2824 | EnterComment(isolate, "NoComment", delta); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2830 | void PagedSpace::ReportStatistics() { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 2831 | int pct = static_cast<int>(Available() * 100 / Capacity()); |
| 2832 | PrintF(" capacity: %" V8_PTR_PREFIX "d" |
| 2833 | ", waste: %" V8_PTR_PREFIX "d" |
| 2834 | ", available: %" V8_PTR_PREFIX "d, %%%d\n", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2835 | Capacity(), Waste(), Available(), pct); |
| 2836 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2837 | if (was_swept_conservatively_) return; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 2838 | ClearHistograms(heap()->isolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2839 | HeapObjectIterator obj_it(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2840 | for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2841 | CollectHistogramInfo(obj); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 2842 | ReportHistogram(heap()->isolate(), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2843 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2844 | #endif |
| 2845 | |
| 2846 | // ----------------------------------------------------------------------------- |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2847 | // FixedSpace implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2848 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2849 | void FixedSpace::PrepareForMarkCompact() { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2850 | // Call prepare of the super class. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2851 | PagedSpace::PrepareForMarkCompact(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2852 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2853 | // During a non-compacting collection, everything below the linear |
| 2854 | // allocation pointer except wasted top-of-page blocks is considered |
| 2855 | // allocated and we will rediscover available bytes during the |
| 2856 | // collection. |
| 2857 | accounting_stats_.AllocateBytes(free_list_.available()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2858 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2859 | // Clear the free list before a full GC---it will be rebuilt afterward. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2860 | free_list_.Reset(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2864 | // ----------------------------------------------------------------------------- |
| 2865 | // MapSpace implementation |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2866 | // TODO(mvstanton): this is weird...the compiler can't make a vtable unless |
| 2867 | // there is at least one non-inlined virtual function. I would prefer to hide |
| 2868 | // the VerifyObject definition behind VERIFY_HEAP. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2869 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2870 | void MapSpace::VerifyObject(HeapObject* object) { |
verwaest@chromium.org | ec6855e | 2013-08-22 12:26:58 +0000 | [diff] [blame] | 2871 | CHECK(object->IsMap()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2872 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2873 | |
| 2874 | |
| 2875 | // ----------------------------------------------------------------------------- |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2876 | // CellSpace and PropertyCellSpace implementation |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2877 | // TODO(mvstanton): this is weird...the compiler can't make a vtable unless |
| 2878 | // there is at least one non-inlined virtual function. I would prefer to hide |
| 2879 | // the VerifyObject definition behind VERIFY_HEAP. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2880 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2881 | void CellSpace::VerifyObject(HeapObject* object) { |
verwaest@chromium.org | ec6855e | 2013-08-22 12:26:58 +0000 | [diff] [blame] | 2882 | CHECK(object->IsCell()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | |
| 2886 | void PropertyCellSpace::VerifyObject(HeapObject* object) { |
verwaest@chromium.org | ec6855e | 2013-08-22 12:26:58 +0000 | [diff] [blame] | 2887 | CHECK(object->IsPropertyCell()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2888 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2889 | |
| 2890 | |
| 2891 | // ----------------------------------------------------------------------------- |
| 2892 | // LargeObjectIterator |
| 2893 | |
| 2894 | LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2895 | current_ = space->first_page_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2896 | size_func_ = NULL; |
| 2897 | } |
| 2898 | |
| 2899 | |
| 2900 | LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space, |
| 2901 | HeapObjectCallback size_func) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2902 | current_ = space->first_page_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2903 | size_func_ = size_func; |
| 2904 | } |
| 2905 | |
| 2906 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2907 | HeapObject* LargeObjectIterator::Next() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2908 | if (current_ == NULL) return NULL; |
| 2909 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2910 | HeapObject* object = current_->GetObject(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2911 | current_ = current_->next_page(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2912 | return object; |
| 2913 | } |
| 2914 | |
| 2915 | |
| 2916 | // ----------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2917 | // LargeObjectSpace |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 2918 | static bool ComparePointers(void* key1, void* key2) { |
| 2919 | return key1 == key2; |
| 2920 | } |
| 2921 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2922 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2923 | LargeObjectSpace::LargeObjectSpace(Heap* heap, |
| 2924 | intptr_t max_capacity, |
| 2925 | AllocationSpace id) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2926 | : Space(heap, id, NOT_EXECUTABLE), // Managed on a per-allocation basis |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2927 | max_capacity_(max_capacity), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2928 | first_page_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2929 | size_(0), |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2930 | page_count_(0), |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 2931 | objects_size_(0), |
| 2932 | chunk_map_(ComparePointers, 1024) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2933 | |
| 2934 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2935 | bool LargeObjectSpace::SetUp() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2936 | first_page_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2937 | size_ = 0; |
| 2938 | page_count_ = 0; |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2939 | objects_size_ = 0; |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 2940 | chunk_map_.Clear(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2941 | return true; |
| 2942 | } |
| 2943 | |
| 2944 | |
| 2945 | void LargeObjectSpace::TearDown() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2946 | while (first_page_ != NULL) { |
| 2947 | LargePage* page = first_page_; |
| 2948 | first_page_ = first_page_->next_page(); |
| 2949 | LOG(heap()->isolate(), DeleteEvent("LargeObjectChunk", page->address())); |
| 2950 | |
| 2951 | ObjectSpace space = static_cast<ObjectSpace>(1 << identity()); |
| 2952 | heap()->isolate()->memory_allocator()->PerformAllocationCallback( |
| 2953 | space, kAllocationActionFree, page->size()); |
| 2954 | heap()->isolate()->memory_allocator()->Free(page); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2955 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2956 | SetUp(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2960 | MaybeObject* LargeObjectSpace::AllocateRaw(int object_size, |
| 2961 | Executability executable) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2962 | // Check if we want to force a GC before growing the old space further. |
| 2963 | // If so, fail the allocation. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2964 | if (!heap()->always_allocate() && |
| 2965 | heap()->OldGenerationAllocationLimitReached()) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 2966 | return Failure::RetryAfterGC(identity()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2969 | if (Size() + object_size > max_capacity_) { |
| 2970 | return Failure::RetryAfterGC(identity()); |
| 2971 | } |
| 2972 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2973 | LargePage* page = heap()->isolate()->memory_allocator()-> |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 2974 | AllocateLargePage(object_size, this, executable); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2975 | if (page == NULL) return Failure::RetryAfterGC(identity()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 2976 | ASSERT(page->area_size() >= object_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2977 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2978 | size_ += static_cast<int>(page->size()); |
| 2979 | objects_size_ += object_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2980 | page_count_++; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2981 | page->set_next_page(first_page_); |
| 2982 | first_page_ = page; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2983 | |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 2984 | // Register all MemoryChunk::kAlignment-aligned chunks covered by |
| 2985 | // this large page in the chunk map. |
| 2986 | uintptr_t base = reinterpret_cast<uintptr_t>(page) / MemoryChunk::kAlignment; |
| 2987 | uintptr_t limit = base + (page->size() - 1) / MemoryChunk::kAlignment; |
| 2988 | for (uintptr_t key = base; key <= limit; key++) { |
| 2989 | HashMap::Entry* entry = chunk_map_.Lookup(reinterpret_cast<void*>(key), |
| 2990 | static_cast<uint32_t>(key), |
| 2991 | true); |
| 2992 | ASSERT(entry != NULL); |
| 2993 | entry->value = page; |
| 2994 | } |
| 2995 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 2996 | HeapObject* object = page->GetObject(); |
| 2997 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2998 | if (Heap::ShouldZapGarbage()) { |
| 2999 | // Make the object consistent so the heap can be verified in OldSpaceStep. |
| 3000 | // We only need to do this in debug builds or if verify_heap is on. |
| 3001 | reinterpret_cast<Object**>(object->address())[0] = |
| 3002 | heap()->fixed_array_map(); |
| 3003 | reinterpret_cast<Object**>(object->address())[1] = Smi::FromInt(0); |
| 3004 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3005 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3006 | heap()->incremental_marking()->OldSpaceStep(object_size); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 3007 | return object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 3011 | size_t LargeObjectSpace::CommittedPhysicalMemory() { |
| 3012 | if (!VirtualMemory::HasLazyCommits()) return CommittedMemory(); |
| 3013 | size_t size = 0; |
| 3014 | LargePage* current = first_page_; |
| 3015 | while (current != NULL) { |
| 3016 | size += current->CommittedPhysicalMemory(); |
| 3017 | current = current->next_page(); |
| 3018 | } |
| 3019 | return size; |
| 3020 | } |
| 3021 | |
| 3022 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3023 | // GC support |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3024 | MaybeObject* LargeObjectSpace::FindObject(Address a) { |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 3025 | LargePage* page = FindPage(a); |
| 3026 | if (page != NULL) { |
| 3027 | return page->GetObject(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3028 | } |
| 3029 | return Failure::Exception(); |
| 3030 | } |
| 3031 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3032 | |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 3033 | LargePage* LargeObjectSpace::FindPage(Address a) { |
| 3034 | uintptr_t key = reinterpret_cast<uintptr_t>(a) / MemoryChunk::kAlignment; |
| 3035 | HashMap::Entry* e = chunk_map_.Lookup(reinterpret_cast<void*>(key), |
| 3036 | static_cast<uint32_t>(key), |
| 3037 | false); |
| 3038 | if (e != NULL) { |
| 3039 | ASSERT(e->value != NULL); |
| 3040 | LargePage* page = reinterpret_cast<LargePage*>(e->value); |
| 3041 | ASSERT(page->is_valid()); |
| 3042 | if (page->Contains(a)) { |
| 3043 | return page; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | return NULL; |
| 3047 | } |
| 3048 | |
| 3049 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3050 | void LargeObjectSpace::FreeUnmarkedObjects() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3051 | LargePage* previous = NULL; |
| 3052 | LargePage* current = first_page_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3053 | while (current != NULL) { |
| 3054 | HeapObject* object = current->GetObject(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3055 | // Can this large page contain pointers to non-trivial objects. No other |
| 3056 | // pointer object is this big. |
| 3057 | bool is_pointer_object = object->IsFixedArray(); |
| 3058 | MarkBit mark_bit = Marking::MarkBitFrom(object); |
| 3059 | if (mark_bit.Get()) { |
| 3060 | mark_bit.Clear(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3061 | Page::FromAddress(object->address())->ResetProgressBar(); |
| 3062 | Page::FromAddress(object->address())->ResetLiveBytes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3063 | previous = current; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3064 | current = current->next_page(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3065 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3066 | LargePage* page = current; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3067 | // Cut the chunk out from the chunk list. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3068 | current = current->next_page(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3069 | if (previous == NULL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3070 | first_page_ = current; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3071 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3072 | previous->set_next_page(current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | // Free the chunk. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3076 | heap()->mark_compact_collector()->ReportDeleteIfNeeded( |
| 3077 | object, heap()->isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3078 | size_ -= static_cast<int>(page->size()); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 3079 | objects_size_ -= object->Size(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3080 | page_count_--; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3081 | |
jkummerow@chromium.org | 531dfe8 | 2012-03-20 13:01:16 +0000 | [diff] [blame] | 3082 | // Remove entries belonging to this page. |
| 3083 | // Use variable alignment to help pass length check (<= 80 characters) |
| 3084 | // of single line in tools/presubmit.py. |
| 3085 | const intptr_t alignment = MemoryChunk::kAlignment; |
| 3086 | uintptr_t base = reinterpret_cast<uintptr_t>(page)/alignment; |
| 3087 | uintptr_t limit = base + (page->size()-1)/alignment; |
| 3088 | for (uintptr_t key = base; key <= limit; key++) { |
| 3089 | chunk_map_.Remove(reinterpret_cast<void*>(key), |
| 3090 | static_cast<uint32_t>(key)); |
| 3091 | } |
| 3092 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3093 | if (is_pointer_object) { |
| 3094 | heap()->QueueMemoryChunkForFree(page); |
| 3095 | } else { |
| 3096 | heap()->isolate()->memory_allocator()->Free(page); |
| 3097 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3098 | } |
| 3099 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3100 | heap()->FreeQueuedChunks(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | |
| 3104 | bool LargeObjectSpace::Contains(HeapObject* object) { |
| 3105 | Address address = object->address(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3106 | MemoryChunk* chunk = MemoryChunk::FromAddress(address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3107 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3108 | bool owned = (chunk->owner() == this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3109 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3110 | SLOW_ASSERT(!owned || !FindObject(address)->IsFailure()); |
| 3111 | |
| 3112 | return owned; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3116 | #ifdef VERIFY_HEAP |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3117 | // We do not assume that the large object iterator works, because it depends |
| 3118 | // on the invariants we are checking during verification. |
| 3119 | void LargeObjectSpace::Verify() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3120 | for (LargePage* chunk = first_page_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3121 | chunk != NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3122 | chunk = chunk->next_page()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3123 | // Each chunk contains an object that starts at the large object page's |
| 3124 | // object area start. |
| 3125 | HeapObject* object = chunk->GetObject(); |
| 3126 | Page* page = Page::FromAddress(object->address()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3127 | CHECK(object->address() == page->area_start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3128 | |
| 3129 | // The first word should be a map, and we expect all map pointers to be |
| 3130 | // in map space. |
| 3131 | Map* map = object->map(); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3132 | CHECK(map->IsMap()); |
| 3133 | CHECK(heap()->map_space()->Contains(map)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3134 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3135 | // We have only code, sequential strings, external strings |
| 3136 | // (sequential strings that have been morphed into external |
| 3137 | // strings), fixed arrays, and byte arrays in large object space. |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3138 | CHECK(object->IsCode() || object->IsSeqString() || |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3139 | object->IsExternalString() || object->IsFixedArray() || |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3140 | object->IsFixedDoubleArray() || object->IsByteArray()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3141 | |
| 3142 | // The object itself should look OK. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3143 | object->Verify(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3144 | |
| 3145 | // Byte arrays and strings don't have interior pointers. |
| 3146 | if (object->IsCode()) { |
| 3147 | VerifyPointersVisitor code_visitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3148 | object->IterateBody(map->instance_type(), |
| 3149 | object->Size(), |
| 3150 | &code_visitor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3151 | } else if (object->IsFixedArray()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3152 | FixedArray* array = FixedArray::cast(object); |
| 3153 | for (int j = 0; j < array->length(); j++) { |
| 3154 | Object* element = array->get(j); |
| 3155 | if (element->IsHeapObject()) { |
| 3156 | HeapObject* element_object = HeapObject::cast(element); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3157 | CHECK(heap()->Contains(element_object)); |
| 3158 | CHECK(element_object->map()->IsMap()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | } |
| 3163 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3164 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3165 | |
| 3166 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 3167 | #ifdef DEBUG |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3168 | void LargeObjectSpace::Print() { |
| 3169 | LargeObjectIterator it(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3170 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3171 | obj->Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3172 | } |
| 3173 | } |
| 3174 | |
| 3175 | |
| 3176 | void LargeObjectSpace::ReportStatistics() { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3177 | PrintF(" size: %" V8_PTR_PREFIX "d\n", size_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3178 | int num_objects = 0; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3179 | ClearHistograms(heap()->isolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3180 | LargeObjectIterator it(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3181 | for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3182 | num_objects++; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3183 | CollectHistogramInfo(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 3186 | PrintF(" number of objects %d, " |
| 3187 | "size of objects %" V8_PTR_PREFIX "d\n", num_objects, objects_size_); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3188 | if (num_objects > 0) ReportHistogram(heap()->isolate(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | |
| 3192 | void LargeObjectSpace::CollectCodeStatistics() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3193 | Isolate* isolate = heap()->isolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3194 | LargeObjectIterator obj_it(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3195 | for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3196 | if (obj->IsCode()) { |
| 3197 | Code* code = Code::cast(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3198 | isolate->code_kind_statistics()[code->kind()] += code->Size(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3199 | } |
| 3200 | } |
| 3201 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3202 | |
| 3203 | |
| 3204 | void Page::Print() { |
| 3205 | // Make a best-effort to print the objects in the page. |
| 3206 | PrintF("Page@%p in %s\n", |
| 3207 | this->address(), |
| 3208 | AllocationSpaceName(this->owner()->identity())); |
| 3209 | printf(" --------------------------------------\n"); |
| 3210 | HeapObjectIterator objects(this, heap()->GcSafeSizeOfOldObjectFunction()); |
| 3211 | unsigned mark_size = 0; |
| 3212 | for (HeapObject* object = objects.Next(); |
| 3213 | object != NULL; |
| 3214 | object = objects.Next()) { |
| 3215 | bool is_marked = Marking::MarkBitFrom(object).Get(); |
| 3216 | PrintF(" %c ", (is_marked ? '!' : ' ')); // Indent a little. |
| 3217 | if (is_marked) { |
| 3218 | mark_size += heap()->GcSafeSizeOfOldObjectFunction()(object); |
| 3219 | } |
| 3220 | object->ShortPrint(); |
| 3221 | PrintF("\n"); |
| 3222 | } |
| 3223 | printf(" --------------------------------------\n"); |
| 3224 | printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes()); |
| 3225 | } |
| 3226 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3227 | #endif // DEBUG |
| 3228 | |
| 3229 | } } // namespace v8::internal |