blob: 2393281bd7711cc6f2c80157a4c95e8cdaa892ec [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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037// For contiguous spaces, top should be in the space (or at the end) and limit
38// should be the end of the space.
39#define ASSERT_SEMISPACE_ALLOCATION_INFO(info, space) \
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000040 ASSERT((space).low() <= (info).top \
41 && (info).top <= (space).high() \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000042 && (info).limit == (space).high())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
45// ----------------------------------------------------------------------------
46// HeapObjectIterator
47
48HeapObjectIterator::HeapObjectIterator(PagedSpace* space) {
49 Initialize(space->bottom(), space->top(), NULL);
50}
51
52
53HeapObjectIterator::HeapObjectIterator(PagedSpace* space,
54 HeapObjectCallback size_func) {
55 Initialize(space->bottom(), space->top(), size_func);
56}
57
58
59HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start) {
60 Initialize(start, space->top(), NULL);
61}
62
63
64HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start,
65 HeapObjectCallback size_func) {
66 Initialize(start, space->top(), size_func);
67}
68
69
70void HeapObjectIterator::Initialize(Address cur, Address end,
71 HeapObjectCallback size_f) {
72 cur_addr_ = cur;
73 end_addr_ = end;
74 end_page_ = Page::FromAllocationTop(end);
75 size_func_ = size_f;
76 Page* p = Page::FromAllocationTop(cur_addr_);
77 cur_limit_ = (p == end_page_) ? end_addr_ : p->AllocationTop();
78
79#ifdef DEBUG
80 Verify();
81#endif
82}
83
84
85bool HeapObjectIterator::HasNextInNextPage() {
86 if (cur_addr_ == end_addr_) return false;
87
88 Page* cur_page = Page::FromAllocationTop(cur_addr_);
89 cur_page = cur_page->next_page();
90 ASSERT(cur_page->is_valid());
91
92 cur_addr_ = cur_page->ObjectAreaStart();
93 cur_limit_ = (cur_page == end_page_) ? end_addr_ : cur_page->AllocationTop();
94
95 ASSERT(cur_addr_ < cur_limit_);
96#ifdef DEBUG
97 Verify();
98#endif
99 return true;
100}
101
102
103#ifdef DEBUG
104void HeapObjectIterator::Verify() {
105 Page* p = Page::FromAllocationTop(cur_addr_);
106 ASSERT(p == Page::FromAllocationTop(cur_limit_));
107 ASSERT(p->Offset(cur_addr_) <= p->Offset(cur_limit_));
108}
109#endif
110
111
112// -----------------------------------------------------------------------------
113// PageIterator
114
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000115PageIterator::PageIterator(PagedSpace* space, Mode mode) : space_(space) {
116 prev_page_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 switch (mode) {
118 case PAGES_IN_USE:
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000119 stop_page_ = space->AllocationTopPage();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 break;
121 case PAGES_USED_BY_MC:
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000122 stop_page_ = space->MCRelocationTopPage();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 break;
124 case ALL_PAGES:
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000125#ifdef DEBUG
126 // Verify that the cached last page in the space is actually the
127 // last page.
128 for (Page* p = space->first_page_; p->is_valid(); p = p->next_page()) {
129 if (!p->next_page()->is_valid()) {
130 ASSERT(space->last_page_ == p);
131 }
132 }
133#endif
134 stop_page_ = space->last_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 break;
136 default:
137 UNREACHABLE();
138 }
139}
140
141
142// -----------------------------------------------------------------------------
143// Page
144
145#ifdef DEBUG
146Page::RSetState Page::rset_state_ = Page::IN_USE;
147#endif
148
149// -----------------------------------------------------------------------------
150// MemoryAllocator
151//
152int MemoryAllocator::capacity_ = 0;
153int MemoryAllocator::size_ = 0;
154
155VirtualMemory* MemoryAllocator::initial_chunk_ = NULL;
156
157// 270 is an estimate based on the static default heap size of a pair of 256K
158// semispaces and a 64M old generation.
159const int kEstimatedNumberOfChunks = 270;
160List<MemoryAllocator::ChunkInfo> MemoryAllocator::chunks_(
161 kEstimatedNumberOfChunks);
162List<int> MemoryAllocator::free_chunk_ids_(kEstimatedNumberOfChunks);
163int MemoryAllocator::max_nof_chunks_ = 0;
164int MemoryAllocator::top_ = 0;
165
166
167void MemoryAllocator::Push(int free_chunk_id) {
168 ASSERT(max_nof_chunks_ > 0);
169 ASSERT(top_ < max_nof_chunks_);
170 free_chunk_ids_[top_++] = free_chunk_id;
171}
172
173
174int MemoryAllocator::Pop() {
175 ASSERT(top_ > 0);
176 return free_chunk_ids_[--top_];
177}
178
179
180bool MemoryAllocator::Setup(int capacity) {
181 capacity_ = RoundUp(capacity, Page::kPageSize);
182
183 // Over-estimate the size of chunks_ array. It assumes the expansion of old
184 // space is always in the unit of a chunk (kChunkSize) except the last
185 // expansion.
186 //
187 // Due to alignment, allocated space might be one page less than required
188 // number (kPagesPerChunk) of pages for old spaces.
189 //
kasper.lund7276f142008-07-30 08:49:36 +0000190 // Reserve two chunk ids for semispaces, one for map space, one for old
191 // space, and one for code space.
192 max_nof_chunks_ = (capacity_ / (kChunkSize - Page::kPageSize)) + 5;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 if (max_nof_chunks_ > kMaxNofChunks) return false;
194
195 size_ = 0;
196 ChunkInfo info; // uninitialized element.
197 for (int i = max_nof_chunks_ - 1; i >= 0; i--) {
198 chunks_.Add(info);
199 free_chunk_ids_.Add(i);
200 }
201 top_ = max_nof_chunks_;
202 return true;
203}
204
205
206void MemoryAllocator::TearDown() {
207 for (int i = 0; i < max_nof_chunks_; i++) {
208 if (chunks_[i].address() != NULL) DeleteChunk(i);
209 }
210 chunks_.Clear();
211 free_chunk_ids_.Clear();
212
213 if (initial_chunk_ != NULL) {
214 LOG(DeleteEvent("InitialChunk", initial_chunk_->address()));
215 delete initial_chunk_;
216 initial_chunk_ = NULL;
217 }
218
219 ASSERT(top_ == max_nof_chunks_); // all chunks are free
220 top_ = 0;
221 capacity_ = 0;
222 size_ = 0;
223 max_nof_chunks_ = 0;
224}
225
226
227void* MemoryAllocator::AllocateRawMemory(const size_t requested,
kasper.lund7276f142008-07-30 08:49:36 +0000228 size_t* allocated,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000229 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 if (size_ + static_cast<int>(requested) > capacity_) return NULL;
231
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000232 void* mem = OS::Allocate(requested, allocated, executable == EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 int alloced = *allocated;
234 size_ += alloced;
235 Counters::memory_allocated.Increment(alloced);
236 return mem;
237}
238
239
240void MemoryAllocator::FreeRawMemory(void* mem, size_t length) {
241 OS::Free(mem, length);
242 Counters::memory_allocated.Decrement(length);
243 size_ -= length;
244 ASSERT(size_ >= 0);
245}
246
247
248void* MemoryAllocator::ReserveInitialChunk(const size_t requested) {
249 ASSERT(initial_chunk_ == NULL);
250
251 initial_chunk_ = new VirtualMemory(requested);
252 CHECK(initial_chunk_ != NULL);
253 if (!initial_chunk_->IsReserved()) {
254 delete initial_chunk_;
255 initial_chunk_ = NULL;
256 return NULL;
257 }
258
259 // We are sure that we have mapped a block of requested addresses.
260 ASSERT(initial_chunk_->size() == requested);
261 LOG(NewEvent("InitialChunk", initial_chunk_->address(), requested));
262 size_ += requested;
263 return initial_chunk_->address();
264}
265
266
267static int PagesInChunk(Address start, size_t size) {
268 // The first page starts on the first page-aligned address from start onward
269 // and the last page ends on the last page-aligned address before
270 // start+size. Page::kPageSize is a power of two so we can divide by
271 // shifting.
272 return (RoundDown(start + size, Page::kPageSize)
273 - RoundUp(start, Page::kPageSize)) >> Page::kPageSizeBits;
274}
275
276
277Page* MemoryAllocator::AllocatePages(int requested_pages, int* allocated_pages,
278 PagedSpace* owner) {
279 if (requested_pages <= 0) return Page::FromAddress(NULL);
280 size_t chunk_size = requested_pages * Page::kPageSize;
281
282 // There is not enough space to guarantee the desired number pages can be
283 // allocated.
284 if (size_ + static_cast<int>(chunk_size) > capacity_) {
285 // Request as many pages as we can.
286 chunk_size = capacity_ - size_;
287 requested_pages = chunk_size >> Page::kPageSizeBits;
288
289 if (requested_pages <= 0) return Page::FromAddress(NULL);
290 }
kasper.lund7276f142008-07-30 08:49:36 +0000291 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 if (chunk == NULL) return Page::FromAddress(NULL);
293 LOG(NewEvent("PagedChunk", chunk, chunk_size));
294
295 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
296 if (*allocated_pages == 0) {
297 FreeRawMemory(chunk, chunk_size);
298 LOG(DeleteEvent("PagedChunk", chunk));
299 return Page::FromAddress(NULL);
300 }
301
302 int chunk_id = Pop();
303 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
304
305 return InitializePagesInChunk(chunk_id, *allocated_pages, owner);
306}
307
308
309Page* MemoryAllocator::CommitPages(Address start, size_t size,
310 PagedSpace* owner, int* num_pages) {
311 ASSERT(start != NULL);
312 *num_pages = PagesInChunk(start, size);
313 ASSERT(*num_pages > 0);
314 ASSERT(initial_chunk_ != NULL);
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000315 ASSERT(InInitialChunk(start));
316 ASSERT(InInitialChunk(start + size - 1));
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000317 if (!initial_chunk_->Commit(start, size, owner->executable() == EXECUTABLE)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 return Page::FromAddress(NULL);
319 }
320 Counters::memory_allocated.Increment(size);
321
322 // So long as we correctly overestimated the number of chunks we should not
323 // run out of chunk ids.
324 CHECK(!OutOfChunkIds());
325 int chunk_id = Pop();
326 chunks_[chunk_id].init(start, size, owner);
327 return InitializePagesInChunk(chunk_id, *num_pages, owner);
328}
329
330
kasper.lund7276f142008-07-30 08:49:36 +0000331bool MemoryAllocator::CommitBlock(Address start,
332 size_t size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000333 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 ASSERT(start != NULL);
335 ASSERT(size > 0);
336 ASSERT(initial_chunk_ != NULL);
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000337 ASSERT(InInitialChunk(start));
338 ASSERT(InInitialChunk(start + size - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339
kasper.lund7276f142008-07-30 08:49:36 +0000340 if (!initial_chunk_->Commit(start, size, executable)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 Counters::memory_allocated.Increment(size);
342 return true;
343}
344
345
346Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
347 PagedSpace* owner) {
348 ASSERT(IsValidChunk(chunk_id));
349 ASSERT(pages_in_chunk > 0);
350
351 Address chunk_start = chunks_[chunk_id].address();
352
353 Address low = RoundUp(chunk_start, Page::kPageSize);
354
355#ifdef DEBUG
356 size_t chunk_size = chunks_[chunk_id].size();
357 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
358 ASSERT(pages_in_chunk <=
359 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
360#endif
361
362 Address page_addr = low;
363 for (int i = 0; i < pages_in_chunk; i++) {
364 Page* p = Page::FromAddress(page_addr);
365 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
366 p->is_normal_page = 1;
367 page_addr += Page::kPageSize;
368 }
369
370 // Set the next page of the last page to 0.
371 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
372 last_page->opaque_header = OffsetFrom(0) | chunk_id;
373
374 return Page::FromAddress(low);
375}
376
377
378Page* MemoryAllocator::FreePages(Page* p) {
379 if (!p->is_valid()) return p;
380
381 // Find the first page in the same chunk as 'p'
382 Page* first_page = FindFirstPageInSameChunk(p);
383 Page* page_to_return = Page::FromAddress(NULL);
384
385 if (p != first_page) {
386 // Find the last page in the same chunk as 'prev'.
387 Page* last_page = FindLastPageInSameChunk(p);
388 first_page = GetNextPage(last_page); // first page in next chunk
389
390 // set the next_page of last_page to NULL
391 SetNextPage(last_page, Page::FromAddress(NULL));
392 page_to_return = p; // return 'p' when exiting
393 }
394
395 while (first_page->is_valid()) {
396 int chunk_id = GetChunkId(first_page);
397 ASSERT(IsValidChunk(chunk_id));
398
399 // Find the first page of the next chunk before deleting this chunk.
400 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
401
402 // Free the current chunk.
403 DeleteChunk(chunk_id);
404 }
405
406 return page_to_return;
407}
408
409
410void MemoryAllocator::DeleteChunk(int chunk_id) {
411 ASSERT(IsValidChunk(chunk_id));
412
413 ChunkInfo& c = chunks_[chunk_id];
414
415 // We cannot free a chunk contained in the initial chunk because it was not
416 // allocated with AllocateRawMemory. Instead we uncommit the virtual
417 // memory.
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000418 if (InInitialChunk(c.address())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 // TODO(1240712): VirtualMemory::Uncommit has a return value which
420 // is ignored here.
421 initial_chunk_->Uncommit(c.address(), c.size());
422 Counters::memory_allocated.Decrement(c.size());
423 } else {
424 LOG(DeleteEvent("PagedChunk", c.address()));
425 FreeRawMemory(c.address(), c.size());
426 }
427 c.init(NULL, 0, NULL);
428 Push(chunk_id);
429}
430
431
432Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
433 int chunk_id = GetChunkId(p);
434 ASSERT(IsValidChunk(chunk_id));
435
436 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
437 return Page::FromAddress(low);
438}
439
440
441Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
442 int chunk_id = GetChunkId(p);
443 ASSERT(IsValidChunk(chunk_id));
444
445 Address chunk_start = chunks_[chunk_id].address();
446 size_t chunk_size = chunks_[chunk_id].size();
447
448 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
449 ASSERT(chunk_start <= p->address() && p->address() < high);
450
451 return Page::FromAddress(high - Page::kPageSize);
452}
453
454
455#ifdef DEBUG
456void MemoryAllocator::ReportStatistics() {
457 float pct = static_cast<float>(capacity_ - size_) / capacity_;
458 PrintF(" capacity: %d, used: %d, available: %%%d\n\n",
459 capacity_, size_, static_cast<int>(pct*100));
460}
461#endif
462
463
464// -----------------------------------------------------------------------------
465// PagedSpace implementation
466
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000467PagedSpace::PagedSpace(int max_capacity,
468 AllocationSpace id,
469 Executability executable)
kasper.lund7276f142008-07-30 08:49:36 +0000470 : Space(id, executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
472 * Page::kObjectAreaSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 accounting_stats_.Clear();
474
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 allocation_info_.top = NULL;
476 allocation_info_.limit = NULL;
477
478 mc_forwarding_info_.top = NULL;
479 mc_forwarding_info_.limit = NULL;
480}
481
482
483bool PagedSpace::Setup(Address start, size_t size) {
484 if (HasBeenSetup()) return false;
485
486 int num_pages = 0;
487 // Try to use the virtual memory range passed to us. If it is too small to
488 // contain at least one page, ignore it and allocate instead.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000489 int pages_in_chunk = PagesInChunk(start, size);
490 if (pages_in_chunk > 0) {
491 first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize),
492 Page::kPageSize * pages_in_chunk,
493 this, &num_pages);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 } else {
495 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
496 max_capacity_ / Page::kObjectAreaSize);
497 first_page_ =
498 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
499 if (!first_page_->is_valid()) return false;
500 }
501
502 // We are sure that the first page is valid and that we have at least one
503 // page.
504 ASSERT(first_page_->is_valid());
505 ASSERT(num_pages > 0);
506 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
507 ASSERT(Capacity() <= max_capacity_);
508
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000509 // Sequentially initialize remembered sets in the newly allocated
510 // pages and cache the current last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
512 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000513 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 }
515
516 // Use first_page_ for allocation.
517 SetAllocationInfo(&allocation_info_, first_page_);
518
519 return true;
520}
521
522
523bool PagedSpace::HasBeenSetup() {
524 return (Capacity() > 0);
525}
526
527
528void PagedSpace::TearDown() {
529 first_page_ = MemoryAllocator::FreePages(first_page_);
530 ASSERT(!first_page_->is_valid());
531
532 accounting_stats_.Clear();
533}
534
535
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000536#ifdef ENABLE_HEAP_PROTECTION
537
538void PagedSpace::Protect() {
539 Page* page = first_page_;
540 while (page->is_valid()) {
541 MemoryAllocator::ProtectChunkFromPage(page);
542 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
543 }
544}
545
546
547void PagedSpace::Unprotect() {
548 Page* page = first_page_;
549 while (page->is_valid()) {
550 MemoryAllocator::UnprotectChunkFromPage(page);
551 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
552 }
553}
554
555#endif
556
557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558void PagedSpace::ClearRSet() {
559 PageIterator it(this, PageIterator::ALL_PAGES);
560 while (it.has_next()) {
561 it.next()->ClearRSet();
562 }
563}
564
565
566Object* PagedSpace::FindObject(Address addr) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 // Note: this function can only be called before or after mark-compact GC
568 // because it accesses map pointers.
569 ASSERT(!MarkCompactCollector::in_use());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570
571 if (!Contains(addr)) return Failure::Exception();
572
573 Page* p = Page::FromAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +0000574 ASSERT(IsUsed(p));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 Address cur = p->ObjectAreaStart();
576 Address end = p->AllocationTop();
577 while (cur < end) {
578 HeapObject* obj = HeapObject::FromAddress(cur);
579 Address next = cur + obj->Size();
580 if ((cur <= addr) && (addr < next)) return obj;
581 cur = next;
582 }
583
kasper.lund7276f142008-07-30 08:49:36 +0000584 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585 return Failure::Exception();
586}
587
588
kasper.lund7276f142008-07-30 08:49:36 +0000589bool PagedSpace::IsUsed(Page* page) {
590 PageIterator it(this, PageIterator::PAGES_IN_USE);
591 while (it.has_next()) {
592 if (page == it.next()) return true;
593 }
594 return false;
595}
596
597
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
599 alloc_info->top = p->ObjectAreaStart();
600 alloc_info->limit = p->ObjectAreaEnd();
kasper.lund7276f142008-07-30 08:49:36 +0000601 ASSERT(alloc_info->VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602}
603
604
605void PagedSpace::MCResetRelocationInfo() {
606 // Set page indexes.
607 int i = 0;
608 PageIterator it(this, PageIterator::ALL_PAGES);
609 while (it.has_next()) {
610 Page* p = it.next();
611 p->mc_page_index = i++;
612 }
613
614 // Set mc_forwarding_info_ to the first page in the space.
615 SetAllocationInfo(&mc_forwarding_info_, first_page_);
616 // All the bytes in the space are 'available'. We will rediscover
617 // allocated and wasted bytes during GC.
618 accounting_stats_.Reset();
619}
620
621
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
623#ifdef DEBUG
624 // The Contains function considers the address at the beginning of a
625 // page in the page, MCSpaceOffsetForAddress considers it is in the
626 // previous page.
627 if (Page::IsAlignedToPageSize(addr)) {
628 ASSERT(Contains(addr - kPointerSize));
629 } else {
630 ASSERT(Contains(addr));
631 }
632#endif
633
634 // If addr is at the end of a page, it belongs to previous page
635 Page* p = Page::IsAlignedToPageSize(addr)
636 ? Page::FromAllocationTop(addr)
637 : Page::FromAddress(addr);
638 int index = p->mc_page_index;
639 return (index * Page::kPageSize) + p->Offset(addr);
640}
641
642
kasper.lund7276f142008-07-30 08:49:36 +0000643// Slow case for reallocating and promoting objects during a compacting
644// collection. This function is not space-specific.
645HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
646 Page* current_page = TopPageOf(mc_forwarding_info_);
647 if (!current_page->next_page()->is_valid()) {
648 if (!Expand(current_page)) {
649 return NULL;
650 }
651 }
652
653 // There are surely more pages in the space now.
654 ASSERT(current_page->next_page()->is_valid());
655 // We do not add the top of page block for current page to the space's
656 // free list---the block may contain live objects so we cannot write
657 // bookkeeping information to it. Instead, we will recover top of page
658 // blocks when we move objects to their new locations.
659 //
660 // We do however write the allocation pointer to the page. The encoding
661 // of forwarding addresses is as an offset in terms of live bytes, so we
662 // need quick access to the allocation top of each page to decode
663 // forwarding addresses.
664 current_page->mc_relocation_top = mc_forwarding_info_.top;
665 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
666 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
667}
668
669
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670bool PagedSpace::Expand(Page* last_page) {
671 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
672 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
673
674 if (Capacity() == max_capacity_) return false;
675
676 ASSERT(Capacity() < max_capacity_);
677 // Last page must be valid and its next page is invalid.
678 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
679
680 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
681 if (available_pages <= 0) return false;
682
683 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
684 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
685 if (!p->is_valid()) return false;
686
687 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
688 ASSERT(Capacity() <= max_capacity_);
689
690 MemoryAllocator::SetNextPage(last_page, p);
691
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000692 // Sequentially clear remembered set of new pages and and cache the
693 // new last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694 while (p->is_valid()) {
695 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000696 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 p = p->next_page();
698 }
699
700 return true;
701}
702
703
704#ifdef DEBUG
705int PagedSpace::CountTotalPages() {
706 int count = 0;
707 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
708 count++;
709 }
710 return count;
711}
712#endif
713
714
715void PagedSpace::Shrink() {
716 // Release half of free pages.
717 Page* top_page = AllocationTopPage();
718 ASSERT(top_page->is_valid());
719
720 // Loop over the pages from the top page to the end of the space to count
721 // the number of pages to keep and find the last page to keep.
722 int free_pages = 0;
723 int pages_to_keep = 0; // Of the free pages.
724 Page* last_page_to_keep = top_page;
725 Page* current_page = top_page->next_page();
726 // Loop over the pages to the end of the space.
727 while (current_page->is_valid()) {
kasper.lund7276f142008-07-30 08:49:36 +0000728 // Advance last_page_to_keep every other step to end up at the midpoint.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729 if ((free_pages & 0x1) == 1) {
730 pages_to_keep++;
731 last_page_to_keep = last_page_to_keep->next_page();
732 }
733 free_pages++;
734 current_page = current_page->next_page();
735 }
736
737 // Free pages after last_page_to_keep, and adjust the next_page link.
738 Page* p = MemoryAllocator::FreePages(last_page_to_keep->next_page());
739 MemoryAllocator::SetNextPage(last_page_to_keep, p);
740
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000741 // Since pages are only freed in whole chunks, we may have kept more
742 // than pages_to_keep. Count the extra pages and cache the new last
743 // page in the space.
744 last_page_ = last_page_to_keep;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 while (p->is_valid()) {
746 pages_to_keep++;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000747 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 p = p->next_page();
749 }
750
751 // The difference between free_pages and pages_to_keep is the number of
752 // pages actually freed.
753 ASSERT(pages_to_keep <= free_pages);
754 int bytes_freed = (free_pages - pages_to_keep) * Page::kObjectAreaSize;
755 accounting_stats_.ShrinkSpace(bytes_freed);
756
757 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
758}
759
760
761bool PagedSpace::EnsureCapacity(int capacity) {
762 if (Capacity() >= capacity) return true;
763
764 // Start from the allocation top and loop to the last page in the space.
765 Page* last_page = AllocationTopPage();
766 Page* next_page = last_page->next_page();
767 while (next_page->is_valid()) {
768 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
769 next_page = last_page->next_page();
770 }
771
772 // Expand the space until it has the required capacity or expansion fails.
773 do {
774 if (!Expand(last_page)) return false;
775 ASSERT(last_page->next_page()->is_valid());
776 last_page =
777 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
778 } while (Capacity() < capacity);
779
780 return true;
781}
782
783
784#ifdef DEBUG
785void PagedSpace::Print() { }
786#endif
787
788
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000789#ifdef DEBUG
790// We do not assume that the PageIterator works, because it depends on the
791// invariants we are checking during verification.
792void PagedSpace::Verify(ObjectVisitor* visitor) {
793 // The allocation pointer should be valid, and it should be in a page in the
794 // space.
795 ASSERT(allocation_info_.VerifyPagedAllocation());
796 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
797 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
798
799 // Loop over all the pages.
800 bool above_allocation_top = false;
801 Page* current_page = first_page_;
802 while (current_page->is_valid()) {
803 if (above_allocation_top) {
804 // We don't care what's above the allocation top.
805 } else {
806 // Unless this is the last page in the space containing allocated
807 // objects, the allocation top should be at a constant offset from the
808 // object area end.
809 Address top = current_page->AllocationTop();
810 if (current_page == top_page) {
811 ASSERT(top == allocation_info_.top);
812 // The next page will be above the allocation top.
813 above_allocation_top = true;
814 } else {
815 ASSERT(top == current_page->ObjectAreaEnd() - page_extra_);
816 }
817
818 // It should be packed with objects from the bottom to the top.
819 Address current = current_page->ObjectAreaStart();
820 while (current < top) {
821 HeapObject* object = HeapObject::FromAddress(current);
822
823 // The first word should be a map, and we expect all map pointers to
824 // be in map space.
825 Map* map = object->map();
826 ASSERT(map->IsMap());
827 ASSERT(Heap::map_space()->Contains(map));
828
829 // Perform space-specific object verification.
830 VerifyObject(object);
831
832 // The object itself should look OK.
833 object->Verify();
834
835 // All the interior pointers should be contained in the heap and
836 // have their remembered set bits set if required as determined
837 // by the visitor.
838 int size = object->Size();
839 if (object->IsCode()) {
840 Code::cast(object)->ConvertICTargetsFromAddressToObject();
841 object->IterateBody(map->instance_type(), size, visitor);
842 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
843 } else {
844 object->IterateBody(map->instance_type(), size, visitor);
845 }
846
847 current += size;
848 }
849
850 // The allocation pointer should not be in the middle of an object.
851 ASSERT(current == top);
852 }
853
854 current_page = current_page->next_page();
855 }
856}
857#endif
858
859
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860// -----------------------------------------------------------------------------
861// NewSpace implementation
862
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000863
864bool NewSpace::Setup(Address start, int size) {
865 // Setup new space based on the preallocated memory block defined by
866 // start and size. The provided space is divided into two semi-spaces.
867 // To support fast containment testing in the new space, the size of
868 // this chunk must be a power of two and it must be aligned to its size.
869 int initial_semispace_capacity = Heap::InitialSemiSpaceSize();
870 int maximum_semispace_capacity = Heap::SemiSpaceSize();
871
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
873 ASSERT(IsPowerOf2(maximum_semispace_capacity));
874 maximum_capacity_ = maximum_semispace_capacity;
875 capacity_ = initial_semispace_capacity;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
877 // Allocate and setup the histogram arrays if necessary.
878#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
879 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
880 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
881
882#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
883 promoted_histogram_[name].set_name(#name);
884 INSTANCE_TYPE_LIST(SET_NAME)
885#undef SET_NAME
886#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 ASSERT(size == 2 * maximum_capacity_);
889 ASSERT(IsAddressAligned(start, size, 0));
890
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000891 if (!to_space_.Setup(start, capacity_, maximum_capacity_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 return false;
893 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000894 if (!from_space_.Setup(start + maximum_capacity_,
895 capacity_,
896 maximum_capacity_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 return false;
898 }
899
900 start_ = start;
901 address_mask_ = ~(size - 1);
902 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +0000903 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000905 allocation_info_.top = to_space_.low();
906 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 mc_forwarding_info_.top = NULL;
908 mc_forwarding_info_.limit = NULL;
909
910 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
911 return true;
912}
913
914
915void NewSpace::TearDown() {
916#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
917 if (allocated_histogram_) {
918 DeleteArray(allocated_histogram_);
919 allocated_histogram_ = NULL;
920 }
921 if (promoted_histogram_) {
922 DeleteArray(promoted_histogram_);
923 promoted_histogram_ = NULL;
924 }
925#endif
926
927 start_ = NULL;
928 capacity_ = 0;
929 allocation_info_.top = NULL;
930 allocation_info_.limit = NULL;
931 mc_forwarding_info_.top = NULL;
932 mc_forwarding_info_.limit = NULL;
933
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000934 to_space_.TearDown();
935 from_space_.TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936}
937
938
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000939#ifdef ENABLE_HEAP_PROTECTION
940
941void NewSpace::Protect() {
942 MemoryAllocator::Protect(ToSpaceLow(), Capacity());
943 MemoryAllocator::Protect(FromSpaceLow(), Capacity());
944}
945
946
947void NewSpace::Unprotect() {
948 MemoryAllocator::Unprotect(ToSpaceLow(), Capacity(),
949 to_space_.executable());
950 MemoryAllocator::Unprotect(FromSpaceLow(), Capacity(),
951 from_space_.executable());
952}
953
954#endif
955
956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957void NewSpace::Flip() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000958 SemiSpace tmp = from_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 from_space_ = to_space_;
960 to_space_ = tmp;
961}
962
963
964bool NewSpace::Double() {
965 ASSERT(capacity_ <= maximum_capacity_ / 2);
966 // TODO(1240712): Failure to double the from space can result in
967 // semispaces of different sizes. In the event of that failure, the
968 // to space doubling should be rolled back before returning false.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000969 if (!to_space_.Double() || !from_space_.Double()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 capacity_ *= 2;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000971 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
973 return true;
974}
975
976
977void NewSpace::ResetAllocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000978 allocation_info_.top = to_space_.low();
979 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
981}
982
983
984void NewSpace::MCResetRelocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000985 mc_forwarding_info_.top = from_space_.low();
986 mc_forwarding_info_.limit = from_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
988}
989
990
991void NewSpace::MCCommitRelocationInfo() {
992 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
993 // valid allocation info for the to space.
994 allocation_info_.top = mc_forwarding_info_.top;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000995 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
997}
998
999
1000#ifdef DEBUG
1001// We do not use the SemispaceIterator because verification doesn't assume
1002// that it works (it depends on the invariants we are checking).
1003void NewSpace::Verify() {
1004 // The allocation pointer should be in the space or at the very end.
1005 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1006
1007 // There should be objects packed in from the low address up to the
1008 // allocation pointer.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001009 Address current = to_space_.low();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 while (current < top()) {
1011 HeapObject* object = HeapObject::FromAddress(current);
1012
1013 // The first word should be a map, and we expect all map pointers to
1014 // be in map space.
1015 Map* map = object->map();
1016 ASSERT(map->IsMap());
1017 ASSERT(Heap::map_space()->Contains(map));
1018
1019 // The object should not be code or a map.
1020 ASSERT(!object->IsMap());
1021 ASSERT(!object->IsCode());
1022
1023 // The object itself should look OK.
1024 object->Verify();
1025
1026 // All the interior pointers should be contained in the heap.
1027 VerifyPointersVisitor visitor;
1028 int size = object->Size();
1029 object->IterateBody(map->instance_type(), size, &visitor);
1030
1031 current += size;
1032 }
1033
1034 // The allocation pointer should not be in the middle of an object.
1035 ASSERT(current == top());
1036}
1037#endif
1038
1039
1040// -----------------------------------------------------------------------------
1041// SemiSpace implementation
1042
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001043bool SemiSpace::Setup(Address start,
1044 int initial_capacity,
1045 int maximum_capacity) {
1046 // Creates a space in the young generation. The constructor does not
1047 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1048 // memory of size 'capacity' when set up, and does not grow or shrink
1049 // otherwise. In the mark-compact collector, the memory region of the from
1050 // space is used as the marking stack. It requires contiguous memory
1051 // addresses.
1052 capacity_ = initial_capacity;
1053 maximum_capacity_ = maximum_capacity;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054
kasper.lund7276f142008-07-30 08:49:36 +00001055 if (!MemoryAllocator::CommitBlock(start, capacity_, executable())) {
1056 return false;
1057 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058
1059 start_ = start;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001060 address_mask_ = ~(maximum_capacity - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +00001062 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063
1064 age_mark_ = start_;
1065 return true;
1066}
1067
1068
1069void SemiSpace::TearDown() {
1070 start_ = NULL;
1071 capacity_ = 0;
1072}
1073
1074
1075bool SemiSpace::Double() {
kasper.lund7276f142008-07-30 08:49:36 +00001076 if (!MemoryAllocator::CommitBlock(high(), capacity_, executable())) {
1077 return false;
1078 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 capacity_ *= 2;
1080 return true;
1081}
1082
1083
1084#ifdef DEBUG
1085void SemiSpace::Print() { }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001086
1087
1088void SemiSpace::Verify() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089#endif
1090
1091
1092// -----------------------------------------------------------------------------
1093// SemiSpaceIterator implementation.
1094SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
1095 Initialize(space, space->bottom(), space->top(), NULL);
1096}
1097
1098
1099SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1100 HeapObjectCallback size_func) {
1101 Initialize(space, space->bottom(), space->top(), size_func);
1102}
1103
1104
1105SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1106 Initialize(space, start, space->top(), NULL);
1107}
1108
1109
1110void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1111 Address end,
1112 HeapObjectCallback size_func) {
1113 ASSERT(space->ToSpaceContains(start));
1114 ASSERT(space->ToSpaceLow() <= end
1115 && end <= space->ToSpaceHigh());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001116 space_ = &space->to_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 current_ = start;
1118 limit_ = end;
1119 size_func_ = size_func;
1120}
1121
1122
1123#ifdef DEBUG
1124// A static array of histogram info for each type.
1125static HistogramInfo heap_histograms[LAST_TYPE+1];
1126static JSObject::SpillInformation js_spill_information;
1127
1128// heap_histograms is shared, always clear it before using it.
1129static void ClearHistograms() {
1130 // We reset the name each time, though it hasn't changed.
1131#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1132 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1133#undef DEF_TYPE_NAME
1134
1135#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1136 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1137#undef CLEAR_HISTOGRAM
1138
1139 js_spill_information.Clear();
1140}
1141
1142
1143static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1144
1145
1146static void ClearCodeKindStatistics() {
1147 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1148 code_kind_statistics[i] = 0;
1149 }
1150}
1151
1152
1153static void ReportCodeKindStatistics() {
1154 const char* table[Code::NUMBER_OF_KINDS];
1155
1156#define CASE(name) \
1157 case Code::name: table[Code::name] = #name; \
1158 break
1159
1160 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1161 switch (static_cast<Code::Kind>(i)) {
1162 CASE(FUNCTION);
1163 CASE(STUB);
1164 CASE(BUILTIN);
1165 CASE(LOAD_IC);
1166 CASE(KEYED_LOAD_IC);
1167 CASE(STORE_IC);
1168 CASE(KEYED_STORE_IC);
1169 CASE(CALL_IC);
1170 }
1171 }
1172
1173#undef CASE
1174
1175 PrintF("\n Code kind histograms: \n");
1176 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1177 if (code_kind_statistics[i] > 0) {
1178 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1179 }
1180 }
1181 PrintF("\n");
1182}
1183
1184
1185static int CollectHistogramInfo(HeapObject* obj) {
1186 InstanceType type = obj->map()->instance_type();
1187 ASSERT(0 <= type && type <= LAST_TYPE);
1188 ASSERT(heap_histograms[type].name() != NULL);
1189 heap_histograms[type].increment_number(1);
1190 heap_histograms[type].increment_bytes(obj->Size());
1191
1192 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1193 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1194 }
1195
1196 return obj->Size();
1197}
1198
1199
1200static void ReportHistogram(bool print_spill) {
1201 PrintF("\n Object Histogram:\n");
1202 for (int i = 0; i <= LAST_TYPE; i++) {
1203 if (heap_histograms[i].number() > 0) {
1204 PrintF(" %-33s%10d (%10d bytes)\n",
1205 heap_histograms[i].name(),
1206 heap_histograms[i].number(),
1207 heap_histograms[i].bytes());
1208 }
1209 }
1210 PrintF("\n");
1211
1212 // Summarize string types.
1213 int string_number = 0;
1214 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001215#define INCREMENT(type, size, name, camel_name) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 string_number += heap_histograms[type].number(); \
1217 string_bytes += heap_histograms[type].bytes();
1218 STRING_TYPE_LIST(INCREMENT)
1219#undef INCREMENT
1220 if (string_number > 0) {
1221 PrintF(" %-33s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
1222 string_bytes);
1223 }
1224
1225 if (FLAG_collect_heap_spill_statistics && print_spill) {
1226 js_spill_information.Print();
1227 }
1228}
1229#endif // DEBUG
1230
1231
1232// Support for statistics gathering for --heap-stats and --log-gc.
1233#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1234void NewSpace::ClearHistograms() {
1235 for (int i = 0; i <= LAST_TYPE; i++) {
1236 allocated_histogram_[i].clear();
1237 promoted_histogram_[i].clear();
1238 }
1239}
1240
1241// Because the copying collector does not touch garbage objects, we iterate
1242// the new space before a collection to get a histogram of allocated objects.
1243// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1244// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1245// flag is set.
1246void NewSpace::CollectStatistics() {
1247 ClearHistograms();
1248 SemiSpaceIterator it(this);
1249 while (it.has_next()) RecordAllocation(it.next());
1250}
1251
1252
1253#ifdef ENABLE_LOGGING_AND_PROFILING
1254static void DoReportStatistics(HistogramInfo* info, const char* description) {
1255 LOG(HeapSampleBeginEvent("NewSpace", description));
1256 // Lump all the string types together.
1257 int string_number = 0;
1258 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001259#define INCREMENT(type, size, name, camel_name) \
1260 string_number += info[type].number(); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 string_bytes += info[type].bytes();
1262 STRING_TYPE_LIST(INCREMENT)
1263#undef INCREMENT
1264 if (string_number > 0) {
1265 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1266 }
1267
1268 // Then do the other types.
1269 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1270 if (info[i].number() > 0) {
1271 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1272 info[i].bytes()));
1273 }
1274 }
1275 LOG(HeapSampleEndEvent("NewSpace", description));
1276}
1277#endif // ENABLE_LOGGING_AND_PROFILING
1278
1279
1280void NewSpace::ReportStatistics() {
1281#ifdef DEBUG
1282 if (FLAG_heap_stats) {
1283 float pct = static_cast<float>(Available()) / Capacity();
1284 PrintF(" capacity: %d, available: %d, %%%d\n",
1285 Capacity(), Available(), static_cast<int>(pct*100));
1286 PrintF("\n Object Histogram:\n");
1287 for (int i = 0; i <= LAST_TYPE; i++) {
1288 if (allocated_histogram_[i].number() > 0) {
1289 PrintF(" %-33s%10d (%10d bytes)\n",
1290 allocated_histogram_[i].name(),
1291 allocated_histogram_[i].number(),
1292 allocated_histogram_[i].bytes());
1293 }
1294 }
1295 PrintF("\n");
1296 }
1297#endif // DEBUG
1298
1299#ifdef ENABLE_LOGGING_AND_PROFILING
1300 if (FLAG_log_gc) {
1301 DoReportStatistics(allocated_histogram_, "allocated");
1302 DoReportStatistics(promoted_histogram_, "promoted");
1303 }
1304#endif // ENABLE_LOGGING_AND_PROFILING
1305}
1306
1307
1308void NewSpace::RecordAllocation(HeapObject* obj) {
1309 InstanceType type = obj->map()->instance_type();
1310 ASSERT(0 <= type && type <= LAST_TYPE);
1311 allocated_histogram_[type].increment_number(1);
1312 allocated_histogram_[type].increment_bytes(obj->Size());
1313}
1314
1315
1316void NewSpace::RecordPromotion(HeapObject* obj) {
1317 InstanceType type = obj->map()->instance_type();
1318 ASSERT(0 <= type && type <= LAST_TYPE);
1319 promoted_histogram_[type].increment_number(1);
1320 promoted_histogram_[type].increment_bytes(obj->Size());
1321}
1322#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1323
1324
1325// -----------------------------------------------------------------------------
1326// Free lists for old object spaces implementation
1327
1328void FreeListNode::set_size(int size_in_bytes) {
1329 ASSERT(size_in_bytes > 0);
1330 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1331
1332 // We write a map and possibly size information to the block. If the block
1333 // is big enough to be a ByteArray with at least one extra word (the next
1334 // pointer), we set its map to be the byte array map and its size to an
1335 // appropriate array length for the desired size from HeapObject::Size().
1336 // If the block is too small (eg, one or two words), to hold both a size
1337 // field and a next pointer, we give it a filler map that gives it the
1338 // correct size.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001339 if (size_in_bytes > ByteArray::kAlignedSize) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001340 set_map(Heap::raw_unchecked_byte_array_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 ByteArray::cast(this)->set_length(ByteArray::LengthFor(size_in_bytes));
1342 } else if (size_in_bytes == kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001343 set_map(Heap::raw_unchecked_one_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 } else if (size_in_bytes == 2 * kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001345 set_map(Heap::raw_unchecked_two_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 } else {
1347 UNREACHABLE();
1348 }
kasper.lund7276f142008-07-30 08:49:36 +00001349 ASSERT(Size() == size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350}
1351
1352
1353Address FreeListNode::next() {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001354 ASSERT(map() == Heap::raw_unchecked_byte_array_map() ||
1355 map() == Heap::raw_unchecked_two_pointer_filler_map());
1356 if (map() == Heap::raw_unchecked_byte_array_map()) {
1357 ASSERT(Size() >= kNextOffset + kPointerSize);
1358 return Memory::Address_at(address() + kNextOffset);
1359 } else {
1360 return Memory::Address_at(address() + kPointerSize);
1361 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362}
1363
1364
1365void FreeListNode::set_next(Address next) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001366 ASSERT(map() == Heap::raw_unchecked_byte_array_map() ||
1367 map() == Heap::raw_unchecked_two_pointer_filler_map());
1368 if (map() == Heap::raw_unchecked_byte_array_map()) {
1369 ASSERT(Size() >= kNextOffset + kPointerSize);
1370 Memory::Address_at(address() + kNextOffset) = next;
1371 } else {
1372 Memory::Address_at(address() + kPointerSize) = next;
1373 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374}
1375
1376
1377OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1378 Reset();
1379}
1380
1381
1382void OldSpaceFreeList::Reset() {
1383 available_ = 0;
1384 for (int i = 0; i < kFreeListsLength; i++) {
1385 free_[i].head_node_ = NULL;
1386 }
1387 needs_rebuild_ = false;
1388 finger_ = kHead;
1389 free_[kHead].next_size_ = kEnd;
1390}
1391
1392
1393void OldSpaceFreeList::RebuildSizeList() {
1394 ASSERT(needs_rebuild_);
1395 int cur = kHead;
1396 for (int i = cur + 1; i < kFreeListsLength; i++) {
1397 if (free_[i].head_node_ != NULL) {
1398 free_[cur].next_size_ = i;
1399 cur = i;
1400 }
1401 }
1402 free_[cur].next_size_ = kEnd;
1403 needs_rebuild_ = false;
1404}
1405
1406
1407int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1408#ifdef DEBUG
1409 for (int i = 0; i < size_in_bytes; i += kPointerSize) {
1410 Memory::Address_at(start + i) = kZapValue;
1411 }
1412#endif
1413 FreeListNode* node = FreeListNode::FromAddress(start);
1414 node->set_size(size_in_bytes);
1415
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001416 // We don't use the freelists in compacting mode. This makes it more like a
1417 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1418 // collector.
1419 if (FLAG_always_compact) {
1420 return size_in_bytes;
1421 }
1422
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 // Early return to drop too-small blocks on the floor (one or two word
1424 // blocks cannot hold a map pointer, a size field, and a pointer to the
1425 // next block in the free list).
1426 if (size_in_bytes < kMinBlockSize) {
1427 return size_in_bytes;
1428 }
1429
1430 // Insert other blocks at the head of an exact free list.
1431 int index = size_in_bytes >> kPointerSizeLog2;
1432 node->set_next(free_[index].head_node_);
1433 free_[index].head_node_ = node->address();
1434 available_ += size_in_bytes;
1435 needs_rebuild_ = true;
1436 return 0;
1437}
1438
1439
1440Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1441 ASSERT(0 < size_in_bytes);
1442 ASSERT(size_in_bytes <= kMaxBlockSize);
1443 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1444
1445 if (needs_rebuild_) RebuildSizeList();
1446 int index = size_in_bytes >> kPointerSizeLog2;
1447 // Check for a perfect fit.
1448 if (free_[index].head_node_ != NULL) {
1449 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1450 // If this was the last block of its size, remove the size.
1451 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1452 available_ -= size_in_bytes;
1453 *wasted_bytes = 0;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001454 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 return node;
1456 }
1457 // Search the size list for the best fit.
1458 int prev = finger_ < index ? finger_ : kHead;
1459 int cur = FindSize(index, &prev);
1460 ASSERT(index < cur);
1461 if (cur == kEnd) {
1462 // No large enough size in list.
1463 *wasted_bytes = 0;
1464 return Failure::RetryAfterGC(size_in_bytes, owner_);
1465 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001466 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 int rem = cur - index;
1468 int rem_bytes = rem << kPointerSizeLog2;
1469 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
kasper.lund7276f142008-07-30 08:49:36 +00001470 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1472 size_in_bytes);
1473 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1474 // to avoid many redundant tests and calls to Insert/RemoveSize.
1475 if (prev < rem) {
1476 // Simple case: insert rem between prev and cur.
1477 finger_ = prev;
1478 free_[prev].next_size_ = rem;
1479 // If this was the last block of size cur, remove the size.
1480 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1481 free_[rem].next_size_ = free_[cur].next_size_;
1482 } else {
1483 free_[rem].next_size_ = cur;
1484 }
1485 // Add the remainder block.
1486 rem_node->set_size(rem_bytes);
1487 rem_node->set_next(free_[rem].head_node_);
1488 free_[rem].head_node_ = rem_node->address();
1489 } else {
1490 // If this was the last block of size cur, remove the size.
1491 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1492 finger_ = prev;
1493 free_[prev].next_size_ = free_[cur].next_size_;
1494 }
1495 if (rem_bytes < kMinBlockSize) {
1496 // Too-small remainder is wasted.
1497 rem_node->set_size(rem_bytes);
1498 available_ -= size_in_bytes + rem_bytes;
1499 *wasted_bytes = rem_bytes;
1500 return cur_node;
1501 }
1502 // Add the remainder block and, if needed, insert its size.
1503 rem_node->set_size(rem_bytes);
1504 rem_node->set_next(free_[rem].head_node_);
1505 free_[rem].head_node_ = rem_node->address();
1506 if (rem_node->next() == NULL) InsertSize(rem);
1507 }
1508 available_ -= size_in_bytes;
1509 *wasted_bytes = 0;
1510 return cur_node;
1511}
1512
1513
kasper.lund7276f142008-07-30 08:49:36 +00001514#ifdef DEBUG
1515bool OldSpaceFreeList::Contains(FreeListNode* node) {
1516 for (int i = 0; i < kFreeListsLength; i++) {
1517 Address cur_addr = free_[i].head_node_;
1518 while (cur_addr != NULL) {
1519 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1520 if (cur_node == node) return true;
1521 cur_addr = cur_node->next();
1522 }
1523 }
1524 return false;
1525}
1526#endif
1527
1528
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001529FixedSizeFreeList::FixedSizeFreeList(AllocationSpace owner, int object_size)
1530 : owner_(owner), object_size_(object_size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 Reset();
1532}
1533
1534
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001535void FixedSizeFreeList::Reset() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536 available_ = 0;
1537 head_ = NULL;
1538}
1539
1540
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001541void FixedSizeFreeList::Free(Address start) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001543 for (int i = 0; i < object_size_; i += kPointerSize) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544 Memory::Address_at(start + i) = kZapValue;
1545 }
1546#endif
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001547 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 FreeListNode* node = FreeListNode::FromAddress(start);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001549 node->set_size(object_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550 node->set_next(head_);
1551 head_ = node->address();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001552 available_ += object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553}
1554
1555
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001556Object* FixedSizeFreeList::Allocate() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557 if (head_ == NULL) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001558 return Failure::RetryAfterGC(object_size_, owner_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 }
1560
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001561 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 FreeListNode* node = FreeListNode::FromAddress(head_);
1563 head_ = node->next();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001564 available_ -= object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 return node;
1566}
1567
1568
1569// -----------------------------------------------------------------------------
1570// OldSpace implementation
1571
1572void OldSpace::PrepareForMarkCompact(bool will_compact) {
1573 if (will_compact) {
1574 // Reset relocation info. During a compacting collection, everything in
1575 // the space is considered 'available' and we will rediscover live data
1576 // and waste during the collection.
1577 MCResetRelocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 ASSERT(Available() == Capacity());
1579 } else {
1580 // During a non-compacting collection, everything below the linear
1581 // allocation pointer is considered allocated (everything above is
1582 // available) and we will rediscover available and wasted bytes during
1583 // the collection.
1584 accounting_stats_.AllocateBytes(free_list_.available());
1585 accounting_stats_.FillWastedBytes(Waste());
1586 }
1587
kasper.lund7276f142008-07-30 08:49:36 +00001588 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590}
1591
1592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593void OldSpace::MCCommitRelocationInfo() {
1594 // Update fast allocation info.
1595 allocation_info_.top = mc_forwarding_info_.top;
1596 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001597 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598
1599 // The space is compacted and we haven't yet built free lists or
1600 // wasted any space.
1601 ASSERT(Waste() == 0);
1602 ASSERT(AvailableFree() == 0);
1603
1604 // Build the free list for the space.
1605 int computed_size = 0;
1606 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1607 while (it.has_next()) {
1608 Page* p = it.next();
1609 // Space below the relocation pointer is allocated.
1610 computed_size += p->mc_relocation_top - p->ObjectAreaStart();
1611 if (it.has_next()) {
1612 // Free the space at the top of the page. We cannot use
1613 // p->mc_relocation_top after the call to Free (because Free will clear
1614 // remembered set bits).
1615 int extra_size = p->ObjectAreaEnd() - p->mc_relocation_top;
1616 if (extra_size > 0) {
1617 int wasted_bytes = free_list_.Free(p->mc_relocation_top, extra_size);
1618 // The bytes we have just "freed" to add to the free list were
1619 // already accounted as available.
1620 accounting_stats_.WasteBytes(wasted_bytes);
1621 }
1622 }
1623 }
1624
1625 // Make sure the computed size - based on the used portion of the pages in
1626 // use - matches the size obtained while computing forwarding addresses.
1627 ASSERT(computed_size == Size());
1628}
1629
1630
kasper.lund7276f142008-07-30 08:49:36 +00001631// Slow case for normal allocation. Try in order: (1) allocate in the next
1632// page in the space, (2) allocate off the space's free list, (3) expand the
1633// space, (4) fail.
1634HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
1635 // Linear allocation in this space has failed. If there is another page
1636 // in the space, move to that page and allocate there. This allocation
1637 // should succeed (size_in_bytes should not be greater than a page's
1638 // object area size).
1639 Page* current_page = TopPageOf(allocation_info_);
1640 if (current_page->next_page()->is_valid()) {
1641 return AllocateInNextPage(current_page, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 }
kasper.lund7276f142008-07-30 08:49:36 +00001643
1644 // There is no next page in this space. Try free list allocation.
1645 int wasted_bytes;
1646 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
1647 accounting_stats_.WasteBytes(wasted_bytes);
1648 if (!result->IsFailure()) {
1649 accounting_stats_.AllocateBytes(size_in_bytes);
1650 return HeapObject::cast(result);
1651 }
1652
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001653 // Free list allocation failed and there is no next page. Fail if we have
1654 // hit the old generation size limit that should cause a garbage
1655 // collection.
1656 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
1657 return NULL;
1658 }
1659
1660 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00001661 ASSERT(!current_page->next_page()->is_valid());
1662 if (Expand(current_page)) {
1663 return AllocateInNextPage(current_page, size_in_bytes);
1664 }
1665
1666 // Finally, fail.
1667 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668}
1669
1670
kasper.lund7276f142008-07-30 08:49:36 +00001671// Add the block at the top of the page to the space's free list, set the
1672// allocation info to the next page (assumed to be one), and allocate
1673// linearly there.
1674HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
1675 int size_in_bytes) {
1676 ASSERT(current_page->next_page()->is_valid());
1677 // Add the block at the top of this page to the free list.
1678 int free_size = current_page->ObjectAreaEnd() - allocation_info_.top;
1679 if (free_size > 0) {
1680 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
1681 accounting_stats_.WasteBytes(wasted_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 }
kasper.lund7276f142008-07-30 08:49:36 +00001683 SetAllocationInfo(&allocation_info_, current_page->next_page());
1684 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685}
1686
1687
1688#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689struct CommentStatistic {
1690 const char* comment;
1691 int size;
1692 int count;
1693 void Clear() {
1694 comment = NULL;
1695 size = 0;
1696 count = 0;
1697 }
1698};
1699
1700
1701// must be small, since an iteration is used for lookup
1702const int kMaxComments = 64;
1703static CommentStatistic comments_statistics[kMaxComments+1];
1704
1705
1706void PagedSpace::ReportCodeStatistics() {
1707 ReportCodeKindStatistics();
1708 PrintF("Code comment statistics (\" [ comment-txt : size/ "
1709 "count (average)\"):\n");
1710 for (int i = 0; i <= kMaxComments; i++) {
1711 const CommentStatistic& cs = comments_statistics[i];
1712 if (cs.size > 0) {
1713 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
1714 cs.size/cs.count);
1715 }
1716 }
1717 PrintF("\n");
1718}
1719
1720
1721void PagedSpace::ResetCodeStatistics() {
1722 ClearCodeKindStatistics();
1723 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
1724 comments_statistics[kMaxComments].comment = "Unknown";
1725 comments_statistics[kMaxComments].size = 0;
1726 comments_statistics[kMaxComments].count = 0;
1727}
1728
1729
1730// Adds comment to 'comment_statistics' table. Performance OK sa long as
1731// 'kMaxComments' is small
1732static void EnterComment(const char* comment, int delta) {
1733 // Do not count empty comments
1734 if (delta <= 0) return;
1735 CommentStatistic* cs = &comments_statistics[kMaxComments];
1736 // Search for a free or matching entry in 'comments_statistics': 'cs'
1737 // points to result.
1738 for (int i = 0; i < kMaxComments; i++) {
1739 if (comments_statistics[i].comment == NULL) {
1740 cs = &comments_statistics[i];
1741 cs->comment = comment;
1742 break;
1743 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
1744 cs = &comments_statistics[i];
1745 break;
1746 }
1747 }
1748 // Update entry for 'comment'
1749 cs->size += delta;
1750 cs->count += 1;
1751}
1752
1753
1754// Call for each nested comment start (start marked with '[ xxx', end marked
1755// with ']'. RelocIterator 'it' must point to a comment reloc info.
1756static void CollectCommentStatistics(RelocIterator* it) {
1757 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00001758 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
1760 if (tmp[0] != '[') {
1761 // Not a nested comment; skip
1762 return;
1763 }
1764
1765 // Search for end of nested comment or a new nested comment
1766 const char* const comment_txt =
1767 reinterpret_cast<const char*>(it->rinfo()->data());
1768 const byte* prev_pc = it->rinfo()->pc();
1769 int flat_delta = 0;
1770 it->next();
1771 while (true) {
1772 // All nested comments must be terminated properly, and therefore exit
1773 // from loop.
1774 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00001775 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776 const char* const txt =
1777 reinterpret_cast<const char*>(it->rinfo()->data());
1778 flat_delta += it->rinfo()->pc() - prev_pc;
1779 if (txt[0] == ']') break; // End of nested comment
1780 // A new comment
1781 CollectCommentStatistics(it);
1782 // Skip code that was covered with previous comment
1783 prev_pc = it->rinfo()->pc();
1784 }
1785 it->next();
1786 }
1787 EnterComment(comment_txt, flat_delta);
1788}
1789
1790
1791// Collects code size statistics:
1792// - by code kind
1793// - by code comment
1794void PagedSpace::CollectCodeStatistics() {
1795 HeapObjectIterator obj_it(this);
1796 while (obj_it.has_next()) {
1797 HeapObject* obj = obj_it.next();
1798 if (obj->IsCode()) {
1799 Code* code = Code::cast(obj);
1800 code_kind_statistics[code->kind()] += code->Size();
1801 RelocIterator it(code);
1802 int delta = 0;
1803 const byte* prev_pc = code->instruction_start();
1804 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001805 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 delta += it.rinfo()->pc() - prev_pc;
1807 CollectCommentStatistics(&it);
1808 prev_pc = it.rinfo()->pc();
1809 }
1810 it.next();
1811 }
1812
1813 ASSERT(code->instruction_start() <= prev_pc &&
1814 prev_pc <= code->relocation_start());
1815 delta += code->relocation_start() - prev_pc;
1816 EnterComment("NoComment", delta);
1817 }
1818 }
1819}
1820
1821
1822void OldSpace::ReportStatistics() {
1823 int pct = Available() * 100 / Capacity();
1824 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
1825 Capacity(), Waste(), Available(), pct);
1826
1827 // Report remembered set statistics.
1828 int rset_marked_pointers = 0;
1829 int rset_marked_arrays = 0;
1830 int rset_marked_array_elements = 0;
1831 int cross_gen_pointers = 0;
1832 int cross_gen_array_elements = 0;
1833
1834 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
1835 while (page_it.has_next()) {
1836 Page* p = page_it.next();
1837
1838 for (Address rset_addr = p->RSetStart();
1839 rset_addr < p->RSetEnd();
1840 rset_addr += kIntSize) {
1841 int rset = Memory::int_at(rset_addr);
1842 if (rset != 0) {
1843 // Bits were set
1844 int intoff = rset_addr - p->address();
1845 int bitoff = 0;
1846 for (; bitoff < kBitsPerInt; ++bitoff) {
1847 if ((rset & (1 << bitoff)) != 0) {
1848 int bitpos = intoff*kBitsPerByte + bitoff;
1849 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
1850 Object** obj = reinterpret_cast<Object**>(slot);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001851 if (*obj == Heap::raw_unchecked_fixed_array_map()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 rset_marked_arrays++;
1853 FixedArray* fa = FixedArray::cast(HeapObject::FromAddress(slot));
1854
1855 rset_marked_array_elements += fa->length();
1856 // Manually inline FixedArray::IterateBody
1857 Address elm_start = slot + FixedArray::kHeaderSize;
1858 Address elm_stop = elm_start + fa->length() * kPointerSize;
1859 for (Address elm_addr = elm_start;
1860 elm_addr < elm_stop; elm_addr += kPointerSize) {
1861 // Filter non-heap-object pointers
1862 Object** elm_p = reinterpret_cast<Object**>(elm_addr);
1863 if (Heap::InNewSpace(*elm_p))
1864 cross_gen_array_elements++;
1865 }
1866 } else {
1867 rset_marked_pointers++;
1868 if (Heap::InNewSpace(*obj))
1869 cross_gen_pointers++;
1870 }
1871 }
1872 }
1873 }
1874 }
1875 }
1876
1877 pct = rset_marked_pointers == 0 ?
1878 0 : cross_gen_pointers * 100 / rset_marked_pointers;
1879 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
1880 rset_marked_pointers, cross_gen_pointers, pct);
1881 PrintF(" rset_marked arrays %d, ", rset_marked_arrays);
1882 PrintF(" elements %d, ", rset_marked_array_elements);
1883 pct = rset_marked_array_elements == 0 ? 0
1884 : cross_gen_array_elements * 100 / rset_marked_array_elements;
1885 PrintF(" pointers to new space %d (%%%d)\n", cross_gen_array_elements, pct);
1886 PrintF(" total rset-marked bits %d\n",
1887 (rset_marked_pointers + rset_marked_arrays));
1888 pct = (rset_marked_pointers + rset_marked_array_elements) == 0 ? 0
1889 : (cross_gen_pointers + cross_gen_array_elements) * 100 /
1890 (rset_marked_pointers + rset_marked_array_elements);
1891 PrintF(" total rset pointers %d, true cross generation ones %d (%%%d)\n",
1892 (rset_marked_pointers + rset_marked_array_elements),
1893 (cross_gen_pointers + cross_gen_array_elements),
1894 pct);
1895
1896 ClearHistograms();
1897 HeapObjectIterator obj_it(this);
1898 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
1899 ReportHistogram(true);
1900}
1901
1902
1903// Dump the range of remembered set words between [start, end) corresponding
1904// to the pointers starting at object_p. The allocation_top is an object
1905// pointer which should not be read past. This is important for large object
1906// pages, where some bits in the remembered set range do not correspond to
1907// allocated addresses.
1908static void PrintRSetRange(Address start, Address end, Object** object_p,
1909 Address allocation_top) {
1910 Address rset_address = start;
1911
1912 // If the range starts on on odd numbered word (eg, for large object extra
1913 // remembered set ranges), print some spaces.
ager@chromium.org9085a012009-05-11 19:22:57 +00001914 if ((reinterpret_cast<uintptr_t>(start) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 PrintF(" ");
1916 }
1917
1918 // Loop over all the words in the range.
1919 while (rset_address < end) {
1920 uint32_t rset_word = Memory::uint32_at(rset_address);
1921 int bit_position = 0;
1922
1923 // Loop over all the bits in the word.
1924 while (bit_position < kBitsPerInt) {
1925 if (object_p == reinterpret_cast<Object**>(allocation_top)) {
1926 // Print a bar at the allocation pointer.
1927 PrintF("|");
1928 } else if (object_p > reinterpret_cast<Object**>(allocation_top)) {
1929 // Do not dereference object_p past the allocation pointer.
1930 PrintF("#");
1931 } else if ((rset_word & (1 << bit_position)) == 0) {
1932 // Print a dot for zero bits.
1933 PrintF(".");
1934 } else if (Heap::InNewSpace(*object_p)) {
1935 // Print an X for one bits for pointers to new space.
1936 PrintF("X");
1937 } else {
1938 // Print a circle for one bits for pointers to old space.
1939 PrintF("o");
1940 }
1941
1942 // Print a space after every 8th bit except the last.
1943 if (bit_position % 8 == 7 && bit_position != (kBitsPerInt - 1)) {
1944 PrintF(" ");
1945 }
1946
1947 // Advance to next bit.
1948 bit_position++;
1949 object_p++;
1950 }
1951
1952 // Print a newline after every odd numbered word, otherwise a space.
ager@chromium.org9085a012009-05-11 19:22:57 +00001953 if ((reinterpret_cast<uintptr_t>(rset_address) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 PrintF("\n");
1955 } else {
1956 PrintF(" ");
1957 }
1958
1959 // Advance to next remembered set word.
1960 rset_address += kIntSize;
1961 }
1962}
1963
1964
1965void PagedSpace::DoPrintRSet(const char* space_name) {
1966 PageIterator it(this, PageIterator::PAGES_IN_USE);
1967 while (it.has_next()) {
1968 Page* p = it.next();
1969 PrintF("%s page 0x%x:\n", space_name, p);
1970 PrintRSetRange(p->RSetStart(), p->RSetEnd(),
1971 reinterpret_cast<Object**>(p->ObjectAreaStart()),
1972 p->AllocationTop());
1973 PrintF("\n");
1974 }
1975}
1976
1977
1978void OldSpace::PrintRSet() { DoPrintRSet("old"); }
1979#endif
1980
1981// -----------------------------------------------------------------------------
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001982// FixedSpace implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001984void FixedSpace::PrepareForMarkCompact(bool will_compact) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985 if (will_compact) {
1986 // Reset relocation info.
1987 MCResetRelocationInfo();
1988
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 // During a compacting collection, everything in the space is considered
1990 // 'available' (set by the call to MCResetRelocationInfo) and we will
1991 // rediscover live and wasted bytes during the collection.
1992 ASSERT(Available() == Capacity());
1993 } else {
1994 // During a non-compacting collection, everything below the linear
1995 // allocation pointer except wasted top-of-page blocks is considered
1996 // allocated and we will rediscover available bytes during the
1997 // collection.
1998 accounting_stats_.AllocateBytes(free_list_.available());
1999 }
2000
kasper.lund7276f142008-07-30 08:49:36 +00002001 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003}
2004
2005
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002006void FixedSpace::MCCommitRelocationInfo() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 // Update fast allocation info.
2008 allocation_info_.top = mc_forwarding_info_.top;
2009 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00002010 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011
2012 // The space is compacted and we haven't yet wasted any space.
2013 ASSERT(Waste() == 0);
2014
2015 // Update allocation_top of each page in use and compute waste.
2016 int computed_size = 0;
2017 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2018 while (it.has_next()) {
2019 Page* page = it.next();
2020 Address page_top = page->AllocationTop();
2021 computed_size += page_top - page->ObjectAreaStart();
2022 if (it.has_next()) {
2023 accounting_stats_.WasteBytes(page->ObjectAreaEnd() - page_top);
2024 }
2025 }
2026
2027 // Make sure the computed size - based on the used portion of the
2028 // pages in use - matches the size we adjust during allocation.
2029 ASSERT(computed_size == Size());
2030}
2031
2032
kasper.lund7276f142008-07-30 08:49:36 +00002033// Slow case for normal allocation. Try in order: (1) allocate in the next
2034// page in the space, (2) allocate off the space's free list, (3) expand the
2035// space, (4) fail.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002036HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2037 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002038 // Linear allocation in this space has failed. If there is another page
2039 // in the space, move to that page and allocate there. This allocation
2040 // should succeed.
2041 Page* current_page = TopPageOf(allocation_info_);
2042 if (current_page->next_page()->is_valid()) {
2043 return AllocateInNextPage(current_page, size_in_bytes);
2044 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002046 // There is no next page in this space. Try free list allocation.
2047 // The fixed space free list implicitly assumes that all free blocks
2048 // are of the fixed size.
2049 if (size_in_bytes == object_size_in_bytes_) {
kasper.lund7276f142008-07-30 08:49:36 +00002050 Object* result = free_list_.Allocate();
2051 if (!result->IsFailure()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 accounting_stats_.AllocateBytes(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002053 return HeapObject::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 }
2055 }
kasper.lund7276f142008-07-30 08:49:36 +00002056
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002057 // Free list allocation failed and there is no next page. Fail if we have
2058 // hit the old generation size limit that should cause a garbage
2059 // collection.
2060 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2061 return NULL;
2062 }
2063
2064 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00002065 ASSERT(!current_page->next_page()->is_valid());
2066 if (Expand(current_page)) {
2067 return AllocateInNextPage(current_page, size_in_bytes);
2068 }
2069
2070 // Finally, fail.
2071 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072}
2073
2074
kasper.lund7276f142008-07-30 08:49:36 +00002075// Move to the next page (there is assumed to be one) and allocate there.
2076// The top of page block is always wasted, because it is too small to hold a
2077// map.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002078HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2079 int size_in_bytes) {
kasper.lund7276f142008-07-30 08:49:36 +00002080 ASSERT(current_page->next_page()->is_valid());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002081 ASSERT(current_page->ObjectAreaEnd() - allocation_info_.top == page_extra_);
2082 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2083 accounting_stats_.WasteBytes(page_extra_);
kasper.lund7276f142008-07-30 08:49:36 +00002084 SetAllocationInfo(&allocation_info_, current_page->next_page());
2085 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086}
2087
2088
2089#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002090void FixedSpace::ReportStatistics() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 int pct = Available() * 100 / Capacity();
2092 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2093 Capacity(), Waste(), Available(), pct);
2094
2095 // Report remembered set statistics.
2096 int rset_marked_pointers = 0;
2097 int cross_gen_pointers = 0;
2098
2099 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2100 while (page_it.has_next()) {
2101 Page* p = page_it.next();
2102
2103 for (Address rset_addr = p->RSetStart();
2104 rset_addr < p->RSetEnd();
2105 rset_addr += kIntSize) {
2106 int rset = Memory::int_at(rset_addr);
2107 if (rset != 0) {
2108 // Bits were set
2109 int intoff = rset_addr - p->address();
2110 int bitoff = 0;
2111 for (; bitoff < kBitsPerInt; ++bitoff) {
2112 if ((rset & (1 << bitoff)) != 0) {
2113 int bitpos = intoff*kBitsPerByte + bitoff;
2114 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2115 Object** obj = reinterpret_cast<Object**>(slot);
2116 rset_marked_pointers++;
2117 if (Heap::InNewSpace(*obj))
2118 cross_gen_pointers++;
2119 }
2120 }
2121 }
2122 }
2123 }
2124
2125 pct = rset_marked_pointers == 0 ?
2126 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2127 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2128 rset_marked_pointers, cross_gen_pointers, pct);
2129
2130 ClearHistograms();
2131 HeapObjectIterator obj_it(this);
2132 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
2133 ReportHistogram(false);
2134}
2135
2136
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002137void FixedSpace::PrintRSet() { DoPrintRSet(name_); }
2138#endif
2139
2140
2141// -----------------------------------------------------------------------------
2142// MapSpace implementation
2143
2144void MapSpace::PrepareForMarkCompact(bool will_compact) {
2145 // Call prepare of the super class.
2146 FixedSpace::PrepareForMarkCompact(will_compact);
2147
2148 if (will_compact) {
2149 // Initialize map index entry.
2150 int page_count = 0;
2151 PageIterator it(this, PageIterator::ALL_PAGES);
2152 while (it.has_next()) {
2153 ASSERT_MAP_PAGE_INDEX(page_count);
2154
2155 Page* p = it.next();
2156 ASSERT(p->mc_page_index == page_count);
2157
2158 page_addresses_[page_count++] = p->address();
2159 }
2160 }
2161}
2162
2163
2164#ifdef DEBUG
2165void MapSpace::VerifyObject(HeapObject* object) {
2166 // The object should be a map or a free-list node.
2167 ASSERT(object->IsMap() || object->IsByteArray());
2168}
2169#endif
2170
2171
2172// -----------------------------------------------------------------------------
2173// GlobalPropertyCellSpace implementation
2174
2175#ifdef DEBUG
2176void CellSpace::VerifyObject(HeapObject* object) {
2177 // The object should be a global object property cell or a free-list node.
2178 ASSERT(object->IsJSGlobalPropertyCell() ||
2179 object->map() == Heap::two_pointer_filler_map());
2180}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181#endif
2182
2183
2184// -----------------------------------------------------------------------------
2185// LargeObjectIterator
2186
2187LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2188 current_ = space->first_chunk_;
2189 size_func_ = NULL;
2190}
2191
2192
2193LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2194 HeapObjectCallback size_func) {
2195 current_ = space->first_chunk_;
2196 size_func_ = size_func;
2197}
2198
2199
2200HeapObject* LargeObjectIterator::next() {
2201 ASSERT(has_next());
2202 HeapObject* object = current_->GetObject();
2203 current_ = current_->next();
2204 return object;
2205}
2206
2207
2208// -----------------------------------------------------------------------------
2209// LargeObjectChunk
2210
2211LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
kasper.lund7276f142008-07-30 08:49:36 +00002212 size_t* chunk_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002213 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 size_t requested = ChunkSizeFor(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002215 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2216 chunk_size,
2217 executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 if (mem == NULL) return NULL;
2219 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2220 if (*chunk_size < requested) {
2221 MemoryAllocator::FreeRawMemory(mem, *chunk_size);
2222 LOG(DeleteEvent("LargeObjectChunk", mem));
2223 return NULL;
2224 }
2225 return reinterpret_cast<LargeObjectChunk*>(mem);
2226}
2227
2228
2229int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
2230 int os_alignment = OS::AllocateAlignment();
2231 if (os_alignment < Page::kPageSize)
2232 size_in_bytes += (Page::kPageSize - os_alignment);
2233 return size_in_bytes + Page::kObjectStartOffset;
2234}
2235
2236// -----------------------------------------------------------------------------
2237// LargeObjectSpace
2238
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002239LargeObjectSpace::LargeObjectSpace(AllocationSpace id)
2240 : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis
kasper.lund7276f142008-07-30 08:49:36 +00002241 first_chunk_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 size_(0),
2243 page_count_(0) {}
2244
2245
2246bool LargeObjectSpace::Setup() {
2247 first_chunk_ = NULL;
2248 size_ = 0;
2249 page_count_ = 0;
2250 return true;
2251}
2252
2253
2254void LargeObjectSpace::TearDown() {
2255 while (first_chunk_ != NULL) {
2256 LargeObjectChunk* chunk = first_chunk_;
2257 first_chunk_ = first_chunk_->next();
2258 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
2259 MemoryAllocator::FreeRawMemory(chunk->address(), chunk->size());
2260 }
2261
2262 size_ = 0;
2263 page_count_ = 0;
2264}
2265
2266
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00002267#ifdef ENABLE_HEAP_PROTECTION
2268
2269void LargeObjectSpace::Protect() {
2270 LargeObjectChunk* chunk = first_chunk_;
2271 while (chunk != NULL) {
2272 MemoryAllocator::Protect(chunk->address(), chunk->size());
2273 chunk = chunk->next();
2274 }
2275}
2276
2277
2278void LargeObjectSpace::Unprotect() {
2279 LargeObjectChunk* chunk = first_chunk_;
2280 while (chunk != NULL) {
2281 bool is_code = chunk->GetObject()->IsCode();
2282 MemoryAllocator::Unprotect(chunk->address(), chunk->size(),
2283 is_code ? EXECUTABLE : NOT_EXECUTABLE);
2284 chunk = chunk->next();
2285 }
2286}
2287
2288#endif
2289
2290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002292 int object_size,
2293 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 ASSERT(0 < object_size && object_size <= requested_size);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002295
2296 // Check if we want to force a GC before growing the old space further.
2297 // If so, fail the allocation.
2298 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2299 return Failure::RetryAfterGC(requested_size, identity());
2300 }
2301
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 size_t chunk_size;
2303 LargeObjectChunk* chunk =
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002304 LargeObjectChunk::New(requested_size, &chunk_size, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 if (chunk == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00002306 return Failure::RetryAfterGC(requested_size, identity());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 }
2308
2309 size_ += chunk_size;
2310 page_count_++;
2311 chunk->set_next(first_chunk_);
2312 chunk->set_size(chunk_size);
2313 first_chunk_ = chunk;
2314
2315 // Set the object address and size in the page header and clear its
2316 // remembered set.
2317 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2318 Address object_address = page->ObjectAreaStart();
2319 // Clear the low order bit of the second word in the page to flag it as a
2320 // large object page. If the chunk_size happened to be written there, its
2321 // low order bit should already be clear.
2322 ASSERT((chunk_size & 0x1) == 0);
2323 page->is_normal_page &= ~0x1;
2324 page->ClearRSet();
2325 int extra_bytes = requested_size - object_size;
2326 if (extra_bytes > 0) {
2327 // The extra memory for the remembered set should be cleared.
2328 memset(object_address + object_size, 0, extra_bytes);
2329 }
2330
2331 return HeapObject::FromAddress(object_address);
2332}
2333
2334
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002335Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 ASSERT(0 < size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002337 return AllocateRawInternal(size_in_bytes,
2338 size_in_bytes,
2339 EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340}
2341
2342
2343Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002344 ASSERT(0 < size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 int extra_rset_bytes = ExtraRSetBytesFor(size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002346 return AllocateRawInternal(size_in_bytes + extra_rset_bytes,
2347 size_in_bytes,
2348 NOT_EXECUTABLE);
2349}
2350
2351
2352Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2353 ASSERT(0 < size_in_bytes);
2354 return AllocateRawInternal(size_in_bytes,
2355 size_in_bytes,
2356 NOT_EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357}
2358
2359
2360// GC support
2361Object* LargeObjectSpace::FindObject(Address a) {
2362 for (LargeObjectChunk* chunk = first_chunk_;
2363 chunk != NULL;
2364 chunk = chunk->next()) {
2365 Address chunk_address = chunk->address();
2366 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2367 return chunk->GetObject();
2368 }
2369 }
2370 return Failure::Exception();
2371}
2372
2373
2374void LargeObjectSpace::ClearRSet() {
2375 ASSERT(Page::is_rset_in_use());
2376
2377 LargeObjectIterator it(this);
2378 while (it.has_next()) {
2379 HeapObject* object = it.next();
2380 // We only have code, sequential strings, or fixed arrays in large
2381 // object space, and only fixed arrays need remembered set support.
2382 if (object->IsFixedArray()) {
2383 // Clear the normal remembered set region of the page;
2384 Page* page = Page::FromAddress(object->address());
2385 page->ClearRSet();
2386
2387 // Clear the extra remembered set.
2388 int size = object->Size();
2389 int extra_rset_bytes = ExtraRSetBytesFor(size);
2390 memset(object->address() + size, 0, extra_rset_bytes);
2391 }
2392 }
2393}
2394
2395
2396void LargeObjectSpace::IterateRSet(ObjectSlotCallback copy_object_func) {
2397 ASSERT(Page::is_rset_in_use());
2398
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002399 static void* lo_rset_histogram = StatsTable::CreateHistogram(
2400 "V8.RSetLO",
2401 0,
2402 // Keeping this histogram's buckets the same as the paged space histogram.
2403 Page::kObjectAreaSize / kPointerSize,
2404 30);
2405
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 LargeObjectIterator it(this);
2407 while (it.has_next()) {
2408 // We only have code, sequential strings, or fixed arrays in large
2409 // object space, and only fixed arrays can possibly contain pointers to
2410 // the young generation.
2411 HeapObject* object = it.next();
2412 if (object->IsFixedArray()) {
2413 // Iterate the normal page remembered set range.
2414 Page* page = Page::FromAddress(object->address());
2415 Address object_end = object->address() + object->Size();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002416 int count = Heap::IterateRSetRange(page->ObjectAreaStart(),
2417 Min(page->ObjectAreaEnd(), object_end),
2418 page->RSetStart(),
2419 copy_object_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420
2421 // Iterate the extra array elements.
2422 if (object_end > page->ObjectAreaEnd()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002423 count += Heap::IterateRSetRange(page->ObjectAreaEnd(), object_end,
2424 object_end, copy_object_func);
2425 }
2426 if (lo_rset_histogram != NULL) {
2427 StatsTable::AddHistogramSample(lo_rset_histogram, count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 }
2429 }
2430 }
2431}
2432
2433
2434void LargeObjectSpace::FreeUnmarkedObjects() {
2435 LargeObjectChunk* previous = NULL;
2436 LargeObjectChunk* current = first_chunk_;
2437 while (current != NULL) {
2438 HeapObject* object = current->GetObject();
kasper.lund7276f142008-07-30 08:49:36 +00002439 if (object->IsMarked()) {
2440 object->ClearMark();
2441 MarkCompactCollector::tracer()->decrement_marked_count();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442 previous = current;
2443 current = current->next();
2444 } else {
2445 Address chunk_address = current->address();
2446 size_t chunk_size = current->size();
2447
2448 // Cut the chunk out from the chunk list.
2449 current = current->next();
2450 if (previous == NULL) {
2451 first_chunk_ = current;
2452 } else {
2453 previous->set_next(current);
2454 }
2455
2456 // Free the chunk.
2457 if (object->IsCode()) {
2458 LOG(CodeDeleteEvent(object->address()));
2459 }
2460 size_ -= chunk_size;
2461 page_count_--;
2462 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size);
2463 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2464 }
2465 }
2466}
2467
2468
2469bool LargeObjectSpace::Contains(HeapObject* object) {
2470 Address address = object->address();
2471 Page* page = Page::FromAddress(address);
2472
2473 SLOW_ASSERT(!page->IsLargeObjectPage()
2474 || !FindObject(address)->IsFailure());
2475
2476 return page->IsLargeObjectPage();
2477}
2478
2479
2480#ifdef DEBUG
2481// We do not assume that the large object iterator works, because it depends
2482// on the invariants we are checking during verification.
2483void LargeObjectSpace::Verify() {
2484 for (LargeObjectChunk* chunk = first_chunk_;
2485 chunk != NULL;
2486 chunk = chunk->next()) {
2487 // Each chunk contains an object that starts at the large object page's
2488 // object area start.
2489 HeapObject* object = chunk->GetObject();
2490 Page* page = Page::FromAddress(object->address());
2491 ASSERT(object->address() == page->ObjectAreaStart());
2492
2493 // The first word should be a map, and we expect all map pointers to be
2494 // in map space.
2495 Map* map = object->map();
2496 ASSERT(map->IsMap());
2497 ASSERT(Heap::map_space()->Contains(map));
2498
2499 // We have only code, sequential strings, fixed arrays, and byte arrays
2500 // in large object space.
2501 ASSERT(object->IsCode() || object->IsSeqString()
2502 || object->IsFixedArray() || object->IsByteArray());
2503
2504 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002505 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506
2507 // Byte arrays and strings don't have interior pointers.
2508 if (object->IsCode()) {
2509 VerifyPointersVisitor code_visitor;
2510 Code::cast(object)->ConvertICTargetsFromAddressToObject();
2511 object->IterateBody(map->instance_type(),
2512 object->Size(),
2513 &code_visitor);
2514 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
2515 } else if (object->IsFixedArray()) {
2516 // We loop over fixed arrays ourselves, rather then using the visitor,
2517 // because the visitor doesn't support the start/offset iteration
2518 // needed for IsRSetSet.
2519 FixedArray* array = FixedArray::cast(object);
2520 for (int j = 0; j < array->length(); j++) {
2521 Object* element = array->get(j);
2522 if (element->IsHeapObject()) {
2523 HeapObject* element_object = HeapObject::cast(element);
2524 ASSERT(Heap::Contains(element_object));
2525 ASSERT(element_object->map()->IsMap());
2526 if (Heap::InNewSpace(element_object)) {
2527 ASSERT(Page::IsRSetSet(object->address(),
2528 FixedArray::kHeaderSize + j * kPointerSize));
2529 }
2530 }
2531 }
2532 }
2533 }
2534}
2535
2536
2537void LargeObjectSpace::Print() {
2538 LargeObjectIterator it(this);
2539 while (it.has_next()) {
2540 it.next()->Print();
2541 }
2542}
2543
2544
2545void LargeObjectSpace::ReportStatistics() {
2546 PrintF(" size: %d\n", size_);
2547 int num_objects = 0;
2548 ClearHistograms();
2549 LargeObjectIterator it(this);
2550 while (it.has_next()) {
2551 num_objects++;
2552 CollectHistogramInfo(it.next());
2553 }
2554
2555 PrintF(" number of objects %d\n", num_objects);
2556 if (num_objects > 0) ReportHistogram(false);
2557}
2558
2559
2560void LargeObjectSpace::CollectCodeStatistics() {
2561 LargeObjectIterator obj_it(this);
2562 while (obj_it.has_next()) {
2563 HeapObject* obj = obj_it.next();
2564 if (obj->IsCode()) {
2565 Code* code = Code::cast(obj);
2566 code_kind_statistics[code->kind()] += code->Size();
2567 }
2568 }
2569}
2570
2571
2572void LargeObjectSpace::PrintRSet() {
2573 LargeObjectIterator it(this);
2574 while (it.has_next()) {
2575 HeapObject* object = it.next();
2576 if (object->IsFixedArray()) {
2577 Page* page = Page::FromAddress(object->address());
2578
2579 Address allocation_top = object->address() + object->Size();
2580 PrintF("large page 0x%x:\n", page);
2581 PrintRSetRange(page->RSetStart(), page->RSetEnd(),
2582 reinterpret_cast<Object**>(object->address()),
2583 allocation_top);
2584 int extra_array_bytes = object->Size() - Page::kObjectAreaSize;
2585 int extra_rset_bits = RoundUp(extra_array_bytes / kPointerSize,
2586 kBitsPerInt);
2587 PrintF("------------------------------------------------------------"
2588 "-----------\n");
2589 PrintRSetRange(allocation_top,
2590 allocation_top + extra_rset_bits / kBitsPerByte,
2591 reinterpret_cast<Object**>(object->address()
2592 + Page::kObjectAreaSize),
2593 allocation_top);
2594 PrintF("\n");
2595 }
2596 }
2597}
2598#endif // DEBUG
2599
2600} } // namespace v8::internal