blob: 50afd0312d79a5df6d0f70acb7cd7134ebff58cb [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "macro-assembler.h"
31#include "mark-compact.h"
32#include "platform.h"
33
34namespace v8 {
35namespace internal {
36
37// 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) \
40 ASSERT((space).low() <= (info).top \
41 && (info).top <= (space).high() \
42 && (info).limit == (space).high())
43
Steve Block791712a2010-08-27 10:21:07 +010044intptr_t Page::watermark_invalidated_mark_ = 1 << Page::WATERMARK_INVALIDATED;
Steve Blocka7e24c12009-10-30 11:49:00 +000045
46// ----------------------------------------------------------------------------
47// HeapObjectIterator
48
49HeapObjectIterator::HeapObjectIterator(PagedSpace* space) {
50 Initialize(space->bottom(), space->top(), NULL);
51}
52
53
54HeapObjectIterator::HeapObjectIterator(PagedSpace* space,
55 HeapObjectCallback size_func) {
56 Initialize(space->bottom(), space->top(), size_func);
57}
58
59
60HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start) {
61 Initialize(start, space->top(), NULL);
62}
63
64
65HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start,
66 HeapObjectCallback size_func) {
67 Initialize(start, space->top(), size_func);
68}
69
70
Kristian Monsen80d68ea2010-09-08 11:05:35 +010071HeapObjectIterator::HeapObjectIterator(Page* page,
72 HeapObjectCallback size_func) {
73 Initialize(page->ObjectAreaStart(), page->AllocationTop(), size_func);
74}
75
76
Steve Blocka7e24c12009-10-30 11:49:00 +000077void HeapObjectIterator::Initialize(Address cur, Address end,
78 HeapObjectCallback size_f) {
79 cur_addr_ = cur;
80 end_addr_ = end;
81 end_page_ = Page::FromAllocationTop(end);
82 size_func_ = size_f;
83 Page* p = Page::FromAllocationTop(cur_addr_);
84 cur_limit_ = (p == end_page_) ? end_addr_ : p->AllocationTop();
85
86#ifdef DEBUG
87 Verify();
88#endif
89}
90
91
Leon Clarked91b9f72010-01-27 17:25:45 +000092HeapObject* HeapObjectIterator::FromNextPage() {
93 if (cur_addr_ == end_addr_) return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000094
95 Page* cur_page = Page::FromAllocationTop(cur_addr_);
96 cur_page = cur_page->next_page();
97 ASSERT(cur_page->is_valid());
98
99 cur_addr_ = cur_page->ObjectAreaStart();
100 cur_limit_ = (cur_page == end_page_) ? end_addr_ : cur_page->AllocationTop();
101
Leon Clarked91b9f72010-01-27 17:25:45 +0000102 if (cur_addr_ == end_addr_) return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 ASSERT(cur_addr_ < cur_limit_);
104#ifdef DEBUG
105 Verify();
106#endif
Leon Clarked91b9f72010-01-27 17:25:45 +0000107 return FromCurrentPage();
Steve Blocka7e24c12009-10-30 11:49:00 +0000108}
109
110
111#ifdef DEBUG
112void HeapObjectIterator::Verify() {
113 Page* p = Page::FromAllocationTop(cur_addr_);
114 ASSERT(p == Page::FromAllocationTop(cur_limit_));
115 ASSERT(p->Offset(cur_addr_) <= p->Offset(cur_limit_));
116}
117#endif
118
119
120// -----------------------------------------------------------------------------
121// PageIterator
122
123PageIterator::PageIterator(PagedSpace* space, Mode mode) : space_(space) {
124 prev_page_ = NULL;
125 switch (mode) {
126 case PAGES_IN_USE:
127 stop_page_ = space->AllocationTopPage();
128 break;
129 case PAGES_USED_BY_MC:
130 stop_page_ = space->MCRelocationTopPage();
131 break;
132 case ALL_PAGES:
133#ifdef DEBUG
134 // Verify that the cached last page in the space is actually the
135 // last page.
136 for (Page* p = space->first_page_; p->is_valid(); p = p->next_page()) {
137 if (!p->next_page()->is_valid()) {
138 ASSERT(space->last_page_ == p);
139 }
140 }
141#endif
142 stop_page_ = space->last_page_;
143 break;
144 }
145}
146
147
148// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000149// CodeRange
150
151List<CodeRange::FreeBlock> CodeRange::free_list_(0);
152List<CodeRange::FreeBlock> CodeRange::allocation_list_(0);
153int CodeRange::current_allocation_block_index_ = 0;
154VirtualMemory* CodeRange::code_range_ = NULL;
155
156
157bool CodeRange::Setup(const size_t requested) {
158 ASSERT(code_range_ == NULL);
159
160 code_range_ = new VirtualMemory(requested);
161 CHECK(code_range_ != NULL);
162 if (!code_range_->IsReserved()) {
163 delete code_range_;
164 code_range_ = NULL;
165 return false;
166 }
167
168 // We are sure that we have mapped a block of requested addresses.
169 ASSERT(code_range_->size() == requested);
170 LOG(NewEvent("CodeRange", code_range_->address(), requested));
171 allocation_list_.Add(FreeBlock(code_range_->address(), code_range_->size()));
172 current_allocation_block_index_ = 0;
173 return true;
174}
175
176
177int CodeRange::CompareFreeBlockAddress(const FreeBlock* left,
178 const FreeBlock* right) {
179 // The entire point of CodeRange is that the difference between two
180 // addresses in the range can be represented as a signed 32-bit int,
181 // so the cast is semantically correct.
182 return static_cast<int>(left->start - right->start);
183}
184
185
186void CodeRange::GetNextAllocationBlock(size_t requested) {
187 for (current_allocation_block_index_++;
188 current_allocation_block_index_ < allocation_list_.length();
189 current_allocation_block_index_++) {
190 if (requested <= allocation_list_[current_allocation_block_index_].size) {
191 return; // Found a large enough allocation block.
192 }
193 }
194
195 // Sort and merge the free blocks on the free list and the allocation list.
196 free_list_.AddAll(allocation_list_);
197 allocation_list_.Clear();
198 free_list_.Sort(&CompareFreeBlockAddress);
199 for (int i = 0; i < free_list_.length();) {
200 FreeBlock merged = free_list_[i];
201 i++;
202 // Add adjacent free blocks to the current merged block.
203 while (i < free_list_.length() &&
204 free_list_[i].start == merged.start + merged.size) {
205 merged.size += free_list_[i].size;
206 i++;
207 }
208 if (merged.size > 0) {
209 allocation_list_.Add(merged);
210 }
211 }
212 free_list_.Clear();
213
214 for (current_allocation_block_index_ = 0;
215 current_allocation_block_index_ < allocation_list_.length();
216 current_allocation_block_index_++) {
217 if (requested <= allocation_list_[current_allocation_block_index_].size) {
218 return; // Found a large enough allocation block.
219 }
220 }
221
222 // Code range is full or too fragmented.
223 V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock");
224}
225
226
227
228void* CodeRange::AllocateRawMemory(const size_t requested, size_t* allocated) {
229 ASSERT(current_allocation_block_index_ < allocation_list_.length());
230 if (requested > allocation_list_[current_allocation_block_index_].size) {
231 // Find an allocation block large enough. This function call may
232 // call V8::FatalProcessOutOfMemory if it cannot find a large enough block.
233 GetNextAllocationBlock(requested);
234 }
235 // Commit the requested memory at the start of the current allocation block.
236 *allocated = RoundUp(requested, Page::kPageSize);
237 FreeBlock current = allocation_list_[current_allocation_block_index_];
238 if (*allocated >= current.size - Page::kPageSize) {
239 // Don't leave a small free block, useless for a large object or chunk.
240 *allocated = current.size;
241 }
242 ASSERT(*allocated <= current.size);
243 if (!code_range_->Commit(current.start, *allocated, true)) {
244 *allocated = 0;
245 return NULL;
246 }
247 allocation_list_[current_allocation_block_index_].start += *allocated;
248 allocation_list_[current_allocation_block_index_].size -= *allocated;
249 if (*allocated == current.size) {
250 GetNextAllocationBlock(0); // This block is used up, get the next one.
251 }
252 return current.start;
253}
254
255
256void CodeRange::FreeRawMemory(void* address, size_t length) {
257 free_list_.Add(FreeBlock(address, length));
258 code_range_->Uncommit(address, length);
259}
260
261
262void CodeRange::TearDown() {
263 delete code_range_; // Frees all memory in the virtual memory range.
264 code_range_ = NULL;
265 free_list_.Free();
266 allocation_list_.Free();
267}
268
269
270// -----------------------------------------------------------------------------
271// MemoryAllocator
272//
273int MemoryAllocator::capacity_ = 0;
274int MemoryAllocator::size_ = 0;
Steve Block791712a2010-08-27 10:21:07 +0100275int MemoryAllocator::size_executable_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000276
277VirtualMemory* MemoryAllocator::initial_chunk_ = NULL;
278
279// 270 is an estimate based on the static default heap size of a pair of 256K
280// semispaces and a 64M old generation.
281const int kEstimatedNumberOfChunks = 270;
282List<MemoryAllocator::ChunkInfo> MemoryAllocator::chunks_(
283 kEstimatedNumberOfChunks);
284List<int> MemoryAllocator::free_chunk_ids_(kEstimatedNumberOfChunks);
285int MemoryAllocator::max_nof_chunks_ = 0;
286int MemoryAllocator::top_ = 0;
287
288
289void MemoryAllocator::Push(int free_chunk_id) {
290 ASSERT(max_nof_chunks_ > 0);
291 ASSERT(top_ < max_nof_chunks_);
292 free_chunk_ids_[top_++] = free_chunk_id;
293}
294
295
296int MemoryAllocator::Pop() {
297 ASSERT(top_ > 0);
298 return free_chunk_ids_[--top_];
299}
300
301
Steve Block791712a2010-08-27 10:21:07 +0100302void *executable_memory_histogram = NULL;
303
Steve Blocka7e24c12009-10-30 11:49:00 +0000304bool MemoryAllocator::Setup(int capacity) {
305 capacity_ = RoundUp(capacity, Page::kPageSize);
306
307 // Over-estimate the size of chunks_ array. It assumes the expansion of old
308 // space is always in the unit of a chunk (kChunkSize) except the last
309 // expansion.
310 //
311 // Due to alignment, allocated space might be one page less than required
312 // number (kPagesPerChunk) of pages for old spaces.
313 //
314 // Reserve two chunk ids for semispaces, one for map space, one for old
315 // space, and one for code space.
316 max_nof_chunks_ = (capacity_ / (kChunkSize - Page::kPageSize)) + 5;
317 if (max_nof_chunks_ > kMaxNofChunks) return false;
318
319 size_ = 0;
Steve Block791712a2010-08-27 10:21:07 +0100320 size_executable_ = 0;
321 executable_memory_histogram =
322 StatsTable::CreateHistogram("V8.ExecutableMemoryMax", 0, MB * 512, 50);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 ChunkInfo info; // uninitialized element.
324 for (int i = max_nof_chunks_ - 1; i >= 0; i--) {
325 chunks_.Add(info);
326 free_chunk_ids_.Add(i);
327 }
328 top_ = max_nof_chunks_;
329 return true;
330}
331
332
333void MemoryAllocator::TearDown() {
334 for (int i = 0; i < max_nof_chunks_; i++) {
335 if (chunks_[i].address() != NULL) DeleteChunk(i);
336 }
337 chunks_.Clear();
338 free_chunk_ids_.Clear();
339
340 if (initial_chunk_ != NULL) {
341 LOG(DeleteEvent("InitialChunk", initial_chunk_->address()));
342 delete initial_chunk_;
343 initial_chunk_ = NULL;
344 }
345
346 ASSERT(top_ == max_nof_chunks_); // all chunks are free
347 top_ = 0;
348 capacity_ = 0;
349 size_ = 0;
350 max_nof_chunks_ = 0;
351}
352
353
354void* MemoryAllocator::AllocateRawMemory(const size_t requested,
355 size_t* allocated,
356 Executability executable) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100357 if (size_ + static_cast<size_t>(requested) > static_cast<size_t>(capacity_)) {
358 return NULL;
359 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 void* mem;
361 if (executable == EXECUTABLE && CodeRange::exists()) {
362 mem = CodeRange::AllocateRawMemory(requested, allocated);
363 } else {
364 mem = OS::Allocate(requested, allocated, (executable == EXECUTABLE));
365 }
Steve Blockd0582a62009-12-15 09:54:21 +0000366 int alloced = static_cast<int>(*allocated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 size_ += alloced;
Steve Block791712a2010-08-27 10:21:07 +0100368
369 if (executable == EXECUTABLE) {
370 size_executable_ += alloced;
371 static int size_executable_max_observed_ = 0;
372 if (size_executable_max_observed_ < size_executable_) {
373 size_executable_max_observed_ = size_executable_;
374 StatsTable::AddHistogramSample(executable_memory_histogram,
375 size_executable_);
376 }
377 }
Leon Clarke4515c472010-02-03 11:58:03 +0000378#ifdef DEBUG
379 ZapBlock(reinterpret_cast<Address>(mem), alloced);
380#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 Counters::memory_allocated.Increment(alloced);
382 return mem;
383}
384
385
Steve Block791712a2010-08-27 10:21:07 +0100386void MemoryAllocator::FreeRawMemory(void* mem,
387 size_t length,
388 Executability executable) {
Leon Clarke4515c472010-02-03 11:58:03 +0000389#ifdef DEBUG
390 ZapBlock(reinterpret_cast<Address>(mem), length);
391#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 if (CodeRange::contains(static_cast<Address>(mem))) {
393 CodeRange::FreeRawMemory(mem, length);
394 } else {
395 OS::Free(mem, length);
396 }
Steve Blockd0582a62009-12-15 09:54:21 +0000397 Counters::memory_allocated.Decrement(static_cast<int>(length));
398 size_ -= static_cast<int>(length);
Steve Block791712a2010-08-27 10:21:07 +0100399 if (executable == EXECUTABLE) size_executable_ -= static_cast<int>(length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 ASSERT(size_ >= 0);
401}
402
403
404void* MemoryAllocator::ReserveInitialChunk(const size_t requested) {
405 ASSERT(initial_chunk_ == NULL);
406
407 initial_chunk_ = new VirtualMemory(requested);
408 CHECK(initial_chunk_ != NULL);
409 if (!initial_chunk_->IsReserved()) {
410 delete initial_chunk_;
411 initial_chunk_ = NULL;
412 return NULL;
413 }
414
415 // We are sure that we have mapped a block of requested addresses.
416 ASSERT(initial_chunk_->size() == requested);
417 LOG(NewEvent("InitialChunk", initial_chunk_->address(), requested));
Steve Blockd0582a62009-12-15 09:54:21 +0000418 size_ += static_cast<int>(requested);
Steve Blocka7e24c12009-10-30 11:49:00 +0000419 return initial_chunk_->address();
420}
421
422
423static int PagesInChunk(Address start, size_t size) {
424 // The first page starts on the first page-aligned address from start onward
425 // and the last page ends on the last page-aligned address before
426 // start+size. Page::kPageSize is a power of two so we can divide by
427 // shifting.
Steve Blockd0582a62009-12-15 09:54:21 +0000428 return static_cast<int>((RoundDown(start + size, Page::kPageSize)
Leon Clarkee46be812010-01-19 14:06:41 +0000429 - RoundUp(start, Page::kPageSize)) >> kPageSizeBits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000430}
431
432
433Page* MemoryAllocator::AllocatePages(int requested_pages, int* allocated_pages,
434 PagedSpace* owner) {
435 if (requested_pages <= 0) return Page::FromAddress(NULL);
436 size_t chunk_size = requested_pages * Page::kPageSize;
437
438 // There is not enough space to guarantee the desired number pages can be
439 // allocated.
440 if (size_ + static_cast<int>(chunk_size) > capacity_) {
441 // Request as many pages as we can.
442 chunk_size = capacity_ - size_;
Leon Clarkee46be812010-01-19 14:06:41 +0000443 requested_pages = static_cast<int>(chunk_size >> kPageSizeBits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000444
445 if (requested_pages <= 0) return Page::FromAddress(NULL);
446 }
447 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
448 if (chunk == NULL) return Page::FromAddress(NULL);
449 LOG(NewEvent("PagedChunk", chunk, chunk_size));
450
451 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
452 if (*allocated_pages == 0) {
Steve Block791712a2010-08-27 10:21:07 +0100453 FreeRawMemory(chunk, chunk_size, owner->executable());
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 LOG(DeleteEvent("PagedChunk", chunk));
455 return Page::FromAddress(NULL);
456 }
457
458 int chunk_id = Pop();
459 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
460
461 return InitializePagesInChunk(chunk_id, *allocated_pages, owner);
462}
463
464
465Page* MemoryAllocator::CommitPages(Address start, size_t size,
466 PagedSpace* owner, int* num_pages) {
467 ASSERT(start != NULL);
468 *num_pages = PagesInChunk(start, size);
469 ASSERT(*num_pages > 0);
470 ASSERT(initial_chunk_ != NULL);
471 ASSERT(InInitialChunk(start));
472 ASSERT(InInitialChunk(start + size - 1));
473 if (!initial_chunk_->Commit(start, size, owner->executable() == EXECUTABLE)) {
474 return Page::FromAddress(NULL);
475 }
Leon Clarke4515c472010-02-03 11:58:03 +0000476#ifdef DEBUG
477 ZapBlock(start, size);
478#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000479 Counters::memory_allocated.Increment(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000480
481 // So long as we correctly overestimated the number of chunks we should not
482 // run out of chunk ids.
483 CHECK(!OutOfChunkIds());
484 int chunk_id = Pop();
485 chunks_[chunk_id].init(start, size, owner);
486 return InitializePagesInChunk(chunk_id, *num_pages, owner);
487}
488
489
490bool MemoryAllocator::CommitBlock(Address start,
491 size_t size,
492 Executability executable) {
493 ASSERT(start != NULL);
494 ASSERT(size > 0);
495 ASSERT(initial_chunk_ != NULL);
496 ASSERT(InInitialChunk(start));
497 ASSERT(InInitialChunk(start + size - 1));
498
499 if (!initial_chunk_->Commit(start, size, executable)) return false;
Leon Clarke4515c472010-02-03 11:58:03 +0000500#ifdef DEBUG
501 ZapBlock(start, size);
502#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000503 Counters::memory_allocated.Increment(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 return true;
505}
506
Leon Clarke4515c472010-02-03 11:58:03 +0000507
Steve Blocka7e24c12009-10-30 11:49:00 +0000508bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
509 ASSERT(start != NULL);
510 ASSERT(size > 0);
511 ASSERT(initial_chunk_ != NULL);
512 ASSERT(InInitialChunk(start));
513 ASSERT(InInitialChunk(start + size - 1));
514
515 if (!initial_chunk_->Uncommit(start, size)) return false;
Steve Blockd0582a62009-12-15 09:54:21 +0000516 Counters::memory_allocated.Decrement(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 return true;
518}
519
Leon Clarke4515c472010-02-03 11:58:03 +0000520
521void MemoryAllocator::ZapBlock(Address start, size_t size) {
522 for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) {
523 Memory::Address_at(start + s) = kZapValue;
524 }
525}
526
527
Steve Blocka7e24c12009-10-30 11:49:00 +0000528Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
529 PagedSpace* owner) {
530 ASSERT(IsValidChunk(chunk_id));
531 ASSERT(pages_in_chunk > 0);
532
533 Address chunk_start = chunks_[chunk_id].address();
534
535 Address low = RoundUp(chunk_start, Page::kPageSize);
536
537#ifdef DEBUG
538 size_t chunk_size = chunks_[chunk_id].size();
539 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
540 ASSERT(pages_in_chunk <=
541 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
542#endif
543
544 Address page_addr = low;
545 for (int i = 0; i < pages_in_chunk; i++) {
546 Page* p = Page::FromAddress(page_addr);
547 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100548 p->InvalidateWatermark(true);
Steve Block6ded16b2010-05-10 14:33:55 +0100549 p->SetIsLargeObjectPage(false);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100550 p->SetAllocationWatermark(p->ObjectAreaStart());
551 p->SetCachedAllocationWatermark(p->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 page_addr += Page::kPageSize;
553 }
554
555 // Set the next page of the last page to 0.
556 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
557 last_page->opaque_header = OffsetFrom(0) | chunk_id;
558
559 return Page::FromAddress(low);
560}
561
562
563Page* MemoryAllocator::FreePages(Page* p) {
564 if (!p->is_valid()) return p;
565
566 // Find the first page in the same chunk as 'p'
567 Page* first_page = FindFirstPageInSameChunk(p);
568 Page* page_to_return = Page::FromAddress(NULL);
569
570 if (p != first_page) {
571 // Find the last page in the same chunk as 'prev'.
572 Page* last_page = FindLastPageInSameChunk(p);
573 first_page = GetNextPage(last_page); // first page in next chunk
574
575 // set the next_page of last_page to NULL
576 SetNextPage(last_page, Page::FromAddress(NULL));
577 page_to_return = p; // return 'p' when exiting
578 }
579
580 while (first_page->is_valid()) {
581 int chunk_id = GetChunkId(first_page);
582 ASSERT(IsValidChunk(chunk_id));
583
584 // Find the first page of the next chunk before deleting this chunk.
585 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
586
587 // Free the current chunk.
588 DeleteChunk(chunk_id);
589 }
590
591 return page_to_return;
592}
593
594
Steve Block6ded16b2010-05-10 14:33:55 +0100595void MemoryAllocator::FreeAllPages(PagedSpace* space) {
596 for (int i = 0, length = chunks_.length(); i < length; i++) {
597 if (chunks_[i].owner() == space) {
598 DeleteChunk(i);
599 }
600 }
601}
602
603
Steve Blocka7e24c12009-10-30 11:49:00 +0000604void MemoryAllocator::DeleteChunk(int chunk_id) {
605 ASSERT(IsValidChunk(chunk_id));
606
607 ChunkInfo& c = chunks_[chunk_id];
608
609 // We cannot free a chunk contained in the initial chunk because it was not
610 // allocated with AllocateRawMemory. Instead we uncommit the virtual
611 // memory.
612 if (InInitialChunk(c.address())) {
613 // TODO(1240712): VirtualMemory::Uncommit has a return value which
614 // is ignored here.
615 initial_chunk_->Uncommit(c.address(), c.size());
Steve Blockd0582a62009-12-15 09:54:21 +0000616 Counters::memory_allocated.Decrement(static_cast<int>(c.size()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 } else {
618 LOG(DeleteEvent("PagedChunk", c.address()));
Steve Block791712a2010-08-27 10:21:07 +0100619 FreeRawMemory(c.address(), c.size(), c.owner()->executable());
Steve Blocka7e24c12009-10-30 11:49:00 +0000620 }
621 c.init(NULL, 0, NULL);
622 Push(chunk_id);
623}
624
625
626Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
627 int chunk_id = GetChunkId(p);
628 ASSERT(IsValidChunk(chunk_id));
629
630 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
631 return Page::FromAddress(low);
632}
633
634
635Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
636 int chunk_id = GetChunkId(p);
637 ASSERT(IsValidChunk(chunk_id));
638
639 Address chunk_start = chunks_[chunk_id].address();
640 size_t chunk_size = chunks_[chunk_id].size();
641
642 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
643 ASSERT(chunk_start <= p->address() && p->address() < high);
644
645 return Page::FromAddress(high - Page::kPageSize);
646}
647
648
649#ifdef DEBUG
650void MemoryAllocator::ReportStatistics() {
651 float pct = static_cast<float>(capacity_ - size_) / capacity_;
652 PrintF(" capacity: %d, used: %d, available: %%%d\n\n",
653 capacity_, size_, static_cast<int>(pct*100));
654}
655#endif
656
657
Steve Block6ded16b2010-05-10 14:33:55 +0100658void MemoryAllocator::RelinkPageListInChunkOrder(PagedSpace* space,
659 Page** first_page,
660 Page** last_page,
661 Page** last_page_in_use) {
662 Page* first = NULL;
663 Page* last = NULL;
664
665 for (int i = 0, length = chunks_.length(); i < length; i++) {
666 ChunkInfo& chunk = chunks_[i];
667
668 if (chunk.owner() == space) {
669 if (first == NULL) {
670 Address low = RoundUp(chunk.address(), Page::kPageSize);
671 first = Page::FromAddress(low);
672 }
673 last = RelinkPagesInChunk(i,
674 chunk.address(),
675 chunk.size(),
676 last,
677 last_page_in_use);
678 }
679 }
680
681 if (first_page != NULL) {
682 *first_page = first;
683 }
684
685 if (last_page != NULL) {
686 *last_page = last;
687 }
688}
689
690
691Page* MemoryAllocator::RelinkPagesInChunk(int chunk_id,
692 Address chunk_start,
693 size_t chunk_size,
694 Page* prev,
695 Page** last_page_in_use) {
696 Address page_addr = RoundUp(chunk_start, Page::kPageSize);
697 int pages_in_chunk = PagesInChunk(chunk_start, chunk_size);
698
699 if (prev->is_valid()) {
700 SetNextPage(prev, Page::FromAddress(page_addr));
701 }
702
703 for (int i = 0; i < pages_in_chunk; i++) {
704 Page* p = Page::FromAddress(page_addr);
705 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
706 page_addr += Page::kPageSize;
707
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100708 p->InvalidateWatermark(true);
Steve Block6ded16b2010-05-10 14:33:55 +0100709 if (p->WasInUseBeforeMC()) {
710 *last_page_in_use = p;
711 }
712 }
713
714 // Set the next page of the last page to 0.
715 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
716 last_page->opaque_header = OffsetFrom(0) | chunk_id;
717
718 if (last_page->WasInUseBeforeMC()) {
719 *last_page_in_use = last_page;
720 }
721
722 return last_page;
723}
724
725
726
Steve Blocka7e24c12009-10-30 11:49:00 +0000727// -----------------------------------------------------------------------------
728// PagedSpace implementation
729
730PagedSpace::PagedSpace(int max_capacity,
731 AllocationSpace id,
732 Executability executable)
733 : Space(id, executable) {
734 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
735 * Page::kObjectAreaSize;
736 accounting_stats_.Clear();
737
738 allocation_info_.top = NULL;
739 allocation_info_.limit = NULL;
740
741 mc_forwarding_info_.top = NULL;
742 mc_forwarding_info_.limit = NULL;
743}
744
745
746bool PagedSpace::Setup(Address start, size_t size) {
747 if (HasBeenSetup()) return false;
748
749 int num_pages = 0;
750 // Try to use the virtual memory range passed to us. If it is too small to
751 // contain at least one page, ignore it and allocate instead.
752 int pages_in_chunk = PagesInChunk(start, size);
753 if (pages_in_chunk > 0) {
754 first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize),
755 Page::kPageSize * pages_in_chunk,
756 this, &num_pages);
757 } else {
758 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
759 max_capacity_ / Page::kObjectAreaSize);
760 first_page_ =
761 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
762 if (!first_page_->is_valid()) return false;
763 }
764
765 // We are sure that the first page is valid and that we have at least one
766 // page.
767 ASSERT(first_page_->is_valid());
768 ASSERT(num_pages > 0);
769 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
770 ASSERT(Capacity() <= max_capacity_);
771
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100772 // Sequentially clear region marks in the newly allocated
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 // pages and cache the current last page in the space.
774 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100775 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000776 last_page_ = p;
777 }
778
779 // Use first_page_ for allocation.
780 SetAllocationInfo(&allocation_info_, first_page_);
781
Steve Block6ded16b2010-05-10 14:33:55 +0100782 page_list_is_chunk_ordered_ = true;
783
Steve Blocka7e24c12009-10-30 11:49:00 +0000784 return true;
785}
786
787
788bool PagedSpace::HasBeenSetup() {
789 return (Capacity() > 0);
790}
791
792
793void PagedSpace::TearDown() {
Steve Block6ded16b2010-05-10 14:33:55 +0100794 MemoryAllocator::FreeAllPages(this);
795 first_page_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 accounting_stats_.Clear();
797}
798
799
800#ifdef ENABLE_HEAP_PROTECTION
801
802void PagedSpace::Protect() {
803 Page* page = first_page_;
804 while (page->is_valid()) {
805 MemoryAllocator::ProtectChunkFromPage(page);
806 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
807 }
808}
809
810
811void PagedSpace::Unprotect() {
812 Page* page = first_page_;
813 while (page->is_valid()) {
814 MemoryAllocator::UnprotectChunkFromPage(page);
815 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
816 }
817}
818
819#endif
820
821
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100822void PagedSpace::MarkAllPagesClean() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000823 PageIterator it(this, PageIterator::ALL_PAGES);
824 while (it.has_next()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100825 it.next()->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000826 }
827}
828
829
830Object* PagedSpace::FindObject(Address addr) {
831 // Note: this function can only be called before or after mark-compact GC
832 // because it accesses map pointers.
833 ASSERT(!MarkCompactCollector::in_use());
834
835 if (!Contains(addr)) return Failure::Exception();
836
837 Page* p = Page::FromAddress(addr);
838 ASSERT(IsUsed(p));
839 Address cur = p->ObjectAreaStart();
840 Address end = p->AllocationTop();
841 while (cur < end) {
842 HeapObject* obj = HeapObject::FromAddress(cur);
843 Address next = cur + obj->Size();
844 if ((cur <= addr) && (addr < next)) return obj;
845 cur = next;
846 }
847
848 UNREACHABLE();
849 return Failure::Exception();
850}
851
852
853bool PagedSpace::IsUsed(Page* page) {
854 PageIterator it(this, PageIterator::PAGES_IN_USE);
855 while (it.has_next()) {
856 if (page == it.next()) return true;
857 }
858 return false;
859}
860
861
862void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
863 alloc_info->top = p->ObjectAreaStart();
864 alloc_info->limit = p->ObjectAreaEnd();
865 ASSERT(alloc_info->VerifyPagedAllocation());
866}
867
868
869void PagedSpace::MCResetRelocationInfo() {
870 // Set page indexes.
871 int i = 0;
872 PageIterator it(this, PageIterator::ALL_PAGES);
873 while (it.has_next()) {
874 Page* p = it.next();
875 p->mc_page_index = i++;
876 }
877
878 // Set mc_forwarding_info_ to the first page in the space.
879 SetAllocationInfo(&mc_forwarding_info_, first_page_);
880 // All the bytes in the space are 'available'. We will rediscover
881 // allocated and wasted bytes during GC.
882 accounting_stats_.Reset();
883}
884
885
886int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
887#ifdef DEBUG
888 // The Contains function considers the address at the beginning of a
889 // page in the page, MCSpaceOffsetForAddress considers it is in the
890 // previous page.
891 if (Page::IsAlignedToPageSize(addr)) {
892 ASSERT(Contains(addr - kPointerSize));
893 } else {
894 ASSERT(Contains(addr));
895 }
896#endif
897
898 // If addr is at the end of a page, it belongs to previous page
899 Page* p = Page::IsAlignedToPageSize(addr)
900 ? Page::FromAllocationTop(addr)
901 : Page::FromAddress(addr);
902 int index = p->mc_page_index;
903 return (index * Page::kPageSize) + p->Offset(addr);
904}
905
906
907// Slow case for reallocating and promoting objects during a compacting
908// collection. This function is not space-specific.
909HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
910 Page* current_page = TopPageOf(mc_forwarding_info_);
911 if (!current_page->next_page()->is_valid()) {
912 if (!Expand(current_page)) {
913 return NULL;
914 }
915 }
916
917 // There are surely more pages in the space now.
918 ASSERT(current_page->next_page()->is_valid());
919 // We do not add the top of page block for current page to the space's
920 // free list---the block may contain live objects so we cannot write
921 // bookkeeping information to it. Instead, we will recover top of page
922 // blocks when we move objects to their new locations.
923 //
924 // We do however write the allocation pointer to the page. The encoding
925 // of forwarding addresses is as an offset in terms of live bytes, so we
926 // need quick access to the allocation top of each page to decode
927 // forwarding addresses.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100928 current_page->SetAllocationWatermark(mc_forwarding_info_.top);
929 current_page->next_page()->InvalidateWatermark(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000930 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
931 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
932}
933
934
935bool PagedSpace::Expand(Page* last_page) {
936 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
937 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
938
939 if (Capacity() == max_capacity_) return false;
940
941 ASSERT(Capacity() < max_capacity_);
942 // Last page must be valid and its next page is invalid.
943 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
944
945 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
946 if (available_pages <= 0) return false;
947
948 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
949 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
950 if (!p->is_valid()) return false;
951
952 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
953 ASSERT(Capacity() <= max_capacity_);
954
955 MemoryAllocator::SetNextPage(last_page, p);
956
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100957 // Sequentially clear region marks of new pages and and cache the
Steve Blocka7e24c12009-10-30 11:49:00 +0000958 // new last page in the space.
959 while (p->is_valid()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100960 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000961 last_page_ = p;
962 p = p->next_page();
963 }
964
965 return true;
966}
967
968
969#ifdef DEBUG
970int PagedSpace::CountTotalPages() {
971 int count = 0;
972 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
973 count++;
974 }
975 return count;
976}
977#endif
978
979
980void PagedSpace::Shrink() {
Steve Block6ded16b2010-05-10 14:33:55 +0100981 if (!page_list_is_chunk_ordered_) {
982 // We can't shrink space if pages is not chunk-ordered
983 // (see comment for class MemoryAllocator for definition).
984 return;
985 }
986
Steve Blocka7e24c12009-10-30 11:49:00 +0000987 // Release half of free pages.
988 Page* top_page = AllocationTopPage();
989 ASSERT(top_page->is_valid());
990
991 // Count the number of pages we would like to free.
992 int pages_to_free = 0;
993 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
994 pages_to_free++;
995 }
996
997 // Free pages after top_page.
998 Page* p = MemoryAllocator::FreePages(top_page->next_page());
999 MemoryAllocator::SetNextPage(top_page, p);
1000
1001 // Find out how many pages we failed to free and update last_page_.
1002 // Please note pages can only be freed in whole chunks.
1003 last_page_ = top_page;
1004 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
1005 pages_to_free--;
1006 last_page_ = p;
1007 }
1008
1009 accounting_stats_.ShrinkSpace(pages_to_free * Page::kObjectAreaSize);
1010 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
1011}
1012
1013
1014bool PagedSpace::EnsureCapacity(int capacity) {
1015 if (Capacity() >= capacity) return true;
1016
1017 // Start from the allocation top and loop to the last page in the space.
1018 Page* last_page = AllocationTopPage();
1019 Page* next_page = last_page->next_page();
1020 while (next_page->is_valid()) {
1021 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
1022 next_page = last_page->next_page();
1023 }
1024
1025 // Expand the space until it has the required capacity or expansion fails.
1026 do {
1027 if (!Expand(last_page)) return false;
1028 ASSERT(last_page->next_page()->is_valid());
1029 last_page =
1030 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
1031 } while (Capacity() < capacity);
1032
1033 return true;
1034}
1035
1036
1037#ifdef DEBUG
1038void PagedSpace::Print() { }
1039#endif
1040
1041
1042#ifdef DEBUG
1043// We do not assume that the PageIterator works, because it depends on the
1044// invariants we are checking during verification.
1045void PagedSpace::Verify(ObjectVisitor* visitor) {
1046 // The allocation pointer should be valid, and it should be in a page in the
1047 // space.
1048 ASSERT(allocation_info_.VerifyPagedAllocation());
1049 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
1050 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
1051
1052 // Loop over all the pages.
1053 bool above_allocation_top = false;
1054 Page* current_page = first_page_;
1055 while (current_page->is_valid()) {
1056 if (above_allocation_top) {
1057 // We don't care what's above the allocation top.
1058 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 Address top = current_page->AllocationTop();
1060 if (current_page == top_page) {
1061 ASSERT(top == allocation_info_.top);
1062 // The next page will be above the allocation top.
1063 above_allocation_top = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001064 }
1065
1066 // It should be packed with objects from the bottom to the top.
1067 Address current = current_page->ObjectAreaStart();
1068 while (current < top) {
1069 HeapObject* object = HeapObject::FromAddress(current);
1070
1071 // The first word should be a map, and we expect all map pointers to
1072 // be in map space.
1073 Map* map = object->map();
1074 ASSERT(map->IsMap());
1075 ASSERT(Heap::map_space()->Contains(map));
1076
1077 // Perform space-specific object verification.
1078 VerifyObject(object);
1079
1080 // The object itself should look OK.
1081 object->Verify();
1082
1083 // All the interior pointers should be contained in the heap and
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001084 // have page regions covering intergenerational references should be
1085 // marked dirty.
Steve Blocka7e24c12009-10-30 11:49:00 +00001086 int size = object->Size();
1087 object->IterateBody(map->instance_type(), size, visitor);
1088
1089 current += size;
1090 }
1091
1092 // The allocation pointer should not be in the middle of an object.
1093 ASSERT(current == top);
1094 }
1095
1096 current_page = current_page->next_page();
1097 }
1098}
1099#endif
1100
1101
1102// -----------------------------------------------------------------------------
1103// NewSpace implementation
1104
1105
1106bool NewSpace::Setup(Address start, int size) {
1107 // Setup new space based on the preallocated memory block defined by
1108 // start and size. The provided space is divided into two semi-spaces.
1109 // To support fast containment testing in the new space, the size of
1110 // this chunk must be a power of two and it must be aligned to its size.
1111 int initial_semispace_capacity = Heap::InitialSemiSpaceSize();
Steve Block3ce2e202009-11-05 08:53:23 +00001112 int maximum_semispace_capacity = Heap::MaxSemiSpaceSize();
Steve Blocka7e24c12009-10-30 11:49:00 +00001113
1114 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
1115 ASSERT(IsPowerOf2(maximum_semispace_capacity));
1116
1117 // Allocate and setup the histogram arrays if necessary.
1118#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1119 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1120 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1121
1122#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
1123 promoted_histogram_[name].set_name(#name);
1124 INSTANCE_TYPE_LIST(SET_NAME)
1125#undef SET_NAME
1126#endif
1127
Steve Block3ce2e202009-11-05 08:53:23 +00001128 ASSERT(size == 2 * Heap::ReservedSemiSpaceSize());
Steve Blocka7e24c12009-10-30 11:49:00 +00001129 ASSERT(IsAddressAligned(start, size, 0));
1130
1131 if (!to_space_.Setup(start,
1132 initial_semispace_capacity,
1133 maximum_semispace_capacity)) {
1134 return false;
1135 }
1136 if (!from_space_.Setup(start + maximum_semispace_capacity,
1137 initial_semispace_capacity,
1138 maximum_semispace_capacity)) {
1139 return false;
1140 }
1141
1142 start_ = start;
1143 address_mask_ = ~(size - 1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001144 object_mask_ = address_mask_ | kHeapObjectTagMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00001145 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
1146
1147 allocation_info_.top = to_space_.low();
1148 allocation_info_.limit = to_space_.high();
1149 mc_forwarding_info_.top = NULL;
1150 mc_forwarding_info_.limit = NULL;
1151
1152 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1153 return true;
1154}
1155
1156
1157void NewSpace::TearDown() {
1158#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1159 if (allocated_histogram_) {
1160 DeleteArray(allocated_histogram_);
1161 allocated_histogram_ = NULL;
1162 }
1163 if (promoted_histogram_) {
1164 DeleteArray(promoted_histogram_);
1165 promoted_histogram_ = NULL;
1166 }
1167#endif
1168
1169 start_ = NULL;
1170 allocation_info_.top = NULL;
1171 allocation_info_.limit = NULL;
1172 mc_forwarding_info_.top = NULL;
1173 mc_forwarding_info_.limit = NULL;
1174
1175 to_space_.TearDown();
1176 from_space_.TearDown();
1177}
1178
1179
1180#ifdef ENABLE_HEAP_PROTECTION
1181
1182void NewSpace::Protect() {
1183 MemoryAllocator::Protect(ToSpaceLow(), Capacity());
1184 MemoryAllocator::Protect(FromSpaceLow(), Capacity());
1185}
1186
1187
1188void NewSpace::Unprotect() {
1189 MemoryAllocator::Unprotect(ToSpaceLow(), Capacity(),
1190 to_space_.executable());
1191 MemoryAllocator::Unprotect(FromSpaceLow(), Capacity(),
1192 from_space_.executable());
1193}
1194
1195#endif
1196
1197
1198void NewSpace::Flip() {
1199 SemiSpace tmp = from_space_;
1200 from_space_ = to_space_;
1201 to_space_ = tmp;
1202}
1203
1204
1205void NewSpace::Grow() {
1206 ASSERT(Capacity() < MaximumCapacity());
1207 if (to_space_.Grow()) {
1208 // Only grow from space if we managed to grow to space.
1209 if (!from_space_.Grow()) {
1210 // If we managed to grow to space but couldn't grow from space,
1211 // attempt to shrink to space.
1212 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
1213 // We are in an inconsistent state because we could not
1214 // commit/uncommit memory from new space.
1215 V8::FatalProcessOutOfMemory("Failed to grow new space.");
1216 }
1217 }
1218 }
1219 allocation_info_.limit = to_space_.high();
1220 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1221}
1222
1223
1224void NewSpace::Shrink() {
1225 int new_capacity = Max(InitialCapacity(), 2 * Size());
Steve Blockd0582a62009-12-15 09:54:21 +00001226 int rounded_new_capacity =
1227 RoundUp(new_capacity, static_cast<int>(OS::AllocateAlignment()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 if (rounded_new_capacity < Capacity() &&
1229 to_space_.ShrinkTo(rounded_new_capacity)) {
1230 // Only shrink from space if we managed to shrink to space.
1231 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
1232 // If we managed to shrink to space but couldn't shrink from
1233 // space, attempt to grow to space again.
1234 if (!to_space_.GrowTo(from_space_.Capacity())) {
1235 // We are in an inconsistent state because we could not
1236 // commit/uncommit memory from new space.
1237 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
1238 }
1239 }
1240 }
1241 allocation_info_.limit = to_space_.high();
1242 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1243}
1244
1245
1246void NewSpace::ResetAllocationInfo() {
1247 allocation_info_.top = to_space_.low();
1248 allocation_info_.limit = to_space_.high();
1249 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1250}
1251
1252
1253void NewSpace::MCResetRelocationInfo() {
1254 mc_forwarding_info_.top = from_space_.low();
1255 mc_forwarding_info_.limit = from_space_.high();
1256 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
1257}
1258
1259
1260void NewSpace::MCCommitRelocationInfo() {
1261 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
1262 // valid allocation info for the to space.
1263 allocation_info_.top = mc_forwarding_info_.top;
1264 allocation_info_.limit = to_space_.high();
1265 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1266}
1267
1268
1269#ifdef DEBUG
1270// We do not use the SemispaceIterator because verification doesn't assume
1271// that it works (it depends on the invariants we are checking).
1272void NewSpace::Verify() {
1273 // The allocation pointer should be in the space or at the very end.
1274 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1275
1276 // There should be objects packed in from the low address up to the
1277 // allocation pointer.
1278 Address current = to_space_.low();
1279 while (current < top()) {
1280 HeapObject* object = HeapObject::FromAddress(current);
1281
1282 // The first word should be a map, and we expect all map pointers to
1283 // be in map space.
1284 Map* map = object->map();
1285 ASSERT(map->IsMap());
1286 ASSERT(Heap::map_space()->Contains(map));
1287
1288 // The object should not be code or a map.
1289 ASSERT(!object->IsMap());
1290 ASSERT(!object->IsCode());
1291
1292 // The object itself should look OK.
1293 object->Verify();
1294
1295 // All the interior pointers should be contained in the heap.
1296 VerifyPointersVisitor visitor;
1297 int size = object->Size();
1298 object->IterateBody(map->instance_type(), size, &visitor);
1299
1300 current += size;
1301 }
1302
1303 // The allocation pointer should not be in the middle of an object.
1304 ASSERT(current == top());
1305}
1306#endif
1307
1308
1309bool SemiSpace::Commit() {
1310 ASSERT(!is_committed());
1311 if (!MemoryAllocator::CommitBlock(start_, capacity_, executable())) {
1312 return false;
1313 }
1314 committed_ = true;
1315 return true;
1316}
1317
1318
1319bool SemiSpace::Uncommit() {
1320 ASSERT(is_committed());
1321 if (!MemoryAllocator::UncommitBlock(start_, capacity_)) {
1322 return false;
1323 }
1324 committed_ = false;
1325 return true;
1326}
1327
1328
1329// -----------------------------------------------------------------------------
1330// SemiSpace implementation
1331
1332bool SemiSpace::Setup(Address start,
1333 int initial_capacity,
1334 int maximum_capacity) {
1335 // Creates a space in the young generation. The constructor does not
1336 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1337 // memory of size 'capacity' when set up, and does not grow or shrink
1338 // otherwise. In the mark-compact collector, the memory region of the from
1339 // space is used as the marking stack. It requires contiguous memory
1340 // addresses.
1341 initial_capacity_ = initial_capacity;
1342 capacity_ = initial_capacity;
1343 maximum_capacity_ = maximum_capacity;
1344 committed_ = false;
1345
1346 start_ = start;
1347 address_mask_ = ~(maximum_capacity - 1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001348 object_mask_ = address_mask_ | kHeapObjectTagMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00001349 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
1350 age_mark_ = start_;
1351
1352 return Commit();
1353}
1354
1355
1356void SemiSpace::TearDown() {
1357 start_ = NULL;
1358 capacity_ = 0;
1359}
1360
1361
1362bool SemiSpace::Grow() {
1363 // Double the semispace size but only up to maximum capacity.
1364 int maximum_extra = maximum_capacity_ - capacity_;
Steve Blockd0582a62009-12-15 09:54:21 +00001365 int extra = Min(RoundUp(capacity_, static_cast<int>(OS::AllocateAlignment())),
Steve Blocka7e24c12009-10-30 11:49:00 +00001366 maximum_extra);
1367 if (!MemoryAllocator::CommitBlock(high(), extra, executable())) {
1368 return false;
1369 }
1370 capacity_ += extra;
1371 return true;
1372}
1373
1374
1375bool SemiSpace::GrowTo(int new_capacity) {
1376 ASSERT(new_capacity <= maximum_capacity_);
1377 ASSERT(new_capacity > capacity_);
1378 size_t delta = new_capacity - capacity_;
1379 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1380 if (!MemoryAllocator::CommitBlock(high(), delta, executable())) {
1381 return false;
1382 }
1383 capacity_ = new_capacity;
1384 return true;
1385}
1386
1387
1388bool SemiSpace::ShrinkTo(int new_capacity) {
1389 ASSERT(new_capacity >= initial_capacity_);
1390 ASSERT(new_capacity < capacity_);
1391 size_t delta = capacity_ - new_capacity;
1392 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1393 if (!MemoryAllocator::UncommitBlock(high() - delta, delta)) {
1394 return false;
1395 }
1396 capacity_ = new_capacity;
1397 return true;
1398}
1399
1400
1401#ifdef DEBUG
1402void SemiSpace::Print() { }
1403
1404
1405void SemiSpace::Verify() { }
1406#endif
1407
1408
1409// -----------------------------------------------------------------------------
1410// SemiSpaceIterator implementation.
1411SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
1412 Initialize(space, space->bottom(), space->top(), NULL);
1413}
1414
1415
1416SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1417 HeapObjectCallback size_func) {
1418 Initialize(space, space->bottom(), space->top(), size_func);
1419}
1420
1421
1422SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1423 Initialize(space, start, space->top(), NULL);
1424}
1425
1426
1427void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1428 Address end,
1429 HeapObjectCallback size_func) {
1430 ASSERT(space->ToSpaceContains(start));
1431 ASSERT(space->ToSpaceLow() <= end
1432 && end <= space->ToSpaceHigh());
1433 space_ = &space->to_space_;
1434 current_ = start;
1435 limit_ = end;
1436 size_func_ = size_func;
1437}
1438
1439
1440#ifdef DEBUG
1441// A static array of histogram info for each type.
1442static HistogramInfo heap_histograms[LAST_TYPE+1];
1443static JSObject::SpillInformation js_spill_information;
1444
1445// heap_histograms is shared, always clear it before using it.
1446static void ClearHistograms() {
1447 // We reset the name each time, though it hasn't changed.
1448#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1449 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1450#undef DEF_TYPE_NAME
1451
1452#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1453 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1454#undef CLEAR_HISTOGRAM
1455
1456 js_spill_information.Clear();
1457}
1458
1459
1460static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1461
1462
1463static void ClearCodeKindStatistics() {
1464 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1465 code_kind_statistics[i] = 0;
1466 }
1467}
1468
1469
1470static void ReportCodeKindStatistics() {
Steve Block6ded16b2010-05-10 14:33:55 +01001471 const char* table[Code::NUMBER_OF_KINDS] = { NULL };
Steve Blocka7e24c12009-10-30 11:49:00 +00001472
1473#define CASE(name) \
1474 case Code::name: table[Code::name] = #name; \
1475 break
1476
1477 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1478 switch (static_cast<Code::Kind>(i)) {
1479 CASE(FUNCTION);
1480 CASE(STUB);
1481 CASE(BUILTIN);
1482 CASE(LOAD_IC);
1483 CASE(KEYED_LOAD_IC);
1484 CASE(STORE_IC);
1485 CASE(KEYED_STORE_IC);
1486 CASE(CALL_IC);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001487 CASE(KEYED_CALL_IC);
Steve Block6ded16b2010-05-10 14:33:55 +01001488 CASE(BINARY_OP_IC);
Steve Blocka7e24c12009-10-30 11:49:00 +00001489 }
1490 }
1491
1492#undef CASE
1493
1494 PrintF("\n Code kind histograms: \n");
1495 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1496 if (code_kind_statistics[i] > 0) {
1497 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1498 }
1499 }
1500 PrintF("\n");
1501}
1502
1503
1504static int CollectHistogramInfo(HeapObject* obj) {
1505 InstanceType type = obj->map()->instance_type();
1506 ASSERT(0 <= type && type <= LAST_TYPE);
1507 ASSERT(heap_histograms[type].name() != NULL);
1508 heap_histograms[type].increment_number(1);
1509 heap_histograms[type].increment_bytes(obj->Size());
1510
1511 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1512 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1513 }
1514
1515 return obj->Size();
1516}
1517
1518
1519static void ReportHistogram(bool print_spill) {
1520 PrintF("\n Object Histogram:\n");
1521 for (int i = 0; i <= LAST_TYPE; i++) {
1522 if (heap_histograms[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001523 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001524 heap_histograms[i].name(),
1525 heap_histograms[i].number(),
1526 heap_histograms[i].bytes());
1527 }
1528 }
1529 PrintF("\n");
1530
1531 // Summarize string types.
1532 int string_number = 0;
1533 int string_bytes = 0;
1534#define INCREMENT(type, size, name, camel_name) \
1535 string_number += heap_histograms[type].number(); \
1536 string_bytes += heap_histograms[type].bytes();
1537 STRING_TYPE_LIST(INCREMENT)
1538#undef INCREMENT
1539 if (string_number > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001540 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
Steve Blocka7e24c12009-10-30 11:49:00 +00001541 string_bytes);
1542 }
1543
1544 if (FLAG_collect_heap_spill_statistics && print_spill) {
1545 js_spill_information.Print();
1546 }
1547}
1548#endif // DEBUG
1549
1550
1551// Support for statistics gathering for --heap-stats and --log-gc.
1552#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1553void NewSpace::ClearHistograms() {
1554 for (int i = 0; i <= LAST_TYPE; i++) {
1555 allocated_histogram_[i].clear();
1556 promoted_histogram_[i].clear();
1557 }
1558}
1559
1560// Because the copying collector does not touch garbage objects, we iterate
1561// the new space before a collection to get a histogram of allocated objects.
1562// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1563// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1564// flag is set.
1565void NewSpace::CollectStatistics() {
1566 ClearHistograms();
1567 SemiSpaceIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00001568 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next())
1569 RecordAllocation(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001570}
1571
1572
1573#ifdef ENABLE_LOGGING_AND_PROFILING
1574static void DoReportStatistics(HistogramInfo* info, const char* description) {
1575 LOG(HeapSampleBeginEvent("NewSpace", description));
1576 // Lump all the string types together.
1577 int string_number = 0;
1578 int string_bytes = 0;
1579#define INCREMENT(type, size, name, camel_name) \
1580 string_number += info[type].number(); \
1581 string_bytes += info[type].bytes();
1582 STRING_TYPE_LIST(INCREMENT)
1583#undef INCREMENT
1584 if (string_number > 0) {
1585 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1586 }
1587
1588 // Then do the other types.
1589 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1590 if (info[i].number() > 0) {
1591 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1592 info[i].bytes()));
1593 }
1594 }
1595 LOG(HeapSampleEndEvent("NewSpace", description));
1596}
1597#endif // ENABLE_LOGGING_AND_PROFILING
1598
1599
1600void NewSpace::ReportStatistics() {
1601#ifdef DEBUG
1602 if (FLAG_heap_stats) {
1603 float pct = static_cast<float>(Available()) / Capacity();
1604 PrintF(" capacity: %d, available: %d, %%%d\n",
1605 Capacity(), Available(), static_cast<int>(pct*100));
1606 PrintF("\n Object Histogram:\n");
1607 for (int i = 0; i <= LAST_TYPE; i++) {
1608 if (allocated_histogram_[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001609 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001610 allocated_histogram_[i].name(),
1611 allocated_histogram_[i].number(),
1612 allocated_histogram_[i].bytes());
1613 }
1614 }
1615 PrintF("\n");
1616 }
1617#endif // DEBUG
1618
1619#ifdef ENABLE_LOGGING_AND_PROFILING
1620 if (FLAG_log_gc) {
1621 DoReportStatistics(allocated_histogram_, "allocated");
1622 DoReportStatistics(promoted_histogram_, "promoted");
1623 }
1624#endif // ENABLE_LOGGING_AND_PROFILING
1625}
1626
1627
1628void NewSpace::RecordAllocation(HeapObject* obj) {
1629 InstanceType type = obj->map()->instance_type();
1630 ASSERT(0 <= type && type <= LAST_TYPE);
1631 allocated_histogram_[type].increment_number(1);
1632 allocated_histogram_[type].increment_bytes(obj->Size());
1633}
1634
1635
1636void NewSpace::RecordPromotion(HeapObject* obj) {
1637 InstanceType type = obj->map()->instance_type();
1638 ASSERT(0 <= type && type <= LAST_TYPE);
1639 promoted_histogram_[type].increment_number(1);
1640 promoted_histogram_[type].increment_bytes(obj->Size());
1641}
1642#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1643
1644
1645// -----------------------------------------------------------------------------
1646// Free lists for old object spaces implementation
1647
1648void FreeListNode::set_size(int size_in_bytes) {
1649 ASSERT(size_in_bytes > 0);
1650 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1651
1652 // We write a map and possibly size information to the block. If the block
1653 // is big enough to be a ByteArray with at least one extra word (the next
1654 // pointer), we set its map to be the byte array map and its size to an
1655 // appropriate array length for the desired size from HeapObject::Size().
1656 // If the block is too small (eg, one or two words), to hold both a size
1657 // field and a next pointer, we give it a filler map that gives it the
1658 // correct size.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001659 if (size_in_bytes > ByteArray::kHeaderSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001660 set_map(Heap::raw_unchecked_byte_array_map());
Steve Blockd0582a62009-12-15 09:54:21 +00001661 // Can't use ByteArray::cast because it fails during deserialization.
1662 ByteArray* this_as_byte_array = reinterpret_cast<ByteArray*>(this);
1663 this_as_byte_array->set_length(ByteArray::LengthFor(size_in_bytes));
Steve Blocka7e24c12009-10-30 11:49:00 +00001664 } else if (size_in_bytes == kPointerSize) {
1665 set_map(Heap::raw_unchecked_one_pointer_filler_map());
1666 } else if (size_in_bytes == 2 * kPointerSize) {
1667 set_map(Heap::raw_unchecked_two_pointer_filler_map());
1668 } else {
1669 UNREACHABLE();
1670 }
Steve Blockd0582a62009-12-15 09:54:21 +00001671 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
1672 // deserialization because the byte array map is not done yet.
Steve Blocka7e24c12009-10-30 11:49:00 +00001673}
1674
1675
1676Address FreeListNode::next() {
Steve Block3ce2e202009-11-05 08:53:23 +00001677 ASSERT(IsFreeListNode(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001678 if (map() == Heap::raw_unchecked_byte_array_map()) {
1679 ASSERT(Size() >= kNextOffset + kPointerSize);
1680 return Memory::Address_at(address() + kNextOffset);
1681 } else {
1682 return Memory::Address_at(address() + kPointerSize);
1683 }
1684}
1685
1686
1687void FreeListNode::set_next(Address next) {
Steve Block3ce2e202009-11-05 08:53:23 +00001688 ASSERT(IsFreeListNode(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001689 if (map() == Heap::raw_unchecked_byte_array_map()) {
1690 ASSERT(Size() >= kNextOffset + kPointerSize);
1691 Memory::Address_at(address() + kNextOffset) = next;
1692 } else {
1693 Memory::Address_at(address() + kPointerSize) = next;
1694 }
1695}
1696
1697
1698OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1699 Reset();
1700}
1701
1702
1703void OldSpaceFreeList::Reset() {
1704 available_ = 0;
1705 for (int i = 0; i < kFreeListsLength; i++) {
1706 free_[i].head_node_ = NULL;
1707 }
1708 needs_rebuild_ = false;
1709 finger_ = kHead;
1710 free_[kHead].next_size_ = kEnd;
1711}
1712
1713
1714void OldSpaceFreeList::RebuildSizeList() {
1715 ASSERT(needs_rebuild_);
1716 int cur = kHead;
1717 for (int i = cur + 1; i < kFreeListsLength; i++) {
1718 if (free_[i].head_node_ != NULL) {
1719 free_[cur].next_size_ = i;
1720 cur = i;
1721 }
1722 }
1723 free_[cur].next_size_ = kEnd;
1724 needs_rebuild_ = false;
1725}
1726
1727
1728int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1729#ifdef DEBUG
Leon Clarke4515c472010-02-03 11:58:03 +00001730 MemoryAllocator::ZapBlock(start, size_in_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001731#endif
1732 FreeListNode* node = FreeListNode::FromAddress(start);
1733 node->set_size(size_in_bytes);
1734
1735 // We don't use the freelists in compacting mode. This makes it more like a
1736 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1737 // collector.
1738 if (FLAG_always_compact) {
1739 return size_in_bytes;
1740 }
1741
1742 // Early return to drop too-small blocks on the floor (one or two word
1743 // blocks cannot hold a map pointer, a size field, and a pointer to the
1744 // next block in the free list).
1745 if (size_in_bytes < kMinBlockSize) {
1746 return size_in_bytes;
1747 }
1748
1749 // Insert other blocks at the head of an exact free list.
1750 int index = size_in_bytes >> kPointerSizeLog2;
1751 node->set_next(free_[index].head_node_);
1752 free_[index].head_node_ = node->address();
1753 available_ += size_in_bytes;
1754 needs_rebuild_ = true;
1755 return 0;
1756}
1757
1758
1759Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1760 ASSERT(0 < size_in_bytes);
1761 ASSERT(size_in_bytes <= kMaxBlockSize);
1762 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1763
1764 if (needs_rebuild_) RebuildSizeList();
1765 int index = size_in_bytes >> kPointerSizeLog2;
1766 // Check for a perfect fit.
1767 if (free_[index].head_node_ != NULL) {
1768 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1769 // If this was the last block of its size, remove the size.
1770 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1771 available_ -= size_in_bytes;
1772 *wasted_bytes = 0;
1773 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1774 return node;
1775 }
1776 // Search the size list for the best fit.
1777 int prev = finger_ < index ? finger_ : kHead;
1778 int cur = FindSize(index, &prev);
1779 ASSERT(index < cur);
1780 if (cur == kEnd) {
1781 // No large enough size in list.
1782 *wasted_bytes = 0;
1783 return Failure::RetryAfterGC(size_in_bytes, owner_);
1784 }
1785 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1786 int rem = cur - index;
1787 int rem_bytes = rem << kPointerSizeLog2;
1788 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
1789 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
1790 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1791 size_in_bytes);
1792 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1793 // to avoid many redundant tests and calls to Insert/RemoveSize.
1794 if (prev < rem) {
1795 // Simple case: insert rem between prev and cur.
1796 finger_ = prev;
1797 free_[prev].next_size_ = rem;
1798 // If this was the last block of size cur, remove the size.
1799 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1800 free_[rem].next_size_ = free_[cur].next_size_;
1801 } else {
1802 free_[rem].next_size_ = cur;
1803 }
1804 // Add the remainder block.
1805 rem_node->set_size(rem_bytes);
1806 rem_node->set_next(free_[rem].head_node_);
1807 free_[rem].head_node_ = rem_node->address();
1808 } else {
1809 // If this was the last block of size cur, remove the size.
1810 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1811 finger_ = prev;
1812 free_[prev].next_size_ = free_[cur].next_size_;
1813 }
1814 if (rem_bytes < kMinBlockSize) {
1815 // Too-small remainder is wasted.
1816 rem_node->set_size(rem_bytes);
1817 available_ -= size_in_bytes + rem_bytes;
1818 *wasted_bytes = rem_bytes;
1819 return cur_node;
1820 }
1821 // Add the remainder block and, if needed, insert its size.
1822 rem_node->set_size(rem_bytes);
1823 rem_node->set_next(free_[rem].head_node_);
1824 free_[rem].head_node_ = rem_node->address();
1825 if (rem_node->next() == NULL) InsertSize(rem);
1826 }
1827 available_ -= size_in_bytes;
1828 *wasted_bytes = 0;
1829 return cur_node;
1830}
1831
1832
1833#ifdef DEBUG
1834bool OldSpaceFreeList::Contains(FreeListNode* node) {
1835 for (int i = 0; i < kFreeListsLength; i++) {
1836 Address cur_addr = free_[i].head_node_;
1837 while (cur_addr != NULL) {
1838 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1839 if (cur_node == node) return true;
1840 cur_addr = cur_node->next();
1841 }
1842 }
1843 return false;
1844}
1845#endif
1846
1847
1848FixedSizeFreeList::FixedSizeFreeList(AllocationSpace owner, int object_size)
1849 : owner_(owner), object_size_(object_size) {
1850 Reset();
1851}
1852
1853
1854void FixedSizeFreeList::Reset() {
1855 available_ = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001856 head_ = tail_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001857}
1858
1859
1860void FixedSizeFreeList::Free(Address start) {
1861#ifdef DEBUG
Leon Clarke4515c472010-02-03 11:58:03 +00001862 MemoryAllocator::ZapBlock(start, object_size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001863#endif
Leon Clarkee46be812010-01-19 14:06:41 +00001864 // We only use the freelists with mark-sweep.
1865 ASSERT(!MarkCompactCollector::IsCompacting());
Steve Blocka7e24c12009-10-30 11:49:00 +00001866 FreeListNode* node = FreeListNode::FromAddress(start);
1867 node->set_size(object_size_);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001868 node->set_next(NULL);
1869 if (head_ == NULL) {
1870 tail_ = head_ = node->address();
1871 } else {
1872 FreeListNode::FromAddress(tail_)->set_next(node->address());
1873 tail_ = node->address();
1874 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001875 available_ += object_size_;
1876}
1877
1878
1879Object* FixedSizeFreeList::Allocate() {
1880 if (head_ == NULL) {
1881 return Failure::RetryAfterGC(object_size_, owner_);
1882 }
1883
1884 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1885 FreeListNode* node = FreeListNode::FromAddress(head_);
1886 head_ = node->next();
1887 available_ -= object_size_;
1888 return node;
1889}
1890
1891
1892// -----------------------------------------------------------------------------
1893// OldSpace implementation
1894
1895void OldSpace::PrepareForMarkCompact(bool will_compact) {
Steve Block6ded16b2010-05-10 14:33:55 +01001896 // Call prepare of the super class.
1897 PagedSpace::PrepareForMarkCompact(will_compact);
1898
Steve Blocka7e24c12009-10-30 11:49:00 +00001899 if (will_compact) {
1900 // Reset relocation info. During a compacting collection, everything in
1901 // the space is considered 'available' and we will rediscover live data
1902 // and waste during the collection.
1903 MCResetRelocationInfo();
1904 ASSERT(Available() == Capacity());
1905 } else {
1906 // During a non-compacting collection, everything below the linear
1907 // allocation pointer is considered allocated (everything above is
1908 // available) and we will rediscover available and wasted bytes during
1909 // the collection.
1910 accounting_stats_.AllocateBytes(free_list_.available());
1911 accounting_stats_.FillWastedBytes(Waste());
1912 }
1913
1914 // Clear the free list before a full GC---it will be rebuilt afterward.
1915 free_list_.Reset();
1916}
1917
1918
1919void OldSpace::MCCommitRelocationInfo() {
1920 // Update fast allocation info.
1921 allocation_info_.top = mc_forwarding_info_.top;
1922 allocation_info_.limit = mc_forwarding_info_.limit;
1923 ASSERT(allocation_info_.VerifyPagedAllocation());
1924
1925 // The space is compacted and we haven't yet built free lists or
1926 // wasted any space.
1927 ASSERT(Waste() == 0);
1928 ASSERT(AvailableFree() == 0);
1929
1930 // Build the free list for the space.
1931 int computed_size = 0;
1932 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1933 while (it.has_next()) {
1934 Page* p = it.next();
1935 // Space below the relocation pointer is allocated.
Steve Blockd0582a62009-12-15 09:54:21 +00001936 computed_size +=
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001937 static_cast<int>(p->AllocationWatermark() - p->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +00001938 if (it.has_next()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001939 // Free the space at the top of the page.
Steve Blockd0582a62009-12-15 09:54:21 +00001940 int extra_size =
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001941 static_cast<int>(p->ObjectAreaEnd() - p->AllocationWatermark());
Steve Blocka7e24c12009-10-30 11:49:00 +00001942 if (extra_size > 0) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001943 int wasted_bytes = free_list_.Free(p->AllocationWatermark(),
1944 extra_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001945 // The bytes we have just "freed" to add to the free list were
1946 // already accounted as available.
1947 accounting_stats_.WasteBytes(wasted_bytes);
1948 }
1949 }
1950 }
1951
1952 // Make sure the computed size - based on the used portion of the pages in
1953 // use - matches the size obtained while computing forwarding addresses.
1954 ASSERT(computed_size == Size());
1955}
1956
1957
Leon Clarkee46be812010-01-19 14:06:41 +00001958bool NewSpace::ReserveSpace(int bytes) {
1959 // We can't reliably unpack a partial snapshot that needs more new space
1960 // space than the minimum NewSpace size.
1961 ASSERT(bytes <= InitialCapacity());
1962 Address limit = allocation_info_.limit;
1963 Address top = allocation_info_.top;
1964 return limit - top >= bytes;
1965}
1966
1967
Steve Block6ded16b2010-05-10 14:33:55 +01001968void PagedSpace::FreePages(Page* prev, Page* last) {
1969 if (last == AllocationTopPage()) {
1970 // Pages are already at the end of used pages.
1971 return;
1972 }
1973
1974 Page* first = NULL;
1975
1976 // Remove pages from the list.
1977 if (prev == NULL) {
1978 first = first_page_;
1979 first_page_ = last->next_page();
1980 } else {
1981 first = prev->next_page();
1982 MemoryAllocator::SetNextPage(prev, last->next_page());
1983 }
1984
1985 // Attach it after the last page.
1986 MemoryAllocator::SetNextPage(last_page_, first);
1987 last_page_ = last;
1988 MemoryAllocator::SetNextPage(last, NULL);
1989
1990 // Clean them up.
1991 do {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001992 first->InvalidateWatermark(true);
1993 first->SetAllocationWatermark(first->ObjectAreaStart());
1994 first->SetCachedAllocationWatermark(first->ObjectAreaStart());
1995 first->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Block6ded16b2010-05-10 14:33:55 +01001996 first = first->next_page();
1997 } while (first != NULL);
1998
1999 // Order of pages in this space might no longer be consistent with
2000 // order of pages in chunks.
2001 page_list_is_chunk_ordered_ = false;
2002}
2003
2004
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002005void PagedSpace::RelinkPageListInChunkOrder(bool deallocate_blocks) {
2006 const bool add_to_freelist = true;
2007
2008 // Mark used and unused pages to properly fill unused pages
2009 // after reordering.
2010 PageIterator all_pages_iterator(this, PageIterator::ALL_PAGES);
2011 Page* last_in_use = AllocationTopPage();
2012 bool in_use = true;
2013
2014 while (all_pages_iterator.has_next()) {
2015 Page* p = all_pages_iterator.next();
2016 p->SetWasInUseBeforeMC(in_use);
2017 if (p == last_in_use) {
2018 // We passed a page containing allocation top. All consequent
2019 // pages are not used.
2020 in_use = false;
2021 }
2022 }
2023
2024 if (page_list_is_chunk_ordered_) return;
2025
2026 Page* new_last_in_use = Page::FromAddress(NULL);
2027 MemoryAllocator::RelinkPageListInChunkOrder(this,
2028 &first_page_,
2029 &last_page_,
2030 &new_last_in_use);
2031 ASSERT(new_last_in_use->is_valid());
2032
2033 if (new_last_in_use != last_in_use) {
2034 // Current allocation top points to a page which is now in the middle
2035 // of page list. We should move allocation top forward to the new last
2036 // used page so various object iterators will continue to work properly.
2037 int size_in_bytes = static_cast<int>(PageAllocationLimit(last_in_use) -
2038 last_in_use->AllocationTop());
2039
2040 last_in_use->SetAllocationWatermark(last_in_use->AllocationTop());
2041 if (size_in_bytes > 0) {
2042 Address start = last_in_use->AllocationTop();
2043 if (deallocate_blocks) {
2044 accounting_stats_.AllocateBytes(size_in_bytes);
2045 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2046 } else {
2047 Heap::CreateFillerObjectAt(start, size_in_bytes);
2048 }
2049 }
2050
2051 // New last in use page was in the middle of the list before
2052 // sorting so it full.
2053 SetTop(new_last_in_use->AllocationTop());
2054
2055 ASSERT(AllocationTopPage() == new_last_in_use);
2056 ASSERT(AllocationTopPage()->WasInUseBeforeMC());
2057 }
2058
2059 PageIterator pages_in_use_iterator(this, PageIterator::PAGES_IN_USE);
2060 while (pages_in_use_iterator.has_next()) {
2061 Page* p = pages_in_use_iterator.next();
2062 if (!p->WasInUseBeforeMC()) {
2063 // Empty page is in the middle of a sequence of used pages.
2064 // Allocate it as a whole and deallocate immediately.
2065 int size_in_bytes = static_cast<int>(PageAllocationLimit(p) -
2066 p->ObjectAreaStart());
2067
2068 p->SetAllocationWatermark(p->ObjectAreaStart());
2069 Address start = p->ObjectAreaStart();
2070 if (deallocate_blocks) {
2071 accounting_stats_.AllocateBytes(size_in_bytes);
2072 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2073 } else {
2074 Heap::CreateFillerObjectAt(start, size_in_bytes);
2075 }
2076 }
2077 }
2078
2079 page_list_is_chunk_ordered_ = true;
2080}
2081
2082
Steve Block6ded16b2010-05-10 14:33:55 +01002083void PagedSpace::PrepareForMarkCompact(bool will_compact) {
2084 if (will_compact) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002085 RelinkPageListInChunkOrder(false);
Steve Block6ded16b2010-05-10 14:33:55 +01002086 }
2087}
2088
2089
Leon Clarkee46be812010-01-19 14:06:41 +00002090bool PagedSpace::ReserveSpace(int bytes) {
2091 Address limit = allocation_info_.limit;
2092 Address top = allocation_info_.top;
2093 if (limit - top >= bytes) return true;
2094
2095 // There wasn't enough space in the current page. Lets put the rest
2096 // of the page on the free list and start a fresh page.
2097 PutRestOfCurrentPageOnFreeList(TopPageOf(allocation_info_));
2098
2099 Page* reserved_page = TopPageOf(allocation_info_);
2100 int bytes_left_to_reserve = bytes;
2101 while (bytes_left_to_reserve > 0) {
2102 if (!reserved_page->next_page()->is_valid()) {
2103 if (Heap::OldGenerationAllocationLimitReached()) return false;
2104 Expand(reserved_page);
2105 }
2106 bytes_left_to_reserve -= Page::kPageSize;
2107 reserved_page = reserved_page->next_page();
2108 if (!reserved_page->is_valid()) return false;
2109 }
2110 ASSERT(TopPageOf(allocation_info_)->next_page()->is_valid());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002111 TopPageOf(allocation_info_)->next_page()->InvalidateWatermark(true);
Leon Clarkee46be812010-01-19 14:06:41 +00002112 SetAllocationInfo(&allocation_info_,
2113 TopPageOf(allocation_info_)->next_page());
2114 return true;
2115}
2116
2117
2118// You have to call this last, since the implementation from PagedSpace
2119// doesn't know that memory was 'promised' to large object space.
2120bool LargeObjectSpace::ReserveSpace(int bytes) {
2121 return Heap::OldGenerationSpaceAvailable() >= bytes;
2122}
2123
2124
Steve Blocka7e24c12009-10-30 11:49:00 +00002125// Slow case for normal allocation. Try in order: (1) allocate in the next
2126// page in the space, (2) allocate off the space's free list, (3) expand the
2127// space, (4) fail.
2128HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
2129 // Linear allocation in this space has failed. If there is another page
2130 // in the space, move to that page and allocate there. This allocation
2131 // should succeed (size_in_bytes should not be greater than a page's
2132 // object area size).
2133 Page* current_page = TopPageOf(allocation_info_);
2134 if (current_page->next_page()->is_valid()) {
2135 return AllocateInNextPage(current_page, size_in_bytes);
2136 }
2137
Steve Blockd0582a62009-12-15 09:54:21 +00002138 // There is no next page in this space. Try free list allocation unless that
2139 // is currently forbidden.
2140 if (!Heap::linear_allocation()) {
2141 int wasted_bytes;
2142 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
2143 accounting_stats_.WasteBytes(wasted_bytes);
2144 if (!result->IsFailure()) {
2145 accounting_stats_.AllocateBytes(size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002146
2147 HeapObject* obj = HeapObject::cast(result);
2148 Page* p = Page::FromAddress(obj->address());
2149
2150 if (obj->address() >= p->AllocationWatermark()) {
2151 // There should be no hole between the allocation watermark
2152 // and allocated object address.
2153 // Memory above the allocation watermark was not swept and
2154 // might contain garbage pointers to new space.
2155 ASSERT(obj->address() == p->AllocationWatermark());
2156 p->SetAllocationWatermark(obj->address() + size_in_bytes);
2157 }
2158
2159 return obj;
Steve Blockd0582a62009-12-15 09:54:21 +00002160 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002161 }
2162
2163 // Free list allocation failed and there is no next page. Fail if we have
2164 // hit the old generation size limit that should cause a garbage
2165 // collection.
2166 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2167 return NULL;
2168 }
2169
2170 // Try to expand the space and allocate in the new next page.
2171 ASSERT(!current_page->next_page()->is_valid());
2172 if (Expand(current_page)) {
2173 return AllocateInNextPage(current_page, size_in_bytes);
2174 }
2175
2176 // Finally, fail.
2177 return NULL;
2178}
2179
2180
Leon Clarkee46be812010-01-19 14:06:41 +00002181void OldSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002182 current_page->SetAllocationWatermark(allocation_info_.top);
Steve Blockd0582a62009-12-15 09:54:21 +00002183 int free_size =
2184 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002185 if (free_size > 0) {
2186 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
2187 accounting_stats_.WasteBytes(wasted_bytes);
2188 }
Leon Clarkee46be812010-01-19 14:06:41 +00002189}
2190
2191
2192void FixedSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002193 current_page->SetAllocationWatermark(allocation_info_.top);
Leon Clarkee46be812010-01-19 14:06:41 +00002194 int free_size =
2195 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
2196 // In the fixed space free list all the free list items have the right size.
2197 // We use up the rest of the page while preserving this invariant.
2198 while (free_size >= object_size_in_bytes_) {
2199 free_list_.Free(allocation_info_.top);
2200 allocation_info_.top += object_size_in_bytes_;
2201 free_size -= object_size_in_bytes_;
2202 accounting_stats_.WasteBytes(object_size_in_bytes_);
2203 }
2204}
2205
2206
2207// Add the block at the top of the page to the space's free list, set the
2208// allocation info to the next page (assumed to be one), and allocate
2209// linearly there.
2210HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
2211 int size_in_bytes) {
2212 ASSERT(current_page->next_page()->is_valid());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002213 Page* next_page = current_page->next_page();
2214 next_page->ClearGCFields();
Leon Clarkee46be812010-01-19 14:06:41 +00002215 PutRestOfCurrentPageOnFreeList(current_page);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002216 SetAllocationInfo(&allocation_info_, next_page);
Steve Blocka7e24c12009-10-30 11:49:00 +00002217 return AllocateLinearly(&allocation_info_, size_in_bytes);
2218}
2219
2220
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002221void OldSpace::DeallocateBlock(Address start,
2222 int size_in_bytes,
2223 bool add_to_freelist) {
2224 Free(start, size_in_bytes, add_to_freelist);
2225}
2226
2227
Steve Blocka7e24c12009-10-30 11:49:00 +00002228#ifdef DEBUG
2229struct CommentStatistic {
2230 const char* comment;
2231 int size;
2232 int count;
2233 void Clear() {
2234 comment = NULL;
2235 size = 0;
2236 count = 0;
2237 }
2238};
2239
2240
2241// must be small, since an iteration is used for lookup
2242const int kMaxComments = 64;
2243static CommentStatistic comments_statistics[kMaxComments+1];
2244
2245
2246void PagedSpace::ReportCodeStatistics() {
2247 ReportCodeKindStatistics();
2248 PrintF("Code comment statistics (\" [ comment-txt : size/ "
2249 "count (average)\"):\n");
2250 for (int i = 0; i <= kMaxComments; i++) {
2251 const CommentStatistic& cs = comments_statistics[i];
2252 if (cs.size > 0) {
2253 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
2254 cs.size/cs.count);
2255 }
2256 }
2257 PrintF("\n");
2258}
2259
2260
2261void PagedSpace::ResetCodeStatistics() {
2262 ClearCodeKindStatistics();
2263 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
2264 comments_statistics[kMaxComments].comment = "Unknown";
2265 comments_statistics[kMaxComments].size = 0;
2266 comments_statistics[kMaxComments].count = 0;
2267}
2268
2269
2270// Adds comment to 'comment_statistics' table. Performance OK sa long as
2271// 'kMaxComments' is small
2272static void EnterComment(const char* comment, int delta) {
2273 // Do not count empty comments
2274 if (delta <= 0) return;
2275 CommentStatistic* cs = &comments_statistics[kMaxComments];
2276 // Search for a free or matching entry in 'comments_statistics': 'cs'
2277 // points to result.
2278 for (int i = 0; i < kMaxComments; i++) {
2279 if (comments_statistics[i].comment == NULL) {
2280 cs = &comments_statistics[i];
2281 cs->comment = comment;
2282 break;
2283 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2284 cs = &comments_statistics[i];
2285 break;
2286 }
2287 }
2288 // Update entry for 'comment'
2289 cs->size += delta;
2290 cs->count += 1;
2291}
2292
2293
2294// Call for each nested comment start (start marked with '[ xxx', end marked
2295// with ']'. RelocIterator 'it' must point to a comment reloc info.
2296static void CollectCommentStatistics(RelocIterator* it) {
2297 ASSERT(!it->done());
2298 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
2299 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2300 if (tmp[0] != '[') {
2301 // Not a nested comment; skip
2302 return;
2303 }
2304
2305 // Search for end of nested comment or a new nested comment
2306 const char* const comment_txt =
2307 reinterpret_cast<const char*>(it->rinfo()->data());
2308 const byte* prev_pc = it->rinfo()->pc();
2309 int flat_delta = 0;
2310 it->next();
2311 while (true) {
2312 // All nested comments must be terminated properly, and therefore exit
2313 // from loop.
2314 ASSERT(!it->done());
2315 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
2316 const char* const txt =
2317 reinterpret_cast<const char*>(it->rinfo()->data());
Steve Blockd0582a62009-12-15 09:54:21 +00002318 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002319 if (txt[0] == ']') break; // End of nested comment
2320 // A new comment
2321 CollectCommentStatistics(it);
2322 // Skip code that was covered with previous comment
2323 prev_pc = it->rinfo()->pc();
2324 }
2325 it->next();
2326 }
2327 EnterComment(comment_txt, flat_delta);
2328}
2329
2330
2331// Collects code size statistics:
2332// - by code kind
2333// - by code comment
2334void PagedSpace::CollectCodeStatistics() {
2335 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002336 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002337 if (obj->IsCode()) {
2338 Code* code = Code::cast(obj);
2339 code_kind_statistics[code->kind()] += code->Size();
2340 RelocIterator it(code);
2341 int delta = 0;
2342 const byte* prev_pc = code->instruction_start();
2343 while (!it.done()) {
2344 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
Steve Blockd0582a62009-12-15 09:54:21 +00002345 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002346 CollectCommentStatistics(&it);
2347 prev_pc = it.rinfo()->pc();
2348 }
2349 it.next();
2350 }
2351
2352 ASSERT(code->instruction_start() <= prev_pc &&
Leon Clarkeac952652010-07-15 11:15:24 +01002353 prev_pc <= code->instruction_end());
2354 delta += static_cast<int>(code->instruction_end() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002355 EnterComment("NoComment", delta);
2356 }
2357 }
2358}
2359
2360
2361void OldSpace::ReportStatistics() {
2362 int pct = Available() * 100 / Capacity();
2363 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2364 Capacity(), Waste(), Available(), pct);
2365
Steve Blocka7e24c12009-10-30 11:49:00 +00002366 ClearHistograms();
2367 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002368 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2369 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002370 ReportHistogram(true);
2371}
Steve Blocka7e24c12009-10-30 11:49:00 +00002372#endif
2373
2374// -----------------------------------------------------------------------------
2375// FixedSpace implementation
2376
2377void FixedSpace::PrepareForMarkCompact(bool will_compact) {
Steve Block6ded16b2010-05-10 14:33:55 +01002378 // Call prepare of the super class.
2379 PagedSpace::PrepareForMarkCompact(will_compact);
2380
Steve Blocka7e24c12009-10-30 11:49:00 +00002381 if (will_compact) {
2382 // Reset relocation info.
2383 MCResetRelocationInfo();
2384
2385 // During a compacting collection, everything in the space is considered
2386 // 'available' (set by the call to MCResetRelocationInfo) and we will
2387 // rediscover live and wasted bytes during the collection.
2388 ASSERT(Available() == Capacity());
2389 } else {
2390 // During a non-compacting collection, everything below the linear
2391 // allocation pointer except wasted top-of-page blocks is considered
2392 // allocated and we will rediscover available bytes during the
2393 // collection.
2394 accounting_stats_.AllocateBytes(free_list_.available());
2395 }
2396
2397 // Clear the free list before a full GC---it will be rebuilt afterward.
2398 free_list_.Reset();
2399}
2400
2401
2402void FixedSpace::MCCommitRelocationInfo() {
2403 // Update fast allocation info.
2404 allocation_info_.top = mc_forwarding_info_.top;
2405 allocation_info_.limit = mc_forwarding_info_.limit;
2406 ASSERT(allocation_info_.VerifyPagedAllocation());
2407
2408 // The space is compacted and we haven't yet wasted any space.
2409 ASSERT(Waste() == 0);
2410
2411 // Update allocation_top of each page in use and compute waste.
2412 int computed_size = 0;
2413 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2414 while (it.has_next()) {
2415 Page* page = it.next();
2416 Address page_top = page->AllocationTop();
Steve Blockd0582a62009-12-15 09:54:21 +00002417 computed_size += static_cast<int>(page_top - page->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +00002418 if (it.has_next()) {
Steve Blockd0582a62009-12-15 09:54:21 +00002419 accounting_stats_.WasteBytes(
2420 static_cast<int>(page->ObjectAreaEnd() - page_top));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002421 page->SetAllocationWatermark(page_top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002422 }
2423 }
2424
2425 // Make sure the computed size - based on the used portion of the
2426 // pages in use - matches the size we adjust during allocation.
2427 ASSERT(computed_size == Size());
2428}
2429
2430
2431// Slow case for normal allocation. Try in order: (1) allocate in the next
2432// page in the space, (2) allocate off the space's free list, (3) expand the
2433// space, (4) fail.
2434HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2435 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2436 // Linear allocation in this space has failed. If there is another page
2437 // in the space, move to that page and allocate there. This allocation
2438 // should succeed.
2439 Page* current_page = TopPageOf(allocation_info_);
2440 if (current_page->next_page()->is_valid()) {
2441 return AllocateInNextPage(current_page, size_in_bytes);
2442 }
2443
Steve Blockd0582a62009-12-15 09:54:21 +00002444 // There is no next page in this space. Try free list allocation unless
2445 // that is currently forbidden. The fixed space free list implicitly assumes
2446 // that all free blocks are of the fixed size.
2447 if (!Heap::linear_allocation()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002448 Object* result = free_list_.Allocate();
2449 if (!result->IsFailure()) {
2450 accounting_stats_.AllocateBytes(size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002451 HeapObject* obj = HeapObject::cast(result);
2452 Page* p = Page::FromAddress(obj->address());
2453
2454 if (obj->address() >= p->AllocationWatermark()) {
2455 // There should be no hole between the allocation watermark
2456 // and allocated object address.
2457 // Memory above the allocation watermark was not swept and
2458 // might contain garbage pointers to new space.
2459 ASSERT(obj->address() == p->AllocationWatermark());
2460 p->SetAllocationWatermark(obj->address() + size_in_bytes);
2461 }
2462
2463 return obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00002464 }
2465 }
2466
2467 // Free list allocation failed and there is no next page. Fail if we have
2468 // hit the old generation size limit that should cause a garbage
2469 // collection.
2470 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2471 return NULL;
2472 }
2473
2474 // Try to expand the space and allocate in the new next page.
2475 ASSERT(!current_page->next_page()->is_valid());
2476 if (Expand(current_page)) {
2477 return AllocateInNextPage(current_page, size_in_bytes);
2478 }
2479
2480 // Finally, fail.
2481 return NULL;
2482}
2483
2484
2485// Move to the next page (there is assumed to be one) and allocate there.
2486// The top of page block is always wasted, because it is too small to hold a
2487// map.
2488HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2489 int size_in_bytes) {
2490 ASSERT(current_page->next_page()->is_valid());
Steve Block6ded16b2010-05-10 14:33:55 +01002491 ASSERT(allocation_info_.top == PageAllocationLimit(current_page));
Steve Blocka7e24c12009-10-30 11:49:00 +00002492 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002493 Page* next_page = current_page->next_page();
2494 next_page->ClearGCFields();
2495 current_page->SetAllocationWatermark(allocation_info_.top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002496 accounting_stats_.WasteBytes(page_extra_);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002497 SetAllocationInfo(&allocation_info_, next_page);
Steve Blocka7e24c12009-10-30 11:49:00 +00002498 return AllocateLinearly(&allocation_info_, size_in_bytes);
2499}
2500
2501
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002502void FixedSpace::DeallocateBlock(Address start,
2503 int size_in_bytes,
2504 bool add_to_freelist) {
2505 // Free-list elements in fixed space are assumed to have a fixed size.
2506 // We break the free block into chunks and add them to the free list
2507 // individually.
2508 int size = object_size_in_bytes();
2509 ASSERT(size_in_bytes % size == 0);
2510 Address end = start + size_in_bytes;
2511 for (Address a = start; a < end; a += size) {
2512 Free(a, add_to_freelist);
2513 }
2514}
2515
2516
Steve Blocka7e24c12009-10-30 11:49:00 +00002517#ifdef DEBUG
2518void FixedSpace::ReportStatistics() {
2519 int pct = Available() * 100 / Capacity();
2520 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2521 Capacity(), Waste(), Available(), pct);
2522
Steve Blocka7e24c12009-10-30 11:49:00 +00002523 ClearHistograms();
2524 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002525 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2526 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002527 ReportHistogram(false);
2528}
Steve Blocka7e24c12009-10-30 11:49:00 +00002529#endif
2530
2531
2532// -----------------------------------------------------------------------------
2533// MapSpace implementation
2534
2535void MapSpace::PrepareForMarkCompact(bool will_compact) {
2536 // Call prepare of the super class.
2537 FixedSpace::PrepareForMarkCompact(will_compact);
2538
2539 if (will_compact) {
2540 // Initialize map index entry.
2541 int page_count = 0;
2542 PageIterator it(this, PageIterator::ALL_PAGES);
2543 while (it.has_next()) {
2544 ASSERT_MAP_PAGE_INDEX(page_count);
2545
2546 Page* p = it.next();
2547 ASSERT(p->mc_page_index == page_count);
2548
2549 page_addresses_[page_count++] = p->address();
2550 }
2551 }
2552}
2553
2554
2555#ifdef DEBUG
2556void MapSpace::VerifyObject(HeapObject* object) {
2557 // The object should be a map or a free-list node.
2558 ASSERT(object->IsMap() || object->IsByteArray());
2559}
2560#endif
2561
2562
2563// -----------------------------------------------------------------------------
2564// GlobalPropertyCellSpace implementation
2565
2566#ifdef DEBUG
2567void CellSpace::VerifyObject(HeapObject* object) {
2568 // The object should be a global object property cell or a free-list node.
2569 ASSERT(object->IsJSGlobalPropertyCell() ||
2570 object->map() == Heap::two_pointer_filler_map());
2571}
2572#endif
2573
2574
2575// -----------------------------------------------------------------------------
2576// LargeObjectIterator
2577
2578LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2579 current_ = space->first_chunk_;
2580 size_func_ = NULL;
2581}
2582
2583
2584LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2585 HeapObjectCallback size_func) {
2586 current_ = space->first_chunk_;
2587 size_func_ = size_func;
2588}
2589
2590
2591HeapObject* LargeObjectIterator::next() {
Leon Clarked91b9f72010-01-27 17:25:45 +00002592 if (current_ == NULL) return NULL;
2593
Steve Blocka7e24c12009-10-30 11:49:00 +00002594 HeapObject* object = current_->GetObject();
2595 current_ = current_->next();
2596 return object;
2597}
2598
2599
2600// -----------------------------------------------------------------------------
2601// LargeObjectChunk
2602
2603LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
2604 size_t* chunk_size,
2605 Executability executable) {
2606 size_t requested = ChunkSizeFor(size_in_bytes);
2607 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2608 chunk_size,
2609 executable);
2610 if (mem == NULL) return NULL;
2611 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2612 if (*chunk_size < requested) {
Steve Block791712a2010-08-27 10:21:07 +01002613 MemoryAllocator::FreeRawMemory(mem, *chunk_size, executable);
Steve Blocka7e24c12009-10-30 11:49:00 +00002614 LOG(DeleteEvent("LargeObjectChunk", mem));
2615 return NULL;
2616 }
2617 return reinterpret_cast<LargeObjectChunk*>(mem);
2618}
2619
2620
2621int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
Steve Blockd0582a62009-12-15 09:54:21 +00002622 int os_alignment = static_cast<int>(OS::AllocateAlignment());
Steve Blocka7e24c12009-10-30 11:49:00 +00002623 if (os_alignment < Page::kPageSize)
2624 size_in_bytes += (Page::kPageSize - os_alignment);
2625 return size_in_bytes + Page::kObjectStartOffset;
2626}
2627
2628// -----------------------------------------------------------------------------
2629// LargeObjectSpace
2630
2631LargeObjectSpace::LargeObjectSpace(AllocationSpace id)
2632 : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis
2633 first_chunk_(NULL),
2634 size_(0),
2635 page_count_(0) {}
2636
2637
2638bool LargeObjectSpace::Setup() {
2639 first_chunk_ = NULL;
2640 size_ = 0;
2641 page_count_ = 0;
2642 return true;
2643}
2644
2645
2646void LargeObjectSpace::TearDown() {
2647 while (first_chunk_ != NULL) {
2648 LargeObjectChunk* chunk = first_chunk_;
2649 first_chunk_ = first_chunk_->next();
2650 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
Steve Block791712a2010-08-27 10:21:07 +01002651 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2652 Executability executable =
2653 page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE;
2654 MemoryAllocator::FreeRawMemory(chunk->address(),
2655 chunk->size(),
2656 executable);
Steve Blocka7e24c12009-10-30 11:49:00 +00002657 }
2658
2659 size_ = 0;
2660 page_count_ = 0;
2661}
2662
2663
2664#ifdef ENABLE_HEAP_PROTECTION
2665
2666void LargeObjectSpace::Protect() {
2667 LargeObjectChunk* chunk = first_chunk_;
2668 while (chunk != NULL) {
2669 MemoryAllocator::Protect(chunk->address(), chunk->size());
2670 chunk = chunk->next();
2671 }
2672}
2673
2674
2675void LargeObjectSpace::Unprotect() {
2676 LargeObjectChunk* chunk = first_chunk_;
2677 while (chunk != NULL) {
2678 bool is_code = chunk->GetObject()->IsCode();
2679 MemoryAllocator::Unprotect(chunk->address(), chunk->size(),
2680 is_code ? EXECUTABLE : NOT_EXECUTABLE);
2681 chunk = chunk->next();
2682 }
2683}
2684
2685#endif
2686
2687
2688Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
2689 int object_size,
2690 Executability executable) {
2691 ASSERT(0 < object_size && object_size <= requested_size);
2692
2693 // Check if we want to force a GC before growing the old space further.
2694 // If so, fail the allocation.
2695 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2696 return Failure::RetryAfterGC(requested_size, identity());
2697 }
2698
2699 size_t chunk_size;
2700 LargeObjectChunk* chunk =
2701 LargeObjectChunk::New(requested_size, &chunk_size, executable);
2702 if (chunk == NULL) {
2703 return Failure::RetryAfterGC(requested_size, identity());
2704 }
2705
Steve Blockd0582a62009-12-15 09:54:21 +00002706 size_ += static_cast<int>(chunk_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002707 page_count_++;
2708 chunk->set_next(first_chunk_);
2709 chunk->set_size(chunk_size);
2710 first_chunk_ = chunk;
2711
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002712 // Initialize page header.
Steve Blocka7e24c12009-10-30 11:49:00 +00002713 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2714 Address object_address = page->ObjectAreaStart();
2715 // Clear the low order bit of the second word in the page to flag it as a
2716 // large object page. If the chunk_size happened to be written there, its
2717 // low order bit should already be clear.
2718 ASSERT((chunk_size & 0x1) == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01002719 page->SetIsLargeObjectPage(true);
Steve Block791712a2010-08-27 10:21:07 +01002720 page->SetIsPageExecutable(executable);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002721 page->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00002722 return HeapObject::FromAddress(object_address);
2723}
2724
2725
2726Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
2727 ASSERT(0 < size_in_bytes);
2728 return AllocateRawInternal(size_in_bytes,
2729 size_in_bytes,
2730 EXECUTABLE);
2731}
2732
2733
2734Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
2735 ASSERT(0 < size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002736 return AllocateRawInternal(size_in_bytes,
Steve Blocka7e24c12009-10-30 11:49:00 +00002737 size_in_bytes,
2738 NOT_EXECUTABLE);
2739}
2740
2741
2742Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2743 ASSERT(0 < size_in_bytes);
2744 return AllocateRawInternal(size_in_bytes,
2745 size_in_bytes,
2746 NOT_EXECUTABLE);
2747}
2748
2749
2750// GC support
2751Object* LargeObjectSpace::FindObject(Address a) {
2752 for (LargeObjectChunk* chunk = first_chunk_;
2753 chunk != NULL;
2754 chunk = chunk->next()) {
2755 Address chunk_address = chunk->address();
2756 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2757 return chunk->GetObject();
2758 }
2759 }
2760 return Failure::Exception();
2761}
2762
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002763
2764LargeObjectChunk* LargeObjectSpace::FindChunkContainingPc(Address pc) {
2765 // TODO(853): Change this implementation to only find executable
2766 // chunks and use some kind of hash-based approach to speed it up.
2767 for (LargeObjectChunk* chunk = first_chunk_;
2768 chunk != NULL;
2769 chunk = chunk->next()) {
2770 Address chunk_address = chunk->address();
2771 if (chunk_address <= pc && pc < chunk_address + chunk->size()) {
2772 return chunk;
2773 }
2774 }
2775 return NULL;
2776}
2777
2778
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002779void LargeObjectSpace::IterateDirtyRegions(ObjectSlotCallback copy_object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002780 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002781 for (HeapObject* object = it.next(); object != NULL; object = it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002782 // We only have code, sequential strings, or fixed arrays in large
2783 // object space, and only fixed arrays can possibly contain pointers to
2784 // the young generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00002785 if (object->IsFixedArray()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002786 Page* page = Page::FromAddress(object->address());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002787 uint32_t marks = page->GetRegionMarks();
2788 uint32_t newmarks = Page::kAllRegionsCleanMarks;
Steve Blocka7e24c12009-10-30 11:49:00 +00002789
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002790 if (marks != Page::kAllRegionsCleanMarks) {
2791 // For a large page a single dirty mark corresponds to several
2792 // regions (modulo 32). So we treat a large page as a sequence of
2793 // normal pages of size Page::kPageSize having same dirty marks
2794 // and subsequently iterate dirty regions on each of these pages.
2795 Address start = object->address();
2796 Address end = page->ObjectAreaEnd();
2797 Address object_end = start + object->Size();
2798
2799 // Iterate regions of the first normal page covering object.
2800 uint32_t first_region_number = page->GetRegionNumberForAddress(start);
2801 newmarks |=
2802 Heap::IterateDirtyRegions(marks >> first_region_number,
2803 start,
2804 end,
2805 &Heap::IteratePointersInDirtyRegion,
2806 copy_object) << first_region_number;
2807
2808 start = end;
2809 end = start + Page::kPageSize;
2810 while (end <= object_end) {
2811 // Iterate next 32 regions.
2812 newmarks |=
2813 Heap::IterateDirtyRegions(marks,
2814 start,
2815 end,
2816 &Heap::IteratePointersInDirtyRegion,
2817 copy_object);
2818 start = end;
2819 end = start + Page::kPageSize;
2820 }
2821
2822 if (start != object_end) {
2823 // Iterate the last piece of an object which is less than
2824 // Page::kPageSize.
2825 newmarks |=
2826 Heap::IterateDirtyRegions(marks,
2827 start,
2828 object_end,
2829 &Heap::IteratePointersInDirtyRegion,
2830 copy_object);
2831 }
2832
2833 page->SetRegionMarks(newmarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00002834 }
2835 }
2836 }
2837}
2838
2839
2840void LargeObjectSpace::FreeUnmarkedObjects() {
2841 LargeObjectChunk* previous = NULL;
2842 LargeObjectChunk* current = first_chunk_;
2843 while (current != NULL) {
2844 HeapObject* object = current->GetObject();
2845 if (object->IsMarked()) {
2846 object->ClearMark();
2847 MarkCompactCollector::tracer()->decrement_marked_count();
2848 previous = current;
2849 current = current->next();
2850 } else {
Steve Block791712a2010-08-27 10:21:07 +01002851 Page* page = Page::FromAddress(RoundUp(current->address(),
2852 Page::kPageSize));
2853 Executability executable =
2854 page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE;
Steve Blocka7e24c12009-10-30 11:49:00 +00002855 Address chunk_address = current->address();
2856 size_t chunk_size = current->size();
2857
2858 // Cut the chunk out from the chunk list.
2859 current = current->next();
2860 if (previous == NULL) {
2861 first_chunk_ = current;
2862 } else {
2863 previous->set_next(current);
2864 }
2865
2866 // Free the chunk.
Leon Clarked91b9f72010-01-27 17:25:45 +00002867 MarkCompactCollector::ReportDeleteIfNeeded(object);
Steve Blockd0582a62009-12-15 09:54:21 +00002868 size_ -= static_cast<int>(chunk_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002869 page_count_--;
Steve Block791712a2010-08-27 10:21:07 +01002870 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size, executable);
Steve Blocka7e24c12009-10-30 11:49:00 +00002871 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2872 }
2873 }
2874}
2875
2876
2877bool LargeObjectSpace::Contains(HeapObject* object) {
2878 Address address = object->address();
Steve Block6ded16b2010-05-10 14:33:55 +01002879 if (Heap::new_space()->Contains(address)) {
2880 return false;
2881 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002882 Page* page = Page::FromAddress(address);
2883
2884 SLOW_ASSERT(!page->IsLargeObjectPage()
2885 || !FindObject(address)->IsFailure());
2886
2887 return page->IsLargeObjectPage();
2888}
2889
2890
2891#ifdef DEBUG
2892// We do not assume that the large object iterator works, because it depends
2893// on the invariants we are checking during verification.
2894void LargeObjectSpace::Verify() {
2895 for (LargeObjectChunk* chunk = first_chunk_;
2896 chunk != NULL;
2897 chunk = chunk->next()) {
2898 // Each chunk contains an object that starts at the large object page's
2899 // object area start.
2900 HeapObject* object = chunk->GetObject();
2901 Page* page = Page::FromAddress(object->address());
2902 ASSERT(object->address() == page->ObjectAreaStart());
2903
2904 // The first word should be a map, and we expect all map pointers to be
2905 // in map space.
2906 Map* map = object->map();
2907 ASSERT(map->IsMap());
2908 ASSERT(Heap::map_space()->Contains(map));
2909
2910 // We have only code, sequential strings, external strings
2911 // (sequential strings that have been morphed into external
2912 // strings), fixed arrays, and byte arrays in large object space.
2913 ASSERT(object->IsCode() || object->IsSeqString() ||
2914 object->IsExternalString() || object->IsFixedArray() ||
2915 object->IsByteArray());
2916
2917 // The object itself should look OK.
2918 object->Verify();
2919
2920 // Byte arrays and strings don't have interior pointers.
2921 if (object->IsCode()) {
2922 VerifyPointersVisitor code_visitor;
2923 object->IterateBody(map->instance_type(),
2924 object->Size(),
2925 &code_visitor);
2926 } else if (object->IsFixedArray()) {
2927 // We loop over fixed arrays ourselves, rather then using the visitor,
2928 // because the visitor doesn't support the start/offset iteration
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002929 // needed for IsRegionDirty.
Steve Blocka7e24c12009-10-30 11:49:00 +00002930 FixedArray* array = FixedArray::cast(object);
2931 for (int j = 0; j < array->length(); j++) {
2932 Object* element = array->get(j);
2933 if (element->IsHeapObject()) {
2934 HeapObject* element_object = HeapObject::cast(element);
2935 ASSERT(Heap::Contains(element_object));
2936 ASSERT(element_object->map()->IsMap());
2937 if (Heap::InNewSpace(element_object)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002938 Address array_addr = object->address();
2939 Address element_addr = array_addr + FixedArray::kHeaderSize +
2940 j * kPointerSize;
2941
2942 ASSERT(Page::FromAddress(array_addr)->IsRegionDirty(element_addr));
Steve Blocka7e24c12009-10-30 11:49:00 +00002943 }
2944 }
2945 }
2946 }
2947 }
2948}
2949
2950
2951void LargeObjectSpace::Print() {
2952 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002953 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
2954 obj->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +00002955 }
2956}
2957
2958
2959void LargeObjectSpace::ReportStatistics() {
2960 PrintF(" size: %d\n", size_);
2961 int num_objects = 0;
2962 ClearHistograms();
2963 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002964 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002965 num_objects++;
Leon Clarked91b9f72010-01-27 17:25:45 +00002966 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002967 }
2968
2969 PrintF(" number of objects %d\n", num_objects);
2970 if (num_objects > 0) ReportHistogram(false);
2971}
2972
2973
2974void LargeObjectSpace::CollectCodeStatistics() {
2975 LargeObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002976 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002977 if (obj->IsCode()) {
2978 Code* code = Code::cast(obj);
2979 code_kind_statistics[code->kind()] += code->Size();
2980 }
2981 }
2982}
Steve Blocka7e24c12009-10-30 11:49:00 +00002983#endif // DEBUG
2984
2985} } // namespace v8::internal