blob: 3adb2e3553ec77e19b4ee7e992ff215ad3be9608 [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
30#include "macro-assembler.h"
31#include "mark-compact.h"
32#include "platform.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038// ----------------------------------------------------------------------------
39// HeapObjectIterator
40
41HeapObjectIterator::HeapObjectIterator(PagedSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000042 // You can't actually iterate over the anchor page. It is not a real page,
43 // just an anchor for the double linked page list. Initialize as if we have
44 // reached the end of the anchor page, then the first iteration will move on
45 // to the first page.
46 Initialize(space,
47 NULL,
48 NULL,
49 kAllPagesInSpace,
50 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051}
52
53
54HeapObjectIterator::HeapObjectIterator(PagedSpace* space,
55 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000056 // You can't actually iterate over the anchor page. It is not a real page,
57 // just an anchor for the double linked page list. Initialize the current
58 // address and end as NULL, then the first iteration will move on
59 // to the first page.
60 Initialize(space,
61 NULL,
62 NULL,
63 kAllPagesInSpace,
64 size_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065}
66
67
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000068HeapObjectIterator::HeapObjectIterator(Page* page,
69 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000070 Space* owner = page->owner();
hpayer@chromium.org8432c912013-02-28 15:55:26 +000071 ASSERT(owner == page->heap()->old_pointer_space() ||
72 owner == page->heap()->old_data_space() ||
73 owner == page->heap()->map_space() ||
74 owner == page->heap()->cell_space() ||
75 owner == page->heap()->code_space());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000076 Initialize(reinterpret_cast<PagedSpace*>(owner),
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000077 page->area_start(),
78 page->area_end(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000079 kOnePageOnly,
80 size_func);
81 ASSERT(page->WasSweptPrecisely());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000082}
83
84
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000085void HeapObjectIterator::Initialize(PagedSpace* space,
86 Address cur, Address end,
87 HeapObjectIterator::PageMode mode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 HeapObjectCallback size_f) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000089 // Check that we actually can iterate this space.
90 ASSERT(!space->was_swept_conservatively());
91
92 space_ = space;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 cur_addr_ = cur;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000094 cur_end_ = end;
95 page_mode_ = mode;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 size_func_ = size_f;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097}
98
99
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000100// We have hit the end of the page and should advance to the next block of
101// objects. This happens at the end of the page.
102bool HeapObjectIterator::AdvanceToNextPage() {
103 ASSERT(cur_addr_ == cur_end_);
104 if (page_mode_ == kOnePageOnly) return false;
105 Page* cur_page;
106 if (cur_addr_ == NULL) {
107 cur_page = space_->anchor();
108 } else {
109 cur_page = Page::FromAddress(cur_addr_ - 1);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000110 ASSERT(cur_addr_ == cur_page->area_end());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000111 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 cur_page = cur_page->next_page();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000113 if (cur_page == space_->anchor()) return false;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000114 cur_addr_ = cur_page->area_start();
115 cur_end_ = cur_page->area_end();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000116 ASSERT(cur_page->WasSweptPrecisely());
117 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118}
119
120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121// -----------------------------------------------------------------------------
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000122// CodeRange
123
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000124
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000125CodeRange::CodeRange(Isolate* isolate)
126 : isolate_(isolate),
127 code_range_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 free_list_(0),
129 allocation_list_(0),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000130 current_allocation_block_index_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000131}
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000132
133
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000134bool CodeRange::SetUp(const size_t requested) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000135 ASSERT(code_range_ == NULL);
136
137 code_range_ = new VirtualMemory(requested);
138 CHECK(code_range_ != NULL);
139 if (!code_range_->IsReserved()) {
140 delete code_range_;
141 code_range_ = NULL;
142 return false;
143 }
144
145 // We are sure that we have mapped a block of requested addresses.
146 ASSERT(code_range_->size() == requested);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000147 LOG(isolate_, NewEvent("CodeRange", code_range_->address(), requested));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000148 Address base = reinterpret_cast<Address>(code_range_->address());
149 Address aligned_base =
150 RoundUp(reinterpret_cast<Address>(code_range_->address()),
151 MemoryChunk::kAlignment);
152 size_t size = code_range_->size() - (aligned_base - base);
153 allocation_list_.Add(FreeBlock(aligned_base, size));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000154 current_allocation_block_index_ = 0;
155 return true;
156}
157
158
159int CodeRange::CompareFreeBlockAddress(const FreeBlock* left,
160 const FreeBlock* right) {
161 // The entire point of CodeRange is that the difference between two
162 // addresses in the range can be represented as a signed 32-bit int,
163 // so the cast is semantically correct.
164 return static_cast<int>(left->start - right->start);
165}
166
167
168void CodeRange::GetNextAllocationBlock(size_t requested) {
169 for (current_allocation_block_index_++;
170 current_allocation_block_index_ < allocation_list_.length();
171 current_allocation_block_index_++) {
172 if (requested <= allocation_list_[current_allocation_block_index_].size) {
173 return; // Found a large enough allocation block.
174 }
175 }
176
177 // Sort and merge the free blocks on the free list and the allocation list.
178 free_list_.AddAll(allocation_list_);
179 allocation_list_.Clear();
180 free_list_.Sort(&CompareFreeBlockAddress);
181 for (int i = 0; i < free_list_.length();) {
182 FreeBlock merged = free_list_[i];
183 i++;
184 // Add adjacent free blocks to the current merged block.
185 while (i < free_list_.length() &&
186 free_list_[i].start == merged.start + merged.size) {
187 merged.size += free_list_[i].size;
188 i++;
189 }
190 if (merged.size > 0) {
191 allocation_list_.Add(merged);
192 }
193 }
194 free_list_.Clear();
195
196 for (current_allocation_block_index_ = 0;
197 current_allocation_block_index_ < allocation_list_.length();
198 current_allocation_block_index_++) {
199 if (requested <= allocation_list_[current_allocation_block_index_].size) {
200 return; // Found a large enough allocation block.
201 }
202 }
203
204 // Code range is full or too fragmented.
205 V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock");
206}
207
208
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000209Address CodeRange::AllocateRawMemory(const size_t requested_size,
210 const size_t commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000211 size_t* allocated) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000212 ASSERT(commit_size <= requested_size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000213 ASSERT(current_allocation_block_index_ < allocation_list_.length());
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000214 if (requested_size > allocation_list_[current_allocation_block_index_].size) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000215 // Find an allocation block large enough. This function call may
216 // call V8::FatalProcessOutOfMemory if it cannot find a large enough block.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000217 GetNextAllocationBlock(requested_size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000218 }
219 // Commit the requested memory at the start of the current allocation block.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000220 size_t aligned_requested = RoundUp(requested_size, 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));
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000230 if (!MemoryAllocator::CommitExecutableMemory(code_range_,
231 current.start,
232 commit_size,
233 *allocated)) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000234 *allocated = 0;
235 return NULL;
236 }
237 allocation_list_[current_allocation_block_index_].start += *allocated;
238 allocation_list_[current_allocation_block_index_].size -= *allocated;
239 if (*allocated == current.size) {
240 GetNextAllocationBlock(0); // This block is used up, get the next one.
241 }
242 return current.start;
243}
244
245
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000246bool CodeRange::CommitRawMemory(Address start, size_t length) {
247 return code_range_->Commit(start, length, true);
248}
249
250
251bool CodeRange::UncommitRawMemory(Address start, size_t length) {
252 return code_range_->Uncommit(start, length);
253}
254
255
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000256void CodeRange::FreeRawMemory(Address address, size_t length) {
257 ASSERT(IsAddressAligned(address, MemoryChunk::kAlignment));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000258 free_list_.Add(FreeBlock(address, length));
259 code_range_->Uncommit(address, length);
260}
261
262
263void CodeRange::TearDown() {
264 delete code_range_; // Frees all memory in the virtual memory range.
265 code_range_ = NULL;
266 free_list_.Free();
267 allocation_list_.Free();
268}
269
270
271// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272// MemoryAllocator
273//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000275MemoryAllocator::MemoryAllocator(Isolate* isolate)
276 : isolate_(isolate),
277 capacity_(0),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278 capacity_executable_(0),
279 size_(0),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000280 size_executable_(0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281}
282
283
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000284bool MemoryAllocator::SetUp(intptr_t capacity, intptr_t capacity_executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 capacity_ = RoundUp(capacity, Page::kPageSize);
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000286 capacity_executable_ = RoundUp(capacity_executable, Page::kPageSize);
287 ASSERT_GE(capacity_, capacity_executable_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 size_ = 0;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000290 size_executable_ = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000291
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 return true;
293}
294
295
296void MemoryAllocator::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000297 // Check that spaces were torn down before MemoryAllocator.
298 ASSERT(size_ == 0);
299 // TODO(gc) this will be true again when we fix FreeMemory.
300 // ASSERT(size_executable_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 capacity_ = 0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000302 capacity_executable_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303}
304
305
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000306void MemoryAllocator::FreeMemory(VirtualMemory* reservation,
307 Executability executable) {
308 // TODO(gc) make code_range part of memory allocator?
309 ASSERT(reservation->IsReserved());
310 size_t size = reservation->size();
311 ASSERT(size_ >= size);
312 size_ -= size;
313
314 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
315
316 if (executable == EXECUTABLE) {
317 ASSERT(size_executable_ >= size);
318 size_executable_ -= size;
319 }
320 // Code which is part of the code-range does not have its own VirtualMemory.
321 ASSERT(!isolate_->code_range()->contains(
322 static_cast<Address>(reservation->address())));
323 ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists());
324 reservation->Release();
325}
326
327
328void MemoryAllocator::FreeMemory(Address base,
329 size_t size,
330 Executability executable) {
331 // TODO(gc) make code_range part of memory allocator?
332 ASSERT(size_ >= size);
333 size_ -= size;
334
335 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
336
337 if (executable == EXECUTABLE) {
338 ASSERT(size_executable_ >= size);
339 size_executable_ -= size;
340 }
341 if (isolate_->code_range()->contains(static_cast<Address>(base))) {
342 ASSERT(executable == EXECUTABLE);
343 isolate_->code_range()->FreeRawMemory(base, size);
344 } else {
345 ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists());
346 bool result = VirtualMemory::ReleaseRegion(base, size);
347 USE(result);
348 ASSERT(result);
349 }
350}
351
352
353Address MemoryAllocator::ReserveAlignedMemory(size_t size,
354 size_t alignment,
355 VirtualMemory* controller) {
356 VirtualMemory reservation(size, alignment);
357
358 if (!reservation.IsReserved()) return NULL;
359 size_ += reservation.size();
360 Address base = RoundUp(static_cast<Address>(reservation.address()),
361 alignment);
362 controller->TakeControl(&reservation);
363 return base;
364}
365
366
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000367Address MemoryAllocator::AllocateAlignedMemory(size_t reserve_size,
368 size_t commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000369 size_t alignment,
370 Executability executable,
371 VirtualMemory* controller) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000372 ASSERT(commit_size <= reserve_size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000373 VirtualMemory reservation;
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000374 Address base = ReserveAlignedMemory(reserve_size, alignment, &reservation);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000375 if (base == NULL) return NULL;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000376
377 if (executable == EXECUTABLE) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000378 if (!CommitExecutableMemory(&reservation,
379 base,
380 commit_size,
381 reserve_size)) {
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000382 base = NULL;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000383 }
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000384 } else {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000385 if (!reservation.Commit(base, commit_size, false)) {
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000386 base = NULL;
387 }
388 }
389
390 if (base == NULL) {
391 // Failed to commit the body. Release the mapping and any partially
392 // commited regions inside it.
393 reservation.Release();
394 return NULL;
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000395 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000396
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000397 controller->TakeControl(&reservation);
398 return base;
399}
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000400
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000401
402void Page::InitializeAsAnchor(PagedSpace* owner) {
403 set_owner(owner);
404 set_prev_page(this);
405 set_next_page(this);
406}
407
408
409NewSpacePage* NewSpacePage::Initialize(Heap* heap,
410 Address start,
411 SemiSpace* semi_space) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000412 Address area_start = start + NewSpacePage::kObjectStartOffset;
413 Address area_end = start + Page::kPageSize;
414
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000415 MemoryChunk* chunk = MemoryChunk::Initialize(heap,
416 start,
417 Page::kPageSize,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000418 area_start,
419 area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000420 NOT_EXECUTABLE,
421 semi_space);
422 chunk->set_next_chunk(NULL);
423 chunk->set_prev_chunk(NULL);
424 chunk->initialize_scan_on_scavenge(true);
425 bool in_to_space = (semi_space->id() != kFromSpace);
426 chunk->SetFlag(in_to_space ? MemoryChunk::IN_TO_SPACE
427 : MemoryChunk::IN_FROM_SPACE);
428 ASSERT(!chunk->IsFlagSet(in_to_space ? MemoryChunk::IN_FROM_SPACE
429 : MemoryChunk::IN_TO_SPACE));
430 NewSpacePage* page = static_cast<NewSpacePage*>(chunk);
431 heap->incremental_marking()->SetNewSpacePageFlags(page);
432 return page;
433}
434
435
436void NewSpacePage::InitializeAsAnchor(SemiSpace* semi_space) {
437 set_owner(semi_space);
438 set_next_chunk(this);
439 set_prev_chunk(this);
440 // Flags marks this invalid page as not being in new-space.
441 // All real new-space pages will be in new-space.
442 SetFlags(0, ~0);
443}
444
445
446MemoryChunk* MemoryChunk::Initialize(Heap* heap,
447 Address base,
448 size_t size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000449 Address area_start,
450 Address area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000451 Executability executable,
452 Space* owner) {
453 MemoryChunk* chunk = FromAddress(base);
454
455 ASSERT(base == chunk->address());
456
457 chunk->heap_ = heap;
458 chunk->size_ = size;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000459 chunk->area_start_ = area_start;
460 chunk->area_end_ = area_end;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000461 chunk->flags_ = 0;
462 chunk->set_owner(owner);
463 chunk->InitializeReservedMemory();
464 chunk->slots_buffer_ = NULL;
465 chunk->skip_list_ = NULL;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000466 chunk->write_barrier_counter_ = kWriteBarrierCounterGranularity;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000467 chunk->progress_bar_ = 0;
danno@chromium.org72204d52012-10-31 10:02:10 +0000468 chunk->high_water_mark_ = static_cast<int>(area_start - base);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000469 chunk->parallel_sweeping_ = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000470 chunk->ResetLiveBytes();
471 Bitmap::Clear(chunk);
472 chunk->initialize_scan_on_scavenge(false);
473 chunk->SetFlag(WAS_SWEPT_PRECISELY);
474
475 ASSERT(OFFSET_OF(MemoryChunk, flags_) == kFlagsOffset);
476 ASSERT(OFFSET_OF(MemoryChunk, live_byte_count_) == kLiveBytesOffset);
477
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000478 if (executable == EXECUTABLE) {
479 chunk->SetFlag(IS_EXECUTABLE);
480 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000481
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000482 if (owner == heap->old_data_space()) {
483 chunk->SetFlag(CONTAINS_ONLY_DATA);
484 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000485
486 return chunk;
487}
488
489
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000490// Commit MemoryChunk area to the requested size.
491bool MemoryChunk::CommitArea(size_t requested) {
492 size_t guard_size = IsFlagSet(IS_EXECUTABLE) ?
493 MemoryAllocator::CodePageGuardSize() : 0;
494 size_t header_size = area_start() - address() - guard_size;
495 size_t commit_size = RoundUp(header_size + requested, OS::CommitPageSize());
496 size_t committed_size = RoundUp(header_size + (area_end() - area_start()),
497 OS::CommitPageSize());
498
499 if (commit_size > committed_size) {
500 // Commit size should be less or equal than the reserved size.
501 ASSERT(commit_size <= size() - 2 * guard_size);
502 // Append the committed area.
503 Address start = address() + committed_size + guard_size;
504 size_t length = commit_size - committed_size;
505 if (reservation_.IsReserved()) {
506 if (!reservation_.Commit(start, length, IsFlagSet(IS_EXECUTABLE))) {
507 return false;
508 }
509 } else {
510 CodeRange* code_range = heap_->isolate()->code_range();
511 ASSERT(code_range->exists() && IsFlagSet(IS_EXECUTABLE));
512 if (!code_range->CommitRawMemory(start, length)) return false;
513 }
514
515 if (Heap::ShouldZapGarbage()) {
516 heap_->isolate()->memory_allocator()->ZapBlock(start, length);
517 }
518 } else if (commit_size < committed_size) {
519 ASSERT(commit_size > 0);
520 // Shrink the committed area.
521 size_t length = committed_size - commit_size;
522 Address start = address() + committed_size + guard_size - length;
523 if (reservation_.IsReserved()) {
524 if (!reservation_.Uncommit(start, length)) return false;
525 } else {
526 CodeRange* code_range = heap_->isolate()->code_range();
527 ASSERT(code_range->exists() && IsFlagSet(IS_EXECUTABLE));
528 if (!code_range->UncommitRawMemory(start, length)) return false;
529 }
530 }
531
532 area_end_ = area_start_ + requested;
533 return true;
534}
535
536
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000537void MemoryChunk::InsertAfter(MemoryChunk* other) {
538 next_chunk_ = other->next_chunk_;
539 prev_chunk_ = other;
540 other->next_chunk_->prev_chunk_ = this;
541 other->next_chunk_ = this;
542}
543
544
545void MemoryChunk::Unlink() {
546 if (!InNewSpace() && IsFlagSet(SCAN_ON_SCAVENGE)) {
547 heap_->decrement_scan_on_scavenge_pages();
548 ClearFlag(SCAN_ON_SCAVENGE);
549 }
550 next_chunk_->prev_chunk_ = prev_chunk_;
551 prev_chunk_->next_chunk_ = next_chunk_;
552 prev_chunk_ = NULL;
553 next_chunk_ = NULL;
554}
555
556
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000557MemoryChunk* MemoryAllocator::AllocateChunk(intptr_t reserve_area_size,
558 intptr_t commit_area_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000559 Executability executable,
560 Space* owner) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000561 ASSERT(commit_area_size <= reserve_area_size);
562
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000563 size_t chunk_size;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000564 Heap* heap = isolate_->heap();
565 Address base = NULL;
566 VirtualMemory reservation;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000567 Address area_start = NULL;
568 Address area_end = NULL;
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000569
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000570 //
571 // MemoryChunk layout:
572 //
573 // Executable
574 // +----------------------------+<- base aligned with MemoryChunk::kAlignment
575 // | Header |
576 // +----------------------------+<- base + CodePageGuardStartOffset
577 // | Guard |
578 // +----------------------------+<- area_start_
579 // | Area |
580 // +----------------------------+<- area_end_ (area_start + commit_area_size)
581 // | Committed but not used |
582 // +----------------------------+<- aligned at OS page boundary
583 // | Reserved but not committed |
584 // +----------------------------+<- aligned at OS page boundary
585 // | Guard |
586 // +----------------------------+<- base + chunk_size
587 //
588 // Non-executable
589 // +----------------------------+<- base aligned with MemoryChunk::kAlignment
590 // | Header |
591 // +----------------------------+<- area_start_ (base + kObjectStartOffset)
592 // | Area |
593 // +----------------------------+<- area_end_ (area_start + commit_area_size)
594 // | Committed but not used |
595 // +----------------------------+<- aligned at OS page boundary
596 // | Reserved but not committed |
597 // +----------------------------+<- base + chunk_size
598 //
599
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000600 if (executable == EXECUTABLE) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000601 chunk_size = RoundUp(CodePageAreaStartOffset() + reserve_area_size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000602 OS::CommitPageSize()) + CodePageGuardSize();
603
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000604 // Check executable memory limit.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000605 if (size_executable_ + chunk_size > capacity_executable_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606 LOG(isolate_,
607 StringEvent("MemoryAllocator::AllocateRawMemory",
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000608 "V8 Executable Allocation capacity exceeded"));
609 return NULL;
610 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000611
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000612 // Size of header (not executable) plus area (executable).
613 size_t commit_size = RoundUp(CodePageGuardStartOffset() + commit_area_size,
614 OS::CommitPageSize());
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000615 // Allocate executable memory either from code range or from the
616 // OS.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000617 if (isolate_->code_range()->exists()) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000618 base = isolate_->code_range()->AllocateRawMemory(chunk_size,
619 commit_size,
620 &chunk_size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000621 ASSERT(IsAligned(reinterpret_cast<intptr_t>(base),
622 MemoryChunk::kAlignment));
623 if (base == NULL) return NULL;
624 size_ += chunk_size;
625 // Update executable memory size.
626 size_executable_ += chunk_size;
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000627 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000628 base = AllocateAlignedMemory(chunk_size,
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000629 commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000630 MemoryChunk::kAlignment,
631 executable,
632 &reservation);
633 if (base == NULL) return NULL;
634 // Update executable memory size.
635 size_executable_ += reservation.size();
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000636 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000637
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000638 if (Heap::ShouldZapGarbage()) {
639 ZapBlock(base, CodePageGuardStartOffset());
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000640 ZapBlock(base + CodePageAreaStartOffset(), commit_area_size);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000641 }
642
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000643 area_start = base + CodePageAreaStartOffset();
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000644 area_end = area_start + commit_area_size;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000645 } else {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000646 chunk_size = RoundUp(MemoryChunk::kObjectStartOffset + reserve_area_size,
647 OS::CommitPageSize());
648 size_t commit_size = RoundUp(MemoryChunk::kObjectStartOffset +
649 commit_area_size, OS::CommitPageSize());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000650 base = AllocateAlignedMemory(chunk_size,
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000651 commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000652 MemoryChunk::kAlignment,
653 executable,
654 &reservation);
655
656 if (base == NULL) return NULL;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000657
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000658 if (Heap::ShouldZapGarbage()) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000659 ZapBlock(base, Page::kObjectStartOffset + commit_area_size);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000660 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000661
662 area_start = base + Page::kObjectStartOffset;
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000663 area_end = area_start + commit_area_size;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000664 }
665
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000666 // Use chunk_size for statistics and callbacks because we assume that they
667 // treat reserved but not-yet committed memory regions of chunks as allocated.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000668 isolate_->counters()->memory_allocated()->
669 Increment(static_cast<int>(chunk_size));
670
671 LOG(isolate_, NewEvent("MemoryChunk", base, chunk_size));
672 if (owner != NULL) {
673 ObjectSpace space = static_cast<ObjectSpace>(1 << owner->identity());
674 PerformAllocationCallback(space, kAllocationActionAllocate, chunk_size);
675 }
676
677 MemoryChunk* result = MemoryChunk::Initialize(heap,
678 base,
679 chunk_size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000680 area_start,
681 area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000682 executable,
683 owner);
684 result->set_reserved_memory(&reservation);
685 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686}
687
688
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000689Page* MemoryAllocator::AllocatePage(intptr_t size,
690 PagedSpace* owner,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000691 Executability executable) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000692 MemoryChunk* chunk = AllocateChunk(size, size, executable, owner);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000693
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000694 if (chunk == NULL) return NULL;
695
696 return Page::Initialize(isolate_->heap(), chunk, executable, owner);
697}
698
699
700LargePage* MemoryAllocator::AllocateLargePage(intptr_t object_size,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000701 Space* owner,
702 Executability executable) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000703 MemoryChunk* chunk = AllocateChunk(object_size,
704 object_size,
705 executable,
706 owner);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000707 if (chunk == NULL) return NULL;
708 return LargePage::Initialize(isolate_->heap(), chunk);
709}
710
711
712void MemoryAllocator::Free(MemoryChunk* chunk) {
713 LOG(isolate_, DeleteEvent("MemoryChunk", chunk));
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000714 if (chunk->owner() != NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000715 ObjectSpace space =
716 static_cast<ObjectSpace>(1 << chunk->owner()->identity());
717 PerformAllocationCallback(space, kAllocationActionFree, chunk->size());
718 }
719
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000720 isolate_->heap()->RememberUnmappedPage(
721 reinterpret_cast<Address>(chunk), chunk->IsEvacuationCandidate());
722
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000723 delete chunk->slots_buffer();
724 delete chunk->skip_list();
725
726 VirtualMemory* reservation = chunk->reserved_memory();
727 if (reservation->IsReserved()) {
728 FreeMemory(reservation, chunk->executable());
729 } else {
730 FreeMemory(chunk->address(),
731 chunk->size(),
732 chunk->executable());
733 }
734}
735
736
737bool MemoryAllocator::CommitBlock(Address start,
738 size_t size,
739 Executability executable) {
740 if (!VirtualMemory::CommitRegion(start, size, executable)) return false;
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000741
742 if (Heap::ShouldZapGarbage()) {
743 ZapBlock(start, size);
744 }
745
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000746 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
747 return true;
748}
749
750
751bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
752 if (!VirtualMemory::UncommitRegion(start, size)) return false;
753 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
754 return true;
755}
756
757
758void MemoryAllocator::ZapBlock(Address start, size_t size) {
759 for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) {
760 Memory::Address_at(start + s) = kZapValue;
761 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762}
763
764
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000765void MemoryAllocator::PerformAllocationCallback(ObjectSpace space,
766 AllocationAction action,
767 size_t size) {
768 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
769 MemoryAllocationCallbackRegistration registration =
770 memory_allocation_callbacks_[i];
771 if ((registration.space & space) == space &&
772 (registration.action & action) == action)
773 registration.callback(space, action, static_cast<int>(size));
774 }
775}
776
777
778bool MemoryAllocator::MemoryAllocationCallbackRegistered(
779 MemoryAllocationCallback callback) {
780 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
781 if (memory_allocation_callbacks_[i].callback == callback) return true;
782 }
783 return false;
784}
785
786
787void MemoryAllocator::AddMemoryAllocationCallback(
788 MemoryAllocationCallback callback,
789 ObjectSpace space,
790 AllocationAction action) {
791 ASSERT(callback != NULL);
792 MemoryAllocationCallbackRegistration registration(callback, space, action);
793 ASSERT(!MemoryAllocator::MemoryAllocationCallbackRegistered(callback));
794 return memory_allocation_callbacks_.Add(registration);
795}
796
797
798void MemoryAllocator::RemoveMemoryAllocationCallback(
799 MemoryAllocationCallback callback) {
800 ASSERT(callback != NULL);
801 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
802 if (memory_allocation_callbacks_[i].callback == callback) {
803 memory_allocation_callbacks_.Remove(i);
804 return;
805 }
806 }
807 UNREACHABLE();
808}
809
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810
811#ifdef DEBUG
812void MemoryAllocator::ReportStatistics() {
813 float pct = static_cast<float>(capacity_ - size_) / capacity_;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000814 PrintF(" capacity: %" V8_PTR_PREFIX "d"
815 ", used: %" V8_PTR_PREFIX "d"
816 ", available: %%%d\n\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 capacity_, size_, static_cast<int>(pct*100));
818}
819#endif
820
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000821
822int MemoryAllocator::CodePageGuardStartOffset() {
823 // We are guarding code pages: the first OS page after the header
824 // will be protected as non-writable.
825 return RoundUp(Page::kObjectStartOffset, OS::CommitPageSize());
826}
827
828
829int MemoryAllocator::CodePageGuardSize() {
830 return static_cast<int>(OS::CommitPageSize());
831}
832
833
834int MemoryAllocator::CodePageAreaStartOffset() {
835 // We are guarding code pages: the first OS page after the header
836 // will be protected as non-writable.
837 return CodePageGuardStartOffset() + CodePageGuardSize();
838}
839
840
841int MemoryAllocator::CodePageAreaEndOffset() {
842 // We are guarding code pages: the last OS page will be protected as
843 // non-writable.
844 return Page::kPageSize - static_cast<int>(OS::CommitPageSize());
845}
846
847
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000848bool MemoryAllocator::CommitExecutableMemory(VirtualMemory* vm,
849 Address start,
850 size_t commit_size,
851 size_t reserved_size) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000852 // Commit page header (not executable).
853 if (!vm->Commit(start,
854 CodePageGuardStartOffset(),
855 false)) {
856 return false;
857 }
858
859 // Create guard page after the header.
860 if (!vm->Guard(start + CodePageGuardStartOffset())) {
861 return false;
862 }
863
864 // Commit page body (executable).
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000865 if (!vm->Commit(start + CodePageAreaStartOffset(),
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000866 commit_size - CodePageGuardStartOffset(),
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000867 true)) {
868 return false;
869 }
870
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000871 // Create guard page before the end.
872 if (!vm->Guard(start + reserved_size - CodePageGuardSize())) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000873 return false;
874 }
875
876 return true;
877}
878
879
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880// -----------------------------------------------------------------------------
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000881// MemoryChunk implementation
882
883void MemoryChunk::IncrementLiveBytesFromMutator(Address address, int by) {
884 MemoryChunk* chunk = MemoryChunk::FromAddress(address);
885 if (!chunk->InNewSpace() && !static_cast<Page*>(chunk)->WasSwept()) {
886 static_cast<PagedSpace*>(chunk->owner())->IncrementUnsweptFreeBytes(-by);
887 }
888 chunk->IncrementLiveBytes(by);
889}
890
891// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892// PagedSpace implementation
893
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000894PagedSpace::PagedSpace(Heap* heap,
895 intptr_t max_capacity,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000896 AllocationSpace id,
897 Executability executable)
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000898 : Space(heap, id, executable),
899 free_list_(this),
900 was_swept_conservatively_(false),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000901 first_unswept_page_(Page::FromAddress(NULL)),
902 unswept_free_bytes_(0) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000903 if (id == CODE_SPACE) {
904 area_size_ = heap->isolate()->memory_allocator()->
905 CodePageAreaSize();
906 } else {
907 area_size_ = Page::kPageSize - Page::kObjectStartOffset;
908 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000910 * AreaSize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 accounting_stats_.Clear();
912
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 allocation_info_.top = NULL;
914 allocation_info_.limit = NULL;
915
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000916 anchor_.InitializeAsAnchor(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917}
918
919
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000920bool PagedSpace::SetUp() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 return true;
922}
923
924
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000925bool PagedSpace::HasBeenSetUp() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000926 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927}
928
929
930void PagedSpace::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000931 PageIterator iterator(this);
932 while (iterator.has_next()) {
933 heap()->isolate()->memory_allocator()->Free(iterator.next());
934 }
935 anchor_.set_next_page(&anchor_);
936 anchor_.set_prev_page(&anchor_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937 accounting_stats_.Clear();
938}
939
940
danno@chromium.org72204d52012-10-31 10:02:10 +0000941size_t PagedSpace::CommittedPhysicalMemory() {
942 if (!VirtualMemory::HasLazyCommits()) return CommittedMemory();
943 MemoryChunk::UpdateHighWaterMark(allocation_info_.top);
944 size_t size = 0;
945 PageIterator it(this);
946 while (it.has_next()) {
947 size += it.next()->CommittedPhysicalMemory();
948 }
949 return size;
950}
951
952
lrn@chromium.org303ada72010-10-27 09:33:13 +0000953MaybeObject* PagedSpace::FindObject(Address addr) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000954 // Note: this function can only be called on precisely swept spaces.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000955 ASSERT(!heap()->mark_compact_collector()->in_use());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956
957 if (!Contains(addr)) return Failure::Exception();
958
959 Page* p = Page::FromAddress(addr);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000960 HeapObjectIterator it(p, NULL);
961 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
962 Address cur = obj->address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963 Address next = cur + obj->Size();
964 if ((cur <= addr) && (addr < next)) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 }
966
kasper.lund7276f142008-07-30 08:49:36 +0000967 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 return Failure::Exception();
969}
970
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000971bool PagedSpace::CanExpand() {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000972 ASSERT(max_capacity_ % AreaSize() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973
974 if (Capacity() == max_capacity_) return false;
975
976 ASSERT(Capacity() < max_capacity_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000978 // Are we going to exceed capacity for this space?
979 if ((Capacity() + Page::kPageSize) > max_capacity_) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000981 return true;
982}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000984bool PagedSpace::Expand() {
985 if (!CanExpand()) return false;
986
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000987 intptr_t size = AreaSize();
988
989 if (anchor_.next_page() == &anchor_) {
990 size = SizeOfFirstPage();
991 }
992
993 Page* p = heap()->isolate()->memory_allocator()->AllocatePage(
994 size, this, executable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000995 if (p == NULL) return false;
996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997 ASSERT(Capacity() <= max_capacity_);
998
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000999 p->InsertAfter(anchor_.prev_page());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000
1001 return true;
1002}
1003
1004
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001005intptr_t PagedSpace::SizeOfFirstPage() {
1006 int size = 0;
1007 switch (identity()) {
1008 case OLD_POINTER_SPACE:
1009 size = 64 * kPointerSize * KB;
1010 break;
1011 case OLD_DATA_SPACE:
1012 size = 192 * KB;
1013 break;
1014 case MAP_SPACE:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001015 size = 16 * kPointerSize * KB;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001016 break;
1017 case CELL_SPACE:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001018 size = 16 * kPointerSize * KB;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001019 break;
1020 case CODE_SPACE:
1021 if (kPointerSize == 8) {
1022 // On x64 we allocate code pages in a special way (from the reserved
1023 // 2Byte area). That part of the code is not yet upgraded to handle
1024 // small pages.
1025 size = AreaSize();
1026 } else {
1027 size = 384 * KB;
1028 }
1029 break;
1030 default:
1031 UNREACHABLE();
1032 }
1033 return Min(size, AreaSize());
1034}
1035
1036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037int PagedSpace::CountTotalPages() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001038 PageIterator it(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 int count = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001040 while (it.has_next()) {
1041 it.next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 count++;
1043 }
1044 return count;
1045}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046
1047
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001048void PagedSpace::ReleasePage(Page* page) {
1049 ASSERT(page->LiveBytes() == 0);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001050 ASSERT(AreaSize() == page->area_size());
danno@chromium.org2c456792011-11-11 12:00:53 +00001051
ricow@chromium.org7ad65222011-12-19 12:13:11 +00001052 // Adjust list of unswept pages if the page is the head of the list.
danno@chromium.org2c456792011-11-11 12:00:53 +00001053 if (first_unswept_page_ == page) {
1054 first_unswept_page_ = page->next_page();
1055 if (first_unswept_page_ == anchor()) {
1056 first_unswept_page_ = Page::FromAddress(NULL);
1057 }
1058 }
1059
1060 if (page->WasSwept()) {
1061 intptr_t size = free_list_.EvictFreeListItems(page);
1062 accounting_stats_.AllocateBytes(size);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001063 ASSERT_EQ(AreaSize(), static_cast<int>(size));
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001064 } else {
1065 DecreaseUnsweptFreeBytes(page);
danno@chromium.org2c456792011-11-11 12:00:53 +00001066 }
1067
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001068 if (Page::FromAllocationTop(allocation_info_.top) == page) {
1069 allocation_info_.top = allocation_info_.limit = NULL;
1070 }
1071
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001072 page->Unlink();
1073 if (page->IsFlagSet(MemoryChunk::CONTAINS_ONLY_DATA)) {
1074 heap()->isolate()->memory_allocator()->Free(page);
1075 } else {
1076 heap()->QueueMemoryChunkForFree(page);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001077 }
1078
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001079 ASSERT(Capacity() > 0);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001080 accounting_stats_.ShrinkSpace(AreaSize());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081}
1082
1083
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084#ifdef DEBUG
1085void PagedSpace::Print() { }
1086#endif
1087
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001088#ifdef VERIFY_HEAP
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001089void PagedSpace::Verify(ObjectVisitor* visitor) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001090 // We can only iterate over the pages if they were swept precisely.
1091 if (was_swept_conservatively_) return;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001092
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001093 bool allocation_pointer_found_in_space =
1094 (allocation_info_.top == allocation_info_.limit);
1095 PageIterator page_iterator(this);
1096 while (page_iterator.has_next()) {
1097 Page* page = page_iterator.next();
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001098 CHECK(page->owner() == this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001099 if (page == Page::FromAllocationTop(allocation_info_.top)) {
1100 allocation_pointer_found_in_space = true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001101 }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001102 CHECK(page->WasSweptPrecisely());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001103 HeapObjectIterator it(page, NULL);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001104 Address end_of_previous_object = page->area_start();
1105 Address top = page->area_end();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001106 int black_size = 0;
1107 for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) {
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001108 CHECK(end_of_previous_object <= object->address());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001109
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001110 // The first word should be a map, and we expect all map pointers to
1111 // be in map space.
1112 Map* map = object->map();
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001113 CHECK(map->IsMap());
1114 CHECK(heap()->map_space()->Contains(map));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001115
1116 // Perform space-specific object verification.
1117 VerifyObject(object);
1118
1119 // The object itself should look OK.
1120 object->Verify();
1121
1122 // All the interior pointers should be contained in the heap.
1123 int size = object->Size();
1124 object->IterateBody(map->instance_type(), size, visitor);
1125 if (Marking::IsBlack(Marking::MarkBitFrom(object))) {
1126 black_size += size;
1127 }
1128
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001129 CHECK(object->address() + size <= top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001130 end_of_previous_object = object->address() + size;
1131 }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001132 CHECK_LE(black_size, page->LiveBytes());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001133 }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001134 CHECK(allocation_pointer_found_in_space);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001135}
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001136#endif // VERIFY_HEAP
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138// -----------------------------------------------------------------------------
1139// NewSpace implementation
1140
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001141
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001142bool NewSpace::SetUp(int reserved_semispace_capacity,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001143 int maximum_semispace_capacity) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001144 // Set up new space based on the preallocated memory block defined by
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001145 // start and size. The provided space is divided into two semi-spaces.
1146 // To support fast containment testing in the new space, the size of
1147 // this chunk must be a power of two and it must be aligned to its size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001148 int initial_semispace_capacity = heap()->InitialSemiSpaceSize();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001149
1150 size_t size = 2 * reserved_semispace_capacity;
1151 Address base =
1152 heap()->isolate()->memory_allocator()->ReserveAlignedMemory(
1153 size, size, &reservation_);
1154 if (base == NULL) return false;
1155
1156 chunk_base_ = base;
1157 chunk_size_ = static_cast<uintptr_t>(size);
1158 LOG(heap()->isolate(), NewEvent("InitialChunk", chunk_base_, chunk_size_));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
1161 ASSERT(IsPowerOf2(maximum_semispace_capacity));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001163 // Allocate and set up the histogram arrays if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1165 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1166
1167#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
1168 promoted_histogram_[name].set_name(#name);
1169 INSTANCE_TYPE_LIST(SET_NAME)
1170#undef SET_NAME
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001172 ASSERT(reserved_semispace_capacity == heap()->ReservedSemiSpaceSize());
1173 ASSERT(static_cast<intptr_t>(chunk_size_) >=
1174 2 * heap()->ReservedSemiSpaceSize());
1175 ASSERT(IsAddressAligned(chunk_base_, 2 * reserved_semispace_capacity, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001177 to_space_.SetUp(chunk_base_,
1178 initial_semispace_capacity,
1179 maximum_semispace_capacity);
1180 from_space_.SetUp(chunk_base_ + reserved_semispace_capacity,
1181 initial_semispace_capacity,
1182 maximum_semispace_capacity);
1183 if (!to_space_.Commit()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 return false;
1185 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001186 ASSERT(!from_space_.is_committed()); // No need to use memory yet.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001188 start_ = chunk_base_;
1189 address_mask_ = ~(2 * reserved_semispace_capacity - 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001190 object_mask_ = address_mask_ | kHeapObjectTagMask;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001191 object_expected_ = reinterpret_cast<uintptr_t>(start_) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001193 ResetAllocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195 return true;
1196}
1197
1198
1199void NewSpace::TearDown() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 if (allocated_histogram_) {
1201 DeleteArray(allocated_histogram_);
1202 allocated_histogram_ = NULL;
1203 }
1204 if (promoted_histogram_) {
1205 DeleteArray(promoted_histogram_);
1206 promoted_histogram_ = NULL;
1207 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
1209 start_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 allocation_info_.top = NULL;
1211 allocation_info_.limit = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001213 to_space_.TearDown();
1214 from_space_.TearDown();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001215
1216 LOG(heap()->isolate(), DeleteEvent("InitialChunk", chunk_base_));
1217
1218 ASSERT(reservation_.IsReserved());
1219 heap()->isolate()->memory_allocator()->FreeMemory(&reservation_,
1220 NOT_EXECUTABLE);
1221 chunk_base_ = NULL;
1222 chunk_size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223}
1224
1225
1226void NewSpace::Flip() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001227 SemiSpace::Swap(&from_space_, &to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228}
1229
1230
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001231void NewSpace::Grow() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001232 // Double the semispace size but only up to maximum capacity.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001233 ASSERT(Capacity() < MaximumCapacity());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001234 int new_capacity = Min(MaximumCapacity(), 2 * static_cast<int>(Capacity()));
1235 if (to_space_.GrowTo(new_capacity)) {
1236 // Only grow from space if we managed to grow to-space.
1237 if (!from_space_.GrowTo(new_capacity)) {
1238 // If we managed to grow to-space but couldn't grow from-space,
1239 // attempt to shrink to-space.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001240 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
1241 // We are in an inconsistent state because we could not
1242 // commit/uncommit memory from new space.
1243 V8::FatalProcessOutOfMemory("Failed to grow new space.");
1244 }
1245 }
1246 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001248}
1249
1250
1251void NewSpace::Shrink() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001252 int new_capacity = Max(InitialCapacity(), 2 * SizeAsInt());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001253 int rounded_new_capacity = RoundUp(new_capacity, Page::kPageSize);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001254 if (rounded_new_capacity < Capacity() &&
1255 to_space_.ShrinkTo(rounded_new_capacity)) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001256 // Only shrink from-space if we managed to shrink to-space.
1257 from_space_.Reset();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001258 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001259 // If we managed to shrink to-space but couldn't shrink from
1260 // space, attempt to grow to-space again.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001261 if (!to_space_.GrowTo(from_space_.Capacity())) {
1262 // We are in an inconsistent state because we could not
1263 // commit/uncommit memory from new space.
1264 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
1265 }
1266 }
1267 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001268 allocation_info_.limit = to_space_.page_high();
1269 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1270}
1271
1272
1273void NewSpace::UpdateAllocationInfo() {
danno@chromium.org72204d52012-10-31 10:02:10 +00001274 MemoryChunk::UpdateHighWaterMark(allocation_info_.top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001275 allocation_info_.top = to_space_.page_low();
1276 allocation_info_.limit = to_space_.page_high();
1277
1278 // Lower limit during incremental marking.
1279 if (heap()->incremental_marking()->IsMarking() &&
1280 inline_allocation_limit_step() != 0) {
1281 Address new_limit =
1282 allocation_info_.top + inline_allocation_limit_step();
1283 allocation_info_.limit = Min(new_limit, allocation_info_.limit);
1284 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001285 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286}
1287
1288
1289void NewSpace::ResetAllocationInfo() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001290 to_space_.Reset();
1291 UpdateAllocationInfo();
1292 pages_used_ = 0;
1293 // Clear all mark-bits in the to-space.
1294 NewSpacePageIterator it(&to_space_);
1295 while (it.has_next()) {
1296 Bitmap::Clear(it.next());
1297 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298}
1299
1300
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001301bool NewSpace::AddFreshPage() {
1302 Address top = allocation_info_.top;
1303 if (NewSpacePage::IsAtStart(top)) {
1304 // The current page is already empty. Don't try to make another.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001306 // We should only get here if someone asks to allocate more
1307 // than what can be stored in a single page.
1308 // TODO(gc): Change the limit on new-space allocation to prevent this
1309 // from happening (all such allocations should go directly to LOSpace).
1310 return false;
1311 }
1312 if (!to_space_.AdvancePage()) {
1313 // Failed to get a new page in to-space.
1314 return false;
1315 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001316
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001317 // Clear remainder of current page.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001318 Address limit = NewSpacePage::FromLimit(top)->area_end();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001319 if (heap()->gc_state() == Heap::SCAVENGE) {
1320 heap()->promotion_queue()->SetNewLimit(limit);
1321 heap()->promotion_queue()->ActivateGuardIfOnTheSamePage();
1322 }
1323
1324 int remaining_in_page = static_cast<int>(limit - top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001325 heap()->CreateFillerObjectAt(top, remaining_in_page);
1326 pages_used_++;
1327 UpdateAllocationInfo();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001328
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001329 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330}
1331
1332
danno@chromium.orgc612e022011-11-10 11:38:15 +00001333MaybeObject* NewSpace::SlowAllocateRaw(int size_in_bytes) {
1334 Address old_top = allocation_info_.top;
1335 Address new_top = old_top + size_in_bytes;
1336 Address high = to_space_.page_high();
1337 if (allocation_info_.limit < high) {
1338 // Incremental marking has lowered the limit to get a
1339 // chance to do a step.
1340 allocation_info_.limit = Min(
1341 allocation_info_.limit + inline_allocation_limit_step_,
1342 high);
1343 int bytes_allocated = static_cast<int>(new_top - top_on_previous_step_);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001344 heap()->incremental_marking()->Step(
1345 bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001346 top_on_previous_step_ = new_top;
1347 return AllocateRaw(size_in_bytes);
1348 } else if (AddFreshPage()) {
1349 // Switched to new page. Try allocating again.
1350 int bytes_allocated = static_cast<int>(old_top - top_on_previous_step_);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001351 heap()->incremental_marking()->Step(
1352 bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001353 top_on_previous_step_ = to_space_.page_low();
1354 return AllocateRaw(size_in_bytes);
1355 } else {
1356 return Failure::RetryAfterGC();
1357 }
1358}
1359
1360
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001361#ifdef VERIFY_HEAP
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001362// We do not use the SemiSpaceIterator because verification doesn't assume
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363// that it works (it depends on the invariants we are checking).
1364void NewSpace::Verify() {
1365 // The allocation pointer should be in the space or at the very end.
1366 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1367
1368 // There should be objects packed in from the low address up to the
1369 // allocation pointer.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001370 Address current = to_space_.first_page()->area_start();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001371 CHECK_EQ(current, to_space_.space_start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001373 while (current != top()) {
1374 if (!NewSpacePage::IsAtEnd(current)) {
1375 // The allocation pointer should not be in the middle of an object.
1376 CHECK(!NewSpacePage::FromLimit(current)->ContainsLimit(top()) ||
1377 current < top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001379 HeapObject* object = HeapObject::FromAddress(current);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001381 // The first word should be a map, and we expect all map pointers to
1382 // be in map space.
1383 Map* map = object->map();
1384 CHECK(map->IsMap());
1385 CHECK(heap()->map_space()->Contains(map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001387 // The object should not be code or a map.
1388 CHECK(!object->IsMap());
1389 CHECK(!object->IsCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001391 // The object itself should look OK.
1392 object->Verify();
1393
1394 // All the interior pointers should be contained in the heap.
1395 VerifyPointersVisitor visitor;
1396 int size = object->Size();
1397 object->IterateBody(map->instance_type(), size, &visitor);
1398
1399 current += size;
1400 } else {
1401 // At end of page, switch to next page.
1402 NewSpacePage* page = NewSpacePage::FromLimit(current)->next_page();
1403 // Next page should be valid.
1404 CHECK(!page->is_anchor());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001405 current = page->area_start();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001406 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 }
1408
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001409 // Check semi-spaces.
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001410 CHECK_EQ(from_space_.id(), kFromSpace);
1411 CHECK_EQ(to_space_.id(), kToSpace);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001412 from_space_.Verify();
1413 to_space_.Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414}
1415#endif
1416
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417// -----------------------------------------------------------------------------
1418// SemiSpace implementation
1419
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001420void SemiSpace::SetUp(Address start,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001421 int initial_capacity,
1422 int maximum_capacity) {
1423 // Creates a space in the young generation. The constructor does not
1424 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1425 // memory of size 'capacity' when set up, and does not grow or shrink
1426 // otherwise. In the mark-compact collector, the memory region of the from
1427 // space is used as the marking stack. It requires contiguous memory
1428 // addresses.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001429 ASSERT(maximum_capacity >= Page::kPageSize);
1430 initial_capacity_ = RoundDown(initial_capacity, Page::kPageSize);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001431 capacity_ = initial_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001432 maximum_capacity_ = RoundDown(maximum_capacity, Page::kPageSize);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001433 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 start_ = start;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001435 address_mask_ = ~(maximum_capacity - 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001436 object_mask_ = address_mask_ | kHeapObjectTagMask;
ager@chromium.org9085a012009-05-11 19:22:57 +00001437 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 age_mark_ = start_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439}
1440
1441
1442void SemiSpace::TearDown() {
1443 start_ = NULL;
1444 capacity_ = 0;
1445}
1446
1447
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001448bool SemiSpace::Commit() {
1449 ASSERT(!is_committed());
1450 int pages = capacity_ / Page::kPageSize;
1451 Address end = start_ + maximum_capacity_;
1452 Address start = end - pages * Page::kPageSize;
1453 if (!heap()->isolate()->memory_allocator()->CommitBlock(start,
1454 capacity_,
1455 executable())) {
kasper.lund7276f142008-07-30 08:49:36 +00001456 return false;
1457 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001458
1459 NewSpacePage* page = anchor();
1460 for (int i = 1; i <= pages; i++) {
1461 NewSpacePage* new_page =
1462 NewSpacePage::Initialize(heap(), end - i * Page::kPageSize, this);
1463 new_page->InsertAfter(page);
1464 page = new_page;
1465 }
1466
1467 committed_ = true;
1468 Reset();
1469 return true;
1470}
1471
1472
1473bool SemiSpace::Uncommit() {
1474 ASSERT(is_committed());
1475 Address start = start_ + maximum_capacity_ - capacity_;
1476 if (!heap()->isolate()->memory_allocator()->UncommitBlock(start, capacity_)) {
1477 return false;
1478 }
1479 anchor()->set_next_page(anchor());
1480 anchor()->set_prev_page(anchor());
1481
1482 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 return true;
1484}
1485
1486
danno@chromium.org72204d52012-10-31 10:02:10 +00001487size_t SemiSpace::CommittedPhysicalMemory() {
1488 if (!is_committed()) return 0;
1489 size_t size = 0;
1490 NewSpacePageIterator it(this);
1491 while (it.has_next()) {
1492 size += it.next()->CommittedPhysicalMemory();
1493 }
1494 return size;
1495}
1496
1497
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001498bool SemiSpace::GrowTo(int new_capacity) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001499 if (!is_committed()) {
1500 if (!Commit()) return false;
1501 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001502 ASSERT((new_capacity & Page::kPageAlignmentMask) == 0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001503 ASSERT(new_capacity <= maximum_capacity_);
1504 ASSERT(new_capacity > capacity_);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001505 int pages_before = capacity_ / Page::kPageSize;
1506 int pages_after = new_capacity / Page::kPageSize;
1507
1508 Address end = start_ + maximum_capacity_;
1509 Address start = end - new_capacity;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001510 size_t delta = new_capacity - capacity_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001511
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001512 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001513 if (!heap()->isolate()->memory_allocator()->CommitBlock(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001514 start, delta, executable())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001515 return false;
1516 }
1517 capacity_ = new_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001518 NewSpacePage* last_page = anchor()->prev_page();
1519 ASSERT(last_page != anchor());
1520 for (int i = pages_before + 1; i <= pages_after; i++) {
1521 Address page_address = end - i * Page::kPageSize;
1522 NewSpacePage* new_page = NewSpacePage::Initialize(heap(),
1523 page_address,
1524 this);
1525 new_page->InsertAfter(last_page);
1526 Bitmap::Clear(new_page);
1527 // Duplicate the flags that was set on the old page.
1528 new_page->SetFlags(last_page->GetFlags(),
1529 NewSpacePage::kCopyOnFlipFlagsMask);
1530 last_page = new_page;
1531 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001532 return true;
1533}
1534
1535
1536bool SemiSpace::ShrinkTo(int new_capacity) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001537 ASSERT((new_capacity & Page::kPageAlignmentMask) == 0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001538 ASSERT(new_capacity >= initial_capacity_);
1539 ASSERT(new_capacity < capacity_);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001540 if (is_committed()) {
1541 // Semispaces grow backwards from the end of their allocated capacity,
1542 // so we find the before and after start addresses relative to the
1543 // end of the space.
1544 Address space_end = start_ + maximum_capacity_;
1545 Address old_start = space_end - capacity_;
1546 size_t delta = capacity_ - new_capacity;
1547 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001548
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001549 MemoryAllocator* allocator = heap()->isolate()->memory_allocator();
1550 if (!allocator->UncommitBlock(old_start, delta)) {
1551 return false;
1552 }
1553
1554 int pages_after = new_capacity / Page::kPageSize;
1555 NewSpacePage* new_last_page =
1556 NewSpacePage::FromAddress(space_end - pages_after * Page::kPageSize);
1557 new_last_page->set_next_page(anchor());
1558 anchor()->set_prev_page(new_last_page);
1559 ASSERT((current_page_ <= first_page()) && (current_page_ >= new_last_page));
1560 }
1561
1562 capacity_ = new_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001563
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001564 return true;
1565}
1566
1567
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001568void SemiSpace::FlipPages(intptr_t flags, intptr_t mask) {
1569 anchor_.set_owner(this);
1570 // Fixup back-pointers to anchor. Address of anchor changes
1571 // when we swap.
1572 anchor_.prev_page()->set_next_page(&anchor_);
1573 anchor_.next_page()->set_prev_page(&anchor_);
1574
1575 bool becomes_to_space = (id_ == kFromSpace);
1576 id_ = becomes_to_space ? kToSpace : kFromSpace;
1577 NewSpacePage* page = anchor_.next_page();
1578 while (page != &anchor_) {
1579 page->set_owner(this);
1580 page->SetFlags(flags, mask);
1581 if (becomes_to_space) {
1582 page->ClearFlag(MemoryChunk::IN_FROM_SPACE);
1583 page->SetFlag(MemoryChunk::IN_TO_SPACE);
1584 page->ClearFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK);
1585 page->ResetLiveBytes();
1586 } else {
1587 page->SetFlag(MemoryChunk::IN_FROM_SPACE);
1588 page->ClearFlag(MemoryChunk::IN_TO_SPACE);
1589 }
1590 ASSERT(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE));
1591 ASSERT(page->IsFlagSet(MemoryChunk::IN_TO_SPACE) ||
1592 page->IsFlagSet(MemoryChunk::IN_FROM_SPACE));
1593 page = page->next_page();
1594 }
1595}
1596
1597
1598void SemiSpace::Reset() {
1599 ASSERT(anchor_.next_page() != &anchor_);
1600 current_page_ = anchor_.next_page();
1601}
1602
1603
1604void SemiSpace::Swap(SemiSpace* from, SemiSpace* to) {
1605 // We won't be swapping semispaces without data in them.
1606 ASSERT(from->anchor_.next_page() != &from->anchor_);
1607 ASSERT(to->anchor_.next_page() != &to->anchor_);
1608
1609 // Swap bits.
1610 SemiSpace tmp = *from;
1611 *from = *to;
1612 *to = tmp;
1613
1614 // Fixup back-pointers to the page list anchor now that its address
1615 // has changed.
1616 // Swap to/from-space bits on pages.
1617 // Copy GC flags from old active space (from-space) to new (to-space).
1618 intptr_t flags = from->current_page()->GetFlags();
1619 to->FlipPages(flags, NewSpacePage::kCopyOnFlipFlagsMask);
1620
1621 from->FlipPages(0, 0);
1622}
1623
1624
1625void SemiSpace::set_age_mark(Address mark) {
1626 ASSERT(NewSpacePage::FromLimit(mark)->semi_space() == this);
1627 age_mark_ = mark;
1628 // Mark all pages up to the one containing mark.
1629 NewSpacePageIterator it(space_start(), mark);
1630 while (it.has_next()) {
1631 it.next()->SetFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK);
1632 }
1633}
1634
1635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636#ifdef DEBUG
1637void SemiSpace::Print() { }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001638#endif
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001639
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001640#ifdef VERIFY_HEAP
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001641void SemiSpace::Verify() {
1642 bool is_from_space = (id_ == kFromSpace);
1643 NewSpacePage* page = anchor_.next_page();
1644 CHECK(anchor_.semi_space() == this);
1645 while (page != &anchor_) {
1646 CHECK(page->semi_space() == this);
1647 CHECK(page->InNewSpace());
1648 CHECK(page->IsFlagSet(is_from_space ? MemoryChunk::IN_FROM_SPACE
1649 : MemoryChunk::IN_TO_SPACE));
1650 CHECK(!page->IsFlagSet(is_from_space ? MemoryChunk::IN_TO_SPACE
1651 : MemoryChunk::IN_FROM_SPACE));
1652 CHECK(page->IsFlagSet(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING));
1653 if (!is_from_space) {
1654 // The pointers-from-here-are-interesting flag isn't updated dynamically
1655 // on from-space pages, so it might be out of sync with the marking state.
1656 if (page->heap()->incremental_marking()->IsMarking()) {
1657 CHECK(page->IsFlagSet(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING));
1658 } else {
1659 CHECK(!page->IsFlagSet(
1660 MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING));
1661 }
1662 // TODO(gc): Check that the live_bytes_count_ field matches the
1663 // black marking on the page (if we make it match in new-space).
1664 }
1665 CHECK(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE));
1666 CHECK(page->prev_page()->next_page() == page);
1667 page = page->next_page();
1668 }
1669}
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001670#endif
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001671
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001672#ifdef DEBUG
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001673void SemiSpace::AssertValidRange(Address start, Address end) {
1674 // Addresses belong to same semi-space
1675 NewSpacePage* page = NewSpacePage::FromLimit(start);
1676 NewSpacePage* end_page = NewSpacePage::FromLimit(end);
1677 SemiSpace* space = page->semi_space();
1678 CHECK_EQ(space, end_page->semi_space());
1679 // Start address is before end address, either on same page,
1680 // or end address is on a later page in the linked list of
1681 // semi-space pages.
1682 if (page == end_page) {
1683 CHECK(start <= end);
1684 } else {
1685 while (page != end_page) {
1686 page = page->next_page();
1687 CHECK_NE(page, space->anchor());
1688 }
1689 }
1690}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691#endif
1692
1693
1694// -----------------------------------------------------------------------------
1695// SemiSpaceIterator implementation.
1696SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001697 Initialize(space->bottom(), space->top(), NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698}
1699
1700
1701SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1702 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001703 Initialize(space->bottom(), space->top(), size_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704}
1705
1706
1707SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001708 Initialize(start, space->top(), NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709}
1710
1711
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001712SemiSpaceIterator::SemiSpaceIterator(Address from, Address to) {
1713 Initialize(from, to, NULL);
1714}
1715
1716
1717void SemiSpaceIterator::Initialize(Address start,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718 Address end,
1719 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001720 SemiSpace::AssertValidRange(start, end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 current_ = start;
1722 limit_ = end;
1723 size_func_ = size_func;
1724}
1725
1726
1727#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728// heap_histograms is shared, always clear it before using it.
1729static void ClearHistograms() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001730 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731 // We reset the name each time, though it hasn't changed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001732#define DEF_TYPE_NAME(name) isolate->heap_histograms()[name].set_name(#name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1734#undef DEF_TYPE_NAME
1735
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001736#define CLEAR_HISTOGRAM(name) isolate->heap_histograms()[name].clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1738#undef CLEAR_HISTOGRAM
1739
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001740 isolate->js_spill_information()->Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741}
1742
1743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744static void ClearCodeKindStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001745 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001747 isolate->code_kind_statistics()[i] = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 }
1749}
1750
1751
1752static void ReportCodeKindStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001753 Isolate* isolate = Isolate::Current();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001754 const char* table[Code::NUMBER_OF_KINDS] = { NULL };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755
1756#define CASE(name) \
1757 case Code::name: table[Code::name] = #name; \
1758 break
1759
1760 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1761 switch (static_cast<Code::Kind>(i)) {
1762 CASE(FUNCTION);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001763 CASE(OPTIMIZED_FUNCTION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 CASE(STUB);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001765 CASE(COMPILED_STUB);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 CASE(BUILTIN);
1767 CASE(LOAD_IC);
1768 CASE(KEYED_LOAD_IC);
1769 CASE(STORE_IC);
1770 CASE(KEYED_STORE_IC);
1771 CASE(CALL_IC);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001772 CASE(KEYED_CALL_IC);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001773 CASE(UNARY_OP_IC);
1774 CASE(BINARY_OP_IC);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001775 CASE(COMPARE_IC);
ricow@chromium.org9fa09672011-07-25 11:05:35 +00001776 CASE(TO_BOOLEAN_IC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 }
1778 }
1779
1780#undef CASE
1781
1782 PrintF("\n Code kind histograms: \n");
1783 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001784 if (isolate->code_kind_statistics()[i] > 0) {
1785 PrintF(" %-20s: %10d bytes\n", table[i],
1786 isolate->code_kind_statistics()[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 }
1788 }
1789 PrintF("\n");
1790}
1791
1792
1793static int CollectHistogramInfo(HeapObject* obj) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001794 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 InstanceType type = obj->map()->instance_type();
1796 ASSERT(0 <= type && type <= LAST_TYPE);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001797 ASSERT(isolate->heap_histograms()[type].name() != NULL);
1798 isolate->heap_histograms()[type].increment_number(1);
1799 isolate->heap_histograms()[type].increment_bytes(obj->Size());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800
1801 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001802 JSObject::cast(obj)->IncrementSpillStatistics(
1803 isolate->js_spill_information());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 }
1805
1806 return obj->Size();
1807}
1808
1809
1810static void ReportHistogram(bool print_spill) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001811 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 PrintF("\n Object Histogram:\n");
1813 for (int i = 0; i <= LAST_TYPE; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001814 if (isolate->heap_histograms()[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001815 PrintF(" %-34s%10d (%10d bytes)\n",
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001816 isolate->heap_histograms()[i].name(),
1817 isolate->heap_histograms()[i].number(),
1818 isolate->heap_histograms()[i].bytes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 }
1820 }
1821 PrintF("\n");
1822
1823 // Summarize string types.
1824 int string_number = 0;
1825 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001826#define INCREMENT(type, size, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001827 string_number += isolate->heap_histograms()[type].number(); \
1828 string_bytes += isolate->heap_histograms()[type].bytes();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 STRING_TYPE_LIST(INCREMENT)
1830#undef INCREMENT
1831 if (string_number > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001832 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 string_bytes);
1834 }
1835
1836 if (FLAG_collect_heap_spill_statistics && print_spill) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001837 isolate->js_spill_information()->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 }
1839}
1840#endif // DEBUG
1841
1842
1843// Support for statistics gathering for --heap-stats and --log-gc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844void NewSpace::ClearHistograms() {
1845 for (int i = 0; i <= LAST_TYPE; i++) {
1846 allocated_histogram_[i].clear();
1847 promoted_histogram_[i].clear();
1848 }
1849}
1850
1851// Because the copying collector does not touch garbage objects, we iterate
1852// the new space before a collection to get a histogram of allocated objects.
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001853// This only happens when --log-gc flag is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854void NewSpace::CollectStatistics() {
1855 ClearHistograms();
1856 SemiSpaceIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001857 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next())
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001858 RecordAllocation(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859}
1860
1861
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001862static void DoReportStatistics(Isolate* isolate,
1863 HistogramInfo* info, const char* description) {
1864 LOG(isolate, HeapSampleBeginEvent("NewSpace", description));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 // Lump all the string types together.
1866 int string_number = 0;
1867 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001868#define INCREMENT(type, size, name, camel_name) \
1869 string_number += info[type].number(); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 string_bytes += info[type].bytes();
1871 STRING_TYPE_LIST(INCREMENT)
1872#undef INCREMENT
1873 if (string_number > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001874 LOG(isolate,
1875 HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 }
1877
1878 // Then do the other types.
1879 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1880 if (info[i].number() > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001881 LOG(isolate,
1882 HeapSampleItemEvent(info[i].name(), info[i].number(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883 info[i].bytes()));
1884 }
1885 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001886 LOG(isolate, HeapSampleEndEvent("NewSpace", description));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888
1889
1890void NewSpace::ReportStatistics() {
1891#ifdef DEBUG
1892 if (FLAG_heap_stats) {
1893 float pct = static_cast<float>(Available()) / Capacity();
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001894 PrintF(" capacity: %" V8_PTR_PREFIX "d"
1895 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 Capacity(), Available(), static_cast<int>(pct*100));
1897 PrintF("\n Object Histogram:\n");
1898 for (int i = 0; i <= LAST_TYPE; i++) {
1899 if (allocated_histogram_[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001900 PrintF(" %-34s%10d (%10d bytes)\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001901 allocated_histogram_[i].name(),
1902 allocated_histogram_[i].number(),
1903 allocated_histogram_[i].bytes());
1904 }
1905 }
1906 PrintF("\n");
1907 }
1908#endif // DEBUG
1909
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 if (FLAG_log_gc) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001911 Isolate* isolate = ISOLATE;
1912 DoReportStatistics(isolate, allocated_histogram_, "allocated");
1913 DoReportStatistics(isolate, promoted_histogram_, "promoted");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915}
1916
1917
1918void NewSpace::RecordAllocation(HeapObject* obj) {
1919 InstanceType type = obj->map()->instance_type();
1920 ASSERT(0 <= type && type <= LAST_TYPE);
1921 allocated_histogram_[type].increment_number(1);
1922 allocated_histogram_[type].increment_bytes(obj->Size());
1923}
1924
1925
1926void NewSpace::RecordPromotion(HeapObject* obj) {
1927 InstanceType type = obj->map()->instance_type();
1928 ASSERT(0 <= type && type <= LAST_TYPE);
1929 promoted_histogram_[type].increment_number(1);
1930 promoted_histogram_[type].increment_bytes(obj->Size());
1931}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932
danno@chromium.org72204d52012-10-31 10:02:10 +00001933
1934size_t NewSpace::CommittedPhysicalMemory() {
1935 if (!VirtualMemory::HasLazyCommits()) return CommittedMemory();
1936 MemoryChunk::UpdateHighWaterMark(allocation_info_.top);
1937 size_t size = to_space_.CommittedPhysicalMemory();
1938 if (from_space_.is_committed()) {
1939 size += from_space_.CommittedPhysicalMemory();
1940 }
1941 return size;
1942}
1943
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944// -----------------------------------------------------------------------------
1945// Free lists for old object spaces implementation
1946
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001947void FreeListNode::set_size(Heap* heap, int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 ASSERT(size_in_bytes > 0);
1949 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1950
1951 // We write a map and possibly size information to the block. If the block
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001952 // is big enough to be a FreeSpace with at least one extra word (the next
1953 // pointer), we set its map to be the free space map and its size to an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 // appropriate array length for the desired size from HeapObject::Size().
1955 // If the block is too small (eg, one or two words), to hold both a size
1956 // field and a next pointer, we give it a filler map that gives it the
1957 // correct size.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001958 if (size_in_bytes > FreeSpace::kHeaderSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001959 set_map_no_write_barrier(heap->raw_unchecked_free_space_map());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001960 // Can't use FreeSpace::cast because it fails during deserialization.
1961 FreeSpace* this_as_free_space = reinterpret_cast<FreeSpace*>(this);
1962 this_as_free_space->set_size(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 } else if (size_in_bytes == kPointerSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001964 set_map_no_write_barrier(heap->raw_unchecked_one_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 } else if (size_in_bytes == 2 * kPointerSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001966 set_map_no_write_barrier(heap->raw_unchecked_two_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 } else {
1968 UNREACHABLE();
1969 }
ager@chromium.org3811b432009-10-28 14:53:37 +00001970 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001971 // deserialization because the free space map is not done yet.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972}
1973
1974
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001975FreeListNode* FreeListNode::next() {
ager@chromium.org3811b432009-10-28 14:53:37 +00001976 ASSERT(IsFreeListNode(this));
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001977 if (map() == GetHeap()->raw_unchecked_free_space_map()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001978 ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize);
1979 return reinterpret_cast<FreeListNode*>(
1980 Memory::Address_at(address() + kNextOffset));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001981 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001982 return reinterpret_cast<FreeListNode*>(
1983 Memory::Address_at(address() + kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001984 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985}
1986
1987
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001988FreeListNode** FreeListNode::next_address() {
ager@chromium.org3811b432009-10-28 14:53:37 +00001989 ASSERT(IsFreeListNode(this));
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001990 if (map() == GetHeap()->raw_unchecked_free_space_map()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001991 ASSERT(Size() >= kNextOffset + kPointerSize);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001992 return reinterpret_cast<FreeListNode**>(address() + kNextOffset);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001993 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001994 return reinterpret_cast<FreeListNode**>(address() + kPointerSize);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001995 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996}
1997
1998
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001999void FreeListNode::set_next(FreeListNode* next) {
2000 ASSERT(IsFreeListNode(this));
2001 // While we are booting the VM the free space map will actually be null. So
2002 // we have to make sure that we don't try to use it for anything at that
2003 // stage.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002004 if (map() == GetHeap()->raw_unchecked_free_space_map()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002005 ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize);
2006 Memory::Address_at(address() + kNextOffset) =
2007 reinterpret_cast<Address>(next);
2008 } else {
2009 Memory::Address_at(address() + kPointerSize) =
2010 reinterpret_cast<Address>(next);
2011 }
2012}
2013
2014
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002015intptr_t FreeListCategory::Concatenate(FreeListCategory* category) {
2016 intptr_t free_bytes = 0;
2017 if (category->top_ != NULL) {
2018 ASSERT(category->end_ != NULL);
2019 // This is safe (not going to deadlock) since Concatenate operations
2020 // are never performed on the same free lists at the same time in
2021 // reverse order.
2022 ScopedLock lock_target(mutex_);
2023 ScopedLock lock_source(category->mutex());
2024 free_bytes = category->available();
2025 if (end_ == NULL) {
2026 end_ = category->end();
2027 } else {
2028 category->end()->set_next(top_);
2029 }
2030 top_ = category->top();
2031 available_ += category->available();
2032 category->Reset();
2033 }
2034 return free_bytes;
2035}
2036
2037
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002038void FreeListCategory::Reset() {
2039 top_ = NULL;
2040 end_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 available_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042}
2043
2044
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002045intptr_t FreeListCategory::CountFreeListItemsInList(Page* p) {
2046 int sum = 0;
2047 FreeListNode* n = top_;
2048 while (n != NULL) {
2049 if (Page::FromAddress(n->address()) == p) {
2050 FreeSpace* free_space = reinterpret_cast<FreeSpace*>(n);
2051 sum += free_space->Size();
2052 }
2053 n = n->next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002055 return sum;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056}
2057
2058
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002059intptr_t FreeListCategory::EvictFreeListItemsInList(Page* p) {
2060 int sum = 0;
2061 FreeListNode** n = &top_;
2062 while (*n != NULL) {
2063 if (Page::FromAddress((*n)->address()) == p) {
2064 FreeSpace* free_space = reinterpret_cast<FreeSpace*>(*n);
2065 sum += free_space->Size();
2066 *n = (*n)->next();
2067 } else {
2068 n = (*n)->next_address();
2069 }
2070 }
2071 if (top_ == NULL) {
2072 end_ = NULL;
2073 }
2074 available_ -= sum;
2075 return sum;
2076}
2077
2078
2079FreeListNode* FreeListCategory::PickNodeFromList(int *node_size) {
2080 FreeListNode* node = top_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002082 if (node == NULL) return NULL;
2083
2084 while (node != NULL &&
2085 Page::FromAddress(node->address())->IsEvacuationCandidate()) {
2086 available_ -= node->Size();
2087 node = node->next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002089
2090 if (node != NULL) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002091 set_top(node->next());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002092 *node_size = node->Size();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002093 available_ -= *node_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 } else {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002095 set_top(NULL);
2096 }
2097
2098 if (top() == NULL) {
2099 set_end(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100 }
2101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 return node;
2103}
2104
2105
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002106void FreeListCategory::Free(FreeListNode* node, int size_in_bytes) {
2107 node->set_next(top_);
2108 top_ = node;
2109 if (end_ == NULL) {
2110 end_ = node;
2111 }
2112 available_ += size_in_bytes;
2113}
2114
2115
2116void FreeListCategory::RepairFreeList(Heap* heap) {
2117 FreeListNode* n = top_;
2118 while (n != NULL) {
2119 Map** map_location = reinterpret_cast<Map**>(n->address());
2120 if (*map_location == NULL) {
2121 *map_location = heap->free_space_map();
2122 } else {
2123 ASSERT(*map_location == heap->free_space_map());
2124 }
2125 n = n->next();
2126 }
2127}
2128
2129
2130FreeList::FreeList(PagedSpace* owner)
2131 : owner_(owner), heap_(owner->heap()) {
2132 Reset();
2133}
2134
2135
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002136intptr_t FreeList::Concatenate(FreeList* free_list) {
2137 intptr_t free_bytes = 0;
2138 free_bytes += small_list_.Concatenate(free_list->small_list());
2139 free_bytes += medium_list_.Concatenate(free_list->medium_list());
2140 free_bytes += large_list_.Concatenate(free_list->large_list());
2141 free_bytes += huge_list_.Concatenate(free_list->huge_list());
2142 return free_bytes;
2143}
2144
2145
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002146void FreeList::Reset() {
2147 small_list_.Reset();
2148 medium_list_.Reset();
2149 large_list_.Reset();
2150 huge_list_.Reset();
2151}
2152
2153
2154int FreeList::Free(Address start, int size_in_bytes) {
2155 if (size_in_bytes == 0) return 0;
2156
2157 FreeListNode* node = FreeListNode::FromAddress(start);
2158 node->set_size(heap_, size_in_bytes);
2159
2160 // Early return to drop too-small blocks on the floor.
2161 if (size_in_bytes < kSmallListMin) return size_in_bytes;
2162
2163 // Insert other blocks at the head of a free list of the appropriate
2164 // magnitude.
2165 if (size_in_bytes <= kSmallListMax) {
2166 small_list_.Free(node, size_in_bytes);
2167 } else if (size_in_bytes <= kMediumListMax) {
2168 medium_list_.Free(node, size_in_bytes);
2169 } else if (size_in_bytes <= kLargeListMax) {
2170 large_list_.Free(node, size_in_bytes);
2171 } else {
2172 huge_list_.Free(node, size_in_bytes);
2173 }
2174
2175 ASSERT(IsVeryLong() || available() == SumFreeLists());
2176 return 0;
2177}
2178
2179
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002180FreeListNode* FreeList::FindNodeFor(int size_in_bytes, int* node_size) {
2181 FreeListNode* node = NULL;
2182
2183 if (size_in_bytes <= kSmallAllocationMax) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002184 node = small_list_.PickNodeFromList(node_size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002185 if (node != NULL) return node;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002186 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002187
2188 if (size_in_bytes <= kMediumAllocationMax) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002189 node = medium_list_.PickNodeFromList(node_size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002190 if (node != NULL) return node;
2191 }
2192
2193 if (size_in_bytes <= kLargeAllocationMax) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002194 node = large_list_.PickNodeFromList(node_size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002195 if (node != NULL) return node;
2196 }
2197
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002198 int huge_list_available = huge_list_.available();
2199 for (FreeListNode** cur = huge_list_.GetTopAddress();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002200 *cur != NULL;
2201 cur = (*cur)->next_address()) {
2202 FreeListNode* cur_node = *cur;
2203 while (cur_node != NULL &&
2204 Page::FromAddress(cur_node->address())->IsEvacuationCandidate()) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002205 huge_list_available -= reinterpret_cast<FreeSpace*>(cur_node)->Size();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002206 cur_node = cur_node->next();
2207 }
2208
2209 *cur = cur_node;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002210 if (cur_node == NULL) {
2211 huge_list_.set_end(NULL);
2212 break;
2213 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002214
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002215 ASSERT((*cur)->map() == heap_->raw_unchecked_free_space_map());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002216 FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(*cur);
2217 int size = cur_as_free_space->Size();
2218 if (size >= size_in_bytes) {
2219 // Large enough node found. Unlink it from the list.
2220 node = *cur;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002221 *cur = node->next();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002222 *node_size = size;
2223 huge_list_available -= size;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002224 break;
2225 }
2226 }
2227
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002228 if (huge_list_.top() == NULL) {
2229 huge_list_.set_end(NULL);
2230 }
2231
2232 huge_list_.set_available(huge_list_available);
2233 ASSERT(IsVeryLong() || available() == SumFreeLists());
2234
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002235 return node;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002236}
2237
2238
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002239// Allocation on the old space free list. If it succeeds then a new linear
2240// allocation space has been set up with the top and limit of the space. If
2241// the allocation fails then NULL is returned, and the caller can perform a GC
2242// or allocate a new page before retrying.
2243HeapObject* FreeList::Allocate(int size_in_bytes) {
2244 ASSERT(0 < size_in_bytes);
2245 ASSERT(size_in_bytes <= kMaxBlockSize);
2246 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2247 // Don't free list allocate if there is linear space available.
2248 ASSERT(owner_->limit() - owner_->top() < size_in_bytes);
2249
2250 int new_node_size = 0;
2251 FreeListNode* new_node = FindNodeFor(size_in_bytes, &new_node_size);
2252 if (new_node == NULL) return NULL;
2253
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002254
2255 int bytes_left = new_node_size - size_in_bytes;
2256 ASSERT(bytes_left >= 0);
2257
2258 int old_linear_size = static_cast<int>(owner_->limit() - owner_->top());
2259 // Mark the old linear allocation area with a free space map so it can be
2260 // skipped when scanning the heap. This also puts it back in the free list
2261 // if it is big enough.
2262 owner_->Free(owner_->top(), old_linear_size);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002263
danno@chromium.orgeb831462012-08-24 11:57:08 +00002264 owner_->heap()->incremental_marking()->OldSpaceStep(
2265 size_in_bytes - old_linear_size);
2266
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002267#ifdef DEBUG
2268 for (int i = 0; i < size_in_bytes / kPointerSize; i++) {
2269 reinterpret_cast<Object**>(new_node->address())[i] =
2270 Smi::FromInt(kCodeZapValue);
2271 }
2272#endif
2273
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +00002274 // The old-space-step might have finished sweeping and restarted marking.
2275 // Verify that it did not turn the page of the new node into an evacuation
2276 // candidate.
2277 ASSERT(!MarkCompactCollector::IsOnEvacuationCandidate(new_node));
2278
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002279 const int kThreshold = IncrementalMarking::kAllocatedThreshold;
2280
2281 // Memory in the linear allocation area is counted as allocated. We may free
2282 // a little of this again immediately - see below.
2283 owner_->Allocate(new_node_size);
2284
2285 if (bytes_left > kThreshold &&
2286 owner_->heap()->incremental_marking()->IsMarkingIncomplete() &&
2287 FLAG_incremental_marking_steps) {
2288 int linear_size = owner_->RoundSizeDownToObjectAlignment(kThreshold);
2289 // We don't want to give too large linear areas to the allocator while
2290 // incremental marking is going on, because we won't check again whether
2291 // we want to do another increment until the linear area is used up.
2292 owner_->Free(new_node->address() + size_in_bytes + linear_size,
2293 new_node_size - size_in_bytes - linear_size);
2294 owner_->SetTop(new_node->address() + size_in_bytes,
2295 new_node->address() + size_in_bytes + linear_size);
2296 } else if (bytes_left > 0) {
2297 // Normally we give the rest of the node to the allocator as its new
2298 // linear allocation area.
2299 owner_->SetTop(new_node->address() + size_in_bytes,
2300 new_node->address() + new_node_size);
2301 } else {
2302 // TODO(gc) Try not freeing linear allocation region when bytes_left
2303 // are zero.
2304 owner_->SetTop(NULL, NULL);
2305 }
2306
2307 return new_node;
2308}
2309
2310
danno@chromium.org2c456792011-11-11 12:00:53 +00002311void FreeList::CountFreeListItems(Page* p, SizeStats* sizes) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002312 sizes->huge_size_ = huge_list_.CountFreeListItemsInList(p);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002313 if (sizes->huge_size_ < p->area_size()) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002314 sizes->small_size_ = small_list_.CountFreeListItemsInList(p);
2315 sizes->medium_size_ = medium_list_.CountFreeListItemsInList(p);
2316 sizes->large_size_ = large_list_.CountFreeListItemsInList(p);
danno@chromium.org2c456792011-11-11 12:00:53 +00002317 } else {
2318 sizes->small_size_ = 0;
2319 sizes->medium_size_ = 0;
2320 sizes->large_size_ = 0;
2321 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002322}
2323
danno@chromium.org2c456792011-11-11 12:00:53 +00002324
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002325intptr_t FreeList::EvictFreeListItems(Page* p) {
2326 intptr_t sum = huge_list_.EvictFreeListItemsInList(p);
2327
2328 if (sum < p->area_size()) {
2329 sum += small_list_.EvictFreeListItemsInList(p) +
2330 medium_list_.EvictFreeListItemsInList(p) +
2331 large_list_.EvictFreeListItemsInList(p);
danno@chromium.org2c456792011-11-11 12:00:53 +00002332 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002333
danno@chromium.org2c456792011-11-11 12:00:53 +00002334 return sum;
2335}
2336
2337
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002338void FreeList::RepairLists(Heap* heap) {
2339 small_list_.RepairFreeList(heap);
2340 medium_list_.RepairFreeList(heap);
2341 large_list_.RepairFreeList(heap);
2342 huge_list_.RepairFreeList(heap);
danno@chromium.org2c456792011-11-11 12:00:53 +00002343}
2344
2345
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002346#ifdef DEBUG
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002347intptr_t FreeListCategory::SumFreeList() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002348 intptr_t sum = 0;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002349 FreeListNode* cur = top_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002350 while (cur != NULL) {
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002351 ASSERT(cur->map() == cur->GetHeap()->raw_unchecked_free_space_map());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002352 FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(cur);
2353 sum += cur_as_free_space->Size();
2354 cur = cur->next();
2355 }
2356 return sum;
2357}
2358
2359
2360static const int kVeryLongFreeList = 500;
2361
2362
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002363int FreeListCategory::FreeListLength() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002364 int length = 0;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002365 FreeListNode* cur = top_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002366 while (cur != NULL) {
2367 length++;
2368 cur = cur->next();
2369 if (length == kVeryLongFreeList) return length;
2370 }
2371 return length;
2372}
2373
2374
2375bool FreeList::IsVeryLong() {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002376 if (small_list_.FreeListLength() == kVeryLongFreeList) return true;
2377 if (medium_list_.FreeListLength() == kVeryLongFreeList) return true;
2378 if (large_list_.FreeListLength() == kVeryLongFreeList) return true;
2379 if (huge_list_.FreeListLength() == kVeryLongFreeList) return true;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002380 return false;
2381}
2382
2383
2384// This can take a very long time because it is linear in the number of entries
2385// on the free list, so it should not be called if FreeListLength returns
2386// kVeryLongFreeList.
2387intptr_t FreeList::SumFreeLists() {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002388 intptr_t sum = small_list_.SumFreeList();
2389 sum += medium_list_.SumFreeList();
2390 sum += large_list_.SumFreeList();
2391 sum += huge_list_.SumFreeList();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002392 return sum;
2393}
2394#endif
2395
2396
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397// -----------------------------------------------------------------------------
2398// OldSpace implementation
2399
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002400bool NewSpace::ReserveSpace(int bytes) {
2401 // We can't reliably unpack a partial snapshot that needs more new space
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002402 // space than the minimum NewSpace size. The limit can be set lower than
2403 // the end of new space either because there is more space on the next page
2404 // or because we have lowered the limit in order to get periodic incremental
2405 // marking. The most reliable way to ensure that there is linear space is
2406 // to do the allocation, then rewind the limit.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002407 ASSERT(bytes <= InitialCapacity());
danno@chromium.orgc612e022011-11-10 11:38:15 +00002408 MaybeObject* maybe = AllocateRaw(bytes);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002409 Object* object = NULL;
2410 if (!maybe->ToObject(&object)) return false;
2411 HeapObject* allocation = HeapObject::cast(object);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002412 Address top = allocation_info_.top;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002413 if ((top - bytes) == allocation->address()) {
2414 allocation_info_.top = allocation->address();
2415 return true;
2416 }
2417 // There may be a borderline case here where the allocation succeeded, but
2418 // the limit and top have moved on to a new page. In that case we try again.
2419 return ReserveSpace(bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002420}
2421
2422
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002423void PagedSpace::PrepareForMarkCompact() {
2424 // We don't have a linear allocation area while sweeping. It will be restored
2425 // on the first allocation after the sweep.
2426 // Mark the old linear allocation area with a free space map so it can be
2427 // skipped when scanning the heap.
2428 int old_linear_size = static_cast<int>(limit() - top());
2429 Free(top(), old_linear_size);
2430 SetTop(NULL, NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002431
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002432 // Stop lazy sweeping and clear marking bits for unswept pages.
2433 if (first_unswept_page_ != NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002434 Page* p = first_unswept_page_;
2435 do {
2436 // Do not use ShouldBeSweptLazily predicate here.
2437 // New evacuation candidates were selected but they still have
2438 // to be swept before collection starts.
2439 if (!p->WasSwept()) {
2440 Bitmap::Clear(p);
2441 if (FLAG_gc_verbose) {
2442 PrintF("Sweeping 0x%" V8PRIxPTR " lazily abandoned.\n",
2443 reinterpret_cast<intptr_t>(p));
2444 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002445 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002446 p = p->next_page();
danno@chromium.org2c456792011-11-11 12:00:53 +00002447 } while (p != anchor());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002448 }
danno@chromium.org2c456792011-11-11 12:00:53 +00002449 first_unswept_page_ = Page::FromAddress(NULL);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002450 unswept_free_bytes_ = 0;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002451
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002452 // Clear the free list before a full GC---it will be rebuilt afterward.
2453 free_list_.Reset();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002454}
2455
2456
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002457bool PagedSpace::ReserveSpace(int size_in_bytes) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002458 ASSERT(size_in_bytes <= AreaSize());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002459 ASSERT(size_in_bytes == RoundSizeDownToObjectAlignment(size_in_bytes));
2460 Address current_top = allocation_info_.top;
2461 Address new_top = current_top + size_in_bytes;
2462 if (new_top <= allocation_info_.limit) return true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002463
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002464 HeapObject* new_area = free_list_.Allocate(size_in_bytes);
2465 if (new_area == NULL) new_area = SlowAllocateRaw(size_in_bytes);
2466 if (new_area == NULL) return false;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002467
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002468 int old_linear_size = static_cast<int>(limit() - top());
2469 // Mark the old linear allocation area with a free space so it can be
2470 // skipped when scanning the heap. This also puts it back in the free list
2471 // if it is big enough.
2472 Free(top(), old_linear_size);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002473
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002474 SetTop(new_area->address(), new_area->address() + size_in_bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002475 return true;
2476}
2477
2478
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002479// After we have booted, we have created a map which represents free space
2480// on the heap. If there was already a free list then the elements on it
2481// were created with the wrong FreeSpaceMap (normally NULL), so we need to
2482// fix them.
2483void PagedSpace::RepairFreeListsAfterBoot() {
2484 free_list_.RepairLists(heap());
2485}
2486
2487
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002488// You have to call this last, since the implementation from PagedSpace
2489// doesn't know that memory was 'promised' to large object space.
2490bool LargeObjectSpace::ReserveSpace(int bytes) {
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00002491 return heap()->OldGenerationCapacityAvailable() >= bytes &&
2492 (!heap()->incremental_marking()->IsStopped() ||
2493 heap()->OldGenerationSpaceAvailable() >= bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002494}
2495
2496
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002497bool PagedSpace::AdvanceSweeper(intptr_t bytes_to_sweep) {
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002498 if (IsLazySweepingComplete()) return true;
kasper.lund7276f142008-07-30 08:49:36 +00002499
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002500 intptr_t freed_bytes = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002501 Page* p = first_unswept_page_;
2502 do {
2503 Page* next_page = p->next_page();
2504 if (ShouldBeSweptLazily(p)) {
2505 if (FLAG_gc_verbose) {
2506 PrintF("Sweeping 0x%" V8PRIxPTR " lazily advanced.\n",
2507 reinterpret_cast<intptr_t>(p));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002508 }
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002509 DecreaseUnsweptFreeBytes(p);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002510 freed_bytes +=
2511 MarkCompactCollector::
2512 SweepConservatively<MarkCompactCollector::SWEEP_SEQUENTIALLY>(
2513 this, NULL, p);
ager@chromium.org3811b432009-10-28 14:53:37 +00002514 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002515 p = next_page;
danno@chromium.org2c456792011-11-11 12:00:53 +00002516 } while (p != anchor() && freed_bytes < bytes_to_sweep);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002517
danno@chromium.org2c456792011-11-11 12:00:53 +00002518 if (p == anchor()) {
2519 first_unswept_page_ = Page::FromAddress(NULL);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002520 } else {
2521 first_unswept_page_ = p;
kasper.lund7276f142008-07-30 08:49:36 +00002522 }
2523
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002524 heap()->FreeQueuedChunks();
2525
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002526 return IsLazySweepingComplete();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002527}
2528
2529
2530void PagedSpace::EvictEvacuationCandidatesFromFreeLists() {
2531 if (allocation_info_.top >= allocation_info_.limit) return;
2532
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002533 if (Page::FromAllocationTop(allocation_info_.top)->IsEvacuationCandidate()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002534 // Create filler object to keep page iterable if it was iterable.
2535 int remaining =
2536 static_cast<int>(allocation_info_.limit - allocation_info_.top);
2537 heap()->CreateFillerObjectAt(allocation_info_.top, remaining);
2538
2539 allocation_info_.top = NULL;
2540 allocation_info_.limit = NULL;
2541 }
2542}
2543
2544
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002545bool PagedSpace::EnsureSweeperProgress(intptr_t size_in_bytes) {
2546 MarkCompactCollector* collector = heap()->mark_compact_collector();
2547 if (collector->AreSweeperThreadsActivated()) {
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002548 if (FLAG_concurrent_sweeping) {
2549 if (collector->StealMemoryFromSweeperThreads(this) < size_in_bytes) {
2550 collector->WaitUntilSweepingCompleted();
ulan@chromium.org2e04b582013-02-21 14:06:02 +00002551 collector->FinalizeSweeping();
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002552 return true;
2553 }
2554 return false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002555 }
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002556 return true;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002557 } else {
2558 return AdvanceSweeper(size_in_bytes);
2559 }
2560}
2561
2562
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002563HeapObject* PagedSpace::SlowAllocateRaw(int size_in_bytes) {
2564 // Allocation in this space has failed.
2565
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +00002566 // If there are unswept pages advance lazy sweeper a bounded number of times
2567 // until we find a size_in_bytes contiguous piece of memory
2568 const int kMaxSweepingTries = 5;
2569 bool sweeping_complete = false;
2570
2571 for (int i = 0; i < kMaxSweepingTries && !sweeping_complete; i++) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002572 sweeping_complete = EnsureSweeperProgress(size_in_bytes);
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002573
2574 // Retry the free list allocation.
2575 HeapObject* object = free_list_.Allocate(size_in_bytes);
2576 if (object != NULL) return object;
2577 }
2578
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002579 // Free list allocation failed and there is no next page. Fail if we have
2580 // hit the old generation size limit that should cause a garbage
2581 // collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002582 if (!heap()->always_allocate() &&
2583 heap()->OldGenerationAllocationLimitReached()) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002584 return NULL;
2585 }
2586
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002587 // Try to expand the space and allocate in the new next page.
2588 if (Expand()) {
2589 return free_list_.Allocate(size_in_bytes);
2590 }
2591
2592 // Last ditch, sweep all the remaining pages to try to find space. This may
2593 // cause a pause.
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002594 if (!IsLazySweepingComplete()) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002595 EnsureSweeperProgress(kMaxInt);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002596
2597 // Retry the free list allocation.
2598 HeapObject* object = free_list_.Allocate(size_in_bytes);
2599 if (object != NULL) return object;
kasper.lund7276f142008-07-30 08:49:36 +00002600 }
2601
2602 // Finally, fail.
2603 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604}
2605
2606
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608void PagedSpace::ReportCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002609 Isolate* isolate = Isolate::Current();
2610 CommentStatistic* comments_statistics =
2611 isolate->paged_space_comments_statistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 ReportCodeKindStatistics();
2613 PrintF("Code comment statistics (\" [ comment-txt : size/ "
2614 "count (average)\"):\n");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002615 for (int i = 0; i <= CommentStatistic::kMaxComments; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 const CommentStatistic& cs = comments_statistics[i];
2617 if (cs.size > 0) {
2618 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
2619 cs.size/cs.count);
2620 }
2621 }
2622 PrintF("\n");
2623}
2624
2625
2626void PagedSpace::ResetCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002627 Isolate* isolate = Isolate::Current();
2628 CommentStatistic* comments_statistics =
2629 isolate->paged_space_comments_statistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 ClearCodeKindStatistics();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002631 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
2632 comments_statistics[i].Clear();
2633 }
2634 comments_statistics[CommentStatistic::kMaxComments].comment = "Unknown";
2635 comments_statistics[CommentStatistic::kMaxComments].size = 0;
2636 comments_statistics[CommentStatistic::kMaxComments].count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637}
2638
2639
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002640// Adds comment to 'comment_statistics' table. Performance OK as long as
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641// 'kMaxComments' is small
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002642static void EnterComment(Isolate* isolate, const char* comment, int delta) {
2643 CommentStatistic* comments_statistics =
2644 isolate->paged_space_comments_statistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 // Do not count empty comments
2646 if (delta <= 0) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002647 CommentStatistic* cs = &comments_statistics[CommentStatistic::kMaxComments];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 // Search for a free or matching entry in 'comments_statistics': 'cs'
2649 // points to result.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002650 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 if (comments_statistics[i].comment == NULL) {
2652 cs = &comments_statistics[i];
2653 cs->comment = comment;
2654 break;
2655 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2656 cs = &comments_statistics[i];
2657 break;
2658 }
2659 }
2660 // Update entry for 'comment'
2661 cs->size += delta;
2662 cs->count += 1;
2663}
2664
2665
2666// Call for each nested comment start (start marked with '[ xxx', end marked
2667// with ']'. RelocIterator 'it' must point to a comment reloc info.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002668static void CollectCommentStatistics(Isolate* isolate, RelocIterator* it) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002670 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2672 if (tmp[0] != '[') {
2673 // Not a nested comment; skip
2674 return;
2675 }
2676
2677 // Search for end of nested comment or a new nested comment
2678 const char* const comment_txt =
2679 reinterpret_cast<const char*>(it->rinfo()->data());
2680 const byte* prev_pc = it->rinfo()->pc();
2681 int flat_delta = 0;
2682 it->next();
2683 while (true) {
2684 // All nested comments must be terminated properly, and therefore exit
2685 // from loop.
2686 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002687 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 const char* const txt =
2689 reinterpret_cast<const char*>(it->rinfo()->data());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002690 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 if (txt[0] == ']') break; // End of nested comment
2692 // A new comment
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002693 CollectCommentStatistics(isolate, it);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 // Skip code that was covered with previous comment
2695 prev_pc = it->rinfo()->pc();
2696 }
2697 it->next();
2698 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002699 EnterComment(isolate, comment_txt, flat_delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700}
2701
2702
2703// Collects code size statistics:
2704// - by code kind
2705// - by code comment
2706void PagedSpace::CollectCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002707 Isolate* isolate = heap()->isolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 HeapObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002709 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 if (obj->IsCode()) {
2711 Code* code = Code::cast(obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002712 isolate->code_kind_statistics()[code->kind()] += code->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 RelocIterator it(code);
2714 int delta = 0;
2715 const byte* prev_pc = code->instruction_start();
2716 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002717 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002718 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002719 CollectCommentStatistics(isolate, &it);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720 prev_pc = it.rinfo()->pc();
2721 }
2722 it.next();
2723 }
2724
2725 ASSERT(code->instruction_start() <= prev_pc &&
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002726 prev_pc <= code->instruction_end());
2727 delta += static_cast<int>(code->instruction_end() - prev_pc);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002728 EnterComment(isolate, "NoComment", delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 }
2730 }
2731}
2732
2733
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002734void PagedSpace::ReportStatistics() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002735 int pct = static_cast<int>(Available() * 100 / Capacity());
2736 PrintF(" capacity: %" V8_PTR_PREFIX "d"
2737 ", waste: %" V8_PTR_PREFIX "d"
2738 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739 Capacity(), Waste(), Available(), pct);
2740
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002741 if (was_swept_conservatively_) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002742 ClearHistograms();
2743 HeapObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002744 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next())
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002745 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746 ReportHistogram(true);
2747}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748#endif
2749
2750// -----------------------------------------------------------------------------
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002751// FixedSpace implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002753void FixedSpace::PrepareForMarkCompact() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002754 // Call prepare of the super class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002755 PagedSpace::PrepareForMarkCompact();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002756
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002757 // During a non-compacting collection, everything below the linear
2758 // allocation pointer except wasted top-of-page blocks is considered
2759 // allocated and we will rediscover available bytes during the
2760 // collection.
2761 accounting_stats_.AllocateBytes(free_list_.available());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762
kasper.lund7276f142008-07-30 08:49:36 +00002763 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765}
2766
2767
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002768// -----------------------------------------------------------------------------
2769// MapSpace implementation
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002770// TODO(mvstanton): this is weird...the compiler can't make a vtable unless
2771// there is at least one non-inlined virtual function. I would prefer to hide
2772// the VerifyObject definition behind VERIFY_HEAP.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002773
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002774void MapSpace::VerifyObject(HeapObject* object) {
2775 // The object should be a map or a free-list node.
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002776 CHECK(object->IsMap() || object->IsFreeSpace());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002777}
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002778
2779
2780// -----------------------------------------------------------------------------
2781// GlobalPropertyCellSpace implementation
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002782// TODO(mvstanton): this is weird...the compiler can't make a vtable unless
2783// there is at least one non-inlined virtual function. I would prefer to hide
2784// the VerifyObject definition behind VERIFY_HEAP.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002785
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002786void CellSpace::VerifyObject(HeapObject* object) {
2787 // The object should be a global object property cell or a free-list node.
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002788 CHECK(object->IsJSGlobalPropertyCell() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002789 object->map() == heap()->two_pointer_filler_map());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002790}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791
2792
2793// -----------------------------------------------------------------------------
2794// LargeObjectIterator
2795
2796LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002797 current_ = space->first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 size_func_ = NULL;
2799}
2800
2801
2802LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2803 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002804 current_ = space->first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805 size_func_ = size_func;
2806}
2807
2808
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002809HeapObject* LargeObjectIterator::Next() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002810 if (current_ == NULL) return NULL;
2811
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812 HeapObject* object = current_->GetObject();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002813 current_ = current_->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002814 return object;
2815}
2816
2817
2818// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819// LargeObjectSpace
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002820static bool ComparePointers(void* key1, void* key2) {
2821 return key1 == key2;
2822}
2823
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002825LargeObjectSpace::LargeObjectSpace(Heap* heap,
2826 intptr_t max_capacity,
2827 AllocationSpace id)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002828 : Space(heap, id, NOT_EXECUTABLE), // Managed on a per-allocation basis
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002829 max_capacity_(max_capacity),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002830 first_page_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 size_(0),
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002832 page_count_(0),
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002833 objects_size_(0),
2834 chunk_map_(ComparePointers, 1024) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835
2836
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002837bool LargeObjectSpace::SetUp() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002838 first_page_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839 size_ = 0;
2840 page_count_ = 0;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002841 objects_size_ = 0;
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002842 chunk_map_.Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843 return true;
2844}
2845
2846
2847void LargeObjectSpace::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002848 while (first_page_ != NULL) {
2849 LargePage* page = first_page_;
2850 first_page_ = first_page_->next_page();
2851 LOG(heap()->isolate(), DeleteEvent("LargeObjectChunk", page->address()));
2852
2853 ObjectSpace space = static_cast<ObjectSpace>(1 << identity());
2854 heap()->isolate()->memory_allocator()->PerformAllocationCallback(
2855 space, kAllocationActionFree, page->size());
2856 heap()->isolate()->memory_allocator()->Free(page);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002857 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002858 SetUp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002859}
2860
2861
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002862MaybeObject* LargeObjectSpace::AllocateRaw(int object_size,
2863 Executability executable) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002864 // Check if we want to force a GC before growing the old space further.
2865 // If so, fail the allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002866 if (!heap()->always_allocate() &&
2867 heap()->OldGenerationAllocationLimitReached()) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002868 return Failure::RetryAfterGC(identity());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002869 }
2870
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002871 if (Size() + object_size > max_capacity_) {
2872 return Failure::RetryAfterGC(identity());
2873 }
2874
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002875 LargePage* page = heap()->isolate()->memory_allocator()->
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002876 AllocateLargePage(object_size, this, executable);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002877 if (page == NULL) return Failure::RetryAfterGC(identity());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002878 ASSERT(page->area_size() >= object_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002880 size_ += static_cast<int>(page->size());
2881 objects_size_ += object_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002882 page_count_++;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002883 page->set_next_page(first_page_);
2884 first_page_ = page;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002886 // Register all MemoryChunk::kAlignment-aligned chunks covered by
2887 // this large page in the chunk map.
2888 uintptr_t base = reinterpret_cast<uintptr_t>(page) / MemoryChunk::kAlignment;
2889 uintptr_t limit = base + (page->size() - 1) / MemoryChunk::kAlignment;
2890 for (uintptr_t key = base; key <= limit; key++) {
2891 HashMap::Entry* entry = chunk_map_.Lookup(reinterpret_cast<void*>(key),
2892 static_cast<uint32_t>(key),
2893 true);
2894 ASSERT(entry != NULL);
2895 entry->value = page;
2896 }
2897
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002898 HeapObject* object = page->GetObject();
2899
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002900 if (Heap::ShouldZapGarbage()) {
2901 // Make the object consistent so the heap can be verified in OldSpaceStep.
2902 // We only need to do this in debug builds or if verify_heap is on.
2903 reinterpret_cast<Object**>(object->address())[0] =
2904 heap()->fixed_array_map();
2905 reinterpret_cast<Object**>(object->address())[1] = Smi::FromInt(0);
2906 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002907
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002908 heap()->incremental_marking()->OldSpaceStep(object_size);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002909 return object;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910}
2911
2912
danno@chromium.org72204d52012-10-31 10:02:10 +00002913size_t LargeObjectSpace::CommittedPhysicalMemory() {
2914 if (!VirtualMemory::HasLazyCommits()) return CommittedMemory();
2915 size_t size = 0;
2916 LargePage* current = first_page_;
2917 while (current != NULL) {
2918 size += current->CommittedPhysicalMemory();
2919 current = current->next_page();
2920 }
2921 return size;
2922}
2923
2924
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925// GC support
lrn@chromium.org303ada72010-10-27 09:33:13 +00002926MaybeObject* LargeObjectSpace::FindObject(Address a) {
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002927 LargePage* page = FindPage(a);
2928 if (page != NULL) {
2929 return page->GetObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930 }
2931 return Failure::Exception();
2932}
2933
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002934
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002935LargePage* LargeObjectSpace::FindPage(Address a) {
2936 uintptr_t key = reinterpret_cast<uintptr_t>(a) / MemoryChunk::kAlignment;
2937 HashMap::Entry* e = chunk_map_.Lookup(reinterpret_cast<void*>(key),
2938 static_cast<uint32_t>(key),
2939 false);
2940 if (e != NULL) {
2941 ASSERT(e->value != NULL);
2942 LargePage* page = reinterpret_cast<LargePage*>(e->value);
2943 ASSERT(page->is_valid());
2944 if (page->Contains(a)) {
2945 return page;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002946 }
2947 }
2948 return NULL;
2949}
2950
2951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952void LargeObjectSpace::FreeUnmarkedObjects() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002953 LargePage* previous = NULL;
2954 LargePage* current = first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955 while (current != NULL) {
2956 HeapObject* object = current->GetObject();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002957 // Can this large page contain pointers to non-trivial objects. No other
2958 // pointer object is this big.
2959 bool is_pointer_object = object->IsFixedArray();
2960 MarkBit mark_bit = Marking::MarkBitFrom(object);
2961 if (mark_bit.Get()) {
2962 mark_bit.Clear();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002963 Page::FromAddress(object->address())->ResetProgressBar();
2964 Page::FromAddress(object->address())->ResetLiveBytes();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965 previous = current;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002966 current = current->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002968 LargePage* page = current;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969 // Cut the chunk out from the chunk list.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002970 current = current->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002971 if (previous == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002972 first_page_ = current;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002974 previous->set_next_page(current);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975 }
2976
2977 // Free the chunk.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002978 heap()->mark_compact_collector()->ReportDeleteIfNeeded(
2979 object, heap()->isolate());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002980 size_ -= static_cast<int>(page->size());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002981 objects_size_ -= object->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 page_count_--;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002983
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002984 // Remove entries belonging to this page.
2985 // Use variable alignment to help pass length check (<= 80 characters)
2986 // of single line in tools/presubmit.py.
2987 const intptr_t alignment = MemoryChunk::kAlignment;
2988 uintptr_t base = reinterpret_cast<uintptr_t>(page)/alignment;
2989 uintptr_t limit = base + (page->size()-1)/alignment;
2990 for (uintptr_t key = base; key <= limit; key++) {
2991 chunk_map_.Remove(reinterpret_cast<void*>(key),
2992 static_cast<uint32_t>(key));
2993 }
2994
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002995 if (is_pointer_object) {
2996 heap()->QueueMemoryChunkForFree(page);
2997 } else {
2998 heap()->isolate()->memory_allocator()->Free(page);
2999 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000 }
3001 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003002 heap()->FreeQueuedChunks();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003}
3004
3005
3006bool LargeObjectSpace::Contains(HeapObject* object) {
3007 Address address = object->address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003008 MemoryChunk* chunk = MemoryChunk::FromAddress(address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003010 bool owned = (chunk->owner() == this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003012 SLOW_ASSERT(!owned || !FindObject(address)->IsFailure());
3013
3014 return owned;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015}
3016
3017
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003018#ifdef VERIFY_HEAP
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003019// We do not assume that the large object iterator works, because it depends
3020// on the invariants we are checking during verification.
3021void LargeObjectSpace::Verify() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003022 for (LargePage* chunk = first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 chunk != NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003024 chunk = chunk->next_page()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003025 // Each chunk contains an object that starts at the large object page's
3026 // object area start.
3027 HeapObject* object = chunk->GetObject();
3028 Page* page = Page::FromAddress(object->address());
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003029 CHECK(object->address() == page->area_start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003030
3031 // The first word should be a map, and we expect all map pointers to be
3032 // in map space.
3033 Map* map = object->map();
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003034 CHECK(map->IsMap());
3035 CHECK(heap()->map_space()->Contains(map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036
ager@chromium.orga1645e22009-09-09 19:27:10 +00003037 // We have only code, sequential strings, external strings
3038 // (sequential strings that have been morphed into external
3039 // strings), fixed arrays, and byte arrays in large object space.
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003040 CHECK(object->IsCode() || object->IsSeqString() ||
ager@chromium.orga1645e22009-09-09 19:27:10 +00003041 object->IsExternalString() || object->IsFixedArray() ||
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003042 object->IsFixedDoubleArray() || object->IsByteArray());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003043
3044 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003045 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046
3047 // Byte arrays and strings don't have interior pointers.
3048 if (object->IsCode()) {
3049 VerifyPointersVisitor code_visitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 object->IterateBody(map->instance_type(),
3051 object->Size(),
3052 &code_visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053 } else if (object->IsFixedArray()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054 FixedArray* array = FixedArray::cast(object);
3055 for (int j = 0; j < array->length(); j++) {
3056 Object* element = array->get(j);
3057 if (element->IsHeapObject()) {
3058 HeapObject* element_object = HeapObject::cast(element);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003059 CHECK(heap()->Contains(element_object));
3060 CHECK(element_object->map()->IsMap());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061 }
3062 }
3063 }
3064 }
3065}
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003066#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003067
3068
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003069#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003070void LargeObjectSpace::Print() {
3071 LargeObjectIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003072 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003073 obj->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003074 }
3075}
3076
3077
3078void LargeObjectSpace::ReportStatistics() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00003079 PrintF(" size: %" V8_PTR_PREFIX "d\n", size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080 int num_objects = 0;
3081 ClearHistograms();
3082 LargeObjectIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003083 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 num_objects++;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003085 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086 }
3087
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003088 PrintF(" number of objects %d, "
3089 "size of objects %" V8_PTR_PREFIX "d\n", num_objects, objects_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003090 if (num_objects > 0) ReportHistogram(false);
3091}
3092
3093
3094void LargeObjectSpace::CollectCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003095 Isolate* isolate = heap()->isolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003096 LargeObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003097 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098 if (obj->IsCode()) {
3099 Code* code = Code::cast(obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003100 isolate->code_kind_statistics()[code->kind()] += code->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 }
3102 }
3103}
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003104
3105
3106void Page::Print() {
3107 // Make a best-effort to print the objects in the page.
3108 PrintF("Page@%p in %s\n",
3109 this->address(),
3110 AllocationSpaceName(this->owner()->identity()));
3111 printf(" --------------------------------------\n");
3112 HeapObjectIterator objects(this, heap()->GcSafeSizeOfOldObjectFunction());
3113 unsigned mark_size = 0;
3114 for (HeapObject* object = objects.Next();
3115 object != NULL;
3116 object = objects.Next()) {
3117 bool is_marked = Marking::MarkBitFrom(object).Get();
3118 PrintF(" %c ", (is_marked ? '!' : ' ')); // Indent a little.
3119 if (is_marked) {
3120 mark_size += heap()->GcSafeSizeOfOldObjectFunction()(object);
3121 }
3122 object->ShortPrint();
3123 PrintF("\n");
3124 }
3125 printf(" --------------------------------------\n");
3126 printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes());
3127}
3128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129#endif // DEBUG
3130
3131} } // namespace v8::internal