blob: d8aecf3951ab98da3223637c668c20819825b53a [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() ||
danno@chromium.org41728482013-06-12 22:31:22 +000075 owner == page->heap()->property_cell_space() ||
hpayer@chromium.org8432c912013-02-28 15:55:26 +000076 owner == page->heap()->code_space());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000077 Initialize(reinterpret_cast<PagedSpace*>(owner),
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000078 page->area_start(),
79 page->area_end(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000080 kOnePageOnly,
81 size_func);
82 ASSERT(page->WasSweptPrecisely());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000083}
84
85
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000086void HeapObjectIterator::Initialize(PagedSpace* space,
87 Address cur, Address end,
88 HeapObjectIterator::PageMode mode,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 HeapObjectCallback size_f) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000090 // Check that we actually can iterate this space.
91 ASSERT(!space->was_swept_conservatively());
92
93 space_ = space;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 cur_addr_ = cur;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000095 cur_end_ = end;
96 page_mode_ = mode;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 size_func_ = size_f;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098}
99
100
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000101// We have hit the end of the page and should advance to the next block of
102// objects. This happens at the end of the page.
103bool HeapObjectIterator::AdvanceToNextPage() {
104 ASSERT(cur_addr_ == cur_end_);
105 if (page_mode_ == kOnePageOnly) return false;
106 Page* cur_page;
107 if (cur_addr_ == NULL) {
108 cur_page = space_->anchor();
109 } else {
110 cur_page = Page::FromAddress(cur_addr_ - 1);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000111 ASSERT(cur_addr_ == cur_page->area_end());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000112 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113 cur_page = cur_page->next_page();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000114 if (cur_page == space_->anchor()) return false;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000115 cur_addr_ = cur_page->area_start();
116 cur_end_ = cur_page->area_end();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000117 ASSERT(cur_page->WasSweptPrecisely());
118 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119}
120
121
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122// -----------------------------------------------------------------------------
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000123// CodeRange
124
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000126CodeRange::CodeRange(Isolate* isolate)
127 : isolate_(isolate),
128 code_range_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000129 free_list_(0),
130 allocation_list_(0),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000131 current_allocation_block_index_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132}
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000133
134
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000135bool CodeRange::SetUp(const size_t requested) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000136 ASSERT(code_range_ == NULL);
137
138 code_range_ = new VirtualMemory(requested);
139 CHECK(code_range_ != NULL);
140 if (!code_range_->IsReserved()) {
141 delete code_range_;
142 code_range_ = NULL;
143 return false;
144 }
145
146 // We are sure that we have mapped a block of requested addresses.
147 ASSERT(code_range_->size() == requested);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148 LOG(isolate_, NewEvent("CodeRange", code_range_->address(), requested));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000149 Address base = reinterpret_cast<Address>(code_range_->address());
150 Address aligned_base =
151 RoundUp(reinterpret_cast<Address>(code_range_->address()),
152 MemoryChunk::kAlignment);
153 size_t size = code_range_->size() - (aligned_base - base);
154 allocation_list_.Add(FreeBlock(aligned_base, size));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000155 current_allocation_block_index_ = 0;
156 return true;
157}
158
159
160int CodeRange::CompareFreeBlockAddress(const FreeBlock* left,
161 const FreeBlock* right) {
162 // The entire point of CodeRange is that the difference between two
163 // addresses in the range can be represented as a signed 32-bit int,
164 // so the cast is semantically correct.
165 return static_cast<int>(left->start - right->start);
166}
167
168
169void CodeRange::GetNextAllocationBlock(size_t requested) {
170 for (current_allocation_block_index_++;
171 current_allocation_block_index_ < allocation_list_.length();
172 current_allocation_block_index_++) {
173 if (requested <= allocation_list_[current_allocation_block_index_].size) {
174 return; // Found a large enough allocation block.
175 }
176 }
177
178 // Sort and merge the free blocks on the free list and the allocation list.
179 free_list_.AddAll(allocation_list_);
180 allocation_list_.Clear();
181 free_list_.Sort(&CompareFreeBlockAddress);
182 for (int i = 0; i < free_list_.length();) {
183 FreeBlock merged = free_list_[i];
184 i++;
185 // Add adjacent free blocks to the current merged block.
186 while (i < free_list_.length() &&
187 free_list_[i].start == merged.start + merged.size) {
188 merged.size += free_list_[i].size;
189 i++;
190 }
191 if (merged.size > 0) {
192 allocation_list_.Add(merged);
193 }
194 }
195 free_list_.Clear();
196
197 for (current_allocation_block_index_ = 0;
198 current_allocation_block_index_ < allocation_list_.length();
199 current_allocation_block_index_++) {
200 if (requested <= allocation_list_[current_allocation_block_index_].size) {
201 return; // Found a large enough allocation block.
202 }
203 }
204
205 // Code range is full or too fragmented.
206 V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock");
207}
208
209
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000210Address CodeRange::AllocateRawMemory(const size_t requested_size,
211 const size_t commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000212 size_t* allocated) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000213 ASSERT(commit_size <= requested_size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000214 ASSERT(current_allocation_block_index_ < allocation_list_.length());
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000215 if (requested_size > allocation_list_[current_allocation_block_index_].size) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000216 // Find an allocation block large enough. This function call may
217 // call V8::FatalProcessOutOfMemory if it cannot find a large enough block.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000218 GetNextAllocationBlock(requested_size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000219 }
220 // Commit the requested memory at the start of the current allocation block.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000221 size_t aligned_requested = RoundUp(requested_size, MemoryChunk::kAlignment);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000222 FreeBlock current = allocation_list_[current_allocation_block_index_];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000223 if (aligned_requested >= (current.size - Page::kPageSize)) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000224 // Don't leave a small free block, useless for a large object or chunk.
225 *allocated = current.size;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000226 } else {
227 *allocated = aligned_requested;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000228 }
229 ASSERT(*allocated <= current.size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000230 ASSERT(IsAddressAligned(current.start, MemoryChunk::kAlignment));
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000231 if (!isolate_->memory_allocator()->CommitExecutableMemory(code_range_,
232 current.start,
233 commit_size,
234 *allocated)) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000235 *allocated = 0;
236 return NULL;
237 }
238 allocation_list_[current_allocation_block_index_].start += *allocated;
239 allocation_list_[current_allocation_block_index_].size -= *allocated;
240 if (*allocated == current.size) {
241 GetNextAllocationBlock(0); // This block is used up, get the next one.
242 }
243 return current.start;
244}
245
246
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000247bool CodeRange::CommitRawMemory(Address start, size_t length) {
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000248 return isolate_->memory_allocator()->CommitMemory(start, length, EXECUTABLE);
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000249}
250
251
252bool CodeRange::UncommitRawMemory(Address start, size_t length) {
253 return code_range_->Uncommit(start, length);
254}
255
256
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000257void CodeRange::FreeRawMemory(Address address, size_t length) {
258 ASSERT(IsAddressAligned(address, MemoryChunk::kAlignment));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000259 free_list_.Add(FreeBlock(address, length));
260 code_range_->Uncommit(address, length);
261}
262
263
264void CodeRange::TearDown() {
265 delete code_range_; // Frees all memory in the virtual memory range.
266 code_range_ = NULL;
267 free_list_.Free();
268 allocation_list_.Free();
269}
270
271
272// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273// MemoryAllocator
274//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000276MemoryAllocator::MemoryAllocator(Isolate* isolate)
277 : isolate_(isolate),
278 capacity_(0),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 capacity_executable_(0),
280 size_(0),
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000281 size_executable_(0),
282 lowest_ever_allocated_(reinterpret_cast<void*>(-1)),
283 highest_ever_allocated_(reinterpret_cast<void*>(0)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284}
285
286
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000287bool MemoryAllocator::SetUp(intptr_t capacity, intptr_t capacity_executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 capacity_ = RoundUp(capacity, Page::kPageSize);
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000289 capacity_executable_ = RoundUp(capacity_executable, Page::kPageSize);
290 ASSERT_GE(capacity_, capacity_executable_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 size_ = 0;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000293 size_executable_ = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 return true;
296}
297
298
299void MemoryAllocator::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000300 // Check that spaces were torn down before MemoryAllocator.
301 ASSERT(size_ == 0);
302 // TODO(gc) this will be true again when we fix FreeMemory.
303 // ASSERT(size_executable_ == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 capacity_ = 0;
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000305 capacity_executable_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306}
307
308
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000309bool MemoryAllocator::CommitMemory(Address base,
310 size_t size,
311 Executability executable) {
312 if (!VirtualMemory::CommitRegion(base, size, executable == EXECUTABLE)) {
313 return false;
314 }
315 UpdateAllocatedSpaceLimits(base, base + size);
316 return true;
317}
318
319
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000320void MemoryAllocator::FreeMemory(VirtualMemory* reservation,
321 Executability executable) {
322 // TODO(gc) make code_range part of memory allocator?
323 ASSERT(reservation->IsReserved());
324 size_t size = reservation->size();
325 ASSERT(size_ >= size);
326 size_ -= size;
327
328 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
329
330 if (executable == EXECUTABLE) {
331 ASSERT(size_executable_ >= size);
332 size_executable_ -= size;
333 }
334 // Code which is part of the code-range does not have its own VirtualMemory.
335 ASSERT(!isolate_->code_range()->contains(
336 static_cast<Address>(reservation->address())));
337 ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists());
338 reservation->Release();
339}
340
341
342void MemoryAllocator::FreeMemory(Address base,
343 size_t size,
344 Executability executable) {
345 // TODO(gc) make code_range part of memory allocator?
346 ASSERT(size_ >= size);
347 size_ -= size;
348
349 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
350
351 if (executable == EXECUTABLE) {
352 ASSERT(size_executable_ >= size);
353 size_executable_ -= size;
354 }
355 if (isolate_->code_range()->contains(static_cast<Address>(base))) {
356 ASSERT(executable == EXECUTABLE);
357 isolate_->code_range()->FreeRawMemory(base, size);
358 } else {
359 ASSERT(executable == NOT_EXECUTABLE || !isolate_->code_range()->exists());
360 bool result = VirtualMemory::ReleaseRegion(base, size);
361 USE(result);
362 ASSERT(result);
363 }
364}
365
366
367Address MemoryAllocator::ReserveAlignedMemory(size_t size,
368 size_t alignment,
369 VirtualMemory* controller) {
370 VirtualMemory reservation(size, alignment);
371
372 if (!reservation.IsReserved()) return NULL;
373 size_ += reservation.size();
374 Address base = RoundUp(static_cast<Address>(reservation.address()),
375 alignment);
376 controller->TakeControl(&reservation);
377 return base;
378}
379
380
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000381Address MemoryAllocator::AllocateAlignedMemory(size_t reserve_size,
382 size_t commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000383 size_t alignment,
384 Executability executable,
385 VirtualMemory* controller) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000386 ASSERT(commit_size <= reserve_size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000387 VirtualMemory reservation;
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000388 Address base = ReserveAlignedMemory(reserve_size, alignment, &reservation);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000389 if (base == NULL) return NULL;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000390
391 if (executable == EXECUTABLE) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000392 if (!CommitExecutableMemory(&reservation,
393 base,
394 commit_size,
395 reserve_size)) {
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000396 base = NULL;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000397 }
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000398 } else {
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000399 if (reservation.Commit(base, commit_size, false)) {
400 UpdateAllocatedSpaceLimits(base, base + commit_size);
401 } else {
danno@chromium.org2c26cb12012-05-03 09:06:43 +0000402 base = NULL;
403 }
404 }
405
406 if (base == NULL) {
407 // Failed to commit the body. Release the mapping and any partially
408 // commited regions inside it.
409 reservation.Release();
410 return NULL;
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000411 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000412
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000413 controller->TakeControl(&reservation);
414 return base;
415}
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000416
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000417
418void Page::InitializeAsAnchor(PagedSpace* owner) {
419 set_owner(owner);
420 set_prev_page(this);
421 set_next_page(this);
422}
423
424
425NewSpacePage* NewSpacePage::Initialize(Heap* heap,
426 Address start,
427 SemiSpace* semi_space) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000428 Address area_start = start + NewSpacePage::kObjectStartOffset;
429 Address area_end = start + Page::kPageSize;
430
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000431 MemoryChunk* chunk = MemoryChunk::Initialize(heap,
432 start,
433 Page::kPageSize,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000434 area_start,
435 area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000436 NOT_EXECUTABLE,
437 semi_space);
438 chunk->set_next_chunk(NULL);
439 chunk->set_prev_chunk(NULL);
440 chunk->initialize_scan_on_scavenge(true);
441 bool in_to_space = (semi_space->id() != kFromSpace);
442 chunk->SetFlag(in_to_space ? MemoryChunk::IN_TO_SPACE
443 : MemoryChunk::IN_FROM_SPACE);
444 ASSERT(!chunk->IsFlagSet(in_to_space ? MemoryChunk::IN_FROM_SPACE
445 : MemoryChunk::IN_TO_SPACE));
446 NewSpacePage* page = static_cast<NewSpacePage*>(chunk);
447 heap->incremental_marking()->SetNewSpacePageFlags(page);
448 return page;
449}
450
451
452void NewSpacePage::InitializeAsAnchor(SemiSpace* semi_space) {
453 set_owner(semi_space);
454 set_next_chunk(this);
455 set_prev_chunk(this);
456 // Flags marks this invalid page as not being in new-space.
457 // All real new-space pages will be in new-space.
458 SetFlags(0, ~0);
459}
460
461
462MemoryChunk* MemoryChunk::Initialize(Heap* heap,
463 Address base,
464 size_t size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000465 Address area_start,
466 Address area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000467 Executability executable,
468 Space* owner) {
469 MemoryChunk* chunk = FromAddress(base);
470
471 ASSERT(base == chunk->address());
472
473 chunk->heap_ = heap;
474 chunk->size_ = size;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000475 chunk->area_start_ = area_start;
476 chunk->area_end_ = area_end;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000477 chunk->flags_ = 0;
478 chunk->set_owner(owner);
479 chunk->InitializeReservedMemory();
480 chunk->slots_buffer_ = NULL;
481 chunk->skip_list_ = NULL;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000482 chunk->write_barrier_counter_ = kWriteBarrierCounterGranularity;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000483 chunk->progress_bar_ = 0;
danno@chromium.org72204d52012-10-31 10:02:10 +0000484 chunk->high_water_mark_ = static_cast<int>(area_start - base);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000485 chunk->parallel_sweeping_ = 0;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000486 chunk->available_in_small_free_list_ = 0;
487 chunk->available_in_medium_free_list_ = 0;
488 chunk->available_in_large_free_list_ = 0;
489 chunk->available_in_huge_free_list_ = 0;
490 chunk->non_available_small_blocks_ = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000491 chunk->ResetLiveBytes();
492 Bitmap::Clear(chunk);
493 chunk->initialize_scan_on_scavenge(false);
494 chunk->SetFlag(WAS_SWEPT_PRECISELY);
495
496 ASSERT(OFFSET_OF(MemoryChunk, flags_) == kFlagsOffset);
497 ASSERT(OFFSET_OF(MemoryChunk, live_byte_count_) == kLiveBytesOffset);
498
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000499 if (executable == EXECUTABLE) {
500 chunk->SetFlag(IS_EXECUTABLE);
501 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000502
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000503 if (owner == heap->old_data_space()) {
504 chunk->SetFlag(CONTAINS_ONLY_DATA);
505 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000506
507 return chunk;
508}
509
510
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000511// Commit MemoryChunk area to the requested size.
512bool MemoryChunk::CommitArea(size_t requested) {
513 size_t guard_size = IsFlagSet(IS_EXECUTABLE) ?
514 MemoryAllocator::CodePageGuardSize() : 0;
515 size_t header_size = area_start() - address() - guard_size;
516 size_t commit_size = RoundUp(header_size + requested, OS::CommitPageSize());
517 size_t committed_size = RoundUp(header_size + (area_end() - area_start()),
518 OS::CommitPageSize());
519
520 if (commit_size > committed_size) {
521 // Commit size should be less or equal than the reserved size.
522 ASSERT(commit_size <= size() - 2 * guard_size);
523 // Append the committed area.
524 Address start = address() + committed_size + guard_size;
525 size_t length = commit_size - committed_size;
526 if (reservation_.IsReserved()) {
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000527 Executability executable = IsFlagSet(IS_EXECUTABLE)
528 ? EXECUTABLE : NOT_EXECUTABLE;
529 if (!heap()->isolate()->memory_allocator()->CommitMemory(
530 start, length, executable)) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000531 return false;
532 }
533 } else {
534 CodeRange* code_range = heap_->isolate()->code_range();
535 ASSERT(code_range->exists() && IsFlagSet(IS_EXECUTABLE));
536 if (!code_range->CommitRawMemory(start, length)) return false;
537 }
538
539 if (Heap::ShouldZapGarbage()) {
540 heap_->isolate()->memory_allocator()->ZapBlock(start, length);
541 }
542 } else if (commit_size < committed_size) {
543 ASSERT(commit_size > 0);
544 // Shrink the committed area.
545 size_t length = committed_size - commit_size;
546 Address start = address() + committed_size + guard_size - length;
547 if (reservation_.IsReserved()) {
548 if (!reservation_.Uncommit(start, length)) return false;
549 } else {
550 CodeRange* code_range = heap_->isolate()->code_range();
551 ASSERT(code_range->exists() && IsFlagSet(IS_EXECUTABLE));
552 if (!code_range->UncommitRawMemory(start, length)) return false;
553 }
554 }
555
556 area_end_ = area_start_ + requested;
557 return true;
558}
559
560
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000561void MemoryChunk::InsertAfter(MemoryChunk* other) {
562 next_chunk_ = other->next_chunk_;
563 prev_chunk_ = other;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000564
565 // This memory barrier is needed since concurrent sweeper threads may iterate
566 // over the list of pages while a new page is inserted.
567 // TODO(hpayer): find a cleaner way to guarantee that the page list can be
568 // expanded concurrently
569 MemoryBarrier();
570
571 // The following two write operations can take effect in arbitrary order
572 // since pages are always iterated by the sweeper threads in LIFO order, i.e,
573 // the inserted page becomes visible for the sweeper threads after
574 // other->next_chunk_ = this;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000575 other->next_chunk_->prev_chunk_ = this;
576 other->next_chunk_ = this;
577}
578
579
580void MemoryChunk::Unlink() {
581 if (!InNewSpace() && IsFlagSet(SCAN_ON_SCAVENGE)) {
582 heap_->decrement_scan_on_scavenge_pages();
583 ClearFlag(SCAN_ON_SCAVENGE);
584 }
585 next_chunk_->prev_chunk_ = prev_chunk_;
586 prev_chunk_->next_chunk_ = next_chunk_;
587 prev_chunk_ = NULL;
588 next_chunk_ = NULL;
589}
590
591
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000592MemoryChunk* MemoryAllocator::AllocateChunk(intptr_t reserve_area_size,
593 intptr_t commit_area_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000594 Executability executable,
595 Space* owner) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000596 ASSERT(commit_area_size <= reserve_area_size);
597
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000598 size_t chunk_size;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000599 Heap* heap = isolate_->heap();
600 Address base = NULL;
601 VirtualMemory reservation;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000602 Address area_start = NULL;
603 Address area_end = NULL;
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000604
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000605 //
606 // MemoryChunk layout:
607 //
608 // Executable
609 // +----------------------------+<- base aligned with MemoryChunk::kAlignment
610 // | Header |
611 // +----------------------------+<- base + CodePageGuardStartOffset
612 // | Guard |
613 // +----------------------------+<- area_start_
614 // | Area |
615 // +----------------------------+<- area_end_ (area_start + commit_area_size)
616 // | Committed but not used |
617 // +----------------------------+<- aligned at OS page boundary
618 // | Reserved but not committed |
619 // +----------------------------+<- aligned at OS page boundary
620 // | Guard |
621 // +----------------------------+<- base + chunk_size
622 //
623 // Non-executable
624 // +----------------------------+<- base aligned with MemoryChunk::kAlignment
625 // | Header |
626 // +----------------------------+<- area_start_ (base + kObjectStartOffset)
627 // | Area |
628 // +----------------------------+<- area_end_ (area_start + commit_area_size)
629 // | Committed but not used |
630 // +----------------------------+<- aligned at OS page boundary
631 // | Reserved but not committed |
632 // +----------------------------+<- base + chunk_size
633 //
634
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000635 if (executable == EXECUTABLE) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000636 chunk_size = RoundUp(CodePageAreaStartOffset() + reserve_area_size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000637 OS::CommitPageSize()) + CodePageGuardSize();
638
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000639 // Check executable memory limit.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000640 if (size_executable_ + chunk_size > capacity_executable_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000641 LOG(isolate_,
642 StringEvent("MemoryAllocator::AllocateRawMemory",
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000643 "V8 Executable Allocation capacity exceeded"));
644 return NULL;
645 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000646
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000647 // Size of header (not executable) plus area (executable).
648 size_t commit_size = RoundUp(CodePageGuardStartOffset() + commit_area_size,
649 OS::CommitPageSize());
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000650 // Allocate executable memory either from code range or from the
651 // OS.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000652 if (isolate_->code_range()->exists()) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000653 base = isolate_->code_range()->AllocateRawMemory(chunk_size,
654 commit_size,
655 &chunk_size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000656 ASSERT(IsAligned(reinterpret_cast<intptr_t>(base),
657 MemoryChunk::kAlignment));
658 if (base == NULL) return NULL;
659 size_ += chunk_size;
660 // Update executable memory size.
661 size_executable_ += chunk_size;
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000662 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000663 base = AllocateAlignedMemory(chunk_size,
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000664 commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000665 MemoryChunk::kAlignment,
666 executable,
667 &reservation);
668 if (base == NULL) return NULL;
669 // Update executable memory size.
670 size_executable_ += reservation.size();
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000671 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000672
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000673 if (Heap::ShouldZapGarbage()) {
674 ZapBlock(base, CodePageGuardStartOffset());
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000675 ZapBlock(base + CodePageAreaStartOffset(), commit_area_size);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000676 }
677
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000678 area_start = base + CodePageAreaStartOffset();
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000679 area_end = area_start + commit_area_size;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000680 } else {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000681 chunk_size = RoundUp(MemoryChunk::kObjectStartOffset + reserve_area_size,
682 OS::CommitPageSize());
683 size_t commit_size = RoundUp(MemoryChunk::kObjectStartOffset +
684 commit_area_size, OS::CommitPageSize());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000685 base = AllocateAlignedMemory(chunk_size,
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000686 commit_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000687 MemoryChunk::kAlignment,
688 executable,
689 &reservation);
690
691 if (base == NULL) return NULL;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000692
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000693 if (Heap::ShouldZapGarbage()) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000694 ZapBlock(base, Page::kObjectStartOffset + commit_area_size);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000695 }
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000696
697 area_start = base + Page::kObjectStartOffset;
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000698 area_end = area_start + commit_area_size;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000699 }
700
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000701 // Use chunk_size for statistics and callbacks because we assume that they
702 // treat reserved but not-yet committed memory regions of chunks as allocated.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000703 isolate_->counters()->memory_allocated()->
704 Increment(static_cast<int>(chunk_size));
705
706 LOG(isolate_, NewEvent("MemoryChunk", base, chunk_size));
707 if (owner != NULL) {
708 ObjectSpace space = static_cast<ObjectSpace>(1 << owner->identity());
709 PerformAllocationCallback(space, kAllocationActionAllocate, chunk_size);
710 }
711
712 MemoryChunk* result = MemoryChunk::Initialize(heap,
713 base,
714 chunk_size,
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000715 area_start,
716 area_end,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000717 executable,
718 owner);
719 result->set_reserved_memory(&reservation);
720 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721}
722
723
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000724void Page::ResetFreeListStatistics() {
725 non_available_small_blocks_ = 0;
726 available_in_small_free_list_ = 0;
727 available_in_medium_free_list_ = 0;
728 available_in_large_free_list_ = 0;
729 available_in_huge_free_list_ = 0;
730}
731
732
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000733Page* MemoryAllocator::AllocatePage(intptr_t size,
734 PagedSpace* owner,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000735 Executability executable) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000736 MemoryChunk* chunk = AllocateChunk(size, size, executable, owner);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000737
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000738 if (chunk == NULL) return NULL;
739
740 return Page::Initialize(isolate_->heap(), chunk, executable, owner);
741}
742
743
744LargePage* MemoryAllocator::AllocateLargePage(intptr_t object_size,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000745 Space* owner,
746 Executability executable) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000747 MemoryChunk* chunk = AllocateChunk(object_size,
748 object_size,
749 executable,
750 owner);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000751 if (chunk == NULL) return NULL;
752 return LargePage::Initialize(isolate_->heap(), chunk);
753}
754
755
756void MemoryAllocator::Free(MemoryChunk* chunk) {
757 LOG(isolate_, DeleteEvent("MemoryChunk", chunk));
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000758 if (chunk->owner() != NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000759 ObjectSpace space =
760 static_cast<ObjectSpace>(1 << chunk->owner()->identity());
761 PerformAllocationCallback(space, kAllocationActionFree, chunk->size());
762 }
763
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000764 isolate_->heap()->RememberUnmappedPage(
765 reinterpret_cast<Address>(chunk), chunk->IsEvacuationCandidate());
766
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000767 delete chunk->slots_buffer();
768 delete chunk->skip_list();
769
770 VirtualMemory* reservation = chunk->reserved_memory();
771 if (reservation->IsReserved()) {
772 FreeMemory(reservation, chunk->executable());
773 } else {
774 FreeMemory(chunk->address(),
775 chunk->size(),
776 chunk->executable());
777 }
778}
779
780
781bool MemoryAllocator::CommitBlock(Address start,
782 size_t size,
783 Executability executable) {
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000784 if (!CommitMemory(start, size, executable)) return false;
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000785
786 if (Heap::ShouldZapGarbage()) {
787 ZapBlock(start, size);
788 }
789
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000790 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
791 return true;
792}
793
794
795bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
796 if (!VirtualMemory::UncommitRegion(start, size)) return false;
797 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
798 return true;
799}
800
801
802void MemoryAllocator::ZapBlock(Address start, size_t size) {
803 for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) {
804 Memory::Address_at(start + s) = kZapValue;
805 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806}
807
808
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000809void MemoryAllocator::PerformAllocationCallback(ObjectSpace space,
810 AllocationAction action,
811 size_t size) {
812 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
813 MemoryAllocationCallbackRegistration registration =
814 memory_allocation_callbacks_[i];
815 if ((registration.space & space) == space &&
816 (registration.action & action) == action)
817 registration.callback(space, action, static_cast<int>(size));
818 }
819}
820
821
822bool MemoryAllocator::MemoryAllocationCallbackRegistered(
823 MemoryAllocationCallback callback) {
824 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
825 if (memory_allocation_callbacks_[i].callback == callback) return true;
826 }
827 return false;
828}
829
830
831void MemoryAllocator::AddMemoryAllocationCallback(
832 MemoryAllocationCallback callback,
833 ObjectSpace space,
834 AllocationAction action) {
835 ASSERT(callback != NULL);
836 MemoryAllocationCallbackRegistration registration(callback, space, action);
837 ASSERT(!MemoryAllocator::MemoryAllocationCallbackRegistered(callback));
838 return memory_allocation_callbacks_.Add(registration);
839}
840
841
842void MemoryAllocator::RemoveMemoryAllocationCallback(
843 MemoryAllocationCallback callback) {
844 ASSERT(callback != NULL);
845 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
846 if (memory_allocation_callbacks_[i].callback == callback) {
847 memory_allocation_callbacks_.Remove(i);
848 return;
849 }
850 }
851 UNREACHABLE();
852}
853
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854
855#ifdef DEBUG
856void MemoryAllocator::ReportStatistics() {
857 float pct = static_cast<float>(capacity_ - size_) / capacity_;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000858 PrintF(" capacity: %" V8_PTR_PREFIX "d"
859 ", used: %" V8_PTR_PREFIX "d"
860 ", available: %%%d\n\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861 capacity_, size_, static_cast<int>(pct*100));
862}
863#endif
864
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000865
866int MemoryAllocator::CodePageGuardStartOffset() {
867 // We are guarding code pages: the first OS page after the header
868 // will be protected as non-writable.
869 return RoundUp(Page::kObjectStartOffset, OS::CommitPageSize());
870}
871
872
873int MemoryAllocator::CodePageGuardSize() {
874 return static_cast<int>(OS::CommitPageSize());
875}
876
877
878int MemoryAllocator::CodePageAreaStartOffset() {
879 // We are guarding code pages: the first OS page after the header
880 // will be protected as non-writable.
881 return CodePageGuardStartOffset() + CodePageGuardSize();
882}
883
884
885int MemoryAllocator::CodePageAreaEndOffset() {
886 // We are guarding code pages: the last OS page will be protected as
887 // non-writable.
888 return Page::kPageSize - static_cast<int>(OS::CommitPageSize());
889}
890
891
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000892bool MemoryAllocator::CommitExecutableMemory(VirtualMemory* vm,
893 Address start,
894 size_t commit_size,
895 size_t reserved_size) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000896 // Commit page header (not executable).
897 if (!vm->Commit(start,
898 CodePageGuardStartOffset(),
899 false)) {
900 return false;
901 }
902
903 // Create guard page after the header.
904 if (!vm->Guard(start + CodePageGuardStartOffset())) {
905 return false;
906 }
907
908 // Commit page body (executable).
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000909 if (!vm->Commit(start + CodePageAreaStartOffset(),
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000910 commit_size - CodePageGuardStartOffset(),
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000911 true)) {
912 return false;
913 }
914
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000915 // Create guard page before the end.
916 if (!vm->Guard(start + reserved_size - CodePageGuardSize())) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000917 return false;
918 }
919
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000920 UpdateAllocatedSpaceLimits(start,
921 start + CodePageAreaStartOffset() +
922 commit_size - CodePageGuardStartOffset());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000923 return true;
924}
925
926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927// -----------------------------------------------------------------------------
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000928// MemoryChunk implementation
929
930void MemoryChunk::IncrementLiveBytesFromMutator(Address address, int by) {
931 MemoryChunk* chunk = MemoryChunk::FromAddress(address);
932 if (!chunk->InNewSpace() && !static_cast<Page*>(chunk)->WasSwept()) {
933 static_cast<PagedSpace*>(chunk->owner())->IncrementUnsweptFreeBytes(-by);
934 }
935 chunk->IncrementLiveBytes(by);
936}
937
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000938
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000939// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940// PagedSpace implementation
941
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000942PagedSpace::PagedSpace(Heap* heap,
943 intptr_t max_capacity,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000944 AllocationSpace id,
945 Executability executable)
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000946 : Space(heap, id, executable),
947 free_list_(this),
948 was_swept_conservatively_(false),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000949 first_unswept_page_(Page::FromAddress(NULL)),
950 unswept_free_bytes_(0) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000951 if (id == CODE_SPACE) {
952 area_size_ = heap->isolate()->memory_allocator()->
953 CodePageAreaSize();
954 } else {
955 area_size_ = Page::kPageSize - Page::kObjectStartOffset;
956 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000958 * AreaSize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 accounting_stats_.Clear();
960
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 allocation_info_.top = NULL;
962 allocation_info_.limit = NULL;
963
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000964 anchor_.InitializeAsAnchor(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965}
966
967
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000968bool PagedSpace::SetUp() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 return true;
970}
971
972
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000973bool PagedSpace::HasBeenSetUp() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000974 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975}
976
977
978void PagedSpace::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000979 PageIterator iterator(this);
980 while (iterator.has_next()) {
981 heap()->isolate()->memory_allocator()->Free(iterator.next());
982 }
983 anchor_.set_next_page(&anchor_);
984 anchor_.set_prev_page(&anchor_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 accounting_stats_.Clear();
986}
987
988
danno@chromium.org72204d52012-10-31 10:02:10 +0000989size_t PagedSpace::CommittedPhysicalMemory() {
990 if (!VirtualMemory::HasLazyCommits()) return CommittedMemory();
991 MemoryChunk::UpdateHighWaterMark(allocation_info_.top);
992 size_t size = 0;
993 PageIterator it(this);
994 while (it.has_next()) {
995 size += it.next()->CommittedPhysicalMemory();
996 }
997 return size;
998}
999
1000
lrn@chromium.org303ada72010-10-27 09:33:13 +00001001MaybeObject* PagedSpace::FindObject(Address addr) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001002 // Note: this function can only be called on precisely swept spaces.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001003 ASSERT(!heap()->mark_compact_collector()->in_use());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004
1005 if (!Contains(addr)) return Failure::Exception();
1006
1007 Page* p = Page::FromAddress(addr);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001008 HeapObjectIterator it(p, NULL);
1009 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
1010 Address cur = obj->address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 Address next = cur + obj->Size();
1012 if ((cur <= addr) && (addr < next)) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 }
1014
kasper.lund7276f142008-07-30 08:49:36 +00001015 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 return Failure::Exception();
1017}
1018
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001019
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001020bool PagedSpace::CanExpand() {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001021 ASSERT(max_capacity_ % AreaSize() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
1023 if (Capacity() == max_capacity_) return false;
1024
1025 ASSERT(Capacity() < max_capacity_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001027 // Are we going to exceed capacity for this space?
1028 if ((Capacity() + Page::kPageSize) > max_capacity_) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001030 return true;
1031}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001033
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001034bool PagedSpace::Expand() {
1035 if (!CanExpand()) return false;
1036
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001037 intptr_t size = AreaSize();
1038
1039 if (anchor_.next_page() == &anchor_) {
1040 size = SizeOfFirstPage();
1041 }
1042
1043 Page* p = heap()->isolate()->memory_allocator()->AllocatePage(
1044 size, this, executable());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001045 if (p == NULL) return false;
1046
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 ASSERT(Capacity() <= max_capacity_);
1048
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001049 p->InsertAfter(anchor_.prev_page());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050
1051 return true;
1052}
1053
1054
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001055intptr_t PagedSpace::SizeOfFirstPage() {
1056 int size = 0;
1057 switch (identity()) {
1058 case OLD_POINTER_SPACE:
jkummerow@chromium.org25b0e212013-10-04 15:38:52 +00001059 size = 72 * kPointerSize * KB;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001060 break;
1061 case OLD_DATA_SPACE:
1062 size = 192 * KB;
1063 break;
1064 case MAP_SPACE:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001065 size = 16 * kPointerSize * KB;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001066 break;
1067 case CELL_SPACE:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001068 size = 16 * kPointerSize * KB;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001069 break;
danno@chromium.org41728482013-06-12 22:31:22 +00001070 case PROPERTY_CELL_SPACE:
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001071 size = 8 * kPointerSize * KB;
danno@chromium.org41728482013-06-12 22:31:22 +00001072 break;
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001073 case CODE_SPACE:
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001074 if (heap()->isolate()->code_range()->exists()) {
1075 // When code range exists, code pages are allocated in a special way
1076 // (from the reserved code range). That part of the code is not yet
1077 // upgraded to handle small pages.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001078 size = AreaSize();
1079 } else {
jkummerow@chromium.org25b0e212013-10-04 15:38:52 +00001080#if V8_TARGET_ARCH_MIPS
1081 // On MIPS, code stubs seem to be quite a bit larger.
1082 // TODO(olivf/MIPS folks): Can we do anything about this? Does it
1083 // indicate the presence of a bug?
1084 size = 464 * KB;
1085#else
1086 size = 416 * KB;
1087#endif
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001088 }
1089 break;
1090 default:
1091 UNREACHABLE();
1092 }
1093 return Min(size, AreaSize());
1094}
1095
1096
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097int PagedSpace::CountTotalPages() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001098 PageIterator it(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 int count = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001100 while (it.has_next()) {
1101 it.next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102 count++;
1103 }
1104 return count;
1105}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106
1107
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001108void PagedSpace::ObtainFreeListStatistics(Page* page, SizeStats* sizes) {
1109 sizes->huge_size_ = page->available_in_huge_free_list();
1110 sizes->small_size_ = page->available_in_small_free_list();
1111 sizes->medium_size_ = page->available_in_medium_free_list();
1112 sizes->large_size_ = page->available_in_large_free_list();
1113}
1114
1115
1116void PagedSpace::ResetFreeListStatistics() {
1117 PageIterator page_iterator(this);
1118 while (page_iterator.has_next()) {
1119 Page* page = page_iterator.next();
1120 page->ResetFreeListStatistics();
1121 }
1122}
1123
1124
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001125void PagedSpace::ReleasePage(Page* page, bool unlink) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001126 ASSERT(page->LiveBytes() == 0);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001127 ASSERT(AreaSize() == page->area_size());
danno@chromium.org2c456792011-11-11 12:00:53 +00001128
ricow@chromium.org7ad65222011-12-19 12:13:11 +00001129 // Adjust list of unswept pages if the page is the head of the list.
danno@chromium.org2c456792011-11-11 12:00:53 +00001130 if (first_unswept_page_ == page) {
1131 first_unswept_page_ = page->next_page();
1132 if (first_unswept_page_ == anchor()) {
1133 first_unswept_page_ = Page::FromAddress(NULL);
1134 }
1135 }
1136
1137 if (page->WasSwept()) {
1138 intptr_t size = free_list_.EvictFreeListItems(page);
1139 accounting_stats_.AllocateBytes(size);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001140 ASSERT_EQ(AreaSize(), static_cast<int>(size));
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001141 } else {
1142 DecreaseUnsweptFreeBytes(page);
danno@chromium.org2c456792011-11-11 12:00:53 +00001143 }
1144
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001145 if (Page::FromAllocationTop(allocation_info_.top) == page) {
1146 allocation_info_.top = allocation_info_.limit = NULL;
1147 }
1148
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001149 if (unlink) {
1150 page->Unlink();
1151 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001152 if (page->IsFlagSet(MemoryChunk::CONTAINS_ONLY_DATA)) {
1153 heap()->isolate()->memory_allocator()->Free(page);
1154 } else {
1155 heap()->QueueMemoryChunkForFree(page);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001156 }
1157
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001158 ASSERT(Capacity() > 0);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001159 accounting_stats_.ShrinkSpace(AreaSize());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160}
1161
1162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163#ifdef DEBUG
1164void PagedSpace::Print() { }
1165#endif
1166
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001167#ifdef VERIFY_HEAP
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001168void PagedSpace::Verify(ObjectVisitor* visitor) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001169 // We can only iterate over the pages if they were swept precisely.
1170 if (was_swept_conservatively_) return;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001171
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001172 bool allocation_pointer_found_in_space =
1173 (allocation_info_.top == allocation_info_.limit);
1174 PageIterator page_iterator(this);
1175 while (page_iterator.has_next()) {
1176 Page* page = page_iterator.next();
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001177 CHECK(page->owner() == this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001178 if (page == Page::FromAllocationTop(allocation_info_.top)) {
1179 allocation_pointer_found_in_space = true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001180 }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001181 CHECK(page->WasSweptPrecisely());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001182 HeapObjectIterator it(page, NULL);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001183 Address end_of_previous_object = page->area_start();
1184 Address top = page->area_end();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001185 int black_size = 0;
1186 for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) {
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001187 CHECK(end_of_previous_object <= object->address());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001188
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001189 // The first word should be a map, and we expect all map pointers to
1190 // be in map space.
1191 Map* map = object->map();
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001192 CHECK(map->IsMap());
1193 CHECK(heap()->map_space()->Contains(map));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001194
1195 // Perform space-specific object verification.
1196 VerifyObject(object);
1197
1198 // The object itself should look OK.
1199 object->Verify();
1200
1201 // All the interior pointers should be contained in the heap.
1202 int size = object->Size();
1203 object->IterateBody(map->instance_type(), size, visitor);
1204 if (Marking::IsBlack(Marking::MarkBitFrom(object))) {
1205 black_size += size;
1206 }
1207
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001208 CHECK(object->address() + size <= top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001209 end_of_previous_object = object->address() + size;
1210 }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001211 CHECK_LE(black_size, page->LiveBytes());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001212 }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001213 CHECK(allocation_pointer_found_in_space);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001214}
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001215#endif // VERIFY_HEAP
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217// -----------------------------------------------------------------------------
1218// NewSpace implementation
1219
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001220
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001221bool NewSpace::SetUp(int reserved_semispace_capacity,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001222 int maximum_semispace_capacity) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001223 // Set up new space based on the preallocated memory block defined by
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001224 // start and size. The provided space is divided into two semi-spaces.
1225 // To support fast containment testing in the new space, the size of
1226 // this chunk must be a power of two and it must be aligned to its size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001227 int initial_semispace_capacity = heap()->InitialSemiSpaceSize();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001228
1229 size_t size = 2 * reserved_semispace_capacity;
1230 Address base =
1231 heap()->isolate()->memory_allocator()->ReserveAlignedMemory(
1232 size, size, &reservation_);
1233 if (base == NULL) return false;
1234
1235 chunk_base_ = base;
1236 chunk_size_ = static_cast<uintptr_t>(size);
1237 LOG(heap()->isolate(), NewEvent("InitialChunk", chunk_base_, chunk_size_));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
1240 ASSERT(IsPowerOf2(maximum_semispace_capacity));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001242 // Allocate and set up the histogram arrays if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1244 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1245
1246#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
1247 promoted_histogram_[name].set_name(#name);
1248 INSTANCE_TYPE_LIST(SET_NAME)
1249#undef SET_NAME
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001251 ASSERT(reserved_semispace_capacity == heap()->ReservedSemiSpaceSize());
1252 ASSERT(static_cast<intptr_t>(chunk_size_) >=
1253 2 * heap()->ReservedSemiSpaceSize());
1254 ASSERT(IsAddressAligned(chunk_base_, 2 * reserved_semispace_capacity, 0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001256 to_space_.SetUp(chunk_base_,
1257 initial_semispace_capacity,
1258 maximum_semispace_capacity);
1259 from_space_.SetUp(chunk_base_ + reserved_semispace_capacity,
1260 initial_semispace_capacity,
1261 maximum_semispace_capacity);
1262 if (!to_space_.Commit()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 return false;
1264 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001265 ASSERT(!from_space_.is_committed()); // No need to use memory yet.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001267 start_ = chunk_base_;
1268 address_mask_ = ~(2 * reserved_semispace_capacity - 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001269 object_mask_ = address_mask_ | kHeapObjectTagMask;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001270 object_expected_ = reinterpret_cast<uintptr_t>(start_) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001272 ResetAllocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 return true;
1275}
1276
1277
1278void NewSpace::TearDown() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 if (allocated_histogram_) {
1280 DeleteArray(allocated_histogram_);
1281 allocated_histogram_ = NULL;
1282 }
1283 if (promoted_histogram_) {
1284 DeleteArray(promoted_histogram_);
1285 promoted_histogram_ = NULL;
1286 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287
1288 start_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 allocation_info_.top = NULL;
1290 allocation_info_.limit = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001292 to_space_.TearDown();
1293 from_space_.TearDown();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001294
1295 LOG(heap()->isolate(), DeleteEvent("InitialChunk", chunk_base_));
1296
1297 ASSERT(reservation_.IsReserved());
1298 heap()->isolate()->memory_allocator()->FreeMemory(&reservation_,
1299 NOT_EXECUTABLE);
1300 chunk_base_ = NULL;
1301 chunk_size_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302}
1303
1304
1305void NewSpace::Flip() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001306 SemiSpace::Swap(&from_space_, &to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307}
1308
1309
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001310void NewSpace::Grow() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001311 // Double the semispace size but only up to maximum capacity.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001312 ASSERT(Capacity() < MaximumCapacity());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001313 int new_capacity = Min(MaximumCapacity(), 2 * static_cast<int>(Capacity()));
1314 if (to_space_.GrowTo(new_capacity)) {
1315 // Only grow from space if we managed to grow to-space.
1316 if (!from_space_.GrowTo(new_capacity)) {
1317 // If we managed to grow to-space but couldn't grow from-space,
1318 // attempt to shrink to-space.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001319 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
1320 // We are in an inconsistent state because we could not
1321 // commit/uncommit memory from new space.
1322 V8::FatalProcessOutOfMemory("Failed to grow new space.");
1323 }
1324 }
1325 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001327}
1328
1329
1330void NewSpace::Shrink() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001331 int new_capacity = Max(InitialCapacity(), 2 * SizeAsInt());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001332 int rounded_new_capacity = RoundUp(new_capacity, Page::kPageSize);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001333 if (rounded_new_capacity < Capacity() &&
1334 to_space_.ShrinkTo(rounded_new_capacity)) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001335 // Only shrink from-space if we managed to shrink to-space.
1336 from_space_.Reset();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001337 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001338 // If we managed to shrink to-space but couldn't shrink from
1339 // space, attempt to grow to-space again.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001340 if (!to_space_.GrowTo(from_space_.Capacity())) {
1341 // We are in an inconsistent state because we could not
1342 // commit/uncommit memory from new space.
1343 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
1344 }
1345 }
1346 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001347 allocation_info_.limit = to_space_.page_high();
1348 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1349}
1350
1351
1352void NewSpace::UpdateAllocationInfo() {
danno@chromium.org72204d52012-10-31 10:02:10 +00001353 MemoryChunk::UpdateHighWaterMark(allocation_info_.top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001354 allocation_info_.top = to_space_.page_low();
1355 allocation_info_.limit = to_space_.page_high();
1356
1357 // Lower limit during incremental marking.
1358 if (heap()->incremental_marking()->IsMarking() &&
1359 inline_allocation_limit_step() != 0) {
1360 Address new_limit =
1361 allocation_info_.top + inline_allocation_limit_step();
1362 allocation_info_.limit = Min(new_limit, allocation_info_.limit);
1363 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001364 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365}
1366
1367
1368void NewSpace::ResetAllocationInfo() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001369 to_space_.Reset();
1370 UpdateAllocationInfo();
1371 pages_used_ = 0;
1372 // Clear all mark-bits in the to-space.
1373 NewSpacePageIterator it(&to_space_);
1374 while (it.has_next()) {
1375 Bitmap::Clear(it.next());
1376 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377}
1378
1379
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001380bool NewSpace::AddFreshPage() {
1381 Address top = allocation_info_.top;
1382 if (NewSpacePage::IsAtStart(top)) {
1383 // The current page is already empty. Don't try to make another.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001385 // We should only get here if someone asks to allocate more
1386 // than what can be stored in a single page.
1387 // TODO(gc): Change the limit on new-space allocation to prevent this
1388 // from happening (all such allocations should go directly to LOSpace).
1389 return false;
1390 }
1391 if (!to_space_.AdvancePage()) {
1392 // Failed to get a new page in to-space.
1393 return false;
1394 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00001395
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001396 // Clear remainder of current page.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001397 Address limit = NewSpacePage::FromLimit(top)->area_end();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001398 if (heap()->gc_state() == Heap::SCAVENGE) {
1399 heap()->promotion_queue()->SetNewLimit(limit);
1400 heap()->promotion_queue()->ActivateGuardIfOnTheSamePage();
1401 }
1402
1403 int remaining_in_page = static_cast<int>(limit - top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001404 heap()->CreateFillerObjectAt(top, remaining_in_page);
1405 pages_used_++;
1406 UpdateAllocationInfo();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001407
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001408 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409}
1410
1411
danno@chromium.orgc612e022011-11-10 11:38:15 +00001412MaybeObject* NewSpace::SlowAllocateRaw(int size_in_bytes) {
1413 Address old_top = allocation_info_.top;
1414 Address new_top = old_top + size_in_bytes;
1415 Address high = to_space_.page_high();
1416 if (allocation_info_.limit < high) {
1417 // Incremental marking has lowered the limit to get a
1418 // chance to do a step.
1419 allocation_info_.limit = Min(
1420 allocation_info_.limit + inline_allocation_limit_step_,
1421 high);
1422 int bytes_allocated = static_cast<int>(new_top - top_on_previous_step_);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001423 heap()->incremental_marking()->Step(
1424 bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001425 top_on_previous_step_ = new_top;
1426 return AllocateRaw(size_in_bytes);
1427 } else if (AddFreshPage()) {
1428 // Switched to new page. Try allocating again.
1429 int bytes_allocated = static_cast<int>(old_top - top_on_previous_step_);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001430 heap()->incremental_marking()->Step(
1431 bytes_allocated, IncrementalMarking::GC_VIA_STACK_GUARD);
danno@chromium.orgc612e022011-11-10 11:38:15 +00001432 top_on_previous_step_ = to_space_.page_low();
1433 return AllocateRaw(size_in_bytes);
1434 } else {
1435 return Failure::RetryAfterGC();
1436 }
1437}
1438
1439
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001440#ifdef VERIFY_HEAP
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001441// We do not use the SemiSpaceIterator because verification doesn't assume
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442// that it works (it depends on the invariants we are checking).
1443void NewSpace::Verify() {
1444 // The allocation pointer should be in the space or at the very end.
1445 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1446
1447 // There should be objects packed in from the low address up to the
1448 // allocation pointer.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001449 Address current = to_space_.first_page()->area_start();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001450 CHECK_EQ(current, to_space_.space_start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001452 while (current != top()) {
1453 if (!NewSpacePage::IsAtEnd(current)) {
1454 // The allocation pointer should not be in the middle of an object.
1455 CHECK(!NewSpacePage::FromLimit(current)->ContainsLimit(top()) ||
1456 current < top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001458 HeapObject* object = HeapObject::FromAddress(current);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001460 // The first word should be a map, and we expect all map pointers to
1461 // be in map space.
1462 Map* map = object->map();
1463 CHECK(map->IsMap());
1464 CHECK(heap()->map_space()->Contains(map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001466 // The object should not be code or a map.
1467 CHECK(!object->IsMap());
1468 CHECK(!object->IsCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001470 // The object itself should look OK.
1471 object->Verify();
1472
1473 // All the interior pointers should be contained in the heap.
1474 VerifyPointersVisitor visitor;
1475 int size = object->Size();
1476 object->IterateBody(map->instance_type(), size, &visitor);
1477
1478 current += size;
1479 } else {
1480 // At end of page, switch to next page.
1481 NewSpacePage* page = NewSpacePage::FromLimit(current)->next_page();
1482 // Next page should be valid.
1483 CHECK(!page->is_anchor());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001484 current = page->area_start();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001485 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 }
1487
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001488 // Check semi-spaces.
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001489 CHECK_EQ(from_space_.id(), kFromSpace);
1490 CHECK_EQ(to_space_.id(), kToSpace);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001491 from_space_.Verify();
1492 to_space_.Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493}
1494#endif
1495
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496// -----------------------------------------------------------------------------
1497// SemiSpace implementation
1498
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001499void SemiSpace::SetUp(Address start,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001500 int initial_capacity,
1501 int maximum_capacity) {
1502 // Creates a space in the young generation. The constructor does not
1503 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1504 // memory of size 'capacity' when set up, and does not grow or shrink
1505 // otherwise. In the mark-compact collector, the memory region of the from
1506 // space is used as the marking stack. It requires contiguous memory
1507 // addresses.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001508 ASSERT(maximum_capacity >= Page::kPageSize);
1509 initial_capacity_ = RoundDown(initial_capacity, Page::kPageSize);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001510 capacity_ = initial_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001511 maximum_capacity_ = RoundDown(maximum_capacity, Page::kPageSize);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001512 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 start_ = start;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001514 address_mask_ = ~(maximum_capacity - 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001515 object_mask_ = address_mask_ | kHeapObjectTagMask;
ager@chromium.org9085a012009-05-11 19:22:57 +00001516 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517 age_mark_ = start_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518}
1519
1520
1521void SemiSpace::TearDown() {
1522 start_ = NULL;
1523 capacity_ = 0;
1524}
1525
1526
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001527bool SemiSpace::Commit() {
1528 ASSERT(!is_committed());
1529 int pages = capacity_ / Page::kPageSize;
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001530 if (!heap()->isolate()->memory_allocator()->CommitBlock(start_,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001531 capacity_,
1532 executable())) {
kasper.lund7276f142008-07-30 08:49:36 +00001533 return false;
1534 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001535
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001536 NewSpacePage* current = anchor();
1537 for (int i = 0; i < pages; i++) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001538 NewSpacePage* new_page =
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001539 NewSpacePage::Initialize(heap(), start_ + i * Page::kPageSize, this);
1540 new_page->InsertAfter(current);
1541 current = new_page;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001542 }
1543
1544 committed_ = true;
1545 Reset();
1546 return true;
1547}
1548
1549
1550bool SemiSpace::Uncommit() {
1551 ASSERT(is_committed());
1552 Address start = start_ + maximum_capacity_ - capacity_;
1553 if (!heap()->isolate()->memory_allocator()->UncommitBlock(start, capacity_)) {
1554 return false;
1555 }
1556 anchor()->set_next_page(anchor());
1557 anchor()->set_prev_page(anchor());
1558
1559 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 return true;
1561}
1562
1563
danno@chromium.org72204d52012-10-31 10:02:10 +00001564size_t SemiSpace::CommittedPhysicalMemory() {
1565 if (!is_committed()) return 0;
1566 size_t size = 0;
1567 NewSpacePageIterator it(this);
1568 while (it.has_next()) {
1569 size += it.next()->CommittedPhysicalMemory();
1570 }
1571 return size;
1572}
1573
1574
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001575bool SemiSpace::GrowTo(int new_capacity) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001576 if (!is_committed()) {
1577 if (!Commit()) return false;
1578 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001579 ASSERT((new_capacity & Page::kPageAlignmentMask) == 0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001580 ASSERT(new_capacity <= maximum_capacity_);
1581 ASSERT(new_capacity > capacity_);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001582 int pages_before = capacity_ / Page::kPageSize;
1583 int pages_after = new_capacity / Page::kPageSize;
1584
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001585 size_t delta = new_capacity - capacity_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001586
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001587 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001588 if (!heap()->isolate()->memory_allocator()->CommitBlock(
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001589 start_ + capacity_, delta, executable())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001590 return false;
1591 }
1592 capacity_ = new_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001593 NewSpacePage* last_page = anchor()->prev_page();
1594 ASSERT(last_page != anchor());
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001595 for (int i = pages_before; i < pages_after; i++) {
1596 Address page_address = start_ + i * Page::kPageSize;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001597 NewSpacePage* new_page = NewSpacePage::Initialize(heap(),
1598 page_address,
1599 this);
1600 new_page->InsertAfter(last_page);
1601 Bitmap::Clear(new_page);
1602 // Duplicate the flags that was set on the old page.
1603 new_page->SetFlags(last_page->GetFlags(),
1604 NewSpacePage::kCopyOnFlipFlagsMask);
1605 last_page = new_page;
1606 }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001607 return true;
1608}
1609
1610
1611bool SemiSpace::ShrinkTo(int new_capacity) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001612 ASSERT((new_capacity & Page::kPageAlignmentMask) == 0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001613 ASSERT(new_capacity >= initial_capacity_);
1614 ASSERT(new_capacity < capacity_);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001615 if (is_committed()) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001616 size_t delta = capacity_ - new_capacity;
1617 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001618
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001619 MemoryAllocator* allocator = heap()->isolate()->memory_allocator();
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001620 if (!allocator->UncommitBlock(start_ + new_capacity, delta)) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001621 return false;
1622 }
1623
1624 int pages_after = new_capacity / Page::kPageSize;
1625 NewSpacePage* new_last_page =
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001626 NewSpacePage::FromAddress(start_ + (pages_after - 1) * Page::kPageSize);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001627 new_last_page->set_next_page(anchor());
1628 anchor()->set_prev_page(new_last_page);
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +00001629 ASSERT((current_page_ >= first_page()) && (current_page_ <= new_last_page));
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001630 }
1631
1632 capacity_ = new_capacity;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001633
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001634 return true;
1635}
1636
1637
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001638void SemiSpace::FlipPages(intptr_t flags, intptr_t mask) {
1639 anchor_.set_owner(this);
1640 // Fixup back-pointers to anchor. Address of anchor changes
1641 // when we swap.
1642 anchor_.prev_page()->set_next_page(&anchor_);
1643 anchor_.next_page()->set_prev_page(&anchor_);
1644
1645 bool becomes_to_space = (id_ == kFromSpace);
1646 id_ = becomes_to_space ? kToSpace : kFromSpace;
1647 NewSpacePage* page = anchor_.next_page();
1648 while (page != &anchor_) {
1649 page->set_owner(this);
1650 page->SetFlags(flags, mask);
1651 if (becomes_to_space) {
1652 page->ClearFlag(MemoryChunk::IN_FROM_SPACE);
1653 page->SetFlag(MemoryChunk::IN_TO_SPACE);
1654 page->ClearFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK);
1655 page->ResetLiveBytes();
1656 } else {
1657 page->SetFlag(MemoryChunk::IN_FROM_SPACE);
1658 page->ClearFlag(MemoryChunk::IN_TO_SPACE);
1659 }
1660 ASSERT(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE));
1661 ASSERT(page->IsFlagSet(MemoryChunk::IN_TO_SPACE) ||
1662 page->IsFlagSet(MemoryChunk::IN_FROM_SPACE));
1663 page = page->next_page();
1664 }
1665}
1666
1667
1668void SemiSpace::Reset() {
1669 ASSERT(anchor_.next_page() != &anchor_);
1670 current_page_ = anchor_.next_page();
1671}
1672
1673
1674void SemiSpace::Swap(SemiSpace* from, SemiSpace* to) {
1675 // We won't be swapping semispaces without data in them.
1676 ASSERT(from->anchor_.next_page() != &from->anchor_);
1677 ASSERT(to->anchor_.next_page() != &to->anchor_);
1678
1679 // Swap bits.
1680 SemiSpace tmp = *from;
1681 *from = *to;
1682 *to = tmp;
1683
1684 // Fixup back-pointers to the page list anchor now that its address
1685 // has changed.
1686 // Swap to/from-space bits on pages.
1687 // Copy GC flags from old active space (from-space) to new (to-space).
1688 intptr_t flags = from->current_page()->GetFlags();
1689 to->FlipPages(flags, NewSpacePage::kCopyOnFlipFlagsMask);
1690
1691 from->FlipPages(0, 0);
1692}
1693
1694
1695void SemiSpace::set_age_mark(Address mark) {
1696 ASSERT(NewSpacePage::FromLimit(mark)->semi_space() == this);
1697 age_mark_ = mark;
1698 // Mark all pages up to the one containing mark.
1699 NewSpacePageIterator it(space_start(), mark);
1700 while (it.has_next()) {
1701 it.next()->SetFlag(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK);
1702 }
1703}
1704
1705
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706#ifdef DEBUG
1707void SemiSpace::Print() { }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001708#endif
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001709
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001710#ifdef VERIFY_HEAP
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001711void SemiSpace::Verify() {
1712 bool is_from_space = (id_ == kFromSpace);
1713 NewSpacePage* page = anchor_.next_page();
1714 CHECK(anchor_.semi_space() == this);
1715 while (page != &anchor_) {
1716 CHECK(page->semi_space() == this);
1717 CHECK(page->InNewSpace());
1718 CHECK(page->IsFlagSet(is_from_space ? MemoryChunk::IN_FROM_SPACE
1719 : MemoryChunk::IN_TO_SPACE));
1720 CHECK(!page->IsFlagSet(is_from_space ? MemoryChunk::IN_TO_SPACE
1721 : MemoryChunk::IN_FROM_SPACE));
1722 CHECK(page->IsFlagSet(MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING));
1723 if (!is_from_space) {
1724 // The pointers-from-here-are-interesting flag isn't updated dynamically
1725 // on from-space pages, so it might be out of sync with the marking state.
1726 if (page->heap()->incremental_marking()->IsMarking()) {
1727 CHECK(page->IsFlagSet(MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING));
1728 } else {
1729 CHECK(!page->IsFlagSet(
1730 MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING));
1731 }
1732 // TODO(gc): Check that the live_bytes_count_ field matches the
1733 // black marking on the page (if we make it match in new-space).
1734 }
1735 CHECK(page->IsFlagSet(MemoryChunk::SCAN_ON_SCAVENGE));
1736 CHECK(page->prev_page()->next_page() == page);
1737 page = page->next_page();
1738 }
1739}
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001740#endif
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001741
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001742#ifdef DEBUG
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001743void SemiSpace::AssertValidRange(Address start, Address end) {
1744 // Addresses belong to same semi-space
1745 NewSpacePage* page = NewSpacePage::FromLimit(start);
1746 NewSpacePage* end_page = NewSpacePage::FromLimit(end);
1747 SemiSpace* space = page->semi_space();
1748 CHECK_EQ(space, end_page->semi_space());
1749 // Start address is before end address, either on same page,
1750 // or end address is on a later page in the linked list of
1751 // semi-space pages.
1752 if (page == end_page) {
1753 CHECK(start <= end);
1754 } else {
1755 while (page != end_page) {
1756 page = page->next_page();
1757 CHECK_NE(page, space->anchor());
1758 }
1759 }
1760}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761#endif
1762
1763
1764// -----------------------------------------------------------------------------
1765// SemiSpaceIterator implementation.
1766SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001767 Initialize(space->bottom(), space->top(), NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768}
1769
1770
1771SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1772 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001773 Initialize(space->bottom(), space->top(), size_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774}
1775
1776
1777SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001778 Initialize(start, space->top(), NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779}
1780
1781
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001782SemiSpaceIterator::SemiSpaceIterator(Address from, Address to) {
1783 Initialize(from, to, NULL);
1784}
1785
1786
1787void SemiSpaceIterator::Initialize(Address start,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 Address end,
1789 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001790 SemiSpace::AssertValidRange(start, end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 current_ = start;
1792 limit_ = end;
1793 size_func_ = size_func;
1794}
1795
1796
1797#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798// heap_histograms is shared, always clear it before using it.
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00001799static void ClearHistograms(Isolate* isolate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 // We reset the name each time, though it hasn't changed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001801#define DEF_TYPE_NAME(name) isolate->heap_histograms()[name].set_name(#name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1803#undef DEF_TYPE_NAME
1804
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001805#define CLEAR_HISTOGRAM(name) isolate->heap_histograms()[name].clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1807#undef CLEAR_HISTOGRAM
1808
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001809 isolate->js_spill_information()->Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810}
1811
1812
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001813static void ClearCodeKindStatistics(int* code_kind_statistics) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001815 code_kind_statistics[i] = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 }
1817}
1818
1819
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001820static void ReportCodeKindStatistics(int* code_kind_statistics) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 PrintF("\n Code kind histograms: \n");
1822 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001823 if (code_kind_statistics[i] > 0) {
1824 PrintF(" %-20s: %10d bytes\n",
1825 Code::Kind2String(static_cast<Code::Kind>(i)),
1826 code_kind_statistics[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 }
1828 }
1829 PrintF("\n");
1830}
1831
1832
1833static int CollectHistogramInfo(HeapObject* obj) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001834 Isolate* isolate = obj->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 InstanceType type = obj->map()->instance_type();
1836 ASSERT(0 <= type && type <= LAST_TYPE);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001837 ASSERT(isolate->heap_histograms()[type].name() != NULL);
1838 isolate->heap_histograms()[type].increment_number(1);
1839 isolate->heap_histograms()[type].increment_bytes(obj->Size());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840
1841 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001842 JSObject::cast(obj)->IncrementSpillStatistics(
1843 isolate->js_spill_information());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 }
1845
1846 return obj->Size();
1847}
1848
1849
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00001850static void ReportHistogram(Isolate* isolate, bool print_spill) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 PrintF("\n Object Histogram:\n");
1852 for (int i = 0; i <= LAST_TYPE; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001853 if (isolate->heap_histograms()[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001854 PrintF(" %-34s%10d (%10d bytes)\n",
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001855 isolate->heap_histograms()[i].name(),
1856 isolate->heap_histograms()[i].number(),
1857 isolate->heap_histograms()[i].bytes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 }
1859 }
1860 PrintF("\n");
1861
1862 // Summarize string types.
1863 int string_number = 0;
1864 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001865#define INCREMENT(type, size, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001866 string_number += isolate->heap_histograms()[type].number(); \
1867 string_bytes += isolate->heap_histograms()[type].bytes();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 STRING_TYPE_LIST(INCREMENT)
1869#undef INCREMENT
1870 if (string_number > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001871 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 string_bytes);
1873 }
1874
1875 if (FLAG_collect_heap_spill_statistics && print_spill) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001876 isolate->js_spill_information()->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877 }
1878}
1879#endif // DEBUG
1880
1881
1882// Support for statistics gathering for --heap-stats and --log-gc.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883void NewSpace::ClearHistograms() {
1884 for (int i = 0; i <= LAST_TYPE; i++) {
1885 allocated_histogram_[i].clear();
1886 promoted_histogram_[i].clear();
1887 }
1888}
1889
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891// Because the copying collector does not touch garbage objects, we iterate
1892// the new space before a collection to get a histogram of allocated objects.
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001893// This only happens when --log-gc flag is set.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894void NewSpace::CollectStatistics() {
1895 ClearHistograms();
1896 SemiSpaceIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001897 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next())
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001898 RecordAllocation(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899}
1900
1901
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001902static void DoReportStatistics(Isolate* isolate,
1903 HistogramInfo* info, const char* description) {
1904 LOG(isolate, HeapSampleBeginEvent("NewSpace", description));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 // Lump all the string types together.
1906 int string_number = 0;
1907 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001908#define INCREMENT(type, size, name, camel_name) \
1909 string_number += info[type].number(); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 string_bytes += info[type].bytes();
1911 STRING_TYPE_LIST(INCREMENT)
1912#undef INCREMENT
1913 if (string_number > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001914 LOG(isolate,
1915 HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 }
1917
1918 // Then do the other types.
1919 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1920 if (info[i].number() > 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001921 LOG(isolate,
1922 HeapSampleItemEvent(info[i].name(), info[i].number(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 info[i].bytes()));
1924 }
1925 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001926 LOG(isolate, HeapSampleEndEvent("NewSpace", description));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928
1929
1930void NewSpace::ReportStatistics() {
1931#ifdef DEBUG
1932 if (FLAG_heap_stats) {
1933 float pct = static_cast<float>(Available()) / Capacity();
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001934 PrintF(" capacity: %" V8_PTR_PREFIX "d"
1935 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 Capacity(), Available(), static_cast<int>(pct*100));
1937 PrintF("\n Object Histogram:\n");
1938 for (int i = 0; i <= LAST_TYPE; i++) {
1939 if (allocated_histogram_[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001940 PrintF(" %-34s%10d (%10d bytes)\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 allocated_histogram_[i].name(),
1942 allocated_histogram_[i].number(),
1943 allocated_histogram_[i].bytes());
1944 }
1945 }
1946 PrintF("\n");
1947 }
1948#endif // DEBUG
1949
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 if (FLAG_log_gc) {
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00001951 Isolate* isolate = heap()->isolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001952 DoReportStatistics(isolate, allocated_histogram_, "allocated");
1953 DoReportStatistics(isolate, promoted_histogram_, "promoted");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955}
1956
1957
1958void NewSpace::RecordAllocation(HeapObject* obj) {
1959 InstanceType type = obj->map()->instance_type();
1960 ASSERT(0 <= type && type <= LAST_TYPE);
1961 allocated_histogram_[type].increment_number(1);
1962 allocated_histogram_[type].increment_bytes(obj->Size());
1963}
1964
1965
1966void NewSpace::RecordPromotion(HeapObject* obj) {
1967 InstanceType type = obj->map()->instance_type();
1968 ASSERT(0 <= type && type <= LAST_TYPE);
1969 promoted_histogram_[type].increment_number(1);
1970 promoted_histogram_[type].increment_bytes(obj->Size());
1971}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972
danno@chromium.org72204d52012-10-31 10:02:10 +00001973
1974size_t NewSpace::CommittedPhysicalMemory() {
1975 if (!VirtualMemory::HasLazyCommits()) return CommittedMemory();
1976 MemoryChunk::UpdateHighWaterMark(allocation_info_.top);
1977 size_t size = to_space_.CommittedPhysicalMemory();
1978 if (from_space_.is_committed()) {
1979 size += from_space_.CommittedPhysicalMemory();
1980 }
1981 return size;
1982}
1983
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001984
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985// -----------------------------------------------------------------------------
1986// Free lists for old object spaces implementation
1987
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001988void FreeListNode::set_size(Heap* heap, int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 ASSERT(size_in_bytes > 0);
1990 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1991
1992 // We write a map and possibly size information to the block. If the block
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001993 // is big enough to be a FreeSpace with at least one extra word (the next
1994 // pointer), we set its map to be the free space map and its size to an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 // appropriate array length for the desired size from HeapObject::Size().
1996 // If the block is too small (eg, one or two words), to hold both a size
1997 // field and a next pointer, we give it a filler map that gives it the
1998 // correct size.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001999 if (size_in_bytes > FreeSpace::kHeaderSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002000 set_map_no_write_barrier(heap->raw_unchecked_free_space_map());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002001 // Can't use FreeSpace::cast because it fails during deserialization.
2002 FreeSpace* this_as_free_space = reinterpret_cast<FreeSpace*>(this);
2003 this_as_free_space->set_size(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 } else if (size_in_bytes == kPointerSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002005 set_map_no_write_barrier(heap->raw_unchecked_one_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 } else if (size_in_bytes == 2 * kPointerSize) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002007 set_map_no_write_barrier(heap->raw_unchecked_two_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 } else {
2009 UNREACHABLE();
2010 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002011 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002012 // deserialization because the free space map is not done yet.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013}
2014
2015
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002016FreeListNode* FreeListNode::next() {
ager@chromium.org3811b432009-10-28 14:53:37 +00002017 ASSERT(IsFreeListNode(this));
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002018 if (map() == GetHeap()->raw_unchecked_free_space_map()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002019 ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize);
2020 return reinterpret_cast<FreeListNode*>(
2021 Memory::Address_at(address() + kNextOffset));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002022 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002023 return reinterpret_cast<FreeListNode*>(
2024 Memory::Address_at(address() + kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002025 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026}
2027
2028
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002029FreeListNode** FreeListNode::next_address() {
ager@chromium.org3811b432009-10-28 14:53:37 +00002030 ASSERT(IsFreeListNode(this));
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002031 if (map() == GetHeap()->raw_unchecked_free_space_map()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002032 ASSERT(Size() >= kNextOffset + kPointerSize);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002033 return reinterpret_cast<FreeListNode**>(address() + kNextOffset);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002034 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002035 return reinterpret_cast<FreeListNode**>(address() + kPointerSize);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002036 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037}
2038
2039
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002040void FreeListNode::set_next(FreeListNode* next) {
2041 ASSERT(IsFreeListNode(this));
2042 // While we are booting the VM the free space map will actually be null. So
2043 // we have to make sure that we don't try to use it for anything at that
2044 // stage.
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002045 if (map() == GetHeap()->raw_unchecked_free_space_map()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002046 ASSERT(map() == NULL || Size() >= kNextOffset + kPointerSize);
2047 Memory::Address_at(address() + kNextOffset) =
2048 reinterpret_cast<Address>(next);
2049 } else {
2050 Memory::Address_at(address() + kPointerSize) =
2051 reinterpret_cast<Address>(next);
2052 }
2053}
2054
2055
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002056intptr_t FreeListCategory::Concatenate(FreeListCategory* category) {
2057 intptr_t free_bytes = 0;
2058 if (category->top_ != NULL) {
2059 ASSERT(category->end_ != NULL);
2060 // This is safe (not going to deadlock) since Concatenate operations
2061 // are never performed on the same free lists at the same time in
2062 // reverse order.
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00002063 LockGuard<Mutex> target_lock_guard(mutex());
2064 LockGuard<Mutex> source_lock_guard(category->mutex());
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002065 free_bytes = category->available();
2066 if (end_ == NULL) {
2067 end_ = category->end();
2068 } else {
2069 category->end()->set_next(top_);
2070 }
2071 top_ = category->top();
2072 available_ += category->available();
2073 category->Reset();
2074 }
2075 return free_bytes;
2076}
2077
2078
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002079void FreeListCategory::Reset() {
2080 top_ = NULL;
2081 end_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082 available_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083}
2084
2085
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002086intptr_t FreeListCategory::EvictFreeListItemsInList(Page* p) {
2087 int sum = 0;
2088 FreeListNode** n = &top_;
2089 while (*n != NULL) {
2090 if (Page::FromAddress((*n)->address()) == p) {
2091 FreeSpace* free_space = reinterpret_cast<FreeSpace*>(*n);
2092 sum += free_space->Size();
2093 *n = (*n)->next();
2094 } else {
2095 n = (*n)->next_address();
2096 }
2097 }
2098 if (top_ == NULL) {
2099 end_ = NULL;
2100 }
2101 available_ -= sum;
2102 return sum;
2103}
2104
2105
2106FreeListNode* FreeListCategory::PickNodeFromList(int *node_size) {
2107 FreeListNode* node = top_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002109 if (node == NULL) return NULL;
2110
2111 while (node != NULL &&
2112 Page::FromAddress(node->address())->IsEvacuationCandidate()) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002113 available_ -= reinterpret_cast<FreeSpace*>(node)->Size();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002114 node = node->next();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002116
2117 if (node != NULL) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002118 set_top(node->next());
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002119 *node_size = reinterpret_cast<FreeSpace*>(node)->Size();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002120 available_ -= *node_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 } else {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002122 set_top(NULL);
2123 }
2124
2125 if (top() == NULL) {
2126 set_end(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127 }
2128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129 return node;
2130}
2131
2132
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002133FreeListNode* FreeListCategory::PickNodeFromList(int size_in_bytes,
2134 int *node_size) {
2135 FreeListNode* node = PickNodeFromList(node_size);
2136 if (node != NULL && *node_size < size_in_bytes) {
2137 Free(node, *node_size);
2138 *node_size = 0;
2139 return NULL;
2140 }
2141 return node;
2142}
2143
2144
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002145void FreeListCategory::Free(FreeListNode* node, int size_in_bytes) {
2146 node->set_next(top_);
2147 top_ = node;
2148 if (end_ == NULL) {
2149 end_ = node;
2150 }
2151 available_ += size_in_bytes;
2152}
2153
2154
2155void FreeListCategory::RepairFreeList(Heap* heap) {
2156 FreeListNode* n = top_;
2157 while (n != NULL) {
2158 Map** map_location = reinterpret_cast<Map**>(n->address());
2159 if (*map_location == NULL) {
2160 *map_location = heap->free_space_map();
2161 } else {
2162 ASSERT(*map_location == heap->free_space_map());
2163 }
2164 n = n->next();
2165 }
2166}
2167
2168
2169FreeList::FreeList(PagedSpace* owner)
2170 : owner_(owner), heap_(owner->heap()) {
2171 Reset();
2172}
2173
2174
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002175intptr_t FreeList::Concatenate(FreeList* free_list) {
2176 intptr_t free_bytes = 0;
2177 free_bytes += small_list_.Concatenate(free_list->small_list());
2178 free_bytes += medium_list_.Concatenate(free_list->medium_list());
2179 free_bytes += large_list_.Concatenate(free_list->large_list());
2180 free_bytes += huge_list_.Concatenate(free_list->huge_list());
2181 return free_bytes;
2182}
2183
2184
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002185void FreeList::Reset() {
2186 small_list_.Reset();
2187 medium_list_.Reset();
2188 large_list_.Reset();
2189 huge_list_.Reset();
2190}
2191
2192
2193int FreeList::Free(Address start, int size_in_bytes) {
2194 if (size_in_bytes == 0) return 0;
2195
2196 FreeListNode* node = FreeListNode::FromAddress(start);
2197 node->set_size(heap_, size_in_bytes);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002198 Page* page = Page::FromAddress(start);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002199
2200 // Early return to drop too-small blocks on the floor.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002201 if (size_in_bytes < kSmallListMin) {
2202 page->add_non_available_small_blocks(size_in_bytes);
2203 return size_in_bytes;
2204 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002205
2206 // Insert other blocks at the head of a free list of the appropriate
2207 // magnitude.
2208 if (size_in_bytes <= kSmallListMax) {
2209 small_list_.Free(node, size_in_bytes);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002210 page->add_available_in_small_free_list(size_in_bytes);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002211 } else if (size_in_bytes <= kMediumListMax) {
2212 medium_list_.Free(node, size_in_bytes);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002213 page->add_available_in_medium_free_list(size_in_bytes);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002214 } else if (size_in_bytes <= kLargeListMax) {
2215 large_list_.Free(node, size_in_bytes);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002216 page->add_available_in_large_free_list(size_in_bytes);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002217 } else {
2218 huge_list_.Free(node, size_in_bytes);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002219 page->add_available_in_huge_free_list(size_in_bytes);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002220 }
2221
2222 ASSERT(IsVeryLong() || available() == SumFreeLists());
2223 return 0;
2224}
2225
2226
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002227FreeListNode* FreeList::FindNodeFor(int size_in_bytes, int* node_size) {
2228 FreeListNode* node = NULL;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002229 Page* page = NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002230
2231 if (size_in_bytes <= kSmallAllocationMax) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002232 node = small_list_.PickNodeFromList(node_size);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002233 if (node != NULL) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002234 ASSERT(size_in_bytes <= *node_size);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002235 page = Page::FromAddress(node->address());
2236 page->add_available_in_small_free_list(-(*node_size));
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002237 ASSERT(IsVeryLong() || available() == SumFreeLists());
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002238 return node;
2239 }
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002240 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002241
2242 if (size_in_bytes <= kMediumAllocationMax) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002243 node = medium_list_.PickNodeFromList(node_size);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002244 if (node != NULL) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002245 ASSERT(size_in_bytes <= *node_size);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002246 page = Page::FromAddress(node->address());
2247 page->add_available_in_medium_free_list(-(*node_size));
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002248 ASSERT(IsVeryLong() || available() == SumFreeLists());
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002249 return node;
2250 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002251 }
2252
2253 if (size_in_bytes <= kLargeAllocationMax) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002254 node = large_list_.PickNodeFromList(node_size);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002255 if (node != NULL) {
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002256 ASSERT(size_in_bytes <= *node_size);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002257 page = Page::FromAddress(node->address());
2258 page->add_available_in_large_free_list(-(*node_size));
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002259 ASSERT(IsVeryLong() || available() == SumFreeLists());
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002260 return node;
2261 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002262 }
2263
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002264 int huge_list_available = huge_list_.available();
2265 for (FreeListNode** cur = huge_list_.GetTopAddress();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002266 *cur != NULL;
2267 cur = (*cur)->next_address()) {
2268 FreeListNode* cur_node = *cur;
2269 while (cur_node != NULL &&
2270 Page::FromAddress(cur_node->address())->IsEvacuationCandidate()) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002271 int size = reinterpret_cast<FreeSpace*>(cur_node)->Size();
2272 huge_list_available -= size;
2273 page = Page::FromAddress(cur_node->address());
2274 page->add_available_in_huge_free_list(-size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002275 cur_node = cur_node->next();
2276 }
2277
2278 *cur = cur_node;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002279 if (cur_node == NULL) {
2280 huge_list_.set_end(NULL);
2281 break;
2282 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002283
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002284 ASSERT((*cur)->map() == heap_->raw_unchecked_free_space_map());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002285 FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(*cur);
2286 int size = cur_as_free_space->Size();
2287 if (size >= size_in_bytes) {
2288 // Large enough node found. Unlink it from the list.
2289 node = *cur;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002290 *cur = node->next();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002291 *node_size = size;
2292 huge_list_available -= size;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002293 page = Page::FromAddress(node->address());
2294 page->add_available_in_huge_free_list(-size);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002295 break;
2296 }
2297 }
2298
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002299 if (huge_list_.top() == NULL) {
2300 huge_list_.set_end(NULL);
2301 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002302 huge_list_.set_available(huge_list_available);
danno@chromium.orgfe578672013-06-15 14:38:35 +00002303
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002304 if (node != NULL) {
2305 ASSERT(IsVeryLong() || available() == SumFreeLists());
2306 return node;
2307 }
2308
2309 if (size_in_bytes <= kSmallListMax) {
2310 node = small_list_.PickNodeFromList(size_in_bytes, node_size);
2311 if (node != NULL) {
2312 ASSERT(size_in_bytes <= *node_size);
2313 page = Page::FromAddress(node->address());
2314 page->add_available_in_small_free_list(-(*node_size));
2315 }
2316 } else if (size_in_bytes <= kMediumListMax) {
2317 node = medium_list_.PickNodeFromList(size_in_bytes, node_size);
2318 if (node != NULL) {
2319 ASSERT(size_in_bytes <= *node_size);
2320 page = Page::FromAddress(node->address());
2321 page->add_available_in_medium_free_list(-(*node_size));
2322 }
2323 } else if (size_in_bytes <= kLargeListMax) {
2324 node = large_list_.PickNodeFromList(size_in_bytes, node_size);
2325 if (node != NULL) {
2326 ASSERT(size_in_bytes <= *node_size);
2327 page = Page::FromAddress(node->address());
2328 page->add_available_in_large_free_list(-(*node_size));
2329 }
2330 }
2331
2332 ASSERT(IsVeryLong() || available() == SumFreeLists());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002333 return node;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002334}
2335
2336
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002337// Allocation on the old space free list. If it succeeds then a new linear
2338// allocation space has been set up with the top and limit of the space. If
2339// the allocation fails then NULL is returned, and the caller can perform a GC
2340// or allocate a new page before retrying.
2341HeapObject* FreeList::Allocate(int size_in_bytes) {
2342 ASSERT(0 < size_in_bytes);
2343 ASSERT(size_in_bytes <= kMaxBlockSize);
2344 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2345 // Don't free list allocate if there is linear space available.
2346 ASSERT(owner_->limit() - owner_->top() < size_in_bytes);
2347
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002348 int old_linear_size = static_cast<int>(owner_->limit() - owner_->top());
2349 // Mark the old linear allocation area with a free space map so it can be
2350 // skipped when scanning the heap. This also puts it back in the free list
2351 // if it is big enough.
2352 owner_->Free(owner_->top(), old_linear_size);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002353
danno@chromium.orgeb831462012-08-24 11:57:08 +00002354 owner_->heap()->incremental_marking()->OldSpaceStep(
2355 size_in_bytes - old_linear_size);
2356
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002357 int new_node_size = 0;
2358 FreeListNode* new_node = FindNodeFor(size_in_bytes, &new_node_size);
2359 if (new_node == NULL) {
2360 owner_->SetTop(NULL, NULL);
2361 return NULL;
2362 }
2363
2364 int bytes_left = new_node_size - size_in_bytes;
2365 ASSERT(bytes_left >= 0);
2366
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002367#ifdef DEBUG
2368 for (int i = 0; i < size_in_bytes / kPointerSize; i++) {
2369 reinterpret_cast<Object**>(new_node->address())[i] =
2370 Smi::FromInt(kCodeZapValue);
2371 }
2372#endif
2373
ricow@chromium.orgfa52deb2011-10-11 19:09:42 +00002374 // The old-space-step might have finished sweeping and restarted marking.
2375 // Verify that it did not turn the page of the new node into an evacuation
2376 // candidate.
2377 ASSERT(!MarkCompactCollector::IsOnEvacuationCandidate(new_node));
2378
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002379 const int kThreshold = IncrementalMarking::kAllocatedThreshold;
2380
2381 // Memory in the linear allocation area is counted as allocated. We may free
2382 // a little of this again immediately - see below.
2383 owner_->Allocate(new_node_size);
2384
2385 if (bytes_left > kThreshold &&
2386 owner_->heap()->incremental_marking()->IsMarkingIncomplete() &&
2387 FLAG_incremental_marking_steps) {
2388 int linear_size = owner_->RoundSizeDownToObjectAlignment(kThreshold);
2389 // We don't want to give too large linear areas to the allocator while
2390 // incremental marking is going on, because we won't check again whether
2391 // we want to do another increment until the linear area is used up.
2392 owner_->Free(new_node->address() + size_in_bytes + linear_size,
2393 new_node_size - size_in_bytes - linear_size);
2394 owner_->SetTop(new_node->address() + size_in_bytes,
2395 new_node->address() + size_in_bytes + linear_size);
2396 } else if (bytes_left > 0) {
2397 // Normally we give the rest of the node to the allocator as its new
2398 // linear allocation area.
2399 owner_->SetTop(new_node->address() + size_in_bytes,
2400 new_node->address() + new_node_size);
2401 } else {
2402 // TODO(gc) Try not freeing linear allocation region when bytes_left
2403 // are zero.
2404 owner_->SetTop(NULL, NULL);
2405 }
2406
2407 return new_node;
2408}
2409
2410
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002411intptr_t FreeList::EvictFreeListItems(Page* p) {
2412 intptr_t sum = huge_list_.EvictFreeListItemsInList(p);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002413 p->set_available_in_huge_free_list(0);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002414
2415 if (sum < p->area_size()) {
2416 sum += small_list_.EvictFreeListItemsInList(p) +
2417 medium_list_.EvictFreeListItemsInList(p) +
2418 large_list_.EvictFreeListItemsInList(p);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002419 p->set_available_in_small_free_list(0);
2420 p->set_available_in_medium_free_list(0);
2421 p->set_available_in_large_free_list(0);
danno@chromium.org2c456792011-11-11 12:00:53 +00002422 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002423
danno@chromium.org2c456792011-11-11 12:00:53 +00002424 return sum;
2425}
2426
2427
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002428void FreeList::RepairLists(Heap* heap) {
2429 small_list_.RepairFreeList(heap);
2430 medium_list_.RepairFreeList(heap);
2431 large_list_.RepairFreeList(heap);
2432 huge_list_.RepairFreeList(heap);
danno@chromium.org2c456792011-11-11 12:00:53 +00002433}
2434
2435
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002436#ifdef DEBUG
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002437intptr_t FreeListCategory::SumFreeList() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002438 intptr_t sum = 0;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002439 FreeListNode* cur = top_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002440 while (cur != NULL) {
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002441 ASSERT(cur->map() == cur->GetHeap()->raw_unchecked_free_space_map());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002442 FreeSpace* cur_as_free_space = reinterpret_cast<FreeSpace*>(cur);
2443 sum += cur_as_free_space->Size();
2444 cur = cur->next();
2445 }
2446 return sum;
2447}
2448
2449
2450static const int kVeryLongFreeList = 500;
2451
2452
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002453int FreeListCategory::FreeListLength() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002454 int length = 0;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002455 FreeListNode* cur = top_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002456 while (cur != NULL) {
2457 length++;
2458 cur = cur->next();
2459 if (length == kVeryLongFreeList) return length;
2460 }
2461 return length;
2462}
2463
2464
2465bool FreeList::IsVeryLong() {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002466 if (small_list_.FreeListLength() == kVeryLongFreeList) return true;
2467 if (medium_list_.FreeListLength() == kVeryLongFreeList) return true;
2468 if (large_list_.FreeListLength() == kVeryLongFreeList) return true;
2469 if (huge_list_.FreeListLength() == kVeryLongFreeList) return true;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002470 return false;
2471}
2472
2473
2474// This can take a very long time because it is linear in the number of entries
2475// on the free list, so it should not be called if FreeListLength returns
2476// kVeryLongFreeList.
2477intptr_t FreeList::SumFreeLists() {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002478 intptr_t sum = small_list_.SumFreeList();
2479 sum += medium_list_.SumFreeList();
2480 sum += large_list_.SumFreeList();
2481 sum += huge_list_.SumFreeList();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002482 return sum;
2483}
2484#endif
2485
2486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487// -----------------------------------------------------------------------------
2488// OldSpace implementation
2489
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002490bool NewSpace::ReserveSpace(int bytes) {
2491 // We can't reliably unpack a partial snapshot that needs more new space
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002492 // space than the minimum NewSpace size. The limit can be set lower than
2493 // the end of new space either because there is more space on the next page
2494 // or because we have lowered the limit in order to get periodic incremental
2495 // marking. The most reliable way to ensure that there is linear space is
2496 // to do the allocation, then rewind the limit.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002497 ASSERT(bytes <= InitialCapacity());
danno@chromium.orgc612e022011-11-10 11:38:15 +00002498 MaybeObject* maybe = AllocateRaw(bytes);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002499 Object* object = NULL;
2500 if (!maybe->ToObject(&object)) return false;
2501 HeapObject* allocation = HeapObject::cast(object);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002502 Address top = allocation_info_.top;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002503 if ((top - bytes) == allocation->address()) {
2504 allocation_info_.top = allocation->address();
2505 return true;
2506 }
2507 // There may be a borderline case here where the allocation succeeded, but
2508 // the limit and top have moved on to a new page. In that case we try again.
2509 return ReserveSpace(bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002510}
2511
2512
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002513void PagedSpace::PrepareForMarkCompact() {
2514 // We don't have a linear allocation area while sweeping. It will be restored
2515 // on the first allocation after the sweep.
2516 // Mark the old linear allocation area with a free space map so it can be
2517 // skipped when scanning the heap.
2518 int old_linear_size = static_cast<int>(limit() - top());
2519 Free(top(), old_linear_size);
2520 SetTop(NULL, NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002521
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002522 // Stop lazy sweeping and clear marking bits for unswept pages.
2523 if (first_unswept_page_ != NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002524 Page* p = first_unswept_page_;
2525 do {
2526 // Do not use ShouldBeSweptLazily predicate here.
2527 // New evacuation candidates were selected but they still have
2528 // to be swept before collection starts.
2529 if (!p->WasSwept()) {
2530 Bitmap::Clear(p);
2531 if (FLAG_gc_verbose) {
2532 PrintF("Sweeping 0x%" V8PRIxPTR " lazily abandoned.\n",
2533 reinterpret_cast<intptr_t>(p));
2534 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002535 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002536 p = p->next_page();
danno@chromium.org2c456792011-11-11 12:00:53 +00002537 } while (p != anchor());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002538 }
danno@chromium.org2c456792011-11-11 12:00:53 +00002539 first_unswept_page_ = Page::FromAddress(NULL);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002540 unswept_free_bytes_ = 0;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002541
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002542 // Clear the free list before a full GC---it will be rebuilt afterward.
2543 free_list_.Reset();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002544}
2545
2546
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002547bool PagedSpace::ReserveSpace(int size_in_bytes) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002548 ASSERT(size_in_bytes <= AreaSize());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002549 ASSERT(size_in_bytes == RoundSizeDownToObjectAlignment(size_in_bytes));
2550 Address current_top = allocation_info_.top;
2551 Address new_top = current_top + size_in_bytes;
2552 if (new_top <= allocation_info_.limit) return true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002553
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002554 HeapObject* new_area = free_list_.Allocate(size_in_bytes);
2555 if (new_area == NULL) new_area = SlowAllocateRaw(size_in_bytes);
2556 if (new_area == NULL) return false;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002557
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002558 int old_linear_size = static_cast<int>(limit() - top());
2559 // Mark the old linear allocation area with a free space so it can be
2560 // skipped when scanning the heap. This also puts it back in the free list
2561 // if it is big enough.
2562 Free(top(), old_linear_size);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002563
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002564 SetTop(new_area->address(), new_area->address() + size_in_bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002565 return true;
2566}
2567
2568
ulan@chromium.org750145a2013-03-07 15:14:13 +00002569intptr_t PagedSpace::SizeOfObjects() {
2570 ASSERT(!heap()->IsSweepingComplete() || (unswept_free_bytes_ == 0));
2571 return Size() - unswept_free_bytes_ - (limit() - top());
2572}
2573
2574
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002575// After we have booted, we have created a map which represents free space
2576// on the heap. If there was already a free list then the elements on it
2577// were created with the wrong FreeSpaceMap (normally NULL), so we need to
2578// fix them.
2579void PagedSpace::RepairFreeListsAfterBoot() {
2580 free_list_.RepairLists(heap());
2581}
2582
2583
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002584// You have to call this last, since the implementation from PagedSpace
2585// doesn't know that memory was 'promised' to large object space.
2586bool LargeObjectSpace::ReserveSpace(int bytes) {
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00002587 return heap()->OldGenerationCapacityAvailable() >= bytes &&
2588 (!heap()->incremental_marking()->IsStopped() ||
2589 heap()->OldGenerationSpaceAvailable() >= bytes);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002590}
2591
2592
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002593bool PagedSpace::AdvanceSweeper(intptr_t bytes_to_sweep) {
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002594 if (IsLazySweepingComplete()) return true;
kasper.lund7276f142008-07-30 08:49:36 +00002595
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002596 intptr_t freed_bytes = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002597 Page* p = first_unswept_page_;
2598 do {
2599 Page* next_page = p->next_page();
2600 if (ShouldBeSweptLazily(p)) {
2601 if (FLAG_gc_verbose) {
2602 PrintF("Sweeping 0x%" V8PRIxPTR " lazily advanced.\n",
2603 reinterpret_cast<intptr_t>(p));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002604 }
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002605 DecreaseUnsweptFreeBytes(p);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002606 freed_bytes +=
2607 MarkCompactCollector::
2608 SweepConservatively<MarkCompactCollector::SWEEP_SEQUENTIALLY>(
2609 this, NULL, p);
ager@chromium.org3811b432009-10-28 14:53:37 +00002610 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002611 p = next_page;
danno@chromium.org2c456792011-11-11 12:00:53 +00002612 } while (p != anchor() && freed_bytes < bytes_to_sweep);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002613
danno@chromium.org2c456792011-11-11 12:00:53 +00002614 if (p == anchor()) {
2615 first_unswept_page_ = Page::FromAddress(NULL);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002616 } else {
2617 first_unswept_page_ = p;
kasper.lund7276f142008-07-30 08:49:36 +00002618 }
2619
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002620 heap()->FreeQueuedChunks();
2621
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002622 return IsLazySweepingComplete();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002623}
2624
2625
2626void PagedSpace::EvictEvacuationCandidatesFromFreeLists() {
2627 if (allocation_info_.top >= allocation_info_.limit) return;
2628
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002629 if (Page::FromAllocationTop(allocation_info_.top)->IsEvacuationCandidate()) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002630 // Create filler object to keep page iterable if it was iterable.
2631 int remaining =
2632 static_cast<int>(allocation_info_.limit - allocation_info_.top);
2633 heap()->CreateFillerObjectAt(allocation_info_.top, remaining);
2634
2635 allocation_info_.top = NULL;
2636 allocation_info_.limit = NULL;
2637 }
2638}
2639
2640
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002641bool PagedSpace::EnsureSweeperProgress(intptr_t size_in_bytes) {
2642 MarkCompactCollector* collector = heap()->mark_compact_collector();
2643 if (collector->AreSweeperThreadsActivated()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002644 if (collector->IsConcurrentSweepingInProgress()) {
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002645 if (collector->StealMemoryFromSweeperThreads(this) < size_in_bytes) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002646 if (!collector->sequential_sweeping()) {
2647 collector->WaitUntilSweepingCompleted();
ulan@chromium.org750145a2013-03-07 15:14:13 +00002648 return true;
2649 }
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002650 }
2651 return false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002652 }
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002653 return true;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002654 } else {
2655 return AdvanceSweeper(size_in_bytes);
2656 }
2657}
2658
2659
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002660HeapObject* PagedSpace::SlowAllocateRaw(int size_in_bytes) {
2661 // Allocation in this space has failed.
2662
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +00002663 // If there are unswept pages advance lazy sweeper a bounded number of times
2664 // until we find a size_in_bytes contiguous piece of memory
2665 const int kMaxSweepingTries = 5;
2666 bool sweeping_complete = false;
2667
2668 for (int i = 0; i < kMaxSweepingTries && !sweeping_complete; i++) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002669 sweeping_complete = EnsureSweeperProgress(size_in_bytes);
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002670
2671 // Retry the free list allocation.
2672 HeapObject* object = free_list_.Allocate(size_in_bytes);
2673 if (object != NULL) return object;
2674 }
2675
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002676 // Free list allocation failed and there is no next page. Fail if we have
2677 // hit the old generation size limit that should cause a garbage
2678 // collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002679 if (!heap()->always_allocate() &&
2680 heap()->OldGenerationAllocationLimitReached()) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002681 return NULL;
2682 }
2683
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002684 // Try to expand the space and allocate in the new next page.
2685 if (Expand()) {
jkummerow@chromium.org25b0e212013-10-04 15:38:52 +00002686 ASSERT(CountTotalPages() > 1 || size_in_bytes <= free_list_.available());
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002687 return free_list_.Allocate(size_in_bytes);
2688 }
2689
2690 // Last ditch, sweep all the remaining pages to try to find space. This may
2691 // cause a pause.
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00002692 if (!IsLazySweepingComplete()) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002693 EnsureSweeperProgress(kMaxInt);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002694
2695 // Retry the free list allocation.
2696 HeapObject* object = free_list_.Allocate(size_in_bytes);
2697 if (object != NULL) return object;
kasper.lund7276f142008-07-30 08:49:36 +00002698 }
2699
2700 // Finally, fail.
2701 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702}
2703
2704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705#ifdef DEBUG
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00002706void PagedSpace::ReportCodeStatistics(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002707 CommentStatistic* comments_statistics =
2708 isolate->paged_space_comments_statistics();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002709 ReportCodeKindStatistics(isolate->code_kind_statistics());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 PrintF("Code comment statistics (\" [ comment-txt : size/ "
2711 "count (average)\"):\n");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002712 for (int i = 0; i <= CommentStatistic::kMaxComments; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 const CommentStatistic& cs = comments_statistics[i];
2714 if (cs.size > 0) {
2715 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
2716 cs.size/cs.count);
2717 }
2718 }
2719 PrintF("\n");
2720}
2721
2722
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00002723void PagedSpace::ResetCodeStatistics(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002724 CommentStatistic* comments_statistics =
2725 isolate->paged_space_comments_statistics();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002726 ClearCodeKindStatistics(isolate->code_kind_statistics());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002727 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
2728 comments_statistics[i].Clear();
2729 }
2730 comments_statistics[CommentStatistic::kMaxComments].comment = "Unknown";
2731 comments_statistics[CommentStatistic::kMaxComments].size = 0;
2732 comments_statistics[CommentStatistic::kMaxComments].count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733}
2734
2735
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002736// Adds comment to 'comment_statistics' table. Performance OK as long as
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737// 'kMaxComments' is small
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002738static void EnterComment(Isolate* isolate, const char* comment, int delta) {
2739 CommentStatistic* comments_statistics =
2740 isolate->paged_space_comments_statistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741 // Do not count empty comments
2742 if (delta <= 0) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002743 CommentStatistic* cs = &comments_statistics[CommentStatistic::kMaxComments];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 // Search for a free or matching entry in 'comments_statistics': 'cs'
2745 // points to result.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002746 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 if (comments_statistics[i].comment == NULL) {
2748 cs = &comments_statistics[i];
2749 cs->comment = comment;
2750 break;
2751 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2752 cs = &comments_statistics[i];
2753 break;
2754 }
2755 }
2756 // Update entry for 'comment'
2757 cs->size += delta;
2758 cs->count += 1;
2759}
2760
2761
2762// Call for each nested comment start (start marked with '[ xxx', end marked
2763// with ']'. RelocIterator 'it' must point to a comment reloc info.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002764static void CollectCommentStatistics(Isolate* isolate, RelocIterator* it) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002766 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2768 if (tmp[0] != '[') {
2769 // Not a nested comment; skip
2770 return;
2771 }
2772
2773 // Search for end of nested comment or a new nested comment
2774 const char* const comment_txt =
2775 reinterpret_cast<const char*>(it->rinfo()->data());
2776 const byte* prev_pc = it->rinfo()->pc();
2777 int flat_delta = 0;
2778 it->next();
2779 while (true) {
2780 // All nested comments must be terminated properly, and therefore exit
2781 // from loop.
2782 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002783 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784 const char* const txt =
2785 reinterpret_cast<const char*>(it->rinfo()->data());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002786 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 if (txt[0] == ']') break; // End of nested comment
2788 // A new comment
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002789 CollectCommentStatistics(isolate, it);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 // Skip code that was covered with previous comment
2791 prev_pc = it->rinfo()->pc();
2792 }
2793 it->next();
2794 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002795 EnterComment(isolate, comment_txt, flat_delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796}
2797
2798
2799// Collects code size statistics:
2800// - by code kind
2801// - by code comment
2802void PagedSpace::CollectCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002803 Isolate* isolate = heap()->isolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002804 HeapObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002805 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 if (obj->IsCode()) {
2807 Code* code = Code::cast(obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002808 isolate->code_kind_statistics()[code->kind()] += code->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809 RelocIterator it(code);
2810 int delta = 0;
2811 const byte* prev_pc = code->instruction_start();
2812 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002813 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002814 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002815 CollectCommentStatistics(isolate, &it);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816 prev_pc = it.rinfo()->pc();
2817 }
2818 it.next();
2819 }
2820
2821 ASSERT(code->instruction_start() <= prev_pc &&
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00002822 prev_pc <= code->instruction_end());
2823 delta += static_cast<int>(code->instruction_end() - prev_pc);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002824 EnterComment(isolate, "NoComment", delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 }
2826 }
2827}
2828
2829
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002830void PagedSpace::ReportStatistics() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002831 int pct = static_cast<int>(Available() * 100 / Capacity());
2832 PrintF(" capacity: %" V8_PTR_PREFIX "d"
2833 ", waste: %" V8_PTR_PREFIX "d"
2834 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 Capacity(), Waste(), Available(), pct);
2836
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002837 if (was_swept_conservatively_) return;
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00002838 ClearHistograms(heap()->isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839 HeapObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002840 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next())
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002841 CollectHistogramInfo(obj);
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00002842 ReportHistogram(heap()->isolate(), true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002844#endif
2845
2846// -----------------------------------------------------------------------------
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002847// FixedSpace implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002848
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002849void FixedSpace::PrepareForMarkCompact() {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002850 // Call prepare of the super class.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002851 PagedSpace::PrepareForMarkCompact();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002852
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002853 // During a non-compacting collection, everything below the linear
2854 // allocation pointer except wasted top-of-page blocks is considered
2855 // allocated and we will rediscover available bytes during the
2856 // collection.
2857 accounting_stats_.AllocateBytes(free_list_.available());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858
kasper.lund7276f142008-07-30 08:49:36 +00002859 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861}
2862
2863
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002864// -----------------------------------------------------------------------------
2865// MapSpace implementation
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002866// TODO(mvstanton): this is weird...the compiler can't make a vtable unless
2867// there is at least one non-inlined virtual function. I would prefer to hide
2868// the VerifyObject definition behind VERIFY_HEAP.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002869
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002870void MapSpace::VerifyObject(HeapObject* object) {
verwaest@chromium.orgec6855e2013-08-22 12:26:58 +00002871 CHECK(object->IsMap());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002872}
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002873
2874
2875// -----------------------------------------------------------------------------
danno@chromium.org41728482013-06-12 22:31:22 +00002876// CellSpace and PropertyCellSpace implementation
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002877// TODO(mvstanton): this is weird...the compiler can't make a vtable unless
2878// there is at least one non-inlined virtual function. I would prefer to hide
2879// the VerifyObject definition behind VERIFY_HEAP.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002880
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002881void CellSpace::VerifyObject(HeapObject* object) {
verwaest@chromium.orgec6855e2013-08-22 12:26:58 +00002882 CHECK(object->IsCell());
danno@chromium.org41728482013-06-12 22:31:22 +00002883}
2884
2885
2886void PropertyCellSpace::VerifyObject(HeapObject* object) {
verwaest@chromium.orgec6855e2013-08-22 12:26:58 +00002887 CHECK(object->IsPropertyCell());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002888}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889
2890
2891// -----------------------------------------------------------------------------
2892// LargeObjectIterator
2893
2894LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002895 current_ = space->first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896 size_func_ = NULL;
2897}
2898
2899
2900LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2901 HeapObjectCallback size_func) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002902 current_ = space->first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903 size_func_ = size_func;
2904}
2905
2906
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002907HeapObject* LargeObjectIterator::Next() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002908 if (current_ == NULL) return NULL;
2909
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 HeapObject* object = current_->GetObject();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002911 current_ = current_->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 return object;
2913}
2914
2915
2916// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917// LargeObjectSpace
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002918static bool ComparePointers(void* key1, void* key2) {
2919 return key1 == key2;
2920}
2921
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002923LargeObjectSpace::LargeObjectSpace(Heap* heap,
2924 intptr_t max_capacity,
2925 AllocationSpace id)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002926 : Space(heap, id, NOT_EXECUTABLE), // Managed on a per-allocation basis
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002927 max_capacity_(max_capacity),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002928 first_page_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929 size_(0),
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002930 page_count_(0),
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002931 objects_size_(0),
2932 chunk_map_(ComparePointers, 1024) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002933
2934
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002935bool LargeObjectSpace::SetUp() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002936 first_page_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 size_ = 0;
2938 page_count_ = 0;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002939 objects_size_ = 0;
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002940 chunk_map_.Clear();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941 return true;
2942}
2943
2944
2945void LargeObjectSpace::TearDown() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002946 while (first_page_ != NULL) {
2947 LargePage* page = first_page_;
2948 first_page_ = first_page_->next_page();
2949 LOG(heap()->isolate(), DeleteEvent("LargeObjectChunk", page->address()));
2950
2951 ObjectSpace space = static_cast<ObjectSpace>(1 << identity());
2952 heap()->isolate()->memory_allocator()->PerformAllocationCallback(
2953 space, kAllocationActionFree, page->size());
2954 heap()->isolate()->memory_allocator()->Free(page);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002956 SetUp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957}
2958
2959
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002960MaybeObject* LargeObjectSpace::AllocateRaw(int object_size,
2961 Executability executable) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002962 // Check if we want to force a GC before growing the old space further.
2963 // If so, fail the allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002964 if (!heap()->always_allocate() &&
2965 heap()->OldGenerationAllocationLimitReached()) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002966 return Failure::RetryAfterGC(identity());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002967 }
2968
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002969 if (Size() + object_size > max_capacity_) {
2970 return Failure::RetryAfterGC(identity());
2971 }
2972
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002973 LargePage* page = heap()->isolate()->memory_allocator()->
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002974 AllocateLargePage(object_size, this, executable);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002975 if (page == NULL) return Failure::RetryAfterGC(identity());
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002976 ASSERT(page->area_size() >= object_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002978 size_ += static_cast<int>(page->size());
2979 objects_size_ += object_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002980 page_count_++;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002981 page->set_next_page(first_page_);
2982 first_page_ = page;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002983
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00002984 // Register all MemoryChunk::kAlignment-aligned chunks covered by
2985 // this large page in the chunk map.
2986 uintptr_t base = reinterpret_cast<uintptr_t>(page) / MemoryChunk::kAlignment;
2987 uintptr_t limit = base + (page->size() - 1) / MemoryChunk::kAlignment;
2988 for (uintptr_t key = base; key <= limit; key++) {
2989 HashMap::Entry* entry = chunk_map_.Lookup(reinterpret_cast<void*>(key),
2990 static_cast<uint32_t>(key),
2991 true);
2992 ASSERT(entry != NULL);
2993 entry->value = page;
2994 }
2995
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002996 HeapObject* object = page->GetObject();
2997
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002998 if (Heap::ShouldZapGarbage()) {
2999 // Make the object consistent so the heap can be verified in OldSpaceStep.
3000 // We only need to do this in debug builds or if verify_heap is on.
3001 reinterpret_cast<Object**>(object->address())[0] =
3002 heap()->fixed_array_map();
3003 reinterpret_cast<Object**>(object->address())[1] = Smi::FromInt(0);
3004 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003005
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003006 heap()->incremental_marking()->OldSpaceStep(object_size);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003007 return object;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008}
3009
3010
danno@chromium.org72204d52012-10-31 10:02:10 +00003011size_t LargeObjectSpace::CommittedPhysicalMemory() {
3012 if (!VirtualMemory::HasLazyCommits()) return CommittedMemory();
3013 size_t size = 0;
3014 LargePage* current = first_page_;
3015 while (current != NULL) {
3016 size += current->CommittedPhysicalMemory();
3017 current = current->next_page();
3018 }
3019 return size;
3020}
3021
3022
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023// GC support
lrn@chromium.org303ada72010-10-27 09:33:13 +00003024MaybeObject* LargeObjectSpace::FindObject(Address a) {
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00003025 LargePage* page = FindPage(a);
3026 if (page != NULL) {
3027 return page->GetObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003028 }
3029 return Failure::Exception();
3030}
3031
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003032
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00003033LargePage* LargeObjectSpace::FindPage(Address a) {
3034 uintptr_t key = reinterpret_cast<uintptr_t>(a) / MemoryChunk::kAlignment;
3035 HashMap::Entry* e = chunk_map_.Lookup(reinterpret_cast<void*>(key),
3036 static_cast<uint32_t>(key),
3037 false);
3038 if (e != NULL) {
3039 ASSERT(e->value != NULL);
3040 LargePage* page = reinterpret_cast<LargePage*>(e->value);
3041 ASSERT(page->is_valid());
3042 if (page->Contains(a)) {
3043 return page;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003044 }
3045 }
3046 return NULL;
3047}
3048
3049
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050void LargeObjectSpace::FreeUnmarkedObjects() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003051 LargePage* previous = NULL;
3052 LargePage* current = first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053 while (current != NULL) {
3054 HeapObject* object = current->GetObject();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003055 // Can this large page contain pointers to non-trivial objects. No other
3056 // pointer object is this big.
3057 bool is_pointer_object = object->IsFixedArray();
3058 MarkBit mark_bit = Marking::MarkBitFrom(object);
3059 if (mark_bit.Get()) {
3060 mark_bit.Clear();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003061 Page::FromAddress(object->address())->ResetProgressBar();
3062 Page::FromAddress(object->address())->ResetLiveBytes();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003063 previous = current;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003064 current = current->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003066 LargePage* page = current;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003067 // Cut the chunk out from the chunk list.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003068 current = current->next_page();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069 if (previous == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003070 first_page_ = current;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003072 previous->set_next_page(current);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073 }
3074
3075 // Free the chunk.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003076 heap()->mark_compact_collector()->ReportDeleteIfNeeded(
3077 object, heap()->isolate());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003078 size_ -= static_cast<int>(page->size());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003079 objects_size_ -= object->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080 page_count_--;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003081
jkummerow@chromium.org531dfe82012-03-20 13:01:16 +00003082 // Remove entries belonging to this page.
3083 // Use variable alignment to help pass length check (<= 80 characters)
3084 // of single line in tools/presubmit.py.
3085 const intptr_t alignment = MemoryChunk::kAlignment;
3086 uintptr_t base = reinterpret_cast<uintptr_t>(page)/alignment;
3087 uintptr_t limit = base + (page->size()-1)/alignment;
3088 for (uintptr_t key = base; key <= limit; key++) {
3089 chunk_map_.Remove(reinterpret_cast<void*>(key),
3090 static_cast<uint32_t>(key));
3091 }
3092
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003093 if (is_pointer_object) {
3094 heap()->QueueMemoryChunkForFree(page);
3095 } else {
3096 heap()->isolate()->memory_allocator()->Free(page);
3097 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098 }
3099 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003100 heap()->FreeQueuedChunks();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101}
3102
3103
3104bool LargeObjectSpace::Contains(HeapObject* object) {
3105 Address address = object->address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003106 MemoryChunk* chunk = MemoryChunk::FromAddress(address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003108 bool owned = (chunk->owner() == this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003110 SLOW_ASSERT(!owned || !FindObject(address)->IsFailure());
3111
3112 return owned;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003113}
3114
3115
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003116#ifdef VERIFY_HEAP
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117// We do not assume that the large object iterator works, because it depends
3118// on the invariants we are checking during verification.
3119void LargeObjectSpace::Verify() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003120 for (LargePage* chunk = first_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003121 chunk != NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003122 chunk = chunk->next_page()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 // Each chunk contains an object that starts at the large object page's
3124 // object area start.
3125 HeapObject* object = chunk->GetObject();
3126 Page* page = Page::FromAddress(object->address());
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003127 CHECK(object->address() == page->area_start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003128
3129 // The first word should be a map, and we expect all map pointers to be
3130 // in map space.
3131 Map* map = object->map();
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003132 CHECK(map->IsMap());
3133 CHECK(heap()->map_space()->Contains(map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003134
ager@chromium.orga1645e22009-09-09 19:27:10 +00003135 // We have only code, sequential strings, external strings
3136 // (sequential strings that have been morphed into external
3137 // strings), fixed arrays, and byte arrays in large object space.
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003138 CHECK(object->IsCode() || object->IsSeqString() ||
ager@chromium.orga1645e22009-09-09 19:27:10 +00003139 object->IsExternalString() || object->IsFixedArray() ||
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003140 object->IsFixedDoubleArray() || object->IsByteArray());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141
3142 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003143 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144
3145 // Byte arrays and strings don't have interior pointers.
3146 if (object->IsCode()) {
3147 VerifyPointersVisitor code_visitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003148 object->IterateBody(map->instance_type(),
3149 object->Size(),
3150 &code_visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003151 } else if (object->IsFixedArray()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003152 FixedArray* array = FixedArray::cast(object);
3153 for (int j = 0; j < array->length(); j++) {
3154 Object* element = array->get(j);
3155 if (element->IsHeapObject()) {
3156 HeapObject* element_object = HeapObject::cast(element);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003157 CHECK(heap()->Contains(element_object));
3158 CHECK(element_object->map()->IsMap());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003159 }
3160 }
3161 }
3162 }
3163}
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003164#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165
3166
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00003167#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003168void LargeObjectSpace::Print() {
3169 LargeObjectIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003170 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003171 obj->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003172 }
3173}
3174
3175
3176void LargeObjectSpace::ReportStatistics() {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00003177 PrintF(" size: %" V8_PTR_PREFIX "d\n", size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 int num_objects = 0;
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003179 ClearHistograms(heap()->isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180 LargeObjectIterator it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003181 for (HeapObject* obj = it.Next(); obj != NULL; obj = it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003182 num_objects++;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003183 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184 }
3185
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003186 PrintF(" number of objects %d, "
3187 "size of objects %" V8_PTR_PREFIX "d\n", num_objects, objects_size_);
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003188 if (num_objects > 0) ReportHistogram(heap()->isolate(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003189}
3190
3191
3192void LargeObjectSpace::CollectCodeStatistics() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003193 Isolate* isolate = heap()->isolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194 LargeObjectIterator obj_it(this);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003195 for (HeapObject* obj = obj_it.Next(); obj != NULL; obj = obj_it.Next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003196 if (obj->IsCode()) {
3197 Code* code = Code::cast(obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003198 isolate->code_kind_statistics()[code->kind()] += code->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003199 }
3200 }
3201}
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003202
3203
3204void Page::Print() {
3205 // Make a best-effort to print the objects in the page.
3206 PrintF("Page@%p in %s\n",
3207 this->address(),
3208 AllocationSpaceName(this->owner()->identity()));
3209 printf(" --------------------------------------\n");
3210 HeapObjectIterator objects(this, heap()->GcSafeSizeOfOldObjectFunction());
3211 unsigned mark_size = 0;
3212 for (HeapObject* object = objects.Next();
3213 object != NULL;
3214 object = objects.Next()) {
3215 bool is_marked = Marking::MarkBitFrom(object).Get();
3216 PrintF(" %c ", (is_marked ? '!' : ' ')); // Indent a little.
3217 if (is_marked) {
3218 mark_size += heap()->GcSafeSizeOfOldObjectFunction()(object);
3219 }
3220 object->ShortPrint();
3221 PrintF("\n");
3222 }
3223 printf(" --------------------------------------\n");
3224 printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes());
3225}
3226
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003227#endif // DEBUG
3228
3229} } // namespace v8::internal