Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "macro-assembler.h" |
| 31 | #include "mark-compact.h" |
| 32 | #include "platform.h" |
| 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | // For contiguous spaces, top should be in the space (or at the end) and limit |
| 38 | // should be the end of the space. |
| 39 | #define ASSERT_SEMISPACE_ALLOCATION_INFO(info, space) \ |
| 40 | ASSERT((space).low() <= (info).top \ |
| 41 | && (info).top <= (space).high() \ |
| 42 | && (info).limit == (space).high()) |
| 43 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 44 | intptr_t Page::watermark_invalidated_mark_ = 1 << Page::WATERMARK_INVALIDATED; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | // HeapObjectIterator |
| 48 | |
| 49 | HeapObjectIterator::HeapObjectIterator(PagedSpace* space) { |
| 50 | Initialize(space->bottom(), space->top(), NULL); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | HeapObjectIterator::HeapObjectIterator(PagedSpace* space, |
| 55 | HeapObjectCallback size_func) { |
| 56 | Initialize(space->bottom(), space->top(), size_func); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start) { |
| 61 | Initialize(start, space->top(), NULL); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start, |
| 66 | HeapObjectCallback size_func) { |
| 67 | Initialize(start, space->top(), size_func); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void HeapObjectIterator::Initialize(Address cur, Address end, |
| 72 | HeapObjectCallback size_f) { |
| 73 | cur_addr_ = cur; |
| 74 | end_addr_ = end; |
| 75 | end_page_ = Page::FromAllocationTop(end); |
| 76 | size_func_ = size_f; |
| 77 | Page* p = Page::FromAllocationTop(cur_addr_); |
| 78 | cur_limit_ = (p == end_page_) ? end_addr_ : p->AllocationTop(); |
| 79 | |
| 80 | #ifdef DEBUG |
| 81 | Verify(); |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 86 | HeapObject* HeapObjectIterator::FromNextPage() { |
| 87 | if (cur_addr_ == end_addr_) return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
| 89 | Page* cur_page = Page::FromAllocationTop(cur_addr_); |
| 90 | cur_page = cur_page->next_page(); |
| 91 | ASSERT(cur_page->is_valid()); |
| 92 | |
| 93 | cur_addr_ = cur_page->ObjectAreaStart(); |
| 94 | cur_limit_ = (cur_page == end_page_) ? end_addr_ : cur_page->AllocationTop(); |
| 95 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 96 | if (cur_addr_ == end_addr_) return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | ASSERT(cur_addr_ < cur_limit_); |
| 98 | #ifdef DEBUG |
| 99 | Verify(); |
| 100 | #endif |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 101 | return FromCurrentPage(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | #ifdef DEBUG |
| 106 | void HeapObjectIterator::Verify() { |
| 107 | Page* p = Page::FromAllocationTop(cur_addr_); |
| 108 | ASSERT(p == Page::FromAllocationTop(cur_limit_)); |
| 109 | ASSERT(p->Offset(cur_addr_) <= p->Offset(cur_limit_)); |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | |
| 114 | // ----------------------------------------------------------------------------- |
| 115 | // PageIterator |
| 116 | |
| 117 | PageIterator::PageIterator(PagedSpace* space, Mode mode) : space_(space) { |
| 118 | prev_page_ = NULL; |
| 119 | switch (mode) { |
| 120 | case PAGES_IN_USE: |
| 121 | stop_page_ = space->AllocationTopPage(); |
| 122 | break; |
| 123 | case PAGES_USED_BY_MC: |
| 124 | stop_page_ = space->MCRelocationTopPage(); |
| 125 | break; |
| 126 | case ALL_PAGES: |
| 127 | #ifdef DEBUG |
| 128 | // Verify that the cached last page in the space is actually the |
| 129 | // last page. |
| 130 | for (Page* p = space->first_page_; p->is_valid(); p = p->next_page()) { |
| 131 | if (!p->next_page()->is_valid()) { |
| 132 | ASSERT(space->last_page_ == p); |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | stop_page_ = space->last_page_; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | // ----------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | // CodeRange |
| 144 | |
| 145 | List<CodeRange::FreeBlock> CodeRange::free_list_(0); |
| 146 | List<CodeRange::FreeBlock> CodeRange::allocation_list_(0); |
| 147 | int CodeRange::current_allocation_block_index_ = 0; |
| 148 | VirtualMemory* CodeRange::code_range_ = NULL; |
| 149 | |
| 150 | |
| 151 | bool CodeRange::Setup(const size_t requested) { |
| 152 | ASSERT(code_range_ == NULL); |
| 153 | |
| 154 | code_range_ = new VirtualMemory(requested); |
| 155 | CHECK(code_range_ != NULL); |
| 156 | if (!code_range_->IsReserved()) { |
| 157 | delete code_range_; |
| 158 | code_range_ = NULL; |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // We are sure that we have mapped a block of requested addresses. |
| 163 | ASSERT(code_range_->size() == requested); |
| 164 | LOG(NewEvent("CodeRange", code_range_->address(), requested)); |
| 165 | allocation_list_.Add(FreeBlock(code_range_->address(), code_range_->size())); |
| 166 | current_allocation_block_index_ = 0; |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | int CodeRange::CompareFreeBlockAddress(const FreeBlock* left, |
| 172 | const FreeBlock* right) { |
| 173 | // The entire point of CodeRange is that the difference between two |
| 174 | // addresses in the range can be represented as a signed 32-bit int, |
| 175 | // so the cast is semantically correct. |
| 176 | return static_cast<int>(left->start - right->start); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | void CodeRange::GetNextAllocationBlock(size_t requested) { |
| 181 | for (current_allocation_block_index_++; |
| 182 | current_allocation_block_index_ < allocation_list_.length(); |
| 183 | current_allocation_block_index_++) { |
| 184 | if (requested <= allocation_list_[current_allocation_block_index_].size) { |
| 185 | return; // Found a large enough allocation block. |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Sort and merge the free blocks on the free list and the allocation list. |
| 190 | free_list_.AddAll(allocation_list_); |
| 191 | allocation_list_.Clear(); |
| 192 | free_list_.Sort(&CompareFreeBlockAddress); |
| 193 | for (int i = 0; i < free_list_.length();) { |
| 194 | FreeBlock merged = free_list_[i]; |
| 195 | i++; |
| 196 | // Add adjacent free blocks to the current merged block. |
| 197 | while (i < free_list_.length() && |
| 198 | free_list_[i].start == merged.start + merged.size) { |
| 199 | merged.size += free_list_[i].size; |
| 200 | i++; |
| 201 | } |
| 202 | if (merged.size > 0) { |
| 203 | allocation_list_.Add(merged); |
| 204 | } |
| 205 | } |
| 206 | free_list_.Clear(); |
| 207 | |
| 208 | for (current_allocation_block_index_ = 0; |
| 209 | current_allocation_block_index_ < allocation_list_.length(); |
| 210 | current_allocation_block_index_++) { |
| 211 | if (requested <= allocation_list_[current_allocation_block_index_].size) { |
| 212 | return; // Found a large enough allocation block. |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Code range is full or too fragmented. |
| 217 | V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock"); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | void* CodeRange::AllocateRawMemory(const size_t requested, size_t* allocated) { |
| 223 | ASSERT(current_allocation_block_index_ < allocation_list_.length()); |
| 224 | if (requested > allocation_list_[current_allocation_block_index_].size) { |
| 225 | // Find an allocation block large enough. This function call may |
| 226 | // call V8::FatalProcessOutOfMemory if it cannot find a large enough block. |
| 227 | GetNextAllocationBlock(requested); |
| 228 | } |
| 229 | // Commit the requested memory at the start of the current allocation block. |
| 230 | *allocated = RoundUp(requested, Page::kPageSize); |
| 231 | FreeBlock current = allocation_list_[current_allocation_block_index_]; |
| 232 | if (*allocated >= current.size - Page::kPageSize) { |
| 233 | // Don't leave a small free block, useless for a large object or chunk. |
| 234 | *allocated = current.size; |
| 235 | } |
| 236 | ASSERT(*allocated <= current.size); |
| 237 | if (!code_range_->Commit(current.start, *allocated, true)) { |
| 238 | *allocated = 0; |
| 239 | return NULL; |
| 240 | } |
| 241 | allocation_list_[current_allocation_block_index_].start += *allocated; |
| 242 | allocation_list_[current_allocation_block_index_].size -= *allocated; |
| 243 | if (*allocated == current.size) { |
| 244 | GetNextAllocationBlock(0); // This block is used up, get the next one. |
| 245 | } |
| 246 | return current.start; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | void CodeRange::FreeRawMemory(void* address, size_t length) { |
| 251 | free_list_.Add(FreeBlock(address, length)); |
| 252 | code_range_->Uncommit(address, length); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | void CodeRange::TearDown() { |
| 257 | delete code_range_; // Frees all memory in the virtual memory range. |
| 258 | code_range_ = NULL; |
| 259 | free_list_.Free(); |
| 260 | allocation_list_.Free(); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | // ----------------------------------------------------------------------------- |
| 265 | // MemoryAllocator |
| 266 | // |
| 267 | int MemoryAllocator::capacity_ = 0; |
| 268 | int MemoryAllocator::size_ = 0; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 269 | int MemoryAllocator::size_executable_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | |
| 271 | VirtualMemory* MemoryAllocator::initial_chunk_ = NULL; |
| 272 | |
| 273 | // 270 is an estimate based on the static default heap size of a pair of 256K |
| 274 | // semispaces and a 64M old generation. |
| 275 | const int kEstimatedNumberOfChunks = 270; |
| 276 | List<MemoryAllocator::ChunkInfo> MemoryAllocator::chunks_( |
| 277 | kEstimatedNumberOfChunks); |
| 278 | List<int> MemoryAllocator::free_chunk_ids_(kEstimatedNumberOfChunks); |
| 279 | int MemoryAllocator::max_nof_chunks_ = 0; |
| 280 | int MemoryAllocator::top_ = 0; |
| 281 | |
| 282 | |
| 283 | void MemoryAllocator::Push(int free_chunk_id) { |
| 284 | ASSERT(max_nof_chunks_ > 0); |
| 285 | ASSERT(top_ < max_nof_chunks_); |
| 286 | free_chunk_ids_[top_++] = free_chunk_id; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | int MemoryAllocator::Pop() { |
| 291 | ASSERT(top_ > 0); |
| 292 | return free_chunk_ids_[--top_]; |
| 293 | } |
| 294 | |
| 295 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 296 | void *executable_memory_histogram = NULL; |
| 297 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | bool MemoryAllocator::Setup(int capacity) { |
| 299 | capacity_ = RoundUp(capacity, Page::kPageSize); |
| 300 | |
| 301 | // Over-estimate the size of chunks_ array. It assumes the expansion of old |
| 302 | // space is always in the unit of a chunk (kChunkSize) except the last |
| 303 | // expansion. |
| 304 | // |
| 305 | // Due to alignment, allocated space might be one page less than required |
| 306 | // number (kPagesPerChunk) of pages for old spaces. |
| 307 | // |
| 308 | // Reserve two chunk ids for semispaces, one for map space, one for old |
| 309 | // space, and one for code space. |
| 310 | max_nof_chunks_ = (capacity_ / (kChunkSize - Page::kPageSize)) + 5; |
| 311 | if (max_nof_chunks_ > kMaxNofChunks) return false; |
| 312 | |
| 313 | size_ = 0; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 314 | size_executable_ = 0; |
| 315 | executable_memory_histogram = |
| 316 | StatsTable::CreateHistogram("V8.ExecutableMemoryMax", 0, MB * 512, 50); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | ChunkInfo info; // uninitialized element. |
| 318 | for (int i = max_nof_chunks_ - 1; i >= 0; i--) { |
| 319 | chunks_.Add(info); |
| 320 | free_chunk_ids_.Add(i); |
| 321 | } |
| 322 | top_ = max_nof_chunks_; |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | void MemoryAllocator::TearDown() { |
| 328 | for (int i = 0; i < max_nof_chunks_; i++) { |
| 329 | if (chunks_[i].address() != NULL) DeleteChunk(i); |
| 330 | } |
| 331 | chunks_.Clear(); |
| 332 | free_chunk_ids_.Clear(); |
| 333 | |
| 334 | if (initial_chunk_ != NULL) { |
| 335 | LOG(DeleteEvent("InitialChunk", initial_chunk_->address())); |
| 336 | delete initial_chunk_; |
| 337 | initial_chunk_ = NULL; |
| 338 | } |
| 339 | |
| 340 | ASSERT(top_ == max_nof_chunks_); // all chunks are free |
| 341 | top_ = 0; |
| 342 | capacity_ = 0; |
| 343 | size_ = 0; |
| 344 | max_nof_chunks_ = 0; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | void* MemoryAllocator::AllocateRawMemory(const size_t requested, |
| 349 | size_t* allocated, |
| 350 | Executability executable) { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 351 | if (size_ + static_cast<size_t>(requested) > static_cast<size_t>(capacity_)) { |
| 352 | return NULL; |
| 353 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | void* mem; |
| 355 | if (executable == EXECUTABLE && CodeRange::exists()) { |
| 356 | mem = CodeRange::AllocateRawMemory(requested, allocated); |
| 357 | } else { |
| 358 | mem = OS::Allocate(requested, allocated, (executable == EXECUTABLE)); |
| 359 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 360 | int alloced = static_cast<int>(*allocated); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | size_ += alloced; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 362 | |
| 363 | if (executable == EXECUTABLE) { |
| 364 | size_executable_ += alloced; |
| 365 | static int size_executable_max_observed_ = 0; |
| 366 | if (size_executable_max_observed_ < size_executable_) { |
| 367 | size_executable_max_observed_ = size_executable_; |
| 368 | StatsTable::AddHistogramSample(executable_memory_histogram, |
| 369 | size_executable_); |
| 370 | } |
| 371 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 372 | #ifdef DEBUG |
| 373 | ZapBlock(reinterpret_cast<Address>(mem), alloced); |
| 374 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 375 | Counters::memory_allocated.Increment(alloced); |
| 376 | return mem; |
| 377 | } |
| 378 | |
| 379 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 380 | void MemoryAllocator::FreeRawMemory(void* mem, |
| 381 | size_t length, |
| 382 | Executability executable) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 383 | #ifdef DEBUG |
| 384 | ZapBlock(reinterpret_cast<Address>(mem), length); |
| 385 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 386 | if (CodeRange::contains(static_cast<Address>(mem))) { |
| 387 | CodeRange::FreeRawMemory(mem, length); |
| 388 | } else { |
| 389 | OS::Free(mem, length); |
| 390 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 391 | Counters::memory_allocated.Decrement(static_cast<int>(length)); |
| 392 | size_ -= static_cast<int>(length); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 393 | if (executable == EXECUTABLE) size_executable_ -= static_cast<int>(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | ASSERT(size_ >= 0); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | void* MemoryAllocator::ReserveInitialChunk(const size_t requested) { |
| 399 | ASSERT(initial_chunk_ == NULL); |
| 400 | |
| 401 | initial_chunk_ = new VirtualMemory(requested); |
| 402 | CHECK(initial_chunk_ != NULL); |
| 403 | if (!initial_chunk_->IsReserved()) { |
| 404 | delete initial_chunk_; |
| 405 | initial_chunk_ = NULL; |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | // We are sure that we have mapped a block of requested addresses. |
| 410 | ASSERT(initial_chunk_->size() == requested); |
| 411 | LOG(NewEvent("InitialChunk", initial_chunk_->address(), requested)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 412 | size_ += static_cast<int>(requested); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 413 | return initial_chunk_->address(); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | static int PagesInChunk(Address start, size_t size) { |
| 418 | // The first page starts on the first page-aligned address from start onward |
| 419 | // and the last page ends on the last page-aligned address before |
| 420 | // start+size. Page::kPageSize is a power of two so we can divide by |
| 421 | // shifting. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 422 | return static_cast<int>((RoundDown(start + size, Page::kPageSize) |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 423 | - RoundUp(start, Page::kPageSize)) >> kPageSizeBits); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | Page* MemoryAllocator::AllocatePages(int requested_pages, int* allocated_pages, |
| 428 | PagedSpace* owner) { |
| 429 | if (requested_pages <= 0) return Page::FromAddress(NULL); |
| 430 | size_t chunk_size = requested_pages * Page::kPageSize; |
| 431 | |
| 432 | // There is not enough space to guarantee the desired number pages can be |
| 433 | // allocated. |
| 434 | if (size_ + static_cast<int>(chunk_size) > capacity_) { |
| 435 | // Request as many pages as we can. |
| 436 | chunk_size = capacity_ - size_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 437 | requested_pages = static_cast<int>(chunk_size >> kPageSizeBits); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 438 | |
| 439 | if (requested_pages <= 0) return Page::FromAddress(NULL); |
| 440 | } |
| 441 | void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable()); |
| 442 | if (chunk == NULL) return Page::FromAddress(NULL); |
| 443 | LOG(NewEvent("PagedChunk", chunk, chunk_size)); |
| 444 | |
| 445 | *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size); |
| 446 | if (*allocated_pages == 0) { |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 447 | FreeRawMemory(chunk, chunk_size, owner->executable()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 448 | LOG(DeleteEvent("PagedChunk", chunk)); |
| 449 | return Page::FromAddress(NULL); |
| 450 | } |
| 451 | |
| 452 | int chunk_id = Pop(); |
| 453 | chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner); |
| 454 | |
| 455 | return InitializePagesInChunk(chunk_id, *allocated_pages, owner); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | Page* MemoryAllocator::CommitPages(Address start, size_t size, |
| 460 | PagedSpace* owner, int* num_pages) { |
| 461 | ASSERT(start != NULL); |
| 462 | *num_pages = PagesInChunk(start, size); |
| 463 | ASSERT(*num_pages > 0); |
| 464 | ASSERT(initial_chunk_ != NULL); |
| 465 | ASSERT(InInitialChunk(start)); |
| 466 | ASSERT(InInitialChunk(start + size - 1)); |
| 467 | if (!initial_chunk_->Commit(start, size, owner->executable() == EXECUTABLE)) { |
| 468 | return Page::FromAddress(NULL); |
| 469 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 470 | #ifdef DEBUG |
| 471 | ZapBlock(start, size); |
| 472 | #endif |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 473 | Counters::memory_allocated.Increment(static_cast<int>(size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 474 | |
| 475 | // So long as we correctly overestimated the number of chunks we should not |
| 476 | // run out of chunk ids. |
| 477 | CHECK(!OutOfChunkIds()); |
| 478 | int chunk_id = Pop(); |
| 479 | chunks_[chunk_id].init(start, size, owner); |
| 480 | return InitializePagesInChunk(chunk_id, *num_pages, owner); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | bool MemoryAllocator::CommitBlock(Address start, |
| 485 | size_t size, |
| 486 | Executability executable) { |
| 487 | ASSERT(start != NULL); |
| 488 | ASSERT(size > 0); |
| 489 | ASSERT(initial_chunk_ != NULL); |
| 490 | ASSERT(InInitialChunk(start)); |
| 491 | ASSERT(InInitialChunk(start + size - 1)); |
| 492 | |
| 493 | if (!initial_chunk_->Commit(start, size, executable)) return false; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 494 | #ifdef DEBUG |
| 495 | ZapBlock(start, size); |
| 496 | #endif |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 497 | Counters::memory_allocated.Increment(static_cast<int>(size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | return true; |
| 499 | } |
| 500 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 501 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | bool MemoryAllocator::UncommitBlock(Address start, size_t size) { |
| 503 | ASSERT(start != NULL); |
| 504 | ASSERT(size > 0); |
| 505 | ASSERT(initial_chunk_ != NULL); |
| 506 | ASSERT(InInitialChunk(start)); |
| 507 | ASSERT(InInitialChunk(start + size - 1)); |
| 508 | |
| 509 | if (!initial_chunk_->Uncommit(start, size)) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 510 | Counters::memory_allocated.Decrement(static_cast<int>(size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 511 | return true; |
| 512 | } |
| 513 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 514 | |
| 515 | void MemoryAllocator::ZapBlock(Address start, size_t size) { |
| 516 | for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) { |
| 517 | Memory::Address_at(start + s) = kZapValue; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk, |
| 523 | PagedSpace* owner) { |
| 524 | ASSERT(IsValidChunk(chunk_id)); |
| 525 | ASSERT(pages_in_chunk > 0); |
| 526 | |
| 527 | Address chunk_start = chunks_[chunk_id].address(); |
| 528 | |
| 529 | Address low = RoundUp(chunk_start, Page::kPageSize); |
| 530 | |
| 531 | #ifdef DEBUG |
| 532 | size_t chunk_size = chunks_[chunk_id].size(); |
| 533 | Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize); |
| 534 | ASSERT(pages_in_chunk <= |
| 535 | ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize)); |
| 536 | #endif |
| 537 | |
| 538 | Address page_addr = low; |
| 539 | for (int i = 0; i < pages_in_chunk; i++) { |
| 540 | Page* p = Page::FromAddress(page_addr); |
| 541 | p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 542 | p->InvalidateWatermark(true); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 543 | p->SetIsLargeObjectPage(false); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 544 | p->SetAllocationWatermark(p->ObjectAreaStart()); |
| 545 | p->SetCachedAllocationWatermark(p->ObjectAreaStart()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 546 | page_addr += Page::kPageSize; |
| 547 | } |
| 548 | |
| 549 | // Set the next page of the last page to 0. |
| 550 | Page* last_page = Page::FromAddress(page_addr - Page::kPageSize); |
| 551 | last_page->opaque_header = OffsetFrom(0) | chunk_id; |
| 552 | |
| 553 | return Page::FromAddress(low); |
| 554 | } |
| 555 | |
| 556 | |
| 557 | Page* MemoryAllocator::FreePages(Page* p) { |
| 558 | if (!p->is_valid()) return p; |
| 559 | |
| 560 | // Find the first page in the same chunk as 'p' |
| 561 | Page* first_page = FindFirstPageInSameChunk(p); |
| 562 | Page* page_to_return = Page::FromAddress(NULL); |
| 563 | |
| 564 | if (p != first_page) { |
| 565 | // Find the last page in the same chunk as 'prev'. |
| 566 | Page* last_page = FindLastPageInSameChunk(p); |
| 567 | first_page = GetNextPage(last_page); // first page in next chunk |
| 568 | |
| 569 | // set the next_page of last_page to NULL |
| 570 | SetNextPage(last_page, Page::FromAddress(NULL)); |
| 571 | page_to_return = p; // return 'p' when exiting |
| 572 | } |
| 573 | |
| 574 | while (first_page->is_valid()) { |
| 575 | int chunk_id = GetChunkId(first_page); |
| 576 | ASSERT(IsValidChunk(chunk_id)); |
| 577 | |
| 578 | // Find the first page of the next chunk before deleting this chunk. |
| 579 | first_page = GetNextPage(FindLastPageInSameChunk(first_page)); |
| 580 | |
| 581 | // Free the current chunk. |
| 582 | DeleteChunk(chunk_id); |
| 583 | } |
| 584 | |
| 585 | return page_to_return; |
| 586 | } |
| 587 | |
| 588 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 589 | void MemoryAllocator::FreeAllPages(PagedSpace* space) { |
| 590 | for (int i = 0, length = chunks_.length(); i < length; i++) { |
| 591 | if (chunks_[i].owner() == space) { |
| 592 | DeleteChunk(i); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 598 | void MemoryAllocator::DeleteChunk(int chunk_id) { |
| 599 | ASSERT(IsValidChunk(chunk_id)); |
| 600 | |
| 601 | ChunkInfo& c = chunks_[chunk_id]; |
| 602 | |
| 603 | // We cannot free a chunk contained in the initial chunk because it was not |
| 604 | // allocated with AllocateRawMemory. Instead we uncommit the virtual |
| 605 | // memory. |
| 606 | if (InInitialChunk(c.address())) { |
| 607 | // TODO(1240712): VirtualMemory::Uncommit has a return value which |
| 608 | // is ignored here. |
| 609 | initial_chunk_->Uncommit(c.address(), c.size()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 610 | Counters::memory_allocated.Decrement(static_cast<int>(c.size())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 611 | } else { |
| 612 | LOG(DeleteEvent("PagedChunk", c.address())); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 613 | FreeRawMemory(c.address(), c.size(), c.owner()->executable()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 614 | } |
| 615 | c.init(NULL, 0, NULL); |
| 616 | Push(chunk_id); |
| 617 | } |
| 618 | |
| 619 | |
| 620 | Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) { |
| 621 | int chunk_id = GetChunkId(p); |
| 622 | ASSERT(IsValidChunk(chunk_id)); |
| 623 | |
| 624 | Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize); |
| 625 | return Page::FromAddress(low); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) { |
| 630 | int chunk_id = GetChunkId(p); |
| 631 | ASSERT(IsValidChunk(chunk_id)); |
| 632 | |
| 633 | Address chunk_start = chunks_[chunk_id].address(); |
| 634 | size_t chunk_size = chunks_[chunk_id].size(); |
| 635 | |
| 636 | Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize); |
| 637 | ASSERT(chunk_start <= p->address() && p->address() < high); |
| 638 | |
| 639 | return Page::FromAddress(high - Page::kPageSize); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | #ifdef DEBUG |
| 644 | void MemoryAllocator::ReportStatistics() { |
| 645 | float pct = static_cast<float>(capacity_ - size_) / capacity_; |
| 646 | PrintF(" capacity: %d, used: %d, available: %%%d\n\n", |
| 647 | capacity_, size_, static_cast<int>(pct*100)); |
| 648 | } |
| 649 | #endif |
| 650 | |
| 651 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 652 | void MemoryAllocator::RelinkPageListInChunkOrder(PagedSpace* space, |
| 653 | Page** first_page, |
| 654 | Page** last_page, |
| 655 | Page** last_page_in_use) { |
| 656 | Page* first = NULL; |
| 657 | Page* last = NULL; |
| 658 | |
| 659 | for (int i = 0, length = chunks_.length(); i < length; i++) { |
| 660 | ChunkInfo& chunk = chunks_[i]; |
| 661 | |
| 662 | if (chunk.owner() == space) { |
| 663 | if (first == NULL) { |
| 664 | Address low = RoundUp(chunk.address(), Page::kPageSize); |
| 665 | first = Page::FromAddress(low); |
| 666 | } |
| 667 | last = RelinkPagesInChunk(i, |
| 668 | chunk.address(), |
| 669 | chunk.size(), |
| 670 | last, |
| 671 | last_page_in_use); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (first_page != NULL) { |
| 676 | *first_page = first; |
| 677 | } |
| 678 | |
| 679 | if (last_page != NULL) { |
| 680 | *last_page = last; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | |
| 685 | Page* MemoryAllocator::RelinkPagesInChunk(int chunk_id, |
| 686 | Address chunk_start, |
| 687 | size_t chunk_size, |
| 688 | Page* prev, |
| 689 | Page** last_page_in_use) { |
| 690 | Address page_addr = RoundUp(chunk_start, Page::kPageSize); |
| 691 | int pages_in_chunk = PagesInChunk(chunk_start, chunk_size); |
| 692 | |
| 693 | if (prev->is_valid()) { |
| 694 | SetNextPage(prev, Page::FromAddress(page_addr)); |
| 695 | } |
| 696 | |
| 697 | for (int i = 0; i < pages_in_chunk; i++) { |
| 698 | Page* p = Page::FromAddress(page_addr); |
| 699 | p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id; |
| 700 | page_addr += Page::kPageSize; |
| 701 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 702 | p->InvalidateWatermark(true); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 703 | if (p->WasInUseBeforeMC()) { |
| 704 | *last_page_in_use = p; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | // Set the next page of the last page to 0. |
| 709 | Page* last_page = Page::FromAddress(page_addr - Page::kPageSize); |
| 710 | last_page->opaque_header = OffsetFrom(0) | chunk_id; |
| 711 | |
| 712 | if (last_page->WasInUseBeforeMC()) { |
| 713 | *last_page_in_use = last_page; |
| 714 | } |
| 715 | |
| 716 | return last_page; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 721 | // ----------------------------------------------------------------------------- |
| 722 | // PagedSpace implementation |
| 723 | |
| 724 | PagedSpace::PagedSpace(int max_capacity, |
| 725 | AllocationSpace id, |
| 726 | Executability executable) |
| 727 | : Space(id, executable) { |
| 728 | max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize) |
| 729 | * Page::kObjectAreaSize; |
| 730 | accounting_stats_.Clear(); |
| 731 | |
| 732 | allocation_info_.top = NULL; |
| 733 | allocation_info_.limit = NULL; |
| 734 | |
| 735 | mc_forwarding_info_.top = NULL; |
| 736 | mc_forwarding_info_.limit = NULL; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | bool PagedSpace::Setup(Address start, size_t size) { |
| 741 | if (HasBeenSetup()) return false; |
| 742 | |
| 743 | int num_pages = 0; |
| 744 | // Try to use the virtual memory range passed to us. If it is too small to |
| 745 | // contain at least one page, ignore it and allocate instead. |
| 746 | int pages_in_chunk = PagesInChunk(start, size); |
| 747 | if (pages_in_chunk > 0) { |
| 748 | first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize), |
| 749 | Page::kPageSize * pages_in_chunk, |
| 750 | this, &num_pages); |
| 751 | } else { |
| 752 | int requested_pages = Min(MemoryAllocator::kPagesPerChunk, |
| 753 | max_capacity_ / Page::kObjectAreaSize); |
| 754 | first_page_ = |
| 755 | MemoryAllocator::AllocatePages(requested_pages, &num_pages, this); |
| 756 | if (!first_page_->is_valid()) return false; |
| 757 | } |
| 758 | |
| 759 | // We are sure that the first page is valid and that we have at least one |
| 760 | // page. |
| 761 | ASSERT(first_page_->is_valid()); |
| 762 | ASSERT(num_pages > 0); |
| 763 | accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize); |
| 764 | ASSERT(Capacity() <= max_capacity_); |
| 765 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 766 | // Sequentially clear region marks in the newly allocated |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 767 | // pages and cache the current last page in the space. |
| 768 | for (Page* p = first_page_; p->is_valid(); p = p->next_page()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 769 | p->SetRegionMarks(Page::kAllRegionsCleanMarks); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | last_page_ = p; |
| 771 | } |
| 772 | |
| 773 | // Use first_page_ for allocation. |
| 774 | SetAllocationInfo(&allocation_info_, first_page_); |
| 775 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 776 | page_list_is_chunk_ordered_ = true; |
| 777 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 778 | return true; |
| 779 | } |
| 780 | |
| 781 | |
| 782 | bool PagedSpace::HasBeenSetup() { |
| 783 | return (Capacity() > 0); |
| 784 | } |
| 785 | |
| 786 | |
| 787 | void PagedSpace::TearDown() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 788 | MemoryAllocator::FreeAllPages(this); |
| 789 | first_page_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 790 | accounting_stats_.Clear(); |
| 791 | } |
| 792 | |
| 793 | |
| 794 | #ifdef ENABLE_HEAP_PROTECTION |
| 795 | |
| 796 | void PagedSpace::Protect() { |
| 797 | Page* page = first_page_; |
| 798 | while (page->is_valid()) { |
| 799 | MemoryAllocator::ProtectChunkFromPage(page); |
| 800 | page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page(); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | |
| 805 | void PagedSpace::Unprotect() { |
| 806 | Page* page = first_page_; |
| 807 | while (page->is_valid()) { |
| 808 | MemoryAllocator::UnprotectChunkFromPage(page); |
| 809 | page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page(); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | #endif |
| 814 | |
| 815 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 816 | void PagedSpace::MarkAllPagesClean() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 817 | PageIterator it(this, PageIterator::ALL_PAGES); |
| 818 | while (it.has_next()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 819 | it.next()->SetRegionMarks(Page::kAllRegionsCleanMarks); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
| 823 | |
| 824 | Object* PagedSpace::FindObject(Address addr) { |
| 825 | // Note: this function can only be called before or after mark-compact GC |
| 826 | // because it accesses map pointers. |
| 827 | ASSERT(!MarkCompactCollector::in_use()); |
| 828 | |
| 829 | if (!Contains(addr)) return Failure::Exception(); |
| 830 | |
| 831 | Page* p = Page::FromAddress(addr); |
| 832 | ASSERT(IsUsed(p)); |
| 833 | Address cur = p->ObjectAreaStart(); |
| 834 | Address end = p->AllocationTop(); |
| 835 | while (cur < end) { |
| 836 | HeapObject* obj = HeapObject::FromAddress(cur); |
| 837 | Address next = cur + obj->Size(); |
| 838 | if ((cur <= addr) && (addr < next)) return obj; |
| 839 | cur = next; |
| 840 | } |
| 841 | |
| 842 | UNREACHABLE(); |
| 843 | return Failure::Exception(); |
| 844 | } |
| 845 | |
| 846 | |
| 847 | bool PagedSpace::IsUsed(Page* page) { |
| 848 | PageIterator it(this, PageIterator::PAGES_IN_USE); |
| 849 | while (it.has_next()) { |
| 850 | if (page == it.next()) return true; |
| 851 | } |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | |
| 856 | void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) { |
| 857 | alloc_info->top = p->ObjectAreaStart(); |
| 858 | alloc_info->limit = p->ObjectAreaEnd(); |
| 859 | ASSERT(alloc_info->VerifyPagedAllocation()); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | void PagedSpace::MCResetRelocationInfo() { |
| 864 | // Set page indexes. |
| 865 | int i = 0; |
| 866 | PageIterator it(this, PageIterator::ALL_PAGES); |
| 867 | while (it.has_next()) { |
| 868 | Page* p = it.next(); |
| 869 | p->mc_page_index = i++; |
| 870 | } |
| 871 | |
| 872 | // Set mc_forwarding_info_ to the first page in the space. |
| 873 | SetAllocationInfo(&mc_forwarding_info_, first_page_); |
| 874 | // All the bytes in the space are 'available'. We will rediscover |
| 875 | // allocated and wasted bytes during GC. |
| 876 | accounting_stats_.Reset(); |
| 877 | } |
| 878 | |
| 879 | |
| 880 | int PagedSpace::MCSpaceOffsetForAddress(Address addr) { |
| 881 | #ifdef DEBUG |
| 882 | // The Contains function considers the address at the beginning of a |
| 883 | // page in the page, MCSpaceOffsetForAddress considers it is in the |
| 884 | // previous page. |
| 885 | if (Page::IsAlignedToPageSize(addr)) { |
| 886 | ASSERT(Contains(addr - kPointerSize)); |
| 887 | } else { |
| 888 | ASSERT(Contains(addr)); |
| 889 | } |
| 890 | #endif |
| 891 | |
| 892 | // If addr is at the end of a page, it belongs to previous page |
| 893 | Page* p = Page::IsAlignedToPageSize(addr) |
| 894 | ? Page::FromAllocationTop(addr) |
| 895 | : Page::FromAddress(addr); |
| 896 | int index = p->mc_page_index; |
| 897 | return (index * Page::kPageSize) + p->Offset(addr); |
| 898 | } |
| 899 | |
| 900 | |
| 901 | // Slow case for reallocating and promoting objects during a compacting |
| 902 | // collection. This function is not space-specific. |
| 903 | HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) { |
| 904 | Page* current_page = TopPageOf(mc_forwarding_info_); |
| 905 | if (!current_page->next_page()->is_valid()) { |
| 906 | if (!Expand(current_page)) { |
| 907 | return NULL; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // There are surely more pages in the space now. |
| 912 | ASSERT(current_page->next_page()->is_valid()); |
| 913 | // We do not add the top of page block for current page to the space's |
| 914 | // free list---the block may contain live objects so we cannot write |
| 915 | // bookkeeping information to it. Instead, we will recover top of page |
| 916 | // blocks when we move objects to their new locations. |
| 917 | // |
| 918 | // We do however write the allocation pointer to the page. The encoding |
| 919 | // of forwarding addresses is as an offset in terms of live bytes, so we |
| 920 | // need quick access to the allocation top of each page to decode |
| 921 | // forwarding addresses. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 922 | current_page->SetAllocationWatermark(mc_forwarding_info_.top); |
| 923 | current_page->next_page()->InvalidateWatermark(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 924 | SetAllocationInfo(&mc_forwarding_info_, current_page->next_page()); |
| 925 | return AllocateLinearly(&mc_forwarding_info_, size_in_bytes); |
| 926 | } |
| 927 | |
| 928 | |
| 929 | bool PagedSpace::Expand(Page* last_page) { |
| 930 | ASSERT(max_capacity_ % Page::kObjectAreaSize == 0); |
| 931 | ASSERT(Capacity() % Page::kObjectAreaSize == 0); |
| 932 | |
| 933 | if (Capacity() == max_capacity_) return false; |
| 934 | |
| 935 | ASSERT(Capacity() < max_capacity_); |
| 936 | // Last page must be valid and its next page is invalid. |
| 937 | ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid()); |
| 938 | |
| 939 | int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize; |
| 940 | if (available_pages <= 0) return false; |
| 941 | |
| 942 | int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk); |
| 943 | Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this); |
| 944 | if (!p->is_valid()) return false; |
| 945 | |
| 946 | accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize); |
| 947 | ASSERT(Capacity() <= max_capacity_); |
| 948 | |
| 949 | MemoryAllocator::SetNextPage(last_page, p); |
| 950 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 951 | // Sequentially clear region marks of new pages and and cache the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 952 | // new last page in the space. |
| 953 | while (p->is_valid()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 954 | p->SetRegionMarks(Page::kAllRegionsCleanMarks); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 955 | last_page_ = p; |
| 956 | p = p->next_page(); |
| 957 | } |
| 958 | |
| 959 | return true; |
| 960 | } |
| 961 | |
| 962 | |
| 963 | #ifdef DEBUG |
| 964 | int PagedSpace::CountTotalPages() { |
| 965 | int count = 0; |
| 966 | for (Page* p = first_page_; p->is_valid(); p = p->next_page()) { |
| 967 | count++; |
| 968 | } |
| 969 | return count; |
| 970 | } |
| 971 | #endif |
| 972 | |
| 973 | |
| 974 | void PagedSpace::Shrink() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 975 | if (!page_list_is_chunk_ordered_) { |
| 976 | // We can't shrink space if pages is not chunk-ordered |
| 977 | // (see comment for class MemoryAllocator for definition). |
| 978 | return; |
| 979 | } |
| 980 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 981 | // Release half of free pages. |
| 982 | Page* top_page = AllocationTopPage(); |
| 983 | ASSERT(top_page->is_valid()); |
| 984 | |
| 985 | // Count the number of pages we would like to free. |
| 986 | int pages_to_free = 0; |
| 987 | for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) { |
| 988 | pages_to_free++; |
| 989 | } |
| 990 | |
| 991 | // Free pages after top_page. |
| 992 | Page* p = MemoryAllocator::FreePages(top_page->next_page()); |
| 993 | MemoryAllocator::SetNextPage(top_page, p); |
| 994 | |
| 995 | // Find out how many pages we failed to free and update last_page_. |
| 996 | // Please note pages can only be freed in whole chunks. |
| 997 | last_page_ = top_page; |
| 998 | for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) { |
| 999 | pages_to_free--; |
| 1000 | last_page_ = p; |
| 1001 | } |
| 1002 | |
| 1003 | accounting_stats_.ShrinkSpace(pages_to_free * Page::kObjectAreaSize); |
| 1004 | ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize); |
| 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | bool PagedSpace::EnsureCapacity(int capacity) { |
| 1009 | if (Capacity() >= capacity) return true; |
| 1010 | |
| 1011 | // Start from the allocation top and loop to the last page in the space. |
| 1012 | Page* last_page = AllocationTopPage(); |
| 1013 | Page* next_page = last_page->next_page(); |
| 1014 | while (next_page->is_valid()) { |
| 1015 | last_page = MemoryAllocator::FindLastPageInSameChunk(next_page); |
| 1016 | next_page = last_page->next_page(); |
| 1017 | } |
| 1018 | |
| 1019 | // Expand the space until it has the required capacity or expansion fails. |
| 1020 | do { |
| 1021 | if (!Expand(last_page)) return false; |
| 1022 | ASSERT(last_page->next_page()->is_valid()); |
| 1023 | last_page = |
| 1024 | MemoryAllocator::FindLastPageInSameChunk(last_page->next_page()); |
| 1025 | } while (Capacity() < capacity); |
| 1026 | |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | #ifdef DEBUG |
| 1032 | void PagedSpace::Print() { } |
| 1033 | #endif |
| 1034 | |
| 1035 | |
| 1036 | #ifdef DEBUG |
| 1037 | // We do not assume that the PageIterator works, because it depends on the |
| 1038 | // invariants we are checking during verification. |
| 1039 | void PagedSpace::Verify(ObjectVisitor* visitor) { |
| 1040 | // The allocation pointer should be valid, and it should be in a page in the |
| 1041 | // space. |
| 1042 | ASSERT(allocation_info_.VerifyPagedAllocation()); |
| 1043 | Page* top_page = Page::FromAllocationTop(allocation_info_.top); |
| 1044 | ASSERT(MemoryAllocator::IsPageInSpace(top_page, this)); |
| 1045 | |
| 1046 | // Loop over all the pages. |
| 1047 | bool above_allocation_top = false; |
| 1048 | Page* current_page = first_page_; |
| 1049 | while (current_page->is_valid()) { |
| 1050 | if (above_allocation_top) { |
| 1051 | // We don't care what's above the allocation top. |
| 1052 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1053 | Address top = current_page->AllocationTop(); |
| 1054 | if (current_page == top_page) { |
| 1055 | ASSERT(top == allocation_info_.top); |
| 1056 | // The next page will be above the allocation top. |
| 1057 | above_allocation_top = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | // It should be packed with objects from the bottom to the top. |
| 1061 | Address current = current_page->ObjectAreaStart(); |
| 1062 | while (current < top) { |
| 1063 | HeapObject* object = HeapObject::FromAddress(current); |
| 1064 | |
| 1065 | // The first word should be a map, and we expect all map pointers to |
| 1066 | // be in map space. |
| 1067 | Map* map = object->map(); |
| 1068 | ASSERT(map->IsMap()); |
| 1069 | ASSERT(Heap::map_space()->Contains(map)); |
| 1070 | |
| 1071 | // Perform space-specific object verification. |
| 1072 | VerifyObject(object); |
| 1073 | |
| 1074 | // The object itself should look OK. |
| 1075 | object->Verify(); |
| 1076 | |
| 1077 | // All the interior pointers should be contained in the heap and |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1078 | // have page regions covering intergenerational references should be |
| 1079 | // marked dirty. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1080 | int size = object->Size(); |
| 1081 | object->IterateBody(map->instance_type(), size, visitor); |
| 1082 | |
| 1083 | current += size; |
| 1084 | } |
| 1085 | |
| 1086 | // The allocation pointer should not be in the middle of an object. |
| 1087 | ASSERT(current == top); |
| 1088 | } |
| 1089 | |
| 1090 | current_page = current_page->next_page(); |
| 1091 | } |
| 1092 | } |
| 1093 | #endif |
| 1094 | |
| 1095 | |
| 1096 | // ----------------------------------------------------------------------------- |
| 1097 | // NewSpace implementation |
| 1098 | |
| 1099 | |
| 1100 | bool NewSpace::Setup(Address start, int size) { |
| 1101 | // Setup new space based on the preallocated memory block defined by |
| 1102 | // start and size. The provided space is divided into two semi-spaces. |
| 1103 | // To support fast containment testing in the new space, the size of |
| 1104 | // this chunk must be a power of two and it must be aligned to its size. |
| 1105 | int initial_semispace_capacity = Heap::InitialSemiSpaceSize(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1106 | int maximum_semispace_capacity = Heap::MaxSemiSpaceSize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1107 | |
| 1108 | ASSERT(initial_semispace_capacity <= maximum_semispace_capacity); |
| 1109 | ASSERT(IsPowerOf2(maximum_semispace_capacity)); |
| 1110 | |
| 1111 | // Allocate and setup the histogram arrays if necessary. |
| 1112 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1113 | allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1); |
| 1114 | promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1); |
| 1115 | |
| 1116 | #define SET_NAME(name) allocated_histogram_[name].set_name(#name); \ |
| 1117 | promoted_histogram_[name].set_name(#name); |
| 1118 | INSTANCE_TYPE_LIST(SET_NAME) |
| 1119 | #undef SET_NAME |
| 1120 | #endif |
| 1121 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1122 | ASSERT(size == 2 * Heap::ReservedSemiSpaceSize()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1123 | ASSERT(IsAddressAligned(start, size, 0)); |
| 1124 | |
| 1125 | if (!to_space_.Setup(start, |
| 1126 | initial_semispace_capacity, |
| 1127 | maximum_semispace_capacity)) { |
| 1128 | return false; |
| 1129 | } |
| 1130 | if (!from_space_.Setup(start + maximum_semispace_capacity, |
| 1131 | initial_semispace_capacity, |
| 1132 | maximum_semispace_capacity)) { |
| 1133 | return false; |
| 1134 | } |
| 1135 | |
| 1136 | start_ = start; |
| 1137 | address_mask_ = ~(size - 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1138 | object_mask_ = address_mask_ | kHeapObjectTagMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1139 | object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag; |
| 1140 | |
| 1141 | allocation_info_.top = to_space_.low(); |
| 1142 | allocation_info_.limit = to_space_.high(); |
| 1143 | mc_forwarding_info_.top = NULL; |
| 1144 | mc_forwarding_info_.limit = NULL; |
| 1145 | |
| 1146 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1147 | return true; |
| 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | void NewSpace::TearDown() { |
| 1152 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1153 | if (allocated_histogram_) { |
| 1154 | DeleteArray(allocated_histogram_); |
| 1155 | allocated_histogram_ = NULL; |
| 1156 | } |
| 1157 | if (promoted_histogram_) { |
| 1158 | DeleteArray(promoted_histogram_); |
| 1159 | promoted_histogram_ = NULL; |
| 1160 | } |
| 1161 | #endif |
| 1162 | |
| 1163 | start_ = NULL; |
| 1164 | allocation_info_.top = NULL; |
| 1165 | allocation_info_.limit = NULL; |
| 1166 | mc_forwarding_info_.top = NULL; |
| 1167 | mc_forwarding_info_.limit = NULL; |
| 1168 | |
| 1169 | to_space_.TearDown(); |
| 1170 | from_space_.TearDown(); |
| 1171 | } |
| 1172 | |
| 1173 | |
| 1174 | #ifdef ENABLE_HEAP_PROTECTION |
| 1175 | |
| 1176 | void NewSpace::Protect() { |
| 1177 | MemoryAllocator::Protect(ToSpaceLow(), Capacity()); |
| 1178 | MemoryAllocator::Protect(FromSpaceLow(), Capacity()); |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | void NewSpace::Unprotect() { |
| 1183 | MemoryAllocator::Unprotect(ToSpaceLow(), Capacity(), |
| 1184 | to_space_.executable()); |
| 1185 | MemoryAllocator::Unprotect(FromSpaceLow(), Capacity(), |
| 1186 | from_space_.executable()); |
| 1187 | } |
| 1188 | |
| 1189 | #endif |
| 1190 | |
| 1191 | |
| 1192 | void NewSpace::Flip() { |
| 1193 | SemiSpace tmp = from_space_; |
| 1194 | from_space_ = to_space_; |
| 1195 | to_space_ = tmp; |
| 1196 | } |
| 1197 | |
| 1198 | |
| 1199 | void NewSpace::Grow() { |
| 1200 | ASSERT(Capacity() < MaximumCapacity()); |
| 1201 | if (to_space_.Grow()) { |
| 1202 | // Only grow from space if we managed to grow to space. |
| 1203 | if (!from_space_.Grow()) { |
| 1204 | // If we managed to grow to space but couldn't grow from space, |
| 1205 | // attempt to shrink to space. |
| 1206 | if (!to_space_.ShrinkTo(from_space_.Capacity())) { |
| 1207 | // We are in an inconsistent state because we could not |
| 1208 | // commit/uncommit memory from new space. |
| 1209 | V8::FatalProcessOutOfMemory("Failed to grow new space."); |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | allocation_info_.limit = to_space_.high(); |
| 1214 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | void NewSpace::Shrink() { |
| 1219 | int new_capacity = Max(InitialCapacity(), 2 * Size()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1220 | int rounded_new_capacity = |
| 1221 | RoundUp(new_capacity, static_cast<int>(OS::AllocateAlignment())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1222 | if (rounded_new_capacity < Capacity() && |
| 1223 | to_space_.ShrinkTo(rounded_new_capacity)) { |
| 1224 | // Only shrink from space if we managed to shrink to space. |
| 1225 | if (!from_space_.ShrinkTo(rounded_new_capacity)) { |
| 1226 | // If we managed to shrink to space but couldn't shrink from |
| 1227 | // space, attempt to grow to space again. |
| 1228 | if (!to_space_.GrowTo(from_space_.Capacity())) { |
| 1229 | // We are in an inconsistent state because we could not |
| 1230 | // commit/uncommit memory from new space. |
| 1231 | V8::FatalProcessOutOfMemory("Failed to shrink new space."); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | allocation_info_.limit = to_space_.high(); |
| 1236 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | void NewSpace::ResetAllocationInfo() { |
| 1241 | allocation_info_.top = to_space_.low(); |
| 1242 | allocation_info_.limit = to_space_.high(); |
| 1243 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | void NewSpace::MCResetRelocationInfo() { |
| 1248 | mc_forwarding_info_.top = from_space_.low(); |
| 1249 | mc_forwarding_info_.limit = from_space_.high(); |
| 1250 | ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_); |
| 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | void NewSpace::MCCommitRelocationInfo() { |
| 1255 | // Assumes that the spaces have been flipped so that mc_forwarding_info_ is |
| 1256 | // valid allocation info for the to space. |
| 1257 | allocation_info_.top = mc_forwarding_info_.top; |
| 1258 | allocation_info_.limit = to_space_.high(); |
| 1259 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | #ifdef DEBUG |
| 1264 | // We do not use the SemispaceIterator because verification doesn't assume |
| 1265 | // that it works (it depends on the invariants we are checking). |
| 1266 | void NewSpace::Verify() { |
| 1267 | // The allocation pointer should be in the space or at the very end. |
| 1268 | ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 1269 | |
| 1270 | // There should be objects packed in from the low address up to the |
| 1271 | // allocation pointer. |
| 1272 | Address current = to_space_.low(); |
| 1273 | while (current < top()) { |
| 1274 | HeapObject* object = HeapObject::FromAddress(current); |
| 1275 | |
| 1276 | // The first word should be a map, and we expect all map pointers to |
| 1277 | // be in map space. |
| 1278 | Map* map = object->map(); |
| 1279 | ASSERT(map->IsMap()); |
| 1280 | ASSERT(Heap::map_space()->Contains(map)); |
| 1281 | |
| 1282 | // The object should not be code or a map. |
| 1283 | ASSERT(!object->IsMap()); |
| 1284 | ASSERT(!object->IsCode()); |
| 1285 | |
| 1286 | // The object itself should look OK. |
| 1287 | object->Verify(); |
| 1288 | |
| 1289 | // All the interior pointers should be contained in the heap. |
| 1290 | VerifyPointersVisitor visitor; |
| 1291 | int size = object->Size(); |
| 1292 | object->IterateBody(map->instance_type(), size, &visitor); |
| 1293 | |
| 1294 | current += size; |
| 1295 | } |
| 1296 | |
| 1297 | // The allocation pointer should not be in the middle of an object. |
| 1298 | ASSERT(current == top()); |
| 1299 | } |
| 1300 | #endif |
| 1301 | |
| 1302 | |
| 1303 | bool SemiSpace::Commit() { |
| 1304 | ASSERT(!is_committed()); |
| 1305 | if (!MemoryAllocator::CommitBlock(start_, capacity_, executable())) { |
| 1306 | return false; |
| 1307 | } |
| 1308 | committed_ = true; |
| 1309 | return true; |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | bool SemiSpace::Uncommit() { |
| 1314 | ASSERT(is_committed()); |
| 1315 | if (!MemoryAllocator::UncommitBlock(start_, capacity_)) { |
| 1316 | return false; |
| 1317 | } |
| 1318 | committed_ = false; |
| 1319 | return true; |
| 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | // ----------------------------------------------------------------------------- |
| 1324 | // SemiSpace implementation |
| 1325 | |
| 1326 | bool SemiSpace::Setup(Address start, |
| 1327 | int initial_capacity, |
| 1328 | int maximum_capacity) { |
| 1329 | // Creates a space in the young generation. The constructor does not |
| 1330 | // allocate memory from the OS. A SemiSpace is given a contiguous chunk of |
| 1331 | // memory of size 'capacity' when set up, and does not grow or shrink |
| 1332 | // otherwise. In the mark-compact collector, the memory region of the from |
| 1333 | // space is used as the marking stack. It requires contiguous memory |
| 1334 | // addresses. |
| 1335 | initial_capacity_ = initial_capacity; |
| 1336 | capacity_ = initial_capacity; |
| 1337 | maximum_capacity_ = maximum_capacity; |
| 1338 | committed_ = false; |
| 1339 | |
| 1340 | start_ = start; |
| 1341 | address_mask_ = ~(maximum_capacity - 1); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1342 | object_mask_ = address_mask_ | kHeapObjectTagMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1343 | object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag; |
| 1344 | age_mark_ = start_; |
| 1345 | |
| 1346 | return Commit(); |
| 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | void SemiSpace::TearDown() { |
| 1351 | start_ = NULL; |
| 1352 | capacity_ = 0; |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | bool SemiSpace::Grow() { |
| 1357 | // Double the semispace size but only up to maximum capacity. |
| 1358 | int maximum_extra = maximum_capacity_ - capacity_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1359 | int extra = Min(RoundUp(capacity_, static_cast<int>(OS::AllocateAlignment())), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1360 | maximum_extra); |
| 1361 | if (!MemoryAllocator::CommitBlock(high(), extra, executable())) { |
| 1362 | return false; |
| 1363 | } |
| 1364 | capacity_ += extra; |
| 1365 | return true; |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | bool SemiSpace::GrowTo(int new_capacity) { |
| 1370 | ASSERT(new_capacity <= maximum_capacity_); |
| 1371 | ASSERT(new_capacity > capacity_); |
| 1372 | size_t delta = new_capacity - capacity_; |
| 1373 | ASSERT(IsAligned(delta, OS::AllocateAlignment())); |
| 1374 | if (!MemoryAllocator::CommitBlock(high(), delta, executable())) { |
| 1375 | return false; |
| 1376 | } |
| 1377 | capacity_ = new_capacity; |
| 1378 | return true; |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | bool SemiSpace::ShrinkTo(int new_capacity) { |
| 1383 | ASSERT(new_capacity >= initial_capacity_); |
| 1384 | ASSERT(new_capacity < capacity_); |
| 1385 | size_t delta = capacity_ - new_capacity; |
| 1386 | ASSERT(IsAligned(delta, OS::AllocateAlignment())); |
| 1387 | if (!MemoryAllocator::UncommitBlock(high() - delta, delta)) { |
| 1388 | return false; |
| 1389 | } |
| 1390 | capacity_ = new_capacity; |
| 1391 | return true; |
| 1392 | } |
| 1393 | |
| 1394 | |
| 1395 | #ifdef DEBUG |
| 1396 | void SemiSpace::Print() { } |
| 1397 | |
| 1398 | |
| 1399 | void SemiSpace::Verify() { } |
| 1400 | #endif |
| 1401 | |
| 1402 | |
| 1403 | // ----------------------------------------------------------------------------- |
| 1404 | // SemiSpaceIterator implementation. |
| 1405 | SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) { |
| 1406 | Initialize(space, space->bottom(), space->top(), NULL); |
| 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, |
| 1411 | HeapObjectCallback size_func) { |
| 1412 | Initialize(space, space->bottom(), space->top(), size_func); |
| 1413 | } |
| 1414 | |
| 1415 | |
| 1416 | SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) { |
| 1417 | Initialize(space, start, space->top(), NULL); |
| 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | void SemiSpaceIterator::Initialize(NewSpace* space, Address start, |
| 1422 | Address end, |
| 1423 | HeapObjectCallback size_func) { |
| 1424 | ASSERT(space->ToSpaceContains(start)); |
| 1425 | ASSERT(space->ToSpaceLow() <= end |
| 1426 | && end <= space->ToSpaceHigh()); |
| 1427 | space_ = &space->to_space_; |
| 1428 | current_ = start; |
| 1429 | limit_ = end; |
| 1430 | size_func_ = size_func; |
| 1431 | } |
| 1432 | |
| 1433 | |
| 1434 | #ifdef DEBUG |
| 1435 | // A static array of histogram info for each type. |
| 1436 | static HistogramInfo heap_histograms[LAST_TYPE+1]; |
| 1437 | static JSObject::SpillInformation js_spill_information; |
| 1438 | |
| 1439 | // heap_histograms is shared, always clear it before using it. |
| 1440 | static void ClearHistograms() { |
| 1441 | // We reset the name each time, though it hasn't changed. |
| 1442 | #define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name); |
| 1443 | INSTANCE_TYPE_LIST(DEF_TYPE_NAME) |
| 1444 | #undef DEF_TYPE_NAME |
| 1445 | |
| 1446 | #define CLEAR_HISTOGRAM(name) heap_histograms[name].clear(); |
| 1447 | INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM) |
| 1448 | #undef CLEAR_HISTOGRAM |
| 1449 | |
| 1450 | js_spill_information.Clear(); |
| 1451 | } |
| 1452 | |
| 1453 | |
| 1454 | static int code_kind_statistics[Code::NUMBER_OF_KINDS]; |
| 1455 | |
| 1456 | |
| 1457 | static void ClearCodeKindStatistics() { |
| 1458 | for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) { |
| 1459 | code_kind_statistics[i] = 0; |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | static void ReportCodeKindStatistics() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1465 | const char* table[Code::NUMBER_OF_KINDS] = { NULL }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1466 | |
| 1467 | #define CASE(name) \ |
| 1468 | case Code::name: table[Code::name] = #name; \ |
| 1469 | break |
| 1470 | |
| 1471 | for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) { |
| 1472 | switch (static_cast<Code::Kind>(i)) { |
| 1473 | CASE(FUNCTION); |
| 1474 | CASE(STUB); |
| 1475 | CASE(BUILTIN); |
| 1476 | CASE(LOAD_IC); |
| 1477 | CASE(KEYED_LOAD_IC); |
| 1478 | CASE(STORE_IC); |
| 1479 | CASE(KEYED_STORE_IC); |
| 1480 | CASE(CALL_IC); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1481 | CASE(KEYED_CALL_IC); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1482 | CASE(BINARY_OP_IC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | #undef CASE |
| 1487 | |
| 1488 | PrintF("\n Code kind histograms: \n"); |
| 1489 | for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) { |
| 1490 | if (code_kind_statistics[i] > 0) { |
| 1491 | PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]); |
| 1492 | } |
| 1493 | } |
| 1494 | PrintF("\n"); |
| 1495 | } |
| 1496 | |
| 1497 | |
| 1498 | static int CollectHistogramInfo(HeapObject* obj) { |
| 1499 | InstanceType type = obj->map()->instance_type(); |
| 1500 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 1501 | ASSERT(heap_histograms[type].name() != NULL); |
| 1502 | heap_histograms[type].increment_number(1); |
| 1503 | heap_histograms[type].increment_bytes(obj->Size()); |
| 1504 | |
| 1505 | if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) { |
| 1506 | JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information); |
| 1507 | } |
| 1508 | |
| 1509 | return obj->Size(); |
| 1510 | } |
| 1511 | |
| 1512 | |
| 1513 | static void ReportHistogram(bool print_spill) { |
| 1514 | PrintF("\n Object Histogram:\n"); |
| 1515 | for (int i = 0; i <= LAST_TYPE; i++) { |
| 1516 | if (heap_histograms[i].number() > 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1517 | PrintF(" %-34s%10d (%10d bytes)\n", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1518 | heap_histograms[i].name(), |
| 1519 | heap_histograms[i].number(), |
| 1520 | heap_histograms[i].bytes()); |
| 1521 | } |
| 1522 | } |
| 1523 | PrintF("\n"); |
| 1524 | |
| 1525 | // Summarize string types. |
| 1526 | int string_number = 0; |
| 1527 | int string_bytes = 0; |
| 1528 | #define INCREMENT(type, size, name, camel_name) \ |
| 1529 | string_number += heap_histograms[type].number(); \ |
| 1530 | string_bytes += heap_histograms[type].bytes(); |
| 1531 | STRING_TYPE_LIST(INCREMENT) |
| 1532 | #undef INCREMENT |
| 1533 | if (string_number > 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1534 | PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1535 | string_bytes); |
| 1536 | } |
| 1537 | |
| 1538 | if (FLAG_collect_heap_spill_statistics && print_spill) { |
| 1539 | js_spill_information.Print(); |
| 1540 | } |
| 1541 | } |
| 1542 | #endif // DEBUG |
| 1543 | |
| 1544 | |
| 1545 | // Support for statistics gathering for --heap-stats and --log-gc. |
| 1546 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1547 | void NewSpace::ClearHistograms() { |
| 1548 | for (int i = 0; i <= LAST_TYPE; i++) { |
| 1549 | allocated_histogram_[i].clear(); |
| 1550 | promoted_histogram_[i].clear(); |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | // Because the copying collector does not touch garbage objects, we iterate |
| 1555 | // the new space before a collection to get a histogram of allocated objects. |
| 1556 | // This only happens (1) when compiled with DEBUG and the --heap-stats flag is |
| 1557 | // set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc |
| 1558 | // flag is set. |
| 1559 | void NewSpace::CollectStatistics() { |
| 1560 | ClearHistograms(); |
| 1561 | SemiSpaceIterator it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1562 | for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) |
| 1563 | RecordAllocation(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 1568 | static void DoReportStatistics(HistogramInfo* info, const char* description) { |
| 1569 | LOG(HeapSampleBeginEvent("NewSpace", description)); |
| 1570 | // Lump all the string types together. |
| 1571 | int string_number = 0; |
| 1572 | int string_bytes = 0; |
| 1573 | #define INCREMENT(type, size, name, camel_name) \ |
| 1574 | string_number += info[type].number(); \ |
| 1575 | string_bytes += info[type].bytes(); |
| 1576 | STRING_TYPE_LIST(INCREMENT) |
| 1577 | #undef INCREMENT |
| 1578 | if (string_number > 0) { |
| 1579 | LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes)); |
| 1580 | } |
| 1581 | |
| 1582 | // Then do the other types. |
| 1583 | for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) { |
| 1584 | if (info[i].number() > 0) { |
| 1585 | LOG(HeapSampleItemEvent(info[i].name(), info[i].number(), |
| 1586 | info[i].bytes())); |
| 1587 | } |
| 1588 | } |
| 1589 | LOG(HeapSampleEndEvent("NewSpace", description)); |
| 1590 | } |
| 1591 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 1592 | |
| 1593 | |
| 1594 | void NewSpace::ReportStatistics() { |
| 1595 | #ifdef DEBUG |
| 1596 | if (FLAG_heap_stats) { |
| 1597 | float pct = static_cast<float>(Available()) / Capacity(); |
| 1598 | PrintF(" capacity: %d, available: %d, %%%d\n", |
| 1599 | Capacity(), Available(), static_cast<int>(pct*100)); |
| 1600 | PrintF("\n Object Histogram:\n"); |
| 1601 | for (int i = 0; i <= LAST_TYPE; i++) { |
| 1602 | if (allocated_histogram_[i].number() > 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1603 | PrintF(" %-34s%10d (%10d bytes)\n", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1604 | allocated_histogram_[i].name(), |
| 1605 | allocated_histogram_[i].number(), |
| 1606 | allocated_histogram_[i].bytes()); |
| 1607 | } |
| 1608 | } |
| 1609 | PrintF("\n"); |
| 1610 | } |
| 1611 | #endif // DEBUG |
| 1612 | |
| 1613 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 1614 | if (FLAG_log_gc) { |
| 1615 | DoReportStatistics(allocated_histogram_, "allocated"); |
| 1616 | DoReportStatistics(promoted_histogram_, "promoted"); |
| 1617 | } |
| 1618 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | void NewSpace::RecordAllocation(HeapObject* obj) { |
| 1623 | InstanceType type = obj->map()->instance_type(); |
| 1624 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 1625 | allocated_histogram_[type].increment_number(1); |
| 1626 | allocated_histogram_[type].increment_bytes(obj->Size()); |
| 1627 | } |
| 1628 | |
| 1629 | |
| 1630 | void NewSpace::RecordPromotion(HeapObject* obj) { |
| 1631 | InstanceType type = obj->map()->instance_type(); |
| 1632 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 1633 | promoted_histogram_[type].increment_number(1); |
| 1634 | promoted_histogram_[type].increment_bytes(obj->Size()); |
| 1635 | } |
| 1636 | #endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1637 | |
| 1638 | |
| 1639 | // ----------------------------------------------------------------------------- |
| 1640 | // Free lists for old object spaces implementation |
| 1641 | |
| 1642 | void FreeListNode::set_size(int size_in_bytes) { |
| 1643 | ASSERT(size_in_bytes > 0); |
| 1644 | ASSERT(IsAligned(size_in_bytes, kPointerSize)); |
| 1645 | |
| 1646 | // We write a map and possibly size information to the block. If the block |
| 1647 | // is big enough to be a ByteArray with at least one extra word (the next |
| 1648 | // pointer), we set its map to be the byte array map and its size to an |
| 1649 | // appropriate array length for the desired size from HeapObject::Size(). |
| 1650 | // If the block is too small (eg, one or two words), to hold both a size |
| 1651 | // field and a next pointer, we give it a filler map that gives it the |
| 1652 | // correct size. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1653 | if (size_in_bytes > ByteArray::kHeaderSize) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1654 | set_map(Heap::raw_unchecked_byte_array_map()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1655 | // Can't use ByteArray::cast because it fails during deserialization. |
| 1656 | ByteArray* this_as_byte_array = reinterpret_cast<ByteArray*>(this); |
| 1657 | this_as_byte_array->set_length(ByteArray::LengthFor(size_in_bytes)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1658 | } else if (size_in_bytes == kPointerSize) { |
| 1659 | set_map(Heap::raw_unchecked_one_pointer_filler_map()); |
| 1660 | } else if (size_in_bytes == 2 * kPointerSize) { |
| 1661 | set_map(Heap::raw_unchecked_two_pointer_filler_map()); |
| 1662 | } else { |
| 1663 | UNREACHABLE(); |
| 1664 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1665 | // We would like to ASSERT(Size() == size_in_bytes) but this would fail during |
| 1666 | // deserialization because the byte array map is not done yet. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | |
| 1670 | Address FreeListNode::next() { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1671 | ASSERT(IsFreeListNode(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1672 | if (map() == Heap::raw_unchecked_byte_array_map()) { |
| 1673 | ASSERT(Size() >= kNextOffset + kPointerSize); |
| 1674 | return Memory::Address_at(address() + kNextOffset); |
| 1675 | } else { |
| 1676 | return Memory::Address_at(address() + kPointerSize); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | |
| 1681 | void FreeListNode::set_next(Address next) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1682 | ASSERT(IsFreeListNode(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1683 | if (map() == Heap::raw_unchecked_byte_array_map()) { |
| 1684 | ASSERT(Size() >= kNextOffset + kPointerSize); |
| 1685 | Memory::Address_at(address() + kNextOffset) = next; |
| 1686 | } else { |
| 1687 | Memory::Address_at(address() + kPointerSize) = next; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) { |
| 1693 | Reset(); |
| 1694 | } |
| 1695 | |
| 1696 | |
| 1697 | void OldSpaceFreeList::Reset() { |
| 1698 | available_ = 0; |
| 1699 | for (int i = 0; i < kFreeListsLength; i++) { |
| 1700 | free_[i].head_node_ = NULL; |
| 1701 | } |
| 1702 | needs_rebuild_ = false; |
| 1703 | finger_ = kHead; |
| 1704 | free_[kHead].next_size_ = kEnd; |
| 1705 | } |
| 1706 | |
| 1707 | |
| 1708 | void OldSpaceFreeList::RebuildSizeList() { |
| 1709 | ASSERT(needs_rebuild_); |
| 1710 | int cur = kHead; |
| 1711 | for (int i = cur + 1; i < kFreeListsLength; i++) { |
| 1712 | if (free_[i].head_node_ != NULL) { |
| 1713 | free_[cur].next_size_ = i; |
| 1714 | cur = i; |
| 1715 | } |
| 1716 | } |
| 1717 | free_[cur].next_size_ = kEnd; |
| 1718 | needs_rebuild_ = false; |
| 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | int OldSpaceFreeList::Free(Address start, int size_in_bytes) { |
| 1723 | #ifdef DEBUG |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1724 | MemoryAllocator::ZapBlock(start, size_in_bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1725 | #endif |
| 1726 | FreeListNode* node = FreeListNode::FromAddress(start); |
| 1727 | node->set_size(size_in_bytes); |
| 1728 | |
| 1729 | // We don't use the freelists in compacting mode. This makes it more like a |
| 1730 | // GC that only has mark-sweep-compact and doesn't have a mark-sweep |
| 1731 | // collector. |
| 1732 | if (FLAG_always_compact) { |
| 1733 | return size_in_bytes; |
| 1734 | } |
| 1735 | |
| 1736 | // Early return to drop too-small blocks on the floor (one or two word |
| 1737 | // blocks cannot hold a map pointer, a size field, and a pointer to the |
| 1738 | // next block in the free list). |
| 1739 | if (size_in_bytes < kMinBlockSize) { |
| 1740 | return size_in_bytes; |
| 1741 | } |
| 1742 | |
| 1743 | // Insert other blocks at the head of an exact free list. |
| 1744 | int index = size_in_bytes >> kPointerSizeLog2; |
| 1745 | node->set_next(free_[index].head_node_); |
| 1746 | free_[index].head_node_ = node->address(); |
| 1747 | available_ += size_in_bytes; |
| 1748 | needs_rebuild_ = true; |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | |
| 1753 | Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) { |
| 1754 | ASSERT(0 < size_in_bytes); |
| 1755 | ASSERT(size_in_bytes <= kMaxBlockSize); |
| 1756 | ASSERT(IsAligned(size_in_bytes, kPointerSize)); |
| 1757 | |
| 1758 | if (needs_rebuild_) RebuildSizeList(); |
| 1759 | int index = size_in_bytes >> kPointerSizeLog2; |
| 1760 | // Check for a perfect fit. |
| 1761 | if (free_[index].head_node_ != NULL) { |
| 1762 | FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_); |
| 1763 | // If this was the last block of its size, remove the size. |
| 1764 | if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index); |
| 1765 | available_ -= size_in_bytes; |
| 1766 | *wasted_bytes = 0; |
| 1767 | ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep. |
| 1768 | return node; |
| 1769 | } |
| 1770 | // Search the size list for the best fit. |
| 1771 | int prev = finger_ < index ? finger_ : kHead; |
| 1772 | int cur = FindSize(index, &prev); |
| 1773 | ASSERT(index < cur); |
| 1774 | if (cur == kEnd) { |
| 1775 | // No large enough size in list. |
| 1776 | *wasted_bytes = 0; |
| 1777 | return Failure::RetryAfterGC(size_in_bytes, owner_); |
| 1778 | } |
| 1779 | ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep. |
| 1780 | int rem = cur - index; |
| 1781 | int rem_bytes = rem << kPointerSizeLog2; |
| 1782 | FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_); |
| 1783 | ASSERT(cur_node->Size() == (cur << kPointerSizeLog2)); |
| 1784 | FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ + |
| 1785 | size_in_bytes); |
| 1786 | // Distinguish the cases prev < rem < cur and rem <= prev < cur |
| 1787 | // to avoid many redundant tests and calls to Insert/RemoveSize. |
| 1788 | if (prev < rem) { |
| 1789 | // Simple case: insert rem between prev and cur. |
| 1790 | finger_ = prev; |
| 1791 | free_[prev].next_size_ = rem; |
| 1792 | // If this was the last block of size cur, remove the size. |
| 1793 | if ((free_[cur].head_node_ = cur_node->next()) == NULL) { |
| 1794 | free_[rem].next_size_ = free_[cur].next_size_; |
| 1795 | } else { |
| 1796 | free_[rem].next_size_ = cur; |
| 1797 | } |
| 1798 | // Add the remainder block. |
| 1799 | rem_node->set_size(rem_bytes); |
| 1800 | rem_node->set_next(free_[rem].head_node_); |
| 1801 | free_[rem].head_node_ = rem_node->address(); |
| 1802 | } else { |
| 1803 | // If this was the last block of size cur, remove the size. |
| 1804 | if ((free_[cur].head_node_ = cur_node->next()) == NULL) { |
| 1805 | finger_ = prev; |
| 1806 | free_[prev].next_size_ = free_[cur].next_size_; |
| 1807 | } |
| 1808 | if (rem_bytes < kMinBlockSize) { |
| 1809 | // Too-small remainder is wasted. |
| 1810 | rem_node->set_size(rem_bytes); |
| 1811 | available_ -= size_in_bytes + rem_bytes; |
| 1812 | *wasted_bytes = rem_bytes; |
| 1813 | return cur_node; |
| 1814 | } |
| 1815 | // Add the remainder block and, if needed, insert its size. |
| 1816 | rem_node->set_size(rem_bytes); |
| 1817 | rem_node->set_next(free_[rem].head_node_); |
| 1818 | free_[rem].head_node_ = rem_node->address(); |
| 1819 | if (rem_node->next() == NULL) InsertSize(rem); |
| 1820 | } |
| 1821 | available_ -= size_in_bytes; |
| 1822 | *wasted_bytes = 0; |
| 1823 | return cur_node; |
| 1824 | } |
| 1825 | |
| 1826 | |
| 1827 | #ifdef DEBUG |
| 1828 | bool OldSpaceFreeList::Contains(FreeListNode* node) { |
| 1829 | for (int i = 0; i < kFreeListsLength; i++) { |
| 1830 | Address cur_addr = free_[i].head_node_; |
| 1831 | while (cur_addr != NULL) { |
| 1832 | FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr); |
| 1833 | if (cur_node == node) return true; |
| 1834 | cur_addr = cur_node->next(); |
| 1835 | } |
| 1836 | } |
| 1837 | return false; |
| 1838 | } |
| 1839 | #endif |
| 1840 | |
| 1841 | |
| 1842 | FixedSizeFreeList::FixedSizeFreeList(AllocationSpace owner, int object_size) |
| 1843 | : owner_(owner), object_size_(object_size) { |
| 1844 | Reset(); |
| 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | void FixedSizeFreeList::Reset() { |
| 1849 | available_ = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1850 | head_ = tail_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | void FixedSizeFreeList::Free(Address start) { |
| 1855 | #ifdef DEBUG |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1856 | MemoryAllocator::ZapBlock(start, object_size_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1857 | #endif |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1858 | // We only use the freelists with mark-sweep. |
| 1859 | ASSERT(!MarkCompactCollector::IsCompacting()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1860 | FreeListNode* node = FreeListNode::FromAddress(start); |
| 1861 | node->set_size(object_size_); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1862 | node->set_next(NULL); |
| 1863 | if (head_ == NULL) { |
| 1864 | tail_ = head_ = node->address(); |
| 1865 | } else { |
| 1866 | FreeListNode::FromAddress(tail_)->set_next(node->address()); |
| 1867 | tail_ = node->address(); |
| 1868 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1869 | available_ += object_size_; |
| 1870 | } |
| 1871 | |
| 1872 | |
| 1873 | Object* FixedSizeFreeList::Allocate() { |
| 1874 | if (head_ == NULL) { |
| 1875 | return Failure::RetryAfterGC(object_size_, owner_); |
| 1876 | } |
| 1877 | |
| 1878 | ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep. |
| 1879 | FreeListNode* node = FreeListNode::FromAddress(head_); |
| 1880 | head_ = node->next(); |
| 1881 | available_ -= object_size_; |
| 1882 | return node; |
| 1883 | } |
| 1884 | |
| 1885 | |
| 1886 | // ----------------------------------------------------------------------------- |
| 1887 | // OldSpace implementation |
| 1888 | |
| 1889 | void OldSpace::PrepareForMarkCompact(bool will_compact) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1890 | // Call prepare of the super class. |
| 1891 | PagedSpace::PrepareForMarkCompact(will_compact); |
| 1892 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1893 | if (will_compact) { |
| 1894 | // Reset relocation info. During a compacting collection, everything in |
| 1895 | // the space is considered 'available' and we will rediscover live data |
| 1896 | // and waste during the collection. |
| 1897 | MCResetRelocationInfo(); |
| 1898 | ASSERT(Available() == Capacity()); |
| 1899 | } else { |
| 1900 | // During a non-compacting collection, everything below the linear |
| 1901 | // allocation pointer is considered allocated (everything above is |
| 1902 | // available) and we will rediscover available and wasted bytes during |
| 1903 | // the collection. |
| 1904 | accounting_stats_.AllocateBytes(free_list_.available()); |
| 1905 | accounting_stats_.FillWastedBytes(Waste()); |
| 1906 | } |
| 1907 | |
| 1908 | // Clear the free list before a full GC---it will be rebuilt afterward. |
| 1909 | free_list_.Reset(); |
| 1910 | } |
| 1911 | |
| 1912 | |
| 1913 | void OldSpace::MCCommitRelocationInfo() { |
| 1914 | // Update fast allocation info. |
| 1915 | allocation_info_.top = mc_forwarding_info_.top; |
| 1916 | allocation_info_.limit = mc_forwarding_info_.limit; |
| 1917 | ASSERT(allocation_info_.VerifyPagedAllocation()); |
| 1918 | |
| 1919 | // The space is compacted and we haven't yet built free lists or |
| 1920 | // wasted any space. |
| 1921 | ASSERT(Waste() == 0); |
| 1922 | ASSERT(AvailableFree() == 0); |
| 1923 | |
| 1924 | // Build the free list for the space. |
| 1925 | int computed_size = 0; |
| 1926 | PageIterator it(this, PageIterator::PAGES_USED_BY_MC); |
| 1927 | while (it.has_next()) { |
| 1928 | Page* p = it.next(); |
| 1929 | // Space below the relocation pointer is allocated. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1930 | computed_size += |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1931 | static_cast<int>(p->AllocationWatermark() - p->ObjectAreaStart()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1932 | if (it.has_next()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1933 | // Free the space at the top of the page. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1934 | int extra_size = |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1935 | static_cast<int>(p->ObjectAreaEnd() - p->AllocationWatermark()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1936 | if (extra_size > 0) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1937 | int wasted_bytes = free_list_.Free(p->AllocationWatermark(), |
| 1938 | extra_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1939 | // The bytes we have just "freed" to add to the free list were |
| 1940 | // already accounted as available. |
| 1941 | accounting_stats_.WasteBytes(wasted_bytes); |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | // Make sure the computed size - based on the used portion of the pages in |
| 1947 | // use - matches the size obtained while computing forwarding addresses. |
| 1948 | ASSERT(computed_size == Size()); |
| 1949 | } |
| 1950 | |
| 1951 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1952 | bool NewSpace::ReserveSpace(int bytes) { |
| 1953 | // We can't reliably unpack a partial snapshot that needs more new space |
| 1954 | // space than the minimum NewSpace size. |
| 1955 | ASSERT(bytes <= InitialCapacity()); |
| 1956 | Address limit = allocation_info_.limit; |
| 1957 | Address top = allocation_info_.top; |
| 1958 | return limit - top >= bytes; |
| 1959 | } |
| 1960 | |
| 1961 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1962 | void PagedSpace::FreePages(Page* prev, Page* last) { |
| 1963 | if (last == AllocationTopPage()) { |
| 1964 | // Pages are already at the end of used pages. |
| 1965 | return; |
| 1966 | } |
| 1967 | |
| 1968 | Page* first = NULL; |
| 1969 | |
| 1970 | // Remove pages from the list. |
| 1971 | if (prev == NULL) { |
| 1972 | first = first_page_; |
| 1973 | first_page_ = last->next_page(); |
| 1974 | } else { |
| 1975 | first = prev->next_page(); |
| 1976 | MemoryAllocator::SetNextPage(prev, last->next_page()); |
| 1977 | } |
| 1978 | |
| 1979 | // Attach it after the last page. |
| 1980 | MemoryAllocator::SetNextPage(last_page_, first); |
| 1981 | last_page_ = last; |
| 1982 | MemoryAllocator::SetNextPage(last, NULL); |
| 1983 | |
| 1984 | // Clean them up. |
| 1985 | do { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1986 | first->InvalidateWatermark(true); |
| 1987 | first->SetAllocationWatermark(first->ObjectAreaStart()); |
| 1988 | first->SetCachedAllocationWatermark(first->ObjectAreaStart()); |
| 1989 | first->SetRegionMarks(Page::kAllRegionsCleanMarks); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1990 | first = first->next_page(); |
| 1991 | } while (first != NULL); |
| 1992 | |
| 1993 | // Order of pages in this space might no longer be consistent with |
| 1994 | // order of pages in chunks. |
| 1995 | page_list_is_chunk_ordered_ = false; |
| 1996 | } |
| 1997 | |
| 1998 | |
| 1999 | void PagedSpace::PrepareForMarkCompact(bool will_compact) { |
| 2000 | if (will_compact) { |
| 2001 | // MarkCompact collector relies on WAS_IN_USE_BEFORE_MC page flag |
| 2002 | // to skip unused pages. Update flag value for all pages in space. |
| 2003 | PageIterator all_pages_iterator(this, PageIterator::ALL_PAGES); |
| 2004 | Page* last_in_use = AllocationTopPage(); |
| 2005 | bool in_use = true; |
| 2006 | |
| 2007 | while (all_pages_iterator.has_next()) { |
| 2008 | Page* p = all_pages_iterator.next(); |
| 2009 | p->SetWasInUseBeforeMC(in_use); |
| 2010 | if (p == last_in_use) { |
| 2011 | // We passed a page containing allocation top. All consequent |
| 2012 | // pages are not used. |
| 2013 | in_use = false; |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | if (!page_list_is_chunk_ordered_) { |
| 2018 | Page* new_last_in_use = Page::FromAddress(NULL); |
| 2019 | MemoryAllocator::RelinkPageListInChunkOrder(this, |
| 2020 | &first_page_, |
| 2021 | &last_page_, |
| 2022 | &new_last_in_use); |
| 2023 | ASSERT(new_last_in_use->is_valid()); |
| 2024 | |
| 2025 | if (new_last_in_use != last_in_use) { |
| 2026 | // Current allocation top points to a page which is now in the middle |
| 2027 | // of page list. We should move allocation top forward to the new last |
| 2028 | // used page so various object iterators will continue to work properly. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2029 | last_in_use->SetAllocationWatermark(last_in_use->AllocationTop()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2030 | |
| 2031 | int size_in_bytes = static_cast<int>(PageAllocationLimit(last_in_use) - |
| 2032 | last_in_use->AllocationTop()); |
| 2033 | |
| 2034 | if (size_in_bytes > 0) { |
| 2035 | // There is still some space left on this page. Create a fake |
| 2036 | // object which will occupy all free space on this page. |
| 2037 | // Otherwise iterators would not be able to scan this page |
| 2038 | // correctly. |
| 2039 | |
| 2040 | Heap::CreateFillerObjectAt(last_in_use->AllocationTop(), |
| 2041 | size_in_bytes); |
| 2042 | } |
| 2043 | |
| 2044 | // New last in use page was in the middle of the list before |
| 2045 | // sorting so it full. |
| 2046 | SetTop(new_last_in_use->AllocationTop()); |
| 2047 | |
| 2048 | ASSERT(AllocationTopPage() == new_last_in_use); |
| 2049 | ASSERT(AllocationTopPage()->WasInUseBeforeMC()); |
| 2050 | } |
| 2051 | |
| 2052 | PageIterator pages_in_use_iterator(this, PageIterator::PAGES_IN_USE); |
| 2053 | while (pages_in_use_iterator.has_next()) { |
| 2054 | Page* p = pages_in_use_iterator.next(); |
| 2055 | if (!p->WasInUseBeforeMC()) { |
| 2056 | // Empty page is in the middle of a sequence of used pages. |
| 2057 | // Create a fake object which will occupy all free space on this page. |
| 2058 | // Otherwise iterators would not be able to scan this page correctly. |
| 2059 | int size_in_bytes = static_cast<int>(PageAllocationLimit(p) - |
| 2060 | p->ObjectAreaStart()); |
| 2061 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2062 | p->SetAllocationWatermark(p->ObjectAreaStart()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2063 | Heap::CreateFillerObjectAt(p->ObjectAreaStart(), size_in_bytes); |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | page_list_is_chunk_ordered_ = true; |
| 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2073 | bool PagedSpace::ReserveSpace(int bytes) { |
| 2074 | Address limit = allocation_info_.limit; |
| 2075 | Address top = allocation_info_.top; |
| 2076 | if (limit - top >= bytes) return true; |
| 2077 | |
| 2078 | // There wasn't enough space in the current page. Lets put the rest |
| 2079 | // of the page on the free list and start a fresh page. |
| 2080 | PutRestOfCurrentPageOnFreeList(TopPageOf(allocation_info_)); |
| 2081 | |
| 2082 | Page* reserved_page = TopPageOf(allocation_info_); |
| 2083 | int bytes_left_to_reserve = bytes; |
| 2084 | while (bytes_left_to_reserve > 0) { |
| 2085 | if (!reserved_page->next_page()->is_valid()) { |
| 2086 | if (Heap::OldGenerationAllocationLimitReached()) return false; |
| 2087 | Expand(reserved_page); |
| 2088 | } |
| 2089 | bytes_left_to_reserve -= Page::kPageSize; |
| 2090 | reserved_page = reserved_page->next_page(); |
| 2091 | if (!reserved_page->is_valid()) return false; |
| 2092 | } |
| 2093 | ASSERT(TopPageOf(allocation_info_)->next_page()->is_valid()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2094 | TopPageOf(allocation_info_)->next_page()->InvalidateWatermark(true); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2095 | SetAllocationInfo(&allocation_info_, |
| 2096 | TopPageOf(allocation_info_)->next_page()); |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
| 2100 | |
| 2101 | // You have to call this last, since the implementation from PagedSpace |
| 2102 | // doesn't know that memory was 'promised' to large object space. |
| 2103 | bool LargeObjectSpace::ReserveSpace(int bytes) { |
| 2104 | return Heap::OldGenerationSpaceAvailable() >= bytes; |
| 2105 | } |
| 2106 | |
| 2107 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2108 | // Slow case for normal allocation. Try in order: (1) allocate in the next |
| 2109 | // page in the space, (2) allocate off the space's free list, (3) expand the |
| 2110 | // space, (4) fail. |
| 2111 | HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) { |
| 2112 | // Linear allocation in this space has failed. If there is another page |
| 2113 | // in the space, move to that page and allocate there. This allocation |
| 2114 | // should succeed (size_in_bytes should not be greater than a page's |
| 2115 | // object area size). |
| 2116 | Page* current_page = TopPageOf(allocation_info_); |
| 2117 | if (current_page->next_page()->is_valid()) { |
| 2118 | return AllocateInNextPage(current_page, size_in_bytes); |
| 2119 | } |
| 2120 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2121 | // There is no next page in this space. Try free list allocation unless that |
| 2122 | // is currently forbidden. |
| 2123 | if (!Heap::linear_allocation()) { |
| 2124 | int wasted_bytes; |
| 2125 | Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes); |
| 2126 | accounting_stats_.WasteBytes(wasted_bytes); |
| 2127 | if (!result->IsFailure()) { |
| 2128 | accounting_stats_.AllocateBytes(size_in_bytes); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2129 | |
| 2130 | HeapObject* obj = HeapObject::cast(result); |
| 2131 | Page* p = Page::FromAddress(obj->address()); |
| 2132 | |
| 2133 | if (obj->address() >= p->AllocationWatermark()) { |
| 2134 | // There should be no hole between the allocation watermark |
| 2135 | // and allocated object address. |
| 2136 | // Memory above the allocation watermark was not swept and |
| 2137 | // might contain garbage pointers to new space. |
| 2138 | ASSERT(obj->address() == p->AllocationWatermark()); |
| 2139 | p->SetAllocationWatermark(obj->address() + size_in_bytes); |
| 2140 | } |
| 2141 | |
| 2142 | return obj; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2143 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | // Free list allocation failed and there is no next page. Fail if we have |
| 2147 | // hit the old generation size limit that should cause a garbage |
| 2148 | // collection. |
| 2149 | if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) { |
| 2150 | return NULL; |
| 2151 | } |
| 2152 | |
| 2153 | // Try to expand the space and allocate in the new next page. |
| 2154 | ASSERT(!current_page->next_page()->is_valid()); |
| 2155 | if (Expand(current_page)) { |
| 2156 | return AllocateInNextPage(current_page, size_in_bytes); |
| 2157 | } |
| 2158 | |
| 2159 | // Finally, fail. |
| 2160 | return NULL; |
| 2161 | } |
| 2162 | |
| 2163 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2164 | void OldSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2165 | current_page->SetAllocationWatermark(allocation_info_.top); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2166 | int free_size = |
| 2167 | static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2168 | if (free_size > 0) { |
| 2169 | int wasted_bytes = free_list_.Free(allocation_info_.top, free_size); |
| 2170 | accounting_stats_.WasteBytes(wasted_bytes); |
| 2171 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | |
| 2175 | void FixedSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2176 | current_page->SetAllocationWatermark(allocation_info_.top); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2177 | int free_size = |
| 2178 | static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top); |
| 2179 | // In the fixed space free list all the free list items have the right size. |
| 2180 | // We use up the rest of the page while preserving this invariant. |
| 2181 | while (free_size >= object_size_in_bytes_) { |
| 2182 | free_list_.Free(allocation_info_.top); |
| 2183 | allocation_info_.top += object_size_in_bytes_; |
| 2184 | free_size -= object_size_in_bytes_; |
| 2185 | accounting_stats_.WasteBytes(object_size_in_bytes_); |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | |
| 2190 | // Add the block at the top of the page to the space's free list, set the |
| 2191 | // allocation info to the next page (assumed to be one), and allocate |
| 2192 | // linearly there. |
| 2193 | HeapObject* OldSpace::AllocateInNextPage(Page* current_page, |
| 2194 | int size_in_bytes) { |
| 2195 | ASSERT(current_page->next_page()->is_valid()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2196 | Page* next_page = current_page->next_page(); |
| 2197 | next_page->ClearGCFields(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2198 | PutRestOfCurrentPageOnFreeList(current_page); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2199 | SetAllocationInfo(&allocation_info_, next_page); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2200 | return AllocateLinearly(&allocation_info_, size_in_bytes); |
| 2201 | } |
| 2202 | |
| 2203 | |
| 2204 | #ifdef DEBUG |
| 2205 | struct CommentStatistic { |
| 2206 | const char* comment; |
| 2207 | int size; |
| 2208 | int count; |
| 2209 | void Clear() { |
| 2210 | comment = NULL; |
| 2211 | size = 0; |
| 2212 | count = 0; |
| 2213 | } |
| 2214 | }; |
| 2215 | |
| 2216 | |
| 2217 | // must be small, since an iteration is used for lookup |
| 2218 | const int kMaxComments = 64; |
| 2219 | static CommentStatistic comments_statistics[kMaxComments+1]; |
| 2220 | |
| 2221 | |
| 2222 | void PagedSpace::ReportCodeStatistics() { |
| 2223 | ReportCodeKindStatistics(); |
| 2224 | PrintF("Code comment statistics (\" [ comment-txt : size/ " |
| 2225 | "count (average)\"):\n"); |
| 2226 | for (int i = 0; i <= kMaxComments; i++) { |
| 2227 | const CommentStatistic& cs = comments_statistics[i]; |
| 2228 | if (cs.size > 0) { |
| 2229 | PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count, |
| 2230 | cs.size/cs.count); |
| 2231 | } |
| 2232 | } |
| 2233 | PrintF("\n"); |
| 2234 | } |
| 2235 | |
| 2236 | |
| 2237 | void PagedSpace::ResetCodeStatistics() { |
| 2238 | ClearCodeKindStatistics(); |
| 2239 | for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear(); |
| 2240 | comments_statistics[kMaxComments].comment = "Unknown"; |
| 2241 | comments_statistics[kMaxComments].size = 0; |
| 2242 | comments_statistics[kMaxComments].count = 0; |
| 2243 | } |
| 2244 | |
| 2245 | |
| 2246 | // Adds comment to 'comment_statistics' table. Performance OK sa long as |
| 2247 | // 'kMaxComments' is small |
| 2248 | static void EnterComment(const char* comment, int delta) { |
| 2249 | // Do not count empty comments |
| 2250 | if (delta <= 0) return; |
| 2251 | CommentStatistic* cs = &comments_statistics[kMaxComments]; |
| 2252 | // Search for a free or matching entry in 'comments_statistics': 'cs' |
| 2253 | // points to result. |
| 2254 | for (int i = 0; i < kMaxComments; i++) { |
| 2255 | if (comments_statistics[i].comment == NULL) { |
| 2256 | cs = &comments_statistics[i]; |
| 2257 | cs->comment = comment; |
| 2258 | break; |
| 2259 | } else if (strcmp(comments_statistics[i].comment, comment) == 0) { |
| 2260 | cs = &comments_statistics[i]; |
| 2261 | break; |
| 2262 | } |
| 2263 | } |
| 2264 | // Update entry for 'comment' |
| 2265 | cs->size += delta; |
| 2266 | cs->count += 1; |
| 2267 | } |
| 2268 | |
| 2269 | |
| 2270 | // Call for each nested comment start (start marked with '[ xxx', end marked |
| 2271 | // with ']'. RelocIterator 'it' must point to a comment reloc info. |
| 2272 | static void CollectCommentStatistics(RelocIterator* it) { |
| 2273 | ASSERT(!it->done()); |
| 2274 | ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT); |
| 2275 | const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data()); |
| 2276 | if (tmp[0] != '[') { |
| 2277 | // Not a nested comment; skip |
| 2278 | return; |
| 2279 | } |
| 2280 | |
| 2281 | // Search for end of nested comment or a new nested comment |
| 2282 | const char* const comment_txt = |
| 2283 | reinterpret_cast<const char*>(it->rinfo()->data()); |
| 2284 | const byte* prev_pc = it->rinfo()->pc(); |
| 2285 | int flat_delta = 0; |
| 2286 | it->next(); |
| 2287 | while (true) { |
| 2288 | // All nested comments must be terminated properly, and therefore exit |
| 2289 | // from loop. |
| 2290 | ASSERT(!it->done()); |
| 2291 | if (it->rinfo()->rmode() == RelocInfo::COMMENT) { |
| 2292 | const char* const txt = |
| 2293 | reinterpret_cast<const char*>(it->rinfo()->data()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2294 | flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2295 | if (txt[0] == ']') break; // End of nested comment |
| 2296 | // A new comment |
| 2297 | CollectCommentStatistics(it); |
| 2298 | // Skip code that was covered with previous comment |
| 2299 | prev_pc = it->rinfo()->pc(); |
| 2300 | } |
| 2301 | it->next(); |
| 2302 | } |
| 2303 | EnterComment(comment_txt, flat_delta); |
| 2304 | } |
| 2305 | |
| 2306 | |
| 2307 | // Collects code size statistics: |
| 2308 | // - by code kind |
| 2309 | // - by code comment |
| 2310 | void PagedSpace::CollectCodeStatistics() { |
| 2311 | HeapObjectIterator obj_it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2312 | for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2313 | if (obj->IsCode()) { |
| 2314 | Code* code = Code::cast(obj); |
| 2315 | code_kind_statistics[code->kind()] += code->Size(); |
| 2316 | RelocIterator it(code); |
| 2317 | int delta = 0; |
| 2318 | const byte* prev_pc = code->instruction_start(); |
| 2319 | while (!it.done()) { |
| 2320 | if (it.rinfo()->rmode() == RelocInfo::COMMENT) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2321 | delta += static_cast<int>(it.rinfo()->pc() - prev_pc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2322 | CollectCommentStatistics(&it); |
| 2323 | prev_pc = it.rinfo()->pc(); |
| 2324 | } |
| 2325 | it.next(); |
| 2326 | } |
| 2327 | |
| 2328 | ASSERT(code->instruction_start() <= prev_pc && |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2329 | prev_pc <= code->instruction_end()); |
| 2330 | delta += static_cast<int>(code->instruction_end() - prev_pc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2331 | EnterComment("NoComment", delta); |
| 2332 | } |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | void OldSpace::ReportStatistics() { |
| 2338 | int pct = Available() * 100 / Capacity(); |
| 2339 | PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n", |
| 2340 | Capacity(), Waste(), Available(), pct); |
| 2341 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2342 | ClearHistograms(); |
| 2343 | HeapObjectIterator obj_it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2344 | for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) |
| 2345 | CollectHistogramInfo(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2346 | ReportHistogram(true); |
| 2347 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2348 | #endif |
| 2349 | |
| 2350 | // ----------------------------------------------------------------------------- |
| 2351 | // FixedSpace implementation |
| 2352 | |
| 2353 | void FixedSpace::PrepareForMarkCompact(bool will_compact) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2354 | // Call prepare of the super class. |
| 2355 | PagedSpace::PrepareForMarkCompact(will_compact); |
| 2356 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2357 | if (will_compact) { |
| 2358 | // Reset relocation info. |
| 2359 | MCResetRelocationInfo(); |
| 2360 | |
| 2361 | // During a compacting collection, everything in the space is considered |
| 2362 | // 'available' (set by the call to MCResetRelocationInfo) and we will |
| 2363 | // rediscover live and wasted bytes during the collection. |
| 2364 | ASSERT(Available() == Capacity()); |
| 2365 | } else { |
| 2366 | // During a non-compacting collection, everything below the linear |
| 2367 | // allocation pointer except wasted top-of-page blocks is considered |
| 2368 | // allocated and we will rediscover available bytes during the |
| 2369 | // collection. |
| 2370 | accounting_stats_.AllocateBytes(free_list_.available()); |
| 2371 | } |
| 2372 | |
| 2373 | // Clear the free list before a full GC---it will be rebuilt afterward. |
| 2374 | free_list_.Reset(); |
| 2375 | } |
| 2376 | |
| 2377 | |
| 2378 | void FixedSpace::MCCommitRelocationInfo() { |
| 2379 | // Update fast allocation info. |
| 2380 | allocation_info_.top = mc_forwarding_info_.top; |
| 2381 | allocation_info_.limit = mc_forwarding_info_.limit; |
| 2382 | ASSERT(allocation_info_.VerifyPagedAllocation()); |
| 2383 | |
| 2384 | // The space is compacted and we haven't yet wasted any space. |
| 2385 | ASSERT(Waste() == 0); |
| 2386 | |
| 2387 | // Update allocation_top of each page in use and compute waste. |
| 2388 | int computed_size = 0; |
| 2389 | PageIterator it(this, PageIterator::PAGES_USED_BY_MC); |
| 2390 | while (it.has_next()) { |
| 2391 | Page* page = it.next(); |
| 2392 | Address page_top = page->AllocationTop(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2393 | computed_size += static_cast<int>(page_top - page->ObjectAreaStart()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2394 | if (it.has_next()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2395 | accounting_stats_.WasteBytes( |
| 2396 | static_cast<int>(page->ObjectAreaEnd() - page_top)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2397 | page->SetAllocationWatermark(page_top); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | // Make sure the computed size - based on the used portion of the |
| 2402 | // pages in use - matches the size we adjust during allocation. |
| 2403 | ASSERT(computed_size == Size()); |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | // Slow case for normal allocation. Try in order: (1) allocate in the next |
| 2408 | // page in the space, (2) allocate off the space's free list, (3) expand the |
| 2409 | // space, (4) fail. |
| 2410 | HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) { |
| 2411 | ASSERT_EQ(object_size_in_bytes_, size_in_bytes); |
| 2412 | // Linear allocation in this space has failed. If there is another page |
| 2413 | // in the space, move to that page and allocate there. This allocation |
| 2414 | // should succeed. |
| 2415 | Page* current_page = TopPageOf(allocation_info_); |
| 2416 | if (current_page->next_page()->is_valid()) { |
| 2417 | return AllocateInNextPage(current_page, size_in_bytes); |
| 2418 | } |
| 2419 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2420 | // There is no next page in this space. Try free list allocation unless |
| 2421 | // that is currently forbidden. The fixed space free list implicitly assumes |
| 2422 | // that all free blocks are of the fixed size. |
| 2423 | if (!Heap::linear_allocation()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2424 | Object* result = free_list_.Allocate(); |
| 2425 | if (!result->IsFailure()) { |
| 2426 | accounting_stats_.AllocateBytes(size_in_bytes); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2427 | HeapObject* obj = HeapObject::cast(result); |
| 2428 | Page* p = Page::FromAddress(obj->address()); |
| 2429 | |
| 2430 | if (obj->address() >= p->AllocationWatermark()) { |
| 2431 | // There should be no hole between the allocation watermark |
| 2432 | // and allocated object address. |
| 2433 | // Memory above the allocation watermark was not swept and |
| 2434 | // might contain garbage pointers to new space. |
| 2435 | ASSERT(obj->address() == p->AllocationWatermark()); |
| 2436 | p->SetAllocationWatermark(obj->address() + size_in_bytes); |
| 2437 | } |
| 2438 | |
| 2439 | return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | // Free list allocation failed and there is no next page. Fail if we have |
| 2444 | // hit the old generation size limit that should cause a garbage |
| 2445 | // collection. |
| 2446 | if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) { |
| 2447 | return NULL; |
| 2448 | } |
| 2449 | |
| 2450 | // Try to expand the space and allocate in the new next page. |
| 2451 | ASSERT(!current_page->next_page()->is_valid()); |
| 2452 | if (Expand(current_page)) { |
| 2453 | return AllocateInNextPage(current_page, size_in_bytes); |
| 2454 | } |
| 2455 | |
| 2456 | // Finally, fail. |
| 2457 | return NULL; |
| 2458 | } |
| 2459 | |
| 2460 | |
| 2461 | // Move to the next page (there is assumed to be one) and allocate there. |
| 2462 | // The top of page block is always wasted, because it is too small to hold a |
| 2463 | // map. |
| 2464 | HeapObject* FixedSpace::AllocateInNextPage(Page* current_page, |
| 2465 | int size_in_bytes) { |
| 2466 | ASSERT(current_page->next_page()->is_valid()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2467 | ASSERT(allocation_info_.top == PageAllocationLimit(current_page)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2468 | ASSERT_EQ(object_size_in_bytes_, size_in_bytes); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2469 | Page* next_page = current_page->next_page(); |
| 2470 | next_page->ClearGCFields(); |
| 2471 | current_page->SetAllocationWatermark(allocation_info_.top); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2472 | accounting_stats_.WasteBytes(page_extra_); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2473 | SetAllocationInfo(&allocation_info_, next_page); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2474 | return AllocateLinearly(&allocation_info_, size_in_bytes); |
| 2475 | } |
| 2476 | |
| 2477 | |
| 2478 | #ifdef DEBUG |
| 2479 | void FixedSpace::ReportStatistics() { |
| 2480 | int pct = Available() * 100 / Capacity(); |
| 2481 | PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n", |
| 2482 | Capacity(), Waste(), Available(), pct); |
| 2483 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2484 | ClearHistograms(); |
| 2485 | HeapObjectIterator obj_it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2486 | for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) |
| 2487 | CollectHistogramInfo(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2488 | ReportHistogram(false); |
| 2489 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2490 | #endif |
| 2491 | |
| 2492 | |
| 2493 | // ----------------------------------------------------------------------------- |
| 2494 | // MapSpace implementation |
| 2495 | |
| 2496 | void MapSpace::PrepareForMarkCompact(bool will_compact) { |
| 2497 | // Call prepare of the super class. |
| 2498 | FixedSpace::PrepareForMarkCompact(will_compact); |
| 2499 | |
| 2500 | if (will_compact) { |
| 2501 | // Initialize map index entry. |
| 2502 | int page_count = 0; |
| 2503 | PageIterator it(this, PageIterator::ALL_PAGES); |
| 2504 | while (it.has_next()) { |
| 2505 | ASSERT_MAP_PAGE_INDEX(page_count); |
| 2506 | |
| 2507 | Page* p = it.next(); |
| 2508 | ASSERT(p->mc_page_index == page_count); |
| 2509 | |
| 2510 | page_addresses_[page_count++] = p->address(); |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | |
| 2516 | #ifdef DEBUG |
| 2517 | void MapSpace::VerifyObject(HeapObject* object) { |
| 2518 | // The object should be a map or a free-list node. |
| 2519 | ASSERT(object->IsMap() || object->IsByteArray()); |
| 2520 | } |
| 2521 | #endif |
| 2522 | |
| 2523 | |
| 2524 | // ----------------------------------------------------------------------------- |
| 2525 | // GlobalPropertyCellSpace implementation |
| 2526 | |
| 2527 | #ifdef DEBUG |
| 2528 | void CellSpace::VerifyObject(HeapObject* object) { |
| 2529 | // The object should be a global object property cell or a free-list node. |
| 2530 | ASSERT(object->IsJSGlobalPropertyCell() || |
| 2531 | object->map() == Heap::two_pointer_filler_map()); |
| 2532 | } |
| 2533 | #endif |
| 2534 | |
| 2535 | |
| 2536 | // ----------------------------------------------------------------------------- |
| 2537 | // LargeObjectIterator |
| 2538 | |
| 2539 | LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) { |
| 2540 | current_ = space->first_chunk_; |
| 2541 | size_func_ = NULL; |
| 2542 | } |
| 2543 | |
| 2544 | |
| 2545 | LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space, |
| 2546 | HeapObjectCallback size_func) { |
| 2547 | current_ = space->first_chunk_; |
| 2548 | size_func_ = size_func; |
| 2549 | } |
| 2550 | |
| 2551 | |
| 2552 | HeapObject* LargeObjectIterator::next() { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2553 | if (current_ == NULL) return NULL; |
| 2554 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2555 | HeapObject* object = current_->GetObject(); |
| 2556 | current_ = current_->next(); |
| 2557 | return object; |
| 2558 | } |
| 2559 | |
| 2560 | |
| 2561 | // ----------------------------------------------------------------------------- |
| 2562 | // LargeObjectChunk |
| 2563 | |
| 2564 | LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes, |
| 2565 | size_t* chunk_size, |
| 2566 | Executability executable) { |
| 2567 | size_t requested = ChunkSizeFor(size_in_bytes); |
| 2568 | void* mem = MemoryAllocator::AllocateRawMemory(requested, |
| 2569 | chunk_size, |
| 2570 | executable); |
| 2571 | if (mem == NULL) return NULL; |
| 2572 | LOG(NewEvent("LargeObjectChunk", mem, *chunk_size)); |
| 2573 | if (*chunk_size < requested) { |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 2574 | MemoryAllocator::FreeRawMemory(mem, *chunk_size, executable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2575 | LOG(DeleteEvent("LargeObjectChunk", mem)); |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | return reinterpret_cast<LargeObjectChunk*>(mem); |
| 2579 | } |
| 2580 | |
| 2581 | |
| 2582 | int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2583 | int os_alignment = static_cast<int>(OS::AllocateAlignment()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2584 | if (os_alignment < Page::kPageSize) |
| 2585 | size_in_bytes += (Page::kPageSize - os_alignment); |
| 2586 | return size_in_bytes + Page::kObjectStartOffset; |
| 2587 | } |
| 2588 | |
| 2589 | // ----------------------------------------------------------------------------- |
| 2590 | // LargeObjectSpace |
| 2591 | |
| 2592 | LargeObjectSpace::LargeObjectSpace(AllocationSpace id) |
| 2593 | : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis |
| 2594 | first_chunk_(NULL), |
| 2595 | size_(0), |
| 2596 | page_count_(0) {} |
| 2597 | |
| 2598 | |
| 2599 | bool LargeObjectSpace::Setup() { |
| 2600 | first_chunk_ = NULL; |
| 2601 | size_ = 0; |
| 2602 | page_count_ = 0; |
| 2603 | return true; |
| 2604 | } |
| 2605 | |
| 2606 | |
| 2607 | void LargeObjectSpace::TearDown() { |
| 2608 | while (first_chunk_ != NULL) { |
| 2609 | LargeObjectChunk* chunk = first_chunk_; |
| 2610 | first_chunk_ = first_chunk_->next(); |
| 2611 | LOG(DeleteEvent("LargeObjectChunk", chunk->address())); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 2612 | Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize)); |
| 2613 | Executability executable = |
| 2614 | page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE; |
| 2615 | MemoryAllocator::FreeRawMemory(chunk->address(), |
| 2616 | chunk->size(), |
| 2617 | executable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | size_ = 0; |
| 2621 | page_count_ = 0; |
| 2622 | } |
| 2623 | |
| 2624 | |
| 2625 | #ifdef ENABLE_HEAP_PROTECTION |
| 2626 | |
| 2627 | void LargeObjectSpace::Protect() { |
| 2628 | LargeObjectChunk* chunk = first_chunk_; |
| 2629 | while (chunk != NULL) { |
| 2630 | MemoryAllocator::Protect(chunk->address(), chunk->size()); |
| 2631 | chunk = chunk->next(); |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | |
| 2636 | void LargeObjectSpace::Unprotect() { |
| 2637 | LargeObjectChunk* chunk = first_chunk_; |
| 2638 | while (chunk != NULL) { |
| 2639 | bool is_code = chunk->GetObject()->IsCode(); |
| 2640 | MemoryAllocator::Unprotect(chunk->address(), chunk->size(), |
| 2641 | is_code ? EXECUTABLE : NOT_EXECUTABLE); |
| 2642 | chunk = chunk->next(); |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | #endif |
| 2647 | |
| 2648 | |
| 2649 | Object* LargeObjectSpace::AllocateRawInternal(int requested_size, |
| 2650 | int object_size, |
| 2651 | Executability executable) { |
| 2652 | ASSERT(0 < object_size && object_size <= requested_size); |
| 2653 | |
| 2654 | // Check if we want to force a GC before growing the old space further. |
| 2655 | // If so, fail the allocation. |
| 2656 | if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) { |
| 2657 | return Failure::RetryAfterGC(requested_size, identity()); |
| 2658 | } |
| 2659 | |
| 2660 | size_t chunk_size; |
| 2661 | LargeObjectChunk* chunk = |
| 2662 | LargeObjectChunk::New(requested_size, &chunk_size, executable); |
| 2663 | if (chunk == NULL) { |
| 2664 | return Failure::RetryAfterGC(requested_size, identity()); |
| 2665 | } |
| 2666 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2667 | size_ += static_cast<int>(chunk_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2668 | page_count_++; |
| 2669 | chunk->set_next(first_chunk_); |
| 2670 | chunk->set_size(chunk_size); |
| 2671 | first_chunk_ = chunk; |
| 2672 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2673 | // Initialize page header. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2674 | Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize)); |
| 2675 | Address object_address = page->ObjectAreaStart(); |
| 2676 | // Clear the low order bit of the second word in the page to flag it as a |
| 2677 | // large object page. If the chunk_size happened to be written there, its |
| 2678 | // low order bit should already be clear. |
| 2679 | ASSERT((chunk_size & 0x1) == 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2680 | page->SetIsLargeObjectPage(true); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 2681 | page->SetIsPageExecutable(executable); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2682 | page->SetRegionMarks(Page::kAllRegionsCleanMarks); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2683 | return HeapObject::FromAddress(object_address); |
| 2684 | } |
| 2685 | |
| 2686 | |
| 2687 | Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) { |
| 2688 | ASSERT(0 < size_in_bytes); |
| 2689 | return AllocateRawInternal(size_in_bytes, |
| 2690 | size_in_bytes, |
| 2691 | EXECUTABLE); |
| 2692 | } |
| 2693 | |
| 2694 | |
| 2695 | Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) { |
| 2696 | ASSERT(0 < size_in_bytes); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2697 | return AllocateRawInternal(size_in_bytes, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2698 | size_in_bytes, |
| 2699 | NOT_EXECUTABLE); |
| 2700 | } |
| 2701 | |
| 2702 | |
| 2703 | Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) { |
| 2704 | ASSERT(0 < size_in_bytes); |
| 2705 | return AllocateRawInternal(size_in_bytes, |
| 2706 | size_in_bytes, |
| 2707 | NOT_EXECUTABLE); |
| 2708 | } |
| 2709 | |
| 2710 | |
| 2711 | // GC support |
| 2712 | Object* LargeObjectSpace::FindObject(Address a) { |
| 2713 | for (LargeObjectChunk* chunk = first_chunk_; |
| 2714 | chunk != NULL; |
| 2715 | chunk = chunk->next()) { |
| 2716 | Address chunk_address = chunk->address(); |
| 2717 | if (chunk_address <= a && a < chunk_address + chunk->size()) { |
| 2718 | return chunk->GetObject(); |
| 2719 | } |
| 2720 | } |
| 2721 | return Failure::Exception(); |
| 2722 | } |
| 2723 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2724 | void LargeObjectSpace::IterateDirtyRegions(ObjectSlotCallback copy_object) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2725 | LargeObjectIterator it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2726 | for (HeapObject* object = it.next(); object != NULL; object = it.next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2727 | // We only have code, sequential strings, or fixed arrays in large |
| 2728 | // object space, and only fixed arrays can possibly contain pointers to |
| 2729 | // the young generation. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2730 | if (object->IsFixedArray()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2731 | Page* page = Page::FromAddress(object->address()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2732 | uint32_t marks = page->GetRegionMarks(); |
| 2733 | uint32_t newmarks = Page::kAllRegionsCleanMarks; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2734 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2735 | if (marks != Page::kAllRegionsCleanMarks) { |
| 2736 | // For a large page a single dirty mark corresponds to several |
| 2737 | // regions (modulo 32). So we treat a large page as a sequence of |
| 2738 | // normal pages of size Page::kPageSize having same dirty marks |
| 2739 | // and subsequently iterate dirty regions on each of these pages. |
| 2740 | Address start = object->address(); |
| 2741 | Address end = page->ObjectAreaEnd(); |
| 2742 | Address object_end = start + object->Size(); |
| 2743 | |
| 2744 | // Iterate regions of the first normal page covering object. |
| 2745 | uint32_t first_region_number = page->GetRegionNumberForAddress(start); |
| 2746 | newmarks |= |
| 2747 | Heap::IterateDirtyRegions(marks >> first_region_number, |
| 2748 | start, |
| 2749 | end, |
| 2750 | &Heap::IteratePointersInDirtyRegion, |
| 2751 | copy_object) << first_region_number; |
| 2752 | |
| 2753 | start = end; |
| 2754 | end = start + Page::kPageSize; |
| 2755 | while (end <= object_end) { |
| 2756 | // Iterate next 32 regions. |
| 2757 | newmarks |= |
| 2758 | Heap::IterateDirtyRegions(marks, |
| 2759 | start, |
| 2760 | end, |
| 2761 | &Heap::IteratePointersInDirtyRegion, |
| 2762 | copy_object); |
| 2763 | start = end; |
| 2764 | end = start + Page::kPageSize; |
| 2765 | } |
| 2766 | |
| 2767 | if (start != object_end) { |
| 2768 | // Iterate the last piece of an object which is less than |
| 2769 | // Page::kPageSize. |
| 2770 | newmarks |= |
| 2771 | Heap::IterateDirtyRegions(marks, |
| 2772 | start, |
| 2773 | object_end, |
| 2774 | &Heap::IteratePointersInDirtyRegion, |
| 2775 | copy_object); |
| 2776 | } |
| 2777 | |
| 2778 | page->SetRegionMarks(newmarks); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2779 | } |
| 2780 | } |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | void LargeObjectSpace::FreeUnmarkedObjects() { |
| 2786 | LargeObjectChunk* previous = NULL; |
| 2787 | LargeObjectChunk* current = first_chunk_; |
| 2788 | while (current != NULL) { |
| 2789 | HeapObject* object = current->GetObject(); |
| 2790 | if (object->IsMarked()) { |
| 2791 | object->ClearMark(); |
| 2792 | MarkCompactCollector::tracer()->decrement_marked_count(); |
| 2793 | previous = current; |
| 2794 | current = current->next(); |
| 2795 | } else { |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 2796 | Page* page = Page::FromAddress(RoundUp(current->address(), |
| 2797 | Page::kPageSize)); |
| 2798 | Executability executable = |
| 2799 | page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2800 | Address chunk_address = current->address(); |
| 2801 | size_t chunk_size = current->size(); |
| 2802 | |
| 2803 | // Cut the chunk out from the chunk list. |
| 2804 | current = current->next(); |
| 2805 | if (previous == NULL) { |
| 2806 | first_chunk_ = current; |
| 2807 | } else { |
| 2808 | previous->set_next(current); |
| 2809 | } |
| 2810 | |
| 2811 | // Free the chunk. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2812 | MarkCompactCollector::ReportDeleteIfNeeded(object); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2813 | size_ -= static_cast<int>(chunk_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2814 | page_count_--; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame^] | 2815 | MemoryAllocator::FreeRawMemory(chunk_address, chunk_size, executable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2816 | LOG(DeleteEvent("LargeObjectChunk", chunk_address)); |
| 2817 | } |
| 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | bool LargeObjectSpace::Contains(HeapObject* object) { |
| 2823 | Address address = object->address(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2824 | if (Heap::new_space()->Contains(address)) { |
| 2825 | return false; |
| 2826 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2827 | Page* page = Page::FromAddress(address); |
| 2828 | |
| 2829 | SLOW_ASSERT(!page->IsLargeObjectPage() |
| 2830 | || !FindObject(address)->IsFailure()); |
| 2831 | |
| 2832 | return page->IsLargeObjectPage(); |
| 2833 | } |
| 2834 | |
| 2835 | |
| 2836 | #ifdef DEBUG |
| 2837 | // We do not assume that the large object iterator works, because it depends |
| 2838 | // on the invariants we are checking during verification. |
| 2839 | void LargeObjectSpace::Verify() { |
| 2840 | for (LargeObjectChunk* chunk = first_chunk_; |
| 2841 | chunk != NULL; |
| 2842 | chunk = chunk->next()) { |
| 2843 | // Each chunk contains an object that starts at the large object page's |
| 2844 | // object area start. |
| 2845 | HeapObject* object = chunk->GetObject(); |
| 2846 | Page* page = Page::FromAddress(object->address()); |
| 2847 | ASSERT(object->address() == page->ObjectAreaStart()); |
| 2848 | |
| 2849 | // The first word should be a map, and we expect all map pointers to be |
| 2850 | // in map space. |
| 2851 | Map* map = object->map(); |
| 2852 | ASSERT(map->IsMap()); |
| 2853 | ASSERT(Heap::map_space()->Contains(map)); |
| 2854 | |
| 2855 | // We have only code, sequential strings, external strings |
| 2856 | // (sequential strings that have been morphed into external |
| 2857 | // strings), fixed arrays, and byte arrays in large object space. |
| 2858 | ASSERT(object->IsCode() || object->IsSeqString() || |
| 2859 | object->IsExternalString() || object->IsFixedArray() || |
| 2860 | object->IsByteArray()); |
| 2861 | |
| 2862 | // The object itself should look OK. |
| 2863 | object->Verify(); |
| 2864 | |
| 2865 | // Byte arrays and strings don't have interior pointers. |
| 2866 | if (object->IsCode()) { |
| 2867 | VerifyPointersVisitor code_visitor; |
| 2868 | object->IterateBody(map->instance_type(), |
| 2869 | object->Size(), |
| 2870 | &code_visitor); |
| 2871 | } else if (object->IsFixedArray()) { |
| 2872 | // We loop over fixed arrays ourselves, rather then using the visitor, |
| 2873 | // because the visitor doesn't support the start/offset iteration |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2874 | // needed for IsRegionDirty. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2875 | FixedArray* array = FixedArray::cast(object); |
| 2876 | for (int j = 0; j < array->length(); j++) { |
| 2877 | Object* element = array->get(j); |
| 2878 | if (element->IsHeapObject()) { |
| 2879 | HeapObject* element_object = HeapObject::cast(element); |
| 2880 | ASSERT(Heap::Contains(element_object)); |
| 2881 | ASSERT(element_object->map()->IsMap()); |
| 2882 | if (Heap::InNewSpace(element_object)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2883 | Address array_addr = object->address(); |
| 2884 | Address element_addr = array_addr + FixedArray::kHeaderSize + |
| 2885 | j * kPointerSize; |
| 2886 | |
| 2887 | ASSERT(Page::FromAddress(array_addr)->IsRegionDirty(element_addr)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2888 | } |
| 2889 | } |
| 2890 | } |
| 2891 | } |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | |
| 2896 | void LargeObjectSpace::Print() { |
| 2897 | LargeObjectIterator it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2898 | for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) { |
| 2899 | obj->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | |
| 2904 | void LargeObjectSpace::ReportStatistics() { |
| 2905 | PrintF(" size: %d\n", size_); |
| 2906 | int num_objects = 0; |
| 2907 | ClearHistograms(); |
| 2908 | LargeObjectIterator it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2909 | for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2910 | num_objects++; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2911 | CollectHistogramInfo(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | PrintF(" number of objects %d\n", num_objects); |
| 2915 | if (num_objects > 0) ReportHistogram(false); |
| 2916 | } |
| 2917 | |
| 2918 | |
| 2919 | void LargeObjectSpace::CollectCodeStatistics() { |
| 2920 | LargeObjectIterator obj_it(this); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2921 | for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2922 | if (obj->IsCode()) { |
| 2923 | Code* code = Code::cast(obj); |
| 2924 | code_kind_statistics[code->kind()] += code->Size(); |
| 2925 | } |
| 2926 | } |
| 2927 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2928 | #endif // DEBUG |
| 2929 | |
| 2930 | } } // namespace v8::internal |