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