blob: 998debb30bcd0395abdd00e08ccb46e36137115b [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
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000343bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
344 ASSERT(start != NULL);
345 ASSERT(size > 0);
346 ASSERT(initial_chunk_ != NULL);
347 ASSERT(InInitialChunk(start));
348 ASSERT(InInitialChunk(start + size - 1));
349
350 if (!initial_chunk_->Uncommit(start, size)) return false;
351 Counters::memory_allocated.Decrement(size);
352 return true;
353}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354
355Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
356 PagedSpace* owner) {
357 ASSERT(IsValidChunk(chunk_id));
358 ASSERT(pages_in_chunk > 0);
359
360 Address chunk_start = chunks_[chunk_id].address();
361
362 Address low = RoundUp(chunk_start, Page::kPageSize);
363
364#ifdef DEBUG
365 size_t chunk_size = chunks_[chunk_id].size();
366 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
367 ASSERT(pages_in_chunk <=
368 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
369#endif
370
371 Address page_addr = low;
372 for (int i = 0; i < pages_in_chunk; i++) {
373 Page* p = Page::FromAddress(page_addr);
374 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
375 p->is_normal_page = 1;
376 page_addr += Page::kPageSize;
377 }
378
379 // Set the next page of the last page to 0.
380 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
381 last_page->opaque_header = OffsetFrom(0) | chunk_id;
382
383 return Page::FromAddress(low);
384}
385
386
387Page* MemoryAllocator::FreePages(Page* p) {
388 if (!p->is_valid()) return p;
389
390 // Find the first page in the same chunk as 'p'
391 Page* first_page = FindFirstPageInSameChunk(p);
392 Page* page_to_return = Page::FromAddress(NULL);
393
394 if (p != first_page) {
395 // Find the last page in the same chunk as 'prev'.
396 Page* last_page = FindLastPageInSameChunk(p);
397 first_page = GetNextPage(last_page); // first page in next chunk
398
399 // set the next_page of last_page to NULL
400 SetNextPage(last_page, Page::FromAddress(NULL));
401 page_to_return = p; // return 'p' when exiting
402 }
403
404 while (first_page->is_valid()) {
405 int chunk_id = GetChunkId(first_page);
406 ASSERT(IsValidChunk(chunk_id));
407
408 // Find the first page of the next chunk before deleting this chunk.
409 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
410
411 // Free the current chunk.
412 DeleteChunk(chunk_id);
413 }
414
415 return page_to_return;
416}
417
418
419void MemoryAllocator::DeleteChunk(int chunk_id) {
420 ASSERT(IsValidChunk(chunk_id));
421
422 ChunkInfo& c = chunks_[chunk_id];
423
424 // We cannot free a chunk contained in the initial chunk because it was not
425 // allocated with AllocateRawMemory. Instead we uncommit the virtual
426 // memory.
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000427 if (InInitialChunk(c.address())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 // TODO(1240712): VirtualMemory::Uncommit has a return value which
429 // is ignored here.
430 initial_chunk_->Uncommit(c.address(), c.size());
431 Counters::memory_allocated.Decrement(c.size());
432 } else {
433 LOG(DeleteEvent("PagedChunk", c.address()));
434 FreeRawMemory(c.address(), c.size());
435 }
436 c.init(NULL, 0, NULL);
437 Push(chunk_id);
438}
439
440
441Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
442 int chunk_id = GetChunkId(p);
443 ASSERT(IsValidChunk(chunk_id));
444
445 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
446 return Page::FromAddress(low);
447}
448
449
450Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
451 int chunk_id = GetChunkId(p);
452 ASSERT(IsValidChunk(chunk_id));
453
454 Address chunk_start = chunks_[chunk_id].address();
455 size_t chunk_size = chunks_[chunk_id].size();
456
457 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
458 ASSERT(chunk_start <= p->address() && p->address() < high);
459
460 return Page::FromAddress(high - Page::kPageSize);
461}
462
463
464#ifdef DEBUG
465void MemoryAllocator::ReportStatistics() {
466 float pct = static_cast<float>(capacity_ - size_) / capacity_;
467 PrintF(" capacity: %d, used: %d, available: %%%d\n\n",
468 capacity_, size_, static_cast<int>(pct*100));
469}
470#endif
471
472
473// -----------------------------------------------------------------------------
474// PagedSpace implementation
475
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000476PagedSpace::PagedSpace(int max_capacity,
477 AllocationSpace id,
478 Executability executable)
kasper.lund7276f142008-07-30 08:49:36 +0000479 : Space(id, executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
481 * Page::kObjectAreaSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 accounting_stats_.Clear();
483
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 allocation_info_.top = NULL;
485 allocation_info_.limit = NULL;
486
487 mc_forwarding_info_.top = NULL;
488 mc_forwarding_info_.limit = NULL;
489}
490
491
492bool PagedSpace::Setup(Address start, size_t size) {
493 if (HasBeenSetup()) return false;
494
495 int num_pages = 0;
496 // Try to use the virtual memory range passed to us. If it is too small to
497 // contain at least one page, ignore it and allocate instead.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000498 int pages_in_chunk = PagesInChunk(start, size);
499 if (pages_in_chunk > 0) {
500 first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize),
501 Page::kPageSize * pages_in_chunk,
502 this, &num_pages);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 } else {
504 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
505 max_capacity_ / Page::kObjectAreaSize);
506 first_page_ =
507 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
508 if (!first_page_->is_valid()) return false;
509 }
510
511 // We are sure that the first page is valid and that we have at least one
512 // page.
513 ASSERT(first_page_->is_valid());
514 ASSERT(num_pages > 0);
515 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
516 ASSERT(Capacity() <= max_capacity_);
517
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000518 // Sequentially initialize remembered sets in the newly allocated
519 // pages and cache the current last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
521 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000522 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 }
524
525 // Use first_page_ for allocation.
526 SetAllocationInfo(&allocation_info_, first_page_);
527
528 return true;
529}
530
531
532bool PagedSpace::HasBeenSetup() {
533 return (Capacity() > 0);
534}
535
536
537void PagedSpace::TearDown() {
538 first_page_ = MemoryAllocator::FreePages(first_page_);
539 ASSERT(!first_page_->is_valid());
540
541 accounting_stats_.Clear();
542}
543
544
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000545#ifdef ENABLE_HEAP_PROTECTION
546
547void PagedSpace::Protect() {
548 Page* page = first_page_;
549 while (page->is_valid()) {
550 MemoryAllocator::ProtectChunkFromPage(page);
551 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
552 }
553}
554
555
556void PagedSpace::Unprotect() {
557 Page* page = first_page_;
558 while (page->is_valid()) {
559 MemoryAllocator::UnprotectChunkFromPage(page);
560 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
561 }
562}
563
564#endif
565
566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567void PagedSpace::ClearRSet() {
568 PageIterator it(this, PageIterator::ALL_PAGES);
569 while (it.has_next()) {
570 it.next()->ClearRSet();
571 }
572}
573
574
575Object* PagedSpace::FindObject(Address addr) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 // Note: this function can only be called before or after mark-compact GC
577 // because it accesses map pointers.
578 ASSERT(!MarkCompactCollector::in_use());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579
580 if (!Contains(addr)) return Failure::Exception();
581
582 Page* p = Page::FromAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +0000583 ASSERT(IsUsed(p));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 Address cur = p->ObjectAreaStart();
585 Address end = p->AllocationTop();
586 while (cur < end) {
587 HeapObject* obj = HeapObject::FromAddress(cur);
588 Address next = cur + obj->Size();
589 if ((cur <= addr) && (addr < next)) return obj;
590 cur = next;
591 }
592
kasper.lund7276f142008-07-30 08:49:36 +0000593 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 return Failure::Exception();
595}
596
597
kasper.lund7276f142008-07-30 08:49:36 +0000598bool PagedSpace::IsUsed(Page* page) {
599 PageIterator it(this, PageIterator::PAGES_IN_USE);
600 while (it.has_next()) {
601 if (page == it.next()) return true;
602 }
603 return false;
604}
605
606
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
608 alloc_info->top = p->ObjectAreaStart();
609 alloc_info->limit = p->ObjectAreaEnd();
kasper.lund7276f142008-07-30 08:49:36 +0000610 ASSERT(alloc_info->VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611}
612
613
614void PagedSpace::MCResetRelocationInfo() {
615 // Set page indexes.
616 int i = 0;
617 PageIterator it(this, PageIterator::ALL_PAGES);
618 while (it.has_next()) {
619 Page* p = it.next();
620 p->mc_page_index = i++;
621 }
622
623 // Set mc_forwarding_info_ to the first page in the space.
624 SetAllocationInfo(&mc_forwarding_info_, first_page_);
625 // All the bytes in the space are 'available'. We will rediscover
626 // allocated and wasted bytes during GC.
627 accounting_stats_.Reset();
628}
629
630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
632#ifdef DEBUG
633 // The Contains function considers the address at the beginning of a
634 // page in the page, MCSpaceOffsetForAddress considers it is in the
635 // previous page.
636 if (Page::IsAlignedToPageSize(addr)) {
637 ASSERT(Contains(addr - kPointerSize));
638 } else {
639 ASSERT(Contains(addr));
640 }
641#endif
642
643 // If addr is at the end of a page, it belongs to previous page
644 Page* p = Page::IsAlignedToPageSize(addr)
645 ? Page::FromAllocationTop(addr)
646 : Page::FromAddress(addr);
647 int index = p->mc_page_index;
648 return (index * Page::kPageSize) + p->Offset(addr);
649}
650
651
kasper.lund7276f142008-07-30 08:49:36 +0000652// Slow case for reallocating and promoting objects during a compacting
653// collection. This function is not space-specific.
654HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
655 Page* current_page = TopPageOf(mc_forwarding_info_);
656 if (!current_page->next_page()->is_valid()) {
657 if (!Expand(current_page)) {
658 return NULL;
659 }
660 }
661
662 // There are surely more pages in the space now.
663 ASSERT(current_page->next_page()->is_valid());
664 // We do not add the top of page block for current page to the space's
665 // free list---the block may contain live objects so we cannot write
666 // bookkeeping information to it. Instead, we will recover top of page
667 // blocks when we move objects to their new locations.
668 //
669 // We do however write the allocation pointer to the page. The encoding
670 // of forwarding addresses is as an offset in terms of live bytes, so we
671 // need quick access to the allocation top of each page to decode
672 // forwarding addresses.
673 current_page->mc_relocation_top = mc_forwarding_info_.top;
674 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
675 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
676}
677
678
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679bool PagedSpace::Expand(Page* last_page) {
680 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
681 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
682
683 if (Capacity() == max_capacity_) return false;
684
685 ASSERT(Capacity() < max_capacity_);
686 // Last page must be valid and its next page is invalid.
687 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
688
689 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
690 if (available_pages <= 0) return false;
691
692 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
693 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
694 if (!p->is_valid()) return false;
695
696 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
697 ASSERT(Capacity() <= max_capacity_);
698
699 MemoryAllocator::SetNextPage(last_page, p);
700
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000701 // Sequentially clear remembered set of new pages and and cache the
702 // new last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 while (p->is_valid()) {
704 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000705 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 p = p->next_page();
707 }
708
709 return true;
710}
711
712
713#ifdef DEBUG
714int PagedSpace::CountTotalPages() {
715 int count = 0;
716 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
717 count++;
718 }
719 return count;
720}
721#endif
722
723
724void PagedSpace::Shrink() {
725 // Release half of free pages.
726 Page* top_page = AllocationTopPage();
727 ASSERT(top_page->is_valid());
728
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000729 // Count the number of pages we would like to free.
730 int pages_to_free = 0;
731 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
732 pages_to_free++;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 }
734
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000735 // Free pages after top_page.
736 Page* p = MemoryAllocator::FreePages(top_page->next_page());
737 MemoryAllocator::SetNextPage(top_page, p);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000739 // Find out how many pages we failed to free and update last_page_.
740 // Please note pages can only be freed in whole chunks.
741 last_page_ = top_page;
742 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
743 pages_to_free--;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000744 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 }
746
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000747 accounting_stats_.ShrinkSpace(pages_to_free * Page::kObjectAreaSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
749}
750
751
752bool PagedSpace::EnsureCapacity(int capacity) {
753 if (Capacity() >= capacity) return true;
754
755 // Start from the allocation top and loop to the last page in the space.
756 Page* last_page = AllocationTopPage();
757 Page* next_page = last_page->next_page();
758 while (next_page->is_valid()) {
759 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
760 next_page = last_page->next_page();
761 }
762
763 // Expand the space until it has the required capacity or expansion fails.
764 do {
765 if (!Expand(last_page)) return false;
766 ASSERT(last_page->next_page()->is_valid());
767 last_page =
768 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
769 } while (Capacity() < capacity);
770
771 return true;
772}
773
774
775#ifdef DEBUG
776void PagedSpace::Print() { }
777#endif
778
779
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000780#ifdef DEBUG
781// We do not assume that the PageIterator works, because it depends on the
782// invariants we are checking during verification.
783void PagedSpace::Verify(ObjectVisitor* visitor) {
784 // The allocation pointer should be valid, and it should be in a page in the
785 // space.
786 ASSERT(allocation_info_.VerifyPagedAllocation());
787 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
788 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
789
790 // Loop over all the pages.
791 bool above_allocation_top = false;
792 Page* current_page = first_page_;
793 while (current_page->is_valid()) {
794 if (above_allocation_top) {
795 // We don't care what's above the allocation top.
796 } else {
797 // Unless this is the last page in the space containing allocated
798 // objects, the allocation top should be at a constant offset from the
799 // object area end.
800 Address top = current_page->AllocationTop();
801 if (current_page == top_page) {
802 ASSERT(top == allocation_info_.top);
803 // The next page will be above the allocation top.
804 above_allocation_top = true;
805 } else {
806 ASSERT(top == current_page->ObjectAreaEnd() - page_extra_);
807 }
808
809 // It should be packed with objects from the bottom to the top.
810 Address current = current_page->ObjectAreaStart();
811 while (current < top) {
812 HeapObject* object = HeapObject::FromAddress(current);
813
814 // The first word should be a map, and we expect all map pointers to
815 // be in map space.
816 Map* map = object->map();
817 ASSERT(map->IsMap());
818 ASSERT(Heap::map_space()->Contains(map));
819
820 // Perform space-specific object verification.
821 VerifyObject(object);
822
823 // The object itself should look OK.
824 object->Verify();
825
826 // All the interior pointers should be contained in the heap and
827 // have their remembered set bits set if required as determined
828 // by the visitor.
829 int size = object->Size();
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000830 object->IterateBody(map->instance_type(), size, visitor);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000831
832 current += size;
833 }
834
835 // The allocation pointer should not be in the middle of an object.
836 ASSERT(current == top);
837 }
838
839 current_page = current_page->next_page();
840 }
841}
842#endif
843
844
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845// -----------------------------------------------------------------------------
846// NewSpace implementation
847
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000848
849bool NewSpace::Setup(Address start, int size) {
850 // Setup new space based on the preallocated memory block defined by
851 // start and size. The provided space is divided into two semi-spaces.
852 // To support fast containment testing in the new space, the size of
853 // this chunk must be a power of two and it must be aligned to its size.
854 int initial_semispace_capacity = Heap::InitialSemiSpaceSize();
855 int maximum_semispace_capacity = Heap::SemiSpaceSize();
856
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
858 ASSERT(IsPowerOf2(maximum_semispace_capacity));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859
860 // Allocate and setup the histogram arrays if necessary.
861#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
862 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
863 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
864
865#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
866 promoted_histogram_[name].set_name(#name);
867 INSTANCE_TYPE_LIST(SET_NAME)
868#undef SET_NAME
869#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000871 ASSERT(size == 2 * maximum_semispace_capacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 ASSERT(IsAddressAligned(start, size, 0));
873
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000874 if (!to_space_.Setup(start,
875 initial_semispace_capacity,
876 maximum_semispace_capacity)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 return false;
878 }
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000879 if (!from_space_.Setup(start + maximum_semispace_capacity,
880 initial_semispace_capacity,
881 maximum_semispace_capacity)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 return false;
883 }
884
885 start_ = start;
886 address_mask_ = ~(size - 1);
887 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +0000888 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000890 allocation_info_.top = to_space_.low();
891 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 mc_forwarding_info_.top = NULL;
893 mc_forwarding_info_.limit = NULL;
894
895 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
896 return true;
897}
898
899
900void NewSpace::TearDown() {
901#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
902 if (allocated_histogram_) {
903 DeleteArray(allocated_histogram_);
904 allocated_histogram_ = NULL;
905 }
906 if (promoted_histogram_) {
907 DeleteArray(promoted_histogram_);
908 promoted_histogram_ = NULL;
909 }
910#endif
911
912 start_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 allocation_info_.top = NULL;
914 allocation_info_.limit = NULL;
915 mc_forwarding_info_.top = NULL;
916 mc_forwarding_info_.limit = NULL;
917
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000918 to_space_.TearDown();
919 from_space_.TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920}
921
922
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000923#ifdef ENABLE_HEAP_PROTECTION
924
925void NewSpace::Protect() {
926 MemoryAllocator::Protect(ToSpaceLow(), Capacity());
927 MemoryAllocator::Protect(FromSpaceLow(), Capacity());
928}
929
930
931void NewSpace::Unprotect() {
932 MemoryAllocator::Unprotect(ToSpaceLow(), Capacity(),
933 to_space_.executable());
934 MemoryAllocator::Unprotect(FromSpaceLow(), Capacity(),
935 from_space_.executable());
936}
937
938#endif
939
940
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941void NewSpace::Flip() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000942 SemiSpace tmp = from_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 from_space_ = to_space_;
944 to_space_ = tmp;
945}
946
947
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000948void NewSpace::Grow() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000949 ASSERT(Capacity() < MaximumCapacity());
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000950 if (to_space_.Grow()) {
951 // Only grow from space if we managed to grow to space.
952 if (!from_space_.Grow()) {
953 // If we managed to grow to space but couldn't grow from space,
954 // attempt to shrink to space.
955 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
956 // We are in an inconsistent state because we could not
957 // commit/uncommit memory from new space.
958 V8::FatalProcessOutOfMemory("Failed to grow new space.");
959 }
960 }
961 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000962 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000964}
965
966
967void NewSpace::Shrink() {
968 int new_capacity = Max(InitialCapacity(), 2 * Size());
969 int rounded_new_capacity = RoundUp(new_capacity, OS::AllocateAlignment());
970 if (rounded_new_capacity < Capacity() &&
971 to_space_.ShrinkTo(rounded_new_capacity)) {
972 // Only shrink from space if we managed to shrink to space.
973 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
974 // If we managed to shrink to space but couldn't shrink from
975 // space, attempt to grow to space again.
976 if (!to_space_.GrowTo(from_space_.Capacity())) {
977 // We are in an inconsistent state because we could not
978 // commit/uncommit memory from new space.
979 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
980 }
981 }
982 }
983 allocation_info_.limit = to_space_.high();
984 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985}
986
987
988void NewSpace::ResetAllocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000989 allocation_info_.top = to_space_.low();
990 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
992}
993
994
995void NewSpace::MCResetRelocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000996 mc_forwarding_info_.top = from_space_.low();
997 mc_forwarding_info_.limit = from_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
999}
1000
1001
1002void NewSpace::MCCommitRelocationInfo() {
1003 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
1004 // valid allocation info for the to space.
1005 allocation_info_.top = mc_forwarding_info_.top;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001006 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1008}
1009
1010
1011#ifdef DEBUG
1012// We do not use the SemispaceIterator because verification doesn't assume
1013// that it works (it depends on the invariants we are checking).
1014void NewSpace::Verify() {
1015 // The allocation pointer should be in the space or at the very end.
1016 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1017
1018 // There should be objects packed in from the low address up to the
1019 // allocation pointer.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001020 Address current = to_space_.low();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021 while (current < top()) {
1022 HeapObject* object = HeapObject::FromAddress(current);
1023
1024 // The first word should be a map, and we expect all map pointers to
1025 // be in map space.
1026 Map* map = object->map();
1027 ASSERT(map->IsMap());
1028 ASSERT(Heap::map_space()->Contains(map));
1029
1030 // The object should not be code or a map.
1031 ASSERT(!object->IsMap());
1032 ASSERT(!object->IsCode());
1033
1034 // The object itself should look OK.
1035 object->Verify();
1036
1037 // All the interior pointers should be contained in the heap.
1038 VerifyPointersVisitor visitor;
1039 int size = object->Size();
1040 object->IterateBody(map->instance_type(), size, &visitor);
1041
1042 current += size;
1043 }
1044
1045 // The allocation pointer should not be in the middle of an object.
1046 ASSERT(current == top());
1047}
1048#endif
1049
1050
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001051bool SemiSpace::Commit() {
1052 ASSERT(!is_committed());
1053 if (!MemoryAllocator::CommitBlock(start_, capacity_, executable())) {
1054 return false;
1055 }
1056 committed_ = true;
1057 return true;
1058}
1059
1060
1061bool SemiSpace::Uncommit() {
1062 ASSERT(is_committed());
1063 if (!MemoryAllocator::UncommitBlock(start_, capacity_)) {
1064 return false;
1065 }
1066 committed_ = false;
1067 return true;
1068}
1069
1070
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071// -----------------------------------------------------------------------------
1072// SemiSpace implementation
1073
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001074bool SemiSpace::Setup(Address start,
1075 int initial_capacity,
1076 int maximum_capacity) {
1077 // Creates a space in the young generation. The constructor does not
1078 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1079 // memory of size 'capacity' when set up, and does not grow or shrink
1080 // otherwise. In the mark-compact collector, the memory region of the from
1081 // space is used as the marking stack. It requires contiguous memory
1082 // addresses.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001083 initial_capacity_ = initial_capacity;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001084 capacity_ = initial_capacity;
1085 maximum_capacity_ = maximum_capacity;
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001086 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
1088 start_ = start;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001089 address_mask_ = ~(maximum_capacity - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +00001091 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092 age_mark_ = start_;
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001093
1094 return Commit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095}
1096
1097
1098void SemiSpace::TearDown() {
1099 start_ = NULL;
1100 capacity_ = 0;
1101}
1102
1103
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001104bool SemiSpace::Grow() {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00001105 // Double the semispace size but only up to maximum capacity.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001106 int maximum_extra = maximum_capacity_ - capacity_;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00001107 int extra = Min(RoundUp(capacity_, OS::AllocateAlignment()),
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001108 maximum_extra);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001109 if (!MemoryAllocator::CommitBlock(high(), extra, executable())) {
kasper.lund7276f142008-07-30 08:49:36 +00001110 return false;
1111 }
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001112 capacity_ += extra;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 return true;
1114}
1115
1116
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001117bool SemiSpace::GrowTo(int new_capacity) {
1118 ASSERT(new_capacity <= maximum_capacity_);
1119 ASSERT(new_capacity > capacity_);
1120 size_t delta = new_capacity - capacity_;
1121 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1122 if (!MemoryAllocator::CommitBlock(high(), delta, executable())) {
1123 return false;
1124 }
1125 capacity_ = new_capacity;
1126 return true;
1127}
1128
1129
1130bool SemiSpace::ShrinkTo(int new_capacity) {
1131 ASSERT(new_capacity >= initial_capacity_);
1132 ASSERT(new_capacity < capacity_);
1133 size_t delta = capacity_ - new_capacity;
1134 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1135 if (!MemoryAllocator::UncommitBlock(high() - delta, delta)) {
1136 return false;
1137 }
1138 capacity_ = new_capacity;
1139 return true;
1140}
1141
1142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143#ifdef DEBUG
1144void SemiSpace::Print() { }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001145
1146
1147void SemiSpace::Verify() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148#endif
1149
1150
1151// -----------------------------------------------------------------------------
1152// SemiSpaceIterator implementation.
1153SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
1154 Initialize(space, space->bottom(), space->top(), NULL);
1155}
1156
1157
1158SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1159 HeapObjectCallback size_func) {
1160 Initialize(space, space->bottom(), space->top(), size_func);
1161}
1162
1163
1164SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1165 Initialize(space, start, space->top(), NULL);
1166}
1167
1168
1169void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1170 Address end,
1171 HeapObjectCallback size_func) {
1172 ASSERT(space->ToSpaceContains(start));
1173 ASSERT(space->ToSpaceLow() <= end
1174 && end <= space->ToSpaceHigh());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001175 space_ = &space->to_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176 current_ = start;
1177 limit_ = end;
1178 size_func_ = size_func;
1179}
1180
1181
1182#ifdef DEBUG
1183// A static array of histogram info for each type.
1184static HistogramInfo heap_histograms[LAST_TYPE+1];
1185static JSObject::SpillInformation js_spill_information;
1186
1187// heap_histograms is shared, always clear it before using it.
1188static void ClearHistograms() {
1189 // We reset the name each time, though it hasn't changed.
1190#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1191 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1192#undef DEF_TYPE_NAME
1193
1194#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1195 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1196#undef CLEAR_HISTOGRAM
1197
1198 js_spill_information.Clear();
1199}
1200
1201
1202static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1203
1204
1205static void ClearCodeKindStatistics() {
1206 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1207 code_kind_statistics[i] = 0;
1208 }
1209}
1210
1211
1212static void ReportCodeKindStatistics() {
1213 const char* table[Code::NUMBER_OF_KINDS];
1214
1215#define CASE(name) \
1216 case Code::name: table[Code::name] = #name; \
1217 break
1218
1219 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1220 switch (static_cast<Code::Kind>(i)) {
1221 CASE(FUNCTION);
1222 CASE(STUB);
1223 CASE(BUILTIN);
1224 CASE(LOAD_IC);
1225 CASE(KEYED_LOAD_IC);
1226 CASE(STORE_IC);
1227 CASE(KEYED_STORE_IC);
1228 CASE(CALL_IC);
1229 }
1230 }
1231
1232#undef CASE
1233
1234 PrintF("\n Code kind histograms: \n");
1235 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1236 if (code_kind_statistics[i] > 0) {
1237 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1238 }
1239 }
1240 PrintF("\n");
1241}
1242
1243
1244static int CollectHistogramInfo(HeapObject* obj) {
1245 InstanceType type = obj->map()->instance_type();
1246 ASSERT(0 <= type && type <= LAST_TYPE);
1247 ASSERT(heap_histograms[type].name() != NULL);
1248 heap_histograms[type].increment_number(1);
1249 heap_histograms[type].increment_bytes(obj->Size());
1250
1251 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1252 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1253 }
1254
1255 return obj->Size();
1256}
1257
1258
1259static void ReportHistogram(bool print_spill) {
1260 PrintF("\n Object Histogram:\n");
1261 for (int i = 0; i <= LAST_TYPE; i++) {
1262 if (heap_histograms[i].number() > 0) {
1263 PrintF(" %-33s%10d (%10d bytes)\n",
1264 heap_histograms[i].name(),
1265 heap_histograms[i].number(),
1266 heap_histograms[i].bytes());
1267 }
1268 }
1269 PrintF("\n");
1270
1271 // Summarize string types.
1272 int string_number = 0;
1273 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001274#define INCREMENT(type, size, name, camel_name) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 string_number += heap_histograms[type].number(); \
1276 string_bytes += heap_histograms[type].bytes();
1277 STRING_TYPE_LIST(INCREMENT)
1278#undef INCREMENT
1279 if (string_number > 0) {
1280 PrintF(" %-33s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
1281 string_bytes);
1282 }
1283
1284 if (FLAG_collect_heap_spill_statistics && print_spill) {
1285 js_spill_information.Print();
1286 }
1287}
1288#endif // DEBUG
1289
1290
1291// Support for statistics gathering for --heap-stats and --log-gc.
1292#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1293void NewSpace::ClearHistograms() {
1294 for (int i = 0; i <= LAST_TYPE; i++) {
1295 allocated_histogram_[i].clear();
1296 promoted_histogram_[i].clear();
1297 }
1298}
1299
1300// Because the copying collector does not touch garbage objects, we iterate
1301// the new space before a collection to get a histogram of allocated objects.
1302// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1303// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1304// flag is set.
1305void NewSpace::CollectStatistics() {
1306 ClearHistograms();
1307 SemiSpaceIterator it(this);
1308 while (it.has_next()) RecordAllocation(it.next());
1309}
1310
1311
1312#ifdef ENABLE_LOGGING_AND_PROFILING
1313static void DoReportStatistics(HistogramInfo* info, const char* description) {
1314 LOG(HeapSampleBeginEvent("NewSpace", description));
1315 // Lump all the string types together.
1316 int string_number = 0;
1317 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001318#define INCREMENT(type, size, name, camel_name) \
1319 string_number += info[type].number(); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 string_bytes += info[type].bytes();
1321 STRING_TYPE_LIST(INCREMENT)
1322#undef INCREMENT
1323 if (string_number > 0) {
1324 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1325 }
1326
1327 // Then do the other types.
1328 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1329 if (info[i].number() > 0) {
1330 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1331 info[i].bytes()));
1332 }
1333 }
1334 LOG(HeapSampleEndEvent("NewSpace", description));
1335}
1336#endif // ENABLE_LOGGING_AND_PROFILING
1337
1338
1339void NewSpace::ReportStatistics() {
1340#ifdef DEBUG
1341 if (FLAG_heap_stats) {
1342 float pct = static_cast<float>(Available()) / Capacity();
1343 PrintF(" capacity: %d, available: %d, %%%d\n",
1344 Capacity(), Available(), static_cast<int>(pct*100));
1345 PrintF("\n Object Histogram:\n");
1346 for (int i = 0; i <= LAST_TYPE; i++) {
1347 if (allocated_histogram_[i].number() > 0) {
1348 PrintF(" %-33s%10d (%10d bytes)\n",
1349 allocated_histogram_[i].name(),
1350 allocated_histogram_[i].number(),
1351 allocated_histogram_[i].bytes());
1352 }
1353 }
1354 PrintF("\n");
1355 }
1356#endif // DEBUG
1357
1358#ifdef ENABLE_LOGGING_AND_PROFILING
1359 if (FLAG_log_gc) {
1360 DoReportStatistics(allocated_histogram_, "allocated");
1361 DoReportStatistics(promoted_histogram_, "promoted");
1362 }
1363#endif // ENABLE_LOGGING_AND_PROFILING
1364}
1365
1366
1367void NewSpace::RecordAllocation(HeapObject* obj) {
1368 InstanceType type = obj->map()->instance_type();
1369 ASSERT(0 <= type && type <= LAST_TYPE);
1370 allocated_histogram_[type].increment_number(1);
1371 allocated_histogram_[type].increment_bytes(obj->Size());
1372}
1373
1374
1375void NewSpace::RecordPromotion(HeapObject* obj) {
1376 InstanceType type = obj->map()->instance_type();
1377 ASSERT(0 <= type && type <= LAST_TYPE);
1378 promoted_histogram_[type].increment_number(1);
1379 promoted_histogram_[type].increment_bytes(obj->Size());
1380}
1381#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1382
1383
1384// -----------------------------------------------------------------------------
1385// Free lists for old object spaces implementation
1386
1387void FreeListNode::set_size(int size_in_bytes) {
1388 ASSERT(size_in_bytes > 0);
1389 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1390
1391 // We write a map and possibly size information to the block. If the block
1392 // is big enough to be a ByteArray with at least one extra word (the next
1393 // pointer), we set its map to be the byte array map and its size to an
1394 // appropriate array length for the desired size from HeapObject::Size().
1395 // If the block is too small (eg, one or two words), to hold both a size
1396 // field and a next pointer, we give it a filler map that gives it the
1397 // correct size.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001398 if (size_in_bytes > ByteArray::kAlignedSize) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001399 set_map(Heap::raw_unchecked_byte_array_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 ByteArray::cast(this)->set_length(ByteArray::LengthFor(size_in_bytes));
1401 } else if (size_in_bytes == kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001402 set_map(Heap::raw_unchecked_one_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 } else if (size_in_bytes == 2 * kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001404 set_map(Heap::raw_unchecked_two_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 } else {
1406 UNREACHABLE();
1407 }
kasper.lund7276f142008-07-30 08:49:36 +00001408 ASSERT(Size() == size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409}
1410
1411
1412Address FreeListNode::next() {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001413 ASSERT(map() == Heap::raw_unchecked_byte_array_map() ||
1414 map() == Heap::raw_unchecked_two_pointer_filler_map());
1415 if (map() == Heap::raw_unchecked_byte_array_map()) {
1416 ASSERT(Size() >= kNextOffset + kPointerSize);
1417 return Memory::Address_at(address() + kNextOffset);
1418 } else {
1419 return Memory::Address_at(address() + kPointerSize);
1420 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421}
1422
1423
1424void FreeListNode::set_next(Address next) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001425 ASSERT(map() == Heap::raw_unchecked_byte_array_map() ||
1426 map() == Heap::raw_unchecked_two_pointer_filler_map());
1427 if (map() == Heap::raw_unchecked_byte_array_map()) {
1428 ASSERT(Size() >= kNextOffset + kPointerSize);
1429 Memory::Address_at(address() + kNextOffset) = next;
1430 } else {
1431 Memory::Address_at(address() + kPointerSize) = next;
1432 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433}
1434
1435
1436OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1437 Reset();
1438}
1439
1440
1441void OldSpaceFreeList::Reset() {
1442 available_ = 0;
1443 for (int i = 0; i < kFreeListsLength; i++) {
1444 free_[i].head_node_ = NULL;
1445 }
1446 needs_rebuild_ = false;
1447 finger_ = kHead;
1448 free_[kHead].next_size_ = kEnd;
1449}
1450
1451
1452void OldSpaceFreeList::RebuildSizeList() {
1453 ASSERT(needs_rebuild_);
1454 int cur = kHead;
1455 for (int i = cur + 1; i < kFreeListsLength; i++) {
1456 if (free_[i].head_node_ != NULL) {
1457 free_[cur].next_size_ = i;
1458 cur = i;
1459 }
1460 }
1461 free_[cur].next_size_ = kEnd;
1462 needs_rebuild_ = false;
1463}
1464
1465
1466int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1467#ifdef DEBUG
1468 for (int i = 0; i < size_in_bytes; i += kPointerSize) {
1469 Memory::Address_at(start + i) = kZapValue;
1470 }
1471#endif
1472 FreeListNode* node = FreeListNode::FromAddress(start);
1473 node->set_size(size_in_bytes);
1474
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001475 // We don't use the freelists in compacting mode. This makes it more like a
1476 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1477 // collector.
1478 if (FLAG_always_compact) {
1479 return size_in_bytes;
1480 }
1481
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 // Early return to drop too-small blocks on the floor (one or two word
1483 // blocks cannot hold a map pointer, a size field, and a pointer to the
1484 // next block in the free list).
1485 if (size_in_bytes < kMinBlockSize) {
1486 return size_in_bytes;
1487 }
1488
1489 // Insert other blocks at the head of an exact free list.
1490 int index = size_in_bytes >> kPointerSizeLog2;
1491 node->set_next(free_[index].head_node_);
1492 free_[index].head_node_ = node->address();
1493 available_ += size_in_bytes;
1494 needs_rebuild_ = true;
1495 return 0;
1496}
1497
1498
1499Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1500 ASSERT(0 < size_in_bytes);
1501 ASSERT(size_in_bytes <= kMaxBlockSize);
1502 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1503
1504 if (needs_rebuild_) RebuildSizeList();
1505 int index = size_in_bytes >> kPointerSizeLog2;
1506 // Check for a perfect fit.
1507 if (free_[index].head_node_ != NULL) {
1508 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1509 // If this was the last block of its size, remove the size.
1510 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1511 available_ -= size_in_bytes;
1512 *wasted_bytes = 0;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001513 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001514 return node;
1515 }
1516 // Search the size list for the best fit.
1517 int prev = finger_ < index ? finger_ : kHead;
1518 int cur = FindSize(index, &prev);
1519 ASSERT(index < cur);
1520 if (cur == kEnd) {
1521 // No large enough size in list.
1522 *wasted_bytes = 0;
1523 return Failure::RetryAfterGC(size_in_bytes, owner_);
1524 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001525 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 int rem = cur - index;
1527 int rem_bytes = rem << kPointerSizeLog2;
1528 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
kasper.lund7276f142008-07-30 08:49:36 +00001529 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1531 size_in_bytes);
1532 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1533 // to avoid many redundant tests and calls to Insert/RemoveSize.
1534 if (prev < rem) {
1535 // Simple case: insert rem between prev and cur.
1536 finger_ = prev;
1537 free_[prev].next_size_ = rem;
1538 // If this was the last block of size cur, remove the size.
1539 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1540 free_[rem].next_size_ = free_[cur].next_size_;
1541 } else {
1542 free_[rem].next_size_ = cur;
1543 }
1544 // Add the remainder block.
1545 rem_node->set_size(rem_bytes);
1546 rem_node->set_next(free_[rem].head_node_);
1547 free_[rem].head_node_ = rem_node->address();
1548 } else {
1549 // If this was the last block of size cur, remove the size.
1550 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1551 finger_ = prev;
1552 free_[prev].next_size_ = free_[cur].next_size_;
1553 }
1554 if (rem_bytes < kMinBlockSize) {
1555 // Too-small remainder is wasted.
1556 rem_node->set_size(rem_bytes);
1557 available_ -= size_in_bytes + rem_bytes;
1558 *wasted_bytes = rem_bytes;
1559 return cur_node;
1560 }
1561 // Add the remainder block and, if needed, insert its size.
1562 rem_node->set_size(rem_bytes);
1563 rem_node->set_next(free_[rem].head_node_);
1564 free_[rem].head_node_ = rem_node->address();
1565 if (rem_node->next() == NULL) InsertSize(rem);
1566 }
1567 available_ -= size_in_bytes;
1568 *wasted_bytes = 0;
1569 return cur_node;
1570}
1571
1572
kasper.lund7276f142008-07-30 08:49:36 +00001573#ifdef DEBUG
1574bool OldSpaceFreeList::Contains(FreeListNode* node) {
1575 for (int i = 0; i < kFreeListsLength; i++) {
1576 Address cur_addr = free_[i].head_node_;
1577 while (cur_addr != NULL) {
1578 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1579 if (cur_node == node) return true;
1580 cur_addr = cur_node->next();
1581 }
1582 }
1583 return false;
1584}
1585#endif
1586
1587
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001588FixedSizeFreeList::FixedSizeFreeList(AllocationSpace owner, int object_size)
1589 : owner_(owner), object_size_(object_size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 Reset();
1591}
1592
1593
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001594void FixedSizeFreeList::Reset() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595 available_ = 0;
1596 head_ = NULL;
1597}
1598
1599
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001600void FixedSizeFreeList::Free(Address start) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001602 for (int i = 0; i < object_size_; i += kPointerSize) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 Memory::Address_at(start + i) = kZapValue;
1604 }
1605#endif
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001606 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 FreeListNode* node = FreeListNode::FromAddress(start);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001608 node->set_size(object_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 node->set_next(head_);
1610 head_ = node->address();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001611 available_ += object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612}
1613
1614
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001615Object* FixedSizeFreeList::Allocate() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 if (head_ == NULL) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001617 return Failure::RetryAfterGC(object_size_, owner_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 }
1619
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001620 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 FreeListNode* node = FreeListNode::FromAddress(head_);
1622 head_ = node->next();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001623 available_ -= object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 return node;
1625}
1626
1627
1628// -----------------------------------------------------------------------------
1629// OldSpace implementation
1630
1631void OldSpace::PrepareForMarkCompact(bool will_compact) {
1632 if (will_compact) {
1633 // Reset relocation info. During a compacting collection, everything in
1634 // the space is considered 'available' and we will rediscover live data
1635 // and waste during the collection.
1636 MCResetRelocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637 ASSERT(Available() == Capacity());
1638 } else {
1639 // During a non-compacting collection, everything below the linear
1640 // allocation pointer is considered allocated (everything above is
1641 // available) and we will rediscover available and wasted bytes during
1642 // the collection.
1643 accounting_stats_.AllocateBytes(free_list_.available());
1644 accounting_stats_.FillWastedBytes(Waste());
1645 }
1646
kasper.lund7276f142008-07-30 08:49:36 +00001647 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649}
1650
1651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652void OldSpace::MCCommitRelocationInfo() {
1653 // Update fast allocation info.
1654 allocation_info_.top = mc_forwarding_info_.top;
1655 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001656 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657
1658 // The space is compacted and we haven't yet built free lists or
1659 // wasted any space.
1660 ASSERT(Waste() == 0);
1661 ASSERT(AvailableFree() == 0);
1662
1663 // Build the free list for the space.
1664 int computed_size = 0;
1665 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1666 while (it.has_next()) {
1667 Page* p = it.next();
1668 // Space below the relocation pointer is allocated.
1669 computed_size += p->mc_relocation_top - p->ObjectAreaStart();
1670 if (it.has_next()) {
1671 // Free the space at the top of the page. We cannot use
1672 // p->mc_relocation_top after the call to Free (because Free will clear
1673 // remembered set bits).
1674 int extra_size = p->ObjectAreaEnd() - p->mc_relocation_top;
1675 if (extra_size > 0) {
1676 int wasted_bytes = free_list_.Free(p->mc_relocation_top, extra_size);
1677 // The bytes we have just "freed" to add to the free list were
1678 // already accounted as available.
1679 accounting_stats_.WasteBytes(wasted_bytes);
1680 }
1681 }
1682 }
1683
1684 // Make sure the computed size - based on the used portion of the pages in
1685 // use - matches the size obtained while computing forwarding addresses.
1686 ASSERT(computed_size == Size());
1687}
1688
1689
kasper.lund7276f142008-07-30 08:49:36 +00001690// Slow case for normal allocation. Try in order: (1) allocate in the next
1691// page in the space, (2) allocate off the space's free list, (3) expand the
1692// space, (4) fail.
1693HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
1694 // Linear allocation in this space has failed. If there is another page
1695 // in the space, move to that page and allocate there. This allocation
1696 // should succeed (size_in_bytes should not be greater than a page's
1697 // object area size).
1698 Page* current_page = TopPageOf(allocation_info_);
1699 if (current_page->next_page()->is_valid()) {
1700 return AllocateInNextPage(current_page, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 }
kasper.lund7276f142008-07-30 08:49:36 +00001702
1703 // There is no next page in this space. Try free list allocation.
1704 int wasted_bytes;
1705 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
1706 accounting_stats_.WasteBytes(wasted_bytes);
1707 if (!result->IsFailure()) {
1708 accounting_stats_.AllocateBytes(size_in_bytes);
1709 return HeapObject::cast(result);
1710 }
1711
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001712 // Free list allocation failed and there is no next page. Fail if we have
1713 // hit the old generation size limit that should cause a garbage
1714 // collection.
1715 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
1716 return NULL;
1717 }
1718
1719 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00001720 ASSERT(!current_page->next_page()->is_valid());
1721 if (Expand(current_page)) {
1722 return AllocateInNextPage(current_page, size_in_bytes);
1723 }
1724
1725 // Finally, fail.
1726 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727}
1728
1729
kasper.lund7276f142008-07-30 08:49:36 +00001730// Add the block at the top of the page to the space's free list, set the
1731// allocation info to the next page (assumed to be one), and allocate
1732// linearly there.
1733HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
1734 int size_in_bytes) {
1735 ASSERT(current_page->next_page()->is_valid());
1736 // Add the block at the top of this page to the free list.
1737 int free_size = current_page->ObjectAreaEnd() - allocation_info_.top;
1738 if (free_size > 0) {
1739 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
1740 accounting_stats_.WasteBytes(wasted_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 }
kasper.lund7276f142008-07-30 08:49:36 +00001742 SetAllocationInfo(&allocation_info_, current_page->next_page());
1743 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744}
1745
1746
1747#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748struct CommentStatistic {
1749 const char* comment;
1750 int size;
1751 int count;
1752 void Clear() {
1753 comment = NULL;
1754 size = 0;
1755 count = 0;
1756 }
1757};
1758
1759
1760// must be small, since an iteration is used for lookup
1761const int kMaxComments = 64;
1762static CommentStatistic comments_statistics[kMaxComments+1];
1763
1764
1765void PagedSpace::ReportCodeStatistics() {
1766 ReportCodeKindStatistics();
1767 PrintF("Code comment statistics (\" [ comment-txt : size/ "
1768 "count (average)\"):\n");
1769 for (int i = 0; i <= kMaxComments; i++) {
1770 const CommentStatistic& cs = comments_statistics[i];
1771 if (cs.size > 0) {
1772 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
1773 cs.size/cs.count);
1774 }
1775 }
1776 PrintF("\n");
1777}
1778
1779
1780void PagedSpace::ResetCodeStatistics() {
1781 ClearCodeKindStatistics();
1782 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
1783 comments_statistics[kMaxComments].comment = "Unknown";
1784 comments_statistics[kMaxComments].size = 0;
1785 comments_statistics[kMaxComments].count = 0;
1786}
1787
1788
1789// Adds comment to 'comment_statistics' table. Performance OK sa long as
1790// 'kMaxComments' is small
1791static void EnterComment(const char* comment, int delta) {
1792 // Do not count empty comments
1793 if (delta <= 0) return;
1794 CommentStatistic* cs = &comments_statistics[kMaxComments];
1795 // Search for a free or matching entry in 'comments_statistics': 'cs'
1796 // points to result.
1797 for (int i = 0; i < kMaxComments; i++) {
1798 if (comments_statistics[i].comment == NULL) {
1799 cs = &comments_statistics[i];
1800 cs->comment = comment;
1801 break;
1802 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
1803 cs = &comments_statistics[i];
1804 break;
1805 }
1806 }
1807 // Update entry for 'comment'
1808 cs->size += delta;
1809 cs->count += 1;
1810}
1811
1812
1813// Call for each nested comment start (start marked with '[ xxx', end marked
1814// with ']'. RelocIterator 'it' must point to a comment reloc info.
1815static void CollectCommentStatistics(RelocIterator* it) {
1816 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00001817 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
1819 if (tmp[0] != '[') {
1820 // Not a nested comment; skip
1821 return;
1822 }
1823
1824 // Search for end of nested comment or a new nested comment
1825 const char* const comment_txt =
1826 reinterpret_cast<const char*>(it->rinfo()->data());
1827 const byte* prev_pc = it->rinfo()->pc();
1828 int flat_delta = 0;
1829 it->next();
1830 while (true) {
1831 // All nested comments must be terminated properly, and therefore exit
1832 // from loop.
1833 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00001834 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 const char* const txt =
1836 reinterpret_cast<const char*>(it->rinfo()->data());
1837 flat_delta += it->rinfo()->pc() - prev_pc;
1838 if (txt[0] == ']') break; // End of nested comment
1839 // A new comment
1840 CollectCommentStatistics(it);
1841 // Skip code that was covered with previous comment
1842 prev_pc = it->rinfo()->pc();
1843 }
1844 it->next();
1845 }
1846 EnterComment(comment_txt, flat_delta);
1847}
1848
1849
1850// Collects code size statistics:
1851// - by code kind
1852// - by code comment
1853void PagedSpace::CollectCodeStatistics() {
1854 HeapObjectIterator obj_it(this);
1855 while (obj_it.has_next()) {
1856 HeapObject* obj = obj_it.next();
1857 if (obj->IsCode()) {
1858 Code* code = Code::cast(obj);
1859 code_kind_statistics[code->kind()] += code->Size();
1860 RelocIterator it(code);
1861 int delta = 0;
1862 const byte* prev_pc = code->instruction_start();
1863 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001864 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 delta += it.rinfo()->pc() - prev_pc;
1866 CollectCommentStatistics(&it);
1867 prev_pc = it.rinfo()->pc();
1868 }
1869 it.next();
1870 }
1871
1872 ASSERT(code->instruction_start() <= prev_pc &&
1873 prev_pc <= code->relocation_start());
1874 delta += code->relocation_start() - prev_pc;
1875 EnterComment("NoComment", delta);
1876 }
1877 }
1878}
1879
1880
1881void OldSpace::ReportStatistics() {
1882 int pct = Available() * 100 / Capacity();
1883 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
1884 Capacity(), Waste(), Available(), pct);
1885
1886 // Report remembered set statistics.
1887 int rset_marked_pointers = 0;
1888 int rset_marked_arrays = 0;
1889 int rset_marked_array_elements = 0;
1890 int cross_gen_pointers = 0;
1891 int cross_gen_array_elements = 0;
1892
1893 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
1894 while (page_it.has_next()) {
1895 Page* p = page_it.next();
1896
1897 for (Address rset_addr = p->RSetStart();
1898 rset_addr < p->RSetEnd();
1899 rset_addr += kIntSize) {
1900 int rset = Memory::int_at(rset_addr);
1901 if (rset != 0) {
1902 // Bits were set
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001903 int intoff = rset_addr - p->address() - Page::kRSetOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904 int bitoff = 0;
1905 for (; bitoff < kBitsPerInt; ++bitoff) {
1906 if ((rset & (1 << bitoff)) != 0) {
1907 int bitpos = intoff*kBitsPerByte + bitoff;
1908 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
1909 Object** obj = reinterpret_cast<Object**>(slot);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001910 if (*obj == Heap::raw_unchecked_fixed_array_map()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 rset_marked_arrays++;
1912 FixedArray* fa = FixedArray::cast(HeapObject::FromAddress(slot));
1913
1914 rset_marked_array_elements += fa->length();
1915 // Manually inline FixedArray::IterateBody
1916 Address elm_start = slot + FixedArray::kHeaderSize;
1917 Address elm_stop = elm_start + fa->length() * kPointerSize;
1918 for (Address elm_addr = elm_start;
1919 elm_addr < elm_stop; elm_addr += kPointerSize) {
1920 // Filter non-heap-object pointers
1921 Object** elm_p = reinterpret_cast<Object**>(elm_addr);
1922 if (Heap::InNewSpace(*elm_p))
1923 cross_gen_array_elements++;
1924 }
1925 } else {
1926 rset_marked_pointers++;
1927 if (Heap::InNewSpace(*obj))
1928 cross_gen_pointers++;
1929 }
1930 }
1931 }
1932 }
1933 }
1934 }
1935
1936 pct = rset_marked_pointers == 0 ?
1937 0 : cross_gen_pointers * 100 / rset_marked_pointers;
1938 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
1939 rset_marked_pointers, cross_gen_pointers, pct);
1940 PrintF(" rset_marked arrays %d, ", rset_marked_arrays);
1941 PrintF(" elements %d, ", rset_marked_array_elements);
1942 pct = rset_marked_array_elements == 0 ? 0
1943 : cross_gen_array_elements * 100 / rset_marked_array_elements;
1944 PrintF(" pointers to new space %d (%%%d)\n", cross_gen_array_elements, pct);
1945 PrintF(" total rset-marked bits %d\n",
1946 (rset_marked_pointers + rset_marked_arrays));
1947 pct = (rset_marked_pointers + rset_marked_array_elements) == 0 ? 0
1948 : (cross_gen_pointers + cross_gen_array_elements) * 100 /
1949 (rset_marked_pointers + rset_marked_array_elements);
1950 PrintF(" total rset pointers %d, true cross generation ones %d (%%%d)\n",
1951 (rset_marked_pointers + rset_marked_array_elements),
1952 (cross_gen_pointers + cross_gen_array_elements),
1953 pct);
1954
1955 ClearHistograms();
1956 HeapObjectIterator obj_it(this);
1957 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
1958 ReportHistogram(true);
1959}
1960
1961
1962// Dump the range of remembered set words between [start, end) corresponding
1963// to the pointers starting at object_p. The allocation_top is an object
1964// pointer which should not be read past. This is important for large object
1965// pages, where some bits in the remembered set range do not correspond to
1966// allocated addresses.
1967static void PrintRSetRange(Address start, Address end, Object** object_p,
1968 Address allocation_top) {
1969 Address rset_address = start;
1970
1971 // If the range starts on on odd numbered word (eg, for large object extra
1972 // remembered set ranges), print some spaces.
ager@chromium.org9085a012009-05-11 19:22:57 +00001973 if ((reinterpret_cast<uintptr_t>(start) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 PrintF(" ");
1975 }
1976
1977 // Loop over all the words in the range.
1978 while (rset_address < end) {
1979 uint32_t rset_word = Memory::uint32_at(rset_address);
1980 int bit_position = 0;
1981
1982 // Loop over all the bits in the word.
1983 while (bit_position < kBitsPerInt) {
1984 if (object_p == reinterpret_cast<Object**>(allocation_top)) {
1985 // Print a bar at the allocation pointer.
1986 PrintF("|");
1987 } else if (object_p > reinterpret_cast<Object**>(allocation_top)) {
1988 // Do not dereference object_p past the allocation pointer.
1989 PrintF("#");
1990 } else if ((rset_word & (1 << bit_position)) == 0) {
1991 // Print a dot for zero bits.
1992 PrintF(".");
1993 } else if (Heap::InNewSpace(*object_p)) {
1994 // Print an X for one bits for pointers to new space.
1995 PrintF("X");
1996 } else {
1997 // Print a circle for one bits for pointers to old space.
1998 PrintF("o");
1999 }
2000
2001 // Print a space after every 8th bit except the last.
2002 if (bit_position % 8 == 7 && bit_position != (kBitsPerInt - 1)) {
2003 PrintF(" ");
2004 }
2005
2006 // Advance to next bit.
2007 bit_position++;
2008 object_p++;
2009 }
2010
2011 // Print a newline after every odd numbered word, otherwise a space.
ager@chromium.org9085a012009-05-11 19:22:57 +00002012 if ((reinterpret_cast<uintptr_t>(rset_address) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013 PrintF("\n");
2014 } else {
2015 PrintF(" ");
2016 }
2017
2018 // Advance to next remembered set word.
2019 rset_address += kIntSize;
2020 }
2021}
2022
2023
2024void PagedSpace::DoPrintRSet(const char* space_name) {
2025 PageIterator it(this, PageIterator::PAGES_IN_USE);
2026 while (it.has_next()) {
2027 Page* p = it.next();
2028 PrintF("%s page 0x%x:\n", space_name, p);
2029 PrintRSetRange(p->RSetStart(), p->RSetEnd(),
2030 reinterpret_cast<Object**>(p->ObjectAreaStart()),
2031 p->AllocationTop());
2032 PrintF("\n");
2033 }
2034}
2035
2036
2037void OldSpace::PrintRSet() { DoPrintRSet("old"); }
2038#endif
2039
2040// -----------------------------------------------------------------------------
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002041// FixedSpace implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002043void FixedSpace::PrepareForMarkCompact(bool will_compact) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 if (will_compact) {
2045 // Reset relocation info.
2046 MCResetRelocationInfo();
2047
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 // During a compacting collection, everything in the space is considered
2049 // 'available' (set by the call to MCResetRelocationInfo) and we will
2050 // rediscover live and wasted bytes during the collection.
2051 ASSERT(Available() == Capacity());
2052 } else {
2053 // During a non-compacting collection, everything below the linear
2054 // allocation pointer except wasted top-of-page blocks is considered
2055 // allocated and we will rediscover available bytes during the
2056 // collection.
2057 accounting_stats_.AllocateBytes(free_list_.available());
2058 }
2059
kasper.lund7276f142008-07-30 08:49:36 +00002060 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062}
2063
2064
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002065void FixedSpace::MCCommitRelocationInfo() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 // Update fast allocation info.
2067 allocation_info_.top = mc_forwarding_info_.top;
2068 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00002069 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070
2071 // The space is compacted and we haven't yet wasted any space.
2072 ASSERT(Waste() == 0);
2073
2074 // Update allocation_top of each page in use and compute waste.
2075 int computed_size = 0;
2076 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2077 while (it.has_next()) {
2078 Page* page = it.next();
2079 Address page_top = page->AllocationTop();
2080 computed_size += page_top - page->ObjectAreaStart();
2081 if (it.has_next()) {
2082 accounting_stats_.WasteBytes(page->ObjectAreaEnd() - page_top);
2083 }
2084 }
2085
2086 // Make sure the computed size - based on the used portion of the
2087 // pages in use - matches the size we adjust during allocation.
2088 ASSERT(computed_size == Size());
2089}
2090
2091
kasper.lund7276f142008-07-30 08:49:36 +00002092// Slow case for normal allocation. Try in order: (1) allocate in the next
2093// page in the space, (2) allocate off the space's free list, (3) expand the
2094// space, (4) fail.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002095HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2096 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002097 // Linear allocation in this space has failed. If there is another page
2098 // in the space, move to that page and allocate there. This allocation
2099 // should succeed.
2100 Page* current_page = TopPageOf(allocation_info_);
2101 if (current_page->next_page()->is_valid()) {
2102 return AllocateInNextPage(current_page, size_in_bytes);
2103 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002105 // There is no next page in this space. Try free list allocation.
2106 // The fixed space free list implicitly assumes that all free blocks
2107 // are of the fixed size.
2108 if (size_in_bytes == object_size_in_bytes_) {
kasper.lund7276f142008-07-30 08:49:36 +00002109 Object* result = free_list_.Allocate();
2110 if (!result->IsFailure()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 accounting_stats_.AllocateBytes(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002112 return HeapObject::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 }
2114 }
kasper.lund7276f142008-07-30 08:49:36 +00002115
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002116 // Free list allocation failed and there is no next page. Fail if we have
2117 // hit the old generation size limit that should cause a garbage
2118 // collection.
2119 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2120 return NULL;
2121 }
2122
2123 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00002124 ASSERT(!current_page->next_page()->is_valid());
2125 if (Expand(current_page)) {
2126 return AllocateInNextPage(current_page, size_in_bytes);
2127 }
2128
2129 // Finally, fail.
2130 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131}
2132
2133
kasper.lund7276f142008-07-30 08:49:36 +00002134// Move to the next page (there is assumed to be one) and allocate there.
2135// The top of page block is always wasted, because it is too small to hold a
2136// map.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002137HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2138 int size_in_bytes) {
kasper.lund7276f142008-07-30 08:49:36 +00002139 ASSERT(current_page->next_page()->is_valid());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002140 ASSERT(current_page->ObjectAreaEnd() - allocation_info_.top == page_extra_);
2141 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2142 accounting_stats_.WasteBytes(page_extra_);
kasper.lund7276f142008-07-30 08:49:36 +00002143 SetAllocationInfo(&allocation_info_, current_page->next_page());
2144 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145}
2146
2147
2148#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002149void FixedSpace::ReportStatistics() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 int pct = Available() * 100 / Capacity();
2151 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2152 Capacity(), Waste(), Available(), pct);
2153
2154 // Report remembered set statistics.
2155 int rset_marked_pointers = 0;
2156 int cross_gen_pointers = 0;
2157
2158 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2159 while (page_it.has_next()) {
2160 Page* p = page_it.next();
2161
2162 for (Address rset_addr = p->RSetStart();
2163 rset_addr < p->RSetEnd();
2164 rset_addr += kIntSize) {
2165 int rset = Memory::int_at(rset_addr);
2166 if (rset != 0) {
2167 // Bits were set
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002168 int intoff = rset_addr - p->address() - Page::kRSetOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 int bitoff = 0;
2170 for (; bitoff < kBitsPerInt; ++bitoff) {
2171 if ((rset & (1 << bitoff)) != 0) {
2172 int bitpos = intoff*kBitsPerByte + bitoff;
2173 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2174 Object** obj = reinterpret_cast<Object**>(slot);
2175 rset_marked_pointers++;
2176 if (Heap::InNewSpace(*obj))
2177 cross_gen_pointers++;
2178 }
2179 }
2180 }
2181 }
2182 }
2183
2184 pct = rset_marked_pointers == 0 ?
2185 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2186 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2187 rset_marked_pointers, cross_gen_pointers, pct);
2188
2189 ClearHistograms();
2190 HeapObjectIterator obj_it(this);
2191 while (obj_it.has_next()) { CollectHistogramInfo(obj_it.next()); }
2192 ReportHistogram(false);
2193}
2194
2195
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002196void FixedSpace::PrintRSet() { DoPrintRSet(name_); }
2197#endif
2198
2199
2200// -----------------------------------------------------------------------------
2201// MapSpace implementation
2202
2203void MapSpace::PrepareForMarkCompact(bool will_compact) {
2204 // Call prepare of the super class.
2205 FixedSpace::PrepareForMarkCompact(will_compact);
2206
2207 if (will_compact) {
2208 // Initialize map index entry.
2209 int page_count = 0;
2210 PageIterator it(this, PageIterator::ALL_PAGES);
2211 while (it.has_next()) {
2212 ASSERT_MAP_PAGE_INDEX(page_count);
2213
2214 Page* p = it.next();
2215 ASSERT(p->mc_page_index == page_count);
2216
2217 page_addresses_[page_count++] = p->address();
2218 }
2219 }
2220}
2221
2222
2223#ifdef DEBUG
2224void MapSpace::VerifyObject(HeapObject* object) {
2225 // The object should be a map or a free-list node.
2226 ASSERT(object->IsMap() || object->IsByteArray());
2227}
2228#endif
2229
2230
2231// -----------------------------------------------------------------------------
2232// GlobalPropertyCellSpace implementation
2233
2234#ifdef DEBUG
2235void CellSpace::VerifyObject(HeapObject* object) {
2236 // The object should be a global object property cell or a free-list node.
2237 ASSERT(object->IsJSGlobalPropertyCell() ||
2238 object->map() == Heap::two_pointer_filler_map());
2239}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240#endif
2241
2242
2243// -----------------------------------------------------------------------------
2244// LargeObjectIterator
2245
2246LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2247 current_ = space->first_chunk_;
2248 size_func_ = NULL;
2249}
2250
2251
2252LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2253 HeapObjectCallback size_func) {
2254 current_ = space->first_chunk_;
2255 size_func_ = size_func;
2256}
2257
2258
2259HeapObject* LargeObjectIterator::next() {
2260 ASSERT(has_next());
2261 HeapObject* object = current_->GetObject();
2262 current_ = current_->next();
2263 return object;
2264}
2265
2266
2267// -----------------------------------------------------------------------------
2268// LargeObjectChunk
2269
2270LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
kasper.lund7276f142008-07-30 08:49:36 +00002271 size_t* chunk_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002272 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 size_t requested = ChunkSizeFor(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002274 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2275 chunk_size,
2276 executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 if (mem == NULL) return NULL;
2278 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2279 if (*chunk_size < requested) {
2280 MemoryAllocator::FreeRawMemory(mem, *chunk_size);
2281 LOG(DeleteEvent("LargeObjectChunk", mem));
2282 return NULL;
2283 }
2284 return reinterpret_cast<LargeObjectChunk*>(mem);
2285}
2286
2287
2288int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
2289 int os_alignment = OS::AllocateAlignment();
2290 if (os_alignment < Page::kPageSize)
2291 size_in_bytes += (Page::kPageSize - os_alignment);
2292 return size_in_bytes + Page::kObjectStartOffset;
2293}
2294
2295// -----------------------------------------------------------------------------
2296// LargeObjectSpace
2297
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002298LargeObjectSpace::LargeObjectSpace(AllocationSpace id)
2299 : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis
kasper.lund7276f142008-07-30 08:49:36 +00002300 first_chunk_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 size_(0),
2302 page_count_(0) {}
2303
2304
2305bool LargeObjectSpace::Setup() {
2306 first_chunk_ = NULL;
2307 size_ = 0;
2308 page_count_ = 0;
2309 return true;
2310}
2311
2312
2313void LargeObjectSpace::TearDown() {
2314 while (first_chunk_ != NULL) {
2315 LargeObjectChunk* chunk = first_chunk_;
2316 first_chunk_ = first_chunk_->next();
2317 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
2318 MemoryAllocator::FreeRawMemory(chunk->address(), chunk->size());
2319 }
2320
2321 size_ = 0;
2322 page_count_ = 0;
2323}
2324
2325
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00002326#ifdef ENABLE_HEAP_PROTECTION
2327
2328void LargeObjectSpace::Protect() {
2329 LargeObjectChunk* chunk = first_chunk_;
2330 while (chunk != NULL) {
2331 MemoryAllocator::Protect(chunk->address(), chunk->size());
2332 chunk = chunk->next();
2333 }
2334}
2335
2336
2337void LargeObjectSpace::Unprotect() {
2338 LargeObjectChunk* chunk = first_chunk_;
2339 while (chunk != NULL) {
2340 bool is_code = chunk->GetObject()->IsCode();
2341 MemoryAllocator::Unprotect(chunk->address(), chunk->size(),
2342 is_code ? EXECUTABLE : NOT_EXECUTABLE);
2343 chunk = chunk->next();
2344 }
2345}
2346
2347#endif
2348
2349
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002351 int object_size,
2352 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 ASSERT(0 < object_size && object_size <= requested_size);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002354
2355 // Check if we want to force a GC before growing the old space further.
2356 // If so, fail the allocation.
2357 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2358 return Failure::RetryAfterGC(requested_size, identity());
2359 }
2360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361 size_t chunk_size;
2362 LargeObjectChunk* chunk =
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002363 LargeObjectChunk::New(requested_size, &chunk_size, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 if (chunk == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00002365 return Failure::RetryAfterGC(requested_size, identity());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366 }
2367
2368 size_ += chunk_size;
2369 page_count_++;
2370 chunk->set_next(first_chunk_);
2371 chunk->set_size(chunk_size);
2372 first_chunk_ = chunk;
2373
2374 // Set the object address and size in the page header and clear its
2375 // remembered set.
2376 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2377 Address object_address = page->ObjectAreaStart();
2378 // Clear the low order bit of the second word in the page to flag it as a
2379 // large object page. If the chunk_size happened to be written there, its
2380 // low order bit should already be clear.
2381 ASSERT((chunk_size & 0x1) == 0);
2382 page->is_normal_page &= ~0x1;
2383 page->ClearRSet();
2384 int extra_bytes = requested_size - object_size;
2385 if (extra_bytes > 0) {
2386 // The extra memory for the remembered set should be cleared.
2387 memset(object_address + object_size, 0, extra_bytes);
2388 }
2389
2390 return HeapObject::FromAddress(object_address);
2391}
2392
2393
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002394Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 ASSERT(0 < size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002396 return AllocateRawInternal(size_in_bytes,
2397 size_in_bytes,
2398 EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399}
2400
2401
2402Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002403 ASSERT(0 < size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404 int extra_rset_bytes = ExtraRSetBytesFor(size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002405 return AllocateRawInternal(size_in_bytes + extra_rset_bytes,
2406 size_in_bytes,
2407 NOT_EXECUTABLE);
2408}
2409
2410
2411Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2412 ASSERT(0 < size_in_bytes);
2413 return AllocateRawInternal(size_in_bytes,
2414 size_in_bytes,
2415 NOT_EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416}
2417
2418
2419// GC support
2420Object* LargeObjectSpace::FindObject(Address a) {
2421 for (LargeObjectChunk* chunk = first_chunk_;
2422 chunk != NULL;
2423 chunk = chunk->next()) {
2424 Address chunk_address = chunk->address();
2425 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2426 return chunk->GetObject();
2427 }
2428 }
2429 return Failure::Exception();
2430}
2431
2432
2433void LargeObjectSpace::ClearRSet() {
2434 ASSERT(Page::is_rset_in_use());
2435
2436 LargeObjectIterator it(this);
2437 while (it.has_next()) {
2438 HeapObject* object = it.next();
2439 // We only have code, sequential strings, or fixed arrays in large
2440 // object space, and only fixed arrays need remembered set support.
2441 if (object->IsFixedArray()) {
2442 // Clear the normal remembered set region of the page;
2443 Page* page = Page::FromAddress(object->address());
2444 page->ClearRSet();
2445
2446 // Clear the extra remembered set.
2447 int size = object->Size();
2448 int extra_rset_bytes = ExtraRSetBytesFor(size);
2449 memset(object->address() + size, 0, extra_rset_bytes);
2450 }
2451 }
2452}
2453
2454
2455void LargeObjectSpace::IterateRSet(ObjectSlotCallback copy_object_func) {
2456 ASSERT(Page::is_rset_in_use());
2457
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002458 static void* lo_rset_histogram = StatsTable::CreateHistogram(
2459 "V8.RSetLO",
2460 0,
2461 // Keeping this histogram's buckets the same as the paged space histogram.
2462 Page::kObjectAreaSize / kPointerSize,
2463 30);
2464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465 LargeObjectIterator it(this);
2466 while (it.has_next()) {
2467 // We only have code, sequential strings, or fixed arrays in large
2468 // object space, and only fixed arrays can possibly contain pointers to
2469 // the young generation.
2470 HeapObject* object = it.next();
2471 if (object->IsFixedArray()) {
2472 // Iterate the normal page remembered set range.
2473 Page* page = Page::FromAddress(object->address());
2474 Address object_end = object->address() + object->Size();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002475 int count = Heap::IterateRSetRange(page->ObjectAreaStart(),
2476 Min(page->ObjectAreaEnd(), object_end),
2477 page->RSetStart(),
2478 copy_object_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479
2480 // Iterate the extra array elements.
2481 if (object_end > page->ObjectAreaEnd()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002482 count += Heap::IterateRSetRange(page->ObjectAreaEnd(), object_end,
2483 object_end, copy_object_func);
2484 }
2485 if (lo_rset_histogram != NULL) {
2486 StatsTable::AddHistogramSample(lo_rset_histogram, count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 }
2488 }
2489 }
2490}
2491
2492
2493void LargeObjectSpace::FreeUnmarkedObjects() {
2494 LargeObjectChunk* previous = NULL;
2495 LargeObjectChunk* current = first_chunk_;
2496 while (current != NULL) {
2497 HeapObject* object = current->GetObject();
kasper.lund7276f142008-07-30 08:49:36 +00002498 if (object->IsMarked()) {
2499 object->ClearMark();
2500 MarkCompactCollector::tracer()->decrement_marked_count();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501 previous = current;
2502 current = current->next();
2503 } else {
2504 Address chunk_address = current->address();
2505 size_t chunk_size = current->size();
2506
2507 // Cut the chunk out from the chunk list.
2508 current = current->next();
2509 if (previous == NULL) {
2510 first_chunk_ = current;
2511 } else {
2512 previous->set_next(current);
2513 }
2514
2515 // Free the chunk.
2516 if (object->IsCode()) {
2517 LOG(CodeDeleteEvent(object->address()));
2518 }
2519 size_ -= chunk_size;
2520 page_count_--;
2521 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size);
2522 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2523 }
2524 }
2525}
2526
2527
2528bool LargeObjectSpace::Contains(HeapObject* object) {
2529 Address address = object->address();
2530 Page* page = Page::FromAddress(address);
2531
2532 SLOW_ASSERT(!page->IsLargeObjectPage()
2533 || !FindObject(address)->IsFailure());
2534
2535 return page->IsLargeObjectPage();
2536}
2537
2538
2539#ifdef DEBUG
2540// We do not assume that the large object iterator works, because it depends
2541// on the invariants we are checking during verification.
2542void LargeObjectSpace::Verify() {
2543 for (LargeObjectChunk* chunk = first_chunk_;
2544 chunk != NULL;
2545 chunk = chunk->next()) {
2546 // Each chunk contains an object that starts at the large object page's
2547 // object area start.
2548 HeapObject* object = chunk->GetObject();
2549 Page* page = Page::FromAddress(object->address());
2550 ASSERT(object->address() == page->ObjectAreaStart());
2551
2552 // The first word should be a map, and we expect all map pointers to be
2553 // in map space.
2554 Map* map = object->map();
2555 ASSERT(map->IsMap());
2556 ASSERT(Heap::map_space()->Contains(map));
2557
ager@chromium.orga1645e22009-09-09 19:27:10 +00002558 // We have only code, sequential strings, external strings
2559 // (sequential strings that have been morphed into external
2560 // strings), fixed arrays, and byte arrays in large object space.
2561 ASSERT(object->IsCode() || object->IsSeqString() ||
2562 object->IsExternalString() || object->IsFixedArray() ||
2563 object->IsByteArray());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564
2565 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002566 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567
2568 // Byte arrays and strings don't have interior pointers.
2569 if (object->IsCode()) {
2570 VerifyPointersVisitor code_visitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 object->IterateBody(map->instance_type(),
2572 object->Size(),
2573 &code_visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 } else if (object->IsFixedArray()) {
2575 // We loop over fixed arrays ourselves, rather then using the visitor,
2576 // because the visitor doesn't support the start/offset iteration
2577 // needed for IsRSetSet.
2578 FixedArray* array = FixedArray::cast(object);
2579 for (int j = 0; j < array->length(); j++) {
2580 Object* element = array->get(j);
2581 if (element->IsHeapObject()) {
2582 HeapObject* element_object = HeapObject::cast(element);
2583 ASSERT(Heap::Contains(element_object));
2584 ASSERT(element_object->map()->IsMap());
2585 if (Heap::InNewSpace(element_object)) {
2586 ASSERT(Page::IsRSetSet(object->address(),
2587 FixedArray::kHeaderSize + j * kPointerSize));
2588 }
2589 }
2590 }
2591 }
2592 }
2593}
2594
2595
2596void LargeObjectSpace::Print() {
2597 LargeObjectIterator it(this);
2598 while (it.has_next()) {
2599 it.next()->Print();
2600 }
2601}
2602
2603
2604void LargeObjectSpace::ReportStatistics() {
2605 PrintF(" size: %d\n", size_);
2606 int num_objects = 0;
2607 ClearHistograms();
2608 LargeObjectIterator it(this);
2609 while (it.has_next()) {
2610 num_objects++;
2611 CollectHistogramInfo(it.next());
2612 }
2613
2614 PrintF(" number of objects %d\n", num_objects);
2615 if (num_objects > 0) ReportHistogram(false);
2616}
2617
2618
2619void LargeObjectSpace::CollectCodeStatistics() {
2620 LargeObjectIterator obj_it(this);
2621 while (obj_it.has_next()) {
2622 HeapObject* obj = obj_it.next();
2623 if (obj->IsCode()) {
2624 Code* code = Code::cast(obj);
2625 code_kind_statistics[code->kind()] += code->Size();
2626 }
2627 }
2628}
2629
2630
2631void LargeObjectSpace::PrintRSet() {
2632 LargeObjectIterator it(this);
2633 while (it.has_next()) {
2634 HeapObject* object = it.next();
2635 if (object->IsFixedArray()) {
2636 Page* page = Page::FromAddress(object->address());
2637
2638 Address allocation_top = object->address() + object->Size();
2639 PrintF("large page 0x%x:\n", page);
2640 PrintRSetRange(page->RSetStart(), page->RSetEnd(),
2641 reinterpret_cast<Object**>(object->address()),
2642 allocation_top);
2643 int extra_array_bytes = object->Size() - Page::kObjectAreaSize;
2644 int extra_rset_bits = RoundUp(extra_array_bytes / kPointerSize,
2645 kBitsPerInt);
2646 PrintF("------------------------------------------------------------"
2647 "-----------\n");
2648 PrintRSetRange(allocation_top,
2649 allocation_top + extra_rset_bits / kBitsPerByte,
2650 reinterpret_cast<Object**>(object->address()
2651 + Page::kObjectAreaSize),
2652 allocation_top);
2653 PrintF("\n");
2654 }
2655 }
2656}
2657#endif // DEBUG
2658
2659} } // namespace v8::internal