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