blob: 279e10adc8c2487e691099650d3757b0e769ba5d [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001// Copyright 2006-2008 Google Inc. 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
34namespace v8 { namespace internal {
35
36#ifdef DEBUG
37DECLARE_bool(heap_stats);
38DEFINE_bool(collect_heap_spill_statistics, false,
39 "report heap spill statistics along with heap_stats "
40 "(requires heap_stats)");
41#endif
42
43#ifdef ENABLE_LOGGING_AND_PROFILING
44DECLARE_bool(log_gc);
45#endif
46
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047// For contiguous spaces, top should be in the space (or at the end) and limit
48// should be the end of the space.
49#define ASSERT_SEMISPACE_ALLOCATION_INFO(info, space) \
50 ASSERT((space)->low() <= (info).top \
51 && (info).top <= (space)->high() \
52 && (info).limit == (space)->high())
53
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
55// ----------------------------------------------------------------------------
56// HeapObjectIterator
57
58HeapObjectIterator::HeapObjectIterator(PagedSpace* space) {
59 Initialize(space->bottom(), space->top(), NULL);
60}
61
62
63HeapObjectIterator::HeapObjectIterator(PagedSpace* space,
64 HeapObjectCallback size_func) {
65 Initialize(space->bottom(), space->top(), size_func);
66}
67
68
69HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start) {
70 Initialize(start, space->top(), NULL);
71}
72
73
74HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start,
75 HeapObjectCallback size_func) {
76 Initialize(start, space->top(), size_func);
77}
78
79
80void HeapObjectIterator::Initialize(Address cur, Address end,
81 HeapObjectCallback size_f) {
82 cur_addr_ = cur;
83 end_addr_ = end;
84 end_page_ = Page::FromAllocationTop(end);
85 size_func_ = size_f;
86 Page* p = Page::FromAllocationTop(cur_addr_);
87 cur_limit_ = (p == end_page_) ? end_addr_ : p->AllocationTop();
88
89#ifdef DEBUG
90 Verify();
91#endif
92}
93
94
95bool HeapObjectIterator::HasNextInNextPage() {
96 if (cur_addr_ == end_addr_) return false;
97
98 Page* cur_page = Page::FromAllocationTop(cur_addr_);
99 cur_page = cur_page->next_page();
100 ASSERT(cur_page->is_valid());
101
102 cur_addr_ = cur_page->ObjectAreaStart();
103 cur_limit_ = (cur_page == end_page_) ? end_addr_ : cur_page->AllocationTop();
104
105 ASSERT(cur_addr_ < cur_limit_);
106#ifdef DEBUG
107 Verify();
108#endif
109 return true;
110}
111
112
113#ifdef DEBUG
114void HeapObjectIterator::Verify() {
115 Page* p = Page::FromAllocationTop(cur_addr_);
116 ASSERT(p == Page::FromAllocationTop(cur_limit_));
117 ASSERT(p->Offset(cur_addr_) <= p->Offset(cur_limit_));
118}
119#endif
120
121
122// -----------------------------------------------------------------------------
123// PageIterator
124
125PageIterator::PageIterator(PagedSpace* space, Mode mode) {
126 cur_page_ = space->first_page_;
127 switch (mode) {
128 case PAGES_IN_USE:
129 stop_page_ = space->AllocationTopPage()->next_page();
130 break;
131 case PAGES_USED_BY_MC:
132 stop_page_ = space->MCRelocationTopPage()->next_page();
133 break;
134 case ALL_PAGES:
135 stop_page_ = Page::FromAddress(NULL);
136 break;
137 default:
138 UNREACHABLE();
139 }
140}
141
142
143// -----------------------------------------------------------------------------
144// Page
145
146#ifdef DEBUG
147Page::RSetState Page::rset_state_ = Page::IN_USE;
148#endif
149
150// -----------------------------------------------------------------------------
151// MemoryAllocator
152//
153int MemoryAllocator::capacity_ = 0;
154int MemoryAllocator::size_ = 0;
155
156VirtualMemory* MemoryAllocator::initial_chunk_ = NULL;
157
158// 270 is an estimate based on the static default heap size of a pair of 256K
159// semispaces and a 64M old generation.
160const int kEstimatedNumberOfChunks = 270;
161List<MemoryAllocator::ChunkInfo> MemoryAllocator::chunks_(
162 kEstimatedNumberOfChunks);
163List<int> MemoryAllocator::free_chunk_ids_(kEstimatedNumberOfChunks);
164int MemoryAllocator::max_nof_chunks_ = 0;
165int MemoryAllocator::top_ = 0;
166
167
168void MemoryAllocator::Push(int free_chunk_id) {
169 ASSERT(max_nof_chunks_ > 0);
170 ASSERT(top_ < max_nof_chunks_);
171 free_chunk_ids_[top_++] = free_chunk_id;
172}
173
174
175int MemoryAllocator::Pop() {
176 ASSERT(top_ > 0);
177 return free_chunk_ids_[--top_];
178}
179
180
181bool MemoryAllocator::Setup(int capacity) {
182 capacity_ = RoundUp(capacity, Page::kPageSize);
183
184 // Over-estimate the size of chunks_ array. It assumes the expansion of old
185 // space is always in the unit of a chunk (kChunkSize) except the last
186 // expansion.
187 //
188 // Due to alignment, allocated space might be one page less than required
189 // number (kPagesPerChunk) of pages for old spaces.
190 //
kasper.lund7276f142008-07-30 08:49:36 +0000191 // Reserve two chunk ids for semispaces, one for map space, one for old
192 // space, and one for code space.
193 max_nof_chunks_ = (capacity_ / (kChunkSize - Page::kPageSize)) + 5;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 if (max_nof_chunks_ > kMaxNofChunks) return false;
195
196 size_ = 0;
197 ChunkInfo info; // uninitialized element.
198 for (int i = max_nof_chunks_ - 1; i >= 0; i--) {
199 chunks_.Add(info);
200 free_chunk_ids_.Add(i);
201 }
202 top_ = max_nof_chunks_;
203 return true;
204}
205
206
207void MemoryAllocator::TearDown() {
208 for (int i = 0; i < max_nof_chunks_; i++) {
209 if (chunks_[i].address() != NULL) DeleteChunk(i);
210 }
211 chunks_.Clear();
212 free_chunk_ids_.Clear();
213
214 if (initial_chunk_ != NULL) {
215 LOG(DeleteEvent("InitialChunk", initial_chunk_->address()));
216 delete initial_chunk_;
217 initial_chunk_ = NULL;
218 }
219
220 ASSERT(top_ == max_nof_chunks_); // all chunks are free
221 top_ = 0;
222 capacity_ = 0;
223 size_ = 0;
224 max_nof_chunks_ = 0;
225}
226
227
228void* MemoryAllocator::AllocateRawMemory(const size_t requested,
kasper.lund7276f142008-07-30 08:49:36 +0000229 size_t* allocated,
230 bool executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 if (size_ + static_cast<int>(requested) > capacity_) return NULL;
232
kasper.lund7276f142008-07-30 08:49:36 +0000233 void* mem = OS::Allocate(requested, allocated, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 int alloced = *allocated;
235 size_ += alloced;
236 Counters::memory_allocated.Increment(alloced);
237 return mem;
238}
239
240
241void MemoryAllocator::FreeRawMemory(void* mem, size_t length) {
242 OS::Free(mem, length);
243 Counters::memory_allocated.Decrement(length);
244 size_ -= length;
245 ASSERT(size_ >= 0);
246}
247
248
249void* MemoryAllocator::ReserveInitialChunk(const size_t requested) {
250 ASSERT(initial_chunk_ == NULL);
251
252 initial_chunk_ = new VirtualMemory(requested);
253 CHECK(initial_chunk_ != NULL);
254 if (!initial_chunk_->IsReserved()) {
255 delete initial_chunk_;
256 initial_chunk_ = NULL;
257 return NULL;
258 }
259
260 // We are sure that we have mapped a block of requested addresses.
261 ASSERT(initial_chunk_->size() == requested);
262 LOG(NewEvent("InitialChunk", initial_chunk_->address(), requested));
263 size_ += requested;
264 return initial_chunk_->address();
265}
266
267
268static int PagesInChunk(Address start, size_t size) {
269 // The first page starts on the first page-aligned address from start onward
270 // and the last page ends on the last page-aligned address before
271 // start+size. Page::kPageSize is a power of two so we can divide by
272 // shifting.
273 return (RoundDown(start + size, Page::kPageSize)
274 - RoundUp(start, Page::kPageSize)) >> Page::kPageSizeBits;
275}
276
277
278Page* MemoryAllocator::AllocatePages(int requested_pages, int* allocated_pages,
279 PagedSpace* owner) {
280 if (requested_pages <= 0) return Page::FromAddress(NULL);
281 size_t chunk_size = requested_pages * Page::kPageSize;
282
283 // There is not enough space to guarantee the desired number pages can be
284 // allocated.
285 if (size_ + static_cast<int>(chunk_size) > capacity_) {
286 // Request as many pages as we can.
287 chunk_size = capacity_ - size_;
288 requested_pages = chunk_size >> Page::kPageSizeBits;
289
290 if (requested_pages <= 0) return Page::FromAddress(NULL);
291 }
kasper.lund7276f142008-07-30 08:49:36 +0000292 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 if (chunk == NULL) return Page::FromAddress(NULL);
294 LOG(NewEvent("PagedChunk", chunk, chunk_size));
295
296 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
297 if (*allocated_pages == 0) {
298 FreeRawMemory(chunk, chunk_size);
299 LOG(DeleteEvent("PagedChunk", chunk));
300 return Page::FromAddress(NULL);
301 }
302
303 int chunk_id = Pop();
304 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
305
306 return InitializePagesInChunk(chunk_id, *allocated_pages, owner);
307}
308
309
310Page* MemoryAllocator::CommitPages(Address start, size_t size,
311 PagedSpace* owner, int* num_pages) {
312 ASSERT(start != NULL);
313 *num_pages = PagesInChunk(start, size);
314 ASSERT(*num_pages > 0);
315 ASSERT(initial_chunk_ != NULL);
316 ASSERT(initial_chunk_->address() <= start);
317 ASSERT(start + size <= reinterpret_cast<Address>(initial_chunk_->address())
318 + initial_chunk_->size());
kasper.lund7276f142008-07-30 08:49:36 +0000319 if (!initial_chunk_->Commit(start, size, owner->executable())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 return Page::FromAddress(NULL);
321 }
322 Counters::memory_allocated.Increment(size);
323
324 // So long as we correctly overestimated the number of chunks we should not
325 // run out of chunk ids.
326 CHECK(!OutOfChunkIds());
327 int chunk_id = Pop();
328 chunks_[chunk_id].init(start, size, owner);
329 return InitializePagesInChunk(chunk_id, *num_pages, owner);
330}
331
332
kasper.lund7276f142008-07-30 08:49:36 +0000333bool MemoryAllocator::CommitBlock(Address start,
334 size_t size,
335 bool executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 ASSERT(start != NULL);
337 ASSERT(size > 0);
338 ASSERT(initial_chunk_ != NULL);
339 ASSERT(initial_chunk_->address() <= start);
340 ASSERT(start + size <= reinterpret_cast<Address>(initial_chunk_->address())
341 + initial_chunk_->size());
342
kasper.lund7276f142008-07-30 08:49:36 +0000343 if (!initial_chunk_->Commit(start, size, executable)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 Counters::memory_allocated.Increment(size);
345 return true;
346}
347
348
349Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
350 PagedSpace* owner) {
351 ASSERT(IsValidChunk(chunk_id));
352 ASSERT(pages_in_chunk > 0);
353
354 Address chunk_start = chunks_[chunk_id].address();
355
356 Address low = RoundUp(chunk_start, Page::kPageSize);
357
358#ifdef DEBUG
359 size_t chunk_size = chunks_[chunk_id].size();
360 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
361 ASSERT(pages_in_chunk <=
362 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
363#endif
364
365 Address page_addr = low;
366 for (int i = 0; i < pages_in_chunk; i++) {
367 Page* p = Page::FromAddress(page_addr);
368 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
369 p->is_normal_page = 1;
370 page_addr += Page::kPageSize;
371 }
372
373 // Set the next page of the last page to 0.
374 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
375 last_page->opaque_header = OffsetFrom(0) | chunk_id;
376
377 return Page::FromAddress(low);
378}
379
380
381Page* MemoryAllocator::FreePages(Page* p) {
382 if (!p->is_valid()) return p;
383
384 // Find the first page in the same chunk as 'p'
385 Page* first_page = FindFirstPageInSameChunk(p);
386 Page* page_to_return = Page::FromAddress(NULL);
387
388 if (p != first_page) {
389 // Find the last page in the same chunk as 'prev'.
390 Page* last_page = FindLastPageInSameChunk(p);
391 first_page = GetNextPage(last_page); // first page in next chunk
392
393 // set the next_page of last_page to NULL
394 SetNextPage(last_page, Page::FromAddress(NULL));
395 page_to_return = p; // return 'p' when exiting
396 }
397
398 while (first_page->is_valid()) {
399 int chunk_id = GetChunkId(first_page);
400 ASSERT(IsValidChunk(chunk_id));
401
402 // Find the first page of the next chunk before deleting this chunk.
403 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
404
405 // Free the current chunk.
406 DeleteChunk(chunk_id);
407 }
408
409 return page_to_return;
410}
411
412
413void MemoryAllocator::DeleteChunk(int chunk_id) {
414 ASSERT(IsValidChunk(chunk_id));
415
416 ChunkInfo& c = chunks_[chunk_id];
417
418 // We cannot free a chunk contained in the initial chunk because it was not
419 // allocated with AllocateRawMemory. Instead we uncommit the virtual
420 // memory.
421 bool in_initial_chunk = false;
422 if (initial_chunk_ != NULL) {
423 Address start = static_cast<Address>(initial_chunk_->address());
424 Address end = start + initial_chunk_->size();
425 in_initial_chunk = (start <= c.address()) && (c.address() < end);
426 }
427
428 if (in_initial_chunk) {
429 // TODO(1240712): VirtualMemory::Uncommit has a return value which
430 // is ignored here.
431 initial_chunk_->Uncommit(c.address(), c.size());
432 Counters::memory_allocated.Decrement(c.size());
433 } else {
434 LOG(DeleteEvent("PagedChunk", c.address()));
435 FreeRawMemory(c.address(), c.size());
436 }
437 c.init(NULL, 0, NULL);
438 Push(chunk_id);
439}
440
441
442Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
443 int chunk_id = GetChunkId(p);
444 ASSERT(IsValidChunk(chunk_id));
445
446 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
447 return Page::FromAddress(low);
448}
449
450
451Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
452 int chunk_id = GetChunkId(p);
453 ASSERT(IsValidChunk(chunk_id));
454
455 Address chunk_start = chunks_[chunk_id].address();
456 size_t chunk_size = chunks_[chunk_id].size();
457
458 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
459 ASSERT(chunk_start <= p->address() && p->address() < high);
460
461 return Page::FromAddress(high - Page::kPageSize);
462}
463
464
465#ifdef DEBUG
466void MemoryAllocator::ReportStatistics() {
467 float pct = static_cast<float>(capacity_ - size_) / capacity_;
468 PrintF(" capacity: %d, used: %d, available: %%%d\n\n",
469 capacity_, size_, static_cast<int>(pct*100));
470}
471#endif
472
473
474// -----------------------------------------------------------------------------
475// PagedSpace implementation
476
kasper.lund7276f142008-07-30 08:49:36 +0000477PagedSpace::PagedSpace(int max_capacity, AllocationSpace id, bool executable)
478 : Space(id, executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
480 * Page::kObjectAreaSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 accounting_stats_.Clear();
482
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 allocation_info_.top = NULL;
484 allocation_info_.limit = NULL;
485
486 mc_forwarding_info_.top = NULL;
487 mc_forwarding_info_.limit = NULL;
488}
489
490
491bool PagedSpace::Setup(Address start, size_t size) {
492 if (HasBeenSetup()) return false;
493
494 int num_pages = 0;
495 // Try to use the virtual memory range passed to us. If it is too small to
496 // contain at least one page, ignore it and allocate instead.
497 if (PagesInChunk(start, size) > 0) {
498 first_page_ = MemoryAllocator::CommitPages(start, size, this, &num_pages);
499 } else {
500 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
501 max_capacity_ / Page::kObjectAreaSize);
502 first_page_ =
503 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
504 if (!first_page_->is_valid()) return false;
505 }
506
507 // We are sure that the first page is valid and that we have at least one
508 // page.
509 ASSERT(first_page_->is_valid());
510 ASSERT(num_pages > 0);
511 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
512 ASSERT(Capacity() <= max_capacity_);
513
514 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
515 p->ClearRSet();
516 }
517
518 // Use first_page_ for allocation.
519 SetAllocationInfo(&allocation_info_, first_page_);
520
521 return true;
522}
523
524
525bool PagedSpace::HasBeenSetup() {
526 return (Capacity() > 0);
527}
528
529
530void PagedSpace::TearDown() {
531 first_page_ = MemoryAllocator::FreePages(first_page_);
532 ASSERT(!first_page_->is_valid());
533
534 accounting_stats_.Clear();
535}
536
537
538void PagedSpace::ClearRSet() {
539 PageIterator it(this, PageIterator::ALL_PAGES);
540 while (it.has_next()) {
541 it.next()->ClearRSet();
542 }
543}
544
545
546Object* PagedSpace::FindObject(Address addr) {
547#ifdef DEBUG
548 // Note: this function can only be called before or after mark-compact GC
549 // because it accesses map pointers.
550 ASSERT(!MarkCompactCollector::in_use());
551#endif
552
553 if (!Contains(addr)) return Failure::Exception();
554
555 Page* p = Page::FromAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +0000556 ASSERT(IsUsed(p));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 Address cur = p->ObjectAreaStart();
558 Address end = p->AllocationTop();
559 while (cur < end) {
560 HeapObject* obj = HeapObject::FromAddress(cur);
561 Address next = cur + obj->Size();
562 if ((cur <= addr) && (addr < next)) return obj;
563 cur = next;
564 }
565
kasper.lund7276f142008-07-30 08:49:36 +0000566 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 return Failure::Exception();
568}
569
570
kasper.lund7276f142008-07-30 08:49:36 +0000571bool PagedSpace::IsUsed(Page* page) {
572 PageIterator it(this, PageIterator::PAGES_IN_USE);
573 while (it.has_next()) {
574 if (page == it.next()) return true;
575 }
576 return false;
577}
578
579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
581 alloc_info->top = p->ObjectAreaStart();
582 alloc_info->limit = p->ObjectAreaEnd();
kasper.lund7276f142008-07-30 08:49:36 +0000583 ASSERT(alloc_info->VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584}
585
586
587void PagedSpace::MCResetRelocationInfo() {
588 // Set page indexes.
589 int i = 0;
590 PageIterator it(this, PageIterator::ALL_PAGES);
591 while (it.has_next()) {
592 Page* p = it.next();
593 p->mc_page_index = i++;
594 }
595
596 // Set mc_forwarding_info_ to the first page in the space.
597 SetAllocationInfo(&mc_forwarding_info_, first_page_);
598 // All the bytes in the space are 'available'. We will rediscover
599 // allocated and wasted bytes during GC.
600 accounting_stats_.Reset();
601}
602
603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
605#ifdef DEBUG
606 // The Contains function considers the address at the beginning of a
607 // page in the page, MCSpaceOffsetForAddress considers it is in the
608 // previous page.
609 if (Page::IsAlignedToPageSize(addr)) {
610 ASSERT(Contains(addr - kPointerSize));
611 } else {
612 ASSERT(Contains(addr));
613 }
614#endif
615
616 // If addr is at the end of a page, it belongs to previous page
617 Page* p = Page::IsAlignedToPageSize(addr)
618 ? Page::FromAllocationTop(addr)
619 : Page::FromAddress(addr);
620 int index = p->mc_page_index;
621 return (index * Page::kPageSize) + p->Offset(addr);
622}
623
624
kasper.lund7276f142008-07-30 08:49:36 +0000625// Slow case for reallocating and promoting objects during a compacting
626// collection. This function is not space-specific.
627HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
628 Page* current_page = TopPageOf(mc_forwarding_info_);
629 if (!current_page->next_page()->is_valid()) {
630 if (!Expand(current_page)) {
631 return NULL;
632 }
633 }
634
635 // There are surely more pages in the space now.
636 ASSERT(current_page->next_page()->is_valid());
637 // We do not add the top of page block for current page to the space's
638 // free list---the block may contain live objects so we cannot write
639 // bookkeeping information to it. Instead, we will recover top of page
640 // blocks when we move objects to their new locations.
641 //
642 // We do however write the allocation pointer to the page. The encoding
643 // of forwarding addresses is as an offset in terms of live bytes, so we
644 // need quick access to the allocation top of each page to decode
645 // forwarding addresses.
646 current_page->mc_relocation_top = mc_forwarding_info_.top;
647 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
648 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
649}
650
651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652bool PagedSpace::Expand(Page* last_page) {
653 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
654 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
655
656 if (Capacity() == max_capacity_) return false;
657
658 ASSERT(Capacity() < max_capacity_);
659 // Last page must be valid and its next page is invalid.
660 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
661
662 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
663 if (available_pages <= 0) return false;
664
665 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
666 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
667 if (!p->is_valid()) return false;
668
669 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
670 ASSERT(Capacity() <= max_capacity_);
671
672 MemoryAllocator::SetNextPage(last_page, p);
673
674 // Clear remembered set of new pages.
675 while (p->is_valid()) {
676 p->ClearRSet();
677 p = p->next_page();
678 }
679
680 return true;
681}
682
683
684#ifdef DEBUG
685int PagedSpace::CountTotalPages() {
686 int count = 0;
687 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
688 count++;
689 }
690 return count;
691}
692#endif
693
694
695void PagedSpace::Shrink() {
696 // Release half of free pages.
697 Page* top_page = AllocationTopPage();
698 ASSERT(top_page->is_valid());
699
700 // Loop over the pages from the top page to the end of the space to count
701 // the number of pages to keep and find the last page to keep.
702 int free_pages = 0;
703 int pages_to_keep = 0; // Of the free pages.
704 Page* last_page_to_keep = top_page;
705 Page* current_page = top_page->next_page();
706 // Loop over the pages to the end of the space.
707 while (current_page->is_valid()) {
kasper.lund7276f142008-07-30 08:49:36 +0000708 // Advance last_page_to_keep every other step to end up at the midpoint.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 if ((free_pages & 0x1) == 1) {
710 pages_to_keep++;
711 last_page_to_keep = last_page_to_keep->next_page();
712 }
713 free_pages++;
714 current_page = current_page->next_page();
715 }
716
717 // Free pages after last_page_to_keep, and adjust the next_page link.
718 Page* p = MemoryAllocator::FreePages(last_page_to_keep->next_page());
719 MemoryAllocator::SetNextPage(last_page_to_keep, p);
720
721 // Since pages are only freed in whole chunks, we may have kept more than
722 // pages_to_keep.
723 while (p->is_valid()) {
724 pages_to_keep++;
725 p = p->next_page();
726 }
727
728 // The difference between free_pages and pages_to_keep is the number of
729 // pages actually freed.
730 ASSERT(pages_to_keep <= free_pages);
731 int bytes_freed = (free_pages - pages_to_keep) * Page::kObjectAreaSize;
732 accounting_stats_.ShrinkSpace(bytes_freed);
733
734 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
735}
736
737
738bool PagedSpace::EnsureCapacity(int capacity) {
739 if (Capacity() >= capacity) return true;
740
741 // Start from the allocation top and loop to the last page in the space.
742 Page* last_page = AllocationTopPage();
743 Page* next_page = last_page->next_page();
744 while (next_page->is_valid()) {
745 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
746 next_page = last_page->next_page();
747 }
748
749 // Expand the space until it has the required capacity or expansion fails.
750 do {
751 if (!Expand(last_page)) return false;
752 ASSERT(last_page->next_page()->is_valid());
753 last_page =
754 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
755 } while (Capacity() < capacity);
756
757 return true;
758}
759
760
761#ifdef DEBUG
762void PagedSpace::Print() { }
763#endif
764
765
766// -----------------------------------------------------------------------------
767// NewSpace implementation
768
769NewSpace::NewSpace(int initial_semispace_capacity,
kasper.lund7276f142008-07-30 08:49:36 +0000770 int maximum_semispace_capacity,
771 AllocationSpace id,
772 bool executable)
773 : Space(id, executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
775 ASSERT(IsPowerOf2(maximum_semispace_capacity));
776 maximum_capacity_ = maximum_semispace_capacity;
777 capacity_ = initial_semispace_capacity;
kasper.lund7276f142008-07-30 08:49:36 +0000778 to_space_ = new SemiSpace(capacity_, maximum_capacity_, id, executable);
779 from_space_ = new SemiSpace(capacity_, maximum_capacity_, id, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780
781 // Allocate and setup the histogram arrays if necessary.
782#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
783 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
784 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
785
786#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
787 promoted_histogram_[name].set_name(#name);
788 INSTANCE_TYPE_LIST(SET_NAME)
789#undef SET_NAME
790#endif
791}
792
793
794bool NewSpace::Setup(Address start, int size) {
795 ASSERT(size == 2 * maximum_capacity_);
796 ASSERT(IsAddressAligned(start, size, 0));
797
798 if (to_space_ == NULL
799 || !to_space_->Setup(start, maximum_capacity_)) {
800 return false;
801 }
802 if (from_space_ == NULL
803 || !from_space_->Setup(start + maximum_capacity_, maximum_capacity_)) {
804 return false;
805 }
806
807 start_ = start;
808 address_mask_ = ~(size - 1);
809 object_mask_ = address_mask_ | kHeapObjectTag;
810 object_expected_ = reinterpret_cast<uint32_t>(start) | kHeapObjectTag;
811
812 allocation_info_.top = to_space_->low();
813 allocation_info_.limit = to_space_->high();
814 mc_forwarding_info_.top = NULL;
815 mc_forwarding_info_.limit = NULL;
816
817 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
818 return true;
819}
820
821
822void NewSpace::TearDown() {
823#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
824 if (allocated_histogram_) {
825 DeleteArray(allocated_histogram_);
826 allocated_histogram_ = NULL;
827 }
828 if (promoted_histogram_) {
829 DeleteArray(promoted_histogram_);
830 promoted_histogram_ = NULL;
831 }
832#endif
833
834 start_ = NULL;
835 capacity_ = 0;
836 allocation_info_.top = NULL;
837 allocation_info_.limit = NULL;
838 mc_forwarding_info_.top = NULL;
839 mc_forwarding_info_.limit = NULL;
840
841 if (to_space_ != NULL) {
842 to_space_->TearDown();
843 delete to_space_;
844 to_space_ = NULL;
845 }
846
847 if (from_space_ != NULL) {
848 from_space_->TearDown();
849 delete from_space_;
850 from_space_ = NULL;
851 }
852}
853
854
855void NewSpace::Flip() {
856 SemiSpace* tmp = from_space_;
857 from_space_ = to_space_;
858 to_space_ = tmp;
859}
860
861
862bool NewSpace::Double() {
863 ASSERT(capacity_ <= maximum_capacity_ / 2);
864 // TODO(1240712): Failure to double the from space can result in
865 // semispaces of different sizes. In the event of that failure, the
866 // to space doubling should be rolled back before returning false.
867 if (!to_space_->Double() || !from_space_->Double()) return false;
868 capacity_ *= 2;
869 allocation_info_.limit = to_space_->high();
870 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
871 return true;
872}
873
874
875void NewSpace::ResetAllocationInfo() {
876 allocation_info_.top = to_space_->low();
877 allocation_info_.limit = to_space_->high();
878 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
879}
880
881
882void NewSpace::MCResetRelocationInfo() {
883 mc_forwarding_info_.top = from_space_->low();
884 mc_forwarding_info_.limit = from_space_->high();
885 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
886}
887
888
889void NewSpace::MCCommitRelocationInfo() {
890 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
891 // valid allocation info for the to space.
892 allocation_info_.top = mc_forwarding_info_.top;
893 allocation_info_.limit = to_space_->high();
894 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
895}
896
897
898#ifdef DEBUG
899// We do not use the SemispaceIterator because verification doesn't assume
900// that it works (it depends on the invariants we are checking).
901void NewSpace::Verify() {
902 // The allocation pointer should be in the space or at the very end.
903 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
904
905 // There should be objects packed in from the low address up to the
906 // allocation pointer.
907 Address current = to_space_->low();
908 while (current < top()) {
909 HeapObject* object = HeapObject::FromAddress(current);
910
911 // The first word should be a map, and we expect all map pointers to
912 // be in map space.
913 Map* map = object->map();
914 ASSERT(map->IsMap());
915 ASSERT(Heap::map_space()->Contains(map));
916
917 // The object should not be code or a map.
918 ASSERT(!object->IsMap());
919 ASSERT(!object->IsCode());
920
921 // The object itself should look OK.
922 object->Verify();
923
924 // All the interior pointers should be contained in the heap.
925 VerifyPointersVisitor visitor;
926 int size = object->Size();
927 object->IterateBody(map->instance_type(), size, &visitor);
928
929 current += size;
930 }
931
932 // The allocation pointer should not be in the middle of an object.
933 ASSERT(current == top());
934}
935#endif
936
937
938// -----------------------------------------------------------------------------
939// SemiSpace implementation
940
kasper.lund7276f142008-07-30 08:49:36 +0000941SemiSpace::SemiSpace(int initial_capacity,
942 int maximum_capacity,
943 AllocationSpace id,
944 bool executable)
945 : Space(id, executable), capacity_(initial_capacity),
946 maximum_capacity_(maximum_capacity), start_(NULL), age_mark_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947}
948
949
950bool SemiSpace::Setup(Address start, int size) {
951 ASSERT(size == maximum_capacity_);
kasper.lund7276f142008-07-30 08:49:36 +0000952 if (!MemoryAllocator::CommitBlock(start, capacity_, executable())) {
953 return false;
954 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955
956 start_ = start;
957 address_mask_ = ~(size - 1);
958 object_mask_ = address_mask_ | kHeapObjectTag;
959 object_expected_ = reinterpret_cast<uint32_t>(start) | kHeapObjectTag;
960
961 age_mark_ = start_;
962 return true;
963}
964
965
966void SemiSpace::TearDown() {
967 start_ = NULL;
968 capacity_ = 0;
969}
970
971
972bool SemiSpace::Double() {
kasper.lund7276f142008-07-30 08:49:36 +0000973 if (!MemoryAllocator::CommitBlock(high(), capacity_, executable())) {
974 return false;
975 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 capacity_ *= 2;
977 return true;
978}
979
980
981#ifdef DEBUG
982void SemiSpace::Print() { }
983#endif
984
985
986// -----------------------------------------------------------------------------
987// SemiSpaceIterator implementation.
988SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
989 Initialize(space, space->bottom(), space->top(), NULL);
990}
991
992
993SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
994 HeapObjectCallback size_func) {
995 Initialize(space, space->bottom(), space->top(), size_func);
996}
997
998
999SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1000 Initialize(space, start, space->top(), NULL);
1001}
1002
1003
1004void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1005 Address end,
1006 HeapObjectCallback size_func) {
1007 ASSERT(space->ToSpaceContains(start));
1008 ASSERT(space->ToSpaceLow() <= end
1009 && end <= space->ToSpaceHigh());
1010 space_ = space->to_space_;
1011 current_ = start;
1012 limit_ = end;
1013 size_func_ = size_func;
1014}
1015
1016
1017#ifdef DEBUG
1018// A static array of histogram info for each type.
1019static HistogramInfo heap_histograms[LAST_TYPE+1];
1020static JSObject::SpillInformation js_spill_information;
1021
1022// heap_histograms is shared, always clear it before using it.
1023static void ClearHistograms() {
1024 // We reset the name each time, though it hasn't changed.
1025#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1026 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1027#undef DEF_TYPE_NAME
1028
1029#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1030 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1031#undef CLEAR_HISTOGRAM
1032
1033 js_spill_information.Clear();
1034}
1035
1036
1037static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1038
1039
1040static void ClearCodeKindStatistics() {
1041 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1042 code_kind_statistics[i] = 0;
1043 }
1044}
1045
1046
1047static void ReportCodeKindStatistics() {
1048 const char* table[Code::NUMBER_OF_KINDS];
1049
1050#define CASE(name) \
1051 case Code::name: table[Code::name] = #name; \
1052 break
1053
1054 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1055 switch (static_cast<Code::Kind>(i)) {
1056 CASE(FUNCTION);
1057 CASE(STUB);
1058 CASE(BUILTIN);
1059 CASE(LOAD_IC);
1060 CASE(KEYED_LOAD_IC);
1061 CASE(STORE_IC);
1062 CASE(KEYED_STORE_IC);
1063 CASE(CALL_IC);
1064 }
1065 }
1066
1067#undef CASE
1068
1069 PrintF("\n Code kind histograms: \n");
1070 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1071 if (code_kind_statistics[i] > 0) {
1072 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1073 }
1074 }
1075 PrintF("\n");
1076}
1077
1078
1079static int CollectHistogramInfo(HeapObject* obj) {
1080 InstanceType type = obj->map()->instance_type();
1081 ASSERT(0 <= type && type <= LAST_TYPE);
1082 ASSERT(heap_histograms[type].name() != NULL);
1083 heap_histograms[type].increment_number(1);
1084 heap_histograms[type].increment_bytes(obj->Size());
1085
1086 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1087 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1088 }
1089
1090 return obj->Size();
1091}
1092
1093
1094static void ReportHistogram(bool print_spill) {
1095 PrintF("\n Object Histogram:\n");
1096 for (int i = 0; i <= LAST_TYPE; i++) {
1097 if (heap_histograms[i].number() > 0) {
1098 PrintF(" %-33s%10d (%10d bytes)\n",
1099 heap_histograms[i].name(),
1100 heap_histograms[i].number(),
1101 heap_histograms[i].bytes());
1102 }
1103 }
1104 PrintF("\n");
1105
1106 // Summarize string types.
1107 int string_number = 0;
1108 int string_bytes = 0;
1109#define INCREMENT(type, size, name) \
1110 string_number += heap_histograms[type].number(); \
1111 string_bytes += heap_histograms[type].bytes();
1112 STRING_TYPE_LIST(INCREMENT)
1113#undef INCREMENT
1114 if (string_number > 0) {
1115 PrintF(" %-33s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
1116 string_bytes);
1117 }
1118
1119 if (FLAG_collect_heap_spill_statistics && print_spill) {
1120 js_spill_information.Print();
1121 }
1122}
1123#endif // DEBUG
1124
1125
1126// Support for statistics gathering for --heap-stats and --log-gc.
1127#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1128void NewSpace::ClearHistograms() {
1129 for (int i = 0; i <= LAST_TYPE; i++) {
1130 allocated_histogram_[i].clear();
1131 promoted_histogram_[i].clear();
1132 }
1133}
1134
1135// Because the copying collector does not touch garbage objects, we iterate
1136// the new space before a collection to get a histogram of allocated objects.
1137// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1138// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1139// flag is set.
1140void NewSpace::CollectStatistics() {
1141 ClearHistograms();
1142 SemiSpaceIterator it(this);
1143 while (it.has_next()) RecordAllocation(it.next());
1144}
1145
1146
1147#ifdef ENABLE_LOGGING_AND_PROFILING
1148static void DoReportStatistics(HistogramInfo* info, const char* description) {
1149 LOG(HeapSampleBeginEvent("NewSpace", description));
1150 // Lump all the string types together.
1151 int string_number = 0;
1152 int string_bytes = 0;
1153#define INCREMENT(type, size, name) \
1154 string_number += info[type].number(); \
1155 string_bytes += info[type].bytes();
1156 STRING_TYPE_LIST(INCREMENT)
1157#undef INCREMENT
1158 if (string_number > 0) {
1159 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1160 }
1161
1162 // Then do the other types.
1163 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1164 if (info[i].number() > 0) {
1165 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1166 info[i].bytes()));
1167 }
1168 }
1169 LOG(HeapSampleEndEvent("NewSpace", description));
1170}
1171#endif // ENABLE_LOGGING_AND_PROFILING
1172
1173
1174void NewSpace::ReportStatistics() {
1175#ifdef DEBUG
1176 if (FLAG_heap_stats) {
1177 float pct = static_cast<float>(Available()) / Capacity();
1178 PrintF(" capacity: %d, available: %d, %%%d\n",
1179 Capacity(), Available(), static_cast<int>(pct*100));
1180 PrintF("\n Object Histogram:\n");
1181 for (int i = 0; i <= LAST_TYPE; i++) {
1182 if (allocated_histogram_[i].number() > 0) {
1183 PrintF(" %-33s%10d (%10d bytes)\n",
1184 allocated_histogram_[i].name(),
1185 allocated_histogram_[i].number(),
1186 allocated_histogram_[i].bytes());
1187 }
1188 }
1189 PrintF("\n");
1190 }
1191#endif // DEBUG
1192
1193#ifdef ENABLE_LOGGING_AND_PROFILING
1194 if (FLAG_log_gc) {
1195 DoReportStatistics(allocated_histogram_, "allocated");
1196 DoReportStatistics(promoted_histogram_, "promoted");
1197 }
1198#endif // ENABLE_LOGGING_AND_PROFILING
1199}
1200
1201
1202void NewSpace::RecordAllocation(HeapObject* obj) {
1203 InstanceType type = obj->map()->instance_type();
1204 ASSERT(0 <= type && type <= LAST_TYPE);
1205 allocated_histogram_[type].increment_number(1);
1206 allocated_histogram_[type].increment_bytes(obj->Size());
1207}
1208
1209
1210void NewSpace::RecordPromotion(HeapObject* obj) {
1211 InstanceType type = obj->map()->instance_type();
1212 ASSERT(0 <= type && type <= LAST_TYPE);
1213 promoted_histogram_[type].increment_number(1);
1214 promoted_histogram_[type].increment_bytes(obj->Size());
1215}
1216#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1217
1218
1219// -----------------------------------------------------------------------------
1220// Free lists for old object spaces implementation
1221
1222void FreeListNode::set_size(int size_in_bytes) {
1223 ASSERT(size_in_bytes > 0);
1224 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1225
1226 // We write a map and possibly size information to the block. If the block
1227 // is big enough to be a ByteArray with at least one extra word (the next
1228 // pointer), we set its map to be the byte array map and its size to an
1229 // appropriate array length for the desired size from HeapObject::Size().
1230 // If the block is too small (eg, one or two words), to hold both a size
1231 // field and a next pointer, we give it a filler map that gives it the
1232 // correct size.
1233 if (size_in_bytes > Array::kHeaderSize) {
1234 set_map(Heap::byte_array_map());
1235 ByteArray::cast(this)->set_length(ByteArray::LengthFor(size_in_bytes));
1236 } else if (size_in_bytes == kPointerSize) {
1237 set_map(Heap::one_word_filler_map());
1238 } else if (size_in_bytes == 2 * kPointerSize) {
1239 set_map(Heap::two_word_filler_map());
1240 } else {
1241 UNREACHABLE();
1242 }
kasper.lund7276f142008-07-30 08:49:36 +00001243 ASSERT(Size() == size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244}
1245
1246
1247Address FreeListNode::next() {
1248 ASSERT(map() == Heap::byte_array_map());
kasper.lund7276f142008-07-30 08:49:36 +00001249 ASSERT(Size() >= kNextOffset + kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 return Memory::Address_at(address() + kNextOffset);
1251}
1252
1253
1254void FreeListNode::set_next(Address next) {
1255 ASSERT(map() == Heap::byte_array_map());
kasper.lund7276f142008-07-30 08:49:36 +00001256 ASSERT(Size() >= kNextOffset + kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 Memory::Address_at(address() + kNextOffset) = next;
1258}
1259
1260
1261OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1262 Reset();
1263}
1264
1265
1266void OldSpaceFreeList::Reset() {
1267 available_ = 0;
1268 for (int i = 0; i < kFreeListsLength; i++) {
1269 free_[i].head_node_ = NULL;
1270 }
1271 needs_rebuild_ = false;
1272 finger_ = kHead;
1273 free_[kHead].next_size_ = kEnd;
1274}
1275
1276
1277void OldSpaceFreeList::RebuildSizeList() {
1278 ASSERT(needs_rebuild_);
1279 int cur = kHead;
1280 for (int i = cur + 1; i < kFreeListsLength; i++) {
1281 if (free_[i].head_node_ != NULL) {
1282 free_[cur].next_size_ = i;
1283 cur = i;
1284 }
1285 }
1286 free_[cur].next_size_ = kEnd;
1287 needs_rebuild_ = false;
1288}
1289
1290
1291int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1292#ifdef DEBUG
1293 for (int i = 0; i < size_in_bytes; i += kPointerSize) {
1294 Memory::Address_at(start + i) = kZapValue;
1295 }
1296#endif
1297 FreeListNode* node = FreeListNode::FromAddress(start);
1298 node->set_size(size_in_bytes);
1299
1300 // Early return to drop too-small blocks on the floor (one or two word
1301 // blocks cannot hold a map pointer, a size field, and a pointer to the
1302 // next block in the free list).
1303 if (size_in_bytes < kMinBlockSize) {
1304 return size_in_bytes;
1305 }
1306
1307 // Insert other blocks at the head of an exact free list.
1308 int index = size_in_bytes >> kPointerSizeLog2;
1309 node->set_next(free_[index].head_node_);
1310 free_[index].head_node_ = node->address();
1311 available_ += size_in_bytes;
1312 needs_rebuild_ = true;
1313 return 0;
1314}
1315
1316
1317Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1318 ASSERT(0 < size_in_bytes);
1319 ASSERT(size_in_bytes <= kMaxBlockSize);
1320 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1321
1322 if (needs_rebuild_) RebuildSizeList();
1323 int index = size_in_bytes >> kPointerSizeLog2;
1324 // Check for a perfect fit.
1325 if (free_[index].head_node_ != NULL) {
1326 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1327 // If this was the last block of its size, remove the size.
1328 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1329 available_ -= size_in_bytes;
1330 *wasted_bytes = 0;
1331 return node;
1332 }
1333 // Search the size list for the best fit.
1334 int prev = finger_ < index ? finger_ : kHead;
1335 int cur = FindSize(index, &prev);
1336 ASSERT(index < cur);
1337 if (cur == kEnd) {
1338 // No large enough size in list.
1339 *wasted_bytes = 0;
1340 return Failure::RetryAfterGC(size_in_bytes, owner_);
1341 }
1342 int rem = cur - index;
1343 int rem_bytes = rem << kPointerSizeLog2;
1344 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
kasper.lund7276f142008-07-30 08:49:36 +00001345 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1347 size_in_bytes);
1348 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1349 // to avoid many redundant tests and calls to Insert/RemoveSize.
1350 if (prev < rem) {
1351 // Simple case: insert rem between prev and cur.
1352 finger_ = prev;
1353 free_[prev].next_size_ = rem;
1354 // If this was the last block of size cur, remove the size.
1355 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1356 free_[rem].next_size_ = free_[cur].next_size_;
1357 } else {
1358 free_[rem].next_size_ = cur;
1359 }
1360 // Add the remainder block.
1361 rem_node->set_size(rem_bytes);
1362 rem_node->set_next(free_[rem].head_node_);
1363 free_[rem].head_node_ = rem_node->address();
1364 } else {
1365 // If this was the last block of size cur, remove the size.
1366 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1367 finger_ = prev;
1368 free_[prev].next_size_ = free_[cur].next_size_;
1369 }
1370 if (rem_bytes < kMinBlockSize) {
1371 // Too-small remainder is wasted.
1372 rem_node->set_size(rem_bytes);
1373 available_ -= size_in_bytes + rem_bytes;
1374 *wasted_bytes = rem_bytes;
1375 return cur_node;
1376 }
1377 // Add the remainder block and, if needed, insert its size.
1378 rem_node->set_size(rem_bytes);
1379 rem_node->set_next(free_[rem].head_node_);
1380 free_[rem].head_node_ = rem_node->address();
1381 if (rem_node->next() == NULL) InsertSize(rem);
1382 }
1383 available_ -= size_in_bytes;
1384 *wasted_bytes = 0;
1385 return cur_node;
1386}
1387
1388
kasper.lund7276f142008-07-30 08:49:36 +00001389#ifdef DEBUG
1390bool OldSpaceFreeList::Contains(FreeListNode* node) {
1391 for (int i = 0; i < kFreeListsLength; i++) {
1392 Address cur_addr = free_[i].head_node_;
1393 while (cur_addr != NULL) {
1394 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1395 if (cur_node == node) return true;
1396 cur_addr = cur_node->next();
1397 }
1398 }
1399 return false;
1400}
1401#endif
1402
1403
1404MapSpaceFreeList::MapSpaceFreeList(AllocationSpace owner) {
1405 owner_ = owner;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 Reset();
1407}
1408
1409
1410void MapSpaceFreeList::Reset() {
1411 available_ = 0;
1412 head_ = NULL;
1413}
1414
1415
1416void MapSpaceFreeList::Free(Address start) {
1417#ifdef DEBUG
1418 for (int i = 0; i < Map::kSize; i += kPointerSize) {
1419 Memory::Address_at(start + i) = kZapValue;
1420 }
1421#endif
1422 FreeListNode* node = FreeListNode::FromAddress(start);
1423 node->set_size(Map::kSize);
1424 node->set_next(head_);
1425 head_ = node->address();
1426 available_ += Map::kSize;
1427}
1428
1429
1430Object* MapSpaceFreeList::Allocate() {
1431 if (head_ == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00001432 return Failure::RetryAfterGC(Map::kSize, owner_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 }
1434
1435 FreeListNode* node = FreeListNode::FromAddress(head_);
1436 head_ = node->next();
1437 available_ -= Map::kSize;
1438 return node;
1439}
1440
1441
1442// -----------------------------------------------------------------------------
1443// OldSpace implementation
1444
1445void OldSpace::PrepareForMarkCompact(bool will_compact) {
1446 if (will_compact) {
1447 // Reset relocation info. During a compacting collection, everything in
1448 // the space is considered 'available' and we will rediscover live data
1449 // and waste during the collection.
1450 MCResetRelocationInfo();
1451 mc_end_of_relocation_ = bottom();
1452 ASSERT(Available() == Capacity());
1453 } else {
1454 // During a non-compacting collection, everything below the linear
1455 // allocation pointer is considered allocated (everything above is
1456 // available) and we will rediscover available and wasted bytes during
1457 // the collection.
1458 accounting_stats_.AllocateBytes(free_list_.available());
1459 accounting_stats_.FillWastedBytes(Waste());
1460 }
1461
kasper.lund7276f142008-07-30 08:49:36 +00001462 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464}
1465
1466
1467void OldSpace::MCAdjustRelocationEnd(Address address, int size_in_bytes) {
1468 ASSERT(Contains(address));
1469 Address current_top = mc_end_of_relocation_;
1470 Page* current_page = Page::FromAllocationTop(current_top);
1471
1472 // No more objects relocated to this page? Move to the next.
1473 ASSERT(current_top <= current_page->mc_relocation_top);
1474 if (current_top == current_page->mc_relocation_top) {
1475 // The space should already be properly expanded.
1476 Page* next_page = current_page->next_page();
1477 CHECK(next_page->is_valid());
1478 mc_end_of_relocation_ = next_page->ObjectAreaStart();
1479 }
1480 ASSERT(mc_end_of_relocation_ == address);
1481 mc_end_of_relocation_ += size_in_bytes;
1482}
1483
1484
1485void OldSpace::MCCommitRelocationInfo() {
1486 // Update fast allocation info.
1487 allocation_info_.top = mc_forwarding_info_.top;
1488 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001489 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490
1491 // The space is compacted and we haven't yet built free lists or
1492 // wasted any space.
1493 ASSERT(Waste() == 0);
1494 ASSERT(AvailableFree() == 0);
1495
1496 // Build the free list for the space.
1497 int computed_size = 0;
1498 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1499 while (it.has_next()) {
1500 Page* p = it.next();
1501 // Space below the relocation pointer is allocated.
1502 computed_size += p->mc_relocation_top - p->ObjectAreaStart();
1503 if (it.has_next()) {
1504 // Free the space at the top of the page. We cannot use
1505 // p->mc_relocation_top after the call to Free (because Free will clear
1506 // remembered set bits).
1507 int extra_size = p->ObjectAreaEnd() - p->mc_relocation_top;
1508 if (extra_size > 0) {
1509 int wasted_bytes = free_list_.Free(p->mc_relocation_top, extra_size);
1510 // The bytes we have just "freed" to add to the free list were
1511 // already accounted as available.
1512 accounting_stats_.WasteBytes(wasted_bytes);
1513 }
1514 }
1515 }
1516
1517 // Make sure the computed size - based on the used portion of the pages in
1518 // use - matches the size obtained while computing forwarding addresses.
1519 ASSERT(computed_size == Size());
1520}
1521
1522
kasper.lund7276f142008-07-30 08:49:36 +00001523// Slow case for normal allocation. Try in order: (1) allocate in the next
1524// page in the space, (2) allocate off the space's free list, (3) expand the
1525// space, (4) fail.
1526HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
1527 // Linear allocation in this space has failed. If there is another page
1528 // in the space, move to that page and allocate there. This allocation
1529 // should succeed (size_in_bytes should not be greater than a page's
1530 // object area size).
1531 Page* current_page = TopPageOf(allocation_info_);
1532 if (current_page->next_page()->is_valid()) {
1533 return AllocateInNextPage(current_page, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534 }
kasper.lund7276f142008-07-30 08:49:36 +00001535
1536 // There is no next page in this space. Try free list allocation.
1537 int wasted_bytes;
1538 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
1539 accounting_stats_.WasteBytes(wasted_bytes);
1540 if (!result->IsFailure()) {
1541 accounting_stats_.AllocateBytes(size_in_bytes);
1542 return HeapObject::cast(result);
1543 }
1544
1545 // Free list allocation failed and there is no next page. Try to expand
1546 // the space and allocate in the new next page.
1547 ASSERT(!current_page->next_page()->is_valid());
1548 if (Expand(current_page)) {
1549 return AllocateInNextPage(current_page, size_in_bytes);
1550 }
1551
1552 // Finally, fail.
1553 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554}
1555
1556
kasper.lund7276f142008-07-30 08:49:36 +00001557// Add the block at the top of the page to the space's free list, set the
1558// allocation info to the next page (assumed to be one), and allocate
1559// linearly there.
1560HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
1561 int size_in_bytes) {
1562 ASSERT(current_page->next_page()->is_valid());
1563 // Add the block at the top of this page to the free list.
1564 int free_size = current_page->ObjectAreaEnd() - allocation_info_.top;
1565 if (free_size > 0) {
1566 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
1567 accounting_stats_.WasteBytes(wasted_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 }
kasper.lund7276f142008-07-30 08:49:36 +00001569 SetAllocationInfo(&allocation_info_, current_page->next_page());
1570 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571}
1572
1573
1574#ifdef DEBUG
1575// We do not assume that the PageIterator works, because it depends on the
1576// invariants we are checking during verification.
1577void OldSpace::Verify() {
1578 // The allocation pointer should be valid, and it should be in a page in the
1579 // space.
kasper.lund7276f142008-07-30 08:49:36 +00001580 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
1582 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
1583
1584 // Loop over all the pages.
1585 bool above_allocation_top = false;
1586 Page* current_page = first_page_;
1587 while (current_page->is_valid()) {
1588 if (above_allocation_top) {
1589 // We don't care what's above the allocation top.
1590 } else {
1591 // Unless this is the last page in the space containing allocated
1592 // objects, the allocation top should be at the object area end.
1593 Address top = current_page->AllocationTop();
1594 if (current_page == top_page) {
1595 ASSERT(top == allocation_info_.top);
1596 // The next page will be above the allocation top.
1597 above_allocation_top = true;
1598 } else {
1599 ASSERT(top == current_page->ObjectAreaEnd());
1600 }
1601
1602 // It should be packed with objects from the bottom to the top.
1603 Address current = current_page->ObjectAreaStart();
1604 while (current < top) {
1605 HeapObject* object = HeapObject::FromAddress(current);
1606
1607 // The first word should be a map, and we expect all map pointers to
1608 // be in map space.
1609 Map* map = object->map();
1610 ASSERT(map->IsMap());
1611 ASSERT(Heap::map_space()->Contains(map));
1612
1613 // The object should not be a map.
1614 ASSERT(!object->IsMap());
1615
1616 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001617 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618
1619 // All the interior pointers should be contained in the heap and have
1620 // their remembered set bits set if they point to new space. Code
1621 // objects do not have remembered set bits that we care about.
1622 VerifyPointersAndRSetVisitor rset_visitor;
1623 VerifyPointersVisitor no_rset_visitor;
1624 int size = object->Size();
1625 if (object->IsCode()) {
1626 Code::cast(object)->ConvertICTargetsFromAddressToObject();
1627 object->IterateBody(map->instance_type(), size, &no_rset_visitor);
1628 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
1629 } else {
1630 object->IterateBody(map->instance_type(), size, &rset_visitor);
1631 }
1632
1633 current += size;
1634 }
1635
1636 // The allocation pointer should not be in the middle of an object.
1637 ASSERT(current == top);
1638 }
1639
1640 current_page = current_page->next_page();
1641 }
1642}
1643
1644
1645struct CommentStatistic {
1646 const char* comment;
1647 int size;
1648 int count;
1649 void Clear() {
1650 comment = NULL;
1651 size = 0;
1652 count = 0;
1653 }
1654};
1655
1656
1657// must be small, since an iteration is used for lookup
1658const int kMaxComments = 64;
1659static CommentStatistic comments_statistics[kMaxComments+1];
1660
1661
1662void PagedSpace::ReportCodeStatistics() {
1663 ReportCodeKindStatistics();
1664 PrintF("Code comment statistics (\" [ comment-txt : size/ "
1665 "count (average)\"):\n");
1666 for (int i = 0; i <= kMaxComments; i++) {
1667 const CommentStatistic& cs = comments_statistics[i];
1668 if (cs.size > 0) {
1669 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
1670 cs.size/cs.count);
1671 }
1672 }
1673 PrintF("\n");
1674}
1675
1676
1677void PagedSpace::ResetCodeStatistics() {
1678 ClearCodeKindStatistics();
1679 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
1680 comments_statistics[kMaxComments].comment = "Unknown";
1681 comments_statistics[kMaxComments].size = 0;
1682 comments_statistics[kMaxComments].count = 0;
1683}
1684
1685
1686// Adds comment to 'comment_statistics' table. Performance OK sa long as
1687// 'kMaxComments' is small
1688static void EnterComment(const char* comment, int delta) {
1689 // Do not count empty comments
1690 if (delta <= 0) return;
1691 CommentStatistic* cs = &comments_statistics[kMaxComments];
1692 // Search for a free or matching entry in 'comments_statistics': 'cs'
1693 // points to result.
1694 for (int i = 0; i < kMaxComments; i++) {
1695 if (comments_statistics[i].comment == NULL) {
1696 cs = &comments_statistics[i];
1697 cs->comment = comment;
1698 break;
1699 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
1700 cs = &comments_statistics[i];
1701 break;
1702 }
1703 }
1704 // Update entry for 'comment'
1705 cs->size += delta;
1706 cs->count += 1;
1707}
1708
1709
1710// Call for each nested comment start (start marked with '[ xxx', end marked
1711// with ']'. RelocIterator 'it' must point to a comment reloc info.
1712static void CollectCommentStatistics(RelocIterator* it) {
1713 ASSERT(!it->done());
1714 ASSERT(it->rinfo()->rmode() == comment);
1715 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
1716 if (tmp[0] != '[') {
1717 // Not a nested comment; skip
1718 return;
1719 }
1720
1721 // Search for end of nested comment or a new nested comment
1722 const char* const comment_txt =
1723 reinterpret_cast<const char*>(it->rinfo()->data());
1724 const byte* prev_pc = it->rinfo()->pc();
1725 int flat_delta = 0;
1726 it->next();
1727 while (true) {
1728 // All nested comments must be terminated properly, and therefore exit
1729 // from loop.
1730 ASSERT(!it->done());
1731 if (it->rinfo()->rmode() == comment) {
1732 const char* const txt =
1733 reinterpret_cast<const char*>(it->rinfo()->data());
1734 flat_delta += it->rinfo()->pc() - prev_pc;
1735 if (txt[0] == ']') break; // End of nested comment
1736 // A new comment
1737 CollectCommentStatistics(it);
1738 // Skip code that was covered with previous comment
1739 prev_pc = it->rinfo()->pc();
1740 }
1741 it->next();
1742 }
1743 EnterComment(comment_txt, flat_delta);
1744}
1745
1746
1747// Collects code size statistics:
1748// - by code kind
1749// - by code comment
1750void PagedSpace::CollectCodeStatistics() {
1751 HeapObjectIterator obj_it(this);
1752 while (obj_it.has_next()) {
1753 HeapObject* obj = obj_it.next();
1754 if (obj->IsCode()) {
1755 Code* code = Code::cast(obj);
1756 code_kind_statistics[code->kind()] += code->Size();
1757 RelocIterator it(code);
1758 int delta = 0;
1759 const byte* prev_pc = code->instruction_start();
1760 while (!it.done()) {
1761 if (it.rinfo()->rmode() == comment) {
1762 delta += it.rinfo()->pc() - prev_pc;
1763 CollectCommentStatistics(&it);
1764 prev_pc = it.rinfo()->pc();
1765 }
1766 it.next();
1767 }
1768
1769 ASSERT(code->instruction_start() <= prev_pc &&
1770 prev_pc <= code->relocation_start());
1771 delta += code->relocation_start() - prev_pc;
1772 EnterComment("NoComment", delta);
1773 }
1774 }
1775}
1776
1777
1778void OldSpace::ReportStatistics() {
1779 int pct = Available() * 100 / Capacity();
1780 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
1781 Capacity(), Waste(), Available(), pct);
1782
1783 // Report remembered set statistics.
1784 int rset_marked_pointers = 0;
1785 int rset_marked_arrays = 0;
1786 int rset_marked_array_elements = 0;
1787 int cross_gen_pointers = 0;
1788 int cross_gen_array_elements = 0;
1789
1790 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
1791 while (page_it.has_next()) {
1792 Page* p = page_it.next();
1793
1794 for (Address rset_addr = p->RSetStart();
1795 rset_addr < p->RSetEnd();
1796 rset_addr += kIntSize) {
1797 int rset = Memory::int_at(rset_addr);
1798 if (rset != 0) {
1799 // Bits were set
1800 int intoff = rset_addr - p->address();
1801 int bitoff = 0;
1802 for (; bitoff < kBitsPerInt; ++bitoff) {
1803 if ((rset & (1 << bitoff)) != 0) {
1804 int bitpos = intoff*kBitsPerByte + bitoff;
1805 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
1806 Object** obj = reinterpret_cast<Object**>(slot);
1807 if (*obj == Heap::fixed_array_map()) {
1808 rset_marked_arrays++;
1809 FixedArray* fa = FixedArray::cast(HeapObject::FromAddress(slot));
1810
1811 rset_marked_array_elements += fa->length();
1812 // Manually inline FixedArray::IterateBody
1813 Address elm_start = slot + FixedArray::kHeaderSize;
1814 Address elm_stop = elm_start + fa->length() * kPointerSize;
1815 for (Address elm_addr = elm_start;
1816 elm_addr < elm_stop; elm_addr += kPointerSize) {
1817 // Filter non-heap-object pointers
1818 Object** elm_p = reinterpret_cast<Object**>(elm_addr);
1819 if (Heap::InNewSpace(*elm_p))
1820 cross_gen_array_elements++;
1821 }
1822 } else {
1823 rset_marked_pointers++;
1824 if (Heap::InNewSpace(*obj))
1825 cross_gen_pointers++;
1826 }
1827 }
1828 }
1829 }
1830 }
1831 }
1832
1833 pct = rset_marked_pointers == 0 ?
1834 0 : cross_gen_pointers * 100 / rset_marked_pointers;
1835 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
1836 rset_marked_pointers, cross_gen_pointers, pct);
1837 PrintF(" rset_marked arrays %d, ", rset_marked_arrays);
1838 PrintF(" elements %d, ", rset_marked_array_elements);
1839 pct = rset_marked_array_elements == 0 ? 0
1840 : cross_gen_array_elements * 100 / rset_marked_array_elements;
1841 PrintF(" pointers to new space %d (%%%d)\n", cross_gen_array_elements, pct);
1842 PrintF(" total rset-marked bits %d\n",
1843 (rset_marked_pointers + rset_marked_arrays));
1844 pct = (rset_marked_pointers + rset_marked_array_elements) == 0 ? 0
1845 : (cross_gen_pointers + cross_gen_array_elements) * 100 /
1846 (rset_marked_pointers + rset_marked_array_elements);
1847 PrintF(" total rset pointers %d, true cross generation ones %d (%%%d)\n",
1848 (rset_marked_pointers + rset_marked_array_elements),
1849 (cross_gen_pointers + cross_gen_array_elements),
1850 pct);
1851
1852 ClearHistograms();
1853 HeapObjectIterator obj_it(this);
1854 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
1855 ReportHistogram(true);
1856}
1857
1858
1859// Dump the range of remembered set words between [start, end) corresponding
1860// to the pointers starting at object_p. The allocation_top is an object
1861// pointer which should not be read past. This is important for large object
1862// pages, where some bits in the remembered set range do not correspond to
1863// allocated addresses.
1864static void PrintRSetRange(Address start, Address end, Object** object_p,
1865 Address allocation_top) {
1866 Address rset_address = start;
1867
1868 // If the range starts on on odd numbered word (eg, for large object extra
1869 // remembered set ranges), print some spaces.
1870 if ((reinterpret_cast<uint32_t>(start) / kIntSize) % 2 == 1) {
1871 PrintF(" ");
1872 }
1873
1874 // Loop over all the words in the range.
1875 while (rset_address < end) {
1876 uint32_t rset_word = Memory::uint32_at(rset_address);
1877 int bit_position = 0;
1878
1879 // Loop over all the bits in the word.
1880 while (bit_position < kBitsPerInt) {
1881 if (object_p == reinterpret_cast<Object**>(allocation_top)) {
1882 // Print a bar at the allocation pointer.
1883 PrintF("|");
1884 } else if (object_p > reinterpret_cast<Object**>(allocation_top)) {
1885 // Do not dereference object_p past the allocation pointer.
1886 PrintF("#");
1887 } else if ((rset_word & (1 << bit_position)) == 0) {
1888 // Print a dot for zero bits.
1889 PrintF(".");
1890 } else if (Heap::InNewSpace(*object_p)) {
1891 // Print an X for one bits for pointers to new space.
1892 PrintF("X");
1893 } else {
1894 // Print a circle for one bits for pointers to old space.
1895 PrintF("o");
1896 }
1897
1898 // Print a space after every 8th bit except the last.
1899 if (bit_position % 8 == 7 && bit_position != (kBitsPerInt - 1)) {
1900 PrintF(" ");
1901 }
1902
1903 // Advance to next bit.
1904 bit_position++;
1905 object_p++;
1906 }
1907
1908 // Print a newline after every odd numbered word, otherwise a space.
1909 if ((reinterpret_cast<uint32_t>(rset_address) / kIntSize) % 2 == 1) {
1910 PrintF("\n");
1911 } else {
1912 PrintF(" ");
1913 }
1914
1915 // Advance to next remembered set word.
1916 rset_address += kIntSize;
1917 }
1918}
1919
1920
1921void PagedSpace::DoPrintRSet(const char* space_name) {
1922 PageIterator it(this, PageIterator::PAGES_IN_USE);
1923 while (it.has_next()) {
1924 Page* p = it.next();
1925 PrintF("%s page 0x%x:\n", space_name, p);
1926 PrintRSetRange(p->RSetStart(), p->RSetEnd(),
1927 reinterpret_cast<Object**>(p->ObjectAreaStart()),
1928 p->AllocationTop());
1929 PrintF("\n");
1930 }
1931}
1932
1933
1934void OldSpace::PrintRSet() { DoPrintRSet("old"); }
1935#endif
1936
1937// -----------------------------------------------------------------------------
1938// MapSpace implementation
1939
1940void MapSpace::PrepareForMarkCompact(bool will_compact) {
1941 if (will_compact) {
1942 // Reset relocation info.
1943 MCResetRelocationInfo();
1944
1945 // Initialize map index entry.
1946 int page_count = 0;
1947 PageIterator it(this, PageIterator::ALL_PAGES);
1948 while (it.has_next()) {
1949 ASSERT_MAP_PAGE_INDEX(page_count);
1950
1951 Page* p = it.next();
1952 ASSERT(p->mc_page_index == page_count);
1953
1954 page_addresses_[page_count++] = p->address();
1955 }
1956
1957 // During a compacting collection, everything in the space is considered
1958 // 'available' (set by the call to MCResetRelocationInfo) and we will
1959 // rediscover live and wasted bytes during the collection.
1960 ASSERT(Available() == Capacity());
1961 } else {
1962 // During a non-compacting collection, everything below the linear
1963 // allocation pointer except wasted top-of-page blocks is considered
1964 // allocated and we will rediscover available bytes during the
1965 // collection.
1966 accounting_stats_.AllocateBytes(free_list_.available());
1967 }
1968
kasper.lund7276f142008-07-30 08:49:36 +00001969 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971}
1972
1973
1974void MapSpace::MCCommitRelocationInfo() {
1975 // Update fast allocation info.
1976 allocation_info_.top = mc_forwarding_info_.top;
1977 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001978 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979
1980 // The space is compacted and we haven't yet wasted any space.
1981 ASSERT(Waste() == 0);
1982
1983 // Update allocation_top of each page in use and compute waste.
1984 int computed_size = 0;
1985 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1986 while (it.has_next()) {
1987 Page* page = it.next();
1988 Address page_top = page->AllocationTop();
1989 computed_size += page_top - page->ObjectAreaStart();
1990 if (it.has_next()) {
1991 accounting_stats_.WasteBytes(page->ObjectAreaEnd() - page_top);
1992 }
1993 }
1994
1995 // Make sure the computed size - based on the used portion of the
1996 // pages in use - matches the size we adjust during allocation.
1997 ASSERT(computed_size == Size());
1998}
1999
2000
kasper.lund7276f142008-07-30 08:49:36 +00002001// Slow case for normal allocation. Try in order: (1) allocate in the next
2002// page in the space, (2) allocate off the space's free list, (3) expand the
2003// space, (4) fail.
2004HeapObject* MapSpace::SlowAllocateRaw(int size_in_bytes) {
2005 // Linear allocation in this space has failed. If there is another page
2006 // in the space, move to that page and allocate there. This allocation
2007 // should succeed.
2008 Page* current_page = TopPageOf(allocation_info_);
2009 if (current_page->next_page()->is_valid()) {
2010 return AllocateInNextPage(current_page, size_in_bytes);
2011 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012
kasper.lund7276f142008-07-30 08:49:36 +00002013 // There is no next page in this space. Try free list allocation. The
2014 // map space free list implicitly assumes that all free blocks are map
2015 // sized.
2016 if (size_in_bytes == Map::kSize) {
2017 Object* result = free_list_.Allocate();
2018 if (!result->IsFailure()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 accounting_stats_.AllocateBytes(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002020 return HeapObject::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 }
2022 }
kasper.lund7276f142008-07-30 08:49:36 +00002023
2024 // Free list allocation failed and there is no next page. Try to expand
2025 // the space and allocate in the new next page.
2026 ASSERT(!current_page->next_page()->is_valid());
2027 if (Expand(current_page)) {
2028 return AllocateInNextPage(current_page, size_in_bytes);
2029 }
2030
2031 // Finally, fail.
2032 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033}
2034
2035
kasper.lund7276f142008-07-30 08:49:36 +00002036// Move to the next page (there is assumed to be one) and allocate there.
2037// The top of page block is always wasted, because it is too small to hold a
2038// map.
2039HeapObject* MapSpace::AllocateInNextPage(Page* current_page,
2040 int size_in_bytes) {
2041 ASSERT(current_page->next_page()->is_valid());
2042 ASSERT(current_page->ObjectAreaEnd() - allocation_info_.top == kPageExtra);
2043 accounting_stats_.WasteBytes(kPageExtra);
2044 SetAllocationInfo(&allocation_info_, current_page->next_page());
2045 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046}
2047
2048
2049#ifdef DEBUG
2050// We do not assume that the PageIterator works, because it depends on the
2051// invariants we are checking during verification.
2052void MapSpace::Verify() {
2053 // The allocation pointer should be valid, and it should be in a page in the
2054 // space.
kasper.lund7276f142008-07-30 08:49:36 +00002055 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
2057 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
2058
2059 // Loop over all the pages.
2060 bool above_allocation_top = false;
2061 Page* current_page = first_page_;
2062 while (current_page->is_valid()) {
2063 if (above_allocation_top) {
2064 // We don't care what's above the allocation top.
2065 } else {
2066 // Unless this is the last page in the space containing allocated
2067 // objects, the allocation top should be at a constant offset from the
2068 // object area end.
2069 Address top = current_page->AllocationTop();
2070 if (current_page == top_page) {
2071 ASSERT(top == allocation_info_.top);
2072 // The next page will be above the allocation top.
2073 above_allocation_top = true;
2074 } else {
2075 ASSERT(top == current_page->ObjectAreaEnd() - kPageExtra);
2076 }
2077
2078 // It should be packed with objects from the bottom to the top.
2079 Address current = current_page->ObjectAreaStart();
2080 while (current < top) {
2081 HeapObject* object = HeapObject::FromAddress(current);
2082
2083 // The first word should be a map, and we expect all map pointers to
2084 // be in map space.
2085 Map* map = object->map();
2086 ASSERT(map->IsMap());
2087 ASSERT(Heap::map_space()->Contains(map));
2088
2089 // The object should be a map or a byte array.
2090 ASSERT(object->IsMap() || object->IsByteArray());
2091
2092 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002093 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094
2095 // All the interior pointers should be contained in the heap and
2096 // have their remembered set bits set if they point to new space.
2097 VerifyPointersAndRSetVisitor visitor;
2098 int size = object->Size();
2099 object->IterateBody(map->instance_type(), size, &visitor);
2100
2101 current += size;
2102 }
2103
2104 // The allocation pointer should not be in the middle of an object.
2105 ASSERT(current == top);
2106 }
2107
2108 current_page = current_page->next_page();
2109 }
2110}
2111
2112
2113void MapSpace::ReportStatistics() {
2114 int pct = Available() * 100 / Capacity();
2115 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2116 Capacity(), Waste(), Available(), pct);
2117
2118 // Report remembered set statistics.
2119 int rset_marked_pointers = 0;
2120 int cross_gen_pointers = 0;
2121
2122 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2123 while (page_it.has_next()) {
2124 Page* p = page_it.next();
2125
2126 for (Address rset_addr = p->RSetStart();
2127 rset_addr < p->RSetEnd();
2128 rset_addr += kIntSize) {
2129 int rset = Memory::int_at(rset_addr);
2130 if (rset != 0) {
2131 // Bits were set
2132 int intoff = rset_addr - p->address();
2133 int bitoff = 0;
2134 for (; bitoff < kBitsPerInt; ++bitoff) {
2135 if ((rset & (1 << bitoff)) != 0) {
2136 int bitpos = intoff*kBitsPerByte + bitoff;
2137 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2138 Object** obj = reinterpret_cast<Object**>(slot);
2139 rset_marked_pointers++;
2140 if (Heap::InNewSpace(*obj))
2141 cross_gen_pointers++;
2142 }
2143 }
2144 }
2145 }
2146 }
2147
2148 pct = rset_marked_pointers == 0 ?
2149 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2150 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2151 rset_marked_pointers, cross_gen_pointers, pct);
2152
2153 ClearHistograms();
2154 HeapObjectIterator obj_it(this);
2155 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
2156 ReportHistogram(false);
2157}
2158
2159
2160void MapSpace::PrintRSet() { DoPrintRSet("map"); }
2161#endif
2162
2163
2164// -----------------------------------------------------------------------------
2165// LargeObjectIterator
2166
2167LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2168 current_ = space->first_chunk_;
2169 size_func_ = NULL;
2170}
2171
2172
2173LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2174 HeapObjectCallback size_func) {
2175 current_ = space->first_chunk_;
2176 size_func_ = size_func;
2177}
2178
2179
2180HeapObject* LargeObjectIterator::next() {
2181 ASSERT(has_next());
2182 HeapObject* object = current_->GetObject();
2183 current_ = current_->next();
2184 return object;
2185}
2186
2187
2188// -----------------------------------------------------------------------------
2189// LargeObjectChunk
2190
2191LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
kasper.lund7276f142008-07-30 08:49:36 +00002192 size_t* chunk_size,
2193 bool executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 size_t requested = ChunkSizeFor(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002195 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2196 chunk_size,
2197 executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 if (mem == NULL) return NULL;
2199 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2200 if (*chunk_size < requested) {
2201 MemoryAllocator::FreeRawMemory(mem, *chunk_size);
2202 LOG(DeleteEvent("LargeObjectChunk", mem));
2203 return NULL;
2204 }
2205 return reinterpret_cast<LargeObjectChunk*>(mem);
2206}
2207
2208
2209int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
2210 int os_alignment = OS::AllocateAlignment();
2211 if (os_alignment < Page::kPageSize)
2212 size_in_bytes += (Page::kPageSize - os_alignment);
2213 return size_in_bytes + Page::kObjectStartOffset;
2214}
2215
2216// -----------------------------------------------------------------------------
2217// LargeObjectSpace
2218
kasper.lund7276f142008-07-30 08:49:36 +00002219LargeObjectSpace::LargeObjectSpace(AllocationSpace id, bool executable)
2220 : Space(id, executable),
2221 first_chunk_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 size_(0),
2223 page_count_(0) {}
2224
2225
2226bool LargeObjectSpace::Setup() {
2227 first_chunk_ = NULL;
2228 size_ = 0;
2229 page_count_ = 0;
2230 return true;
2231}
2232
2233
2234void LargeObjectSpace::TearDown() {
2235 while (first_chunk_ != NULL) {
2236 LargeObjectChunk* chunk = first_chunk_;
2237 first_chunk_ = first_chunk_->next();
2238 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
2239 MemoryAllocator::FreeRawMemory(chunk->address(), chunk->size());
2240 }
2241
2242 size_ = 0;
2243 page_count_ = 0;
2244}
2245
2246
2247Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
2248 int object_size) {
2249 ASSERT(0 < object_size && object_size <= requested_size);
2250 size_t chunk_size;
2251 LargeObjectChunk* chunk =
kasper.lund7276f142008-07-30 08:49:36 +00002252 LargeObjectChunk::New(requested_size, &chunk_size, executable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 if (chunk == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00002254 return Failure::RetryAfterGC(requested_size, identity());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 }
2256
2257 size_ += chunk_size;
2258 page_count_++;
2259 chunk->set_next(first_chunk_);
2260 chunk->set_size(chunk_size);
2261 first_chunk_ = chunk;
2262
2263 // Set the object address and size in the page header and clear its
2264 // remembered set.
2265 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2266 Address object_address = page->ObjectAreaStart();
2267 // Clear the low order bit of the second word in the page to flag it as a
2268 // large object page. If the chunk_size happened to be written there, its
2269 // low order bit should already be clear.
2270 ASSERT((chunk_size & 0x1) == 0);
2271 page->is_normal_page &= ~0x1;
2272 page->ClearRSet();
2273 int extra_bytes = requested_size - object_size;
2274 if (extra_bytes > 0) {
2275 // The extra memory for the remembered set should be cleared.
2276 memset(object_address + object_size, 0, extra_bytes);
2277 }
2278
2279 return HeapObject::FromAddress(object_address);
2280}
2281
2282
2283Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2284 ASSERT(0 < size_in_bytes);
2285 return AllocateRawInternal(size_in_bytes, size_in_bytes);
2286}
2287
2288
2289Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
2290 int extra_rset_bytes = ExtraRSetBytesFor(size_in_bytes);
2291 return AllocateRawInternal(size_in_bytes + extra_rset_bytes, size_in_bytes);
2292}
2293
2294
2295// GC support
2296Object* LargeObjectSpace::FindObject(Address a) {
2297 for (LargeObjectChunk* chunk = first_chunk_;
2298 chunk != NULL;
2299 chunk = chunk->next()) {
2300 Address chunk_address = chunk->address();
2301 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2302 return chunk->GetObject();
2303 }
2304 }
2305 return Failure::Exception();
2306}
2307
2308
2309void LargeObjectSpace::ClearRSet() {
2310 ASSERT(Page::is_rset_in_use());
2311
2312 LargeObjectIterator it(this);
2313 while (it.has_next()) {
2314 HeapObject* object = it.next();
2315 // We only have code, sequential strings, or fixed arrays in large
2316 // object space, and only fixed arrays need remembered set support.
2317 if (object->IsFixedArray()) {
2318 // Clear the normal remembered set region of the page;
2319 Page* page = Page::FromAddress(object->address());
2320 page->ClearRSet();
2321
2322 // Clear the extra remembered set.
2323 int size = object->Size();
2324 int extra_rset_bytes = ExtraRSetBytesFor(size);
2325 memset(object->address() + size, 0, extra_rset_bytes);
2326 }
2327 }
2328}
2329
2330
2331void LargeObjectSpace::IterateRSet(ObjectSlotCallback copy_object_func) {
2332 ASSERT(Page::is_rset_in_use());
2333
2334 LargeObjectIterator it(this);
2335 while (it.has_next()) {
2336 // We only have code, sequential strings, or fixed arrays in large
2337 // object space, and only fixed arrays can possibly contain pointers to
2338 // the young generation.
2339 HeapObject* object = it.next();
2340 if (object->IsFixedArray()) {
2341 // Iterate the normal page remembered set range.
2342 Page* page = Page::FromAddress(object->address());
2343 Address object_end = object->address() + object->Size();
2344 Heap::IterateRSetRange(page->ObjectAreaStart(),
2345 Min(page->ObjectAreaEnd(), object_end),
2346 page->RSetStart(),
2347 copy_object_func);
2348
2349 // Iterate the extra array elements.
2350 if (object_end > page->ObjectAreaEnd()) {
2351 Heap::IterateRSetRange(page->ObjectAreaEnd(), object_end,
2352 object_end, copy_object_func);
2353 }
2354 }
2355 }
2356}
2357
2358
2359void LargeObjectSpace::FreeUnmarkedObjects() {
2360 LargeObjectChunk* previous = NULL;
2361 LargeObjectChunk* current = first_chunk_;
2362 while (current != NULL) {
2363 HeapObject* object = current->GetObject();
kasper.lund7276f142008-07-30 08:49:36 +00002364 if (object->IsMarked()) {
2365 object->ClearMark();
2366 MarkCompactCollector::tracer()->decrement_marked_count();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 previous = current;
2368 current = current->next();
2369 } else {
2370 Address chunk_address = current->address();
2371 size_t chunk_size = current->size();
2372
2373 // Cut the chunk out from the chunk list.
2374 current = current->next();
2375 if (previous == NULL) {
2376 first_chunk_ = current;
2377 } else {
2378 previous->set_next(current);
2379 }
2380
2381 // Free the chunk.
2382 if (object->IsCode()) {
2383 LOG(CodeDeleteEvent(object->address()));
2384 }
2385 size_ -= chunk_size;
2386 page_count_--;
2387 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size);
2388 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2389 }
2390 }
2391}
2392
2393
2394bool LargeObjectSpace::Contains(HeapObject* object) {
2395 Address address = object->address();
2396 Page* page = Page::FromAddress(address);
2397
2398 SLOW_ASSERT(!page->IsLargeObjectPage()
2399 || !FindObject(address)->IsFailure());
2400
2401 return page->IsLargeObjectPage();
2402}
2403
2404
2405#ifdef DEBUG
2406// We do not assume that the large object iterator works, because it depends
2407// on the invariants we are checking during verification.
2408void LargeObjectSpace::Verify() {
2409 for (LargeObjectChunk* chunk = first_chunk_;
2410 chunk != NULL;
2411 chunk = chunk->next()) {
2412 // Each chunk contains an object that starts at the large object page's
2413 // object area start.
2414 HeapObject* object = chunk->GetObject();
2415 Page* page = Page::FromAddress(object->address());
2416 ASSERT(object->address() == page->ObjectAreaStart());
2417
2418 // The first word should be a map, and we expect all map pointers to be
2419 // in map space.
2420 Map* map = object->map();
2421 ASSERT(map->IsMap());
2422 ASSERT(Heap::map_space()->Contains(map));
2423
2424 // We have only code, sequential strings, fixed arrays, and byte arrays
2425 // in large object space.
2426 ASSERT(object->IsCode() || object->IsSeqString()
2427 || object->IsFixedArray() || object->IsByteArray());
2428
2429 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002430 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431
2432 // Byte arrays and strings don't have interior pointers.
2433 if (object->IsCode()) {
2434 VerifyPointersVisitor code_visitor;
2435 Code::cast(object)->ConvertICTargetsFromAddressToObject();
2436 object->IterateBody(map->instance_type(),
2437 object->Size(),
2438 &code_visitor);
2439 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
2440 } else if (object->IsFixedArray()) {
2441 // We loop over fixed arrays ourselves, rather then using the visitor,
2442 // because the visitor doesn't support the start/offset iteration
2443 // needed for IsRSetSet.
2444 FixedArray* array = FixedArray::cast(object);
2445 for (int j = 0; j < array->length(); j++) {
2446 Object* element = array->get(j);
2447 if (element->IsHeapObject()) {
2448 HeapObject* element_object = HeapObject::cast(element);
2449 ASSERT(Heap::Contains(element_object));
2450 ASSERT(element_object->map()->IsMap());
2451 if (Heap::InNewSpace(element_object)) {
2452 ASSERT(Page::IsRSetSet(object->address(),
2453 FixedArray::kHeaderSize + j * kPointerSize));
2454 }
2455 }
2456 }
2457 }
2458 }
2459}
2460
2461
2462void LargeObjectSpace::Print() {
2463 LargeObjectIterator it(this);
2464 while (it.has_next()) {
2465 it.next()->Print();
2466 }
2467}
2468
2469
2470void LargeObjectSpace::ReportStatistics() {
2471 PrintF(" size: %d\n", size_);
2472 int num_objects = 0;
2473 ClearHistograms();
2474 LargeObjectIterator it(this);
2475 while (it.has_next()) {
2476 num_objects++;
2477 CollectHistogramInfo(it.next());
2478 }
2479
2480 PrintF(" number of objects %d\n", num_objects);
2481 if (num_objects > 0) ReportHistogram(false);
2482}
2483
2484
2485void LargeObjectSpace::CollectCodeStatistics() {
2486 LargeObjectIterator obj_it(this);
2487 while (obj_it.has_next()) {
2488 HeapObject* obj = obj_it.next();
2489 if (obj->IsCode()) {
2490 Code* code = Code::cast(obj);
2491 code_kind_statistics[code->kind()] += code->Size();
2492 }
2493 }
2494}
2495
2496
2497void LargeObjectSpace::PrintRSet() {
2498 LargeObjectIterator it(this);
2499 while (it.has_next()) {
2500 HeapObject* object = it.next();
2501 if (object->IsFixedArray()) {
2502 Page* page = Page::FromAddress(object->address());
2503
2504 Address allocation_top = object->address() + object->Size();
2505 PrintF("large page 0x%x:\n", page);
2506 PrintRSetRange(page->RSetStart(), page->RSetEnd(),
2507 reinterpret_cast<Object**>(object->address()),
2508 allocation_top);
2509 int extra_array_bytes = object->Size() - Page::kObjectAreaSize;
2510 int extra_rset_bits = RoundUp(extra_array_bytes / kPointerSize,
2511 kBitsPerInt);
2512 PrintF("------------------------------------------------------------"
2513 "-----------\n");
2514 PrintRSetRange(allocation_top,
2515 allocation_top + extra_rset_bits / kBitsPerByte,
2516 reinterpret_cast<Object**>(object->address()
2517 + Page::kObjectAreaSize),
2518 allocation_top);
2519 PrintF("\n");
2520 }
2521 }
2522}
2523#endif // DEBUG
2524
2525} } // namespace v8::internal