blob: 7920187cc3bc460e8dc56b9f4b66b91a36f17483 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000230 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 if (size_ + static_cast<int>(requested) > capacity_) return NULL;
232
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000233 void* mem = OS::Allocate(requested, allocated, executable == 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());
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000319 if (!initial_chunk_->Commit(start, size, owner->executable() == 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,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000335 Executability 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
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000477PagedSpace::PagedSpace(int max_capacity,
478 AllocationSpace id,
479 Executability executable)
kasper.lund7276f142008-07-30 08:49:36 +0000480 : Space(id, executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
482 * Page::kObjectAreaSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 accounting_stats_.Clear();
484
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 allocation_info_.top = NULL;
486 allocation_info_.limit = NULL;
487
488 mc_forwarding_info_.top = NULL;
489 mc_forwarding_info_.limit = NULL;
490}
491
492
493bool PagedSpace::Setup(Address start, size_t size) {
494 if (HasBeenSetup()) return false;
495
496 int num_pages = 0;
497 // Try to use the virtual memory range passed to us. If it is too small to
498 // contain at least one page, ignore it and allocate instead.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000499 int pages_in_chunk = PagesInChunk(start, size);
500 if (pages_in_chunk > 0) {
501 first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize),
502 Page::kPageSize * pages_in_chunk,
503 this, &num_pages);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 } else {
505 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
506 max_capacity_ / Page::kObjectAreaSize);
507 first_page_ =
508 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
509 if (!first_page_->is_valid()) return false;
510 }
511
512 // We are sure that the first page is valid and that we have at least one
513 // page.
514 ASSERT(first_page_->is_valid());
515 ASSERT(num_pages > 0);
516 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
517 ASSERT(Capacity() <= max_capacity_);
518
519 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
520 p->ClearRSet();
521 }
522
523 // Use first_page_ for allocation.
524 SetAllocationInfo(&allocation_info_, first_page_);
525
526 return true;
527}
528
529
530bool PagedSpace::HasBeenSetup() {
531 return (Capacity() > 0);
532}
533
534
535void PagedSpace::TearDown() {
536 first_page_ = MemoryAllocator::FreePages(first_page_);
537 ASSERT(!first_page_->is_valid());
538
539 accounting_stats_.Clear();
540}
541
542
543void PagedSpace::ClearRSet() {
544 PageIterator it(this, PageIterator::ALL_PAGES);
545 while (it.has_next()) {
546 it.next()->ClearRSet();
547 }
548}
549
550
551Object* PagedSpace::FindObject(Address addr) {
552#ifdef DEBUG
553 // Note: this function can only be called before or after mark-compact GC
554 // because it accesses map pointers.
555 ASSERT(!MarkCompactCollector::in_use());
556#endif
557
558 if (!Contains(addr)) return Failure::Exception();
559
560 Page* p = Page::FromAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +0000561 ASSERT(IsUsed(p));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 Address cur = p->ObjectAreaStart();
563 Address end = p->AllocationTop();
564 while (cur < end) {
565 HeapObject* obj = HeapObject::FromAddress(cur);
566 Address next = cur + obj->Size();
567 if ((cur <= addr) && (addr < next)) return obj;
568 cur = next;
569 }
570
kasper.lund7276f142008-07-30 08:49:36 +0000571 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 return Failure::Exception();
573}
574
575
kasper.lund7276f142008-07-30 08:49:36 +0000576bool PagedSpace::IsUsed(Page* page) {
577 PageIterator it(this, PageIterator::PAGES_IN_USE);
578 while (it.has_next()) {
579 if (page == it.next()) return true;
580 }
581 return false;
582}
583
584
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
586 alloc_info->top = p->ObjectAreaStart();
587 alloc_info->limit = p->ObjectAreaEnd();
kasper.lund7276f142008-07-30 08:49:36 +0000588 ASSERT(alloc_info->VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589}
590
591
592void PagedSpace::MCResetRelocationInfo() {
593 // Set page indexes.
594 int i = 0;
595 PageIterator it(this, PageIterator::ALL_PAGES);
596 while (it.has_next()) {
597 Page* p = it.next();
598 p->mc_page_index = i++;
599 }
600
601 // Set mc_forwarding_info_ to the first page in the space.
602 SetAllocationInfo(&mc_forwarding_info_, first_page_);
603 // All the bytes in the space are 'available'. We will rediscover
604 // allocated and wasted bytes during GC.
605 accounting_stats_.Reset();
606}
607
608
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
610#ifdef DEBUG
611 // The Contains function considers the address at the beginning of a
612 // page in the page, MCSpaceOffsetForAddress considers it is in the
613 // previous page.
614 if (Page::IsAlignedToPageSize(addr)) {
615 ASSERT(Contains(addr - kPointerSize));
616 } else {
617 ASSERT(Contains(addr));
618 }
619#endif
620
621 // If addr is at the end of a page, it belongs to previous page
622 Page* p = Page::IsAlignedToPageSize(addr)
623 ? Page::FromAllocationTop(addr)
624 : Page::FromAddress(addr);
625 int index = p->mc_page_index;
626 return (index * Page::kPageSize) + p->Offset(addr);
627}
628
629
kasper.lund7276f142008-07-30 08:49:36 +0000630// Slow case for reallocating and promoting objects during a compacting
631// collection. This function is not space-specific.
632HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
633 Page* current_page = TopPageOf(mc_forwarding_info_);
634 if (!current_page->next_page()->is_valid()) {
635 if (!Expand(current_page)) {
636 return NULL;
637 }
638 }
639
640 // There are surely more pages in the space now.
641 ASSERT(current_page->next_page()->is_valid());
642 // We do not add the top of page block for current page to the space's
643 // free list---the block may contain live objects so we cannot write
644 // bookkeeping information to it. Instead, we will recover top of page
645 // blocks when we move objects to their new locations.
646 //
647 // We do however write the allocation pointer to the page. The encoding
648 // of forwarding addresses is as an offset in terms of live bytes, so we
649 // need quick access to the allocation top of each page to decode
650 // forwarding addresses.
651 current_page->mc_relocation_top = mc_forwarding_info_.top;
652 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
653 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
654}
655
656
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657bool PagedSpace::Expand(Page* last_page) {
658 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
659 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
660
661 if (Capacity() == max_capacity_) return false;
662
663 ASSERT(Capacity() < max_capacity_);
664 // Last page must be valid and its next page is invalid.
665 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
666
667 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
668 if (available_pages <= 0) return false;
669
670 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
671 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
672 if (!p->is_valid()) return false;
673
674 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
675 ASSERT(Capacity() <= max_capacity_);
676
677 MemoryAllocator::SetNextPage(last_page, p);
678
679 // Clear remembered set of new pages.
680 while (p->is_valid()) {
681 p->ClearRSet();
682 p = p->next_page();
683 }
684
685 return true;
686}
687
688
689#ifdef DEBUG
690int PagedSpace::CountTotalPages() {
691 int count = 0;
692 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
693 count++;
694 }
695 return count;
696}
697#endif
698
699
700void PagedSpace::Shrink() {
701 // Release half of free pages.
702 Page* top_page = AllocationTopPage();
703 ASSERT(top_page->is_valid());
704
705 // Loop over the pages from the top page to the end of the space to count
706 // the number of pages to keep and find the last page to keep.
707 int free_pages = 0;
708 int pages_to_keep = 0; // Of the free pages.
709 Page* last_page_to_keep = top_page;
710 Page* current_page = top_page->next_page();
711 // Loop over the pages to the end of the space.
712 while (current_page->is_valid()) {
kasper.lund7276f142008-07-30 08:49:36 +0000713 // Advance last_page_to_keep every other step to end up at the midpoint.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 if ((free_pages & 0x1) == 1) {
715 pages_to_keep++;
716 last_page_to_keep = last_page_to_keep->next_page();
717 }
718 free_pages++;
719 current_page = current_page->next_page();
720 }
721
722 // Free pages after last_page_to_keep, and adjust the next_page link.
723 Page* p = MemoryAllocator::FreePages(last_page_to_keep->next_page());
724 MemoryAllocator::SetNextPage(last_page_to_keep, p);
725
726 // Since pages are only freed in whole chunks, we may have kept more than
727 // pages_to_keep.
728 while (p->is_valid()) {
729 pages_to_keep++;
730 p = p->next_page();
731 }
732
733 // The difference between free_pages and pages_to_keep is the number of
734 // pages actually freed.
735 ASSERT(pages_to_keep <= free_pages);
736 int bytes_freed = (free_pages - pages_to_keep) * Page::kObjectAreaSize;
737 accounting_stats_.ShrinkSpace(bytes_freed);
738
739 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
740}
741
742
743bool PagedSpace::EnsureCapacity(int capacity) {
744 if (Capacity() >= capacity) return true;
745
746 // Start from the allocation top and loop to the last page in the space.
747 Page* last_page = AllocationTopPage();
748 Page* next_page = last_page->next_page();
749 while (next_page->is_valid()) {
750 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
751 next_page = last_page->next_page();
752 }
753
754 // Expand the space until it has the required capacity or expansion fails.
755 do {
756 if (!Expand(last_page)) return false;
757 ASSERT(last_page->next_page()->is_valid());
758 last_page =
759 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
760 } while (Capacity() < capacity);
761
762 return true;
763}
764
765
766#ifdef DEBUG
767void PagedSpace::Print() { }
768#endif
769
770
771// -----------------------------------------------------------------------------
772// NewSpace implementation
773
774NewSpace::NewSpace(int initial_semispace_capacity,
kasper.lund7276f142008-07-30 08:49:36 +0000775 int maximum_semispace_capacity,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000776 AllocationSpace id)
777 : Space(id, NOT_EXECUTABLE) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
779 ASSERT(IsPowerOf2(maximum_semispace_capacity));
780 maximum_capacity_ = maximum_semispace_capacity;
781 capacity_ = initial_semispace_capacity;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000782 to_space_ = new SemiSpace(capacity_, maximum_capacity_, id);
783 from_space_ = new SemiSpace(capacity_, maximum_capacity_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784
785 // Allocate and setup the histogram arrays if necessary.
786#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
787 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
788 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
789
790#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
791 promoted_histogram_[name].set_name(#name);
792 INSTANCE_TYPE_LIST(SET_NAME)
793#undef SET_NAME
794#endif
795}
796
797
798bool NewSpace::Setup(Address start, int size) {
799 ASSERT(size == 2 * maximum_capacity_);
800 ASSERT(IsAddressAligned(start, size, 0));
801
802 if (to_space_ == NULL
803 || !to_space_->Setup(start, maximum_capacity_)) {
804 return false;
805 }
806 if (from_space_ == NULL
807 || !from_space_->Setup(start + maximum_capacity_, maximum_capacity_)) {
808 return false;
809 }
810
811 start_ = start;
812 address_mask_ = ~(size - 1);
813 object_mask_ = address_mask_ | kHeapObjectTag;
814 object_expected_ = reinterpret_cast<uint32_t>(start) | kHeapObjectTag;
815
816 allocation_info_.top = to_space_->low();
817 allocation_info_.limit = to_space_->high();
818 mc_forwarding_info_.top = NULL;
819 mc_forwarding_info_.limit = NULL;
820
821 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
822 return true;
823}
824
825
826void NewSpace::TearDown() {
827#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
828 if (allocated_histogram_) {
829 DeleteArray(allocated_histogram_);
830 allocated_histogram_ = NULL;
831 }
832 if (promoted_histogram_) {
833 DeleteArray(promoted_histogram_);
834 promoted_histogram_ = NULL;
835 }
836#endif
837
838 start_ = NULL;
839 capacity_ = 0;
840 allocation_info_.top = NULL;
841 allocation_info_.limit = NULL;
842 mc_forwarding_info_.top = NULL;
843 mc_forwarding_info_.limit = NULL;
844
845 if (to_space_ != NULL) {
846 to_space_->TearDown();
847 delete to_space_;
848 to_space_ = NULL;
849 }
850
851 if (from_space_ != NULL) {
852 from_space_->TearDown();
853 delete from_space_;
854 from_space_ = NULL;
855 }
856}
857
858
859void NewSpace::Flip() {
860 SemiSpace* tmp = from_space_;
861 from_space_ = to_space_;
862 to_space_ = tmp;
863}
864
865
866bool NewSpace::Double() {
867 ASSERT(capacity_ <= maximum_capacity_ / 2);
868 // TODO(1240712): Failure to double the from space can result in
869 // semispaces of different sizes. In the event of that failure, the
870 // to space doubling should be rolled back before returning false.
871 if (!to_space_->Double() || !from_space_->Double()) return false;
872 capacity_ *= 2;
873 allocation_info_.limit = to_space_->high();
874 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
875 return true;
876}
877
878
879void NewSpace::ResetAllocationInfo() {
880 allocation_info_.top = to_space_->low();
881 allocation_info_.limit = to_space_->high();
882 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
883}
884
885
886void NewSpace::MCResetRelocationInfo() {
887 mc_forwarding_info_.top = from_space_->low();
888 mc_forwarding_info_.limit = from_space_->high();
889 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
890}
891
892
893void NewSpace::MCCommitRelocationInfo() {
894 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
895 // valid allocation info for the to space.
896 allocation_info_.top = mc_forwarding_info_.top;
897 allocation_info_.limit = to_space_->high();
898 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
899}
900
901
902#ifdef DEBUG
903// We do not use the SemispaceIterator because verification doesn't assume
904// that it works (it depends on the invariants we are checking).
905void NewSpace::Verify() {
906 // The allocation pointer should be in the space or at the very end.
907 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
908
909 // There should be objects packed in from the low address up to the
910 // allocation pointer.
911 Address current = to_space_->low();
912 while (current < top()) {
913 HeapObject* object = HeapObject::FromAddress(current);
914
915 // The first word should be a map, and we expect all map pointers to
916 // be in map space.
917 Map* map = object->map();
918 ASSERT(map->IsMap());
919 ASSERT(Heap::map_space()->Contains(map));
920
921 // The object should not be code or a map.
922 ASSERT(!object->IsMap());
923 ASSERT(!object->IsCode());
924
925 // The object itself should look OK.
926 object->Verify();
927
928 // All the interior pointers should be contained in the heap.
929 VerifyPointersVisitor visitor;
930 int size = object->Size();
931 object->IterateBody(map->instance_type(), size, &visitor);
932
933 current += size;
934 }
935
936 // The allocation pointer should not be in the middle of an object.
937 ASSERT(current == top());
938}
939#endif
940
941
942// -----------------------------------------------------------------------------
943// SemiSpace implementation
944
kasper.lund7276f142008-07-30 08:49:36 +0000945SemiSpace::SemiSpace(int initial_capacity,
946 int maximum_capacity,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000947 AllocationSpace id)
948 : Space(id, NOT_EXECUTABLE), capacity_(initial_capacity),
kasper.lund7276f142008-07-30 08:49:36 +0000949 maximum_capacity_(maximum_capacity), start_(NULL), age_mark_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950}
951
952
953bool SemiSpace::Setup(Address start, int size) {
954 ASSERT(size == maximum_capacity_);
kasper.lund7276f142008-07-30 08:49:36 +0000955 if (!MemoryAllocator::CommitBlock(start, capacity_, executable())) {
956 return false;
957 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958
959 start_ = start;
960 address_mask_ = ~(size - 1);
961 object_mask_ = address_mask_ | kHeapObjectTag;
962 object_expected_ = reinterpret_cast<uint32_t>(start) | kHeapObjectTag;
963
964 age_mark_ = start_;
965 return true;
966}
967
968
969void SemiSpace::TearDown() {
970 start_ = NULL;
971 capacity_ = 0;
972}
973
974
975bool SemiSpace::Double() {
kasper.lund7276f142008-07-30 08:49:36 +0000976 if (!MemoryAllocator::CommitBlock(high(), capacity_, executable())) {
977 return false;
978 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 capacity_ *= 2;
980 return true;
981}
982
983
984#ifdef DEBUG
985void SemiSpace::Print() { }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000986
987
988void SemiSpace::Verify() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989#endif
990
991
992// -----------------------------------------------------------------------------
993// SemiSpaceIterator implementation.
994SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
995 Initialize(space, space->bottom(), space->top(), NULL);
996}
997
998
999SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1000 HeapObjectCallback size_func) {
1001 Initialize(space, space->bottom(), space->top(), size_func);
1002}
1003
1004
1005SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1006 Initialize(space, start, space->top(), NULL);
1007}
1008
1009
1010void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1011 Address end,
1012 HeapObjectCallback size_func) {
1013 ASSERT(space->ToSpaceContains(start));
1014 ASSERT(space->ToSpaceLow() <= end
1015 && end <= space->ToSpaceHigh());
1016 space_ = space->to_space_;
1017 current_ = start;
1018 limit_ = end;
1019 size_func_ = size_func;
1020}
1021
1022
1023#ifdef DEBUG
1024// A static array of histogram info for each type.
1025static HistogramInfo heap_histograms[LAST_TYPE+1];
1026static JSObject::SpillInformation js_spill_information;
1027
1028// heap_histograms is shared, always clear it before using it.
1029static void ClearHistograms() {
1030 // We reset the name each time, though it hasn't changed.
1031#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1032 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1033#undef DEF_TYPE_NAME
1034
1035#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1036 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1037#undef CLEAR_HISTOGRAM
1038
1039 js_spill_information.Clear();
1040}
1041
1042
1043static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1044
1045
1046static void ClearCodeKindStatistics() {
1047 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1048 code_kind_statistics[i] = 0;
1049 }
1050}
1051
1052
1053static void ReportCodeKindStatistics() {
1054 const char* table[Code::NUMBER_OF_KINDS];
1055
1056#define CASE(name) \
1057 case Code::name: table[Code::name] = #name; \
1058 break
1059
1060 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1061 switch (static_cast<Code::Kind>(i)) {
1062 CASE(FUNCTION);
1063 CASE(STUB);
1064 CASE(BUILTIN);
1065 CASE(LOAD_IC);
1066 CASE(KEYED_LOAD_IC);
1067 CASE(STORE_IC);
1068 CASE(KEYED_STORE_IC);
1069 CASE(CALL_IC);
1070 }
1071 }
1072
1073#undef CASE
1074
1075 PrintF("\n Code kind histograms: \n");
1076 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1077 if (code_kind_statistics[i] > 0) {
1078 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1079 }
1080 }
1081 PrintF("\n");
1082}
1083
1084
1085static int CollectHistogramInfo(HeapObject* obj) {
1086 InstanceType type = obj->map()->instance_type();
1087 ASSERT(0 <= type && type <= LAST_TYPE);
1088 ASSERT(heap_histograms[type].name() != NULL);
1089 heap_histograms[type].increment_number(1);
1090 heap_histograms[type].increment_bytes(obj->Size());
1091
1092 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1093 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1094 }
1095
1096 return obj->Size();
1097}
1098
1099
1100static void ReportHistogram(bool print_spill) {
1101 PrintF("\n Object Histogram:\n");
1102 for (int i = 0; i <= LAST_TYPE; i++) {
1103 if (heap_histograms[i].number() > 0) {
1104 PrintF(" %-33s%10d (%10d bytes)\n",
1105 heap_histograms[i].name(),
1106 heap_histograms[i].number(),
1107 heap_histograms[i].bytes());
1108 }
1109 }
1110 PrintF("\n");
1111
1112 // Summarize string types.
1113 int string_number = 0;
1114 int string_bytes = 0;
1115#define INCREMENT(type, size, name) \
1116 string_number += heap_histograms[type].number(); \
1117 string_bytes += heap_histograms[type].bytes();
1118 STRING_TYPE_LIST(INCREMENT)
1119#undef INCREMENT
1120 if (string_number > 0) {
1121 PrintF(" %-33s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
1122 string_bytes);
1123 }
1124
1125 if (FLAG_collect_heap_spill_statistics && print_spill) {
1126 js_spill_information.Print();
1127 }
1128}
1129#endif // DEBUG
1130
1131
1132// Support for statistics gathering for --heap-stats and --log-gc.
1133#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1134void NewSpace::ClearHistograms() {
1135 for (int i = 0; i <= LAST_TYPE; i++) {
1136 allocated_histogram_[i].clear();
1137 promoted_histogram_[i].clear();
1138 }
1139}
1140
1141// Because the copying collector does not touch garbage objects, we iterate
1142// the new space before a collection to get a histogram of allocated objects.
1143// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1144// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1145// flag is set.
1146void NewSpace::CollectStatistics() {
1147 ClearHistograms();
1148 SemiSpaceIterator it(this);
1149 while (it.has_next()) RecordAllocation(it.next());
1150}
1151
1152
1153#ifdef ENABLE_LOGGING_AND_PROFILING
1154static void DoReportStatistics(HistogramInfo* info, const char* description) {
1155 LOG(HeapSampleBeginEvent("NewSpace", description));
1156 // Lump all the string types together.
1157 int string_number = 0;
1158 int string_bytes = 0;
1159#define INCREMENT(type, size, name) \
1160 string_number += info[type].number(); \
1161 string_bytes += info[type].bytes();
1162 STRING_TYPE_LIST(INCREMENT)
1163#undef INCREMENT
1164 if (string_number > 0) {
1165 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1166 }
1167
1168 // Then do the other types.
1169 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1170 if (info[i].number() > 0) {
1171 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1172 info[i].bytes()));
1173 }
1174 }
1175 LOG(HeapSampleEndEvent("NewSpace", description));
1176}
1177#endif // ENABLE_LOGGING_AND_PROFILING
1178
1179
1180void NewSpace::ReportStatistics() {
1181#ifdef DEBUG
1182 if (FLAG_heap_stats) {
1183 float pct = static_cast<float>(Available()) / Capacity();
1184 PrintF(" capacity: %d, available: %d, %%%d\n",
1185 Capacity(), Available(), static_cast<int>(pct*100));
1186 PrintF("\n Object Histogram:\n");
1187 for (int i = 0; i <= LAST_TYPE; i++) {
1188 if (allocated_histogram_[i].number() > 0) {
1189 PrintF(" %-33s%10d (%10d bytes)\n",
1190 allocated_histogram_[i].name(),
1191 allocated_histogram_[i].number(),
1192 allocated_histogram_[i].bytes());
1193 }
1194 }
1195 PrintF("\n");
1196 }
1197#endif // DEBUG
1198
1199#ifdef ENABLE_LOGGING_AND_PROFILING
1200 if (FLAG_log_gc) {
1201 DoReportStatistics(allocated_histogram_, "allocated");
1202 DoReportStatistics(promoted_histogram_, "promoted");
1203 }
1204#endif // ENABLE_LOGGING_AND_PROFILING
1205}
1206
1207
1208void NewSpace::RecordAllocation(HeapObject* obj) {
1209 InstanceType type = obj->map()->instance_type();
1210 ASSERT(0 <= type && type <= LAST_TYPE);
1211 allocated_histogram_[type].increment_number(1);
1212 allocated_histogram_[type].increment_bytes(obj->Size());
1213}
1214
1215
1216void NewSpace::RecordPromotion(HeapObject* obj) {
1217 InstanceType type = obj->map()->instance_type();
1218 ASSERT(0 <= type && type <= LAST_TYPE);
1219 promoted_histogram_[type].increment_number(1);
1220 promoted_histogram_[type].increment_bytes(obj->Size());
1221}
1222#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1223
1224
1225// -----------------------------------------------------------------------------
1226// Free lists for old object spaces implementation
1227
1228void FreeListNode::set_size(int size_in_bytes) {
1229 ASSERT(size_in_bytes > 0);
1230 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1231
1232 // We write a map and possibly size information to the block. If the block
1233 // is big enough to be a ByteArray with at least one extra word (the next
1234 // pointer), we set its map to be the byte array map and its size to an
1235 // appropriate array length for the desired size from HeapObject::Size().
1236 // If the block is too small (eg, one or two words), to hold both a size
1237 // field and a next pointer, we give it a filler map that gives it the
1238 // correct size.
1239 if (size_in_bytes > Array::kHeaderSize) {
1240 set_map(Heap::byte_array_map());
1241 ByteArray::cast(this)->set_length(ByteArray::LengthFor(size_in_bytes));
1242 } else if (size_in_bytes == kPointerSize) {
1243 set_map(Heap::one_word_filler_map());
1244 } else if (size_in_bytes == 2 * kPointerSize) {
1245 set_map(Heap::two_word_filler_map());
1246 } else {
1247 UNREACHABLE();
1248 }
kasper.lund7276f142008-07-30 08:49:36 +00001249 ASSERT(Size() == size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250}
1251
1252
1253Address FreeListNode::next() {
1254 ASSERT(map() == Heap::byte_array_map());
kasper.lund7276f142008-07-30 08:49:36 +00001255 ASSERT(Size() >= kNextOffset + kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 return Memory::Address_at(address() + kNextOffset);
1257}
1258
1259
1260void FreeListNode::set_next(Address next) {
1261 ASSERT(map() == Heap::byte_array_map());
kasper.lund7276f142008-07-30 08:49:36 +00001262 ASSERT(Size() >= kNextOffset + kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 Memory::Address_at(address() + kNextOffset) = next;
1264}
1265
1266
1267OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1268 Reset();
1269}
1270
1271
1272void OldSpaceFreeList::Reset() {
1273 available_ = 0;
1274 for (int i = 0; i < kFreeListsLength; i++) {
1275 free_[i].head_node_ = NULL;
1276 }
1277 needs_rebuild_ = false;
1278 finger_ = kHead;
1279 free_[kHead].next_size_ = kEnd;
1280}
1281
1282
1283void OldSpaceFreeList::RebuildSizeList() {
1284 ASSERT(needs_rebuild_);
1285 int cur = kHead;
1286 for (int i = cur + 1; i < kFreeListsLength; i++) {
1287 if (free_[i].head_node_ != NULL) {
1288 free_[cur].next_size_ = i;
1289 cur = i;
1290 }
1291 }
1292 free_[cur].next_size_ = kEnd;
1293 needs_rebuild_ = false;
1294}
1295
1296
1297int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1298#ifdef DEBUG
1299 for (int i = 0; i < size_in_bytes; i += kPointerSize) {
1300 Memory::Address_at(start + i) = kZapValue;
1301 }
1302#endif
1303 FreeListNode* node = FreeListNode::FromAddress(start);
1304 node->set_size(size_in_bytes);
1305
1306 // Early return to drop too-small blocks on the floor (one or two word
1307 // blocks cannot hold a map pointer, a size field, and a pointer to the
1308 // next block in the free list).
1309 if (size_in_bytes < kMinBlockSize) {
1310 return size_in_bytes;
1311 }
1312
1313 // Insert other blocks at the head of an exact free list.
1314 int index = size_in_bytes >> kPointerSizeLog2;
1315 node->set_next(free_[index].head_node_);
1316 free_[index].head_node_ = node->address();
1317 available_ += size_in_bytes;
1318 needs_rebuild_ = true;
1319 return 0;
1320}
1321
1322
1323Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1324 ASSERT(0 < size_in_bytes);
1325 ASSERT(size_in_bytes <= kMaxBlockSize);
1326 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1327
1328 if (needs_rebuild_) RebuildSizeList();
1329 int index = size_in_bytes >> kPointerSizeLog2;
1330 // Check for a perfect fit.
1331 if (free_[index].head_node_ != NULL) {
1332 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1333 // If this was the last block of its size, remove the size.
1334 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1335 available_ -= size_in_bytes;
1336 *wasted_bytes = 0;
1337 return node;
1338 }
1339 // Search the size list for the best fit.
1340 int prev = finger_ < index ? finger_ : kHead;
1341 int cur = FindSize(index, &prev);
1342 ASSERT(index < cur);
1343 if (cur == kEnd) {
1344 // No large enough size in list.
1345 *wasted_bytes = 0;
1346 return Failure::RetryAfterGC(size_in_bytes, owner_);
1347 }
1348 int rem = cur - index;
1349 int rem_bytes = rem << kPointerSizeLog2;
1350 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
kasper.lund7276f142008-07-30 08:49:36 +00001351 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1353 size_in_bytes);
1354 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1355 // to avoid many redundant tests and calls to Insert/RemoveSize.
1356 if (prev < rem) {
1357 // Simple case: insert rem between prev and cur.
1358 finger_ = prev;
1359 free_[prev].next_size_ = rem;
1360 // If this was the last block of size cur, remove the size.
1361 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1362 free_[rem].next_size_ = free_[cur].next_size_;
1363 } else {
1364 free_[rem].next_size_ = cur;
1365 }
1366 // Add the remainder block.
1367 rem_node->set_size(rem_bytes);
1368 rem_node->set_next(free_[rem].head_node_);
1369 free_[rem].head_node_ = rem_node->address();
1370 } else {
1371 // If this was the last block of size cur, remove the size.
1372 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1373 finger_ = prev;
1374 free_[prev].next_size_ = free_[cur].next_size_;
1375 }
1376 if (rem_bytes < kMinBlockSize) {
1377 // Too-small remainder is wasted.
1378 rem_node->set_size(rem_bytes);
1379 available_ -= size_in_bytes + rem_bytes;
1380 *wasted_bytes = rem_bytes;
1381 return cur_node;
1382 }
1383 // Add the remainder block and, if needed, insert its size.
1384 rem_node->set_size(rem_bytes);
1385 rem_node->set_next(free_[rem].head_node_);
1386 free_[rem].head_node_ = rem_node->address();
1387 if (rem_node->next() == NULL) InsertSize(rem);
1388 }
1389 available_ -= size_in_bytes;
1390 *wasted_bytes = 0;
1391 return cur_node;
1392}
1393
1394
kasper.lund7276f142008-07-30 08:49:36 +00001395#ifdef DEBUG
1396bool OldSpaceFreeList::Contains(FreeListNode* node) {
1397 for (int i = 0; i < kFreeListsLength; i++) {
1398 Address cur_addr = free_[i].head_node_;
1399 while (cur_addr != NULL) {
1400 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1401 if (cur_node == node) return true;
1402 cur_addr = cur_node->next();
1403 }
1404 }
1405 return false;
1406}
1407#endif
1408
1409
1410MapSpaceFreeList::MapSpaceFreeList(AllocationSpace owner) {
1411 owner_ = owner;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 Reset();
1413}
1414
1415
1416void MapSpaceFreeList::Reset() {
1417 available_ = 0;
1418 head_ = NULL;
1419}
1420
1421
1422void MapSpaceFreeList::Free(Address start) {
1423#ifdef DEBUG
1424 for (int i = 0; i < Map::kSize; i += kPointerSize) {
1425 Memory::Address_at(start + i) = kZapValue;
1426 }
1427#endif
1428 FreeListNode* node = FreeListNode::FromAddress(start);
1429 node->set_size(Map::kSize);
1430 node->set_next(head_);
1431 head_ = node->address();
1432 available_ += Map::kSize;
1433}
1434
1435
1436Object* MapSpaceFreeList::Allocate() {
1437 if (head_ == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00001438 return Failure::RetryAfterGC(Map::kSize, owner_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 }
1440
1441 FreeListNode* node = FreeListNode::FromAddress(head_);
1442 head_ = node->next();
1443 available_ -= Map::kSize;
1444 return node;
1445}
1446
1447
1448// -----------------------------------------------------------------------------
1449// OldSpace implementation
1450
1451void OldSpace::PrepareForMarkCompact(bool will_compact) {
1452 if (will_compact) {
1453 // Reset relocation info. During a compacting collection, everything in
1454 // the space is considered 'available' and we will rediscover live data
1455 // and waste during the collection.
1456 MCResetRelocationInfo();
1457 mc_end_of_relocation_ = bottom();
1458 ASSERT(Available() == Capacity());
1459 } else {
1460 // During a non-compacting collection, everything below the linear
1461 // allocation pointer is considered allocated (everything above is
1462 // available) and we will rediscover available and wasted bytes during
1463 // the collection.
1464 accounting_stats_.AllocateBytes(free_list_.available());
1465 accounting_stats_.FillWastedBytes(Waste());
1466 }
1467
kasper.lund7276f142008-07-30 08:49:36 +00001468 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470}
1471
1472
1473void OldSpace::MCAdjustRelocationEnd(Address address, int size_in_bytes) {
1474 ASSERT(Contains(address));
1475 Address current_top = mc_end_of_relocation_;
1476 Page* current_page = Page::FromAllocationTop(current_top);
1477
1478 // No more objects relocated to this page? Move to the next.
1479 ASSERT(current_top <= current_page->mc_relocation_top);
1480 if (current_top == current_page->mc_relocation_top) {
1481 // The space should already be properly expanded.
1482 Page* next_page = current_page->next_page();
1483 CHECK(next_page->is_valid());
1484 mc_end_of_relocation_ = next_page->ObjectAreaStart();
1485 }
1486 ASSERT(mc_end_of_relocation_ == address);
1487 mc_end_of_relocation_ += size_in_bytes;
1488}
1489
1490
1491void OldSpace::MCCommitRelocationInfo() {
1492 // Update fast allocation info.
1493 allocation_info_.top = mc_forwarding_info_.top;
1494 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001495 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496
1497 // The space is compacted and we haven't yet built free lists or
1498 // wasted any space.
1499 ASSERT(Waste() == 0);
1500 ASSERT(AvailableFree() == 0);
1501
1502 // Build the free list for the space.
1503 int computed_size = 0;
1504 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1505 while (it.has_next()) {
1506 Page* p = it.next();
1507 // Space below the relocation pointer is allocated.
1508 computed_size += p->mc_relocation_top - p->ObjectAreaStart();
1509 if (it.has_next()) {
1510 // Free the space at the top of the page. We cannot use
1511 // p->mc_relocation_top after the call to Free (because Free will clear
1512 // remembered set bits).
1513 int extra_size = p->ObjectAreaEnd() - p->mc_relocation_top;
1514 if (extra_size > 0) {
1515 int wasted_bytes = free_list_.Free(p->mc_relocation_top, extra_size);
1516 // The bytes we have just "freed" to add to the free list were
1517 // already accounted as available.
1518 accounting_stats_.WasteBytes(wasted_bytes);
1519 }
1520 }
1521 }
1522
1523 // Make sure the computed size - based on the used portion of the pages in
1524 // use - matches the size obtained while computing forwarding addresses.
1525 ASSERT(computed_size == Size());
1526}
1527
1528
kasper.lund7276f142008-07-30 08:49:36 +00001529// Slow case for normal allocation. Try in order: (1) allocate in the next
1530// page in the space, (2) allocate off the space's free list, (3) expand the
1531// space, (4) fail.
1532HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
1533 // Linear allocation in this space has failed. If there is another page
1534 // in the space, move to that page and allocate there. This allocation
1535 // should succeed (size_in_bytes should not be greater than a page's
1536 // object area size).
1537 Page* current_page = TopPageOf(allocation_info_);
1538 if (current_page->next_page()->is_valid()) {
1539 return AllocateInNextPage(current_page, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540 }
kasper.lund7276f142008-07-30 08:49:36 +00001541
1542 // There is no next page in this space. Try free list allocation.
1543 int wasted_bytes;
1544 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
1545 accounting_stats_.WasteBytes(wasted_bytes);
1546 if (!result->IsFailure()) {
1547 accounting_stats_.AllocateBytes(size_in_bytes);
1548 return HeapObject::cast(result);
1549 }
1550
1551 // Free list allocation failed and there is no next page. Try to expand
1552 // the space and allocate in the new next page.
1553 ASSERT(!current_page->next_page()->is_valid());
1554 if (Expand(current_page)) {
1555 return AllocateInNextPage(current_page, size_in_bytes);
1556 }
1557
1558 // Finally, fail.
1559 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560}
1561
1562
kasper.lund7276f142008-07-30 08:49:36 +00001563// Add the block at the top of the page to the space's free list, set the
1564// allocation info to the next page (assumed to be one), and allocate
1565// linearly there.
1566HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
1567 int size_in_bytes) {
1568 ASSERT(current_page->next_page()->is_valid());
1569 // Add the block at the top of this page to the free list.
1570 int free_size = current_page->ObjectAreaEnd() - allocation_info_.top;
1571 if (free_size > 0) {
1572 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
1573 accounting_stats_.WasteBytes(wasted_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 }
kasper.lund7276f142008-07-30 08:49:36 +00001575 SetAllocationInfo(&allocation_info_, current_page->next_page());
1576 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577}
1578
1579
1580#ifdef DEBUG
1581// We do not assume that the PageIterator works, because it depends on the
1582// invariants we are checking during verification.
1583void OldSpace::Verify() {
1584 // The allocation pointer should be valid, and it should be in a page in the
1585 // space.
kasper.lund7276f142008-07-30 08:49:36 +00001586 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
1588 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
1589
1590 // Loop over all the pages.
1591 bool above_allocation_top = false;
1592 Page* current_page = first_page_;
1593 while (current_page->is_valid()) {
1594 if (above_allocation_top) {
1595 // We don't care what's above the allocation top.
1596 } else {
1597 // Unless this is the last page in the space containing allocated
1598 // objects, the allocation top should be at the object area end.
1599 Address top = current_page->AllocationTop();
1600 if (current_page == top_page) {
1601 ASSERT(top == allocation_info_.top);
1602 // The next page will be above the allocation top.
1603 above_allocation_top = true;
1604 } else {
1605 ASSERT(top == current_page->ObjectAreaEnd());
1606 }
1607
1608 // It should be packed with objects from the bottom to the top.
1609 Address current = current_page->ObjectAreaStart();
1610 while (current < top) {
1611 HeapObject* object = HeapObject::FromAddress(current);
1612
1613 // The first word should be a map, and we expect all map pointers to
1614 // be in map space.
1615 Map* map = object->map();
1616 ASSERT(map->IsMap());
1617 ASSERT(Heap::map_space()->Contains(map));
1618
1619 // The object should not be a map.
1620 ASSERT(!object->IsMap());
1621
1622 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001623 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624
1625 // All the interior pointers should be contained in the heap and have
1626 // their remembered set bits set if they point to new space. Code
1627 // objects do not have remembered set bits that we care about.
1628 VerifyPointersAndRSetVisitor rset_visitor;
1629 VerifyPointersVisitor no_rset_visitor;
1630 int size = object->Size();
1631 if (object->IsCode()) {
1632 Code::cast(object)->ConvertICTargetsFromAddressToObject();
1633 object->IterateBody(map->instance_type(), size, &no_rset_visitor);
1634 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
1635 } else {
1636 object->IterateBody(map->instance_type(), size, &rset_visitor);
1637 }
1638
1639 current += size;
1640 }
1641
1642 // The allocation pointer should not be in the middle of an object.
1643 ASSERT(current == top);
1644 }
1645
1646 current_page = current_page->next_page();
1647 }
1648}
1649
1650
1651struct CommentStatistic {
1652 const char* comment;
1653 int size;
1654 int count;
1655 void Clear() {
1656 comment = NULL;
1657 size = 0;
1658 count = 0;
1659 }
1660};
1661
1662
1663// must be small, since an iteration is used for lookup
1664const int kMaxComments = 64;
1665static CommentStatistic comments_statistics[kMaxComments+1];
1666
1667
1668void PagedSpace::ReportCodeStatistics() {
1669 ReportCodeKindStatistics();
1670 PrintF("Code comment statistics (\" [ comment-txt : size/ "
1671 "count (average)\"):\n");
1672 for (int i = 0; i <= kMaxComments; i++) {
1673 const CommentStatistic& cs = comments_statistics[i];
1674 if (cs.size > 0) {
1675 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
1676 cs.size/cs.count);
1677 }
1678 }
1679 PrintF("\n");
1680}
1681
1682
1683void PagedSpace::ResetCodeStatistics() {
1684 ClearCodeKindStatistics();
1685 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
1686 comments_statistics[kMaxComments].comment = "Unknown";
1687 comments_statistics[kMaxComments].size = 0;
1688 comments_statistics[kMaxComments].count = 0;
1689}
1690
1691
1692// Adds comment to 'comment_statistics' table. Performance OK sa long as
1693// 'kMaxComments' is small
1694static void EnterComment(const char* comment, int delta) {
1695 // Do not count empty comments
1696 if (delta <= 0) return;
1697 CommentStatistic* cs = &comments_statistics[kMaxComments];
1698 // Search for a free or matching entry in 'comments_statistics': 'cs'
1699 // points to result.
1700 for (int i = 0; i < kMaxComments; i++) {
1701 if (comments_statistics[i].comment == NULL) {
1702 cs = &comments_statistics[i];
1703 cs->comment = comment;
1704 break;
1705 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
1706 cs = &comments_statistics[i];
1707 break;
1708 }
1709 }
1710 // Update entry for 'comment'
1711 cs->size += delta;
1712 cs->count += 1;
1713}
1714
1715
1716// Call for each nested comment start (start marked with '[ xxx', end marked
1717// with ']'. RelocIterator 'it' must point to a comment reloc info.
1718static void CollectCommentStatistics(RelocIterator* it) {
1719 ASSERT(!it->done());
1720 ASSERT(it->rinfo()->rmode() == comment);
1721 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
1722 if (tmp[0] != '[') {
1723 // Not a nested comment; skip
1724 return;
1725 }
1726
1727 // Search for end of nested comment or a new nested comment
1728 const char* const comment_txt =
1729 reinterpret_cast<const char*>(it->rinfo()->data());
1730 const byte* prev_pc = it->rinfo()->pc();
1731 int flat_delta = 0;
1732 it->next();
1733 while (true) {
1734 // All nested comments must be terminated properly, and therefore exit
1735 // from loop.
1736 ASSERT(!it->done());
1737 if (it->rinfo()->rmode() == comment) {
1738 const char* const txt =
1739 reinterpret_cast<const char*>(it->rinfo()->data());
1740 flat_delta += it->rinfo()->pc() - prev_pc;
1741 if (txt[0] == ']') break; // End of nested comment
1742 // A new comment
1743 CollectCommentStatistics(it);
1744 // Skip code that was covered with previous comment
1745 prev_pc = it->rinfo()->pc();
1746 }
1747 it->next();
1748 }
1749 EnterComment(comment_txt, flat_delta);
1750}
1751
1752
1753// Collects code size statistics:
1754// - by code kind
1755// - by code comment
1756void PagedSpace::CollectCodeStatistics() {
1757 HeapObjectIterator obj_it(this);
1758 while (obj_it.has_next()) {
1759 HeapObject* obj = obj_it.next();
1760 if (obj->IsCode()) {
1761 Code* code = Code::cast(obj);
1762 code_kind_statistics[code->kind()] += code->Size();
1763 RelocIterator it(code);
1764 int delta = 0;
1765 const byte* prev_pc = code->instruction_start();
1766 while (!it.done()) {
1767 if (it.rinfo()->rmode() == comment) {
1768 delta += it.rinfo()->pc() - prev_pc;
1769 CollectCommentStatistics(&it);
1770 prev_pc = it.rinfo()->pc();
1771 }
1772 it.next();
1773 }
1774
1775 ASSERT(code->instruction_start() <= prev_pc &&
1776 prev_pc <= code->relocation_start());
1777 delta += code->relocation_start() - prev_pc;
1778 EnterComment("NoComment", delta);
1779 }
1780 }
1781}
1782
1783
1784void OldSpace::ReportStatistics() {
1785 int pct = Available() * 100 / Capacity();
1786 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
1787 Capacity(), Waste(), Available(), pct);
1788
1789 // Report remembered set statistics.
1790 int rset_marked_pointers = 0;
1791 int rset_marked_arrays = 0;
1792 int rset_marked_array_elements = 0;
1793 int cross_gen_pointers = 0;
1794 int cross_gen_array_elements = 0;
1795
1796 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
1797 while (page_it.has_next()) {
1798 Page* p = page_it.next();
1799
1800 for (Address rset_addr = p->RSetStart();
1801 rset_addr < p->RSetEnd();
1802 rset_addr += kIntSize) {
1803 int rset = Memory::int_at(rset_addr);
1804 if (rset != 0) {
1805 // Bits were set
1806 int intoff = rset_addr - p->address();
1807 int bitoff = 0;
1808 for (; bitoff < kBitsPerInt; ++bitoff) {
1809 if ((rset & (1 << bitoff)) != 0) {
1810 int bitpos = intoff*kBitsPerByte + bitoff;
1811 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
1812 Object** obj = reinterpret_cast<Object**>(slot);
1813 if (*obj == Heap::fixed_array_map()) {
1814 rset_marked_arrays++;
1815 FixedArray* fa = FixedArray::cast(HeapObject::FromAddress(slot));
1816
1817 rset_marked_array_elements += fa->length();
1818 // Manually inline FixedArray::IterateBody
1819 Address elm_start = slot + FixedArray::kHeaderSize;
1820 Address elm_stop = elm_start + fa->length() * kPointerSize;
1821 for (Address elm_addr = elm_start;
1822 elm_addr < elm_stop; elm_addr += kPointerSize) {
1823 // Filter non-heap-object pointers
1824 Object** elm_p = reinterpret_cast<Object**>(elm_addr);
1825 if (Heap::InNewSpace(*elm_p))
1826 cross_gen_array_elements++;
1827 }
1828 } else {
1829 rset_marked_pointers++;
1830 if (Heap::InNewSpace(*obj))
1831 cross_gen_pointers++;
1832 }
1833 }
1834 }
1835 }
1836 }
1837 }
1838
1839 pct = rset_marked_pointers == 0 ?
1840 0 : cross_gen_pointers * 100 / rset_marked_pointers;
1841 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
1842 rset_marked_pointers, cross_gen_pointers, pct);
1843 PrintF(" rset_marked arrays %d, ", rset_marked_arrays);
1844 PrintF(" elements %d, ", rset_marked_array_elements);
1845 pct = rset_marked_array_elements == 0 ? 0
1846 : cross_gen_array_elements * 100 / rset_marked_array_elements;
1847 PrintF(" pointers to new space %d (%%%d)\n", cross_gen_array_elements, pct);
1848 PrintF(" total rset-marked bits %d\n",
1849 (rset_marked_pointers + rset_marked_arrays));
1850 pct = (rset_marked_pointers + rset_marked_array_elements) == 0 ? 0
1851 : (cross_gen_pointers + cross_gen_array_elements) * 100 /
1852 (rset_marked_pointers + rset_marked_array_elements);
1853 PrintF(" total rset pointers %d, true cross generation ones %d (%%%d)\n",
1854 (rset_marked_pointers + rset_marked_array_elements),
1855 (cross_gen_pointers + cross_gen_array_elements),
1856 pct);
1857
1858 ClearHistograms();
1859 HeapObjectIterator obj_it(this);
1860 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
1861 ReportHistogram(true);
1862}
1863
1864
1865// Dump the range of remembered set words between [start, end) corresponding
1866// to the pointers starting at object_p. The allocation_top is an object
1867// pointer which should not be read past. This is important for large object
1868// pages, where some bits in the remembered set range do not correspond to
1869// allocated addresses.
1870static void PrintRSetRange(Address start, Address end, Object** object_p,
1871 Address allocation_top) {
1872 Address rset_address = start;
1873
1874 // If the range starts on on odd numbered word (eg, for large object extra
1875 // remembered set ranges), print some spaces.
1876 if ((reinterpret_cast<uint32_t>(start) / kIntSize) % 2 == 1) {
1877 PrintF(" ");
1878 }
1879
1880 // Loop over all the words in the range.
1881 while (rset_address < end) {
1882 uint32_t rset_word = Memory::uint32_at(rset_address);
1883 int bit_position = 0;
1884
1885 // Loop over all the bits in the word.
1886 while (bit_position < kBitsPerInt) {
1887 if (object_p == reinterpret_cast<Object**>(allocation_top)) {
1888 // Print a bar at the allocation pointer.
1889 PrintF("|");
1890 } else if (object_p > reinterpret_cast<Object**>(allocation_top)) {
1891 // Do not dereference object_p past the allocation pointer.
1892 PrintF("#");
1893 } else if ((rset_word & (1 << bit_position)) == 0) {
1894 // Print a dot for zero bits.
1895 PrintF(".");
1896 } else if (Heap::InNewSpace(*object_p)) {
1897 // Print an X for one bits for pointers to new space.
1898 PrintF("X");
1899 } else {
1900 // Print a circle for one bits for pointers to old space.
1901 PrintF("o");
1902 }
1903
1904 // Print a space after every 8th bit except the last.
1905 if (bit_position % 8 == 7 && bit_position != (kBitsPerInt - 1)) {
1906 PrintF(" ");
1907 }
1908
1909 // Advance to next bit.
1910 bit_position++;
1911 object_p++;
1912 }
1913
1914 // Print a newline after every odd numbered word, otherwise a space.
1915 if ((reinterpret_cast<uint32_t>(rset_address) / kIntSize) % 2 == 1) {
1916 PrintF("\n");
1917 } else {
1918 PrintF(" ");
1919 }
1920
1921 // Advance to next remembered set word.
1922 rset_address += kIntSize;
1923 }
1924}
1925
1926
1927void PagedSpace::DoPrintRSet(const char* space_name) {
1928 PageIterator it(this, PageIterator::PAGES_IN_USE);
1929 while (it.has_next()) {
1930 Page* p = it.next();
1931 PrintF("%s page 0x%x:\n", space_name, p);
1932 PrintRSetRange(p->RSetStart(), p->RSetEnd(),
1933 reinterpret_cast<Object**>(p->ObjectAreaStart()),
1934 p->AllocationTop());
1935 PrintF("\n");
1936 }
1937}
1938
1939
1940void OldSpace::PrintRSet() { DoPrintRSet("old"); }
1941#endif
1942
1943// -----------------------------------------------------------------------------
1944// MapSpace implementation
1945
1946void MapSpace::PrepareForMarkCompact(bool will_compact) {
1947 if (will_compact) {
1948 // Reset relocation info.
1949 MCResetRelocationInfo();
1950
1951 // Initialize map index entry.
1952 int page_count = 0;
1953 PageIterator it(this, PageIterator::ALL_PAGES);
1954 while (it.has_next()) {
1955 ASSERT_MAP_PAGE_INDEX(page_count);
1956
1957 Page* p = it.next();
1958 ASSERT(p->mc_page_index == page_count);
1959
1960 page_addresses_[page_count++] = p->address();
1961 }
1962
1963 // During a compacting collection, everything in the space is considered
1964 // 'available' (set by the call to MCResetRelocationInfo) and we will
1965 // rediscover live and wasted bytes during the collection.
1966 ASSERT(Available() == Capacity());
1967 } else {
1968 // During a non-compacting collection, everything below the linear
1969 // allocation pointer except wasted top-of-page blocks is considered
1970 // allocated and we will rediscover available bytes during the
1971 // collection.
1972 accounting_stats_.AllocateBytes(free_list_.available());
1973 }
1974
kasper.lund7276f142008-07-30 08:49:36 +00001975 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977}
1978
1979
1980void MapSpace::MCCommitRelocationInfo() {
1981 // Update fast allocation info.
1982 allocation_info_.top = mc_forwarding_info_.top;
1983 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001984 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985
1986 // The space is compacted and we haven't yet wasted any space.
1987 ASSERT(Waste() == 0);
1988
1989 // Update allocation_top of each page in use and compute waste.
1990 int computed_size = 0;
1991 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1992 while (it.has_next()) {
1993 Page* page = it.next();
1994 Address page_top = page->AllocationTop();
1995 computed_size += page_top - page->ObjectAreaStart();
1996 if (it.has_next()) {
1997 accounting_stats_.WasteBytes(page->ObjectAreaEnd() - page_top);
1998 }
1999 }
2000
2001 // Make sure the computed size - based on the used portion of the
2002 // pages in use - matches the size we adjust during allocation.
2003 ASSERT(computed_size == Size());
2004}
2005
2006
kasper.lund7276f142008-07-30 08:49:36 +00002007// Slow case for normal allocation. Try in order: (1) allocate in the next
2008// page in the space, (2) allocate off the space's free list, (3) expand the
2009// space, (4) fail.
2010HeapObject* MapSpace::SlowAllocateRaw(int size_in_bytes) {
2011 // Linear allocation in this space has failed. If there is another page
2012 // in the space, move to that page and allocate there. This allocation
2013 // should succeed.
2014 Page* current_page = TopPageOf(allocation_info_);
2015 if (current_page->next_page()->is_valid()) {
2016 return AllocateInNextPage(current_page, size_in_bytes);
2017 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018
kasper.lund7276f142008-07-30 08:49:36 +00002019 // There is no next page in this space. Try free list allocation. The
2020 // map space free list implicitly assumes that all free blocks are map
2021 // sized.
2022 if (size_in_bytes == Map::kSize) {
2023 Object* result = free_list_.Allocate();
2024 if (!result->IsFailure()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 accounting_stats_.AllocateBytes(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002026 return HeapObject::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 }
2028 }
kasper.lund7276f142008-07-30 08:49:36 +00002029
2030 // Free list allocation failed and there is no next page. Try to expand
2031 // the space and allocate in the new next page.
2032 ASSERT(!current_page->next_page()->is_valid());
2033 if (Expand(current_page)) {
2034 return AllocateInNextPage(current_page, size_in_bytes);
2035 }
2036
2037 // Finally, fail.
2038 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039}
2040
2041
kasper.lund7276f142008-07-30 08:49:36 +00002042// Move to the next page (there is assumed to be one) and allocate there.
2043// The top of page block is always wasted, because it is too small to hold a
2044// map.
2045HeapObject* MapSpace::AllocateInNextPage(Page* current_page,
2046 int size_in_bytes) {
2047 ASSERT(current_page->next_page()->is_valid());
2048 ASSERT(current_page->ObjectAreaEnd() - allocation_info_.top == kPageExtra);
2049 accounting_stats_.WasteBytes(kPageExtra);
2050 SetAllocationInfo(&allocation_info_, current_page->next_page());
2051 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052}
2053
2054
2055#ifdef DEBUG
2056// We do not assume that the PageIterator works, because it depends on the
2057// invariants we are checking during verification.
2058void MapSpace::Verify() {
2059 // The allocation pointer should be valid, and it should be in a page in the
2060 // space.
kasper.lund7276f142008-07-30 08:49:36 +00002061 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
2063 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
2064
2065 // Loop over all the pages.
2066 bool above_allocation_top = false;
2067 Page* current_page = first_page_;
2068 while (current_page->is_valid()) {
2069 if (above_allocation_top) {
2070 // We don't care what's above the allocation top.
2071 } else {
2072 // Unless this is the last page in the space containing allocated
2073 // objects, the allocation top should be at a constant offset from the
2074 // object area end.
2075 Address top = current_page->AllocationTop();
2076 if (current_page == top_page) {
2077 ASSERT(top == allocation_info_.top);
2078 // The next page will be above the allocation top.
2079 above_allocation_top = true;
2080 } else {
2081 ASSERT(top == current_page->ObjectAreaEnd() - kPageExtra);
2082 }
2083
2084 // It should be packed with objects from the bottom to the top.
2085 Address current = current_page->ObjectAreaStart();
2086 while (current < top) {
2087 HeapObject* object = HeapObject::FromAddress(current);
2088
2089 // The first word should be a map, and we expect all map pointers to
2090 // be in map space.
2091 Map* map = object->map();
2092 ASSERT(map->IsMap());
2093 ASSERT(Heap::map_space()->Contains(map));
2094
2095 // The object should be a map or a byte array.
2096 ASSERT(object->IsMap() || object->IsByteArray());
2097
2098 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002099 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100
2101 // All the interior pointers should be contained in the heap and
2102 // have their remembered set bits set if they point to new space.
2103 VerifyPointersAndRSetVisitor visitor;
2104 int size = object->Size();
2105 object->IterateBody(map->instance_type(), size, &visitor);
2106
2107 current += size;
2108 }
2109
2110 // The allocation pointer should not be in the middle of an object.
2111 ASSERT(current == top);
2112 }
2113
2114 current_page = current_page->next_page();
2115 }
2116}
2117
2118
2119void MapSpace::ReportStatistics() {
2120 int pct = Available() * 100 / Capacity();
2121 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2122 Capacity(), Waste(), Available(), pct);
2123
2124 // Report remembered set statistics.
2125 int rset_marked_pointers = 0;
2126 int cross_gen_pointers = 0;
2127
2128 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2129 while (page_it.has_next()) {
2130 Page* p = page_it.next();
2131
2132 for (Address rset_addr = p->RSetStart();
2133 rset_addr < p->RSetEnd();
2134 rset_addr += kIntSize) {
2135 int rset = Memory::int_at(rset_addr);
2136 if (rset != 0) {
2137 // Bits were set
2138 int intoff = rset_addr - p->address();
2139 int bitoff = 0;
2140 for (; bitoff < kBitsPerInt; ++bitoff) {
2141 if ((rset & (1 << bitoff)) != 0) {
2142 int bitpos = intoff*kBitsPerByte + bitoff;
2143 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2144 Object** obj = reinterpret_cast<Object**>(slot);
2145 rset_marked_pointers++;
2146 if (Heap::InNewSpace(*obj))
2147 cross_gen_pointers++;
2148 }
2149 }
2150 }
2151 }
2152 }
2153
2154 pct = rset_marked_pointers == 0 ?
2155 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2156 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2157 rset_marked_pointers, cross_gen_pointers, pct);
2158
2159 ClearHistograms();
2160 HeapObjectIterator obj_it(this);
2161 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
2162 ReportHistogram(false);
2163}
2164
2165
2166void MapSpace::PrintRSet() { DoPrintRSet("map"); }
2167#endif
2168
2169
2170// -----------------------------------------------------------------------------
2171// LargeObjectIterator
2172
2173LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2174 current_ = space->first_chunk_;
2175 size_func_ = NULL;
2176}
2177
2178
2179LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2180 HeapObjectCallback size_func) {
2181 current_ = space->first_chunk_;
2182 size_func_ = size_func;
2183}
2184
2185
2186HeapObject* LargeObjectIterator::next() {
2187 ASSERT(has_next());
2188 HeapObject* object = current_->GetObject();
2189 current_ = current_->next();
2190 return object;
2191}
2192
2193
2194// -----------------------------------------------------------------------------
2195// LargeObjectChunk
2196
2197LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
kasper.lund7276f142008-07-30 08:49:36 +00002198 size_t* chunk_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002199 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 size_t requested = ChunkSizeFor(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002201 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2202 chunk_size,
2203 executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 if (mem == NULL) return NULL;
2205 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2206 if (*chunk_size < requested) {
2207 MemoryAllocator::FreeRawMemory(mem, *chunk_size);
2208 LOG(DeleteEvent("LargeObjectChunk", mem));
2209 return NULL;
2210 }
2211 return reinterpret_cast<LargeObjectChunk*>(mem);
2212}
2213
2214
2215int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
2216 int os_alignment = OS::AllocateAlignment();
2217 if (os_alignment < Page::kPageSize)
2218 size_in_bytes += (Page::kPageSize - os_alignment);
2219 return size_in_bytes + Page::kObjectStartOffset;
2220}
2221
2222// -----------------------------------------------------------------------------
2223// LargeObjectSpace
2224
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002225LargeObjectSpace::LargeObjectSpace(AllocationSpace id)
2226 : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis
kasper.lund7276f142008-07-30 08:49:36 +00002227 first_chunk_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 size_(0),
2229 page_count_(0) {}
2230
2231
2232bool LargeObjectSpace::Setup() {
2233 first_chunk_ = NULL;
2234 size_ = 0;
2235 page_count_ = 0;
2236 return true;
2237}
2238
2239
2240void LargeObjectSpace::TearDown() {
2241 while (first_chunk_ != NULL) {
2242 LargeObjectChunk* chunk = first_chunk_;
2243 first_chunk_ = first_chunk_->next();
2244 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
2245 MemoryAllocator::FreeRawMemory(chunk->address(), chunk->size());
2246 }
2247
2248 size_ = 0;
2249 page_count_ = 0;
2250}
2251
2252
2253Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002254 int object_size,
2255 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 ASSERT(0 < object_size && object_size <= requested_size);
2257 size_t chunk_size;
2258 LargeObjectChunk* chunk =
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002259 LargeObjectChunk::New(requested_size, &chunk_size, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 if (chunk == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00002261 return Failure::RetryAfterGC(requested_size, identity());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 }
2263
2264 size_ += chunk_size;
2265 page_count_++;
2266 chunk->set_next(first_chunk_);
2267 chunk->set_size(chunk_size);
2268 first_chunk_ = chunk;
2269
2270 // Set the object address and size in the page header and clear its
2271 // remembered set.
2272 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2273 Address object_address = page->ObjectAreaStart();
2274 // Clear the low order bit of the second word in the page to flag it as a
2275 // large object page. If the chunk_size happened to be written there, its
2276 // low order bit should already be clear.
2277 ASSERT((chunk_size & 0x1) == 0);
2278 page->is_normal_page &= ~0x1;
2279 page->ClearRSet();
2280 int extra_bytes = requested_size - object_size;
2281 if (extra_bytes > 0) {
2282 // The extra memory for the remembered set should be cleared.
2283 memset(object_address + object_size, 0, extra_bytes);
2284 }
2285
2286 return HeapObject::FromAddress(object_address);
2287}
2288
2289
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002290Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 ASSERT(0 < size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002292 return AllocateRawInternal(size_in_bytes,
2293 size_in_bytes,
2294 EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295}
2296
2297
2298Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002299 ASSERT(0 < size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 int extra_rset_bytes = ExtraRSetBytesFor(size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002301 return AllocateRawInternal(size_in_bytes + extra_rset_bytes,
2302 size_in_bytes,
2303 NOT_EXECUTABLE);
2304}
2305
2306
2307Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2308 ASSERT(0 < size_in_bytes);
2309 return AllocateRawInternal(size_in_bytes,
2310 size_in_bytes,
2311 NOT_EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312}
2313
2314
2315// GC support
2316Object* LargeObjectSpace::FindObject(Address a) {
2317 for (LargeObjectChunk* chunk = first_chunk_;
2318 chunk != NULL;
2319 chunk = chunk->next()) {
2320 Address chunk_address = chunk->address();
2321 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2322 return chunk->GetObject();
2323 }
2324 }
2325 return Failure::Exception();
2326}
2327
2328
2329void LargeObjectSpace::ClearRSet() {
2330 ASSERT(Page::is_rset_in_use());
2331
2332 LargeObjectIterator it(this);
2333 while (it.has_next()) {
2334 HeapObject* object = it.next();
2335 // We only have code, sequential strings, or fixed arrays in large
2336 // object space, and only fixed arrays need remembered set support.
2337 if (object->IsFixedArray()) {
2338 // Clear the normal remembered set region of the page;
2339 Page* page = Page::FromAddress(object->address());
2340 page->ClearRSet();
2341
2342 // Clear the extra remembered set.
2343 int size = object->Size();
2344 int extra_rset_bytes = ExtraRSetBytesFor(size);
2345 memset(object->address() + size, 0, extra_rset_bytes);
2346 }
2347 }
2348}
2349
2350
2351void LargeObjectSpace::IterateRSet(ObjectSlotCallback copy_object_func) {
2352 ASSERT(Page::is_rset_in_use());
2353
2354 LargeObjectIterator it(this);
2355 while (it.has_next()) {
2356 // We only have code, sequential strings, or fixed arrays in large
2357 // object space, and only fixed arrays can possibly contain pointers to
2358 // the young generation.
2359 HeapObject* object = it.next();
2360 if (object->IsFixedArray()) {
2361 // Iterate the normal page remembered set range.
2362 Page* page = Page::FromAddress(object->address());
2363 Address object_end = object->address() + object->Size();
2364 Heap::IterateRSetRange(page->ObjectAreaStart(),
2365 Min(page->ObjectAreaEnd(), object_end),
2366 page->RSetStart(),
2367 copy_object_func);
2368
2369 // Iterate the extra array elements.
2370 if (object_end > page->ObjectAreaEnd()) {
2371 Heap::IterateRSetRange(page->ObjectAreaEnd(), object_end,
2372 object_end, copy_object_func);
2373 }
2374 }
2375 }
2376}
2377
2378
2379void LargeObjectSpace::FreeUnmarkedObjects() {
2380 LargeObjectChunk* previous = NULL;
2381 LargeObjectChunk* current = first_chunk_;
2382 while (current != NULL) {
2383 HeapObject* object = current->GetObject();
kasper.lund7276f142008-07-30 08:49:36 +00002384 if (object->IsMarked()) {
2385 object->ClearMark();
2386 MarkCompactCollector::tracer()->decrement_marked_count();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 previous = current;
2388 current = current->next();
2389 } else {
2390 Address chunk_address = current->address();
2391 size_t chunk_size = current->size();
2392
2393 // Cut the chunk out from the chunk list.
2394 current = current->next();
2395 if (previous == NULL) {
2396 first_chunk_ = current;
2397 } else {
2398 previous->set_next(current);
2399 }
2400
2401 // Free the chunk.
2402 if (object->IsCode()) {
2403 LOG(CodeDeleteEvent(object->address()));
2404 }
2405 size_ -= chunk_size;
2406 page_count_--;
2407 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size);
2408 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2409 }
2410 }
2411}
2412
2413
2414bool LargeObjectSpace::Contains(HeapObject* object) {
2415 Address address = object->address();
2416 Page* page = Page::FromAddress(address);
2417
2418 SLOW_ASSERT(!page->IsLargeObjectPage()
2419 || !FindObject(address)->IsFailure());
2420
2421 return page->IsLargeObjectPage();
2422}
2423
2424
2425#ifdef DEBUG
2426// We do not assume that the large object iterator works, because it depends
2427// on the invariants we are checking during verification.
2428void LargeObjectSpace::Verify() {
2429 for (LargeObjectChunk* chunk = first_chunk_;
2430 chunk != NULL;
2431 chunk = chunk->next()) {
2432 // Each chunk contains an object that starts at the large object page's
2433 // object area start.
2434 HeapObject* object = chunk->GetObject();
2435 Page* page = Page::FromAddress(object->address());
2436 ASSERT(object->address() == page->ObjectAreaStart());
2437
2438 // The first word should be a map, and we expect all map pointers to be
2439 // in map space.
2440 Map* map = object->map();
2441 ASSERT(map->IsMap());
2442 ASSERT(Heap::map_space()->Contains(map));
2443
2444 // We have only code, sequential strings, fixed arrays, and byte arrays
2445 // in large object space.
2446 ASSERT(object->IsCode() || object->IsSeqString()
2447 || object->IsFixedArray() || object->IsByteArray());
2448
2449 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002450 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002451
2452 // Byte arrays and strings don't have interior pointers.
2453 if (object->IsCode()) {
2454 VerifyPointersVisitor code_visitor;
2455 Code::cast(object)->ConvertICTargetsFromAddressToObject();
2456 object->IterateBody(map->instance_type(),
2457 object->Size(),
2458 &code_visitor);
2459 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
2460 } else if (object->IsFixedArray()) {
2461 // We loop over fixed arrays ourselves, rather then using the visitor,
2462 // because the visitor doesn't support the start/offset iteration
2463 // needed for IsRSetSet.
2464 FixedArray* array = FixedArray::cast(object);
2465 for (int j = 0; j < array->length(); j++) {
2466 Object* element = array->get(j);
2467 if (element->IsHeapObject()) {
2468 HeapObject* element_object = HeapObject::cast(element);
2469 ASSERT(Heap::Contains(element_object));
2470 ASSERT(element_object->map()->IsMap());
2471 if (Heap::InNewSpace(element_object)) {
2472 ASSERT(Page::IsRSetSet(object->address(),
2473 FixedArray::kHeaderSize + j * kPointerSize));
2474 }
2475 }
2476 }
2477 }
2478 }
2479}
2480
2481
2482void LargeObjectSpace::Print() {
2483 LargeObjectIterator it(this);
2484 while (it.has_next()) {
2485 it.next()->Print();
2486 }
2487}
2488
2489
2490void LargeObjectSpace::ReportStatistics() {
2491 PrintF(" size: %d\n", size_);
2492 int num_objects = 0;
2493 ClearHistograms();
2494 LargeObjectIterator it(this);
2495 while (it.has_next()) {
2496 num_objects++;
2497 CollectHistogramInfo(it.next());
2498 }
2499
2500 PrintF(" number of objects %d\n", num_objects);
2501 if (num_objects > 0) ReportHistogram(false);
2502}
2503
2504
2505void LargeObjectSpace::CollectCodeStatistics() {
2506 LargeObjectIterator obj_it(this);
2507 while (obj_it.has_next()) {
2508 HeapObject* obj = obj_it.next();
2509 if (obj->IsCode()) {
2510 Code* code = Code::cast(obj);
2511 code_kind_statistics[code->kind()] += code->Size();
2512 }
2513 }
2514}
2515
2516
2517void LargeObjectSpace::PrintRSet() {
2518 LargeObjectIterator it(this);
2519 while (it.has_next()) {
2520 HeapObject* object = it.next();
2521 if (object->IsFixedArray()) {
2522 Page* page = Page::FromAddress(object->address());
2523
2524 Address allocation_top = object->address() + object->Size();
2525 PrintF("large page 0x%x:\n", page);
2526 PrintRSetRange(page->RSetStart(), page->RSetEnd(),
2527 reinterpret_cast<Object**>(object->address()),
2528 allocation_top);
2529 int extra_array_bytes = object->Size() - Page::kObjectAreaSize;
2530 int extra_rset_bits = RoundUp(extra_array_bytes / kPointerSize,
2531 kBitsPerInt);
2532 PrintF("------------------------------------------------------------"
2533 "-----------\n");
2534 PrintRSetRange(allocation_top,
2535 allocation_top + extra_rset_bits / kBitsPerByte,
2536 reinterpret_cast<Object**>(object->address()
2537 + Page::kObjectAreaSize),
2538 allocation_top);
2539 PrintF("\n");
2540 }
2541 }
2542}
2543#endif // DEBUG
2544
2545} } // namespace v8::internal