blob: caa403eef9e50d2725f53205591dd65fd1c66e95 [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.
1617 // This is blocked by bug #1006953.
1618 // object->Verify();
1619
1620 // All the interior pointers should be contained in the heap and have
1621 // their remembered set bits set if they point to new space. Code
1622 // objects do not have remembered set bits that we care about.
1623 VerifyPointersAndRSetVisitor rset_visitor;
1624 VerifyPointersVisitor no_rset_visitor;
1625 int size = object->Size();
1626 if (object->IsCode()) {
1627 Code::cast(object)->ConvertICTargetsFromAddressToObject();
1628 object->IterateBody(map->instance_type(), size, &no_rset_visitor);
1629 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
1630 } else {
1631 object->IterateBody(map->instance_type(), size, &rset_visitor);
1632 }
1633
1634 current += size;
1635 }
1636
1637 // The allocation pointer should not be in the middle of an object.
1638 ASSERT(current == top);
1639 }
1640
1641 current_page = current_page->next_page();
1642 }
1643}
1644
1645
1646struct CommentStatistic {
1647 const char* comment;
1648 int size;
1649 int count;
1650 void Clear() {
1651 comment = NULL;
1652 size = 0;
1653 count = 0;
1654 }
1655};
1656
1657
1658// must be small, since an iteration is used for lookup
1659const int kMaxComments = 64;
1660static CommentStatistic comments_statistics[kMaxComments+1];
1661
1662
1663void PagedSpace::ReportCodeStatistics() {
1664 ReportCodeKindStatistics();
1665 PrintF("Code comment statistics (\" [ comment-txt : size/ "
1666 "count (average)\"):\n");
1667 for (int i = 0; i <= kMaxComments; i++) {
1668 const CommentStatistic& cs = comments_statistics[i];
1669 if (cs.size > 0) {
1670 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
1671 cs.size/cs.count);
1672 }
1673 }
1674 PrintF("\n");
1675}
1676
1677
1678void PagedSpace::ResetCodeStatistics() {
1679 ClearCodeKindStatistics();
1680 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
1681 comments_statistics[kMaxComments].comment = "Unknown";
1682 comments_statistics[kMaxComments].size = 0;
1683 comments_statistics[kMaxComments].count = 0;
1684}
1685
1686
1687// Adds comment to 'comment_statistics' table. Performance OK sa long as
1688// 'kMaxComments' is small
1689static void EnterComment(const char* comment, int delta) {
1690 // Do not count empty comments
1691 if (delta <= 0) return;
1692 CommentStatistic* cs = &comments_statistics[kMaxComments];
1693 // Search for a free or matching entry in 'comments_statistics': 'cs'
1694 // points to result.
1695 for (int i = 0; i < kMaxComments; i++) {
1696 if (comments_statistics[i].comment == NULL) {
1697 cs = &comments_statistics[i];
1698 cs->comment = comment;
1699 break;
1700 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
1701 cs = &comments_statistics[i];
1702 break;
1703 }
1704 }
1705 // Update entry for 'comment'
1706 cs->size += delta;
1707 cs->count += 1;
1708}
1709
1710
1711// Call for each nested comment start (start marked with '[ xxx', end marked
1712// with ']'. RelocIterator 'it' must point to a comment reloc info.
1713static void CollectCommentStatistics(RelocIterator* it) {
1714 ASSERT(!it->done());
1715 ASSERT(it->rinfo()->rmode() == comment);
1716 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
1717 if (tmp[0] != '[') {
1718 // Not a nested comment; skip
1719 return;
1720 }
1721
1722 // Search for end of nested comment or a new nested comment
1723 const char* const comment_txt =
1724 reinterpret_cast<const char*>(it->rinfo()->data());
1725 const byte* prev_pc = it->rinfo()->pc();
1726 int flat_delta = 0;
1727 it->next();
1728 while (true) {
1729 // All nested comments must be terminated properly, and therefore exit
1730 // from loop.
1731 ASSERT(!it->done());
1732 if (it->rinfo()->rmode() == comment) {
1733 const char* const txt =
1734 reinterpret_cast<const char*>(it->rinfo()->data());
1735 flat_delta += it->rinfo()->pc() - prev_pc;
1736 if (txt[0] == ']') break; // End of nested comment
1737 // A new comment
1738 CollectCommentStatistics(it);
1739 // Skip code that was covered with previous comment
1740 prev_pc = it->rinfo()->pc();
1741 }
1742 it->next();
1743 }
1744 EnterComment(comment_txt, flat_delta);
1745}
1746
1747
1748// Collects code size statistics:
1749// - by code kind
1750// - by code comment
1751void PagedSpace::CollectCodeStatistics() {
1752 HeapObjectIterator obj_it(this);
1753 while (obj_it.has_next()) {
1754 HeapObject* obj = obj_it.next();
1755 if (obj->IsCode()) {
1756 Code* code = Code::cast(obj);
1757 code_kind_statistics[code->kind()] += code->Size();
1758 RelocIterator it(code);
1759 int delta = 0;
1760 const byte* prev_pc = code->instruction_start();
1761 while (!it.done()) {
1762 if (it.rinfo()->rmode() == comment) {
1763 delta += it.rinfo()->pc() - prev_pc;
1764 CollectCommentStatistics(&it);
1765 prev_pc = it.rinfo()->pc();
1766 }
1767 it.next();
1768 }
1769
1770 ASSERT(code->instruction_start() <= prev_pc &&
1771 prev_pc <= code->relocation_start());
1772 delta += code->relocation_start() - prev_pc;
1773 EnterComment("NoComment", delta);
1774 }
1775 }
1776}
1777
1778
1779void OldSpace::ReportStatistics() {
1780 int pct = Available() * 100 / Capacity();
1781 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
1782 Capacity(), Waste(), Available(), pct);
1783
1784 // Report remembered set statistics.
1785 int rset_marked_pointers = 0;
1786 int rset_marked_arrays = 0;
1787 int rset_marked_array_elements = 0;
1788 int cross_gen_pointers = 0;
1789 int cross_gen_array_elements = 0;
1790
1791 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
1792 while (page_it.has_next()) {
1793 Page* p = page_it.next();
1794
1795 for (Address rset_addr = p->RSetStart();
1796 rset_addr < p->RSetEnd();
1797 rset_addr += kIntSize) {
1798 int rset = Memory::int_at(rset_addr);
1799 if (rset != 0) {
1800 // Bits were set
1801 int intoff = rset_addr - p->address();
1802 int bitoff = 0;
1803 for (; bitoff < kBitsPerInt; ++bitoff) {
1804 if ((rset & (1 << bitoff)) != 0) {
1805 int bitpos = intoff*kBitsPerByte + bitoff;
1806 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
1807 Object** obj = reinterpret_cast<Object**>(slot);
1808 if (*obj == Heap::fixed_array_map()) {
1809 rset_marked_arrays++;
1810 FixedArray* fa = FixedArray::cast(HeapObject::FromAddress(slot));
1811
1812 rset_marked_array_elements += fa->length();
1813 // Manually inline FixedArray::IterateBody
1814 Address elm_start = slot + FixedArray::kHeaderSize;
1815 Address elm_stop = elm_start + fa->length() * kPointerSize;
1816 for (Address elm_addr = elm_start;
1817 elm_addr < elm_stop; elm_addr += kPointerSize) {
1818 // Filter non-heap-object pointers
1819 Object** elm_p = reinterpret_cast<Object**>(elm_addr);
1820 if (Heap::InNewSpace(*elm_p))
1821 cross_gen_array_elements++;
1822 }
1823 } else {
1824 rset_marked_pointers++;
1825 if (Heap::InNewSpace(*obj))
1826 cross_gen_pointers++;
1827 }
1828 }
1829 }
1830 }
1831 }
1832 }
1833
1834 pct = rset_marked_pointers == 0 ?
1835 0 : cross_gen_pointers * 100 / rset_marked_pointers;
1836 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
1837 rset_marked_pointers, cross_gen_pointers, pct);
1838 PrintF(" rset_marked arrays %d, ", rset_marked_arrays);
1839 PrintF(" elements %d, ", rset_marked_array_elements);
1840 pct = rset_marked_array_elements == 0 ? 0
1841 : cross_gen_array_elements * 100 / rset_marked_array_elements;
1842 PrintF(" pointers to new space %d (%%%d)\n", cross_gen_array_elements, pct);
1843 PrintF(" total rset-marked bits %d\n",
1844 (rset_marked_pointers + rset_marked_arrays));
1845 pct = (rset_marked_pointers + rset_marked_array_elements) == 0 ? 0
1846 : (cross_gen_pointers + cross_gen_array_elements) * 100 /
1847 (rset_marked_pointers + rset_marked_array_elements);
1848 PrintF(" total rset pointers %d, true cross generation ones %d (%%%d)\n",
1849 (rset_marked_pointers + rset_marked_array_elements),
1850 (cross_gen_pointers + cross_gen_array_elements),
1851 pct);
1852
1853 ClearHistograms();
1854 HeapObjectIterator obj_it(this);
1855 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
1856 ReportHistogram(true);
1857}
1858
1859
1860// Dump the range of remembered set words between [start, end) corresponding
1861// to the pointers starting at object_p. The allocation_top is an object
1862// pointer which should not be read past. This is important for large object
1863// pages, where some bits in the remembered set range do not correspond to
1864// allocated addresses.
1865static void PrintRSetRange(Address start, Address end, Object** object_p,
1866 Address allocation_top) {
1867 Address rset_address = start;
1868
1869 // If the range starts on on odd numbered word (eg, for large object extra
1870 // remembered set ranges), print some spaces.
1871 if ((reinterpret_cast<uint32_t>(start) / kIntSize) % 2 == 1) {
1872 PrintF(" ");
1873 }
1874
1875 // Loop over all the words in the range.
1876 while (rset_address < end) {
1877 uint32_t rset_word = Memory::uint32_at(rset_address);
1878 int bit_position = 0;
1879
1880 // Loop over all the bits in the word.
1881 while (bit_position < kBitsPerInt) {
1882 if (object_p == reinterpret_cast<Object**>(allocation_top)) {
1883 // Print a bar at the allocation pointer.
1884 PrintF("|");
1885 } else if (object_p > reinterpret_cast<Object**>(allocation_top)) {
1886 // Do not dereference object_p past the allocation pointer.
1887 PrintF("#");
1888 } else if ((rset_word & (1 << bit_position)) == 0) {
1889 // Print a dot for zero bits.
1890 PrintF(".");
1891 } else if (Heap::InNewSpace(*object_p)) {
1892 // Print an X for one bits for pointers to new space.
1893 PrintF("X");
1894 } else {
1895 // Print a circle for one bits for pointers to old space.
1896 PrintF("o");
1897 }
1898
1899 // Print a space after every 8th bit except the last.
1900 if (bit_position % 8 == 7 && bit_position != (kBitsPerInt - 1)) {
1901 PrintF(" ");
1902 }
1903
1904 // Advance to next bit.
1905 bit_position++;
1906 object_p++;
1907 }
1908
1909 // Print a newline after every odd numbered word, otherwise a space.
1910 if ((reinterpret_cast<uint32_t>(rset_address) / kIntSize) % 2 == 1) {
1911 PrintF("\n");
1912 } else {
1913 PrintF(" ");
1914 }
1915
1916 // Advance to next remembered set word.
1917 rset_address += kIntSize;
1918 }
1919}
1920
1921
1922void PagedSpace::DoPrintRSet(const char* space_name) {
1923 PageIterator it(this, PageIterator::PAGES_IN_USE);
1924 while (it.has_next()) {
1925 Page* p = it.next();
1926 PrintF("%s page 0x%x:\n", space_name, p);
1927 PrintRSetRange(p->RSetStart(), p->RSetEnd(),
1928 reinterpret_cast<Object**>(p->ObjectAreaStart()),
1929 p->AllocationTop());
1930 PrintF("\n");
1931 }
1932}
1933
1934
1935void OldSpace::PrintRSet() { DoPrintRSet("old"); }
1936#endif
1937
1938// -----------------------------------------------------------------------------
1939// MapSpace implementation
1940
1941void MapSpace::PrepareForMarkCompact(bool will_compact) {
1942 if (will_compact) {
1943 // Reset relocation info.
1944 MCResetRelocationInfo();
1945
1946 // Initialize map index entry.
1947 int page_count = 0;
1948 PageIterator it(this, PageIterator::ALL_PAGES);
1949 while (it.has_next()) {
1950 ASSERT_MAP_PAGE_INDEX(page_count);
1951
1952 Page* p = it.next();
1953 ASSERT(p->mc_page_index == page_count);
1954
1955 page_addresses_[page_count++] = p->address();
1956 }
1957
1958 // During a compacting collection, everything in the space is considered
1959 // 'available' (set by the call to MCResetRelocationInfo) and we will
1960 // rediscover live and wasted bytes during the collection.
1961 ASSERT(Available() == Capacity());
1962 } else {
1963 // During a non-compacting collection, everything below the linear
1964 // allocation pointer except wasted top-of-page blocks is considered
1965 // allocated and we will rediscover available bytes during the
1966 // collection.
1967 accounting_stats_.AllocateBytes(free_list_.available());
1968 }
1969
kasper.lund7276f142008-07-30 08:49:36 +00001970 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972}
1973
1974
1975void MapSpace::MCCommitRelocationInfo() {
1976 // Update fast allocation info.
1977 allocation_info_.top = mc_forwarding_info_.top;
1978 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001979 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980
1981 // The space is compacted and we haven't yet wasted any space.
1982 ASSERT(Waste() == 0);
1983
1984 // Update allocation_top of each page in use and compute waste.
1985 int computed_size = 0;
1986 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1987 while (it.has_next()) {
1988 Page* page = it.next();
1989 Address page_top = page->AllocationTop();
1990 computed_size += page_top - page->ObjectAreaStart();
1991 if (it.has_next()) {
1992 accounting_stats_.WasteBytes(page->ObjectAreaEnd() - page_top);
1993 }
1994 }
1995
1996 // Make sure the computed size - based on the used portion of the
1997 // pages in use - matches the size we adjust during allocation.
1998 ASSERT(computed_size == Size());
1999}
2000
2001
kasper.lund7276f142008-07-30 08:49:36 +00002002// Slow case for normal allocation. Try in order: (1) allocate in the next
2003// page in the space, (2) allocate off the space's free list, (3) expand the
2004// space, (4) fail.
2005HeapObject* MapSpace::SlowAllocateRaw(int size_in_bytes) {
2006 // Linear allocation in this space has failed. If there is another page
2007 // in the space, move to that page and allocate there. This allocation
2008 // should succeed.
2009 Page* current_page = TopPageOf(allocation_info_);
2010 if (current_page->next_page()->is_valid()) {
2011 return AllocateInNextPage(current_page, size_in_bytes);
2012 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013
kasper.lund7276f142008-07-30 08:49:36 +00002014 // There is no next page in this space. Try free list allocation. The
2015 // map space free list implicitly assumes that all free blocks are map
2016 // sized.
2017 if (size_in_bytes == Map::kSize) {
2018 Object* result = free_list_.Allocate();
2019 if (!result->IsFailure()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 accounting_stats_.AllocateBytes(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002021 return HeapObject::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 }
2023 }
kasper.lund7276f142008-07-30 08:49:36 +00002024
2025 // Free list allocation failed and there is no next page. Try to expand
2026 // the space and allocate in the new next page.
2027 ASSERT(!current_page->next_page()->is_valid());
2028 if (Expand(current_page)) {
2029 return AllocateInNextPage(current_page, size_in_bytes);
2030 }
2031
2032 // Finally, fail.
2033 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034}
2035
2036
kasper.lund7276f142008-07-30 08:49:36 +00002037// Move to the next page (there is assumed to be one) and allocate there.
2038// The top of page block is always wasted, because it is too small to hold a
2039// map.
2040HeapObject* MapSpace::AllocateInNextPage(Page* current_page,
2041 int size_in_bytes) {
2042 ASSERT(current_page->next_page()->is_valid());
2043 ASSERT(current_page->ObjectAreaEnd() - allocation_info_.top == kPageExtra);
2044 accounting_stats_.WasteBytes(kPageExtra);
2045 SetAllocationInfo(&allocation_info_, current_page->next_page());
2046 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047}
2048
2049
2050#ifdef DEBUG
2051// We do not assume that the PageIterator works, because it depends on the
2052// invariants we are checking during verification.
2053void MapSpace::Verify() {
2054 // The allocation pointer should be valid, and it should be in a page in the
2055 // space.
kasper.lund7276f142008-07-30 08:49:36 +00002056 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
2058 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
2059
2060 // Loop over all the pages.
2061 bool above_allocation_top = false;
2062 Page* current_page = first_page_;
2063 while (current_page->is_valid()) {
2064 if (above_allocation_top) {
2065 // We don't care what's above the allocation top.
2066 } else {
2067 // Unless this is the last page in the space containing allocated
2068 // objects, the allocation top should be at a constant offset from the
2069 // object area end.
2070 Address top = current_page->AllocationTop();
2071 if (current_page == top_page) {
2072 ASSERT(top == allocation_info_.top);
2073 // The next page will be above the allocation top.
2074 above_allocation_top = true;
2075 } else {
2076 ASSERT(top == current_page->ObjectAreaEnd() - kPageExtra);
2077 }
2078
2079 // It should be packed with objects from the bottom to the top.
2080 Address current = current_page->ObjectAreaStart();
2081 while (current < top) {
2082 HeapObject* object = HeapObject::FromAddress(current);
2083
2084 // The first word should be a map, and we expect all map pointers to
2085 // be in map space.
2086 Map* map = object->map();
2087 ASSERT(map->IsMap());
2088 ASSERT(Heap::map_space()->Contains(map));
2089
2090 // The object should be a map or a byte array.
2091 ASSERT(object->IsMap() || object->IsByteArray());
2092
2093 // The object itself should look OK.
2094 // This is blocked by bug #1006953.
2095 // object->Verify();
2096
2097 // All the interior pointers should be contained in the heap and
2098 // have their remembered set bits set if they point to new space.
2099 VerifyPointersAndRSetVisitor visitor;
2100 int size = object->Size();
2101 object->IterateBody(map->instance_type(), size, &visitor);
2102
2103 current += size;
2104 }
2105
2106 // The allocation pointer should not be in the middle of an object.
2107 ASSERT(current == top);
2108 }
2109
2110 current_page = current_page->next_page();
2111 }
2112}
2113
2114
2115void MapSpace::ReportStatistics() {
2116 int pct = Available() * 100 / Capacity();
2117 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2118 Capacity(), Waste(), Available(), pct);
2119
2120 // Report remembered set statistics.
2121 int rset_marked_pointers = 0;
2122 int cross_gen_pointers = 0;
2123
2124 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2125 while (page_it.has_next()) {
2126 Page* p = page_it.next();
2127
2128 for (Address rset_addr = p->RSetStart();
2129 rset_addr < p->RSetEnd();
2130 rset_addr += kIntSize) {
2131 int rset = Memory::int_at(rset_addr);
2132 if (rset != 0) {
2133 // Bits were set
2134 int intoff = rset_addr - p->address();
2135 int bitoff = 0;
2136 for (; bitoff < kBitsPerInt; ++bitoff) {
2137 if ((rset & (1 << bitoff)) != 0) {
2138 int bitpos = intoff*kBitsPerByte + bitoff;
2139 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2140 Object** obj = reinterpret_cast<Object**>(slot);
2141 rset_marked_pointers++;
2142 if (Heap::InNewSpace(*obj))
2143 cross_gen_pointers++;
2144 }
2145 }
2146 }
2147 }
2148 }
2149
2150 pct = rset_marked_pointers == 0 ?
2151 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2152 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2153 rset_marked_pointers, cross_gen_pointers, pct);
2154
2155 ClearHistograms();
2156 HeapObjectIterator obj_it(this);
2157 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
2158 ReportHistogram(false);
2159}
2160
2161
2162void MapSpace::PrintRSet() { DoPrintRSet("map"); }
2163#endif
2164
2165
2166// -----------------------------------------------------------------------------
2167// LargeObjectIterator
2168
2169LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2170 current_ = space->first_chunk_;
2171 size_func_ = NULL;
2172}
2173
2174
2175LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2176 HeapObjectCallback size_func) {
2177 current_ = space->first_chunk_;
2178 size_func_ = size_func;
2179}
2180
2181
2182HeapObject* LargeObjectIterator::next() {
2183 ASSERT(has_next());
2184 HeapObject* object = current_->GetObject();
2185 current_ = current_->next();
2186 return object;
2187}
2188
2189
2190// -----------------------------------------------------------------------------
2191// LargeObjectChunk
2192
2193LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
kasper.lund7276f142008-07-30 08:49:36 +00002194 size_t* chunk_size,
2195 bool executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 size_t requested = ChunkSizeFor(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002197 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2198 chunk_size,
2199 executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 if (mem == NULL) return NULL;
2201 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2202 if (*chunk_size < requested) {
2203 MemoryAllocator::FreeRawMemory(mem, *chunk_size);
2204 LOG(DeleteEvent("LargeObjectChunk", mem));
2205 return NULL;
2206 }
2207 return reinterpret_cast<LargeObjectChunk*>(mem);
2208}
2209
2210
2211int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
2212 int os_alignment = OS::AllocateAlignment();
2213 if (os_alignment < Page::kPageSize)
2214 size_in_bytes += (Page::kPageSize - os_alignment);
2215 return size_in_bytes + Page::kObjectStartOffset;
2216}
2217
2218// -----------------------------------------------------------------------------
2219// LargeObjectSpace
2220
kasper.lund7276f142008-07-30 08:49:36 +00002221LargeObjectSpace::LargeObjectSpace(AllocationSpace id, bool executable)
2222 : Space(id, executable),
2223 first_chunk_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 size_(0),
2225 page_count_(0) {}
2226
2227
2228bool LargeObjectSpace::Setup() {
2229 first_chunk_ = NULL;
2230 size_ = 0;
2231 page_count_ = 0;
2232 return true;
2233}
2234
2235
2236void LargeObjectSpace::TearDown() {
2237 while (first_chunk_ != NULL) {
2238 LargeObjectChunk* chunk = first_chunk_;
2239 first_chunk_ = first_chunk_->next();
2240 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
2241 MemoryAllocator::FreeRawMemory(chunk->address(), chunk->size());
2242 }
2243
2244 size_ = 0;
2245 page_count_ = 0;
2246}
2247
2248
2249Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
2250 int object_size) {
2251 ASSERT(0 < object_size && object_size <= requested_size);
2252 size_t chunk_size;
2253 LargeObjectChunk* chunk =
kasper.lund7276f142008-07-30 08:49:36 +00002254 LargeObjectChunk::New(requested_size, &chunk_size, executable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 if (chunk == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00002256 return Failure::RetryAfterGC(requested_size, identity());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 }
2258
2259 size_ += chunk_size;
2260 page_count_++;
2261 chunk->set_next(first_chunk_);
2262 chunk->set_size(chunk_size);
2263 first_chunk_ = chunk;
2264
2265 // Set the object address and size in the page header and clear its
2266 // remembered set.
2267 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2268 Address object_address = page->ObjectAreaStart();
2269 // Clear the low order bit of the second word in the page to flag it as a
2270 // large object page. If the chunk_size happened to be written there, its
2271 // low order bit should already be clear.
2272 ASSERT((chunk_size & 0x1) == 0);
2273 page->is_normal_page &= ~0x1;
2274 page->ClearRSet();
2275 int extra_bytes = requested_size - object_size;
2276 if (extra_bytes > 0) {
2277 // The extra memory for the remembered set should be cleared.
2278 memset(object_address + object_size, 0, extra_bytes);
2279 }
2280
2281 return HeapObject::FromAddress(object_address);
2282}
2283
2284
2285Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2286 ASSERT(0 < size_in_bytes);
2287 return AllocateRawInternal(size_in_bytes, size_in_bytes);
2288}
2289
2290
2291Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
2292 int extra_rset_bytes = ExtraRSetBytesFor(size_in_bytes);
2293 return AllocateRawInternal(size_in_bytes + extra_rset_bytes, size_in_bytes);
2294}
2295
2296
2297// GC support
2298Object* LargeObjectSpace::FindObject(Address a) {
2299 for (LargeObjectChunk* chunk = first_chunk_;
2300 chunk != NULL;
2301 chunk = chunk->next()) {
2302 Address chunk_address = chunk->address();
2303 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2304 return chunk->GetObject();
2305 }
2306 }
2307 return Failure::Exception();
2308}
2309
2310
2311void LargeObjectSpace::ClearRSet() {
2312 ASSERT(Page::is_rset_in_use());
2313
2314 LargeObjectIterator it(this);
2315 while (it.has_next()) {
2316 HeapObject* object = it.next();
2317 // We only have code, sequential strings, or fixed arrays in large
2318 // object space, and only fixed arrays need remembered set support.
2319 if (object->IsFixedArray()) {
2320 // Clear the normal remembered set region of the page;
2321 Page* page = Page::FromAddress(object->address());
2322 page->ClearRSet();
2323
2324 // Clear the extra remembered set.
2325 int size = object->Size();
2326 int extra_rset_bytes = ExtraRSetBytesFor(size);
2327 memset(object->address() + size, 0, extra_rset_bytes);
2328 }
2329 }
2330}
2331
2332
2333void LargeObjectSpace::IterateRSet(ObjectSlotCallback copy_object_func) {
2334 ASSERT(Page::is_rset_in_use());
2335
2336 LargeObjectIterator it(this);
2337 while (it.has_next()) {
2338 // We only have code, sequential strings, or fixed arrays in large
2339 // object space, and only fixed arrays can possibly contain pointers to
2340 // the young generation.
2341 HeapObject* object = it.next();
2342 if (object->IsFixedArray()) {
2343 // Iterate the normal page remembered set range.
2344 Page* page = Page::FromAddress(object->address());
2345 Address object_end = object->address() + object->Size();
2346 Heap::IterateRSetRange(page->ObjectAreaStart(),
2347 Min(page->ObjectAreaEnd(), object_end),
2348 page->RSetStart(),
2349 copy_object_func);
2350
2351 // Iterate the extra array elements.
2352 if (object_end > page->ObjectAreaEnd()) {
2353 Heap::IterateRSetRange(page->ObjectAreaEnd(), object_end,
2354 object_end, copy_object_func);
2355 }
2356 }
2357 }
2358}
2359
2360
2361void LargeObjectSpace::FreeUnmarkedObjects() {
2362 LargeObjectChunk* previous = NULL;
2363 LargeObjectChunk* current = first_chunk_;
2364 while (current != NULL) {
2365 HeapObject* object = current->GetObject();
kasper.lund7276f142008-07-30 08:49:36 +00002366 if (object->IsMarked()) {
2367 object->ClearMark();
2368 MarkCompactCollector::tracer()->decrement_marked_count();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 previous = current;
2370 current = current->next();
2371 } else {
2372 Address chunk_address = current->address();
2373 size_t chunk_size = current->size();
2374
2375 // Cut the chunk out from the chunk list.
2376 current = current->next();
2377 if (previous == NULL) {
2378 first_chunk_ = current;
2379 } else {
2380 previous->set_next(current);
2381 }
2382
2383 // Free the chunk.
2384 if (object->IsCode()) {
2385 LOG(CodeDeleteEvent(object->address()));
2386 }
2387 size_ -= chunk_size;
2388 page_count_--;
2389 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size);
2390 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2391 }
2392 }
2393}
2394
2395
2396bool LargeObjectSpace::Contains(HeapObject* object) {
2397 Address address = object->address();
2398 Page* page = Page::FromAddress(address);
2399
2400 SLOW_ASSERT(!page->IsLargeObjectPage()
2401 || !FindObject(address)->IsFailure());
2402
2403 return page->IsLargeObjectPage();
2404}
2405
2406
2407#ifdef DEBUG
2408// We do not assume that the large object iterator works, because it depends
2409// on the invariants we are checking during verification.
2410void LargeObjectSpace::Verify() {
2411 for (LargeObjectChunk* chunk = first_chunk_;
2412 chunk != NULL;
2413 chunk = chunk->next()) {
2414 // Each chunk contains an object that starts at the large object page's
2415 // object area start.
2416 HeapObject* object = chunk->GetObject();
2417 Page* page = Page::FromAddress(object->address());
2418 ASSERT(object->address() == page->ObjectAreaStart());
2419
2420 // The first word should be a map, and we expect all map pointers to be
2421 // in map space.
2422 Map* map = object->map();
2423 ASSERT(map->IsMap());
2424 ASSERT(Heap::map_space()->Contains(map));
2425
2426 // We have only code, sequential strings, fixed arrays, and byte arrays
2427 // in large object space.
2428 ASSERT(object->IsCode() || object->IsSeqString()
2429 || object->IsFixedArray() || object->IsByteArray());
2430
2431 // The object itself should look OK.
2432 // This is blocked by bug #1006953.
2433 // object->Verify();
2434
2435 // Byte arrays and strings don't have interior pointers.
2436 if (object->IsCode()) {
2437 VerifyPointersVisitor code_visitor;
2438 Code::cast(object)->ConvertICTargetsFromAddressToObject();
2439 object->IterateBody(map->instance_type(),
2440 object->Size(),
2441 &code_visitor);
2442 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
2443 } else if (object->IsFixedArray()) {
2444 // We loop over fixed arrays ourselves, rather then using the visitor,
2445 // because the visitor doesn't support the start/offset iteration
2446 // needed for IsRSetSet.
2447 FixedArray* array = FixedArray::cast(object);
2448 for (int j = 0; j < array->length(); j++) {
2449 Object* element = array->get(j);
2450 if (element->IsHeapObject()) {
2451 HeapObject* element_object = HeapObject::cast(element);
2452 ASSERT(Heap::Contains(element_object));
2453 ASSERT(element_object->map()->IsMap());
2454 if (Heap::InNewSpace(element_object)) {
2455 ASSERT(Page::IsRSetSet(object->address(),
2456 FixedArray::kHeaderSize + j * kPointerSize));
2457 }
2458 }
2459 }
2460 }
2461 }
2462}
2463
2464
2465void LargeObjectSpace::Print() {
2466 LargeObjectIterator it(this);
2467 while (it.has_next()) {
2468 it.next()->Print();
2469 }
2470}
2471
2472
2473void LargeObjectSpace::ReportStatistics() {
2474 PrintF(" size: %d\n", size_);
2475 int num_objects = 0;
2476 ClearHistograms();
2477 LargeObjectIterator it(this);
2478 while (it.has_next()) {
2479 num_objects++;
2480 CollectHistogramInfo(it.next());
2481 }
2482
2483 PrintF(" number of objects %d\n", num_objects);
2484 if (num_objects > 0) ReportHistogram(false);
2485}
2486
2487
2488void LargeObjectSpace::CollectCodeStatistics() {
2489 LargeObjectIterator obj_it(this);
2490 while (obj_it.has_next()) {
2491 HeapObject* obj = obj_it.next();
2492 if (obj->IsCode()) {
2493 Code* code = Code::cast(obj);
2494 code_kind_statistics[code->kind()] += code->Size();
2495 }
2496 }
2497}
2498
2499
2500void LargeObjectSpace::PrintRSet() {
2501 LargeObjectIterator it(this);
2502 while (it.has_next()) {
2503 HeapObject* object = it.next();
2504 if (object->IsFixedArray()) {
2505 Page* page = Page::FromAddress(object->address());
2506
2507 Address allocation_top = object->address() + object->Size();
2508 PrintF("large page 0x%x:\n", page);
2509 PrintRSetRange(page->RSetStart(), page->RSetEnd(),
2510 reinterpret_cast<Object**>(object->address()),
2511 allocation_top);
2512 int extra_array_bytes = object->Size() - Page::kObjectAreaSize;
2513 int extra_rset_bits = RoundUp(extra_array_bytes / kPointerSize,
2514 kBitsPerInt);
2515 PrintF("------------------------------------------------------------"
2516 "-----------\n");
2517 PrintRSetRange(allocation_top,
2518 allocation_top + extra_rset_bits / kBitsPerByte,
2519 reinterpret_cast<Object**>(object->address()
2520 + Page::kObjectAreaSize),
2521 allocation_top);
2522 PrintF("\n");
2523 }
2524 }
2525}
2526#endif // DEBUG
2527
2528} } // namespace v8::internal