blob: a0c8f2cba168c6bc06bc0fe04879e0e4cd8d8c54 [file] [log] [blame]
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
ager@chromium.org0ee099b2011-01-25 14:06:47 +000030#include "liveobjectlist-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000031#include "macro-assembler.h"
32#include "mark-compact.h"
33#include "platform.h"
34
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039// ----------------------------------------------------------------------------
40// HeapObjectIterator
41
42HeapObjectIterator::HeapObjectIterator(PagedSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000043 // You can't actually iterate over the anchor page. It is not a real page,
44 // just an anchor for the double linked page list. Initialize as if we have
45 // reached the end of the anchor page, then the first iteration will move on
46 // to the first page.
47 Initialize(space,
48 NULL,
49 NULL,
50 kAllPagesInSpace,
51 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052}
53
54
55HeapObjectIterator::HeapObjectIterator(PagedSpace* space,
56 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000057 // You can't actually iterate over the anchor page. It is not a real page,
58 // just an anchor for the double linked page list. Initialize the current
59 // address and end as NULL, then the first iteration will move on
60 // to the first page.
61 Initialize(space,
62 NULL,
63 NULL,
64 kAllPagesInSpace,
65 size_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066}
67
68
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000069HeapObjectIterator::HeapObjectIterator(Page* page,
70 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000071 Space* owner = page->owner();
72 ASSERT(owner == HEAP->old_pointer_space() ||
73 owner == HEAP->old_data_space() ||
74 owner == HEAP->map_space() ||
75 owner == HEAP->cell_space() ||
76 owner == HEAP->code_space());
77 Initialize(reinterpret_cast<PagedSpace*>(owner),
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000078 page->area_start(),
79 page->area_end(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000080 kOnePageOnly,
81 size_func);
82 ASSERT(page->WasSweptPrecisely());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000083}
84
85
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000086void HeapObjectIterator::Initialize(PagedSpace* space,
87 Address cur, Address end,
88 HeapObjectIterator::PageMode mode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 HeapObjectCallback size_f) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000090 // Check that we actually can iterate this space.
91 ASSERT(!space->was_swept_conservatively());
92
93 space_ = space;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 cur_addr_ = cur;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000095 cur_end_ = end;
96 page_mode_ = mode;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 size_func_ = size_f;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098}
99
100
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000101// We have hit the end of the page and should advance to the next block of
102// objects. This happens at the end of the page.
103bool HeapObjectIterator::AdvanceToNextPage() {
104 ASSERT(cur_addr_ == cur_end_);
105 if (page_mode_ == kOnePageOnly) return false;
106 Page* cur_page;
107 if (cur_addr_ == NULL) {
108 cur_page = space_->anchor();
109 } else {
110 cur_page = Page::FromAddress(cur_addr_ - 1);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000111 ASSERT(cur_addr_ == cur_page->area_end());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000112 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113 cur_page = cur_page->next_page();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000114 if (cur_page == space_->anchor()) return false;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000115 cur_addr_ = cur_page->area_start();
116 cur_end_ = cur_page->area_end();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000117 ASSERT(cur_page->WasSweptPrecisely());
118 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119}
120
121
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122// -----------------------------------------------------------------------------
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000123// CodeRange
124
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000126CodeRange::CodeRange(Isolate* isolate)
127 : isolate_(isolate),
128 code_range_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000129 free_list_(0),
130 allocation_list_(0),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000131 current_allocation_block_index_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132}
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000133
134
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000135bool CodeRange::SetUp(const size_t requested) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000136 ASSERT(code_range_ == NULL);
137
138 code_range_ = new VirtualMemory(requested);
139 CHECK(code_range_ != NULL);
140 if (!code_range_->IsReserved()) {
141 delete code_range_;
142 code_range_ = NULL;
143 return false;
144 }
145
146 // We are sure that we have mapped a block of requested addresses.
147 ASSERT(code_range_->size() == requested);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148 LOG(isolate_, NewEvent("CodeRange", code_range_->address(), requested));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000149 Address base = reinterpret_cast<Address>(code_range_->address());
150 Address aligned_base =
151 RoundUp(reinterpret_cast<Address>(code_range_->address()),
152 MemoryChunk::kAlignment);
153 size_t size = code_range_->size() - (aligned_base - base);
154 allocation_list_.Add(FreeBlock(aligned_base, size));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000155 current_allocation_block_index_ = 0;
156 return true;
157}
158
159
160int CodeRange::CompareFreeBlockAddress(const FreeBlock* left,
161 const FreeBlock* right) {
162 // The entire point of CodeRange is that the difference between two
163 // addresses in the range can be represented as a signed 32-bit int,
164 // so the cast is semantically correct.
165 return static_cast<int>(left->start - right->start);
166}
167
168
169void CodeRange::GetNextAllocationBlock(size_t requested) {
170 for (current_allocation_block_index_++;
171 current_allocation_block_index_ < allocation_list_.length();
172 current_allocation_block_index_++) {
173 if (requested <= allocation_list_[current_allocation_block_index_].size) {
174 return; // Found a large enough allocation block.
175 }
176 }
177
178 // Sort and merge the free blocks on the free list and the allocation list.
179 free_list_.AddAll(allocation_list_);
180 allocation_list_.Clear();
181 free_list_.Sort(&CompareFreeBlockAddress);
182 for (int i = 0; i < free_list_.length();) {
183 FreeBlock merged = free_list_[i];
184 i++;
185 // Add adjacent free blocks to the current merged block.
186 while (i < free_list_.length() &&
187 free_list_[i].start == merged.start + merged.size) {
188 merged.size += free_list_[i].size;
189 i++;
190 }
191 if (merged.size > 0) {
192 allocation_list_.Add(merged);
193 }
194 }
195 free_list_.Clear();
196
197 for (current_allocation_block_index_ = 0;
198 current_allocation_block_index_ < allocation_list_.length();
199 current_allocation_block_index_++) {
200 if (requested <= allocation_list_[current_allocation_block_index_].size) {
201 return; // Found a large enough allocation block.
202 }
203 }
204
205 // Code range is full or too fragmented.
206 V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock");
207}
208
209
210
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000211Address CodeRange::AllocateRawMemory(const size_t requested,
212 size_t* allocated) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000213 ASSERT(current_allocation_block_index_ < allocation_list_.length());
214 if (requested > allocation_list_[current_allocation_block_index_].size) {
215 // Find an allocation block large enough. This function call may
216 // call V8::FatalProcessOutOfMemory if it cannot find a large enough block.
217 GetNextAllocationBlock(requested);
218 }
219 // Commit the requested memory at the start of the current allocation block.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000220 size_t aligned_requested = RoundUp(requested, MemoryChunk::kAlignment);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000221 FreeBlock current = allocation_list_[current_allocation_block_index_];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000222 if (aligned_requested >= (current.size - Page::kPageSize)) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000223 // Don't leave a small free block, useless for a large object or chunk.
224 *allocated = current.size;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000225 } else {
226 *allocated = aligned_requested;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000227 }
228 ASSERT(*allocated <= current.size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000229 ASSERT(IsAddressAligned(current.start, MemoryChunk::kAlignment));
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000230 if (!MemoryAllocator::CommitCodePage(code_range_,
231 current.start,
232 *allocated)) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000233 *allocated = 0;
234 return NULL;
235 }
236 allocation_list_[current_allocation_block_index_].start += *allocated;
237 allocation_list_[current_allocation_block_index_].size -= *allocated;
238 if (*allocated == current.size) {
239 GetNextAllocationBlock(0); // This block is used up, get the next one.
240 }
241 return current.start;
242}
243
244
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000245void CodeRange::FreeRawMemory(Address address, size_t length) {
246 ASSERT(IsAddressAligned(address, MemoryChunk::kAlignment));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000247 free_list_.Add(FreeBlock(address, length));
248 code_range_->Uncommit(address, length);
249}
250
251
252void CodeRange::TearDown() {
253 delete code_range_; // Frees all memory in the virtual memory range.
254 code_range_ = NULL;
255 free_list_.Free();
256 allocation_list_.Free();
257}
258
259
260// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261// MemoryAllocator
262//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000264MemoryAllocator::MemoryAllocator(Isolate* isolate)
265 : isolate_(isolate),
266 capacity_(0),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000267 capacity_executable_(0),
268 size_(0),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000269 size_executable_(0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270}
271
272
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000273bool MemoryAllocator::SetUp(intptr_t capacity, intptr_t capacity_executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 capacity_ = RoundUp(capacity, Page::kPageSize);
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000275 capacity_executable_ = RoundUp(capacity_executable, Page::kPageSize);
276 ASSERT_GE(capacity_, capacity_executable_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 size_ = 0;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000279 size_executable_ = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 return true;
282}
283
284
285void MemoryAllocator::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000286 // Check that spaces were torn down before MemoryAllocator.
287 ASSERT(size_ == 0);
288 // TODO(gc) this will be true again when we fix FreeMemory.
289 // ASSERT(size_executable_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 capacity_ = 0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000291 capacity_executable_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292}
293
294
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000295void MemoryAllocator::FreeMemory(VirtualMemory* reservation,
296 Executability executable) {
297 // TODO(gc) make code_range part of memory allocator?
298 ASSERT(reservation->IsReserved());
299 size_t size = reservation->size();
300 ASSERT(size_ >= size);
301 size_ -= size;
302
303 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
304
305 if (executable == EXECUTABLE) {
306 ASSERT(size_executable_ >= size);
307 size_executable_ -= size;
308 }
309 // Code which is part of the code-range does not have its own VirtualMemory.
310 ASSERT(!isolate_->code_range()->contains(
311 static_cast<Address>(reservation->address())));
312 ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists());
313 reservation->Release();
314}
315
316
317void MemoryAllocator::FreeMemory(Address base,
318 size_t size,
319 Executability executable) {
320 // TODO(gc) make code_range part of memory allocator?
321 ASSERT(size_ >= size);
322 size_ -= size;
323
324 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
325
326 if (executable == EXECUTABLE) {
327 ASSERT(size_executable_ >= size);
328 size_executable_ -= size;
329 }
330 if (isolate_->code_range()->contains(static_cast<Address>(base))) {
331 ASSERT(executable == EXECUTABLE);
332 isolate_->code_range()->FreeRawMemory(base, size);
333 } else {
334 ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists());
335 bool result = VirtualMemory::ReleaseRegion(base, size);
336 USE(result);
337 ASSERT(result);
338 }
339}
340
341
342Address MemoryAllocator::ReserveAlignedMemory(size_t size,
343 size_t alignment,
344 VirtualMemory* controller) {
345 VirtualMemory reservation(size, alignment);
346
347 if (!reservation.IsReserved()) return NULL;
348 size_ += reservation.size();
349 Address base = RoundUp(static_cast<Address>(reservation.address()),
350 alignment);
351 controller->TakeControl(&reservation);
352 return base;
353}
354
355
356Address MemoryAllocator::AllocateAlignedMemory(size_t size,
357 size_t alignment,
358 Executability executable,
359 VirtualMemory* controller) {
360 VirtualMemory reservation;
361 Address base = ReserveAlignedMemory(size, alignment, &reservation);
362 if (base == NULL) return NULL;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000363
364 if (executable == EXECUTABLE) {
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000365 if (!CommitCodePage(&reservation, base, size)) {
366 base = NULL;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000367 }
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000368 } else {
369 if (!reservation.Commit(base, size, false)) {
370 base = NULL;
371 }
372 }
373
374 if (base == NULL) {
375 // Failed to commit the body. Release the mapping and any partially
376 // commited regions inside it.
377 reservation.Release();
378 return NULL;
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000379 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000380
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000381 controller->TakeControl(&reservation);
382 return base;
383}
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000384
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000385
386void Page::InitializeAsAnchor(PagedSpace* owner) {
387 set_owner(owner);
388 set_prev_page(this);
389 set_next_page(this);
390}
391
392
393NewSpacePage* NewSpacePage::Initialize(Heap* heap,
394 Address start,
395 SemiSpace* semi_space) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000396 Address area_start = start + NewSpacePage::kObjectStartOffset;
397 Address area_end = start + Page::kPageSize;
398
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000399 MemoryChunk* chunk = MemoryChunk::Initialize(heap,
400 start,
401 Page::kPageSize,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000402 area_start,
403 area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000404 NOT_EXECUTABLE,
405 semi_space);
406 chunk->set_next_chunk(NULL);
407 chunk->set_prev_chunk(NULL);
408 chunk->initialize_scan_on_scavenge(true);
409 bool in_to_space = (semi_space->id() != kFromSpace);
410 chunk->SetFlag(in_to_space ? MemoryChunk::IN_TO_SPACE
411 : MemoryChunk::IN_FROM_SPACE);
412 ASSERT(!chunk->IsFlagSet(in_to_space ? MemoryChunk::IN_FROM_SPACE
413 : MemoryChunk::IN_TO_SPACE));
414 NewSpacePage* page = static_cast<NewSpacePage*>(chunk);
415 heap->incremental_marking()->SetNewSpacePageFlags(page);
416 return page;
417}
418
419
420void NewSpacePage::InitializeAsAnchor(SemiSpace* semi_space) {
421 set_owner(semi_space);
422 set_next_chunk(this);
423 set_prev_chunk(this);
424 // Flags marks this invalid page as not being in new-space.
425 // All real new-space pages will be in new-space.
426 SetFlags(0, ~0);
427}
428
429
430MemoryChunk* MemoryChunk::Initialize(Heap* heap,
431 Address base,
432 size_t size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000433 Address area_start,
434 Address area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000435 Executability executable,
436 Space* owner) {
437 MemoryChunk* chunk = FromAddress(base);
438
439 ASSERT(base == chunk->address());
440
441 chunk->heap_ = heap;
442 chunk->size_ = size;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000443 chunk->area_start_ = area_start;
444 chunk->area_end_ = area_end;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000445 chunk->flags_ = 0;
446 chunk->set_owner(owner);
447 chunk->InitializeReservedMemory();
448 chunk->slots_buffer_ = NULL;
449 chunk->skip_list_ = NULL;
450 chunk->ResetLiveBytes();
451 Bitmap::Clear(chunk);
452 chunk->initialize_scan_on_scavenge(false);
453 chunk->SetFlag(WAS_SWEPT_PRECISELY);
454
455 ASSERT(OFFSET_OF(MemoryChunk, flags_) == kFlagsOffset);
456 ASSERT(OFFSET_OF(MemoryChunk, live_byte_count_) == kLiveBytesOffset);
457
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000458 if (executable == EXECUTABLE) {
459 chunk->SetFlag(IS_EXECUTABLE);
460 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000461
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000462 if (owner == heap->old_data_space()) {
463 chunk->SetFlag(CONTAINS_ONLY_DATA);
464 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000465
466 return chunk;
467}
468
469
470void MemoryChunk::InsertAfter(MemoryChunk* other) {
471 next_chunk_ = other->next_chunk_;
472 prev_chunk_ = other;
473 other->next_chunk_->prev_chunk_ = this;
474 other->next_chunk_ = this;
475}
476
477
478void MemoryChunk::Unlink() {
479 if (!InNewSpace() && IsFlagSet(SCAN_ON_SCAVENGE)) {
480 heap_->decrement_scan_on_scavenge_pages();
481 ClearFlag(SCAN_ON_SCAVENGE);
482 }
483 next_chunk_->prev_chunk_ = prev_chunk_;
484 prev_chunk_->next_chunk_ = next_chunk_;
485 prev_chunk_ = NULL;
486 next_chunk_ = NULL;
487}
488
489
490MemoryChunk* MemoryAllocator::AllocateChunk(intptr_t body_size,
491 Executability executable,
492 Space* owner) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000493 size_t chunk_size;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000494 Heap* heap = isolate_->heap();
495 Address base = NULL;
496 VirtualMemory reservation;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000497 Address area_start = NULL;
498 Address area_end = NULL;
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000499 if (executable == EXECUTABLE) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000500 chunk_size = RoundUp(CodePageAreaStartOffset() + body_size,
501 OS::CommitPageSize()) + CodePageGuardSize();
502
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000503 // Check executable memory limit.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000504 if (size_executable_ + chunk_size > capacity_executable_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000505 LOG(isolate_,
506 StringEvent("MemoryAllocator::AllocateRawMemory",
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000507 "V8 Executable Allocation capacity exceeded"));
508 return NULL;
509 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000510
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000511 // Allocate executable memory either from code range or from the
512 // OS.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000513 if (isolate_->code_range()->exists()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000514 base = isolate_->code_range()->AllocateRawMemory(chunk_size, &chunk_size);
515 ASSERT(IsAligned(reinterpret_cast<intptr_t>(base),
516 MemoryChunk::kAlignment));
517 if (base == NULL) return NULL;
518 size_ += chunk_size;
519 // Update executable memory size.
520 size_executable_ += chunk_size;
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000521 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000522 base = AllocateAlignedMemory(chunk_size,
523 MemoryChunk::kAlignment,
524 executable,
525 &reservation);
526 if (base == NULL) return NULL;
527 // Update executable memory size.
528 size_executable_ += reservation.size();
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000529 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000530
531#ifdef DEBUG
532 ZapBlock(base, CodePageGuardStartOffset());
533 ZapBlock(base + CodePageAreaStartOffset(), body_size);
534#endif
535 area_start = base + CodePageAreaStartOffset();
536 area_end = area_start + body_size;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000537 } else {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000538 chunk_size = MemoryChunk::kObjectStartOffset + body_size;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000539 base = AllocateAlignedMemory(chunk_size,
540 MemoryChunk::kAlignment,
541 executable,
542 &reservation);
543
544 if (base == NULL) return NULL;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000545
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000546#ifdef DEBUG
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000547 ZapBlock(base, chunk_size);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000548#endif
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000549
550 area_start = base + Page::kObjectStartOffset;
551 area_end = base + chunk_size;
552 }
553
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000554 isolate_->counters()->memory_allocated()->
555 Increment(static_cast<int>(chunk_size));
556
557 LOG(isolate_, NewEvent("MemoryChunk", base, chunk_size));
558 if (owner != NULL) {
559 ObjectSpace space = static_cast<ObjectSpace>(1 << owner->identity());
560 PerformAllocationCallback(space, kAllocationActionAllocate, chunk_size);
561 }
562
563 MemoryChunk* result = MemoryChunk::Initialize(heap,
564 base,
565 chunk_size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000566 area_start,
567 area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000568 executable,
569 owner);
570 result->set_reserved_memory(&reservation);
571 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572}
573
574
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000575Page* MemoryAllocator::AllocatePage(intptr_t size,
576 PagedSpace* owner,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000577 Executability executable) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000578 MemoryChunk* chunk = AllocateChunk(size, executable, owner);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000579
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000580 if (chunk == NULL) return NULL;
581
582 return Page::Initialize(isolate_->heap(), chunk, executable, owner);
583}
584
585
586LargePage* MemoryAllocator::AllocateLargePage(intptr_t object_size,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000587 Space* owner,
588 Executability executable) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000589 MemoryChunk* chunk = AllocateChunk(object_size, executable, owner);
590 if (chunk == NULL) return NULL;
591 return LargePage::Initialize(isolate_->heap(), chunk);
592}
593
594
595void MemoryAllocator::Free(MemoryChunk* chunk) {
596 LOG(isolate_, DeleteEvent("MemoryChunk", chunk));
597 if (chunk->owner() != NULL) {
598 ObjectSpace space =
599 static_cast<ObjectSpace>(1 << chunk->owner()->identity());
600 PerformAllocationCallback(space, kAllocationActionFree, chunk->size());
601 }
602
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000603 isolate_->heap()->RememberUnmappedPage(
604 reinterpret_cast<Address>(chunk), chunk->IsEvacuationCandidate());
605
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000606 delete chunk->slots_buffer();
607 delete chunk->skip_list();
608
609 VirtualMemory* reservation = chunk->reserved_memory();
610 if (reservation->IsReserved()) {
611 FreeMemory(reservation, chunk->executable());
612 } else {
613 FreeMemory(chunk->address(),
614 chunk->size(),
615 chunk->executable());
616 }
617}
618
619
620bool MemoryAllocator::CommitBlock(Address start,
621 size_t size,
622 Executability executable) {
623 if (!VirtualMemory::CommitRegion(start, size, executable)) return false;
624#ifdef DEBUG
625 ZapBlock(start, size);
626#endif
627 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
628 return true;
629}
630
631
632bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
633 if (!VirtualMemory::UncommitRegion(start, size)) return false;
634 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
635 return true;
636}
637
638
639void MemoryAllocator::ZapBlock(Address start, size_t size) {
640 for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) {
641 Memory::Address_at(start + s) = kZapValue;
642 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643}
644
645
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000646void MemoryAllocator::PerformAllocationCallback(ObjectSpace space,
647 AllocationAction action,
648 size_t size) {
649 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
650 MemoryAllocationCallbackRegistration registration =
651 memory_allocation_callbacks_[i];
652 if ((registration.space & space) == space &&
653 (registration.action & action) == action)
654 registration.callback(space, action, static_cast<int>(size));
655 }
656}
657
658
659bool MemoryAllocator::MemoryAllocationCallbackRegistered(
660 MemoryAllocationCallback callback) {
661 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
662 if (memory_allocation_callbacks_[i].callback == callback) return true;
663 }
664 return false;
665}
666
667
668void MemoryAllocator::AddMemoryAllocationCallback(
669 MemoryAllocationCallback callback,
670 ObjectSpace space,
671 AllocationAction action) {
672 ASSERT(callback != NULL);
673 MemoryAllocationCallbackRegistration registration(callback, space, action);
674 ASSERT(!MemoryAllocator::MemoryAllocationCallbackRegistered(callback));
675 return memory_allocation_callbacks_.Add(registration);
676}
677
678
679void MemoryAllocator::RemoveMemoryAllocationCallback(
680 MemoryAllocationCallback callback) {
681 ASSERT(callback != NULL);
682 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
683 if (memory_allocation_callbacks_[i].callback == callback) {
684 memory_allocation_callbacks_.Remove(i);
685 return;
686 }
687 }
688 UNREACHABLE();
689}
690
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691
692#ifdef DEBUG
693void MemoryAllocator::ReportStatistics() {
694 float pct = static_cast<float>(capacity_ - size_) / capacity_;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000695 PrintF(" capacity: %" V8_PTR_PREFIX "d"
696 ", used: %" V8_PTR_PREFIX "d"
697 ", available: %%%d\n\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 capacity_, size_, static_cast<int>(pct*100));
699}
700#endif
701
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000702
703int MemoryAllocator::CodePageGuardStartOffset() {
704 // We are guarding code pages: the first OS page after the header
705 // will be protected as non-writable.
706 return RoundUp(Page::kObjectStartOffset, OS::CommitPageSize());
707}
708
709
710int MemoryAllocator::CodePageGuardSize() {
711 return static_cast<int>(OS::CommitPageSize());
712}
713
714
715int MemoryAllocator::CodePageAreaStartOffset() {
716 // We are guarding code pages: the first OS page after the header
717 // will be protected as non-writable.
718 return CodePageGuardStartOffset() + CodePageGuardSize();
719}
720
721
722int MemoryAllocator::CodePageAreaEndOffset() {
723 // We are guarding code pages: the last OS page will be protected as
724 // non-writable.
725 return Page::kPageSize - static_cast<int>(OS::CommitPageSize());
726}
727
728
729bool MemoryAllocator::CommitCodePage(VirtualMemory* vm,
730 Address start,
731 size_t size) {
732 // Commit page header (not executable).
733 if (!vm->Commit(start,
734 CodePageGuardStartOffset(),
735 false)) {
736 return false;
737 }
738
739 // Create guard page after the header.
740 if (!vm->Guard(start + CodePageGuardStartOffset())) {
741 return false;
742 }
743
744 // Commit page body (executable).
745 size_t area_size = size - CodePageAreaStartOffset() - CodePageGuardSize();
746 if (!vm->Commit(start + CodePageAreaStartOffset(),
747 area_size,
748 true)) {
749 return false;
750 }
751
752 // Create guard page after the allocatable area.
753 if (!vm->Guard(start + CodePageAreaStartOffset() + area_size)) {
754 return false;
755 }
756
757 return true;
758}
759
760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761// -----------------------------------------------------------------------------
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000762// MemoryChunk implementation
763
764void MemoryChunk::IncrementLiveBytesFromMutator(Address address, int by) {
765 MemoryChunk* chunk = MemoryChunk::FromAddress(address);
766 if (!chunk->InNewSpace() && !static_cast<Page*>(chunk)->WasSwept()) {
767 static_cast<PagedSpace*>(chunk->owner())->IncrementUnsweptFreeBytes(-by);
768 }
769 chunk->IncrementLiveBytes(by);
770}
771
772// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773// PagedSpace implementation
774
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000775PagedSpace::PagedSpace(Heap* heap,
776 intptr_t max_capacity,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000777 AllocationSpace id,
778 Executability executable)
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000779 : Space(heap, id, executable),
780 free_list_(this),
781 was_swept_conservatively_(false),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000782 first_unswept_page_(Page::FromAddress(NULL)),
783 unswept_free_bytes_(0) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000784 if (id == CODE_SPACE) {
785 area_size_ = heap->isolate()->memory_allocator()->
786 CodePageAreaSize();
787 } else {
788 area_size_ = Page::kPageSize - Page::kObjectStartOffset;
789 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000791 * AreaSize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 accounting_stats_.Clear();
793
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 allocation_info_.top = NULL;
795 allocation_info_.limit = NULL;
796
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000797 anchor_.InitializeAsAnchor(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798}
799
800
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000801bool PagedSpace::SetUp() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 return true;
803}
804
805
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000806bool PagedSpace::HasBeenSetUp() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000807 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808}
809
810
811void PagedSpace::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000812 PageIterator iterator(this);
813 while (iterator.has_next()) {
814 heap()->isolate()->memory_allocator()->Free(iterator.next());
815 }
816 anchor_.set_next_page(&anchor_);
817 anchor_.set_prev_page(&anchor_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 accounting_stats_.Clear();
819}
820
821
lrn@chromium.org303ada72010-10-27 09:33:13 +0000822MaybeObject* PagedSpace::FindObject(Address addr) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000823 // Note: this function can only be called on precisely swept spaces.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000824 ASSERT(!heap()->mark_compact_collector()->in_use());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825
826 if (!Contains(addr)) return Failure::Exception();
827
828 Page* p = Page::FromAddress(addr);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000829 HeapObjectIterator it(p, NULL);
830 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
831 Address cur = obj->address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832 Address next = cur + obj->Size();
833 if ((cur <= addr) && (addr < next)) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834 }
835
kasper.lund7276f142008-07-30 08:49:36 +0000836 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837 return Failure::Exception();
838}
839
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000840bool PagedSpace::CanExpand() {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000841 ASSERT(max_capacity_ % AreaSize() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842
843 if (Capacity() == max_capacity_) return false;
844
845 ASSERT(Capacity() < max_capacity_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000847 // Are we going to exceed capacity for this space?
848 if ((Capacity() + Page::kPageSize) > max_capacity_) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000850 return true;
851}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000853bool PagedSpace::Expand() {
854 if (!CanExpand()) return false;
855
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000856 intptr_t size = AreaSize();
857
858 if (anchor_.next_page() == &anchor_) {
859 size = SizeOfFirstPage();
860 }
861
862 Page* p = heap()->isolate()->memory_allocator()->AllocatePage(
863 size, this, executable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000864 if (p == NULL) return false;
865
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 ASSERT(Capacity() <= max_capacity_);
867
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000868 p->InsertAfter(anchor_.prev_page());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
870 return true;
871}
872
873
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000874intptr_t PagedSpace::SizeOfFirstPage() {
875 int size = 0;
876 switch (identity()) {
877 case OLD_POINTER_SPACE:
878 size = 64 * kPointerSize * KB;
879 break;
880 case OLD_DATA_SPACE:
881 size = 192 * KB;
882 break;
883 case MAP_SPACE:
884 size = 128 * KB;
885 break;
886 case CELL_SPACE:
887 size = 96 * KB;
888 break;
889 case CODE_SPACE:
890 if (kPointerSize == 8) {
891 // On x64 we allocate code pages in a special way (from the reserved
892 // 2Byte area). That part of the code is not yet upgraded to handle
893 // small pages.
894 size = AreaSize();
895 } else {
896 size = 384 * KB;
897 }
898 break;
899 default:
900 UNREACHABLE();
901 }
902 return Min(size, AreaSize());
903}
904
905
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906int PagedSpace::CountTotalPages() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000907 PageIterator it(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 int count = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000909 while (it.has_next()) {
910 it.next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 count++;
912 }
913 return count;
914}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
916
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000917void PagedSpace::ReleasePage(Page* page) {
918 ASSERT(page->LiveBytes() == 0);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000919 ASSERT(AreaSize() == page->area_size());
danno@chromium.org2c456792011-11-11 12:00:53 +0000920
ricow@chromium.org7ad65222011-12-19 12:13:11 +0000921 // Adjust list of unswept pages if the page is the head of the list.
danno@chromium.org2c456792011-11-11 12:00:53 +0000922 if (first_unswept_page_ == page) {
923 first_unswept_page_ = page->next_page();
924 if (first_unswept_page_ == anchor()) {
925 first_unswept_page_ = Page::FromAddress(NULL);
926 }
927 }
928
929 if (page->WasSwept()) {
930 intptr_t size = free_list_.EvictFreeListItems(page);
931 accounting_stats_.AllocateBytes(size);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000932 ASSERT_EQ(AreaSize(), static_cast<int>(size));
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000933 } else {
934 DecreaseUnsweptFreeBytes(page);
danno@chromium.org2c456792011-11-11 12:00:53 +0000935 }
936
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000937 if (Page::FromAllocationTop(allocation_info_.top) == page) {
938 allocation_info_.top = allocation_info_.limit = NULL;
939 }
940
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000941 page->Unlink();
942 if (page->IsFlagSet(MemoryChunk::CONTAINS_ONLY_DATA)) {
943 heap()->isolate()->memory_allocator()->Free(page);
944 } else {
945 heap()->QueueMemoryChunkForFree(page);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000946 }
947
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000948 ASSERT(Capacity() > 0);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000949 accounting_stats_.ShrinkSpace(AreaSize());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950}
951
952
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000953void PagedSpace::ReleaseAllUnusedPages() {
954 PageIterator it(this);
955 while (it.has_next()) {
956 Page* page = it.next();
danno@chromium.org2c456792011-11-11 12:00:53 +0000957 if (!page->WasSwept()) {
958 if (page->LiveBytes() == 0) ReleasePage(page);
959 } else {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000960 HeapObject* obj = HeapObject::FromAddress(page->area_start());
danno@chromium.org2c456792011-11-11 12:00:53 +0000961 if (obj->IsFreeSpace() &&
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000962 FreeSpace::cast(obj)->size() == AreaSize()) {
danno@chromium.org2c456792011-11-11 12:00:53 +0000963 // Sometimes we allocate memory from free list but don't
964 // immediately initialize it (e.g. see PagedSpace::ReserveSpace
965 // called from Heap::ReserveSpace that can cause GC before
966 // reserved space is actually initialized).
967 // Thus we can't simply assume that obj represents a valid
968 // node still owned by a free list
969 // Instead we should verify that the page is fully covered
970 // by free list items.
971 FreeList::SizeStats sizes;
972 free_list_.CountFreeListItems(page, &sizes);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000973 if (sizes.Total() == AreaSize()) {
danno@chromium.org2c456792011-11-11 12:00:53 +0000974 ReleasePage(page);
975 }
976 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000977 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000979 heap()->FreeQueuedChunks();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980}
981
982
983#ifdef DEBUG
984void PagedSpace::Print() { }
985#endif
986
987
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000988#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000989void PagedSpace::Verify(ObjectVisitor* visitor) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000990 // We can only iterate over the pages if they were swept precisely.
991 if (was_swept_conservatively_) return;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000992
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000993 bool allocation_pointer_found_in_space =
994 (allocation_info_.top == allocation_info_.limit);
995 PageIterator page_iterator(this);
996 while (page_iterator.has_next()) {
997 Page* page = page_iterator.next();
998 ASSERT(page->owner() == this);
999 if (page == Page::FromAllocationTop(allocation_info_.top)) {
1000 allocation_pointer_found_in_space = true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001001 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001002 ASSERT(page->WasSweptPrecisely());
1003 HeapObjectIterator it(page, NULL);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001004 Address end_of_previous_object = page->area_start();
1005 Address top = page->area_end();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001006 int black_size = 0;
1007 for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) {
1008 ASSERT(end_of_previous_object <= object->address());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001009
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001010 // The first word should be a map, and we expect all map pointers to
1011 // be in map space.
1012 Map* map = object->map();
1013 ASSERT(map->IsMap());
1014 ASSERT(heap()->map_space()->Contains(map));
1015
1016 // Perform space-specific object verification.
1017 VerifyObject(object);
1018
1019 // The object itself should look OK.
1020 object->Verify();
1021
1022 // All the interior pointers should be contained in the heap.
1023 int size = object->Size();
1024 object->IterateBody(map->instance_type(), size, visitor);
1025 if (Marking::IsBlack(Marking::MarkBitFrom(object))) {
1026 black_size += size;
1027 }
1028
1029 ASSERT(object->address() + size <= top);
1030 end_of_previous_object = object->address() + size;
1031 }
1032 ASSERT_LE(black_size, page->LiveBytes());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001033 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001034 ASSERT(allocation_pointer_found_in_space);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001035}
1036#endif
1037
1038
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039// -----------------------------------------------------------------------------
1040// NewSpace implementation
1041
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001042
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001043bool NewSpace::SetUp(int reserved_semispace_capacity,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001044 int maximum_semispace_capacity) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001045 // Set up new space based on the preallocated memory block defined by
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001046 // start and size. The provided space is divided into two semi-spaces.
1047 // To support fast containment testing in the new space, the size of
1048 // this chunk must be a power of two and it must be aligned to its size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001049 int initial_semispace_capacity = heap()->InitialSemiSpaceSize();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001050
1051 size_t size = 2 * reserved_semispace_capacity;
1052 Address base =
1053 heap()->isolate()->memory_allocator()->ReserveAlignedMemory(
1054 size, size, &reservation_);
1055 if (base == NULL) return false;
1056
1057 chunk_base_ = base;
1058 chunk_size_ = static_cast<uintptr_t>(size);
1059 LOG(heap()->isolate(), NewEvent("InitialChunk", chunk_base_, chunk_size_));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
1062 ASSERT(IsPowerOf2(maximum_semispace_capacity));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001064 // Allocate and set up the histogram arrays if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1066 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1067
1068#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
1069 promoted_histogram_[name].set_name(#name);
1070 INSTANCE_TYPE_LIST(SET_NAME)
1071#undef SET_NAME
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001073 ASSERT(reserved_semispace_capacity == heap()->ReservedSemiSpaceSize());
1074 ASSERT(static_cast<intptr_t>(chunk_size_) >=
1075 2 * heap()->ReservedSemiSpaceSize());
1076 ASSERT(IsAddressAligned(chunk_base_, 2 * reserved_semispace_capacity, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001078 to_space_.SetUp(chunk_base_,
1079 initial_semispace_capacity,
1080 maximum_semispace_capacity);
1081 from_space_.SetUp(chunk_base_ + reserved_semispace_capacity,
1082 initial_semispace_capacity,
1083 maximum_semispace_capacity);
1084 if (!to_space_.Commit()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 return false;
1086 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001087 ASSERT(!from_space_.is_committed()); // No need to use memory yet.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001089 start_ = chunk_base_;
1090 address_mask_ = ~(2 * reserved_semispace_capacity - 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001091 object_mask_ = address_mask_ | kHeapObjectTagMask;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001092 object_expected_ = reinterpret_cast<uintptr_t>(start_) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001094 ResetAllocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 return true;
1097}
1098
1099
1100void NewSpace::TearDown() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 if (allocated_histogram_) {
1102 DeleteArray(allocated_histogram_);
1103 allocated_histogram_ = NULL;
1104 }
1105 if (promoted_histogram_) {
1106 DeleteArray(promoted_histogram_);
1107 promoted_histogram_ = NULL;
1108 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
1110 start_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 allocation_info_.top = NULL;
1112 allocation_info_.limit = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001114 to_space_.TearDown();
1115 from_space_.TearDown();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001116
1117 LOG(heap()->isolate(), DeleteEvent("InitialChunk", chunk_base_));
1118
1119 ASSERT(reservation_.IsReserved());
1120 heap()->isolate()->memory_allocator()->FreeMemory(&reservation_,
1121 NOT_EXECUTABLE);
1122 chunk_base_ = NULL;
1123 chunk_size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124}
1125
1126
1127void NewSpace::Flip() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001128 SemiSpace::Swap(&from_space_, &to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129}
1130
1131
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001132void NewSpace::Grow() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001133 // Double the semispace size but only up to maximum capacity.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001134 ASSERT(Capacity() < MaximumCapacity());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001135 int new_capacity = Min(MaximumCapacity(), 2 * static_cast<int>(Capacity()));
1136 if (to_space_.GrowTo(new_capacity)) {
1137 // Only grow from space if we managed to grow to-space.
1138 if (!from_space_.GrowTo(new_capacity)) {
1139 // If we managed to grow to-space but couldn't grow from-space,
1140 // attempt to shrink to-space.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001141 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
1142 // We are in an inconsistent state because we could not
1143 // commit/uncommit memory from new space.
1144 V8::FatalProcessOutOfMemory("Failed to grow new space.");
1145 }
1146 }
1147 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001149}
1150
1151
1152void NewSpace::Shrink() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001153 int new_capacity = Max(InitialCapacity(), 2 * SizeAsInt());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001154 int rounded_new_capacity = RoundUp(new_capacity, Page::kPageSize);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001155 if (rounded_new_capacity < Capacity() &&
1156 to_space_.ShrinkTo(rounded_new_capacity)) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001157 // Only shrink from-space if we managed to shrink to-space.
1158 from_space_.Reset();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001159 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001160 // If we managed to shrink to-space but couldn't shrink from
1161 // space, attempt to grow to-space again.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001162 if (!to_space_.GrowTo(from_space_.Capacity())) {
1163 // We are in an inconsistent state because we could not
1164 // commit/uncommit memory from new space.
1165 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
1166 }
1167 }
1168 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001169 allocation_info_.limit = to_space_.page_high();
1170 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1171}
1172
1173
1174void NewSpace::UpdateAllocationInfo() {
1175 allocation_info_.top = to_space_.page_low();
1176 allocation_info_.limit = to_space_.page_high();
1177
1178 // Lower limit during incremental marking.
1179 if (heap()->incremental_marking()->IsMarking() &&
1180 inline_allocation_limit_step() != 0) {
1181 Address new_limit =
1182 allocation_info_.top + inline_allocation_limit_step();
1183 allocation_info_.limit = Min(new_limit, allocation_info_.limit);
1184 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001185 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186}
1187
1188
1189void NewSpace::ResetAllocationInfo() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001190 to_space_.Reset();
1191 UpdateAllocationInfo();
1192 pages_used_ = 0;
1193 // Clear all mark-bits in the to-space.
1194 NewSpacePageIterator it(&to_space_);
1195 while (it.has_next()) {
1196 Bitmap::Clear(it.next());
1197 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198}
1199
1200
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001201bool NewSpace::AddFreshPage() {
1202 Address top = allocation_info_.top;
1203 if (NewSpacePage::IsAtStart(top)) {
1204 // The current page is already empty. Don't try to make another.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001206 // We should only get here if someone asks to allocate more
1207 // than what can be stored in a single page.
1208 // TODO(gc): Change the limit on new-space allocation to prevent this
1209 // from happening (all such allocations should go directly to LOSpace).
1210 return false;
1211 }
1212 if (!to_space_.AdvancePage()) {
1213 // Failed to get a new page in to-space.
1214 return false;
1215 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001216
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001217 // Clear remainder of current page.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001218 Address limit = NewSpacePage::FromLimit(top)->area_end();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001219 if (heap()->gc_state() == Heap::SCAVENGE) {
1220 heap()->promotion_queue()->SetNewLimit(limit);
1221 heap()->promotion_queue()->ActivateGuardIfOnTheSamePage();
1222 }
1223
1224 int remaining_in_page = static_cast<int>(limit - top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001225 heap()->CreateFillerObjectAt(top, remaining_in_page);
1226 pages_used_++;
1227 UpdateAllocationInfo();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001228
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001229 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230}
1231
1232
danno@chromium.orgc612e022011-11-10 11:38:15 +00001233MaybeObject* NewSpace::SlowAllocateRaw(int size_in_bytes) {
1234 Address old_top = allocation_info_.top;
1235 Address new_top = old_top + size_in_bytes;
1236 Address high = to_space_.page_high();
1237 if (allocation_info_.limit < high) {
1238 // Incremental marking has lowered the limit to get a
1239 // chance to do a step.
1240 allocation_info_.limit = Min(
1241 allocation_info_.limit + inline_allocation_limit_step_,
1242 high);
1243 int bytes_allocated = static_cast<int>(new_top - top_on_previous_step_);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001244 heap()->incremental_marking()->Step(
1245 bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001246 top_on_previous_step_ = new_top;
1247 return AllocateRaw(size_in_bytes);
1248 } else if (AddFreshPage()) {
1249 // Switched to new page. Try allocating again.
1250 int bytes_allocated = static_cast<int>(old_top - top_on_previous_step_);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001251 heap()->incremental_marking()->Step(
1252 bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001253 top_on_previous_step_ = to_space_.page_low();
1254 return AllocateRaw(size_in_bytes);
1255 } else {
1256 return Failure::RetryAfterGC();
1257 }
1258}
1259
1260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261#ifdef DEBUG
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001262// We do not use the SemiSpaceIterator because verification doesn't assume
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263// that it works (it depends on the invariants we are checking).
1264void NewSpace::Verify() {
1265 // The allocation pointer should be in the space or at the very end.
1266 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1267
1268 // There should be objects packed in from the low address up to the
1269 // allocation pointer.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001270 Address current = to_space_.first_page()->area_start();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001271 CHECK_EQ(current, to_space_.space_start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001273 while (current != top()) {
1274 if (!NewSpacePage::IsAtEnd(current)) {
1275 // The allocation pointer should not be in the middle of an object.
1276 CHECK(!NewSpacePage::FromLimit(current)->ContainsLimit(top()) ||
1277 current < top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001279 HeapObject* object = HeapObject::FromAddress(current);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001281 // The first word should be a map, and we expect all map pointers to
1282 // be in map space.
1283 Map* map = object->map();
1284 CHECK(map->IsMap());
1285 CHECK(heap()->map_space()->Contains(map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001287 // The object should not be code or a map.
1288 CHECK(!object->IsMap());
1289 CHECK(!object->IsCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001291 // The object itself should look OK.
1292 object->Verify();
1293
1294 // All the interior pointers should be contained in the heap.
1295 VerifyPointersVisitor visitor;
1296 int size = object->Size();
1297 object->IterateBody(map->instance_type(), size, &visitor);
1298
1299 current += size;
1300 } else {
1301 // At end of page, switch to next page.
1302 NewSpacePage* page = NewSpacePage::FromLimit(current)->next_page();
1303 // Next page should be valid.
1304 CHECK(!page->is_anchor());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001305 current = page->area_start();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001306 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 }
1308
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001309 // Check semi-spaces.
1310 ASSERT_EQ(from_space_.id(), kFromSpace);
1311 ASSERT_EQ(to_space_.id(), kToSpace);
1312 from_space_.Verify();
1313 to_space_.Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314}
1315#endif
1316
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317// -----------------------------------------------------------------------------
1318// SemiSpace implementation
1319
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001320void SemiSpace::SetUp(Address start,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001321 int initial_capacity,
1322 int maximum_capacity) {
1323 // Creates a space in the young generation. The constructor does not
1324 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1325 // memory of size 'capacity' when set up, and does not grow or shrink
1326 // otherwise. In the mark-compact collector, the memory region of the from
1327 // space is used as the marking stack. It requires contiguous memory
1328 // addresses.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001329 ASSERT(maximum_capacity >= Page::kPageSize);
1330 initial_capacity_ = RoundDown(initial_capacity, Page::kPageSize);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001331 capacity_ = initial_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001332 maximum_capacity_ = RoundDown(maximum_capacity, Page::kPageSize);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001333 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 start_ = start;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001335 address_mask_ = ~(maximum_capacity - 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001336 object_mask_ = address_mask_ | kHeapObjectTagMask;
ager@chromium.org9085a012009-05-11 19:22:57 +00001337 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 age_mark_ = start_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339}
1340
1341
1342void SemiSpace::TearDown() {
1343 start_ = NULL;
1344 capacity_ = 0;
1345}
1346
1347
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001348bool SemiSpace::Commit() {
1349 ASSERT(!is_committed());
1350 int pages = capacity_ / Page::kPageSize;
1351 Address end = start_ + maximum_capacity_;
1352 Address start = end - pages * Page::kPageSize;
1353 if (!heap()->isolate()->memory_allocator()->CommitBlock(start,
1354 capacity_,
1355 executable())) {
kasper.lund7276f142008-07-30 08:49:36 +00001356 return false;
1357 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001358
1359 NewSpacePage* page = anchor();
1360 for (int i = 1; i <= pages; i++) {
1361 NewSpacePage* new_page =
1362 NewSpacePage::Initialize(heap(), end - i * Page::kPageSize, this);
1363 new_page->InsertAfter(page);
1364 page = new_page;
1365 }
1366
1367 committed_ = true;
1368 Reset();
1369 return true;
1370}
1371
1372
1373bool SemiSpace::Uncommit() {
1374 ASSERT(is_committed());
1375 Address start = start_ + maximum_capacity_ - capacity_;
1376 if (!heap()->isolate()->memory_allocator()->UncommitBlock(start, capacity_)) {
1377 return false;
1378 }
1379 anchor()->set_next_page(anchor());
1380 anchor()->set_prev_page(anchor());
1381
1382 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 return true;
1384}
1385
1386
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001387bool SemiSpace::GrowTo(int new_capacity) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001388 if (!is_committed()) {
1389 if (!Commit()) return false;
1390 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001391 ASSERT((new_capacity & Page::kPageAlignmentMask) == 0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001392 ASSERT(new_capacity <= maximum_capacity_);
1393 ASSERT(new_capacity > capacity_);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001394 int pages_before = capacity_ / Page::kPageSize;
1395 int pages_after = new_capacity / Page::kPageSize;
1396
1397 Address end = start_ + maximum_capacity_;
1398 Address start = end - new_capacity;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001399 size_t delta = new_capacity - capacity_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001400
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001401 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001402 if (!heap()->isolate()->memory_allocator()->CommitBlock(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001403 start, delta, executable())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001404 return false;
1405 }
1406 capacity_ = new_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001407 NewSpacePage* last_page = anchor()->prev_page();
1408 ASSERT(last_page != anchor());
1409 for (int i = pages_before + 1; i <= pages_after; i++) {
1410 Address page_address = end - i * Page::kPageSize;
1411 NewSpacePage* new_page = NewSpacePage::Initialize(heap(),
1412 page_address,
1413 this);
1414 new_page->InsertAfter(last_page);
1415 Bitmap::Clear(new_page);
1416 // Duplicate the flags that was set on the old page.
1417 new_page->SetFlags(last_page->GetFlags(),
1418 NewSpacePage::kCopyOnFlipFlagsMask);
1419 last_page = new_page;
1420 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001421 return true;
1422}
1423
1424
1425bool SemiSpace::ShrinkTo(int new_capacity) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001426 ASSERT((new_capacity & Page::kPageAlignmentMask) == 0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001427 ASSERT(new_capacity >= initial_capacity_);
1428 ASSERT(new_capacity < capacity_);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001429 if (is_committed()) {
1430 // Semispaces grow backwards from the end of their allocated capacity,
1431 // so we find the before and after start addresses relative to the
1432 // end of the space.
1433 Address space_end = start_ + maximum_capacity_;
1434 Address old_start = space_end - capacity_;
1435 size_t delta = capacity_ - new_capacity;
1436 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001437
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001438 MemoryAllocator* allocator = heap()->isolate()->memory_allocator();
1439 if (!allocator->UncommitBlock(old_start, delta)) {
1440 return false;
1441 }
1442
1443 int pages_after = new_capacity / Page::kPageSize;
1444 NewSpacePage* new_last_page =
1445 NewSpacePage::FromAddress(space_end - pages_after * Page::kPageSize);
1446 new_last_page->set_next_page(anchor());
1447 anchor()->set_prev_page(new_last_page);
1448 ASSERT((current_page_ <= first_page()) && (current_page_ >= new_last_page));
1449 }
1450
1451 capacity_ = new_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001452
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001453 return true;
1454}
1455
1456
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001457void SemiSpace::FlipPages(intptr_t flags, intptr_t mask) {
1458 anchor_.set_owner(this);
1459 // Fixup back-pointers to anchor. Address of anchor changes
1460 // when we swap.
1461 anchor_.prev_page()->set_next_page(&anchor_);
1462 anchor_.next_page()->set_prev_page(&anchor_);
1463
1464 bool becomes_to_space = (id_ == kFromSpace);
1465 id_ = becomes_to_space ? kToSpace : kFromSpace;
1466 NewSpacePage* page = anchor_.next_page();
1467 while (page != &anchor_) {
1468 page->set_owner(this);
1469 page->SetFlags(flags, mask);
1470 if (becomes_to_space) {
1471 page->ClearFlag(MemoryChunk::IN_FROM_SPACE);
1472 page->SetFlag(MemoryChunk::IN_TO_SPACE);
1473 page->ClearFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK);
1474 page->ResetLiveBytes();
1475 } else {
1476 page->SetFlag(MemoryChunk::IN_FROM_SPACE);
1477 page->ClearFlag(MemoryChunk::IN_TO_SPACE);
1478 }
1479 ASSERT(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE));
1480 ASSERT(page->IsFlagSet(MemoryChunk::IN_TO_SPACE) ||
1481 page->IsFlagSet(MemoryChunk::IN_FROM_SPACE));
1482 page = page->next_page();
1483 }
1484}
1485
1486
1487void SemiSpace::Reset() {
1488 ASSERT(anchor_.next_page() != &anchor_);
1489 current_page_ = anchor_.next_page();
1490}
1491
1492
1493void SemiSpace::Swap(SemiSpace* from, SemiSpace* to) {
1494 // We won't be swapping semispaces without data in them.
1495 ASSERT(from->anchor_.next_page() != &from->anchor_);
1496 ASSERT(to->anchor_.next_page() != &to->anchor_);
1497
1498 // Swap bits.
1499 SemiSpace tmp = *from;
1500 *from = *to;
1501 *to = tmp;
1502
1503 // Fixup back-pointers to the page list anchor now that its address
1504 // has changed.
1505 // Swap to/from-space bits on pages.
1506 // Copy GC flags from old active space (from-space) to new (to-space).
1507 intptr_t flags = from->current_page()->GetFlags();
1508 to->FlipPages(flags, NewSpacePage::kCopyOnFlipFlagsMask);
1509
1510 from->FlipPages(0, 0);
1511}
1512
1513
1514void SemiSpace::set_age_mark(Address mark) {
1515 ASSERT(NewSpacePage::FromLimit(mark)->semi_space() == this);
1516 age_mark_ = mark;
1517 // Mark all pages up to the one containing mark.
1518 NewSpacePageIterator it(space_start(), mark);
1519 while (it.has_next()) {
1520 it.next()->SetFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK);
1521 }
1522}
1523
1524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525#ifdef DEBUG
1526void SemiSpace::Print() { }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001527
1528
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001529void SemiSpace::Verify() {
1530 bool is_from_space = (id_ == kFromSpace);
1531 NewSpacePage* page = anchor_.next_page();
1532 CHECK(anchor_.semi_space() == this);
1533 while (page != &anchor_) {
1534 CHECK(page->semi_space() == this);
1535 CHECK(page->InNewSpace());
1536 CHECK(page->IsFlagSet(is_from_space ? MemoryChunk::IN_FROM_SPACE
1537 : MemoryChunk::IN_TO_SPACE));
1538 CHECK(!page->IsFlagSet(is_from_space ? MemoryChunk::IN_TO_SPACE
1539 : MemoryChunk::IN_FROM_SPACE));
1540 CHECK(page->IsFlagSet(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING));
1541 if (!is_from_space) {
1542 // The pointers-from-here-are-interesting flag isn't updated dynamically
1543 // on from-space pages, so it might be out of sync with the marking state.
1544 if (page->heap()->incremental_marking()->IsMarking()) {
1545 CHECK(page->IsFlagSet(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING));
1546 } else {
1547 CHECK(!page->IsFlagSet(
1548 MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING));
1549 }
1550 // TODO(gc): Check that the live_bytes_count_ field matches the
1551 // black marking on the page (if we make it match in new-space).
1552 }
1553 CHECK(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE));
1554 CHECK(page->prev_page()->next_page() == page);
1555 page = page->next_page();
1556 }
1557}
1558
1559
1560void SemiSpace::AssertValidRange(Address start, Address end) {
1561 // Addresses belong to same semi-space
1562 NewSpacePage* page = NewSpacePage::FromLimit(start);
1563 NewSpacePage* end_page = NewSpacePage::FromLimit(end);
1564 SemiSpace* space = page->semi_space();
1565 CHECK_EQ(space, end_page->semi_space());
1566 // Start address is before end address, either on same page,
1567 // or end address is on a later page in the linked list of
1568 // semi-space pages.
1569 if (page == end_page) {
1570 CHECK(start <= end);
1571 } else {
1572 while (page != end_page) {
1573 page = page->next_page();
1574 CHECK_NE(page, space->anchor());
1575 }
1576 }
1577}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578#endif
1579
1580
1581// -----------------------------------------------------------------------------
1582// SemiSpaceIterator implementation.
1583SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001584 Initialize(space->bottom(), space->top(), NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585}
1586
1587
1588SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1589 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001590 Initialize(space->bottom(), space->top(), size_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591}
1592
1593
1594SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001595 Initialize(start, space->top(), NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596}
1597
1598
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001599SemiSpaceIterator::SemiSpaceIterator(Address from, Address to) {
1600 Initialize(from, to, NULL);
1601}
1602
1603
1604void SemiSpaceIterator::Initialize(Address start,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 Address end,
1606 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001607 SemiSpace::AssertValidRange(start, end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 current_ = start;
1609 limit_ = end;
1610 size_func_ = size_func;
1611}
1612
1613
1614#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615// heap_histograms is shared, always clear it before using it.
1616static void ClearHistograms() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001617 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 // We reset the name each time, though it hasn't changed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001619#define DEF_TYPE_NAME(name) isolate->heap_histograms()[name].set_name(#name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1621#undef DEF_TYPE_NAME
1622
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001623#define CLEAR_HISTOGRAM(name) isolate->heap_histograms()[name].clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1625#undef CLEAR_HISTOGRAM
1626
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001627 isolate->js_spill_information()->Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628}
1629
1630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631static void ClearCodeKindStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001632 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001634 isolate->code_kind_statistics()[i] = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 }
1636}
1637
1638
1639static void ReportCodeKindStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001640 Isolate* isolate = Isolate::Current();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001641 const char* table[Code::NUMBER_OF_KINDS] = { NULL };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642
1643#define CASE(name) \
1644 case Code::name: table[Code::name] = #name; \
1645 break
1646
1647 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1648 switch (static_cast<Code::Kind>(i)) {
1649 CASE(FUNCTION);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001650 CASE(OPTIMIZED_FUNCTION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 CASE(STUB);
1652 CASE(BUILTIN);
1653 CASE(LOAD_IC);
1654 CASE(KEYED_LOAD_IC);
1655 CASE(STORE_IC);
1656 CASE(KEYED_STORE_IC);
1657 CASE(CALL_IC);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001658 CASE(KEYED_CALL_IC);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001659 CASE(UNARY_OP_IC);
1660 CASE(BINARY_OP_IC);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001661 CASE(COMPARE_IC);
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001662 CASE(TO_BOOLEAN_IC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 }
1664 }
1665
1666#undef CASE
1667
1668 PrintF("\n Code kind histograms: \n");
1669 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001670 if (isolate->code_kind_statistics()[i] > 0) {
1671 PrintF(" %-20s: %10d bytes\n", table[i],
1672 isolate->code_kind_statistics()[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 }
1674 }
1675 PrintF("\n");
1676}
1677
1678
1679static int CollectHistogramInfo(HeapObject* obj) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001680 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 InstanceType type = obj->map()->instance_type();
1682 ASSERT(0 <= type && type <= LAST_TYPE);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001683 ASSERT(isolate->heap_histograms()[type].name() != NULL);
1684 isolate->heap_histograms()[type].increment_number(1);
1685 isolate->heap_histograms()[type].increment_bytes(obj->Size());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
1687 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001688 JSObject::cast(obj)->IncrementSpillStatistics(
1689 isolate->js_spill_information());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 }
1691
1692 return obj->Size();
1693}
1694
1695
1696static void ReportHistogram(bool print_spill) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001697 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 PrintF("\n Object Histogram:\n");
1699 for (int i = 0; i <= LAST_TYPE; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001700 if (isolate->heap_histograms()[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001701 PrintF(" %-34s%10d (%10d bytes)\n",
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001702 isolate->heap_histograms()[i].name(),
1703 isolate->heap_histograms()[i].number(),
1704 isolate->heap_histograms()[i].bytes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 }
1706 }
1707 PrintF("\n");
1708
1709 // Summarize string types.
1710 int string_number = 0;
1711 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001712#define INCREMENT(type, size, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001713 string_number += isolate->heap_histograms()[type].number(); \
1714 string_bytes += isolate->heap_histograms()[type].bytes();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 STRING_TYPE_LIST(INCREMENT)
1716#undef INCREMENT
1717 if (string_number > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001718 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 string_bytes);
1720 }
1721
1722 if (FLAG_collect_heap_spill_statistics && print_spill) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001723 isolate->js_spill_information()->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724 }
1725}
1726#endif // DEBUG
1727
1728
1729// Support for statistics gathering for --heap-stats and --log-gc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730void NewSpace::ClearHistograms() {
1731 for (int i = 0; i <= LAST_TYPE; i++) {
1732 allocated_histogram_[i].clear();
1733 promoted_histogram_[i].clear();
1734 }
1735}
1736
1737// Because the copying collector does not touch garbage objects, we iterate
1738// the new space before a collection to get a histogram of allocated objects.
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001739// This only happens when --log-gc flag is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740void NewSpace::CollectStatistics() {
1741 ClearHistograms();
1742 SemiSpaceIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001743 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next())
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001744 RecordAllocation(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745}
1746
1747
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001748static void DoReportStatistics(Isolate* isolate,
1749 HistogramInfo* info, const char* description) {
1750 LOG(isolate, HeapSampleBeginEvent("NewSpace", description));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 // Lump all the string types together.
1752 int string_number = 0;
1753 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001754#define INCREMENT(type, size, name, camel_name) \
1755 string_number += info[type].number(); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 string_bytes += info[type].bytes();
1757 STRING_TYPE_LIST(INCREMENT)
1758#undef INCREMENT
1759 if (string_number > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001760 LOG(isolate,
1761 HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 }
1763
1764 // Then do the other types.
1765 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1766 if (info[i].number() > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001767 LOG(isolate,
1768 HeapSampleItemEvent(info[i].name(), info[i].number(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 info[i].bytes()));
1770 }
1771 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001772 LOG(isolate, HeapSampleEndEvent("NewSpace", description));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774
1775
1776void NewSpace::ReportStatistics() {
1777#ifdef DEBUG
1778 if (FLAG_heap_stats) {
1779 float pct = static_cast<float>(Available()) / Capacity();
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001780 PrintF(" capacity: %" V8_PTR_PREFIX "d"
1781 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782 Capacity(), Available(), static_cast<int>(pct*100));
1783 PrintF("\n Object Histogram:\n");
1784 for (int i = 0; i <= LAST_TYPE; i++) {
1785 if (allocated_histogram_[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001786 PrintF(" %-34s%10d (%10d bytes)\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 allocated_histogram_[i].name(),
1788 allocated_histogram_[i].number(),
1789 allocated_histogram_[i].bytes());
1790 }
1791 }
1792 PrintF("\n");
1793 }
1794#endif // DEBUG
1795
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 if (FLAG_log_gc) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001797 Isolate* isolate = ISOLATE;
1798 DoReportStatistics(isolate, allocated_histogram_, "allocated");
1799 DoReportStatistics(isolate, promoted_histogram_, "promoted");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801}
1802
1803
1804void NewSpace::RecordAllocation(HeapObject* obj) {
1805 InstanceType type = obj->map()->instance_type();
1806 ASSERT(0 <= type && type <= LAST_TYPE);
1807 allocated_histogram_[type].increment_number(1);
1808 allocated_histogram_[type].increment_bytes(obj->Size());
1809}
1810
1811
1812void NewSpace::RecordPromotion(HeapObject* obj) {
1813 InstanceType type = obj->map()->instance_type();
1814 ASSERT(0 <= type && type <= LAST_TYPE);
1815 promoted_histogram_[type].increment_number(1);
1816 promoted_histogram_[type].increment_bytes(obj->Size());
1817}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819// -----------------------------------------------------------------------------
1820// Free lists for old object spaces implementation
1821
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001822void FreeListNode::set_size(Heap* heap, int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 ASSERT(size_in_bytes > 0);
1824 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1825
1826 // We write a map and possibly size information to the block. If the block
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001827 // is big enough to be a FreeSpace with at least one extra word (the next
1828 // pointer), we set its map to be the free space map and its size to an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 // appropriate array length for the desired size from HeapObject::Size().
1830 // If the block is too small (eg, one or two words), to hold both a size
1831 // field and a next pointer, we give it a filler map that gives it the
1832 // correct size.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001833 if (size_in_bytes > FreeSpace::kHeaderSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001834 set_map_no_write_barrier(heap->raw_unchecked_free_space_map());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001835 // Can't use FreeSpace::cast because it fails during deserialization.
1836 FreeSpace* this_as_free_space = reinterpret_cast<FreeSpace*>(this);
1837 this_as_free_space->set_size(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 } else if (size_in_bytes == kPointerSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001839 set_map_no_write_barrier(heap->raw_unchecked_one_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 } else if (size_in_bytes == 2 * kPointerSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001841 set_map_no_write_barrier(heap->raw_unchecked_two_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 } else {
1843 UNREACHABLE();
1844 }
ager@chromium.org3811b432009-10-28 14:53:37 +00001845 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001846 // deserialization because the free space map is not done yet.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847}
1848
1849
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001850FreeListNode* FreeListNode::next() {
ager@chromium.org3811b432009-10-28 14:53:37 +00001851 ASSERT(IsFreeListNode(this));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001852 if (map() == HEAP->raw_unchecked_free_space_map()) {
1853 ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize);
1854 return reinterpret_cast<FreeListNode*>(
1855 Memory::Address_at(address() + kNextOffset));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001856 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001857 return reinterpret_cast<FreeListNode*>(
1858 Memory::Address_at(address() + kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001859 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860}
1861
1862
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001863FreeListNode** FreeListNode::next_address() {
ager@chromium.org3811b432009-10-28 14:53:37 +00001864 ASSERT(IsFreeListNode(this));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001865 if (map() == HEAP->raw_unchecked_free_space_map()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001866 ASSERT(Size() >= kNextOffset + kPointerSize);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001867 return reinterpret_cast<FreeListNode**>(address() + kNextOffset);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001868 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001869 return reinterpret_cast<FreeListNode**>(address() + kPointerSize);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001870 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871}
1872
1873
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001874void FreeListNode::set_next(FreeListNode* next) {
1875 ASSERT(IsFreeListNode(this));
1876 // While we are booting the VM the free space map will actually be null. So
1877 // we have to make sure that we don't try to use it for anything at that
1878 // stage.
1879 if (map() == HEAP->raw_unchecked_free_space_map()) {
1880 ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize);
1881 Memory::Address_at(address() + kNextOffset) =
1882 reinterpret_cast<Address>(next);
1883 } else {
1884 Memory::Address_at(address() + kPointerSize) =
1885 reinterpret_cast<Address>(next);
1886 }
1887}
1888
1889
1890FreeList::FreeList(PagedSpace* owner)
1891 : owner_(owner), heap_(owner->heap()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 Reset();
1893}
1894
1895
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001896void FreeList::Reset() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 available_ = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001898 small_list_ = NULL;
1899 medium_list_ = NULL;
1900 large_list_ = NULL;
1901 huge_list_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902}
1903
1904
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001905int FreeList::Free(Address start, int size_in_bytes) {
1906 if (size_in_bytes == 0) return 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 FreeListNode* node = FreeListNode::FromAddress(start);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001908 node->set_size(heap_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001910 // Early return to drop too-small blocks on the floor.
1911 if (size_in_bytes < kSmallListMin) return size_in_bytes;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001912
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001913 // Insert other blocks at the head of a free list of the appropriate
1914 // magnitude.
1915 if (size_in_bytes <= kSmallListMax) {
1916 node->set_next(small_list_);
1917 small_list_ = node;
1918 } else if (size_in_bytes <= kMediumListMax) {
1919 node->set_next(medium_list_);
1920 medium_list_ = node;
1921 } else if (size_in_bytes <= kLargeListMax) {
1922 node->set_next(large_list_);
1923 large_list_ = node;
1924 } else {
1925 node->set_next(huge_list_);
1926 huge_list_ = node;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 available_ += size_in_bytes;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001929 ASSERT(IsVeryLong() || available_ == SumFreeLists());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 return 0;
1931}
1932
1933
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001934FreeListNode* FreeList::PickNodeFromList(FreeListNode** list, int* node_size) {
1935 FreeListNode* node = *list;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001937 if (node == NULL) return NULL;
1938
1939 while (node != NULL &&
1940 Page::FromAddress(node->address())->IsEvacuationCandidate()) {
1941 available_ -= node->Size();
1942 node = node->next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001944
1945 if (node != NULL) {
1946 *node_size = node->Size();
1947 *list = node->next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001949 *list = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 }
1951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 return node;
1953}
1954
1955
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001956FreeListNode* FreeList::FindNodeFor(int size_in_bytes, int* node_size) {
1957 FreeListNode* node = NULL;
1958
1959 if (size_in_bytes <= kSmallAllocationMax) {
1960 node = PickNodeFromList(&small_list_, node_size);
1961 if (node != NULL) return node;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001962 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001963
1964 if (size_in_bytes <= kMediumAllocationMax) {
1965 node = PickNodeFromList(&medium_list_, node_size);
1966 if (node != NULL) return node;
1967 }
1968
1969 if (size_in_bytes <= kLargeAllocationMax) {
1970 node = PickNodeFromList(&large_list_, node_size);
1971 if (node != NULL) return node;
1972 }
1973
1974 for (FreeListNode** cur = &huge_list_;
1975 *cur != NULL;
1976 cur = (*cur)->next_address()) {
1977 FreeListNode* cur_node = *cur;
1978 while (cur_node != NULL &&
1979 Page::FromAddress(cur_node->address())->IsEvacuationCandidate()) {
1980 available_ -= reinterpret_cast<FreeSpace*>(cur_node)->Size();
1981 cur_node = cur_node->next();
1982 }
1983
1984 *cur = cur_node;
1985 if (cur_node == NULL) break;
1986
1987 ASSERT((*cur)->map() == HEAP->raw_unchecked_free_space_map());
1988 FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(*cur);
1989 int size = cur_as_free_space->Size();
1990 if (size >= size_in_bytes) {
1991 // Large enough node found. Unlink it from the list.
1992 node = *cur;
1993 *node_size = size;
1994 *cur = node->next();
1995 break;
1996 }
1997 }
1998
1999 return node;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002000}
2001
2002
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002003// Allocation on the old space free list. If it succeeds then a new linear
2004// allocation space has been set up with the top and limit of the space. If
2005// the allocation fails then NULL is returned, and the caller can perform a GC
2006// or allocate a new page before retrying.
2007HeapObject* FreeList::Allocate(int size_in_bytes) {
2008 ASSERT(0 < size_in_bytes);
2009 ASSERT(size_in_bytes <= kMaxBlockSize);
2010 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2011 // Don't free list allocate if there is linear space available.
2012 ASSERT(owner_->limit() - owner_->top() < size_in_bytes);
2013
2014 int new_node_size = 0;
2015 FreeListNode* new_node = FindNodeFor(size_in_bytes, &new_node_size);
2016 if (new_node == NULL) return NULL;
2017
2018 available_ -= new_node_size;
2019 ASSERT(IsVeryLong() || available_ == SumFreeLists());
2020
2021 int bytes_left = new_node_size - size_in_bytes;
2022 ASSERT(bytes_left >= 0);
2023
2024 int old_linear_size = static_cast<int>(owner_->limit() - owner_->top());
2025 // Mark the old linear allocation area with a free space map so it can be
2026 // skipped when scanning the heap. This also puts it back in the free list
2027 // if it is big enough.
2028 owner_->Free(owner_->top(), old_linear_size);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002029
2030#ifdef DEBUG
2031 for (int i = 0; i < size_in_bytes / kPointerSize; i++) {
2032 reinterpret_cast<Object**>(new_node->address())[i] = Smi::FromInt(0);
2033 }
2034#endif
2035
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002036 owner_->heap()->incremental_marking()->OldSpaceStep(
2037 size_in_bytes - old_linear_size);
2038
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +00002039 // The old-space-step might have finished sweeping and restarted marking.
2040 // Verify that it did not turn the page of the new node into an evacuation
2041 // candidate.
2042 ASSERT(!MarkCompactCollector::IsOnEvacuationCandidate(new_node));
2043
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002044 const int kThreshold = IncrementalMarking::kAllocatedThreshold;
2045
2046 // Memory in the linear allocation area is counted as allocated. We may free
2047 // a little of this again immediately - see below.
2048 owner_->Allocate(new_node_size);
2049
2050 if (bytes_left > kThreshold &&
2051 owner_->heap()->incremental_marking()->IsMarkingIncomplete() &&
2052 FLAG_incremental_marking_steps) {
2053 int linear_size = owner_->RoundSizeDownToObjectAlignment(kThreshold);
2054 // We don't want to give too large linear areas to the allocator while
2055 // incremental marking is going on, because we won't check again whether
2056 // we want to do another increment until the linear area is used up.
2057 owner_->Free(new_node->address() + size_in_bytes + linear_size,
2058 new_node_size - size_in_bytes - linear_size);
2059 owner_->SetTop(new_node->address() + size_in_bytes,
2060 new_node->address() + size_in_bytes + linear_size);
2061 } else if (bytes_left > 0) {
2062 // Normally we give the rest of the node to the allocator as its new
2063 // linear allocation area.
2064 owner_->SetTop(new_node->address() + size_in_bytes,
2065 new_node->address() + new_node_size);
2066 } else {
2067 // TODO(gc) Try not freeing linear allocation region when bytes_left
2068 // are zero.
2069 owner_->SetTop(NULL, NULL);
2070 }
2071
2072 return new_node;
2073}
2074
2075
2076static intptr_t CountFreeListItemsInList(FreeListNode* n, Page* p) {
2077 intptr_t sum = 0;
2078 while (n != NULL) {
2079 if (Page::FromAddress(n->address()) == p) {
2080 FreeSpace* free_space = reinterpret_cast<FreeSpace*>(n);
2081 sum += free_space->Size();
2082 }
2083 n = n->next();
2084 }
2085 return sum;
2086}
2087
2088
danno@chromium.org2c456792011-11-11 12:00:53 +00002089void FreeList::CountFreeListItems(Page* p, SizeStats* sizes) {
2090 sizes->huge_size_ = CountFreeListItemsInList(huge_list_, p);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002091 if (sizes->huge_size_ < p->area_size()) {
danno@chromium.org2c456792011-11-11 12:00:53 +00002092 sizes->small_size_ = CountFreeListItemsInList(small_list_, p);
2093 sizes->medium_size_ = CountFreeListItemsInList(medium_list_, p);
2094 sizes->large_size_ = CountFreeListItemsInList(large_list_, p);
2095 } else {
2096 sizes->small_size_ = 0;
2097 sizes->medium_size_ = 0;
2098 sizes->large_size_ = 0;
2099 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002100}
2101
danno@chromium.org2c456792011-11-11 12:00:53 +00002102
2103static intptr_t EvictFreeListItemsInList(FreeListNode** n, Page* p) {
2104 intptr_t sum = 0;
2105 while (*n != NULL) {
2106 if (Page::FromAddress((*n)->address()) == p) {
2107 FreeSpace* free_space = reinterpret_cast<FreeSpace*>(*n);
2108 sum += free_space->Size();
2109 *n = (*n)->next();
2110 } else {
2111 n = (*n)->next_address();
2112 }
2113 }
2114 return sum;
2115}
2116
2117
2118intptr_t FreeList::EvictFreeListItems(Page* p) {
2119 intptr_t sum = EvictFreeListItemsInList(&huge_list_, p);
2120
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002121 if (sum < p->area_size()) {
danno@chromium.org2c456792011-11-11 12:00:53 +00002122 sum += EvictFreeListItemsInList(&small_list_, p) +
2123 EvictFreeListItemsInList(&medium_list_, p) +
2124 EvictFreeListItemsInList(&large_list_, p);
2125 }
2126
2127 available_ -= static_cast<int>(sum);
2128
2129 return sum;
2130}
2131
2132
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002133#ifdef DEBUG
2134intptr_t FreeList::SumFreeList(FreeListNode* cur) {
2135 intptr_t sum = 0;
2136 while (cur != NULL) {
2137 ASSERT(cur->map() == HEAP->raw_unchecked_free_space_map());
2138 FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(cur);
2139 sum += cur_as_free_space->Size();
2140 cur = cur->next();
2141 }
2142 return sum;
2143}
2144
2145
2146static const int kVeryLongFreeList = 500;
2147
2148
2149int FreeList::FreeListLength(FreeListNode* cur) {
2150 int length = 0;
2151 while (cur != NULL) {
2152 length++;
2153 cur = cur->next();
2154 if (length == kVeryLongFreeList) return length;
2155 }
2156 return length;
2157}
2158
2159
2160bool FreeList::IsVeryLong() {
2161 if (FreeListLength(small_list_) == kVeryLongFreeList) return true;
2162 if (FreeListLength(medium_list_) == kVeryLongFreeList) return true;
2163 if (FreeListLength(large_list_) == kVeryLongFreeList) return true;
2164 if (FreeListLength(huge_list_) == kVeryLongFreeList) return true;
2165 return false;
2166}
2167
2168
2169// This can take a very long time because it is linear in the number of entries
2170// on the free list, so it should not be called if FreeListLength returns
2171// kVeryLongFreeList.
2172intptr_t FreeList::SumFreeLists() {
2173 intptr_t sum = SumFreeList(small_list_);
2174 sum += SumFreeList(medium_list_);
2175 sum += SumFreeList(large_list_);
2176 sum += SumFreeList(huge_list_);
2177 return sum;
2178}
2179#endif
2180
2181
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182// -----------------------------------------------------------------------------
2183// OldSpace implementation
2184
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002185bool NewSpace::ReserveSpace(int bytes) {
2186 // We can't reliably unpack a partial snapshot that needs more new space
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002187 // space than the minimum NewSpace size. The limit can be set lower than
2188 // the end of new space either because there is more space on the next page
2189 // or because we have lowered the limit in order to get periodic incremental
2190 // marking. The most reliable way to ensure that there is linear space is
2191 // to do the allocation, then rewind the limit.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002192 ASSERT(bytes <= InitialCapacity());
danno@chromium.orgc612e022011-11-10 11:38:15 +00002193 MaybeObject* maybe = AllocateRaw(bytes);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002194 Object* object = NULL;
2195 if (!maybe->ToObject(&object)) return false;
2196 HeapObject* allocation = HeapObject::cast(object);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002197 Address top = allocation_info_.top;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002198 if ((top - bytes) == allocation->address()) {
2199 allocation_info_.top = allocation->address();
2200 return true;
2201 }
2202 // There may be a borderline case here where the allocation succeeded, but
2203 // the limit and top have moved on to a new page. In that case we try again.
2204 return ReserveSpace(bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002205}
2206
2207
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002208void PagedSpace::PrepareForMarkCompact() {
2209 // We don't have a linear allocation area while sweeping. It will be restored
2210 // on the first allocation after the sweep.
2211 // Mark the old linear allocation area with a free space map so it can be
2212 // skipped when scanning the heap.
2213 int old_linear_size = static_cast<int>(limit() - top());
2214 Free(top(), old_linear_size);
2215 SetTop(NULL, NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002216
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002217 // Stop lazy sweeping and clear marking bits for unswept pages.
2218 if (first_unswept_page_ != NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002219 Page* p = first_unswept_page_;
2220 do {
2221 // Do not use ShouldBeSweptLazily predicate here.
2222 // New evacuation candidates were selected but they still have
2223 // to be swept before collection starts.
2224 if (!p->WasSwept()) {
2225 Bitmap::Clear(p);
2226 if (FLAG_gc_verbose) {
2227 PrintF("Sweeping 0x%" V8PRIxPTR " lazily abandoned.\n",
2228 reinterpret_cast<intptr_t>(p));
2229 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002230 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002231 p = p->next_page();
danno@chromium.org2c456792011-11-11 12:00:53 +00002232 } while (p != anchor());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002233 }
danno@chromium.org2c456792011-11-11 12:00:53 +00002234 first_unswept_page_ = Page::FromAddress(NULL);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002235 unswept_free_bytes_ = 0;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002236
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002237 // Clear the free list before a full GC---it will be rebuilt afterward.
2238 free_list_.Reset();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002239}
2240
2241
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002242bool PagedSpace::ReserveSpace(int size_in_bytes) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002243 ASSERT(size_in_bytes <= AreaSize());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002244 ASSERT(size_in_bytes == RoundSizeDownToObjectAlignment(size_in_bytes));
2245 Address current_top = allocation_info_.top;
2246 Address new_top = current_top + size_in_bytes;
2247 if (new_top <= allocation_info_.limit) return true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002248
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002249 HeapObject* new_area = free_list_.Allocate(size_in_bytes);
2250 if (new_area == NULL) new_area = SlowAllocateRaw(size_in_bytes);
2251 if (new_area == NULL) return false;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002252
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002253 int old_linear_size = static_cast<int>(limit() - top());
2254 // Mark the old linear allocation area with a free space so it can be
2255 // skipped when scanning the heap. This also puts it back in the free list
2256 // if it is big enough.
2257 Free(top(), old_linear_size);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002258
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002259 SetTop(new_area->address(), new_area->address() + size_in_bytes);
2260 Allocate(size_in_bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002261 return true;
2262}
2263
2264
2265// You have to call this last, since the implementation from PagedSpace
2266// doesn't know that memory was 'promised' to large object space.
2267bool LargeObjectSpace::ReserveSpace(int bytes) {
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00002268 return heap()->OldGenerationCapacityAvailable() >= bytes &&
2269 (!heap()->incremental_marking()->IsStopped() ||
2270 heap()->OldGenerationSpaceAvailable() >= bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002271}
2272
2273
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002274bool PagedSpace::AdvanceSweeper(intptr_t bytes_to_sweep) {
2275 if (IsSweepingComplete()) return true;
kasper.lund7276f142008-07-30 08:49:36 +00002276
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002277 intptr_t freed_bytes = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002278 Page* p = first_unswept_page_;
2279 do {
2280 Page* next_page = p->next_page();
2281 if (ShouldBeSweptLazily(p)) {
2282 if (FLAG_gc_verbose) {
2283 PrintF("Sweeping 0x%" V8PRIxPTR " lazily advanced.\n",
2284 reinterpret_cast<intptr_t>(p));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002285 }
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002286 DecreaseUnsweptFreeBytes(p);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002287 freed_bytes += MarkCompactCollector::SweepConservatively(this, p);
ager@chromium.org3811b432009-10-28 14:53:37 +00002288 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002289 p = next_page;
danno@chromium.org2c456792011-11-11 12:00:53 +00002290 } while (p != anchor() && freed_bytes < bytes_to_sweep);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002291
danno@chromium.org2c456792011-11-11 12:00:53 +00002292 if (p == anchor()) {
2293 first_unswept_page_ = Page::FromAddress(NULL);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002294 } else {
2295 first_unswept_page_ = p;
kasper.lund7276f142008-07-30 08:49:36 +00002296 }
2297
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002298 heap()->FreeQueuedChunks();
2299
2300 return IsSweepingComplete();
2301}
2302
2303
2304void PagedSpace::EvictEvacuationCandidatesFromFreeLists() {
2305 if (allocation_info_.top >= allocation_info_.limit) return;
2306
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002307 if (Page::FromAllocationTop(allocation_info_.top)->IsEvacuationCandidate()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002308 // Create filler object to keep page iterable if it was iterable.
2309 int remaining =
2310 static_cast<int>(allocation_info_.limit - allocation_info_.top);
2311 heap()->CreateFillerObjectAt(allocation_info_.top, remaining);
2312
2313 allocation_info_.top = NULL;
2314 allocation_info_.limit = NULL;
2315 }
2316}
2317
2318
2319HeapObject* PagedSpace::SlowAllocateRaw(int size_in_bytes) {
2320 // Allocation in this space has failed.
2321
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002322 // If there are unswept pages advance lazy sweeper then sweep one page before
2323 // allocating a new page.
2324 if (first_unswept_page_->is_valid()) {
2325 AdvanceSweeper(size_in_bytes);
2326
2327 // Retry the free list allocation.
2328 HeapObject* object = free_list_.Allocate(size_in_bytes);
2329 if (object != NULL) return object;
2330 }
2331
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002332 // Free list allocation failed and there is no next page. Fail if we have
2333 // hit the old generation size limit that should cause a garbage
2334 // collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002335 if (!heap()->always_allocate() &&
2336 heap()->OldGenerationAllocationLimitReached()) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002337 return NULL;
2338 }
2339
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002340 // Try to expand the space and allocate in the new next page.
2341 if (Expand()) {
2342 return free_list_.Allocate(size_in_bytes);
2343 }
2344
2345 // Last ditch, sweep all the remaining pages to try to find space. This may
2346 // cause a pause.
2347 if (!IsSweepingComplete()) {
2348 AdvanceSweeper(kMaxInt);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002349
2350 // Retry the free list allocation.
2351 HeapObject* object = free_list_.Allocate(size_in_bytes);
2352 if (object != NULL) return object;
kasper.lund7276f142008-07-30 08:49:36 +00002353 }
2354
2355 // Finally, fail.
2356 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357}
2358
2359
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361void PagedSpace::ReportCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002362 Isolate* isolate = Isolate::Current();
2363 CommentStatistic* comments_statistics =
2364 isolate->paged_space_comments_statistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365 ReportCodeKindStatistics();
2366 PrintF("Code comment statistics (\" [ comment-txt : size/ "
2367 "count (average)\"):\n");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002368 for (int i = 0; i <= CommentStatistic::kMaxComments; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 const CommentStatistic& cs = comments_statistics[i];
2370 if (cs.size > 0) {
2371 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
2372 cs.size/cs.count);
2373 }
2374 }
2375 PrintF("\n");
2376}
2377
2378
2379void PagedSpace::ResetCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002380 Isolate* isolate = Isolate::Current();
2381 CommentStatistic* comments_statistics =
2382 isolate->paged_space_comments_statistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 ClearCodeKindStatistics();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002384 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
2385 comments_statistics[i].Clear();
2386 }
2387 comments_statistics[CommentStatistic::kMaxComments].comment = "Unknown";
2388 comments_statistics[CommentStatistic::kMaxComments].size = 0;
2389 comments_statistics[CommentStatistic::kMaxComments].count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390}
2391
2392
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002393// Adds comment to 'comment_statistics' table. Performance OK as long as
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394// 'kMaxComments' is small
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002395static void EnterComment(Isolate* isolate, const char* comment, int delta) {
2396 CommentStatistic* comments_statistics =
2397 isolate->paged_space_comments_statistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398 // Do not count empty comments
2399 if (delta <= 0) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002400 CommentStatistic* cs = &comments_statistics[CommentStatistic::kMaxComments];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 // Search for a free or matching entry in 'comments_statistics': 'cs'
2402 // points to result.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002403 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404 if (comments_statistics[i].comment == NULL) {
2405 cs = &comments_statistics[i];
2406 cs->comment = comment;
2407 break;
2408 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2409 cs = &comments_statistics[i];
2410 break;
2411 }
2412 }
2413 // Update entry for 'comment'
2414 cs->size += delta;
2415 cs->count += 1;
2416}
2417
2418
2419// Call for each nested comment start (start marked with '[ xxx', end marked
2420// with ']'. RelocIterator 'it' must point to a comment reloc info.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002421static void CollectCommentStatistics(Isolate* isolate, RelocIterator* it) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002423 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2425 if (tmp[0] != '[') {
2426 // Not a nested comment; skip
2427 return;
2428 }
2429
2430 // Search for end of nested comment or a new nested comment
2431 const char* const comment_txt =
2432 reinterpret_cast<const char*>(it->rinfo()->data());
2433 const byte* prev_pc = it->rinfo()->pc();
2434 int flat_delta = 0;
2435 it->next();
2436 while (true) {
2437 // All nested comments must be terminated properly, and therefore exit
2438 // from loop.
2439 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002440 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 const char* const txt =
2442 reinterpret_cast<const char*>(it->rinfo()->data());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002443 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 if (txt[0] == ']') break; // End of nested comment
2445 // A new comment
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002446 CollectCommentStatistics(isolate, it);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447 // Skip code that was covered with previous comment
2448 prev_pc = it->rinfo()->pc();
2449 }
2450 it->next();
2451 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002452 EnterComment(isolate, comment_txt, flat_delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002453}
2454
2455
2456// Collects code size statistics:
2457// - by code kind
2458// - by code comment
2459void PagedSpace::CollectCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002460 Isolate* isolate = heap()->isolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 HeapObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002462 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463 if (obj->IsCode()) {
2464 Code* code = Code::cast(obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002465 isolate->code_kind_statistics()[code->kind()] += code->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466 RelocIterator it(code);
2467 int delta = 0;
2468 const byte* prev_pc = code->instruction_start();
2469 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002470 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002471 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002472 CollectCommentStatistics(isolate, &it);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 prev_pc = it.rinfo()->pc();
2474 }
2475 it.next();
2476 }
2477
2478 ASSERT(code->instruction_start() <= prev_pc &&
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002479 prev_pc <= code->instruction_end());
2480 delta += static_cast<int>(code->instruction_end() - prev_pc);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002481 EnterComment(isolate, "NoComment", delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482 }
2483 }
2484}
2485
2486
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002487void PagedSpace::ReportStatistics() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002488 int pct = static_cast<int>(Available() * 100 / Capacity());
2489 PrintF(" capacity: %" V8_PTR_PREFIX "d"
2490 ", waste: %" V8_PTR_PREFIX "d"
2491 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 Capacity(), Waste(), Available(), pct);
2493
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002494 if (was_swept_conservatively_) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 ClearHistograms();
2496 HeapObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002497 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next())
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002498 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 ReportHistogram(true);
2500}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501#endif
2502
2503// -----------------------------------------------------------------------------
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002504// FixedSpace implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002506void FixedSpace::PrepareForMarkCompact() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002507 // Call prepare of the super class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002508 PagedSpace::PrepareForMarkCompact();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002509
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002510 // During a non-compacting collection, everything below the linear
2511 // allocation pointer except wasted top-of-page blocks is considered
2512 // allocated and we will rediscover available bytes during the
2513 // collection.
2514 accounting_stats_.AllocateBytes(free_list_.available());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515
kasper.lund7276f142008-07-30 08:49:36 +00002516 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518}
2519
2520
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002521// -----------------------------------------------------------------------------
2522// MapSpace implementation
2523
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002524#ifdef DEBUG
2525void MapSpace::VerifyObject(HeapObject* object) {
2526 // The object should be a map or a free-list node.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002527 ASSERT(object->IsMap() || object->IsFreeSpace());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002528}
2529#endif
2530
2531
2532// -----------------------------------------------------------------------------
2533// GlobalPropertyCellSpace implementation
2534
2535#ifdef DEBUG
2536void CellSpace::VerifyObject(HeapObject* object) {
2537 // The object should be a global object property cell or a free-list node.
2538 ASSERT(object->IsJSGlobalPropertyCell() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002539 object->map() == heap()->two_pointer_filler_map());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002540}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541#endif
2542
2543
2544// -----------------------------------------------------------------------------
2545// LargeObjectIterator
2546
2547LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002548 current_ = space->first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549 size_func_ = NULL;
2550}
2551
2552
2553LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2554 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002555 current_ = space->first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 size_func_ = size_func;
2557}
2558
2559
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002560HeapObject* LargeObjectIterator::Next() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002561 if (current_ == NULL) return NULL;
2562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 HeapObject* object = current_->GetObject();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002564 current_ = current_->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 return object;
2566}
2567
2568
2569// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570// LargeObjectSpace
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002571static bool ComparePointers(void* key1, void* key2) {
2572 return key1 == key2;
2573}
2574
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002576LargeObjectSpace::LargeObjectSpace(Heap* heap,
2577 intptr_t max_capacity,
2578 AllocationSpace id)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002579 : Space(heap, id, NOT_EXECUTABLE), // Managed on a per-allocation basis
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002580 max_capacity_(max_capacity),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002581 first_page_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 size_(0),
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002583 page_count_(0),
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002584 objects_size_(0),
2585 chunk_map_(ComparePointers, 1024) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586
2587
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002588bool LargeObjectSpace::SetUp() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002589 first_page_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 size_ = 0;
2591 page_count_ = 0;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002592 objects_size_ = 0;
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002593 chunk_map_.Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594 return true;
2595}
2596
2597
2598void LargeObjectSpace::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002599 while (first_page_ != NULL) {
2600 LargePage* page = first_page_;
2601 first_page_ = first_page_->next_page();
2602 LOG(heap()->isolate(), DeleteEvent("LargeObjectChunk", page->address()));
2603
2604 ObjectSpace space = static_cast<ObjectSpace>(1 << identity());
2605 heap()->isolate()->memory_allocator()->PerformAllocationCallback(
2606 space, kAllocationActionFree, page->size());
2607 heap()->isolate()->memory_allocator()->Free(page);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002609 SetUp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610}
2611
2612
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002613MaybeObject* LargeObjectSpace::AllocateRaw(int object_size,
2614 Executability executable) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002615 // Check if we want to force a GC before growing the old space further.
2616 // If so, fail the allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002617 if (!heap()->always_allocate() &&
2618 heap()->OldGenerationAllocationLimitReached()) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002619 return Failure::RetryAfterGC(identity());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002620 }
2621
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002622 if (Size() + object_size > max_capacity_) {
2623 return Failure::RetryAfterGC(identity());
2624 }
2625
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002626 LargePage* page = heap()->isolate()->memory_allocator()->
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002627 AllocateLargePage(object_size, this, executable);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002628 if (page == NULL) return Failure::RetryAfterGC(identity());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002629 ASSERT(page->area_size() >= object_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002631 size_ += static_cast<int>(page->size());
2632 objects_size_ += object_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 page_count_++;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002634 page->set_next_page(first_page_);
2635 first_page_ = page;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002637 // Register all MemoryChunk::kAlignment-aligned chunks covered by
2638 // this large page in the chunk map.
2639 uintptr_t base = reinterpret_cast<uintptr_t>(page) / MemoryChunk::kAlignment;
2640 uintptr_t limit = base + (page->size() - 1) / MemoryChunk::kAlignment;
2641 for (uintptr_t key = base; key <= limit; key++) {
2642 HashMap::Entry* entry = chunk_map_.Lookup(reinterpret_cast<void*>(key),
2643 static_cast<uint32_t>(key),
2644 true);
2645 ASSERT(entry != NULL);
2646 entry->value = page;
2647 }
2648
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002649 HeapObject* object = page->GetObject();
2650
2651#ifdef DEBUG
2652 // Make the object consistent so the heap can be vefified in OldSpaceStep.
2653 reinterpret_cast<Object**>(object->address())[0] =
2654 heap()->fixed_array_map();
2655 reinterpret_cast<Object**>(object->address())[1] = Smi::FromInt(0);
2656#endif
2657
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002658 heap()->incremental_marking()->OldSpaceStep(object_size);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002659 return object;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660}
2661
2662
2663// GC support
lrn@chromium.org303ada72010-10-27 09:33:13 +00002664MaybeObject* LargeObjectSpace::FindObject(Address a) {
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002665 LargePage* page = FindPage(a);
2666 if (page != NULL) {
2667 return page->GetObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 }
2669 return Failure::Exception();
2670}
2671
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002672
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002673LargePage* LargeObjectSpace::FindPage(Address a) {
2674 uintptr_t key = reinterpret_cast<uintptr_t>(a) / MemoryChunk::kAlignment;
2675 HashMap::Entry* e = chunk_map_.Lookup(reinterpret_cast<void*>(key),
2676 static_cast<uint32_t>(key),
2677 false);
2678 if (e != NULL) {
2679 ASSERT(e->value != NULL);
2680 LargePage* page = reinterpret_cast<LargePage*>(e->value);
2681 ASSERT(page->is_valid());
2682 if (page->Contains(a)) {
2683 return page;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002684 }
2685 }
2686 return NULL;
2687}
2688
2689
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690void LargeObjectSpace::FreeUnmarkedObjects() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002691 LargePage* previous = NULL;
2692 LargePage* current = first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 while (current != NULL) {
2694 HeapObject* object = current->GetObject();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002695 // Can this large page contain pointers to non-trivial objects. No other
2696 // pointer object is this big.
2697 bool is_pointer_object = object->IsFixedArray();
2698 MarkBit mark_bit = Marking::MarkBitFrom(object);
2699 if (mark_bit.Get()) {
2700 mark_bit.Clear();
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002701 MemoryChunk::IncrementLiveBytesFromGC(object->address(), -object->Size());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 previous = current;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002703 current = current->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002705 LargePage* page = current;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 // Cut the chunk out from the chunk list.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002707 current = current->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 if (previous == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002709 first_page_ = current;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002711 previous->set_next_page(current);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002712 }
2713
2714 // Free the chunk.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002715 heap()->mark_compact_collector()->ReportDeleteIfNeeded(
2716 object, heap()->isolate());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002717 size_ -= static_cast<int>(page->size());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002718 objects_size_ -= object->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719 page_count_--;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002720
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002721 // Remove entries belonging to this page.
2722 // Use variable alignment to help pass length check (<= 80 characters)
2723 // of single line in tools/presubmit.py.
2724 const intptr_t alignment = MemoryChunk::kAlignment;
2725 uintptr_t base = reinterpret_cast<uintptr_t>(page)/alignment;
2726 uintptr_t limit = base + (page->size()-1)/alignment;
2727 for (uintptr_t key = base; key <= limit; key++) {
2728 chunk_map_.Remove(reinterpret_cast<void*>(key),
2729 static_cast<uint32_t>(key));
2730 }
2731
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002732 if (is_pointer_object) {
2733 heap()->QueueMemoryChunkForFree(page);
2734 } else {
2735 heap()->isolate()->memory_allocator()->Free(page);
2736 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737 }
2738 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002739 heap()->FreeQueuedChunks();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740}
2741
2742
2743bool LargeObjectSpace::Contains(HeapObject* object) {
2744 Address address = object->address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002745 MemoryChunk* chunk = MemoryChunk::FromAddress(address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002747 bool owned = (chunk->owner() == this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002749 SLOW_ASSERT(!owned || !FindObject(address)->IsFailure());
2750
2751 return owned;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752}
2753
2754
2755#ifdef DEBUG
2756// We do not assume that the large object iterator works, because it depends
2757// on the invariants we are checking during verification.
2758void LargeObjectSpace::Verify() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002759 for (LargePage* chunk = first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 chunk != NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002761 chunk = chunk->next_page()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 // Each chunk contains an object that starts at the large object page's
2763 // object area start.
2764 HeapObject* object = chunk->GetObject();
2765 Page* page = Page::FromAddress(object->address());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002766 ASSERT(object->address() == page->area_start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767
2768 // The first word should be a map, and we expect all map pointers to be
2769 // in map space.
2770 Map* map = object->map();
2771 ASSERT(map->IsMap());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002772 ASSERT(heap()->map_space()->Contains(map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002773
ager@chromium.orga1645e22009-09-09 19:27:10 +00002774 // We have only code, sequential strings, external strings
2775 // (sequential strings that have been morphed into external
2776 // strings), fixed arrays, and byte arrays in large object space.
2777 ASSERT(object->IsCode() || object->IsSeqString() ||
2778 object->IsExternalString() || object->IsFixedArray() ||
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002779 object->IsFixedDoubleArray() || object->IsByteArray());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780
2781 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002782 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783
2784 // Byte arrays and strings don't have interior pointers.
2785 if (object->IsCode()) {
2786 VerifyPointersVisitor code_visitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 object->IterateBody(map->instance_type(),
2788 object->Size(),
2789 &code_visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 } else if (object->IsFixedArray()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791 FixedArray* array = FixedArray::cast(object);
2792 for (int j = 0; j < array->length(); j++) {
2793 Object* element = array->get(j);
2794 if (element->IsHeapObject()) {
2795 HeapObject* element_object = HeapObject::cast(element);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002796 ASSERT(heap()->Contains(element_object));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797 ASSERT(element_object->map()->IsMap());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 }
2799 }
2800 }
2801 }
2802}
2803
2804
2805void LargeObjectSpace::Print() {
2806 LargeObjectIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002807 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002808 obj->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809 }
2810}
2811
2812
2813void LargeObjectSpace::ReportStatistics() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002814 PrintF(" size: %" V8_PTR_PREFIX "d\n", size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815 int num_objects = 0;
2816 ClearHistograms();
2817 LargeObjectIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002818 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819 num_objects++;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002820 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821 }
2822
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002823 PrintF(" number of objects %d, "
2824 "size of objects %" V8_PTR_PREFIX "d\n", num_objects, objects_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 if (num_objects > 0) ReportHistogram(false);
2826}
2827
2828
2829void LargeObjectSpace::CollectCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002830 Isolate* isolate = heap()->isolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 LargeObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002832 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 if (obj->IsCode()) {
2834 Code* code = Code::cast(obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002835 isolate->code_kind_statistics()[code->kind()] += code->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002836 }
2837 }
2838}
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002839
2840
2841void Page::Print() {
2842 // Make a best-effort to print the objects in the page.
2843 PrintF("Page@%p in %s\n",
2844 this->address(),
2845 AllocationSpaceName(this->owner()->identity()));
2846 printf(" --------------------------------------\n");
2847 HeapObjectIterator objects(this, heap()->GcSafeSizeOfOldObjectFunction());
2848 unsigned mark_size = 0;
2849 for (HeapObject* object = objects.Next();
2850 object != NULL;
2851 object = objects.Next()) {
2852 bool is_marked = Marking::MarkBitFrom(object).Get();
2853 PrintF(" %c ", (is_marked ? '!' : ' ')); // Indent a little.
2854 if (is_marked) {
2855 mark_size += heap()->GcSafeSizeOfOldObjectFunction()(object);
2856 }
2857 object->ShortPrint();
2858 PrintF("\n");
2859 }
2860 printf(" --------------------------------------\n");
2861 printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes());
2862}
2863
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002864#endif // DEBUG
2865
2866} } // namespace v8::internal