blob: 3d2d42f098b7c0c7c8d0d4cf9cf4428bcbfb8679 [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
Iain Merrick9ac36c92010-09-13 15:29:50 +0100277List<MemoryAllocator::MemoryAllocationCallbackRegistration>
278 MemoryAllocator::memory_allocation_callbacks_;
279
Steve Blocka7e24c12009-10-30 11:49:00 +0000280VirtualMemory* MemoryAllocator::initial_chunk_ = NULL;
281
282// 270 is an estimate based on the static default heap size of a pair of 256K
283// semispaces and a 64M old generation.
284const int kEstimatedNumberOfChunks = 270;
285List<MemoryAllocator::ChunkInfo> MemoryAllocator::chunks_(
286 kEstimatedNumberOfChunks);
287List<int> MemoryAllocator::free_chunk_ids_(kEstimatedNumberOfChunks);
288int MemoryAllocator::max_nof_chunks_ = 0;
289int MemoryAllocator::top_ = 0;
290
291
292void MemoryAllocator::Push(int free_chunk_id) {
293 ASSERT(max_nof_chunks_ > 0);
294 ASSERT(top_ < max_nof_chunks_);
295 free_chunk_ids_[top_++] = free_chunk_id;
296}
297
298
299int MemoryAllocator::Pop() {
300 ASSERT(top_ > 0);
301 return free_chunk_ids_[--top_];
302}
303
304
305bool MemoryAllocator::Setup(int capacity) {
306 capacity_ = RoundUp(capacity, Page::kPageSize);
307
308 // Over-estimate the size of chunks_ array. It assumes the expansion of old
309 // space is always in the unit of a chunk (kChunkSize) except the last
310 // expansion.
311 //
312 // Due to alignment, allocated space might be one page less than required
313 // number (kPagesPerChunk) of pages for old spaces.
314 //
315 // Reserve two chunk ids for semispaces, one for map space, one for old
316 // space, and one for code space.
317 max_nof_chunks_ = (capacity_ / (kChunkSize - Page::kPageSize)) + 5;
318 if (max_nof_chunks_ > kMaxNofChunks) return false;
319
320 size_ = 0;
Steve Block791712a2010-08-27 10:21:07 +0100321 size_executable_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 ChunkInfo info; // uninitialized element.
323 for (int i = max_nof_chunks_ - 1; i >= 0; i--) {
324 chunks_.Add(info);
325 free_chunk_ids_.Add(i);
326 }
327 top_ = max_nof_chunks_;
328 return true;
329}
330
331
332void MemoryAllocator::TearDown() {
333 for (int i = 0; i < max_nof_chunks_; i++) {
334 if (chunks_[i].address() != NULL) DeleteChunk(i);
335 }
336 chunks_.Clear();
337 free_chunk_ids_.Clear();
338
339 if (initial_chunk_ != NULL) {
340 LOG(DeleteEvent("InitialChunk", initial_chunk_->address()));
341 delete initial_chunk_;
342 initial_chunk_ = NULL;
343 }
344
345 ASSERT(top_ == max_nof_chunks_); // all chunks are free
346 top_ = 0;
347 capacity_ = 0;
348 size_ = 0;
349 max_nof_chunks_ = 0;
350}
351
352
353void* MemoryAllocator::AllocateRawMemory(const size_t requested,
354 size_t* allocated,
355 Executability executable) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100356 if (size_ + static_cast<size_t>(requested) > static_cast<size_t>(capacity_)) {
357 return NULL;
358 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 void* mem;
360 if (executable == EXECUTABLE && CodeRange::exists()) {
361 mem = CodeRange::AllocateRawMemory(requested, allocated);
362 } else {
363 mem = OS::Allocate(requested, allocated, (executable == EXECUTABLE));
364 }
Steve Blockd0582a62009-12-15 09:54:21 +0000365 int alloced = static_cast<int>(*allocated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 size_ += alloced;
Steve Block791712a2010-08-27 10:21:07 +0100367
Iain Merrick9ac36c92010-09-13 15:29:50 +0100368 if (executable == EXECUTABLE) size_executable_ += alloced;
Leon Clarke4515c472010-02-03 11:58:03 +0000369#ifdef DEBUG
370 ZapBlock(reinterpret_cast<Address>(mem), alloced);
371#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 Counters::memory_allocated.Increment(alloced);
373 return mem;
374}
375
376
Steve Block791712a2010-08-27 10:21:07 +0100377void MemoryAllocator::FreeRawMemory(void* mem,
378 size_t length,
379 Executability executable) {
Leon Clarke4515c472010-02-03 11:58:03 +0000380#ifdef DEBUG
381 ZapBlock(reinterpret_cast<Address>(mem), length);
382#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000383 if (CodeRange::contains(static_cast<Address>(mem))) {
384 CodeRange::FreeRawMemory(mem, length);
385 } else {
386 OS::Free(mem, length);
387 }
Steve Blockd0582a62009-12-15 09:54:21 +0000388 Counters::memory_allocated.Decrement(static_cast<int>(length));
389 size_ -= static_cast<int>(length);
Steve Block791712a2010-08-27 10:21:07 +0100390 if (executable == EXECUTABLE) size_executable_ -= static_cast<int>(length);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100391
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 ASSERT(size_ >= 0);
393}
394
395
Iain Merrick9ac36c92010-09-13 15:29:50 +0100396void MemoryAllocator::PerformAllocationCallback(ObjectSpace space,
397 AllocationAction action,
398 size_t size) {
399 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
400 MemoryAllocationCallbackRegistration registration =
401 memory_allocation_callbacks_[i];
402 if ((registration.space & space) == space &&
403 (registration.action & action) == action)
404 registration.callback(space, action, static_cast<int>(size));
405 }
406}
407
408
409bool MemoryAllocator::MemoryAllocationCallbackRegistered(
410 MemoryAllocationCallback callback) {
411 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
412 if (memory_allocation_callbacks_[i].callback == callback) return true;
413 }
414 return false;
415}
416
417
418void MemoryAllocator::AddMemoryAllocationCallback(
419 MemoryAllocationCallback callback,
420 ObjectSpace space,
421 AllocationAction action) {
422 ASSERT(callback != NULL);
423 MemoryAllocationCallbackRegistration registration(callback, space, action);
424 ASSERT(!MemoryAllocator::MemoryAllocationCallbackRegistered(callback));
425 return memory_allocation_callbacks_.Add(registration);
426}
427
428
429void MemoryAllocator::RemoveMemoryAllocationCallback(
430 MemoryAllocationCallback callback) {
431 ASSERT(callback != NULL);
432 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
433 if (memory_allocation_callbacks_[i].callback == callback) {
434 memory_allocation_callbacks_.Remove(i);
435 return;
436 }
437 }
438 UNREACHABLE();
439}
440
Steve Blocka7e24c12009-10-30 11:49:00 +0000441void* MemoryAllocator::ReserveInitialChunk(const size_t requested) {
442 ASSERT(initial_chunk_ == NULL);
443
444 initial_chunk_ = new VirtualMemory(requested);
445 CHECK(initial_chunk_ != NULL);
446 if (!initial_chunk_->IsReserved()) {
447 delete initial_chunk_;
448 initial_chunk_ = NULL;
449 return NULL;
450 }
451
452 // We are sure that we have mapped a block of requested addresses.
453 ASSERT(initial_chunk_->size() == requested);
454 LOG(NewEvent("InitialChunk", initial_chunk_->address(), requested));
Steve Blockd0582a62009-12-15 09:54:21 +0000455 size_ += static_cast<int>(requested);
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 return initial_chunk_->address();
457}
458
459
460static int PagesInChunk(Address start, size_t size) {
461 // The first page starts on the first page-aligned address from start onward
462 // and the last page ends on the last page-aligned address before
463 // start+size. Page::kPageSize is a power of two so we can divide by
464 // shifting.
Steve Blockd0582a62009-12-15 09:54:21 +0000465 return static_cast<int>((RoundDown(start + size, Page::kPageSize)
Leon Clarkee46be812010-01-19 14:06:41 +0000466 - RoundUp(start, Page::kPageSize)) >> kPageSizeBits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000467}
468
469
470Page* MemoryAllocator::AllocatePages(int requested_pages, int* allocated_pages,
471 PagedSpace* owner) {
472 if (requested_pages <= 0) return Page::FromAddress(NULL);
473 size_t chunk_size = requested_pages * Page::kPageSize;
474
475 // There is not enough space to guarantee the desired number pages can be
476 // allocated.
477 if (size_ + static_cast<int>(chunk_size) > capacity_) {
478 // Request as many pages as we can.
479 chunk_size = capacity_ - size_;
Leon Clarkee46be812010-01-19 14:06:41 +0000480 requested_pages = static_cast<int>(chunk_size >> kPageSizeBits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000481
482 if (requested_pages <= 0) return Page::FromAddress(NULL);
483 }
484 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
485 if (chunk == NULL) return Page::FromAddress(NULL);
486 LOG(NewEvent("PagedChunk", chunk, chunk_size));
487
488 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
489 if (*allocated_pages == 0) {
Steve Block791712a2010-08-27 10:21:07 +0100490 FreeRawMemory(chunk, chunk_size, owner->executable());
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 LOG(DeleteEvent("PagedChunk", chunk));
492 return Page::FromAddress(NULL);
493 }
494
495 int chunk_id = Pop();
496 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
497
Iain Merrick9ac36c92010-09-13 15:29:50 +0100498 ObjectSpace space = static_cast<ObjectSpace>(1 << owner->identity());
499 PerformAllocationCallback(space, kAllocationActionAllocate, chunk_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 return InitializePagesInChunk(chunk_id, *allocated_pages, owner);
501}
502
503
504Page* MemoryAllocator::CommitPages(Address start, size_t size,
505 PagedSpace* owner, int* num_pages) {
506 ASSERT(start != NULL);
507 *num_pages = PagesInChunk(start, size);
508 ASSERT(*num_pages > 0);
509 ASSERT(initial_chunk_ != NULL);
510 ASSERT(InInitialChunk(start));
511 ASSERT(InInitialChunk(start + size - 1));
512 if (!initial_chunk_->Commit(start, size, owner->executable() == EXECUTABLE)) {
513 return Page::FromAddress(NULL);
514 }
Leon Clarke4515c472010-02-03 11:58:03 +0000515#ifdef DEBUG
516 ZapBlock(start, size);
517#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000518 Counters::memory_allocated.Increment(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000519
520 // So long as we correctly overestimated the number of chunks we should not
521 // run out of chunk ids.
522 CHECK(!OutOfChunkIds());
523 int chunk_id = Pop();
524 chunks_[chunk_id].init(start, size, owner);
525 return InitializePagesInChunk(chunk_id, *num_pages, owner);
526}
527
528
529bool MemoryAllocator::CommitBlock(Address start,
530 size_t size,
531 Executability executable) {
532 ASSERT(start != NULL);
533 ASSERT(size > 0);
534 ASSERT(initial_chunk_ != NULL);
535 ASSERT(InInitialChunk(start));
536 ASSERT(InInitialChunk(start + size - 1));
537
538 if (!initial_chunk_->Commit(start, size, executable)) return false;
Leon Clarke4515c472010-02-03 11:58:03 +0000539#ifdef DEBUG
540 ZapBlock(start, size);
541#endif
Steve Blockd0582a62009-12-15 09:54:21 +0000542 Counters::memory_allocated.Increment(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000543 return true;
544}
545
Leon Clarke4515c472010-02-03 11:58:03 +0000546
Steve Blocka7e24c12009-10-30 11:49:00 +0000547bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
548 ASSERT(start != NULL);
549 ASSERT(size > 0);
550 ASSERT(initial_chunk_ != NULL);
551 ASSERT(InInitialChunk(start));
552 ASSERT(InInitialChunk(start + size - 1));
553
554 if (!initial_chunk_->Uncommit(start, size)) return false;
Steve Blockd0582a62009-12-15 09:54:21 +0000555 Counters::memory_allocated.Decrement(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 return true;
557}
558
Leon Clarke4515c472010-02-03 11:58:03 +0000559
560void MemoryAllocator::ZapBlock(Address start, size_t size) {
561 for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) {
562 Memory::Address_at(start + s) = kZapValue;
563 }
564}
565
566
Steve Blocka7e24c12009-10-30 11:49:00 +0000567Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
568 PagedSpace* owner) {
569 ASSERT(IsValidChunk(chunk_id));
570 ASSERT(pages_in_chunk > 0);
571
572 Address chunk_start = chunks_[chunk_id].address();
573
574 Address low = RoundUp(chunk_start, Page::kPageSize);
575
576#ifdef DEBUG
577 size_t chunk_size = chunks_[chunk_id].size();
578 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
579 ASSERT(pages_in_chunk <=
580 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
581#endif
582
583 Address page_addr = low;
584 for (int i = 0; i < pages_in_chunk; i++) {
585 Page* p = Page::FromAddress(page_addr);
586 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100587 p->InvalidateWatermark(true);
Steve Block6ded16b2010-05-10 14:33:55 +0100588 p->SetIsLargeObjectPage(false);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100589 p->SetAllocationWatermark(p->ObjectAreaStart());
590 p->SetCachedAllocationWatermark(p->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +0000591 page_addr += Page::kPageSize;
592 }
593
594 // Set the next page of the last page to 0.
595 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
596 last_page->opaque_header = OffsetFrom(0) | chunk_id;
597
598 return Page::FromAddress(low);
599}
600
601
602Page* MemoryAllocator::FreePages(Page* p) {
603 if (!p->is_valid()) return p;
604
605 // Find the first page in the same chunk as 'p'
606 Page* first_page = FindFirstPageInSameChunk(p);
607 Page* page_to_return = Page::FromAddress(NULL);
608
609 if (p != first_page) {
610 // Find the last page in the same chunk as 'prev'.
611 Page* last_page = FindLastPageInSameChunk(p);
612 first_page = GetNextPage(last_page); // first page in next chunk
613
614 // set the next_page of last_page to NULL
615 SetNextPage(last_page, Page::FromAddress(NULL));
616 page_to_return = p; // return 'p' when exiting
617 }
618
619 while (first_page->is_valid()) {
620 int chunk_id = GetChunkId(first_page);
621 ASSERT(IsValidChunk(chunk_id));
622
623 // Find the first page of the next chunk before deleting this chunk.
624 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
625
626 // Free the current chunk.
627 DeleteChunk(chunk_id);
628 }
629
630 return page_to_return;
631}
632
633
Steve Block6ded16b2010-05-10 14:33:55 +0100634void MemoryAllocator::FreeAllPages(PagedSpace* space) {
635 for (int i = 0, length = chunks_.length(); i < length; i++) {
636 if (chunks_[i].owner() == space) {
637 DeleteChunk(i);
638 }
639 }
640}
641
642
Steve Blocka7e24c12009-10-30 11:49:00 +0000643void MemoryAllocator::DeleteChunk(int chunk_id) {
644 ASSERT(IsValidChunk(chunk_id));
645
646 ChunkInfo& c = chunks_[chunk_id];
647
648 // We cannot free a chunk contained in the initial chunk because it was not
649 // allocated with AllocateRawMemory. Instead we uncommit the virtual
650 // memory.
651 if (InInitialChunk(c.address())) {
652 // TODO(1240712): VirtualMemory::Uncommit has a return value which
653 // is ignored here.
654 initial_chunk_->Uncommit(c.address(), c.size());
Steve Blockd0582a62009-12-15 09:54:21 +0000655 Counters::memory_allocated.Decrement(static_cast<int>(c.size()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 } else {
657 LOG(DeleteEvent("PagedChunk", c.address()));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100658 ObjectSpace space = static_cast<ObjectSpace>(1 << c.owner()->identity());
659 size_t size = c.size();
660 FreeRawMemory(c.address(), size, c.executable());
661 PerformAllocationCallback(space, kAllocationActionFree, size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000662 }
663 c.init(NULL, 0, NULL);
664 Push(chunk_id);
665}
666
667
668Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
669 int chunk_id = GetChunkId(p);
670 ASSERT(IsValidChunk(chunk_id));
671
672 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
673 return Page::FromAddress(low);
674}
675
676
677Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
678 int chunk_id = GetChunkId(p);
679 ASSERT(IsValidChunk(chunk_id));
680
681 Address chunk_start = chunks_[chunk_id].address();
682 size_t chunk_size = chunks_[chunk_id].size();
683
684 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
685 ASSERT(chunk_start <= p->address() && p->address() < high);
686
687 return Page::FromAddress(high - Page::kPageSize);
688}
689
690
691#ifdef DEBUG
692void MemoryAllocator::ReportStatistics() {
693 float pct = static_cast<float>(capacity_ - size_) / capacity_;
694 PrintF(" capacity: %d, used: %d, available: %%%d\n\n",
695 capacity_, size_, static_cast<int>(pct*100));
696}
697#endif
698
699
Steve Block6ded16b2010-05-10 14:33:55 +0100700void MemoryAllocator::RelinkPageListInChunkOrder(PagedSpace* space,
701 Page** first_page,
702 Page** last_page,
703 Page** last_page_in_use) {
704 Page* first = NULL;
705 Page* last = NULL;
706
707 for (int i = 0, length = chunks_.length(); i < length; i++) {
708 ChunkInfo& chunk = chunks_[i];
709
710 if (chunk.owner() == space) {
711 if (first == NULL) {
712 Address low = RoundUp(chunk.address(), Page::kPageSize);
713 first = Page::FromAddress(low);
714 }
715 last = RelinkPagesInChunk(i,
716 chunk.address(),
717 chunk.size(),
718 last,
719 last_page_in_use);
720 }
721 }
722
723 if (first_page != NULL) {
724 *first_page = first;
725 }
726
727 if (last_page != NULL) {
728 *last_page = last;
729 }
730}
731
732
733Page* MemoryAllocator::RelinkPagesInChunk(int chunk_id,
734 Address chunk_start,
735 size_t chunk_size,
736 Page* prev,
737 Page** last_page_in_use) {
738 Address page_addr = RoundUp(chunk_start, Page::kPageSize);
739 int pages_in_chunk = PagesInChunk(chunk_start, chunk_size);
740
741 if (prev->is_valid()) {
742 SetNextPage(prev, Page::FromAddress(page_addr));
743 }
744
745 for (int i = 0; i < pages_in_chunk; i++) {
746 Page* p = Page::FromAddress(page_addr);
747 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
748 page_addr += Page::kPageSize;
749
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100750 p->InvalidateWatermark(true);
Steve Block6ded16b2010-05-10 14:33:55 +0100751 if (p->WasInUseBeforeMC()) {
752 *last_page_in_use = p;
753 }
754 }
755
756 // Set the next page of the last page to 0.
757 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
758 last_page->opaque_header = OffsetFrom(0) | chunk_id;
759
760 if (last_page->WasInUseBeforeMC()) {
761 *last_page_in_use = last_page;
762 }
763
764 return last_page;
765}
766
767
768
Steve Blocka7e24c12009-10-30 11:49:00 +0000769// -----------------------------------------------------------------------------
770// PagedSpace implementation
771
772PagedSpace::PagedSpace(int max_capacity,
773 AllocationSpace id,
774 Executability executable)
775 : Space(id, executable) {
776 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
777 * Page::kObjectAreaSize;
778 accounting_stats_.Clear();
779
780 allocation_info_.top = NULL;
781 allocation_info_.limit = NULL;
782
783 mc_forwarding_info_.top = NULL;
784 mc_forwarding_info_.limit = NULL;
785}
786
787
788bool PagedSpace::Setup(Address start, size_t size) {
789 if (HasBeenSetup()) return false;
790
791 int num_pages = 0;
792 // Try to use the virtual memory range passed to us. If it is too small to
793 // contain at least one page, ignore it and allocate instead.
794 int pages_in_chunk = PagesInChunk(start, size);
795 if (pages_in_chunk > 0) {
796 first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize),
797 Page::kPageSize * pages_in_chunk,
798 this, &num_pages);
799 } else {
800 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
801 max_capacity_ / Page::kObjectAreaSize);
802 first_page_ =
803 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
804 if (!first_page_->is_valid()) return false;
805 }
806
807 // We are sure that the first page is valid and that we have at least one
808 // page.
809 ASSERT(first_page_->is_valid());
810 ASSERT(num_pages > 0);
811 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
812 ASSERT(Capacity() <= max_capacity_);
813
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100814 // Sequentially clear region marks in the newly allocated
Steve Blocka7e24c12009-10-30 11:49:00 +0000815 // pages and cache the current last page in the space.
816 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100817 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000818 last_page_ = p;
819 }
820
821 // Use first_page_ for allocation.
822 SetAllocationInfo(&allocation_info_, first_page_);
823
Steve Block6ded16b2010-05-10 14:33:55 +0100824 page_list_is_chunk_ordered_ = true;
825
Steve Blocka7e24c12009-10-30 11:49:00 +0000826 return true;
827}
828
829
830bool PagedSpace::HasBeenSetup() {
831 return (Capacity() > 0);
832}
833
834
835void PagedSpace::TearDown() {
Steve Block6ded16b2010-05-10 14:33:55 +0100836 MemoryAllocator::FreeAllPages(this);
837 first_page_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 accounting_stats_.Clear();
839}
840
841
842#ifdef ENABLE_HEAP_PROTECTION
843
844void PagedSpace::Protect() {
845 Page* page = first_page_;
846 while (page->is_valid()) {
847 MemoryAllocator::ProtectChunkFromPage(page);
848 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
849 }
850}
851
852
853void PagedSpace::Unprotect() {
854 Page* page = first_page_;
855 while (page->is_valid()) {
856 MemoryAllocator::UnprotectChunkFromPage(page);
857 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
858 }
859}
860
861#endif
862
863
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100864void PagedSpace::MarkAllPagesClean() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 PageIterator it(this, PageIterator::ALL_PAGES);
866 while (it.has_next()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100867 it.next()->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000868 }
869}
870
871
872Object* PagedSpace::FindObject(Address addr) {
873 // Note: this function can only be called before or after mark-compact GC
874 // because it accesses map pointers.
875 ASSERT(!MarkCompactCollector::in_use());
876
877 if (!Contains(addr)) return Failure::Exception();
878
879 Page* p = Page::FromAddress(addr);
880 ASSERT(IsUsed(p));
881 Address cur = p->ObjectAreaStart();
882 Address end = p->AllocationTop();
883 while (cur < end) {
884 HeapObject* obj = HeapObject::FromAddress(cur);
885 Address next = cur + obj->Size();
886 if ((cur <= addr) && (addr < next)) return obj;
887 cur = next;
888 }
889
890 UNREACHABLE();
891 return Failure::Exception();
892}
893
894
895bool PagedSpace::IsUsed(Page* page) {
896 PageIterator it(this, PageIterator::PAGES_IN_USE);
897 while (it.has_next()) {
898 if (page == it.next()) return true;
899 }
900 return false;
901}
902
903
904void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
905 alloc_info->top = p->ObjectAreaStart();
906 alloc_info->limit = p->ObjectAreaEnd();
907 ASSERT(alloc_info->VerifyPagedAllocation());
908}
909
910
911void PagedSpace::MCResetRelocationInfo() {
912 // Set page indexes.
913 int i = 0;
914 PageIterator it(this, PageIterator::ALL_PAGES);
915 while (it.has_next()) {
916 Page* p = it.next();
917 p->mc_page_index = i++;
918 }
919
920 // Set mc_forwarding_info_ to the first page in the space.
921 SetAllocationInfo(&mc_forwarding_info_, first_page_);
922 // All the bytes in the space are 'available'. We will rediscover
923 // allocated and wasted bytes during GC.
924 accounting_stats_.Reset();
925}
926
927
928int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
929#ifdef DEBUG
930 // The Contains function considers the address at the beginning of a
931 // page in the page, MCSpaceOffsetForAddress considers it is in the
932 // previous page.
933 if (Page::IsAlignedToPageSize(addr)) {
934 ASSERT(Contains(addr - kPointerSize));
935 } else {
936 ASSERT(Contains(addr));
937 }
938#endif
939
940 // If addr is at the end of a page, it belongs to previous page
941 Page* p = Page::IsAlignedToPageSize(addr)
942 ? Page::FromAllocationTop(addr)
943 : Page::FromAddress(addr);
944 int index = p->mc_page_index;
945 return (index * Page::kPageSize) + p->Offset(addr);
946}
947
948
949// Slow case for reallocating and promoting objects during a compacting
950// collection. This function is not space-specific.
951HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
952 Page* current_page = TopPageOf(mc_forwarding_info_);
953 if (!current_page->next_page()->is_valid()) {
954 if (!Expand(current_page)) {
955 return NULL;
956 }
957 }
958
959 // There are surely more pages in the space now.
960 ASSERT(current_page->next_page()->is_valid());
961 // We do not add the top of page block for current page to the space's
962 // free list---the block may contain live objects so we cannot write
963 // bookkeeping information to it. Instead, we will recover top of page
964 // blocks when we move objects to their new locations.
965 //
966 // We do however write the allocation pointer to the page. The encoding
967 // of forwarding addresses is as an offset in terms of live bytes, so we
968 // need quick access to the allocation top of each page to decode
969 // forwarding addresses.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100970 current_page->SetAllocationWatermark(mc_forwarding_info_.top);
971 current_page->next_page()->InvalidateWatermark(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
973 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
974}
975
976
977bool PagedSpace::Expand(Page* last_page) {
978 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
979 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
980
981 if (Capacity() == max_capacity_) return false;
982
983 ASSERT(Capacity() < max_capacity_);
984 // Last page must be valid and its next page is invalid.
985 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
986
987 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
988 if (available_pages <= 0) return false;
989
990 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
991 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
992 if (!p->is_valid()) return false;
993
994 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
995 ASSERT(Capacity() <= max_capacity_);
996
997 MemoryAllocator::SetNextPage(last_page, p);
998
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100999 // Sequentially clear region marks of new pages and and cache the
Steve Blocka7e24c12009-10-30 11:49:00 +00001000 // new last page in the space.
1001 while (p->is_valid()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001002 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00001003 last_page_ = p;
1004 p = p->next_page();
1005 }
1006
1007 return true;
1008}
1009
1010
1011#ifdef DEBUG
1012int PagedSpace::CountTotalPages() {
1013 int count = 0;
1014 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
1015 count++;
1016 }
1017 return count;
1018}
1019#endif
1020
1021
1022void PagedSpace::Shrink() {
Steve Block6ded16b2010-05-10 14:33:55 +01001023 if (!page_list_is_chunk_ordered_) {
1024 // We can't shrink space if pages is not chunk-ordered
1025 // (see comment for class MemoryAllocator for definition).
1026 return;
1027 }
1028
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 // Release half of free pages.
1030 Page* top_page = AllocationTopPage();
1031 ASSERT(top_page->is_valid());
1032
1033 // Count the number of pages we would like to free.
1034 int pages_to_free = 0;
1035 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
1036 pages_to_free++;
1037 }
1038
1039 // Free pages after top_page.
1040 Page* p = MemoryAllocator::FreePages(top_page->next_page());
1041 MemoryAllocator::SetNextPage(top_page, p);
1042
1043 // Find out how many pages we failed to free and update last_page_.
1044 // Please note pages can only be freed in whole chunks.
1045 last_page_ = top_page;
1046 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
1047 pages_to_free--;
1048 last_page_ = p;
1049 }
1050
1051 accounting_stats_.ShrinkSpace(pages_to_free * Page::kObjectAreaSize);
1052 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
1053}
1054
1055
1056bool PagedSpace::EnsureCapacity(int capacity) {
1057 if (Capacity() >= capacity) return true;
1058
1059 // Start from the allocation top and loop to the last page in the space.
1060 Page* last_page = AllocationTopPage();
1061 Page* next_page = last_page->next_page();
1062 while (next_page->is_valid()) {
1063 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
1064 next_page = last_page->next_page();
1065 }
1066
1067 // Expand the space until it has the required capacity or expansion fails.
1068 do {
1069 if (!Expand(last_page)) return false;
1070 ASSERT(last_page->next_page()->is_valid());
1071 last_page =
1072 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
1073 } while (Capacity() < capacity);
1074
1075 return true;
1076}
1077
1078
1079#ifdef DEBUG
1080void PagedSpace::Print() { }
1081#endif
1082
1083
1084#ifdef DEBUG
1085// We do not assume that the PageIterator works, because it depends on the
1086// invariants we are checking during verification.
1087void PagedSpace::Verify(ObjectVisitor* visitor) {
1088 // The allocation pointer should be valid, and it should be in a page in the
1089 // space.
1090 ASSERT(allocation_info_.VerifyPagedAllocation());
1091 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
1092 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
1093
1094 // Loop over all the pages.
1095 bool above_allocation_top = false;
1096 Page* current_page = first_page_;
1097 while (current_page->is_valid()) {
1098 if (above_allocation_top) {
1099 // We don't care what's above the allocation top.
1100 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00001101 Address top = current_page->AllocationTop();
1102 if (current_page == top_page) {
1103 ASSERT(top == allocation_info_.top);
1104 // The next page will be above the allocation top.
1105 above_allocation_top = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001106 }
1107
1108 // It should be packed with objects from the bottom to the top.
1109 Address current = current_page->ObjectAreaStart();
1110 while (current < top) {
1111 HeapObject* object = HeapObject::FromAddress(current);
1112
1113 // The first word should be a map, and we expect all map pointers to
1114 // be in map space.
1115 Map* map = object->map();
1116 ASSERT(map->IsMap());
1117 ASSERT(Heap::map_space()->Contains(map));
1118
1119 // Perform space-specific object verification.
1120 VerifyObject(object);
1121
1122 // The object itself should look OK.
1123 object->Verify();
1124
1125 // All the interior pointers should be contained in the heap and
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001126 // have page regions covering intergenerational references should be
1127 // marked dirty.
Steve Blocka7e24c12009-10-30 11:49:00 +00001128 int size = object->Size();
1129 object->IterateBody(map->instance_type(), size, visitor);
1130
1131 current += size;
1132 }
1133
1134 // The allocation pointer should not be in the middle of an object.
1135 ASSERT(current == top);
1136 }
1137
1138 current_page = current_page->next_page();
1139 }
1140}
1141#endif
1142
1143
1144// -----------------------------------------------------------------------------
1145// NewSpace implementation
1146
1147
1148bool NewSpace::Setup(Address start, int size) {
1149 // Setup new space based on the preallocated memory block defined by
1150 // start and size. The provided space is divided into two semi-spaces.
1151 // To support fast containment testing in the new space, the size of
1152 // this chunk must be a power of two and it must be aligned to its size.
1153 int initial_semispace_capacity = Heap::InitialSemiSpaceSize();
Steve Block3ce2e202009-11-05 08:53:23 +00001154 int maximum_semispace_capacity = Heap::MaxSemiSpaceSize();
Steve Blocka7e24c12009-10-30 11:49:00 +00001155
1156 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
1157 ASSERT(IsPowerOf2(maximum_semispace_capacity));
1158
1159 // Allocate and setup the histogram arrays if necessary.
1160#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1161 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1162 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1163
1164#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
1165 promoted_histogram_[name].set_name(#name);
1166 INSTANCE_TYPE_LIST(SET_NAME)
1167#undef SET_NAME
1168#endif
1169
Steve Block3ce2e202009-11-05 08:53:23 +00001170 ASSERT(size == 2 * Heap::ReservedSemiSpaceSize());
Steve Blocka7e24c12009-10-30 11:49:00 +00001171 ASSERT(IsAddressAligned(start, size, 0));
1172
1173 if (!to_space_.Setup(start,
1174 initial_semispace_capacity,
1175 maximum_semispace_capacity)) {
1176 return false;
1177 }
1178 if (!from_space_.Setup(start + maximum_semispace_capacity,
1179 initial_semispace_capacity,
1180 maximum_semispace_capacity)) {
1181 return false;
1182 }
1183
1184 start_ = start;
1185 address_mask_ = ~(size - 1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001186 object_mask_ = address_mask_ | kHeapObjectTagMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00001187 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
1188
1189 allocation_info_.top = to_space_.low();
1190 allocation_info_.limit = to_space_.high();
1191 mc_forwarding_info_.top = NULL;
1192 mc_forwarding_info_.limit = NULL;
1193
1194 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1195 return true;
1196}
1197
1198
1199void NewSpace::TearDown() {
1200#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1201 if (allocated_histogram_) {
1202 DeleteArray(allocated_histogram_);
1203 allocated_histogram_ = NULL;
1204 }
1205 if (promoted_histogram_) {
1206 DeleteArray(promoted_histogram_);
1207 promoted_histogram_ = NULL;
1208 }
1209#endif
1210
1211 start_ = NULL;
1212 allocation_info_.top = NULL;
1213 allocation_info_.limit = NULL;
1214 mc_forwarding_info_.top = NULL;
1215 mc_forwarding_info_.limit = NULL;
1216
1217 to_space_.TearDown();
1218 from_space_.TearDown();
1219}
1220
1221
1222#ifdef ENABLE_HEAP_PROTECTION
1223
1224void NewSpace::Protect() {
1225 MemoryAllocator::Protect(ToSpaceLow(), Capacity());
1226 MemoryAllocator::Protect(FromSpaceLow(), Capacity());
1227}
1228
1229
1230void NewSpace::Unprotect() {
1231 MemoryAllocator::Unprotect(ToSpaceLow(), Capacity(),
1232 to_space_.executable());
1233 MemoryAllocator::Unprotect(FromSpaceLow(), Capacity(),
1234 from_space_.executable());
1235}
1236
1237#endif
1238
1239
1240void NewSpace::Flip() {
1241 SemiSpace tmp = from_space_;
1242 from_space_ = to_space_;
1243 to_space_ = tmp;
1244}
1245
1246
1247void NewSpace::Grow() {
1248 ASSERT(Capacity() < MaximumCapacity());
1249 if (to_space_.Grow()) {
1250 // Only grow from space if we managed to grow to space.
1251 if (!from_space_.Grow()) {
1252 // If we managed to grow to space but couldn't grow from space,
1253 // attempt to shrink to space.
1254 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
1255 // We are in an inconsistent state because we could not
1256 // commit/uncommit memory from new space.
1257 V8::FatalProcessOutOfMemory("Failed to grow new space.");
1258 }
1259 }
1260 }
1261 allocation_info_.limit = to_space_.high();
1262 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1263}
1264
1265
1266void NewSpace::Shrink() {
1267 int new_capacity = Max(InitialCapacity(), 2 * Size());
Steve Blockd0582a62009-12-15 09:54:21 +00001268 int rounded_new_capacity =
1269 RoundUp(new_capacity, static_cast<int>(OS::AllocateAlignment()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001270 if (rounded_new_capacity < Capacity() &&
1271 to_space_.ShrinkTo(rounded_new_capacity)) {
1272 // Only shrink from space if we managed to shrink to space.
1273 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
1274 // If we managed to shrink to space but couldn't shrink from
1275 // space, attempt to grow to space again.
1276 if (!to_space_.GrowTo(from_space_.Capacity())) {
1277 // We are in an inconsistent state because we could not
1278 // commit/uncommit memory from new space.
1279 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
1280 }
1281 }
1282 }
1283 allocation_info_.limit = to_space_.high();
1284 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1285}
1286
1287
1288void NewSpace::ResetAllocationInfo() {
1289 allocation_info_.top = to_space_.low();
1290 allocation_info_.limit = to_space_.high();
1291 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1292}
1293
1294
1295void NewSpace::MCResetRelocationInfo() {
1296 mc_forwarding_info_.top = from_space_.low();
1297 mc_forwarding_info_.limit = from_space_.high();
1298 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
1299}
1300
1301
1302void NewSpace::MCCommitRelocationInfo() {
1303 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
1304 // valid allocation info for the to space.
1305 allocation_info_.top = mc_forwarding_info_.top;
1306 allocation_info_.limit = to_space_.high();
1307 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1308}
1309
1310
1311#ifdef DEBUG
1312// We do not use the SemispaceIterator because verification doesn't assume
1313// that it works (it depends on the invariants we are checking).
1314void NewSpace::Verify() {
1315 // The allocation pointer should be in the space or at the very end.
1316 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1317
1318 // There should be objects packed in from the low address up to the
1319 // allocation pointer.
1320 Address current = to_space_.low();
1321 while (current < top()) {
1322 HeapObject* object = HeapObject::FromAddress(current);
1323
1324 // The first word should be a map, and we expect all map pointers to
1325 // be in map space.
1326 Map* map = object->map();
1327 ASSERT(map->IsMap());
1328 ASSERT(Heap::map_space()->Contains(map));
1329
1330 // The object should not be code or a map.
1331 ASSERT(!object->IsMap());
1332 ASSERT(!object->IsCode());
1333
1334 // The object itself should look OK.
1335 object->Verify();
1336
1337 // All the interior pointers should be contained in the heap.
1338 VerifyPointersVisitor visitor;
1339 int size = object->Size();
1340 object->IterateBody(map->instance_type(), size, &visitor);
1341
1342 current += size;
1343 }
1344
1345 // The allocation pointer should not be in the middle of an object.
1346 ASSERT(current == top());
1347}
1348#endif
1349
1350
1351bool SemiSpace::Commit() {
1352 ASSERT(!is_committed());
1353 if (!MemoryAllocator::CommitBlock(start_, capacity_, executable())) {
1354 return false;
1355 }
1356 committed_ = true;
1357 return true;
1358}
1359
1360
1361bool SemiSpace::Uncommit() {
1362 ASSERT(is_committed());
1363 if (!MemoryAllocator::UncommitBlock(start_, capacity_)) {
1364 return false;
1365 }
1366 committed_ = false;
1367 return true;
1368}
1369
1370
1371// -----------------------------------------------------------------------------
1372// SemiSpace implementation
1373
1374bool SemiSpace::Setup(Address start,
1375 int initial_capacity,
1376 int maximum_capacity) {
1377 // Creates a space in the young generation. The constructor does not
1378 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1379 // memory of size 'capacity' when set up, and does not grow or shrink
1380 // otherwise. In the mark-compact collector, the memory region of the from
1381 // space is used as the marking stack. It requires contiguous memory
1382 // addresses.
1383 initial_capacity_ = initial_capacity;
1384 capacity_ = initial_capacity;
1385 maximum_capacity_ = maximum_capacity;
1386 committed_ = false;
1387
1388 start_ = start;
1389 address_mask_ = ~(maximum_capacity - 1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001390 object_mask_ = address_mask_ | kHeapObjectTagMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00001391 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
1392 age_mark_ = start_;
1393
1394 return Commit();
1395}
1396
1397
1398void SemiSpace::TearDown() {
1399 start_ = NULL;
1400 capacity_ = 0;
1401}
1402
1403
1404bool SemiSpace::Grow() {
1405 // Double the semispace size but only up to maximum capacity.
1406 int maximum_extra = maximum_capacity_ - capacity_;
Steve Blockd0582a62009-12-15 09:54:21 +00001407 int extra = Min(RoundUp(capacity_, static_cast<int>(OS::AllocateAlignment())),
Steve Blocka7e24c12009-10-30 11:49:00 +00001408 maximum_extra);
1409 if (!MemoryAllocator::CommitBlock(high(), extra, executable())) {
1410 return false;
1411 }
1412 capacity_ += extra;
1413 return true;
1414}
1415
1416
1417bool SemiSpace::GrowTo(int new_capacity) {
1418 ASSERT(new_capacity <= maximum_capacity_);
1419 ASSERT(new_capacity > capacity_);
1420 size_t delta = new_capacity - capacity_;
1421 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1422 if (!MemoryAllocator::CommitBlock(high(), delta, executable())) {
1423 return false;
1424 }
1425 capacity_ = new_capacity;
1426 return true;
1427}
1428
1429
1430bool SemiSpace::ShrinkTo(int new_capacity) {
1431 ASSERT(new_capacity >= initial_capacity_);
1432 ASSERT(new_capacity < capacity_);
1433 size_t delta = capacity_ - new_capacity;
1434 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1435 if (!MemoryAllocator::UncommitBlock(high() - delta, delta)) {
1436 return false;
1437 }
1438 capacity_ = new_capacity;
1439 return true;
1440}
1441
1442
1443#ifdef DEBUG
1444void SemiSpace::Print() { }
1445
1446
1447void SemiSpace::Verify() { }
1448#endif
1449
1450
1451// -----------------------------------------------------------------------------
1452// SemiSpaceIterator implementation.
1453SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
1454 Initialize(space, space->bottom(), space->top(), NULL);
1455}
1456
1457
1458SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1459 HeapObjectCallback size_func) {
1460 Initialize(space, space->bottom(), space->top(), size_func);
1461}
1462
1463
1464SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1465 Initialize(space, start, space->top(), NULL);
1466}
1467
1468
1469void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1470 Address end,
1471 HeapObjectCallback size_func) {
1472 ASSERT(space->ToSpaceContains(start));
1473 ASSERT(space->ToSpaceLow() <= end
1474 && end <= space->ToSpaceHigh());
1475 space_ = &space->to_space_;
1476 current_ = start;
1477 limit_ = end;
1478 size_func_ = size_func;
1479}
1480
1481
1482#ifdef DEBUG
1483// A static array of histogram info for each type.
1484static HistogramInfo heap_histograms[LAST_TYPE+1];
1485static JSObject::SpillInformation js_spill_information;
1486
1487// heap_histograms is shared, always clear it before using it.
1488static void ClearHistograms() {
1489 // We reset the name each time, though it hasn't changed.
1490#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1491 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1492#undef DEF_TYPE_NAME
1493
1494#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1495 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1496#undef CLEAR_HISTOGRAM
1497
1498 js_spill_information.Clear();
1499}
1500
1501
1502static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1503
1504
1505static void ClearCodeKindStatistics() {
1506 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1507 code_kind_statistics[i] = 0;
1508 }
1509}
1510
1511
1512static void ReportCodeKindStatistics() {
Steve Block6ded16b2010-05-10 14:33:55 +01001513 const char* table[Code::NUMBER_OF_KINDS] = { NULL };
Steve Blocka7e24c12009-10-30 11:49:00 +00001514
1515#define CASE(name) \
1516 case Code::name: table[Code::name] = #name; \
1517 break
1518
1519 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1520 switch (static_cast<Code::Kind>(i)) {
1521 CASE(FUNCTION);
1522 CASE(STUB);
1523 CASE(BUILTIN);
1524 CASE(LOAD_IC);
1525 CASE(KEYED_LOAD_IC);
1526 CASE(STORE_IC);
1527 CASE(KEYED_STORE_IC);
1528 CASE(CALL_IC);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001529 CASE(KEYED_CALL_IC);
Steve Block6ded16b2010-05-10 14:33:55 +01001530 CASE(BINARY_OP_IC);
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 }
1532 }
1533
1534#undef CASE
1535
1536 PrintF("\n Code kind histograms: \n");
1537 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1538 if (code_kind_statistics[i] > 0) {
1539 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1540 }
1541 }
1542 PrintF("\n");
1543}
1544
1545
1546static int CollectHistogramInfo(HeapObject* obj) {
1547 InstanceType type = obj->map()->instance_type();
1548 ASSERT(0 <= type && type <= LAST_TYPE);
1549 ASSERT(heap_histograms[type].name() != NULL);
1550 heap_histograms[type].increment_number(1);
1551 heap_histograms[type].increment_bytes(obj->Size());
1552
1553 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1554 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1555 }
1556
1557 return obj->Size();
1558}
1559
1560
1561static void ReportHistogram(bool print_spill) {
1562 PrintF("\n Object Histogram:\n");
1563 for (int i = 0; i <= LAST_TYPE; i++) {
1564 if (heap_histograms[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001565 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001566 heap_histograms[i].name(),
1567 heap_histograms[i].number(),
1568 heap_histograms[i].bytes());
1569 }
1570 }
1571 PrintF("\n");
1572
1573 // Summarize string types.
1574 int string_number = 0;
1575 int string_bytes = 0;
1576#define INCREMENT(type, size, name, camel_name) \
1577 string_number += heap_histograms[type].number(); \
1578 string_bytes += heap_histograms[type].bytes();
1579 STRING_TYPE_LIST(INCREMENT)
1580#undef INCREMENT
1581 if (string_number > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001582 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
Steve Blocka7e24c12009-10-30 11:49:00 +00001583 string_bytes);
1584 }
1585
1586 if (FLAG_collect_heap_spill_statistics && print_spill) {
1587 js_spill_information.Print();
1588 }
1589}
1590#endif // DEBUG
1591
1592
1593// Support for statistics gathering for --heap-stats and --log-gc.
1594#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1595void NewSpace::ClearHistograms() {
1596 for (int i = 0; i <= LAST_TYPE; i++) {
1597 allocated_histogram_[i].clear();
1598 promoted_histogram_[i].clear();
1599 }
1600}
1601
1602// Because the copying collector does not touch garbage objects, we iterate
1603// the new space before a collection to get a histogram of allocated objects.
1604// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1605// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1606// flag is set.
1607void NewSpace::CollectStatistics() {
1608 ClearHistograms();
1609 SemiSpaceIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00001610 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next())
1611 RecordAllocation(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001612}
1613
1614
1615#ifdef ENABLE_LOGGING_AND_PROFILING
1616static void DoReportStatistics(HistogramInfo* info, const char* description) {
1617 LOG(HeapSampleBeginEvent("NewSpace", description));
1618 // Lump all the string types together.
1619 int string_number = 0;
1620 int string_bytes = 0;
1621#define INCREMENT(type, size, name, camel_name) \
1622 string_number += info[type].number(); \
1623 string_bytes += info[type].bytes();
1624 STRING_TYPE_LIST(INCREMENT)
1625#undef INCREMENT
1626 if (string_number > 0) {
1627 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1628 }
1629
1630 // Then do the other types.
1631 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1632 if (info[i].number() > 0) {
1633 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1634 info[i].bytes()));
1635 }
1636 }
1637 LOG(HeapSampleEndEvent("NewSpace", description));
1638}
1639#endif // ENABLE_LOGGING_AND_PROFILING
1640
1641
1642void NewSpace::ReportStatistics() {
1643#ifdef DEBUG
1644 if (FLAG_heap_stats) {
1645 float pct = static_cast<float>(Available()) / Capacity();
1646 PrintF(" capacity: %d, available: %d, %%%d\n",
1647 Capacity(), Available(), static_cast<int>(pct*100));
1648 PrintF("\n Object Histogram:\n");
1649 for (int i = 0; i <= LAST_TYPE; i++) {
1650 if (allocated_histogram_[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001651 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001652 allocated_histogram_[i].name(),
1653 allocated_histogram_[i].number(),
1654 allocated_histogram_[i].bytes());
1655 }
1656 }
1657 PrintF("\n");
1658 }
1659#endif // DEBUG
1660
1661#ifdef ENABLE_LOGGING_AND_PROFILING
1662 if (FLAG_log_gc) {
1663 DoReportStatistics(allocated_histogram_, "allocated");
1664 DoReportStatistics(promoted_histogram_, "promoted");
1665 }
1666#endif // ENABLE_LOGGING_AND_PROFILING
1667}
1668
1669
1670void NewSpace::RecordAllocation(HeapObject* obj) {
1671 InstanceType type = obj->map()->instance_type();
1672 ASSERT(0 <= type && type <= LAST_TYPE);
1673 allocated_histogram_[type].increment_number(1);
1674 allocated_histogram_[type].increment_bytes(obj->Size());
1675}
1676
1677
1678void NewSpace::RecordPromotion(HeapObject* obj) {
1679 InstanceType type = obj->map()->instance_type();
1680 ASSERT(0 <= type && type <= LAST_TYPE);
1681 promoted_histogram_[type].increment_number(1);
1682 promoted_histogram_[type].increment_bytes(obj->Size());
1683}
1684#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1685
1686
1687// -----------------------------------------------------------------------------
1688// Free lists for old object spaces implementation
1689
1690void FreeListNode::set_size(int size_in_bytes) {
1691 ASSERT(size_in_bytes > 0);
1692 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1693
1694 // We write a map and possibly size information to the block. If the block
1695 // is big enough to be a ByteArray with at least one extra word (the next
1696 // pointer), we set its map to be the byte array map and its size to an
1697 // appropriate array length for the desired size from HeapObject::Size().
1698 // If the block is too small (eg, one or two words), to hold both a size
1699 // field and a next pointer, we give it a filler map that gives it the
1700 // correct size.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001701 if (size_in_bytes > ByteArray::kHeaderSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001702 set_map(Heap::raw_unchecked_byte_array_map());
Steve Blockd0582a62009-12-15 09:54:21 +00001703 // Can't use ByteArray::cast because it fails during deserialization.
1704 ByteArray* this_as_byte_array = reinterpret_cast<ByteArray*>(this);
1705 this_as_byte_array->set_length(ByteArray::LengthFor(size_in_bytes));
Steve Blocka7e24c12009-10-30 11:49:00 +00001706 } else if (size_in_bytes == kPointerSize) {
1707 set_map(Heap::raw_unchecked_one_pointer_filler_map());
1708 } else if (size_in_bytes == 2 * kPointerSize) {
1709 set_map(Heap::raw_unchecked_two_pointer_filler_map());
1710 } else {
1711 UNREACHABLE();
1712 }
Steve Blockd0582a62009-12-15 09:54:21 +00001713 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
1714 // deserialization because the byte array map is not done yet.
Steve Blocka7e24c12009-10-30 11:49:00 +00001715}
1716
1717
1718Address FreeListNode::next() {
Steve Block3ce2e202009-11-05 08:53:23 +00001719 ASSERT(IsFreeListNode(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001720 if (map() == Heap::raw_unchecked_byte_array_map()) {
1721 ASSERT(Size() >= kNextOffset + kPointerSize);
1722 return Memory::Address_at(address() + kNextOffset);
1723 } else {
1724 return Memory::Address_at(address() + kPointerSize);
1725 }
1726}
1727
1728
1729void FreeListNode::set_next(Address next) {
Steve Block3ce2e202009-11-05 08:53:23 +00001730 ASSERT(IsFreeListNode(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001731 if (map() == Heap::raw_unchecked_byte_array_map()) {
1732 ASSERT(Size() >= kNextOffset + kPointerSize);
1733 Memory::Address_at(address() + kNextOffset) = next;
1734 } else {
1735 Memory::Address_at(address() + kPointerSize) = next;
1736 }
1737}
1738
1739
1740OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1741 Reset();
1742}
1743
1744
1745void OldSpaceFreeList::Reset() {
1746 available_ = 0;
1747 for (int i = 0; i < kFreeListsLength; i++) {
1748 free_[i].head_node_ = NULL;
1749 }
1750 needs_rebuild_ = false;
1751 finger_ = kHead;
1752 free_[kHead].next_size_ = kEnd;
1753}
1754
1755
1756void OldSpaceFreeList::RebuildSizeList() {
1757 ASSERT(needs_rebuild_);
1758 int cur = kHead;
1759 for (int i = cur + 1; i < kFreeListsLength; i++) {
1760 if (free_[i].head_node_ != NULL) {
1761 free_[cur].next_size_ = i;
1762 cur = i;
1763 }
1764 }
1765 free_[cur].next_size_ = kEnd;
1766 needs_rebuild_ = false;
1767}
1768
1769
1770int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1771#ifdef DEBUG
Leon Clarke4515c472010-02-03 11:58:03 +00001772 MemoryAllocator::ZapBlock(start, size_in_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001773#endif
1774 FreeListNode* node = FreeListNode::FromAddress(start);
1775 node->set_size(size_in_bytes);
1776
1777 // We don't use the freelists in compacting mode. This makes it more like a
1778 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1779 // collector.
1780 if (FLAG_always_compact) {
1781 return size_in_bytes;
1782 }
1783
1784 // Early return to drop too-small blocks on the floor (one or two word
1785 // blocks cannot hold a map pointer, a size field, and a pointer to the
1786 // next block in the free list).
1787 if (size_in_bytes < kMinBlockSize) {
1788 return size_in_bytes;
1789 }
1790
1791 // Insert other blocks at the head of an exact free list.
1792 int index = size_in_bytes >> kPointerSizeLog2;
1793 node->set_next(free_[index].head_node_);
1794 free_[index].head_node_ = node->address();
1795 available_ += size_in_bytes;
1796 needs_rebuild_ = true;
1797 return 0;
1798}
1799
1800
1801Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1802 ASSERT(0 < size_in_bytes);
1803 ASSERT(size_in_bytes <= kMaxBlockSize);
1804 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1805
1806 if (needs_rebuild_) RebuildSizeList();
1807 int index = size_in_bytes >> kPointerSizeLog2;
1808 // Check for a perfect fit.
1809 if (free_[index].head_node_ != NULL) {
1810 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1811 // If this was the last block of its size, remove the size.
1812 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1813 available_ -= size_in_bytes;
1814 *wasted_bytes = 0;
1815 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1816 return node;
1817 }
1818 // Search the size list for the best fit.
1819 int prev = finger_ < index ? finger_ : kHead;
1820 int cur = FindSize(index, &prev);
1821 ASSERT(index < cur);
1822 if (cur == kEnd) {
1823 // No large enough size in list.
1824 *wasted_bytes = 0;
1825 return Failure::RetryAfterGC(size_in_bytes, owner_);
1826 }
1827 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1828 int rem = cur - index;
1829 int rem_bytes = rem << kPointerSizeLog2;
1830 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
1831 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
1832 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1833 size_in_bytes);
1834 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1835 // to avoid many redundant tests and calls to Insert/RemoveSize.
1836 if (prev < rem) {
1837 // Simple case: insert rem between prev and cur.
1838 finger_ = prev;
1839 free_[prev].next_size_ = rem;
1840 // If this was the last block of size cur, remove the size.
1841 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1842 free_[rem].next_size_ = free_[cur].next_size_;
1843 } else {
1844 free_[rem].next_size_ = cur;
1845 }
1846 // Add the remainder block.
1847 rem_node->set_size(rem_bytes);
1848 rem_node->set_next(free_[rem].head_node_);
1849 free_[rem].head_node_ = rem_node->address();
1850 } else {
1851 // If this was the last block of size cur, remove the size.
1852 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1853 finger_ = prev;
1854 free_[prev].next_size_ = free_[cur].next_size_;
1855 }
1856 if (rem_bytes < kMinBlockSize) {
1857 // Too-small remainder is wasted.
1858 rem_node->set_size(rem_bytes);
1859 available_ -= size_in_bytes + rem_bytes;
1860 *wasted_bytes = rem_bytes;
1861 return cur_node;
1862 }
1863 // Add the remainder block and, if needed, insert its size.
1864 rem_node->set_size(rem_bytes);
1865 rem_node->set_next(free_[rem].head_node_);
1866 free_[rem].head_node_ = rem_node->address();
1867 if (rem_node->next() == NULL) InsertSize(rem);
1868 }
1869 available_ -= size_in_bytes;
1870 *wasted_bytes = 0;
1871 return cur_node;
1872}
1873
1874
1875#ifdef DEBUG
1876bool OldSpaceFreeList::Contains(FreeListNode* node) {
1877 for (int i = 0; i < kFreeListsLength; i++) {
1878 Address cur_addr = free_[i].head_node_;
1879 while (cur_addr != NULL) {
1880 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1881 if (cur_node == node) return true;
1882 cur_addr = cur_node->next();
1883 }
1884 }
1885 return false;
1886}
1887#endif
1888
1889
1890FixedSizeFreeList::FixedSizeFreeList(AllocationSpace owner, int object_size)
1891 : owner_(owner), object_size_(object_size) {
1892 Reset();
1893}
1894
1895
1896void FixedSizeFreeList::Reset() {
1897 available_ = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001898 head_ = tail_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001899}
1900
1901
1902void FixedSizeFreeList::Free(Address start) {
1903#ifdef DEBUG
Leon Clarke4515c472010-02-03 11:58:03 +00001904 MemoryAllocator::ZapBlock(start, object_size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001905#endif
Leon Clarkee46be812010-01-19 14:06:41 +00001906 // We only use the freelists with mark-sweep.
1907 ASSERT(!MarkCompactCollector::IsCompacting());
Steve Blocka7e24c12009-10-30 11:49:00 +00001908 FreeListNode* node = FreeListNode::FromAddress(start);
1909 node->set_size(object_size_);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001910 node->set_next(NULL);
1911 if (head_ == NULL) {
1912 tail_ = head_ = node->address();
1913 } else {
1914 FreeListNode::FromAddress(tail_)->set_next(node->address());
1915 tail_ = node->address();
1916 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001917 available_ += object_size_;
1918}
1919
1920
1921Object* FixedSizeFreeList::Allocate() {
1922 if (head_ == NULL) {
1923 return Failure::RetryAfterGC(object_size_, owner_);
1924 }
1925
1926 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1927 FreeListNode* node = FreeListNode::FromAddress(head_);
1928 head_ = node->next();
1929 available_ -= object_size_;
1930 return node;
1931}
1932
1933
1934// -----------------------------------------------------------------------------
1935// OldSpace implementation
1936
1937void OldSpace::PrepareForMarkCompact(bool will_compact) {
Steve Block6ded16b2010-05-10 14:33:55 +01001938 // Call prepare of the super class.
1939 PagedSpace::PrepareForMarkCompact(will_compact);
1940
Steve Blocka7e24c12009-10-30 11:49:00 +00001941 if (will_compact) {
1942 // Reset relocation info. During a compacting collection, everything in
1943 // the space is considered 'available' and we will rediscover live data
1944 // and waste during the collection.
1945 MCResetRelocationInfo();
1946 ASSERT(Available() == Capacity());
1947 } else {
1948 // During a non-compacting collection, everything below the linear
1949 // allocation pointer is considered allocated (everything above is
1950 // available) and we will rediscover available and wasted bytes during
1951 // the collection.
1952 accounting_stats_.AllocateBytes(free_list_.available());
1953 accounting_stats_.FillWastedBytes(Waste());
1954 }
1955
1956 // Clear the free list before a full GC---it will be rebuilt afterward.
1957 free_list_.Reset();
1958}
1959
1960
1961void OldSpace::MCCommitRelocationInfo() {
1962 // Update fast allocation info.
1963 allocation_info_.top = mc_forwarding_info_.top;
1964 allocation_info_.limit = mc_forwarding_info_.limit;
1965 ASSERT(allocation_info_.VerifyPagedAllocation());
1966
1967 // The space is compacted and we haven't yet built free lists or
1968 // wasted any space.
1969 ASSERT(Waste() == 0);
1970 ASSERT(AvailableFree() == 0);
1971
1972 // Build the free list for the space.
1973 int computed_size = 0;
1974 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1975 while (it.has_next()) {
1976 Page* p = it.next();
1977 // Space below the relocation pointer is allocated.
Steve Blockd0582a62009-12-15 09:54:21 +00001978 computed_size +=
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001979 static_cast<int>(p->AllocationWatermark() - p->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +00001980 if (it.has_next()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001981 // Free the space at the top of the page.
Steve Blockd0582a62009-12-15 09:54:21 +00001982 int extra_size =
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001983 static_cast<int>(p->ObjectAreaEnd() - p->AllocationWatermark());
Steve Blocka7e24c12009-10-30 11:49:00 +00001984 if (extra_size > 0) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001985 int wasted_bytes = free_list_.Free(p->AllocationWatermark(),
1986 extra_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001987 // The bytes we have just "freed" to add to the free list were
1988 // already accounted as available.
1989 accounting_stats_.WasteBytes(wasted_bytes);
1990 }
1991 }
1992 }
1993
1994 // Make sure the computed size - based on the used portion of the pages in
1995 // use - matches the size obtained while computing forwarding addresses.
1996 ASSERT(computed_size == Size());
1997}
1998
1999
Leon Clarkee46be812010-01-19 14:06:41 +00002000bool NewSpace::ReserveSpace(int bytes) {
2001 // We can't reliably unpack a partial snapshot that needs more new space
2002 // space than the minimum NewSpace size.
2003 ASSERT(bytes <= InitialCapacity());
2004 Address limit = allocation_info_.limit;
2005 Address top = allocation_info_.top;
2006 return limit - top >= bytes;
2007}
2008
2009
Steve Block6ded16b2010-05-10 14:33:55 +01002010void PagedSpace::FreePages(Page* prev, Page* last) {
2011 if (last == AllocationTopPage()) {
2012 // Pages are already at the end of used pages.
2013 return;
2014 }
2015
2016 Page* first = NULL;
2017
2018 // Remove pages from the list.
2019 if (prev == NULL) {
2020 first = first_page_;
2021 first_page_ = last->next_page();
2022 } else {
2023 first = prev->next_page();
2024 MemoryAllocator::SetNextPage(prev, last->next_page());
2025 }
2026
2027 // Attach it after the last page.
2028 MemoryAllocator::SetNextPage(last_page_, first);
2029 last_page_ = last;
2030 MemoryAllocator::SetNextPage(last, NULL);
2031
2032 // Clean them up.
2033 do {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002034 first->InvalidateWatermark(true);
2035 first->SetAllocationWatermark(first->ObjectAreaStart());
2036 first->SetCachedAllocationWatermark(first->ObjectAreaStart());
2037 first->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Block6ded16b2010-05-10 14:33:55 +01002038 first = first->next_page();
2039 } while (first != NULL);
2040
2041 // Order of pages in this space might no longer be consistent with
2042 // order of pages in chunks.
2043 page_list_is_chunk_ordered_ = false;
2044}
2045
2046
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002047void PagedSpace::RelinkPageListInChunkOrder(bool deallocate_blocks) {
2048 const bool add_to_freelist = true;
2049
2050 // Mark used and unused pages to properly fill unused pages
2051 // after reordering.
2052 PageIterator all_pages_iterator(this, PageIterator::ALL_PAGES);
2053 Page* last_in_use = AllocationTopPage();
2054 bool in_use = true;
2055
2056 while (all_pages_iterator.has_next()) {
2057 Page* p = all_pages_iterator.next();
2058 p->SetWasInUseBeforeMC(in_use);
2059 if (p == last_in_use) {
2060 // We passed a page containing allocation top. All consequent
2061 // pages are not used.
2062 in_use = false;
2063 }
2064 }
2065
2066 if (page_list_is_chunk_ordered_) return;
2067
2068 Page* new_last_in_use = Page::FromAddress(NULL);
2069 MemoryAllocator::RelinkPageListInChunkOrder(this,
2070 &first_page_,
2071 &last_page_,
2072 &new_last_in_use);
2073 ASSERT(new_last_in_use->is_valid());
2074
2075 if (new_last_in_use != last_in_use) {
2076 // Current allocation top points to a page which is now in the middle
2077 // of page list. We should move allocation top forward to the new last
2078 // used page so various object iterators will continue to work properly.
2079 int size_in_bytes = static_cast<int>(PageAllocationLimit(last_in_use) -
2080 last_in_use->AllocationTop());
2081
2082 last_in_use->SetAllocationWatermark(last_in_use->AllocationTop());
2083 if (size_in_bytes > 0) {
2084 Address start = last_in_use->AllocationTop();
2085 if (deallocate_blocks) {
2086 accounting_stats_.AllocateBytes(size_in_bytes);
2087 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2088 } else {
2089 Heap::CreateFillerObjectAt(start, size_in_bytes);
2090 }
2091 }
2092
2093 // New last in use page was in the middle of the list before
2094 // sorting so it full.
2095 SetTop(new_last_in_use->AllocationTop());
2096
2097 ASSERT(AllocationTopPage() == new_last_in_use);
2098 ASSERT(AllocationTopPage()->WasInUseBeforeMC());
2099 }
2100
2101 PageIterator pages_in_use_iterator(this, PageIterator::PAGES_IN_USE);
2102 while (pages_in_use_iterator.has_next()) {
2103 Page* p = pages_in_use_iterator.next();
2104 if (!p->WasInUseBeforeMC()) {
2105 // Empty page is in the middle of a sequence of used pages.
2106 // Allocate it as a whole and deallocate immediately.
2107 int size_in_bytes = static_cast<int>(PageAllocationLimit(p) -
2108 p->ObjectAreaStart());
2109
2110 p->SetAllocationWatermark(p->ObjectAreaStart());
2111 Address start = p->ObjectAreaStart();
2112 if (deallocate_blocks) {
2113 accounting_stats_.AllocateBytes(size_in_bytes);
2114 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2115 } else {
2116 Heap::CreateFillerObjectAt(start, size_in_bytes);
2117 }
2118 }
2119 }
2120
2121 page_list_is_chunk_ordered_ = true;
2122}
2123
2124
Steve Block6ded16b2010-05-10 14:33:55 +01002125void PagedSpace::PrepareForMarkCompact(bool will_compact) {
2126 if (will_compact) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002127 RelinkPageListInChunkOrder(false);
Steve Block6ded16b2010-05-10 14:33:55 +01002128 }
2129}
2130
2131
Leon Clarkee46be812010-01-19 14:06:41 +00002132bool PagedSpace::ReserveSpace(int bytes) {
2133 Address limit = allocation_info_.limit;
2134 Address top = allocation_info_.top;
2135 if (limit - top >= bytes) return true;
2136
2137 // There wasn't enough space in the current page. Lets put the rest
2138 // of the page on the free list and start a fresh page.
2139 PutRestOfCurrentPageOnFreeList(TopPageOf(allocation_info_));
2140
2141 Page* reserved_page = TopPageOf(allocation_info_);
2142 int bytes_left_to_reserve = bytes;
2143 while (bytes_left_to_reserve > 0) {
2144 if (!reserved_page->next_page()->is_valid()) {
2145 if (Heap::OldGenerationAllocationLimitReached()) return false;
2146 Expand(reserved_page);
2147 }
2148 bytes_left_to_reserve -= Page::kPageSize;
2149 reserved_page = reserved_page->next_page();
2150 if (!reserved_page->is_valid()) return false;
2151 }
2152 ASSERT(TopPageOf(allocation_info_)->next_page()->is_valid());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002153 TopPageOf(allocation_info_)->next_page()->InvalidateWatermark(true);
Leon Clarkee46be812010-01-19 14:06:41 +00002154 SetAllocationInfo(&allocation_info_,
2155 TopPageOf(allocation_info_)->next_page());
2156 return true;
2157}
2158
2159
2160// You have to call this last, since the implementation from PagedSpace
2161// doesn't know that memory was 'promised' to large object space.
2162bool LargeObjectSpace::ReserveSpace(int bytes) {
2163 return Heap::OldGenerationSpaceAvailable() >= bytes;
2164}
2165
2166
Steve Blocka7e24c12009-10-30 11:49:00 +00002167// Slow case for normal allocation. Try in order: (1) allocate in the next
2168// page in the space, (2) allocate off the space's free list, (3) expand the
2169// space, (4) fail.
2170HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
2171 // Linear allocation in this space has failed. If there is another page
2172 // in the space, move to that page and allocate there. This allocation
2173 // should succeed (size_in_bytes should not be greater than a page's
2174 // object area size).
2175 Page* current_page = TopPageOf(allocation_info_);
2176 if (current_page->next_page()->is_valid()) {
2177 return AllocateInNextPage(current_page, size_in_bytes);
2178 }
2179
Steve Blockd0582a62009-12-15 09:54:21 +00002180 // There is no next page in this space. Try free list allocation unless that
2181 // is currently forbidden.
2182 if (!Heap::linear_allocation()) {
2183 int wasted_bytes;
2184 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
2185 accounting_stats_.WasteBytes(wasted_bytes);
2186 if (!result->IsFailure()) {
2187 accounting_stats_.AllocateBytes(size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002188
2189 HeapObject* obj = HeapObject::cast(result);
2190 Page* p = Page::FromAddress(obj->address());
2191
2192 if (obj->address() >= p->AllocationWatermark()) {
2193 // There should be no hole between the allocation watermark
2194 // and allocated object address.
2195 // Memory above the allocation watermark was not swept and
2196 // might contain garbage pointers to new space.
2197 ASSERT(obj->address() == p->AllocationWatermark());
2198 p->SetAllocationWatermark(obj->address() + size_in_bytes);
2199 }
2200
2201 return obj;
Steve Blockd0582a62009-12-15 09:54:21 +00002202 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002203 }
2204
2205 // Free list allocation failed and there is no next page. Fail if we have
2206 // hit the old generation size limit that should cause a garbage
2207 // collection.
2208 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2209 return NULL;
2210 }
2211
2212 // Try to expand the space and allocate in the new next page.
2213 ASSERT(!current_page->next_page()->is_valid());
2214 if (Expand(current_page)) {
2215 return AllocateInNextPage(current_page, size_in_bytes);
2216 }
2217
2218 // Finally, fail.
2219 return NULL;
2220}
2221
2222
Leon Clarkee46be812010-01-19 14:06:41 +00002223void OldSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002224 current_page->SetAllocationWatermark(allocation_info_.top);
Steve Blockd0582a62009-12-15 09:54:21 +00002225 int free_size =
2226 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002227 if (free_size > 0) {
2228 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
2229 accounting_stats_.WasteBytes(wasted_bytes);
2230 }
Leon Clarkee46be812010-01-19 14:06:41 +00002231}
2232
2233
2234void FixedSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002235 current_page->SetAllocationWatermark(allocation_info_.top);
Leon Clarkee46be812010-01-19 14:06:41 +00002236 int free_size =
2237 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
2238 // In the fixed space free list all the free list items have the right size.
2239 // We use up the rest of the page while preserving this invariant.
2240 while (free_size >= object_size_in_bytes_) {
2241 free_list_.Free(allocation_info_.top);
2242 allocation_info_.top += object_size_in_bytes_;
2243 free_size -= object_size_in_bytes_;
2244 accounting_stats_.WasteBytes(object_size_in_bytes_);
2245 }
2246}
2247
2248
2249// Add the block at the top of the page to the space's free list, set the
2250// allocation info to the next page (assumed to be one), and allocate
2251// linearly there.
2252HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
2253 int size_in_bytes) {
2254 ASSERT(current_page->next_page()->is_valid());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002255 Page* next_page = current_page->next_page();
2256 next_page->ClearGCFields();
Leon Clarkee46be812010-01-19 14:06:41 +00002257 PutRestOfCurrentPageOnFreeList(current_page);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002258 SetAllocationInfo(&allocation_info_, next_page);
Steve Blocka7e24c12009-10-30 11:49:00 +00002259 return AllocateLinearly(&allocation_info_, size_in_bytes);
2260}
2261
2262
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002263void OldSpace::DeallocateBlock(Address start,
2264 int size_in_bytes,
2265 bool add_to_freelist) {
2266 Free(start, size_in_bytes, add_to_freelist);
2267}
2268
2269
Steve Blocka7e24c12009-10-30 11:49:00 +00002270#ifdef DEBUG
2271struct CommentStatistic {
2272 const char* comment;
2273 int size;
2274 int count;
2275 void Clear() {
2276 comment = NULL;
2277 size = 0;
2278 count = 0;
2279 }
2280};
2281
2282
2283// must be small, since an iteration is used for lookup
2284const int kMaxComments = 64;
2285static CommentStatistic comments_statistics[kMaxComments+1];
2286
2287
2288void PagedSpace::ReportCodeStatistics() {
2289 ReportCodeKindStatistics();
2290 PrintF("Code comment statistics (\" [ comment-txt : size/ "
2291 "count (average)\"):\n");
2292 for (int i = 0; i <= kMaxComments; i++) {
2293 const CommentStatistic& cs = comments_statistics[i];
2294 if (cs.size > 0) {
2295 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
2296 cs.size/cs.count);
2297 }
2298 }
2299 PrintF("\n");
2300}
2301
2302
2303void PagedSpace::ResetCodeStatistics() {
2304 ClearCodeKindStatistics();
2305 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
2306 comments_statistics[kMaxComments].comment = "Unknown";
2307 comments_statistics[kMaxComments].size = 0;
2308 comments_statistics[kMaxComments].count = 0;
2309}
2310
2311
2312// Adds comment to 'comment_statistics' table. Performance OK sa long as
2313// 'kMaxComments' is small
2314static void EnterComment(const char* comment, int delta) {
2315 // Do not count empty comments
2316 if (delta <= 0) return;
2317 CommentStatistic* cs = &comments_statistics[kMaxComments];
2318 // Search for a free or matching entry in 'comments_statistics': 'cs'
2319 // points to result.
2320 for (int i = 0; i < kMaxComments; i++) {
2321 if (comments_statistics[i].comment == NULL) {
2322 cs = &comments_statistics[i];
2323 cs->comment = comment;
2324 break;
2325 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2326 cs = &comments_statistics[i];
2327 break;
2328 }
2329 }
2330 // Update entry for 'comment'
2331 cs->size += delta;
2332 cs->count += 1;
2333}
2334
2335
2336// Call for each nested comment start (start marked with '[ xxx', end marked
2337// with ']'. RelocIterator 'it' must point to a comment reloc info.
2338static void CollectCommentStatistics(RelocIterator* it) {
2339 ASSERT(!it->done());
2340 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
2341 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2342 if (tmp[0] != '[') {
2343 // Not a nested comment; skip
2344 return;
2345 }
2346
2347 // Search for end of nested comment or a new nested comment
2348 const char* const comment_txt =
2349 reinterpret_cast<const char*>(it->rinfo()->data());
2350 const byte* prev_pc = it->rinfo()->pc();
2351 int flat_delta = 0;
2352 it->next();
2353 while (true) {
2354 // All nested comments must be terminated properly, and therefore exit
2355 // from loop.
2356 ASSERT(!it->done());
2357 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
2358 const char* const txt =
2359 reinterpret_cast<const char*>(it->rinfo()->data());
Steve Blockd0582a62009-12-15 09:54:21 +00002360 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002361 if (txt[0] == ']') break; // End of nested comment
2362 // A new comment
2363 CollectCommentStatistics(it);
2364 // Skip code that was covered with previous comment
2365 prev_pc = it->rinfo()->pc();
2366 }
2367 it->next();
2368 }
2369 EnterComment(comment_txt, flat_delta);
2370}
2371
2372
2373// Collects code size statistics:
2374// - by code kind
2375// - by code comment
2376void PagedSpace::CollectCodeStatistics() {
2377 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002378 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002379 if (obj->IsCode()) {
2380 Code* code = Code::cast(obj);
2381 code_kind_statistics[code->kind()] += code->Size();
2382 RelocIterator it(code);
2383 int delta = 0;
2384 const byte* prev_pc = code->instruction_start();
2385 while (!it.done()) {
2386 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
Steve Blockd0582a62009-12-15 09:54:21 +00002387 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002388 CollectCommentStatistics(&it);
2389 prev_pc = it.rinfo()->pc();
2390 }
2391 it.next();
2392 }
2393
2394 ASSERT(code->instruction_start() <= prev_pc &&
Leon Clarkeac952652010-07-15 11:15:24 +01002395 prev_pc <= code->instruction_end());
2396 delta += static_cast<int>(code->instruction_end() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002397 EnterComment("NoComment", delta);
2398 }
2399 }
2400}
2401
2402
2403void OldSpace::ReportStatistics() {
2404 int pct = Available() * 100 / Capacity();
2405 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2406 Capacity(), Waste(), Available(), pct);
2407
Steve Blocka7e24c12009-10-30 11:49:00 +00002408 ClearHistograms();
2409 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002410 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2411 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002412 ReportHistogram(true);
2413}
Steve Blocka7e24c12009-10-30 11:49:00 +00002414#endif
2415
2416// -----------------------------------------------------------------------------
2417// FixedSpace implementation
2418
2419void FixedSpace::PrepareForMarkCompact(bool will_compact) {
Steve Block6ded16b2010-05-10 14:33:55 +01002420 // Call prepare of the super class.
2421 PagedSpace::PrepareForMarkCompact(will_compact);
2422
Steve Blocka7e24c12009-10-30 11:49:00 +00002423 if (will_compact) {
2424 // Reset relocation info.
2425 MCResetRelocationInfo();
2426
2427 // During a compacting collection, everything in the space is considered
2428 // 'available' (set by the call to MCResetRelocationInfo) and we will
2429 // rediscover live and wasted bytes during the collection.
2430 ASSERT(Available() == Capacity());
2431 } else {
2432 // During a non-compacting collection, everything below the linear
2433 // allocation pointer except wasted top-of-page blocks is considered
2434 // allocated and we will rediscover available bytes during the
2435 // collection.
2436 accounting_stats_.AllocateBytes(free_list_.available());
2437 }
2438
2439 // Clear the free list before a full GC---it will be rebuilt afterward.
2440 free_list_.Reset();
2441}
2442
2443
2444void FixedSpace::MCCommitRelocationInfo() {
2445 // Update fast allocation info.
2446 allocation_info_.top = mc_forwarding_info_.top;
2447 allocation_info_.limit = mc_forwarding_info_.limit;
2448 ASSERT(allocation_info_.VerifyPagedAllocation());
2449
2450 // The space is compacted and we haven't yet wasted any space.
2451 ASSERT(Waste() == 0);
2452
2453 // Update allocation_top of each page in use and compute waste.
2454 int computed_size = 0;
2455 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2456 while (it.has_next()) {
2457 Page* page = it.next();
2458 Address page_top = page->AllocationTop();
Steve Blockd0582a62009-12-15 09:54:21 +00002459 computed_size += static_cast<int>(page_top - page->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +00002460 if (it.has_next()) {
Steve Blockd0582a62009-12-15 09:54:21 +00002461 accounting_stats_.WasteBytes(
2462 static_cast<int>(page->ObjectAreaEnd() - page_top));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002463 page->SetAllocationWatermark(page_top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002464 }
2465 }
2466
2467 // Make sure the computed size - based on the used portion of the
2468 // pages in use - matches the size we adjust during allocation.
2469 ASSERT(computed_size == Size());
2470}
2471
2472
2473// Slow case for normal allocation. Try in order: (1) allocate in the next
2474// page in the space, (2) allocate off the space's free list, (3) expand the
2475// space, (4) fail.
2476HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2477 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2478 // Linear allocation in this space has failed. If there is another page
2479 // in the space, move to that page and allocate there. This allocation
2480 // should succeed.
2481 Page* current_page = TopPageOf(allocation_info_);
2482 if (current_page->next_page()->is_valid()) {
2483 return AllocateInNextPage(current_page, size_in_bytes);
2484 }
2485
Steve Blockd0582a62009-12-15 09:54:21 +00002486 // There is no next page in this space. Try free list allocation unless
2487 // that is currently forbidden. The fixed space free list implicitly assumes
2488 // that all free blocks are of the fixed size.
2489 if (!Heap::linear_allocation()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002490 Object* result = free_list_.Allocate();
2491 if (!result->IsFailure()) {
2492 accounting_stats_.AllocateBytes(size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002493 HeapObject* obj = HeapObject::cast(result);
2494 Page* p = Page::FromAddress(obj->address());
2495
2496 if (obj->address() >= p->AllocationWatermark()) {
2497 // There should be no hole between the allocation watermark
2498 // and allocated object address.
2499 // Memory above the allocation watermark was not swept and
2500 // might contain garbage pointers to new space.
2501 ASSERT(obj->address() == p->AllocationWatermark());
2502 p->SetAllocationWatermark(obj->address() + size_in_bytes);
2503 }
2504
2505 return obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00002506 }
2507 }
2508
2509 // Free list allocation failed and there is no next page. Fail if we have
2510 // hit the old generation size limit that should cause a garbage
2511 // collection.
2512 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2513 return NULL;
2514 }
2515
2516 // Try to expand the space and allocate in the new next page.
2517 ASSERT(!current_page->next_page()->is_valid());
2518 if (Expand(current_page)) {
2519 return AllocateInNextPage(current_page, size_in_bytes);
2520 }
2521
2522 // Finally, fail.
2523 return NULL;
2524}
2525
2526
2527// Move to the next page (there is assumed to be one) and allocate there.
2528// The top of page block is always wasted, because it is too small to hold a
2529// map.
2530HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2531 int size_in_bytes) {
2532 ASSERT(current_page->next_page()->is_valid());
Steve Block6ded16b2010-05-10 14:33:55 +01002533 ASSERT(allocation_info_.top == PageAllocationLimit(current_page));
Steve Blocka7e24c12009-10-30 11:49:00 +00002534 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002535 Page* next_page = current_page->next_page();
2536 next_page->ClearGCFields();
2537 current_page->SetAllocationWatermark(allocation_info_.top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002538 accounting_stats_.WasteBytes(page_extra_);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002539 SetAllocationInfo(&allocation_info_, next_page);
Steve Blocka7e24c12009-10-30 11:49:00 +00002540 return AllocateLinearly(&allocation_info_, size_in_bytes);
2541}
2542
2543
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002544void FixedSpace::DeallocateBlock(Address start,
2545 int size_in_bytes,
2546 bool add_to_freelist) {
2547 // Free-list elements in fixed space are assumed to have a fixed size.
2548 // We break the free block into chunks and add them to the free list
2549 // individually.
2550 int size = object_size_in_bytes();
2551 ASSERT(size_in_bytes % size == 0);
2552 Address end = start + size_in_bytes;
2553 for (Address a = start; a < end; a += size) {
2554 Free(a, add_to_freelist);
2555 }
2556}
2557
2558
Steve Blocka7e24c12009-10-30 11:49:00 +00002559#ifdef DEBUG
2560void FixedSpace::ReportStatistics() {
2561 int pct = Available() * 100 / Capacity();
2562 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2563 Capacity(), Waste(), Available(), pct);
2564
Steve Blocka7e24c12009-10-30 11:49:00 +00002565 ClearHistograms();
2566 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002567 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2568 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002569 ReportHistogram(false);
2570}
Steve Blocka7e24c12009-10-30 11:49:00 +00002571#endif
2572
2573
2574// -----------------------------------------------------------------------------
2575// MapSpace implementation
2576
2577void MapSpace::PrepareForMarkCompact(bool will_compact) {
2578 // Call prepare of the super class.
2579 FixedSpace::PrepareForMarkCompact(will_compact);
2580
2581 if (will_compact) {
2582 // Initialize map index entry.
2583 int page_count = 0;
2584 PageIterator it(this, PageIterator::ALL_PAGES);
2585 while (it.has_next()) {
2586 ASSERT_MAP_PAGE_INDEX(page_count);
2587
2588 Page* p = it.next();
2589 ASSERT(p->mc_page_index == page_count);
2590
2591 page_addresses_[page_count++] = p->address();
2592 }
2593 }
2594}
2595
2596
2597#ifdef DEBUG
2598void MapSpace::VerifyObject(HeapObject* object) {
2599 // The object should be a map or a free-list node.
2600 ASSERT(object->IsMap() || object->IsByteArray());
2601}
2602#endif
2603
2604
2605// -----------------------------------------------------------------------------
2606// GlobalPropertyCellSpace implementation
2607
2608#ifdef DEBUG
2609void CellSpace::VerifyObject(HeapObject* object) {
2610 // The object should be a global object property cell or a free-list node.
2611 ASSERT(object->IsJSGlobalPropertyCell() ||
2612 object->map() == Heap::two_pointer_filler_map());
2613}
2614#endif
2615
2616
2617// -----------------------------------------------------------------------------
2618// LargeObjectIterator
2619
2620LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2621 current_ = space->first_chunk_;
2622 size_func_ = NULL;
2623}
2624
2625
2626LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2627 HeapObjectCallback size_func) {
2628 current_ = space->first_chunk_;
2629 size_func_ = size_func;
2630}
2631
2632
2633HeapObject* LargeObjectIterator::next() {
Leon Clarked91b9f72010-01-27 17:25:45 +00002634 if (current_ == NULL) return NULL;
2635
Steve Blocka7e24c12009-10-30 11:49:00 +00002636 HeapObject* object = current_->GetObject();
2637 current_ = current_->next();
2638 return object;
2639}
2640
2641
2642// -----------------------------------------------------------------------------
2643// LargeObjectChunk
2644
2645LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
2646 size_t* chunk_size,
2647 Executability executable) {
2648 size_t requested = ChunkSizeFor(size_in_bytes);
2649 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2650 chunk_size,
2651 executable);
2652 if (mem == NULL) return NULL;
2653 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2654 if (*chunk_size < requested) {
Steve Block791712a2010-08-27 10:21:07 +01002655 MemoryAllocator::FreeRawMemory(mem, *chunk_size, executable);
Steve Blocka7e24c12009-10-30 11:49:00 +00002656 LOG(DeleteEvent("LargeObjectChunk", mem));
2657 return NULL;
2658 }
Iain Merrick9ac36c92010-09-13 15:29:50 +01002659 ObjectSpace space =
2660 (executable == EXECUTABLE) ? kObjectSpaceCodeSpace : kObjectSpaceLoSpace;
2661 MemoryAllocator::PerformAllocationCallback(space,
2662 kAllocationActionAllocate,
2663 *chunk_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002664 return reinterpret_cast<LargeObjectChunk*>(mem);
2665}
2666
2667
2668int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
Steve Blockd0582a62009-12-15 09:54:21 +00002669 int os_alignment = static_cast<int>(OS::AllocateAlignment());
Steve Blocka7e24c12009-10-30 11:49:00 +00002670 if (os_alignment < Page::kPageSize)
2671 size_in_bytes += (Page::kPageSize - os_alignment);
2672 return size_in_bytes + Page::kObjectStartOffset;
2673}
2674
2675// -----------------------------------------------------------------------------
2676// LargeObjectSpace
2677
2678LargeObjectSpace::LargeObjectSpace(AllocationSpace id)
2679 : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis
2680 first_chunk_(NULL),
2681 size_(0),
2682 page_count_(0) {}
2683
2684
2685bool LargeObjectSpace::Setup() {
2686 first_chunk_ = NULL;
2687 size_ = 0;
2688 page_count_ = 0;
2689 return true;
2690}
2691
2692
2693void LargeObjectSpace::TearDown() {
2694 while (first_chunk_ != NULL) {
2695 LargeObjectChunk* chunk = first_chunk_;
2696 first_chunk_ = first_chunk_->next();
2697 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
Steve Block791712a2010-08-27 10:21:07 +01002698 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2699 Executability executable =
2700 page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE;
Iain Merrick9ac36c92010-09-13 15:29:50 +01002701 ObjectSpace space = kObjectSpaceLoSpace;
2702 if (executable == EXECUTABLE) space = kObjectSpaceCodeSpace;
2703 size_t size = chunk->size();
2704 MemoryAllocator::FreeRawMemory(chunk->address(), size, executable);
2705 MemoryAllocator::PerformAllocationCallback(
2706 space, kAllocationActionFree, size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002707 }
2708
2709 size_ = 0;
2710 page_count_ = 0;
2711}
2712
2713
2714#ifdef ENABLE_HEAP_PROTECTION
2715
2716void LargeObjectSpace::Protect() {
2717 LargeObjectChunk* chunk = first_chunk_;
2718 while (chunk != NULL) {
2719 MemoryAllocator::Protect(chunk->address(), chunk->size());
2720 chunk = chunk->next();
2721 }
2722}
2723
2724
2725void LargeObjectSpace::Unprotect() {
2726 LargeObjectChunk* chunk = first_chunk_;
2727 while (chunk != NULL) {
2728 bool is_code = chunk->GetObject()->IsCode();
2729 MemoryAllocator::Unprotect(chunk->address(), chunk->size(),
2730 is_code ? EXECUTABLE : NOT_EXECUTABLE);
2731 chunk = chunk->next();
2732 }
2733}
2734
2735#endif
2736
2737
2738Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
2739 int object_size,
2740 Executability executable) {
2741 ASSERT(0 < object_size && object_size <= requested_size);
2742
2743 // Check if we want to force a GC before growing the old space further.
2744 // If so, fail the allocation.
2745 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2746 return Failure::RetryAfterGC(requested_size, identity());
2747 }
2748
2749 size_t chunk_size;
2750 LargeObjectChunk* chunk =
2751 LargeObjectChunk::New(requested_size, &chunk_size, executable);
2752 if (chunk == NULL) {
2753 return Failure::RetryAfterGC(requested_size, identity());
2754 }
2755
Steve Blockd0582a62009-12-15 09:54:21 +00002756 size_ += static_cast<int>(chunk_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002757 page_count_++;
2758 chunk->set_next(first_chunk_);
2759 chunk->set_size(chunk_size);
2760 first_chunk_ = chunk;
2761
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002762 // Initialize page header.
Steve Blocka7e24c12009-10-30 11:49:00 +00002763 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2764 Address object_address = page->ObjectAreaStart();
2765 // Clear the low order bit of the second word in the page to flag it as a
2766 // large object page. If the chunk_size happened to be written there, its
2767 // low order bit should already be clear.
2768 ASSERT((chunk_size & 0x1) == 0);
Steve Block6ded16b2010-05-10 14:33:55 +01002769 page->SetIsLargeObjectPage(true);
Steve Block791712a2010-08-27 10:21:07 +01002770 page->SetIsPageExecutable(executable);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002771 page->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00002772 return HeapObject::FromAddress(object_address);
2773}
2774
2775
2776Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
2777 ASSERT(0 < size_in_bytes);
2778 return AllocateRawInternal(size_in_bytes,
2779 size_in_bytes,
2780 EXECUTABLE);
2781}
2782
2783
2784Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
2785 ASSERT(0 < size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002786 return AllocateRawInternal(size_in_bytes,
Steve Blocka7e24c12009-10-30 11:49:00 +00002787 size_in_bytes,
2788 NOT_EXECUTABLE);
2789}
2790
2791
2792Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2793 ASSERT(0 < size_in_bytes);
2794 return AllocateRawInternal(size_in_bytes,
2795 size_in_bytes,
2796 NOT_EXECUTABLE);
2797}
2798
2799
2800// GC support
2801Object* LargeObjectSpace::FindObject(Address a) {
2802 for (LargeObjectChunk* chunk = first_chunk_;
2803 chunk != NULL;
2804 chunk = chunk->next()) {
2805 Address chunk_address = chunk->address();
2806 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2807 return chunk->GetObject();
2808 }
2809 }
2810 return Failure::Exception();
2811}
2812
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002813
2814LargeObjectChunk* LargeObjectSpace::FindChunkContainingPc(Address pc) {
2815 // TODO(853): Change this implementation to only find executable
2816 // chunks and use some kind of hash-based approach to speed it up.
2817 for (LargeObjectChunk* chunk = first_chunk_;
2818 chunk != NULL;
2819 chunk = chunk->next()) {
2820 Address chunk_address = chunk->address();
2821 if (chunk_address <= pc && pc < chunk_address + chunk->size()) {
2822 return chunk;
2823 }
2824 }
2825 return NULL;
2826}
2827
2828
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002829void LargeObjectSpace::IterateDirtyRegions(ObjectSlotCallback copy_object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002830 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002831 for (HeapObject* object = it.next(); object != NULL; object = it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002832 // We only have code, sequential strings, or fixed arrays in large
2833 // object space, and only fixed arrays can possibly contain pointers to
2834 // the young generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00002835 if (object->IsFixedArray()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002836 Page* page = Page::FromAddress(object->address());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002837 uint32_t marks = page->GetRegionMarks();
2838 uint32_t newmarks = Page::kAllRegionsCleanMarks;
Steve Blocka7e24c12009-10-30 11:49:00 +00002839
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002840 if (marks != Page::kAllRegionsCleanMarks) {
2841 // For a large page a single dirty mark corresponds to several
2842 // regions (modulo 32). So we treat a large page as a sequence of
2843 // normal pages of size Page::kPageSize having same dirty marks
2844 // and subsequently iterate dirty regions on each of these pages.
2845 Address start = object->address();
2846 Address end = page->ObjectAreaEnd();
2847 Address object_end = start + object->Size();
2848
2849 // Iterate regions of the first normal page covering object.
2850 uint32_t first_region_number = page->GetRegionNumberForAddress(start);
2851 newmarks |=
2852 Heap::IterateDirtyRegions(marks >> first_region_number,
2853 start,
2854 end,
2855 &Heap::IteratePointersInDirtyRegion,
2856 copy_object) << first_region_number;
2857
2858 start = end;
2859 end = start + Page::kPageSize;
2860 while (end <= object_end) {
2861 // Iterate next 32 regions.
2862 newmarks |=
2863 Heap::IterateDirtyRegions(marks,
2864 start,
2865 end,
2866 &Heap::IteratePointersInDirtyRegion,
2867 copy_object);
2868 start = end;
2869 end = start + Page::kPageSize;
2870 }
2871
2872 if (start != object_end) {
2873 // Iterate the last piece of an object which is less than
2874 // Page::kPageSize.
2875 newmarks |=
2876 Heap::IterateDirtyRegions(marks,
2877 start,
2878 object_end,
2879 &Heap::IteratePointersInDirtyRegion,
2880 copy_object);
2881 }
2882
2883 page->SetRegionMarks(newmarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00002884 }
2885 }
2886 }
2887}
2888
2889
2890void LargeObjectSpace::FreeUnmarkedObjects() {
2891 LargeObjectChunk* previous = NULL;
2892 LargeObjectChunk* current = first_chunk_;
2893 while (current != NULL) {
2894 HeapObject* object = current->GetObject();
2895 if (object->IsMarked()) {
2896 object->ClearMark();
2897 MarkCompactCollector::tracer()->decrement_marked_count();
2898 previous = current;
2899 current = current->next();
2900 } else {
Steve Block791712a2010-08-27 10:21:07 +01002901 Page* page = Page::FromAddress(RoundUp(current->address(),
2902 Page::kPageSize));
2903 Executability executable =
2904 page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE;
Steve Blocka7e24c12009-10-30 11:49:00 +00002905 Address chunk_address = current->address();
2906 size_t chunk_size = current->size();
2907
2908 // Cut the chunk out from the chunk list.
2909 current = current->next();
2910 if (previous == NULL) {
2911 first_chunk_ = current;
2912 } else {
2913 previous->set_next(current);
2914 }
2915
2916 // Free the chunk.
Leon Clarked91b9f72010-01-27 17:25:45 +00002917 MarkCompactCollector::ReportDeleteIfNeeded(object);
Steve Blockd0582a62009-12-15 09:54:21 +00002918 size_ -= static_cast<int>(chunk_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002919 page_count_--;
Iain Merrick9ac36c92010-09-13 15:29:50 +01002920 ObjectSpace space = kObjectSpaceLoSpace;
2921 if (executable == EXECUTABLE) space = kObjectSpaceCodeSpace;
Steve Block791712a2010-08-27 10:21:07 +01002922 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size, executable);
Iain Merrick9ac36c92010-09-13 15:29:50 +01002923 MemoryAllocator::PerformAllocationCallback(space, kAllocationActionFree,
2924 size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002925 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2926 }
2927 }
2928}
2929
2930
2931bool LargeObjectSpace::Contains(HeapObject* object) {
2932 Address address = object->address();
Steve Block6ded16b2010-05-10 14:33:55 +01002933 if (Heap::new_space()->Contains(address)) {
2934 return false;
2935 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002936 Page* page = Page::FromAddress(address);
2937
2938 SLOW_ASSERT(!page->IsLargeObjectPage()
2939 || !FindObject(address)->IsFailure());
2940
2941 return page->IsLargeObjectPage();
2942}
2943
2944
2945#ifdef DEBUG
2946// We do not assume that the large object iterator works, because it depends
2947// on the invariants we are checking during verification.
2948void LargeObjectSpace::Verify() {
2949 for (LargeObjectChunk* chunk = first_chunk_;
2950 chunk != NULL;
2951 chunk = chunk->next()) {
2952 // Each chunk contains an object that starts at the large object page's
2953 // object area start.
2954 HeapObject* object = chunk->GetObject();
2955 Page* page = Page::FromAddress(object->address());
2956 ASSERT(object->address() == page->ObjectAreaStart());
2957
2958 // The first word should be a map, and we expect all map pointers to be
2959 // in map space.
2960 Map* map = object->map();
2961 ASSERT(map->IsMap());
2962 ASSERT(Heap::map_space()->Contains(map));
2963
2964 // We have only code, sequential strings, external strings
2965 // (sequential strings that have been morphed into external
2966 // strings), fixed arrays, and byte arrays in large object space.
2967 ASSERT(object->IsCode() || object->IsSeqString() ||
2968 object->IsExternalString() || object->IsFixedArray() ||
2969 object->IsByteArray());
2970
2971 // The object itself should look OK.
2972 object->Verify();
2973
2974 // Byte arrays and strings don't have interior pointers.
2975 if (object->IsCode()) {
2976 VerifyPointersVisitor code_visitor;
2977 object->IterateBody(map->instance_type(),
2978 object->Size(),
2979 &code_visitor);
2980 } else if (object->IsFixedArray()) {
2981 // We loop over fixed arrays ourselves, rather then using the visitor,
2982 // because the visitor doesn't support the start/offset iteration
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002983 // needed for IsRegionDirty.
Steve Blocka7e24c12009-10-30 11:49:00 +00002984 FixedArray* array = FixedArray::cast(object);
2985 for (int j = 0; j < array->length(); j++) {
2986 Object* element = array->get(j);
2987 if (element->IsHeapObject()) {
2988 HeapObject* element_object = HeapObject::cast(element);
2989 ASSERT(Heap::Contains(element_object));
2990 ASSERT(element_object->map()->IsMap());
2991 if (Heap::InNewSpace(element_object)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002992 Address array_addr = object->address();
2993 Address element_addr = array_addr + FixedArray::kHeaderSize +
2994 j * kPointerSize;
2995
2996 ASSERT(Page::FromAddress(array_addr)->IsRegionDirty(element_addr));
Steve Blocka7e24c12009-10-30 11:49:00 +00002997 }
2998 }
2999 }
3000 }
3001 }
3002}
3003
3004
3005void LargeObjectSpace::Print() {
3006 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00003007 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
3008 obj->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +00003009 }
3010}
3011
3012
3013void LargeObjectSpace::ReportStatistics() {
3014 PrintF(" size: %d\n", size_);
3015 int num_objects = 0;
3016 ClearHistograms();
3017 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00003018 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003019 num_objects++;
Leon Clarked91b9f72010-01-27 17:25:45 +00003020 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003021 }
3022
3023 PrintF(" number of objects %d\n", num_objects);
3024 if (num_objects > 0) ReportHistogram(false);
3025}
3026
3027
3028void LargeObjectSpace::CollectCodeStatistics() {
3029 LargeObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00003030 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003031 if (obj->IsCode()) {
3032 Code* code = Code::cast(obj);
3033 code_kind_statistics[code->kind()] += code->Size();
3034 }
3035 }
3036}
Steve Blocka7e24c12009-10-30 11:49:00 +00003037#endif // DEBUG
3038
3039} } // namespace v8::internal