blob: 6283bbad9365f949bbf74380b0e543fdf8f59cac [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;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 }
137}
138
139
140// -----------------------------------------------------------------------------
141// Page
142
143#ifdef DEBUG
144Page::RSetState Page::rset_state_ = Page::IN_USE;
145#endif
146
147// -----------------------------------------------------------------------------
148// MemoryAllocator
149//
150int MemoryAllocator::capacity_ = 0;
151int MemoryAllocator::size_ = 0;
152
153VirtualMemory* MemoryAllocator::initial_chunk_ = NULL;
154
155// 270 is an estimate based on the static default heap size of a pair of 256K
156// semispaces and a 64M old generation.
157const int kEstimatedNumberOfChunks = 270;
158List<MemoryAllocator::ChunkInfo> MemoryAllocator::chunks_(
159 kEstimatedNumberOfChunks);
160List<int> MemoryAllocator::free_chunk_ids_(kEstimatedNumberOfChunks);
161int MemoryAllocator::max_nof_chunks_ = 0;
162int MemoryAllocator::top_ = 0;
163
164
165void MemoryAllocator::Push(int free_chunk_id) {
166 ASSERT(max_nof_chunks_ > 0);
167 ASSERT(top_ < max_nof_chunks_);
168 free_chunk_ids_[top_++] = free_chunk_id;
169}
170
171
172int MemoryAllocator::Pop() {
173 ASSERT(top_ > 0);
174 return free_chunk_ids_[--top_];
175}
176
177
178bool MemoryAllocator::Setup(int capacity) {
179 capacity_ = RoundUp(capacity, Page::kPageSize);
180
181 // Over-estimate the size of chunks_ array. It assumes the expansion of old
182 // space is always in the unit of a chunk (kChunkSize) except the last
183 // expansion.
184 //
185 // Due to alignment, allocated space might be one page less than required
186 // number (kPagesPerChunk) of pages for old spaces.
187 //
kasper.lund7276f142008-07-30 08:49:36 +0000188 // Reserve two chunk ids for semispaces, one for map space, one for old
189 // space, and one for code space.
190 max_nof_chunks_ = (capacity_ / (kChunkSize - Page::kPageSize)) + 5;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 if (max_nof_chunks_ > kMaxNofChunks) return false;
192
193 size_ = 0;
194 ChunkInfo info; // uninitialized element.
195 for (int i = max_nof_chunks_ - 1; i >= 0; i--) {
196 chunks_.Add(info);
197 free_chunk_ids_.Add(i);
198 }
199 top_ = max_nof_chunks_;
200 return true;
201}
202
203
204void MemoryAllocator::TearDown() {
205 for (int i = 0; i < max_nof_chunks_; i++) {
206 if (chunks_[i].address() != NULL) DeleteChunk(i);
207 }
208 chunks_.Clear();
209 free_chunk_ids_.Clear();
210
211 if (initial_chunk_ != NULL) {
212 LOG(DeleteEvent("InitialChunk", initial_chunk_->address()));
213 delete initial_chunk_;
214 initial_chunk_ = NULL;
215 }
216
217 ASSERT(top_ == max_nof_chunks_); // all chunks are free
218 top_ = 0;
219 capacity_ = 0;
220 size_ = 0;
221 max_nof_chunks_ = 0;
222}
223
224
225void* MemoryAllocator::AllocateRawMemory(const size_t requested,
kasper.lund7276f142008-07-30 08:49:36 +0000226 size_t* allocated,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000227 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228 if (size_ + static_cast<int>(requested) > capacity_) return NULL;
229
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000230 void* mem = OS::Allocate(requested, allocated, executable == EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 int alloced = *allocated;
232 size_ += alloced;
233 Counters::memory_allocated.Increment(alloced);
234 return mem;
235}
236
237
238void MemoryAllocator::FreeRawMemory(void* mem, size_t length) {
239 OS::Free(mem, length);
240 Counters::memory_allocated.Decrement(length);
241 size_ -= length;
242 ASSERT(size_ >= 0);
243}
244
245
246void* MemoryAllocator::ReserveInitialChunk(const size_t requested) {
247 ASSERT(initial_chunk_ == NULL);
248
249 initial_chunk_ = new VirtualMemory(requested);
250 CHECK(initial_chunk_ != NULL);
251 if (!initial_chunk_->IsReserved()) {
252 delete initial_chunk_;
253 initial_chunk_ = NULL;
254 return NULL;
255 }
256
257 // We are sure that we have mapped a block of requested addresses.
258 ASSERT(initial_chunk_->size() == requested);
259 LOG(NewEvent("InitialChunk", initial_chunk_->address(), requested));
260 size_ += requested;
261 return initial_chunk_->address();
262}
263
264
265static int PagesInChunk(Address start, size_t size) {
266 // The first page starts on the first page-aligned address from start onward
267 // and the last page ends on the last page-aligned address before
268 // start+size. Page::kPageSize is a power of two so we can divide by
269 // shifting.
270 return (RoundDown(start + size, Page::kPageSize)
271 - RoundUp(start, Page::kPageSize)) >> Page::kPageSizeBits;
272}
273
274
275Page* MemoryAllocator::AllocatePages(int requested_pages, int* allocated_pages,
276 PagedSpace* owner) {
277 if (requested_pages <= 0) return Page::FromAddress(NULL);
278 size_t chunk_size = requested_pages * Page::kPageSize;
279
280 // There is not enough space to guarantee the desired number pages can be
281 // allocated.
282 if (size_ + static_cast<int>(chunk_size) > capacity_) {
283 // Request as many pages as we can.
284 chunk_size = capacity_ - size_;
285 requested_pages = chunk_size >> Page::kPageSizeBits;
286
287 if (requested_pages <= 0) return Page::FromAddress(NULL);
288 }
kasper.lund7276f142008-07-30 08:49:36 +0000289 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 if (chunk == NULL) return Page::FromAddress(NULL);
291 LOG(NewEvent("PagedChunk", chunk, chunk_size));
292
293 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
294 if (*allocated_pages == 0) {
295 FreeRawMemory(chunk, chunk_size);
296 LOG(DeleteEvent("PagedChunk", chunk));
297 return Page::FromAddress(NULL);
298 }
299
300 int chunk_id = Pop();
301 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
302
303 return InitializePagesInChunk(chunk_id, *allocated_pages, owner);
304}
305
306
307Page* MemoryAllocator::CommitPages(Address start, size_t size,
308 PagedSpace* owner, int* num_pages) {
309 ASSERT(start != NULL);
310 *num_pages = PagesInChunk(start, size);
311 ASSERT(*num_pages > 0);
312 ASSERT(initial_chunk_ != NULL);
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000313 ASSERT(InInitialChunk(start));
314 ASSERT(InInitialChunk(start + size - 1));
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000315 if (!initial_chunk_->Commit(start, size, owner->executable() == EXECUTABLE)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 return Page::FromAddress(NULL);
317 }
318 Counters::memory_allocated.Increment(size);
319
320 // So long as we correctly overestimated the number of chunks we should not
321 // run out of chunk ids.
322 CHECK(!OutOfChunkIds());
323 int chunk_id = Pop();
324 chunks_[chunk_id].init(start, size, owner);
325 return InitializePagesInChunk(chunk_id, *num_pages, owner);
326}
327
328
kasper.lund7276f142008-07-30 08:49:36 +0000329bool MemoryAllocator::CommitBlock(Address start,
330 size_t size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000331 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 ASSERT(start != NULL);
333 ASSERT(size > 0);
334 ASSERT(initial_chunk_ != NULL);
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000335 ASSERT(InInitialChunk(start));
336 ASSERT(InInitialChunk(start + size - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337
kasper.lund7276f142008-07-30 08:49:36 +0000338 if (!initial_chunk_->Commit(start, size, executable)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 Counters::memory_allocated.Increment(size);
340 return true;
341}
342
343
344Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
345 PagedSpace* owner) {
346 ASSERT(IsValidChunk(chunk_id));
347 ASSERT(pages_in_chunk > 0);
348
349 Address chunk_start = chunks_[chunk_id].address();
350
351 Address low = RoundUp(chunk_start, Page::kPageSize);
352
353#ifdef DEBUG
354 size_t chunk_size = chunks_[chunk_id].size();
355 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
356 ASSERT(pages_in_chunk <=
357 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
358#endif
359
360 Address page_addr = low;
361 for (int i = 0; i < pages_in_chunk; i++) {
362 Page* p = Page::FromAddress(page_addr);
363 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
364 p->is_normal_page = 1;
365 page_addr += Page::kPageSize;
366 }
367
368 // Set the next page of the last page to 0.
369 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
370 last_page->opaque_header = OffsetFrom(0) | chunk_id;
371
372 return Page::FromAddress(low);
373}
374
375
376Page* MemoryAllocator::FreePages(Page* p) {
377 if (!p->is_valid()) return p;
378
379 // Find the first page in the same chunk as 'p'
380 Page* first_page = FindFirstPageInSameChunk(p);
381 Page* page_to_return = Page::FromAddress(NULL);
382
383 if (p != first_page) {
384 // Find the last page in the same chunk as 'prev'.
385 Page* last_page = FindLastPageInSameChunk(p);
386 first_page = GetNextPage(last_page); // first page in next chunk
387
388 // set the next_page of last_page to NULL
389 SetNextPage(last_page, Page::FromAddress(NULL));
390 page_to_return = p; // return 'p' when exiting
391 }
392
393 while (first_page->is_valid()) {
394 int chunk_id = GetChunkId(first_page);
395 ASSERT(IsValidChunk(chunk_id));
396
397 // Find the first page of the next chunk before deleting this chunk.
398 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
399
400 // Free the current chunk.
401 DeleteChunk(chunk_id);
402 }
403
404 return page_to_return;
405}
406
407
408void MemoryAllocator::DeleteChunk(int chunk_id) {
409 ASSERT(IsValidChunk(chunk_id));
410
411 ChunkInfo& c = chunks_[chunk_id];
412
413 // We cannot free a chunk contained in the initial chunk because it was not
414 // allocated with AllocateRawMemory. Instead we uncommit the virtual
415 // memory.
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000416 if (InInitialChunk(c.address())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 // TODO(1240712): VirtualMemory::Uncommit has a return value which
418 // is ignored here.
419 initial_chunk_->Uncommit(c.address(), c.size());
420 Counters::memory_allocated.Decrement(c.size());
421 } else {
422 LOG(DeleteEvent("PagedChunk", c.address()));
423 FreeRawMemory(c.address(), c.size());
424 }
425 c.init(NULL, 0, NULL);
426 Push(chunk_id);
427}
428
429
430Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
431 int chunk_id = GetChunkId(p);
432 ASSERT(IsValidChunk(chunk_id));
433
434 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
435 return Page::FromAddress(low);
436}
437
438
439Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
440 int chunk_id = GetChunkId(p);
441 ASSERT(IsValidChunk(chunk_id));
442
443 Address chunk_start = chunks_[chunk_id].address();
444 size_t chunk_size = chunks_[chunk_id].size();
445
446 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
447 ASSERT(chunk_start <= p->address() && p->address() < high);
448
449 return Page::FromAddress(high - Page::kPageSize);
450}
451
452
453#ifdef DEBUG
454void MemoryAllocator::ReportStatistics() {
455 float pct = static_cast<float>(capacity_ - size_) / capacity_;
456 PrintF(" capacity: %d, used: %d, available: %%%d\n\n",
457 capacity_, size_, static_cast<int>(pct*100));
458}
459#endif
460
461
462// -----------------------------------------------------------------------------
463// PagedSpace implementation
464
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000465PagedSpace::PagedSpace(int max_capacity,
466 AllocationSpace id,
467 Executability executable)
kasper.lund7276f142008-07-30 08:49:36 +0000468 : Space(id, executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
470 * Page::kObjectAreaSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 accounting_stats_.Clear();
472
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 allocation_info_.top = NULL;
474 allocation_info_.limit = NULL;
475
476 mc_forwarding_info_.top = NULL;
477 mc_forwarding_info_.limit = NULL;
478}
479
480
481bool PagedSpace::Setup(Address start, size_t size) {
482 if (HasBeenSetup()) return false;
483
484 int num_pages = 0;
485 // Try to use the virtual memory range passed to us. If it is too small to
486 // contain at least one page, ignore it and allocate instead.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000487 int pages_in_chunk = PagesInChunk(start, size);
488 if (pages_in_chunk > 0) {
489 first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize),
490 Page::kPageSize * pages_in_chunk,
491 this, &num_pages);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 } else {
493 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
494 max_capacity_ / Page::kObjectAreaSize);
495 first_page_ =
496 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
497 if (!first_page_->is_valid()) return false;
498 }
499
500 // We are sure that the first page is valid and that we have at least one
501 // page.
502 ASSERT(first_page_->is_valid());
503 ASSERT(num_pages > 0);
504 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
505 ASSERT(Capacity() <= max_capacity_);
506
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000507 // Sequentially initialize remembered sets in the newly allocated
508 // pages and cache the current last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
510 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000511 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 }
513
514 // Use first_page_ for allocation.
515 SetAllocationInfo(&allocation_info_, first_page_);
516
517 return true;
518}
519
520
521bool PagedSpace::HasBeenSetup() {
522 return (Capacity() > 0);
523}
524
525
526void PagedSpace::TearDown() {
527 first_page_ = MemoryAllocator::FreePages(first_page_);
528 ASSERT(!first_page_->is_valid());
529
530 accounting_stats_.Clear();
531}
532
533
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000534#ifdef ENABLE_HEAP_PROTECTION
535
536void PagedSpace::Protect() {
537 Page* page = first_page_;
538 while (page->is_valid()) {
539 MemoryAllocator::ProtectChunkFromPage(page);
540 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
541 }
542}
543
544
545void PagedSpace::Unprotect() {
546 Page* page = first_page_;
547 while (page->is_valid()) {
548 MemoryAllocator::UnprotectChunkFromPage(page);
549 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
550 }
551}
552
553#endif
554
555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556void PagedSpace::ClearRSet() {
557 PageIterator it(this, PageIterator::ALL_PAGES);
558 while (it.has_next()) {
559 it.next()->ClearRSet();
560 }
561}
562
563
564Object* PagedSpace::FindObject(Address addr) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 // Note: this function can only be called before or after mark-compact GC
566 // because it accesses map pointers.
567 ASSERT(!MarkCompactCollector::in_use());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568
569 if (!Contains(addr)) return Failure::Exception();
570
571 Page* p = Page::FromAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +0000572 ASSERT(IsUsed(p));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 Address cur = p->ObjectAreaStart();
574 Address end = p->AllocationTop();
575 while (cur < end) {
576 HeapObject* obj = HeapObject::FromAddress(cur);
577 Address next = cur + obj->Size();
578 if ((cur <= addr) && (addr < next)) return obj;
579 cur = next;
580 }
581
kasper.lund7276f142008-07-30 08:49:36 +0000582 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 return Failure::Exception();
584}
585
586
kasper.lund7276f142008-07-30 08:49:36 +0000587bool PagedSpace::IsUsed(Page* page) {
588 PageIterator it(this, PageIterator::PAGES_IN_USE);
589 while (it.has_next()) {
590 if (page == it.next()) return true;
591 }
592 return false;
593}
594
595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
597 alloc_info->top = p->ObjectAreaStart();
598 alloc_info->limit = p->ObjectAreaEnd();
kasper.lund7276f142008-07-30 08:49:36 +0000599 ASSERT(alloc_info->VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600}
601
602
603void PagedSpace::MCResetRelocationInfo() {
604 // Set page indexes.
605 int i = 0;
606 PageIterator it(this, PageIterator::ALL_PAGES);
607 while (it.has_next()) {
608 Page* p = it.next();
609 p->mc_page_index = i++;
610 }
611
612 // Set mc_forwarding_info_ to the first page in the space.
613 SetAllocationInfo(&mc_forwarding_info_, first_page_);
614 // All the bytes in the space are 'available'. We will rediscover
615 // allocated and wasted bytes during GC.
616 accounting_stats_.Reset();
617}
618
619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
621#ifdef DEBUG
622 // The Contains function considers the address at the beginning of a
623 // page in the page, MCSpaceOffsetForAddress considers it is in the
624 // previous page.
625 if (Page::IsAlignedToPageSize(addr)) {
626 ASSERT(Contains(addr - kPointerSize));
627 } else {
628 ASSERT(Contains(addr));
629 }
630#endif
631
632 // If addr is at the end of a page, it belongs to previous page
633 Page* p = Page::IsAlignedToPageSize(addr)
634 ? Page::FromAllocationTop(addr)
635 : Page::FromAddress(addr);
636 int index = p->mc_page_index;
637 return (index * Page::kPageSize) + p->Offset(addr);
638}
639
640
kasper.lund7276f142008-07-30 08:49:36 +0000641// Slow case for reallocating and promoting objects during a compacting
642// collection. This function is not space-specific.
643HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
644 Page* current_page = TopPageOf(mc_forwarding_info_);
645 if (!current_page->next_page()->is_valid()) {
646 if (!Expand(current_page)) {
647 return NULL;
648 }
649 }
650
651 // There are surely more pages in the space now.
652 ASSERT(current_page->next_page()->is_valid());
653 // We do not add the top of page block for current page to the space's
654 // free list---the block may contain live objects so we cannot write
655 // bookkeeping information to it. Instead, we will recover top of page
656 // blocks when we move objects to their new locations.
657 //
658 // We do however write the allocation pointer to the page. The encoding
659 // of forwarding addresses is as an offset in terms of live bytes, so we
660 // need quick access to the allocation top of each page to decode
661 // forwarding addresses.
662 current_page->mc_relocation_top = mc_forwarding_info_.top;
663 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
664 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
665}
666
667
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668bool PagedSpace::Expand(Page* last_page) {
669 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
670 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
671
672 if (Capacity() == max_capacity_) return false;
673
674 ASSERT(Capacity() < max_capacity_);
675 // Last page must be valid and its next page is invalid.
676 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
677
678 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
679 if (available_pages <= 0) return false;
680
681 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
682 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
683 if (!p->is_valid()) return false;
684
685 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
686 ASSERT(Capacity() <= max_capacity_);
687
688 MemoryAllocator::SetNextPage(last_page, p);
689
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000690 // Sequentially clear remembered set of new pages and and cache the
691 // new last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 while (p->is_valid()) {
693 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000694 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 p = p->next_page();
696 }
697
698 return true;
699}
700
701
702#ifdef DEBUG
703int PagedSpace::CountTotalPages() {
704 int count = 0;
705 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
706 count++;
707 }
708 return count;
709}
710#endif
711
712
713void PagedSpace::Shrink() {
714 // Release half of free pages.
715 Page* top_page = AllocationTopPage();
716 ASSERT(top_page->is_valid());
717
718 // Loop over the pages from the top page to the end of the space to count
719 // the number of pages to keep and find the last page to keep.
720 int free_pages = 0;
721 int pages_to_keep = 0; // Of the free pages.
722 Page* last_page_to_keep = top_page;
723 Page* current_page = top_page->next_page();
724 // Loop over the pages to the end of the space.
725 while (current_page->is_valid()) {
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000726#if defined(ANDROID)
727 // Free all chunks if possible
728#else
kasper.lund7276f142008-07-30 08:49:36 +0000729 // Advance last_page_to_keep every other step to end up at the midpoint.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 if ((free_pages & 0x1) == 1) {
731 pages_to_keep++;
732 last_page_to_keep = last_page_to_keep->next_page();
733 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000734#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 free_pages++;
736 current_page = current_page->next_page();
737 }
738
739 // Free pages after last_page_to_keep, and adjust the next_page link.
740 Page* p = MemoryAllocator::FreePages(last_page_to_keep->next_page());
741 MemoryAllocator::SetNextPage(last_page_to_keep, p);
742
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000743 // Since pages are only freed in whole chunks, we may have kept more
744 // than pages_to_keep. Count the extra pages and cache the new last
745 // page in the space.
746 last_page_ = last_page_to_keep;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 while (p->is_valid()) {
748 pages_to_keep++;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000749 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 p = p->next_page();
751 }
752
753 // The difference between free_pages and pages_to_keep is the number of
754 // pages actually freed.
755 ASSERT(pages_to_keep <= free_pages);
756 int bytes_freed = (free_pages - pages_to_keep) * Page::kObjectAreaSize;
757 accounting_stats_.ShrinkSpace(bytes_freed);
758
759 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
760}
761
762
763bool PagedSpace::EnsureCapacity(int capacity) {
764 if (Capacity() >= capacity) return true;
765
766 // Start from the allocation top and loop to the last page in the space.
767 Page* last_page = AllocationTopPage();
768 Page* next_page = last_page->next_page();
769 while (next_page->is_valid()) {
770 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
771 next_page = last_page->next_page();
772 }
773
774 // Expand the space until it has the required capacity or expansion fails.
775 do {
776 if (!Expand(last_page)) return false;
777 ASSERT(last_page->next_page()->is_valid());
778 last_page =
779 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
780 } while (Capacity() < capacity);
781
782 return true;
783}
784
785
786#ifdef DEBUG
787void PagedSpace::Print() { }
788#endif
789
790
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000791#ifdef DEBUG
792// We do not assume that the PageIterator works, because it depends on the
793// invariants we are checking during verification.
794void PagedSpace::Verify(ObjectVisitor* visitor) {
795 // The allocation pointer should be valid, and it should be in a page in the
796 // space.
797 ASSERT(allocation_info_.VerifyPagedAllocation());
798 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
799 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
800
801 // Loop over all the pages.
802 bool above_allocation_top = false;
803 Page* current_page = first_page_;
804 while (current_page->is_valid()) {
805 if (above_allocation_top) {
806 // We don't care what's above the allocation top.
807 } else {
808 // Unless this is the last page in the space containing allocated
809 // objects, the allocation top should be at a constant offset from the
810 // object area end.
811 Address top = current_page->AllocationTop();
812 if (current_page == top_page) {
813 ASSERT(top == allocation_info_.top);
814 // The next page will be above the allocation top.
815 above_allocation_top = true;
816 } else {
817 ASSERT(top == current_page->ObjectAreaEnd() - page_extra_);
818 }
819
820 // It should be packed with objects from the bottom to the top.
821 Address current = current_page->ObjectAreaStart();
822 while (current < top) {
823 HeapObject* object = HeapObject::FromAddress(current);
824
825 // The first word should be a map, and we expect all map pointers to
826 // be in map space.
827 Map* map = object->map();
828 ASSERT(map->IsMap());
829 ASSERT(Heap::map_space()->Contains(map));
830
831 // Perform space-specific object verification.
832 VerifyObject(object);
833
834 // The object itself should look OK.
835 object->Verify();
836
837 // All the interior pointers should be contained in the heap and
838 // have their remembered set bits set if required as determined
839 // by the visitor.
840 int size = object->Size();
841 if (object->IsCode()) {
842 Code::cast(object)->ConvertICTargetsFromAddressToObject();
843 object->IterateBody(map->instance_type(), size, visitor);
844 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
845 } else {
846 object->IterateBody(map->instance_type(), size, visitor);
847 }
848
849 current += size;
850 }
851
852 // The allocation pointer should not be in the middle of an object.
853 ASSERT(current == top);
854 }
855
856 current_page = current_page->next_page();
857 }
858}
859#endif
860
861
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862// -----------------------------------------------------------------------------
863// NewSpace implementation
864
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000865
866bool NewSpace::Setup(Address start, int size) {
867 // Setup new space based on the preallocated memory block defined by
868 // start and size. The provided space is divided into two semi-spaces.
869 // To support fast containment testing in the new space, the size of
870 // this chunk must be a power of two and it must be aligned to its size.
871 int initial_semispace_capacity = Heap::InitialSemiSpaceSize();
872 int maximum_semispace_capacity = Heap::SemiSpaceSize();
873
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
875 ASSERT(IsPowerOf2(maximum_semispace_capacity));
876 maximum_capacity_ = maximum_semispace_capacity;
877 capacity_ = initial_semispace_capacity;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
879 // Allocate and setup the histogram arrays if necessary.
880#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
881 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
882 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
883
884#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
885 promoted_histogram_[name].set_name(#name);
886 INSTANCE_TYPE_LIST(SET_NAME)
887#undef SET_NAME
888#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 ASSERT(size == 2 * maximum_capacity_);
891 ASSERT(IsAddressAligned(start, size, 0));
892
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000893 if (!to_space_.Setup(start, capacity_, maximum_capacity_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 return false;
895 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000896 if (!from_space_.Setup(start + maximum_capacity_,
897 capacity_,
898 maximum_capacity_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 return false;
900 }
901
902 start_ = start;
903 address_mask_ = ~(size - 1);
904 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +0000905 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000907 allocation_info_.top = to_space_.low();
908 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 mc_forwarding_info_.top = NULL;
910 mc_forwarding_info_.limit = NULL;
911
912 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
913 return true;
914}
915
916
917void NewSpace::TearDown() {
918#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
919 if (allocated_histogram_) {
920 DeleteArray(allocated_histogram_);
921 allocated_histogram_ = NULL;
922 }
923 if (promoted_histogram_) {
924 DeleteArray(promoted_histogram_);
925 promoted_histogram_ = NULL;
926 }
927#endif
928
929 start_ = NULL;
930 capacity_ = 0;
931 allocation_info_.top = NULL;
932 allocation_info_.limit = NULL;
933 mc_forwarding_info_.top = NULL;
934 mc_forwarding_info_.limit = NULL;
935
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000936 to_space_.TearDown();
937 from_space_.TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938}
939
940
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000941#ifdef ENABLE_HEAP_PROTECTION
942
943void NewSpace::Protect() {
944 MemoryAllocator::Protect(ToSpaceLow(), Capacity());
945 MemoryAllocator::Protect(FromSpaceLow(), Capacity());
946}
947
948
949void NewSpace::Unprotect() {
950 MemoryAllocator::Unprotect(ToSpaceLow(), Capacity(),
951 to_space_.executable());
952 MemoryAllocator::Unprotect(FromSpaceLow(), Capacity(),
953 from_space_.executable());
954}
955
956#endif
957
958
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959void NewSpace::Flip() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000960 SemiSpace tmp = from_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 from_space_ = to_space_;
962 to_space_ = tmp;
963}
964
965
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000966bool NewSpace::Grow() {
967 ASSERT(capacity_ < maximum_capacity_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 // TODO(1240712): Failure to double the from space can result in
969 // semispaces of different sizes. In the event of that failure, the
970 // to space doubling should be rolled back before returning false.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000971 if (!to_space_.Grow() || !from_space_.Grow()) return false;
972 capacity_ = to_space_.Capacity() + from_space_.Capacity();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000973 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
975 return true;
976}
977
978
979void NewSpace::ResetAllocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000980 allocation_info_.top = to_space_.low();
981 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
983}
984
985
986void NewSpace::MCResetRelocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000987 mc_forwarding_info_.top = from_space_.low();
988 mc_forwarding_info_.limit = from_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
990}
991
992
993void NewSpace::MCCommitRelocationInfo() {
994 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
995 // valid allocation info for the to space.
996 allocation_info_.top = mc_forwarding_info_.top;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000997 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
999}
1000
1001
1002#ifdef DEBUG
1003// We do not use the SemispaceIterator because verification doesn't assume
1004// that it works (it depends on the invariants we are checking).
1005void NewSpace::Verify() {
1006 // The allocation pointer should be in the space or at the very end.
1007 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1008
1009 // There should be objects packed in from the low address up to the
1010 // allocation pointer.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001011 Address current = to_space_.low();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012 while (current < top()) {
1013 HeapObject* object = HeapObject::FromAddress(current);
1014
1015 // The first word should be a map, and we expect all map pointers to
1016 // be in map space.
1017 Map* map = object->map();
1018 ASSERT(map->IsMap());
1019 ASSERT(Heap::map_space()->Contains(map));
1020
1021 // The object should not be code or a map.
1022 ASSERT(!object->IsMap());
1023 ASSERT(!object->IsCode());
1024
1025 // The object itself should look OK.
1026 object->Verify();
1027
1028 // All the interior pointers should be contained in the heap.
1029 VerifyPointersVisitor visitor;
1030 int size = object->Size();
1031 object->IterateBody(map->instance_type(), size, &visitor);
1032
1033 current += size;
1034 }
1035
1036 // The allocation pointer should not be in the middle of an object.
1037 ASSERT(current == top());
1038}
1039#endif
1040
1041
1042// -----------------------------------------------------------------------------
1043// SemiSpace implementation
1044
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001045bool SemiSpace::Setup(Address start,
1046 int initial_capacity,
1047 int maximum_capacity) {
1048 // Creates a space in the young generation. The constructor does not
1049 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1050 // memory of size 'capacity' when set up, and does not grow or shrink
1051 // otherwise. In the mark-compact collector, the memory region of the from
1052 // space is used as the marking stack. It requires contiguous memory
1053 // addresses.
1054 capacity_ = initial_capacity;
1055 maximum_capacity_ = maximum_capacity;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056
kasper.lund7276f142008-07-30 08:49:36 +00001057 if (!MemoryAllocator::CommitBlock(start, capacity_, executable())) {
1058 return false;
1059 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060
1061 start_ = start;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001062 address_mask_ = ~(maximum_capacity - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +00001064 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065
1066 age_mark_ = start_;
1067 return true;
1068}
1069
1070
1071void SemiSpace::TearDown() {
1072 start_ = NULL;
1073 capacity_ = 0;
1074}
1075
1076
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001077bool SemiSpace::Grow() {
1078 // Commit 50% extra space but only up to maximum capacity.
1079 int extra = capacity_/2;
1080 if (capacity_ + extra > maximum_capacity_) {
1081 extra = maximum_capacity_ - capacity_;
1082 }
1083 if (!MemoryAllocator::CommitBlock(high(), extra, executable())) {
kasper.lund7276f142008-07-30 08:49:36 +00001084 return false;
1085 }
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001086 capacity_ += extra;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 return true;
1088}
1089
1090
1091#ifdef DEBUG
1092void SemiSpace::Print() { }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001093
1094
1095void SemiSpace::Verify() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096#endif
1097
1098
1099// -----------------------------------------------------------------------------
1100// SemiSpaceIterator implementation.
1101SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
1102 Initialize(space, space->bottom(), space->top(), NULL);
1103}
1104
1105
1106SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1107 HeapObjectCallback size_func) {
1108 Initialize(space, space->bottom(), space->top(), size_func);
1109}
1110
1111
1112SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1113 Initialize(space, start, space->top(), NULL);
1114}
1115
1116
1117void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1118 Address end,
1119 HeapObjectCallback size_func) {
1120 ASSERT(space->ToSpaceContains(start));
1121 ASSERT(space->ToSpaceLow() <= end
1122 && end <= space->ToSpaceHigh());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001123 space_ = &space->to_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 current_ = start;
1125 limit_ = end;
1126 size_func_ = size_func;
1127}
1128
1129
1130#ifdef DEBUG
1131// A static array of histogram info for each type.
1132static HistogramInfo heap_histograms[LAST_TYPE+1];
1133static JSObject::SpillInformation js_spill_information;
1134
1135// heap_histograms is shared, always clear it before using it.
1136static void ClearHistograms() {
1137 // We reset the name each time, though it hasn't changed.
1138#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1139 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1140#undef DEF_TYPE_NAME
1141
1142#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1143 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1144#undef CLEAR_HISTOGRAM
1145
1146 js_spill_information.Clear();
1147}
1148
1149
1150static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1151
1152
1153static void ClearCodeKindStatistics() {
1154 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1155 code_kind_statistics[i] = 0;
1156 }
1157}
1158
1159
1160static void ReportCodeKindStatistics() {
1161 const char* table[Code::NUMBER_OF_KINDS];
1162
1163#define CASE(name) \
1164 case Code::name: table[Code::name] = #name; \
1165 break
1166
1167 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1168 switch (static_cast<Code::Kind>(i)) {
1169 CASE(FUNCTION);
1170 CASE(STUB);
1171 CASE(BUILTIN);
1172 CASE(LOAD_IC);
1173 CASE(KEYED_LOAD_IC);
1174 CASE(STORE_IC);
1175 CASE(KEYED_STORE_IC);
1176 CASE(CALL_IC);
1177 }
1178 }
1179
1180#undef CASE
1181
1182 PrintF("\n Code kind histograms: \n");
1183 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1184 if (code_kind_statistics[i] > 0) {
1185 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1186 }
1187 }
1188 PrintF("\n");
1189}
1190
1191
1192static int CollectHistogramInfo(HeapObject* obj) {
1193 InstanceType type = obj->map()->instance_type();
1194 ASSERT(0 <= type && type <= LAST_TYPE);
1195 ASSERT(heap_histograms[type].name() != NULL);
1196 heap_histograms[type].increment_number(1);
1197 heap_histograms[type].increment_bytes(obj->Size());
1198
1199 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1200 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1201 }
1202
1203 return obj->Size();
1204}
1205
1206
1207static void ReportHistogram(bool print_spill) {
1208 PrintF("\n Object Histogram:\n");
1209 for (int i = 0; i <= LAST_TYPE; i++) {
1210 if (heap_histograms[i].number() > 0) {
1211 PrintF(" %-33s%10d (%10d bytes)\n",
1212 heap_histograms[i].name(),
1213 heap_histograms[i].number(),
1214 heap_histograms[i].bytes());
1215 }
1216 }
1217 PrintF("\n");
1218
1219 // Summarize string types.
1220 int string_number = 0;
1221 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001222#define INCREMENT(type, size, name, camel_name) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 string_number += heap_histograms[type].number(); \
1224 string_bytes += heap_histograms[type].bytes();
1225 STRING_TYPE_LIST(INCREMENT)
1226#undef INCREMENT
1227 if (string_number > 0) {
1228 PrintF(" %-33s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
1229 string_bytes);
1230 }
1231
1232 if (FLAG_collect_heap_spill_statistics && print_spill) {
1233 js_spill_information.Print();
1234 }
1235}
1236#endif // DEBUG
1237
1238
1239// Support for statistics gathering for --heap-stats and --log-gc.
1240#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1241void NewSpace::ClearHistograms() {
1242 for (int i = 0; i <= LAST_TYPE; i++) {
1243 allocated_histogram_[i].clear();
1244 promoted_histogram_[i].clear();
1245 }
1246}
1247
1248// Because the copying collector does not touch garbage objects, we iterate
1249// the new space before a collection to get a histogram of allocated objects.
1250// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1251// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1252// flag is set.
1253void NewSpace::CollectStatistics() {
1254 ClearHistograms();
1255 SemiSpaceIterator it(this);
1256 while (it.has_next()) RecordAllocation(it.next());
1257}
1258
1259
1260#ifdef ENABLE_LOGGING_AND_PROFILING
1261static void DoReportStatistics(HistogramInfo* info, const char* description) {
1262 LOG(HeapSampleBeginEvent("NewSpace", description));
1263 // Lump all the string types together.
1264 int string_number = 0;
1265 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001266#define INCREMENT(type, size, name, camel_name) \
1267 string_number += info[type].number(); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 string_bytes += info[type].bytes();
1269 STRING_TYPE_LIST(INCREMENT)
1270#undef INCREMENT
1271 if (string_number > 0) {
1272 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1273 }
1274
1275 // Then do the other types.
1276 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1277 if (info[i].number() > 0) {
1278 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1279 info[i].bytes()));
1280 }
1281 }
1282 LOG(HeapSampleEndEvent("NewSpace", description));
1283}
1284#endif // ENABLE_LOGGING_AND_PROFILING
1285
1286
1287void NewSpace::ReportStatistics() {
1288#ifdef DEBUG
1289 if (FLAG_heap_stats) {
1290 float pct = static_cast<float>(Available()) / Capacity();
1291 PrintF(" capacity: %d, available: %d, %%%d\n",
1292 Capacity(), Available(), static_cast<int>(pct*100));
1293 PrintF("\n Object Histogram:\n");
1294 for (int i = 0; i <= LAST_TYPE; i++) {
1295 if (allocated_histogram_[i].number() > 0) {
1296 PrintF(" %-33s%10d (%10d bytes)\n",
1297 allocated_histogram_[i].name(),
1298 allocated_histogram_[i].number(),
1299 allocated_histogram_[i].bytes());
1300 }
1301 }
1302 PrintF("\n");
1303 }
1304#endif // DEBUG
1305
1306#ifdef ENABLE_LOGGING_AND_PROFILING
1307 if (FLAG_log_gc) {
1308 DoReportStatistics(allocated_histogram_, "allocated");
1309 DoReportStatistics(promoted_histogram_, "promoted");
1310 }
1311#endif // ENABLE_LOGGING_AND_PROFILING
1312}
1313
1314
1315void NewSpace::RecordAllocation(HeapObject* obj) {
1316 InstanceType type = obj->map()->instance_type();
1317 ASSERT(0 <= type && type <= LAST_TYPE);
1318 allocated_histogram_[type].increment_number(1);
1319 allocated_histogram_[type].increment_bytes(obj->Size());
1320}
1321
1322
1323void NewSpace::RecordPromotion(HeapObject* obj) {
1324 InstanceType type = obj->map()->instance_type();
1325 ASSERT(0 <= type && type <= LAST_TYPE);
1326 promoted_histogram_[type].increment_number(1);
1327 promoted_histogram_[type].increment_bytes(obj->Size());
1328}
1329#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1330
1331
1332// -----------------------------------------------------------------------------
1333// Free lists for old object spaces implementation
1334
1335void FreeListNode::set_size(int size_in_bytes) {
1336 ASSERT(size_in_bytes > 0);
1337 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1338
1339 // We write a map and possibly size information to the block. If the block
1340 // is big enough to be a ByteArray with at least one extra word (the next
1341 // pointer), we set its map to be the byte array map and its size to an
1342 // appropriate array length for the desired size from HeapObject::Size().
1343 // If the block is too small (eg, one or two words), to hold both a size
1344 // field and a next pointer, we give it a filler map that gives it the
1345 // correct size.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001346 if (size_in_bytes > ByteArray::kAlignedSize) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001347 set_map(Heap::raw_unchecked_byte_array_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 ByteArray::cast(this)->set_length(ByteArray::LengthFor(size_in_bytes));
1349 } else if (size_in_bytes == kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001350 set_map(Heap::raw_unchecked_one_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 } else if (size_in_bytes == 2 * kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001352 set_map(Heap::raw_unchecked_two_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 } else {
1354 UNREACHABLE();
1355 }
kasper.lund7276f142008-07-30 08:49:36 +00001356 ASSERT(Size() == size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357}
1358
1359
1360Address FreeListNode::next() {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001361 ASSERT(map() == Heap::raw_unchecked_byte_array_map() ||
1362 map() == Heap::raw_unchecked_two_pointer_filler_map());
1363 if (map() == Heap::raw_unchecked_byte_array_map()) {
1364 ASSERT(Size() >= kNextOffset + kPointerSize);
1365 return Memory::Address_at(address() + kNextOffset);
1366 } else {
1367 return Memory::Address_at(address() + kPointerSize);
1368 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369}
1370
1371
1372void FreeListNode::set_next(Address next) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001373 ASSERT(map() == Heap::raw_unchecked_byte_array_map() ||
1374 map() == Heap::raw_unchecked_two_pointer_filler_map());
1375 if (map() == Heap::raw_unchecked_byte_array_map()) {
1376 ASSERT(Size() >= kNextOffset + kPointerSize);
1377 Memory::Address_at(address() + kNextOffset) = next;
1378 } else {
1379 Memory::Address_at(address() + kPointerSize) = next;
1380 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381}
1382
1383
1384OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1385 Reset();
1386}
1387
1388
1389void OldSpaceFreeList::Reset() {
1390 available_ = 0;
1391 for (int i = 0; i < kFreeListsLength; i++) {
1392 free_[i].head_node_ = NULL;
1393 }
1394 needs_rebuild_ = false;
1395 finger_ = kHead;
1396 free_[kHead].next_size_ = kEnd;
1397}
1398
1399
1400void OldSpaceFreeList::RebuildSizeList() {
1401 ASSERT(needs_rebuild_);
1402 int cur = kHead;
1403 for (int i = cur + 1; i < kFreeListsLength; i++) {
1404 if (free_[i].head_node_ != NULL) {
1405 free_[cur].next_size_ = i;
1406 cur = i;
1407 }
1408 }
1409 free_[cur].next_size_ = kEnd;
1410 needs_rebuild_ = false;
1411}
1412
1413
1414int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1415#ifdef DEBUG
1416 for (int i = 0; i < size_in_bytes; i += kPointerSize) {
1417 Memory::Address_at(start + i) = kZapValue;
1418 }
1419#endif
1420 FreeListNode* node = FreeListNode::FromAddress(start);
1421 node->set_size(size_in_bytes);
1422
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001423 // We don't use the freelists in compacting mode. This makes it more like a
1424 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1425 // collector.
1426 if (FLAG_always_compact) {
1427 return size_in_bytes;
1428 }
1429
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 // Early return to drop too-small blocks on the floor (one or two word
1431 // blocks cannot hold a map pointer, a size field, and a pointer to the
1432 // next block in the free list).
1433 if (size_in_bytes < kMinBlockSize) {
1434 return size_in_bytes;
1435 }
1436
1437 // Insert other blocks at the head of an exact free list.
1438 int index = size_in_bytes >> kPointerSizeLog2;
1439 node->set_next(free_[index].head_node_);
1440 free_[index].head_node_ = node->address();
1441 available_ += size_in_bytes;
1442 needs_rebuild_ = true;
1443 return 0;
1444}
1445
1446
1447Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1448 ASSERT(0 < size_in_bytes);
1449 ASSERT(size_in_bytes <= kMaxBlockSize);
1450 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1451
1452 if (needs_rebuild_) RebuildSizeList();
1453 int index = size_in_bytes >> kPointerSizeLog2;
1454 // Check for a perfect fit.
1455 if (free_[index].head_node_ != NULL) {
1456 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1457 // If this was the last block of its size, remove the size.
1458 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1459 available_ -= size_in_bytes;
1460 *wasted_bytes = 0;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001461 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 return node;
1463 }
1464 // Search the size list for the best fit.
1465 int prev = finger_ < index ? finger_ : kHead;
1466 int cur = FindSize(index, &prev);
1467 ASSERT(index < cur);
1468 if (cur == kEnd) {
1469 // No large enough size in list.
1470 *wasted_bytes = 0;
1471 return Failure::RetryAfterGC(size_in_bytes, owner_);
1472 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001473 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 int rem = cur - index;
1475 int rem_bytes = rem << kPointerSizeLog2;
1476 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
kasper.lund7276f142008-07-30 08:49:36 +00001477 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1479 size_in_bytes);
1480 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1481 // to avoid many redundant tests and calls to Insert/RemoveSize.
1482 if (prev < rem) {
1483 // Simple case: insert rem between prev and cur.
1484 finger_ = prev;
1485 free_[prev].next_size_ = rem;
1486 // If this was the last block of size cur, remove the size.
1487 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1488 free_[rem].next_size_ = free_[cur].next_size_;
1489 } else {
1490 free_[rem].next_size_ = cur;
1491 }
1492 // Add the remainder block.
1493 rem_node->set_size(rem_bytes);
1494 rem_node->set_next(free_[rem].head_node_);
1495 free_[rem].head_node_ = rem_node->address();
1496 } else {
1497 // If this was the last block of size cur, remove the size.
1498 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1499 finger_ = prev;
1500 free_[prev].next_size_ = free_[cur].next_size_;
1501 }
1502 if (rem_bytes < kMinBlockSize) {
1503 // Too-small remainder is wasted.
1504 rem_node->set_size(rem_bytes);
1505 available_ -= size_in_bytes + rem_bytes;
1506 *wasted_bytes = rem_bytes;
1507 return cur_node;
1508 }
1509 // Add the remainder block and, if needed, insert its size.
1510 rem_node->set_size(rem_bytes);
1511 rem_node->set_next(free_[rem].head_node_);
1512 free_[rem].head_node_ = rem_node->address();
1513 if (rem_node->next() == NULL) InsertSize(rem);
1514 }
1515 available_ -= size_in_bytes;
1516 *wasted_bytes = 0;
1517 return cur_node;
1518}
1519
1520
kasper.lund7276f142008-07-30 08:49:36 +00001521#ifdef DEBUG
1522bool OldSpaceFreeList::Contains(FreeListNode* node) {
1523 for (int i = 0; i < kFreeListsLength; i++) {
1524 Address cur_addr = free_[i].head_node_;
1525 while (cur_addr != NULL) {
1526 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1527 if (cur_node == node) return true;
1528 cur_addr = cur_node->next();
1529 }
1530 }
1531 return false;
1532}
1533#endif
1534
1535
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001536FixedSizeFreeList::FixedSizeFreeList(AllocationSpace owner, int object_size)
1537 : owner_(owner), object_size_(object_size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 Reset();
1539}
1540
1541
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001542void FixedSizeFreeList::Reset() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 available_ = 0;
1544 head_ = NULL;
1545}
1546
1547
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001548void FixedSizeFreeList::Free(Address start) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001550 for (int i = 0; i < object_size_; i += kPointerSize) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551 Memory::Address_at(start + i) = kZapValue;
1552 }
1553#endif
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001554 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555 FreeListNode* node = FreeListNode::FromAddress(start);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001556 node->set_size(object_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557 node->set_next(head_);
1558 head_ = node->address();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001559 available_ += object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560}
1561
1562
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001563Object* FixedSizeFreeList::Allocate() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 if (head_ == NULL) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001565 return Failure::RetryAfterGC(object_size_, owner_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 }
1567
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001568 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569 FreeListNode* node = FreeListNode::FromAddress(head_);
1570 head_ = node->next();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001571 available_ -= object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 return node;
1573}
1574
1575
1576// -----------------------------------------------------------------------------
1577// OldSpace implementation
1578
1579void OldSpace::PrepareForMarkCompact(bool will_compact) {
1580 if (will_compact) {
1581 // Reset relocation info. During a compacting collection, everything in
1582 // the space is considered 'available' and we will rediscover live data
1583 // and waste during the collection.
1584 MCResetRelocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 ASSERT(Available() == Capacity());
1586 } else {
1587 // During a non-compacting collection, everything below the linear
1588 // allocation pointer is considered allocated (everything above is
1589 // available) and we will rediscover available and wasted bytes during
1590 // the collection.
1591 accounting_stats_.AllocateBytes(free_list_.available());
1592 accounting_stats_.FillWastedBytes(Waste());
1593 }
1594
kasper.lund7276f142008-07-30 08:49:36 +00001595 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597}
1598
1599
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600void OldSpace::MCCommitRelocationInfo() {
1601 // Update fast allocation info.
1602 allocation_info_.top = mc_forwarding_info_.top;
1603 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001604 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605
1606 // The space is compacted and we haven't yet built free lists or
1607 // wasted any space.
1608 ASSERT(Waste() == 0);
1609 ASSERT(AvailableFree() == 0);
1610
1611 // Build the free list for the space.
1612 int computed_size = 0;
1613 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1614 while (it.has_next()) {
1615 Page* p = it.next();
1616 // Space below the relocation pointer is allocated.
1617 computed_size += p->mc_relocation_top - p->ObjectAreaStart();
1618 if (it.has_next()) {
1619 // Free the space at the top of the page. We cannot use
1620 // p->mc_relocation_top after the call to Free (because Free will clear
1621 // remembered set bits).
1622 int extra_size = p->ObjectAreaEnd() - p->mc_relocation_top;
1623 if (extra_size > 0) {
1624 int wasted_bytes = free_list_.Free(p->mc_relocation_top, extra_size);
1625 // The bytes we have just "freed" to add to the free list were
1626 // already accounted as available.
1627 accounting_stats_.WasteBytes(wasted_bytes);
1628 }
1629 }
1630 }
1631
1632 // Make sure the computed size - based on the used portion of the pages in
1633 // use - matches the size obtained while computing forwarding addresses.
1634 ASSERT(computed_size == Size());
1635}
1636
1637
kasper.lund7276f142008-07-30 08:49:36 +00001638// Slow case for normal allocation. Try in order: (1) allocate in the next
1639// page in the space, (2) allocate off the space's free list, (3) expand the
1640// space, (4) fail.
1641HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
1642 // Linear allocation in this space has failed. If there is another page
1643 // in the space, move to that page and allocate there. This allocation
1644 // should succeed (size_in_bytes should not be greater than a page's
1645 // object area size).
1646 Page* current_page = TopPageOf(allocation_info_);
1647 if (current_page->next_page()->is_valid()) {
1648 return AllocateInNextPage(current_page, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 }
kasper.lund7276f142008-07-30 08:49:36 +00001650
1651 // There is no next page in this space. Try free list allocation.
1652 int wasted_bytes;
1653 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
1654 accounting_stats_.WasteBytes(wasted_bytes);
1655 if (!result->IsFailure()) {
1656 accounting_stats_.AllocateBytes(size_in_bytes);
1657 return HeapObject::cast(result);
1658 }
1659
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001660 // Free list allocation failed and there is no next page. Fail if we have
1661 // hit the old generation size limit that should cause a garbage
1662 // collection.
1663 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
1664 return NULL;
1665 }
1666
1667 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00001668 ASSERT(!current_page->next_page()->is_valid());
1669 if (Expand(current_page)) {
1670 return AllocateInNextPage(current_page, size_in_bytes);
1671 }
1672
1673 // Finally, fail.
1674 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675}
1676
1677
kasper.lund7276f142008-07-30 08:49:36 +00001678// Add the block at the top of the page to the space's free list, set the
1679// allocation info to the next page (assumed to be one), and allocate
1680// linearly there.
1681HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
1682 int size_in_bytes) {
1683 ASSERT(current_page->next_page()->is_valid());
1684 // Add the block at the top of this page to the free list.
1685 int free_size = current_page->ObjectAreaEnd() - allocation_info_.top;
1686 if (free_size > 0) {
1687 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
1688 accounting_stats_.WasteBytes(wasted_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689 }
kasper.lund7276f142008-07-30 08:49:36 +00001690 SetAllocationInfo(&allocation_info_, current_page->next_page());
1691 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692}
1693
1694
1695#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696struct CommentStatistic {
1697 const char* comment;
1698 int size;
1699 int count;
1700 void Clear() {
1701 comment = NULL;
1702 size = 0;
1703 count = 0;
1704 }
1705};
1706
1707
1708// must be small, since an iteration is used for lookup
1709const int kMaxComments = 64;
1710static CommentStatistic comments_statistics[kMaxComments+1];
1711
1712
1713void PagedSpace::ReportCodeStatistics() {
1714 ReportCodeKindStatistics();
1715 PrintF("Code comment statistics (\" [ comment-txt : size/ "
1716 "count (average)\"):\n");
1717 for (int i = 0; i <= kMaxComments; i++) {
1718 const CommentStatistic& cs = comments_statistics[i];
1719 if (cs.size > 0) {
1720 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
1721 cs.size/cs.count);
1722 }
1723 }
1724 PrintF("\n");
1725}
1726
1727
1728void PagedSpace::ResetCodeStatistics() {
1729 ClearCodeKindStatistics();
1730 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
1731 comments_statistics[kMaxComments].comment = "Unknown";
1732 comments_statistics[kMaxComments].size = 0;
1733 comments_statistics[kMaxComments].count = 0;
1734}
1735
1736
1737// Adds comment to 'comment_statistics' table. Performance OK sa long as
1738// 'kMaxComments' is small
1739static void EnterComment(const char* comment, int delta) {
1740 // Do not count empty comments
1741 if (delta <= 0) return;
1742 CommentStatistic* cs = &comments_statistics[kMaxComments];
1743 // Search for a free or matching entry in 'comments_statistics': 'cs'
1744 // points to result.
1745 for (int i = 0; i < kMaxComments; i++) {
1746 if (comments_statistics[i].comment == NULL) {
1747 cs = &comments_statistics[i];
1748 cs->comment = comment;
1749 break;
1750 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
1751 cs = &comments_statistics[i];
1752 break;
1753 }
1754 }
1755 // Update entry for 'comment'
1756 cs->size += delta;
1757 cs->count += 1;
1758}
1759
1760
1761// Call for each nested comment start (start marked with '[ xxx', end marked
1762// with ']'. RelocIterator 'it' must point to a comment reloc info.
1763static void CollectCommentStatistics(RelocIterator* it) {
1764 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00001765 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
1767 if (tmp[0] != '[') {
1768 // Not a nested comment; skip
1769 return;
1770 }
1771
1772 // Search for end of nested comment or a new nested comment
1773 const char* const comment_txt =
1774 reinterpret_cast<const char*>(it->rinfo()->data());
1775 const byte* prev_pc = it->rinfo()->pc();
1776 int flat_delta = 0;
1777 it->next();
1778 while (true) {
1779 // All nested comments must be terminated properly, and therefore exit
1780 // from loop.
1781 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00001782 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 const char* const txt =
1784 reinterpret_cast<const char*>(it->rinfo()->data());
1785 flat_delta += it->rinfo()->pc() - prev_pc;
1786 if (txt[0] == ']') break; // End of nested comment
1787 // A new comment
1788 CollectCommentStatistics(it);
1789 // Skip code that was covered with previous comment
1790 prev_pc = it->rinfo()->pc();
1791 }
1792 it->next();
1793 }
1794 EnterComment(comment_txt, flat_delta);
1795}
1796
1797
1798// Collects code size statistics:
1799// - by code kind
1800// - by code comment
1801void PagedSpace::CollectCodeStatistics() {
1802 HeapObjectIterator obj_it(this);
1803 while (obj_it.has_next()) {
1804 HeapObject* obj = obj_it.next();
1805 if (obj->IsCode()) {
1806 Code* code = Code::cast(obj);
1807 code_kind_statistics[code->kind()] += code->Size();
1808 RelocIterator it(code);
1809 int delta = 0;
1810 const byte* prev_pc = code->instruction_start();
1811 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001812 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 delta += it.rinfo()->pc() - prev_pc;
1814 CollectCommentStatistics(&it);
1815 prev_pc = it.rinfo()->pc();
1816 }
1817 it.next();
1818 }
1819
1820 ASSERT(code->instruction_start() <= prev_pc &&
1821 prev_pc <= code->relocation_start());
1822 delta += code->relocation_start() - prev_pc;
1823 EnterComment("NoComment", delta);
1824 }
1825 }
1826}
1827
1828
1829void OldSpace::ReportStatistics() {
1830 int pct = Available() * 100 / Capacity();
1831 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
1832 Capacity(), Waste(), Available(), pct);
1833
1834 // Report remembered set statistics.
1835 int rset_marked_pointers = 0;
1836 int rset_marked_arrays = 0;
1837 int rset_marked_array_elements = 0;
1838 int cross_gen_pointers = 0;
1839 int cross_gen_array_elements = 0;
1840
1841 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
1842 while (page_it.has_next()) {
1843 Page* p = page_it.next();
1844
1845 for (Address rset_addr = p->RSetStart();
1846 rset_addr < p->RSetEnd();
1847 rset_addr += kIntSize) {
1848 int rset = Memory::int_at(rset_addr);
1849 if (rset != 0) {
1850 // Bits were set
1851 int intoff = rset_addr - p->address();
1852 int bitoff = 0;
1853 for (; bitoff < kBitsPerInt; ++bitoff) {
1854 if ((rset & (1 << bitoff)) != 0) {
1855 int bitpos = intoff*kBitsPerByte + bitoff;
1856 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
1857 Object** obj = reinterpret_cast<Object**>(slot);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001858 if (*obj == Heap::raw_unchecked_fixed_array_map()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 rset_marked_arrays++;
1860 FixedArray* fa = FixedArray::cast(HeapObject::FromAddress(slot));
1861
1862 rset_marked_array_elements += fa->length();
1863 // Manually inline FixedArray::IterateBody
1864 Address elm_start = slot + FixedArray::kHeaderSize;
1865 Address elm_stop = elm_start + fa->length() * kPointerSize;
1866 for (Address elm_addr = elm_start;
1867 elm_addr < elm_stop; elm_addr += kPointerSize) {
1868 // Filter non-heap-object pointers
1869 Object** elm_p = reinterpret_cast<Object**>(elm_addr);
1870 if (Heap::InNewSpace(*elm_p))
1871 cross_gen_array_elements++;
1872 }
1873 } else {
1874 rset_marked_pointers++;
1875 if (Heap::InNewSpace(*obj))
1876 cross_gen_pointers++;
1877 }
1878 }
1879 }
1880 }
1881 }
1882 }
1883
1884 pct = rset_marked_pointers == 0 ?
1885 0 : cross_gen_pointers * 100 / rset_marked_pointers;
1886 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
1887 rset_marked_pointers, cross_gen_pointers, pct);
1888 PrintF(" rset_marked arrays %d, ", rset_marked_arrays);
1889 PrintF(" elements %d, ", rset_marked_array_elements);
1890 pct = rset_marked_array_elements == 0 ? 0
1891 : cross_gen_array_elements * 100 / rset_marked_array_elements;
1892 PrintF(" pointers to new space %d (%%%d)\n", cross_gen_array_elements, pct);
1893 PrintF(" total rset-marked bits %d\n",
1894 (rset_marked_pointers + rset_marked_arrays));
1895 pct = (rset_marked_pointers + rset_marked_array_elements) == 0 ? 0
1896 : (cross_gen_pointers + cross_gen_array_elements) * 100 /
1897 (rset_marked_pointers + rset_marked_array_elements);
1898 PrintF(" total rset pointers %d, true cross generation ones %d (%%%d)\n",
1899 (rset_marked_pointers + rset_marked_array_elements),
1900 (cross_gen_pointers + cross_gen_array_elements),
1901 pct);
1902
1903 ClearHistograms();
1904 HeapObjectIterator obj_it(this);
1905 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
1906 ReportHistogram(true);
1907}
1908
1909
1910// Dump the range of remembered set words between [start, end) corresponding
1911// to the pointers starting at object_p. The allocation_top is an object
1912// pointer which should not be read past. This is important for large object
1913// pages, where some bits in the remembered set range do not correspond to
1914// allocated addresses.
1915static void PrintRSetRange(Address start, Address end, Object** object_p,
1916 Address allocation_top) {
1917 Address rset_address = start;
1918
1919 // If the range starts on on odd numbered word (eg, for large object extra
1920 // remembered set ranges), print some spaces.
ager@chromium.org9085a012009-05-11 19:22:57 +00001921 if ((reinterpret_cast<uintptr_t>(start) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 PrintF(" ");
1923 }
1924
1925 // Loop over all the words in the range.
1926 while (rset_address < end) {
1927 uint32_t rset_word = Memory::uint32_at(rset_address);
1928 int bit_position = 0;
1929
1930 // Loop over all the bits in the word.
1931 while (bit_position < kBitsPerInt) {
1932 if (object_p == reinterpret_cast<Object**>(allocation_top)) {
1933 // Print a bar at the allocation pointer.
1934 PrintF("|");
1935 } else if (object_p > reinterpret_cast<Object**>(allocation_top)) {
1936 // Do not dereference object_p past the allocation pointer.
1937 PrintF("#");
1938 } else if ((rset_word & (1 << bit_position)) == 0) {
1939 // Print a dot for zero bits.
1940 PrintF(".");
1941 } else if (Heap::InNewSpace(*object_p)) {
1942 // Print an X for one bits for pointers to new space.
1943 PrintF("X");
1944 } else {
1945 // Print a circle for one bits for pointers to old space.
1946 PrintF("o");
1947 }
1948
1949 // Print a space after every 8th bit except the last.
1950 if (bit_position % 8 == 7 && bit_position != (kBitsPerInt - 1)) {
1951 PrintF(" ");
1952 }
1953
1954 // Advance to next bit.
1955 bit_position++;
1956 object_p++;
1957 }
1958
1959 // Print a newline after every odd numbered word, otherwise a space.
ager@chromium.org9085a012009-05-11 19:22:57 +00001960 if ((reinterpret_cast<uintptr_t>(rset_address) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 PrintF("\n");
1962 } else {
1963 PrintF(" ");
1964 }
1965
1966 // Advance to next remembered set word.
1967 rset_address += kIntSize;
1968 }
1969}
1970
1971
1972void PagedSpace::DoPrintRSet(const char* space_name) {
1973 PageIterator it(this, PageIterator::PAGES_IN_USE);
1974 while (it.has_next()) {
1975 Page* p = it.next();
1976 PrintF("%s page 0x%x:\n", space_name, p);
1977 PrintRSetRange(p->RSetStart(), p->RSetEnd(),
1978 reinterpret_cast<Object**>(p->ObjectAreaStart()),
1979 p->AllocationTop());
1980 PrintF("\n");
1981 }
1982}
1983
1984
1985void OldSpace::PrintRSet() { DoPrintRSet("old"); }
1986#endif
1987
1988// -----------------------------------------------------------------------------
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001989// FixedSpace implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001991void FixedSpace::PrepareForMarkCompact(bool will_compact) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 if (will_compact) {
1993 // Reset relocation info.
1994 MCResetRelocationInfo();
1995
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 // During a compacting collection, everything in the space is considered
1997 // 'available' (set by the call to MCResetRelocationInfo) and we will
1998 // rediscover live and wasted bytes during the collection.
1999 ASSERT(Available() == Capacity());
2000 } else {
2001 // During a non-compacting collection, everything below the linear
2002 // allocation pointer except wasted top-of-page blocks is considered
2003 // allocated and we will rediscover available bytes during the
2004 // collection.
2005 accounting_stats_.AllocateBytes(free_list_.available());
2006 }
2007
kasper.lund7276f142008-07-30 08:49:36 +00002008 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010}
2011
2012
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002013void FixedSpace::MCCommitRelocationInfo() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 // Update fast allocation info.
2015 allocation_info_.top = mc_forwarding_info_.top;
2016 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00002017 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018
2019 // The space is compacted and we haven't yet wasted any space.
2020 ASSERT(Waste() == 0);
2021
2022 // Update allocation_top of each page in use and compute waste.
2023 int computed_size = 0;
2024 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2025 while (it.has_next()) {
2026 Page* page = it.next();
2027 Address page_top = page->AllocationTop();
2028 computed_size += page_top - page->ObjectAreaStart();
2029 if (it.has_next()) {
2030 accounting_stats_.WasteBytes(page->ObjectAreaEnd() - page_top);
2031 }
2032 }
2033
2034 // Make sure the computed size - based on the used portion of the
2035 // pages in use - matches the size we adjust during allocation.
2036 ASSERT(computed_size == Size());
2037}
2038
2039
kasper.lund7276f142008-07-30 08:49:36 +00002040// Slow case for normal allocation. Try in order: (1) allocate in the next
2041// page in the space, (2) allocate off the space's free list, (3) expand the
2042// space, (4) fail.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002043HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2044 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002045 // Linear allocation in this space has failed. If there is another page
2046 // in the space, move to that page and allocate there. This allocation
2047 // should succeed.
2048 Page* current_page = TopPageOf(allocation_info_);
2049 if (current_page->next_page()->is_valid()) {
2050 return AllocateInNextPage(current_page, size_in_bytes);
2051 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002053 // There is no next page in this space. Try free list allocation.
2054 // The fixed space free list implicitly assumes that all free blocks
2055 // are of the fixed size.
2056 if (size_in_bytes == object_size_in_bytes_) {
kasper.lund7276f142008-07-30 08:49:36 +00002057 Object* result = free_list_.Allocate();
2058 if (!result->IsFailure()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059 accounting_stats_.AllocateBytes(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002060 return HeapObject::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 }
2062 }
kasper.lund7276f142008-07-30 08:49:36 +00002063
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002064 // Free list allocation failed and there is no next page. Fail if we have
2065 // hit the old generation size limit that should cause a garbage
2066 // collection.
2067 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2068 return NULL;
2069 }
2070
2071 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00002072 ASSERT(!current_page->next_page()->is_valid());
2073 if (Expand(current_page)) {
2074 return AllocateInNextPage(current_page, size_in_bytes);
2075 }
2076
2077 // Finally, fail.
2078 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079}
2080
2081
kasper.lund7276f142008-07-30 08:49:36 +00002082// Move to the next page (there is assumed to be one) and allocate there.
2083// The top of page block is always wasted, because it is too small to hold a
2084// map.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002085HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2086 int size_in_bytes) {
kasper.lund7276f142008-07-30 08:49:36 +00002087 ASSERT(current_page->next_page()->is_valid());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002088 ASSERT(current_page->ObjectAreaEnd() - allocation_info_.top == page_extra_);
2089 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2090 accounting_stats_.WasteBytes(page_extra_);
kasper.lund7276f142008-07-30 08:49:36 +00002091 SetAllocationInfo(&allocation_info_, current_page->next_page());
2092 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093}
2094
2095
2096#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002097void FixedSpace::ReportStatistics() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 int pct = Available() * 100 / Capacity();
2099 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2100 Capacity(), Waste(), Available(), pct);
2101
2102 // Report remembered set statistics.
2103 int rset_marked_pointers = 0;
2104 int cross_gen_pointers = 0;
2105
2106 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2107 while (page_it.has_next()) {
2108 Page* p = page_it.next();
2109
2110 for (Address rset_addr = p->RSetStart();
2111 rset_addr < p->RSetEnd();
2112 rset_addr += kIntSize) {
2113 int rset = Memory::int_at(rset_addr);
2114 if (rset != 0) {
2115 // Bits were set
2116 int intoff = rset_addr - p->address();
2117 int bitoff = 0;
2118 for (; bitoff < kBitsPerInt; ++bitoff) {
2119 if ((rset & (1 << bitoff)) != 0) {
2120 int bitpos = intoff*kBitsPerByte + bitoff;
2121 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2122 Object** obj = reinterpret_cast<Object**>(slot);
2123 rset_marked_pointers++;
2124 if (Heap::InNewSpace(*obj))
2125 cross_gen_pointers++;
2126 }
2127 }
2128 }
2129 }
2130 }
2131
2132 pct = rset_marked_pointers == 0 ?
2133 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2134 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2135 rset_marked_pointers, cross_gen_pointers, pct);
2136
2137 ClearHistograms();
2138 HeapObjectIterator obj_it(this);
2139 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
2140 ReportHistogram(false);
2141}
2142
2143
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002144void FixedSpace::PrintRSet() { DoPrintRSet(name_); }
2145#endif
2146
2147
2148// -----------------------------------------------------------------------------
2149// MapSpace implementation
2150
2151void MapSpace::PrepareForMarkCompact(bool will_compact) {
2152 // Call prepare of the super class.
2153 FixedSpace::PrepareForMarkCompact(will_compact);
2154
2155 if (will_compact) {
2156 // Initialize map index entry.
2157 int page_count = 0;
2158 PageIterator it(this, PageIterator::ALL_PAGES);
2159 while (it.has_next()) {
2160 ASSERT_MAP_PAGE_INDEX(page_count);
2161
2162 Page* p = it.next();
2163 ASSERT(p->mc_page_index == page_count);
2164
2165 page_addresses_[page_count++] = p->address();
2166 }
2167 }
2168}
2169
2170
2171#ifdef DEBUG
2172void MapSpace::VerifyObject(HeapObject* object) {
2173 // The object should be a map or a free-list node.
2174 ASSERT(object->IsMap() || object->IsByteArray());
2175}
2176#endif
2177
2178
2179// -----------------------------------------------------------------------------
2180// GlobalPropertyCellSpace implementation
2181
2182#ifdef DEBUG
2183void CellSpace::VerifyObject(HeapObject* object) {
2184 // The object should be a global object property cell or a free-list node.
2185 ASSERT(object->IsJSGlobalPropertyCell() ||
2186 object->map() == Heap::two_pointer_filler_map());
2187}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188#endif
2189
2190
2191// -----------------------------------------------------------------------------
2192// LargeObjectIterator
2193
2194LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2195 current_ = space->first_chunk_;
2196 size_func_ = NULL;
2197}
2198
2199
2200LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2201 HeapObjectCallback size_func) {
2202 current_ = space->first_chunk_;
2203 size_func_ = size_func;
2204}
2205
2206
2207HeapObject* LargeObjectIterator::next() {
2208 ASSERT(has_next());
2209 HeapObject* object = current_->GetObject();
2210 current_ = current_->next();
2211 return object;
2212}
2213
2214
2215// -----------------------------------------------------------------------------
2216// LargeObjectChunk
2217
2218LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
kasper.lund7276f142008-07-30 08:49:36 +00002219 size_t* chunk_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002220 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 size_t requested = ChunkSizeFor(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002222 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2223 chunk_size,
2224 executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 if (mem == NULL) return NULL;
2226 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2227 if (*chunk_size < requested) {
2228 MemoryAllocator::FreeRawMemory(mem, *chunk_size);
2229 LOG(DeleteEvent("LargeObjectChunk", mem));
2230 return NULL;
2231 }
2232 return reinterpret_cast<LargeObjectChunk*>(mem);
2233}
2234
2235
2236int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
2237 int os_alignment = OS::AllocateAlignment();
2238 if (os_alignment < Page::kPageSize)
2239 size_in_bytes += (Page::kPageSize - os_alignment);
2240 return size_in_bytes + Page::kObjectStartOffset;
2241}
2242
2243// -----------------------------------------------------------------------------
2244// LargeObjectSpace
2245
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002246LargeObjectSpace::LargeObjectSpace(AllocationSpace id)
2247 : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis
kasper.lund7276f142008-07-30 08:49:36 +00002248 first_chunk_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 size_(0),
2250 page_count_(0) {}
2251
2252
2253bool LargeObjectSpace::Setup() {
2254 first_chunk_ = NULL;
2255 size_ = 0;
2256 page_count_ = 0;
2257 return true;
2258}
2259
2260
2261void LargeObjectSpace::TearDown() {
2262 while (first_chunk_ != NULL) {
2263 LargeObjectChunk* chunk = first_chunk_;
2264 first_chunk_ = first_chunk_->next();
2265 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
2266 MemoryAllocator::FreeRawMemory(chunk->address(), chunk->size());
2267 }
2268
2269 size_ = 0;
2270 page_count_ = 0;
2271}
2272
2273
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00002274#ifdef ENABLE_HEAP_PROTECTION
2275
2276void LargeObjectSpace::Protect() {
2277 LargeObjectChunk* chunk = first_chunk_;
2278 while (chunk != NULL) {
2279 MemoryAllocator::Protect(chunk->address(), chunk->size());
2280 chunk = chunk->next();
2281 }
2282}
2283
2284
2285void LargeObjectSpace::Unprotect() {
2286 LargeObjectChunk* chunk = first_chunk_;
2287 while (chunk != NULL) {
2288 bool is_code = chunk->GetObject()->IsCode();
2289 MemoryAllocator::Unprotect(chunk->address(), chunk->size(),
2290 is_code ? EXECUTABLE : NOT_EXECUTABLE);
2291 chunk = chunk->next();
2292 }
2293}
2294
2295#endif
2296
2297
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002299 int object_size,
2300 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 ASSERT(0 < object_size && object_size <= requested_size);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002302
2303 // Check if we want to force a GC before growing the old space further.
2304 // If so, fail the allocation.
2305 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2306 return Failure::RetryAfterGC(requested_size, identity());
2307 }
2308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309 size_t chunk_size;
2310 LargeObjectChunk* chunk =
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002311 LargeObjectChunk::New(requested_size, &chunk_size, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 if (chunk == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00002313 return Failure::RetryAfterGC(requested_size, identity());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314 }
2315
2316 size_ += chunk_size;
2317 page_count_++;
2318 chunk->set_next(first_chunk_);
2319 chunk->set_size(chunk_size);
2320 first_chunk_ = chunk;
2321
2322 // Set the object address and size in the page header and clear its
2323 // remembered set.
2324 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2325 Address object_address = page->ObjectAreaStart();
2326 // Clear the low order bit of the second word in the page to flag it as a
2327 // large object page. If the chunk_size happened to be written there, its
2328 // low order bit should already be clear.
2329 ASSERT((chunk_size & 0x1) == 0);
2330 page->is_normal_page &= ~0x1;
2331 page->ClearRSet();
2332 int extra_bytes = requested_size - object_size;
2333 if (extra_bytes > 0) {
2334 // The extra memory for the remembered set should be cleared.
2335 memset(object_address + object_size, 0, extra_bytes);
2336 }
2337
2338 return HeapObject::FromAddress(object_address);
2339}
2340
2341
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002342Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 ASSERT(0 < size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002344 return AllocateRawInternal(size_in_bytes,
2345 size_in_bytes,
2346 EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347}
2348
2349
2350Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002351 ASSERT(0 < size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 int extra_rset_bytes = ExtraRSetBytesFor(size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002353 return AllocateRawInternal(size_in_bytes + extra_rset_bytes,
2354 size_in_bytes,
2355 NOT_EXECUTABLE);
2356}
2357
2358
2359Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2360 ASSERT(0 < size_in_bytes);
2361 return AllocateRawInternal(size_in_bytes,
2362 size_in_bytes,
2363 NOT_EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364}
2365
2366
2367// GC support
2368Object* LargeObjectSpace::FindObject(Address a) {
2369 for (LargeObjectChunk* chunk = first_chunk_;
2370 chunk != NULL;
2371 chunk = chunk->next()) {
2372 Address chunk_address = chunk->address();
2373 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2374 return chunk->GetObject();
2375 }
2376 }
2377 return Failure::Exception();
2378}
2379
2380
2381void LargeObjectSpace::ClearRSet() {
2382 ASSERT(Page::is_rset_in_use());
2383
2384 LargeObjectIterator it(this);
2385 while (it.has_next()) {
2386 HeapObject* object = it.next();
2387 // We only have code, sequential strings, or fixed arrays in large
2388 // object space, and only fixed arrays need remembered set support.
2389 if (object->IsFixedArray()) {
2390 // Clear the normal remembered set region of the page;
2391 Page* page = Page::FromAddress(object->address());
2392 page->ClearRSet();
2393
2394 // Clear the extra remembered set.
2395 int size = object->Size();
2396 int extra_rset_bytes = ExtraRSetBytesFor(size);
2397 memset(object->address() + size, 0, extra_rset_bytes);
2398 }
2399 }
2400}
2401
2402
2403void LargeObjectSpace::IterateRSet(ObjectSlotCallback copy_object_func) {
2404 ASSERT(Page::is_rset_in_use());
2405
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002406 static void* lo_rset_histogram = StatsTable::CreateHistogram(
2407 "V8.RSetLO",
2408 0,
2409 // Keeping this histogram's buckets the same as the paged space histogram.
2410 Page::kObjectAreaSize / kPointerSize,
2411 30);
2412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 LargeObjectIterator it(this);
2414 while (it.has_next()) {
2415 // We only have code, sequential strings, or fixed arrays in large
2416 // object space, and only fixed arrays can possibly contain pointers to
2417 // the young generation.
2418 HeapObject* object = it.next();
2419 if (object->IsFixedArray()) {
2420 // Iterate the normal page remembered set range.
2421 Page* page = Page::FromAddress(object->address());
2422 Address object_end = object->address() + object->Size();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002423 int count = Heap::IterateRSetRange(page->ObjectAreaStart(),
2424 Min(page->ObjectAreaEnd(), object_end),
2425 page->RSetStart(),
2426 copy_object_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427
2428 // Iterate the extra array elements.
2429 if (object_end > page->ObjectAreaEnd()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002430 count += Heap::IterateRSetRange(page->ObjectAreaEnd(), object_end,
2431 object_end, copy_object_func);
2432 }
2433 if (lo_rset_histogram != NULL) {
2434 StatsTable::AddHistogramSample(lo_rset_histogram, count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435 }
2436 }
2437 }
2438}
2439
2440
2441void LargeObjectSpace::FreeUnmarkedObjects() {
2442 LargeObjectChunk* previous = NULL;
2443 LargeObjectChunk* current = first_chunk_;
2444 while (current != NULL) {
2445 HeapObject* object = current->GetObject();
kasper.lund7276f142008-07-30 08:49:36 +00002446 if (object->IsMarked()) {
2447 object->ClearMark();
2448 MarkCompactCollector::tracer()->decrement_marked_count();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 previous = current;
2450 current = current->next();
2451 } else {
2452 Address chunk_address = current->address();
2453 size_t chunk_size = current->size();
2454
2455 // Cut the chunk out from the chunk list.
2456 current = current->next();
2457 if (previous == NULL) {
2458 first_chunk_ = current;
2459 } else {
2460 previous->set_next(current);
2461 }
2462
2463 // Free the chunk.
2464 if (object->IsCode()) {
2465 LOG(CodeDeleteEvent(object->address()));
2466 }
2467 size_ -= chunk_size;
2468 page_count_--;
2469 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size);
2470 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2471 }
2472 }
2473}
2474
2475
2476bool LargeObjectSpace::Contains(HeapObject* object) {
2477 Address address = object->address();
2478 Page* page = Page::FromAddress(address);
2479
2480 SLOW_ASSERT(!page->IsLargeObjectPage()
2481 || !FindObject(address)->IsFailure());
2482
2483 return page->IsLargeObjectPage();
2484}
2485
2486
2487#ifdef DEBUG
2488// We do not assume that the large object iterator works, because it depends
2489// on the invariants we are checking during verification.
2490void LargeObjectSpace::Verify() {
2491 for (LargeObjectChunk* chunk = first_chunk_;
2492 chunk != NULL;
2493 chunk = chunk->next()) {
2494 // Each chunk contains an object that starts at the large object page's
2495 // object area start.
2496 HeapObject* object = chunk->GetObject();
2497 Page* page = Page::FromAddress(object->address());
2498 ASSERT(object->address() == page->ObjectAreaStart());
2499
2500 // The first word should be a map, and we expect all map pointers to be
2501 // in map space.
2502 Map* map = object->map();
2503 ASSERT(map->IsMap());
2504 ASSERT(Heap::map_space()->Contains(map));
2505
2506 // We have only code, sequential strings, fixed arrays, and byte arrays
2507 // in large object space.
2508 ASSERT(object->IsCode() || object->IsSeqString()
2509 || object->IsFixedArray() || object->IsByteArray());
2510
2511 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002512 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002513
2514 // Byte arrays and strings don't have interior pointers.
2515 if (object->IsCode()) {
2516 VerifyPointersVisitor code_visitor;
2517 Code::cast(object)->ConvertICTargetsFromAddressToObject();
2518 object->IterateBody(map->instance_type(),
2519 object->Size(),
2520 &code_visitor);
2521 Code::cast(object)->ConvertICTargetsFromObjectToAddress();
2522 } else if (object->IsFixedArray()) {
2523 // We loop over fixed arrays ourselves, rather then using the visitor,
2524 // because the visitor doesn't support the start/offset iteration
2525 // needed for IsRSetSet.
2526 FixedArray* array = FixedArray::cast(object);
2527 for (int j = 0; j < array->length(); j++) {
2528 Object* element = array->get(j);
2529 if (element->IsHeapObject()) {
2530 HeapObject* element_object = HeapObject::cast(element);
2531 ASSERT(Heap::Contains(element_object));
2532 ASSERT(element_object->map()->IsMap());
2533 if (Heap::InNewSpace(element_object)) {
2534 ASSERT(Page::IsRSetSet(object->address(),
2535 FixedArray::kHeaderSize + j * kPointerSize));
2536 }
2537 }
2538 }
2539 }
2540 }
2541}
2542
2543
2544void LargeObjectSpace::Print() {
2545 LargeObjectIterator it(this);
2546 while (it.has_next()) {
2547 it.next()->Print();
2548 }
2549}
2550
2551
2552void LargeObjectSpace::ReportStatistics() {
2553 PrintF(" size: %d\n", size_);
2554 int num_objects = 0;
2555 ClearHistograms();
2556 LargeObjectIterator it(this);
2557 while (it.has_next()) {
2558 num_objects++;
2559 CollectHistogramInfo(it.next());
2560 }
2561
2562 PrintF(" number of objects %d\n", num_objects);
2563 if (num_objects > 0) ReportHistogram(false);
2564}
2565
2566
2567void LargeObjectSpace::CollectCodeStatistics() {
2568 LargeObjectIterator obj_it(this);
2569 while (obj_it.has_next()) {
2570 HeapObject* obj = obj_it.next();
2571 if (obj->IsCode()) {
2572 Code* code = Code::cast(obj);
2573 code_kind_statistics[code->kind()] += code->Size();
2574 }
2575 }
2576}
2577
2578
2579void LargeObjectSpace::PrintRSet() {
2580 LargeObjectIterator it(this);
2581 while (it.has_next()) {
2582 HeapObject* object = it.next();
2583 if (object->IsFixedArray()) {
2584 Page* page = Page::FromAddress(object->address());
2585
2586 Address allocation_top = object->address() + object->Size();
2587 PrintF("large page 0x%x:\n", page);
2588 PrintRSetRange(page->RSetStart(), page->RSetEnd(),
2589 reinterpret_cast<Object**>(object->address()),
2590 allocation_top);
2591 int extra_array_bytes = object->Size() - Page::kObjectAreaSize;
2592 int extra_rset_bits = RoundUp(extra_array_bytes / kPointerSize,
2593 kBitsPerInt);
2594 PrintF("------------------------------------------------------------"
2595 "-----------\n");
2596 PrintRSetRange(allocation_top,
2597 allocation_top + extra_rset_bits / kBitsPerByte,
2598 reinterpret_cast<Object**>(object->address()
2599 + Page::kObjectAreaSize),
2600 allocation_top);
2601 PrintF("\n");
2602 }
2603 }
2604}
2605#endif // DEBUG
2606
2607} } // namespace v8::internal