blob: 97c6d2ac19f8c90ad80ae0160eb788c6db42adbf [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Steve Block1e0659c2011-05-24 12:43:12 +010030#include "liveobjectlist-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000031#include "macro-assembler.h"
32#include "mark-compact.h"
33#include "platform.h"
34
35namespace v8 {
36namespace internal {
37
Ben Murdoch85b71792012-04-11 18:30:58 +010038// For contiguous spaces, top should be in the space (or at the end) and limit
39// should be the end of the space.
40#define ASSERT_SEMISPACE_ALLOCATION_INFO(info, space) \
41 ASSERT((space).low() <= (info).top \
42 && (info).top <= (space).high() \
43 && (info).limit == (space).high())
Steve Blocka7e24c12009-10-30 11:49:00 +000044
Steve Blocka7e24c12009-10-30 11:49:00 +000045// ----------------------------------------------------------------------------
46// HeapObjectIterator
47
48HeapObjectIterator::HeapObjectIterator(PagedSpace* space) {
Ben Murdoch85b71792012-04-11 18:30:58 +010049 Initialize(space->bottom(), space->top(), NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +000050}
51
52
53HeapObjectIterator::HeapObjectIterator(PagedSpace* space,
54 HeapObjectCallback size_func) {
Ben Murdoch85b71792012-04-11 18:30:58 +010055 Initialize(space->bottom(), space->top(), size_func);
56}
57
58
59HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start) {
60 Initialize(start, space->top(), NULL);
61}
62
63
64HeapObjectIterator::HeapObjectIterator(PagedSpace* space, Address start,
65 HeapObjectCallback size_func) {
66 Initialize(start, space->top(), size_func);
Steve Blocka7e24c12009-10-30 11:49:00 +000067}
68
69
Kristian Monsen80d68ea2010-09-08 11:05:35 +010070HeapObjectIterator::HeapObjectIterator(Page* page,
71 HeapObjectCallback size_func) {
Ben Murdoch85b71792012-04-11 18:30:58 +010072 Initialize(page->ObjectAreaStart(), page->AllocationTop(), size_func);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010073}
74
75
Ben Murdoch85b71792012-04-11 18:30:58 +010076void HeapObjectIterator::Initialize(Address cur, Address end,
Steve Blocka7e24c12009-10-30 11:49:00 +000077 HeapObjectCallback size_f) {
78 cur_addr_ = cur;
Ben Murdoch85b71792012-04-11 18:30:58 +010079 end_addr_ = end;
80 end_page_ = Page::FromAllocationTop(end);
Steve Blocka7e24c12009-10-30 11:49:00 +000081 size_func_ = size_f;
Ben Murdoch85b71792012-04-11 18:30:58 +010082 Page* p = Page::FromAllocationTop(cur_addr_);
83 cur_limit_ = (p == end_page_) ? end_addr_ : p->AllocationTop();
84
85#ifdef DEBUG
86 Verify();
87#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000088}
89
90
Ben Murdoch85b71792012-04-11 18:30:58 +010091HeapObject* HeapObjectIterator::FromNextPage() {
92 if (cur_addr_ == end_addr_) return NULL;
93
94 Page* cur_page = Page::FromAllocationTop(cur_addr_);
Ben Murdoch592a9fc2012-03-05 11:04:45 +000095 cur_page = cur_page->next_page();
Ben Murdoch85b71792012-04-11 18:30:58 +010096 ASSERT(cur_page->is_valid());
97
98 cur_addr_ = cur_page->ObjectAreaStart();
99 cur_limit_ = (cur_page == end_page_) ? end_addr_ : cur_page->AllocationTop();
100
101 if (cur_addr_ == end_addr_) return NULL;
102 ASSERT(cur_addr_ < cur_limit_);
103#ifdef DEBUG
104 Verify();
105#endif
106 return FromCurrentPage();
107}
108
109
110#ifdef DEBUG
111void HeapObjectIterator::Verify() {
112 Page* p = Page::FromAllocationTop(cur_addr_);
113 ASSERT(p == Page::FromAllocationTop(cur_limit_));
114 ASSERT(p->Offset(cur_addr_) <= p->Offset(cur_limit_));
115}
116#endif
117
118
119// -----------------------------------------------------------------------------
120// PageIterator
121
122PageIterator::PageIterator(PagedSpace* space, Mode mode) : space_(space) {
123 prev_page_ = NULL;
124 switch (mode) {
125 case PAGES_IN_USE:
126 stop_page_ = space->AllocationTopPage();
127 break;
128 case PAGES_USED_BY_MC:
129 stop_page_ = space->MCRelocationTopPage();
130 break;
131 case ALL_PAGES:
132#ifdef DEBUG
133 // Verify that the cached last page in the space is actually the
134 // last page.
135 for (Page* p = space->first_page_; p->is_valid(); p = p->next_page()) {
136 if (!p->next_page()->is_valid()) {
137 ASSERT(space->last_page_ == p);
138 }
139 }
140#endif
141 stop_page_ = space->last_page_;
142 break;
143 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000144}
145
146
147// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000148// CodeRange
149
Steve Block44f0eee2011-05-26 01:26:41 +0100150
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000151CodeRange::CodeRange(Isolate* isolate)
152 : isolate_(isolate),
153 code_range_(NULL),
Steve Block44f0eee2011-05-26 01:26:41 +0100154 free_list_(0),
155 allocation_list_(0),
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000156 current_allocation_block_index_(0) {
Steve Block44f0eee2011-05-26 01:26:41 +0100157}
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
159
Ben Murdoch85b71792012-04-11 18:30:58 +0100160bool CodeRange::Setup(const size_t requested) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 ASSERT(code_range_ == NULL);
162
163 code_range_ = new VirtualMemory(requested);
164 CHECK(code_range_ != NULL);
165 if (!code_range_->IsReserved()) {
166 delete code_range_;
167 code_range_ = NULL;
168 return false;
169 }
170
171 // We are sure that we have mapped a block of requested addresses.
172 ASSERT(code_range_->size() == requested);
Steve Block44f0eee2011-05-26 01:26:41 +0100173 LOG(isolate_, NewEvent("CodeRange", code_range_->address(), requested));
Ben Murdoch85b71792012-04-11 18:30:58 +0100174 allocation_list_.Add(FreeBlock(code_range_->address(), code_range_->size()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 current_allocation_block_index_ = 0;
176 return true;
177}
178
179
180int CodeRange::CompareFreeBlockAddress(const FreeBlock* left,
181 const FreeBlock* right) {
182 // The entire point of CodeRange is that the difference between two
183 // addresses in the range can be represented as a signed 32-bit int,
184 // so the cast is semantically correct.
185 return static_cast<int>(left->start - right->start);
186}
187
188
189void CodeRange::GetNextAllocationBlock(size_t requested) {
190 for (current_allocation_block_index_++;
191 current_allocation_block_index_ < allocation_list_.length();
192 current_allocation_block_index_++) {
193 if (requested <= allocation_list_[current_allocation_block_index_].size) {
194 return; // Found a large enough allocation block.
195 }
196 }
197
198 // Sort and merge the free blocks on the free list and the allocation list.
199 free_list_.AddAll(allocation_list_);
200 allocation_list_.Clear();
201 free_list_.Sort(&CompareFreeBlockAddress);
202 for (int i = 0; i < free_list_.length();) {
203 FreeBlock merged = free_list_[i];
204 i++;
205 // Add adjacent free blocks to the current merged block.
206 while (i < free_list_.length() &&
207 free_list_[i].start == merged.start + merged.size) {
208 merged.size += free_list_[i].size;
209 i++;
210 }
211 if (merged.size > 0) {
212 allocation_list_.Add(merged);
213 }
214 }
215 free_list_.Clear();
216
217 for (current_allocation_block_index_ = 0;
218 current_allocation_block_index_ < allocation_list_.length();
219 current_allocation_block_index_++) {
220 if (requested <= allocation_list_[current_allocation_block_index_].size) {
221 return; // Found a large enough allocation block.
222 }
223 }
224
225 // Code range is full or too fragmented.
226 V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock");
227}
228
229
230
Ben Murdoch85b71792012-04-11 18:30:58 +0100231void* CodeRange::AllocateRawMemory(const size_t requested, size_t* allocated) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 ASSERT(current_allocation_block_index_ < allocation_list_.length());
233 if (requested > allocation_list_[current_allocation_block_index_].size) {
234 // Find an allocation block large enough. This function call may
235 // call V8::FatalProcessOutOfMemory if it cannot find a large enough block.
236 GetNextAllocationBlock(requested);
237 }
238 // Commit the requested memory at the start of the current allocation block.
Ben Murdoch85b71792012-04-11 18:30:58 +0100239 *allocated = RoundUp(requested, Page::kPageSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 FreeBlock current = allocation_list_[current_allocation_block_index_];
Ben Murdoch85b71792012-04-11 18:30:58 +0100241 if (*allocated >= current.size - Page::kPageSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000242 // Don't leave a small free block, useless for a large object or chunk.
243 *allocated = current.size;
244 }
245 ASSERT(*allocated <= current.size);
Ben Murdoch85b71792012-04-11 18:30:58 +0100246 if (!code_range_->Commit(current.start, *allocated, true)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000247 *allocated = 0;
248 return NULL;
249 }
250 allocation_list_[current_allocation_block_index_].start += *allocated;
251 allocation_list_[current_allocation_block_index_].size -= *allocated;
252 if (*allocated == current.size) {
253 GetNextAllocationBlock(0); // This block is used up, get the next one.
254 }
255 return current.start;
256}
257
258
Ben Murdoch85b71792012-04-11 18:30:58 +0100259void CodeRange::FreeRawMemory(void* address, size_t length) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 free_list_.Add(FreeBlock(address, length));
261 code_range_->Uncommit(address, length);
262}
263
264
265void CodeRange::TearDown() {
266 delete code_range_; // Frees all memory in the virtual memory range.
267 code_range_ = NULL;
268 free_list_.Free();
269 allocation_list_.Free();
270}
271
272
273// -----------------------------------------------------------------------------
274// MemoryAllocator
275//
Steve Blocka7e24c12009-10-30 11:49:00 +0000276
Ben Murdoch85b71792012-04-11 18:30:58 +0100277// 270 is an estimate based on the static default heap size of a pair of 256K
278// semispaces and a 64M old generation.
279const int kEstimatedNumberOfChunks = 270;
280
281
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000282MemoryAllocator::MemoryAllocator(Isolate* isolate)
283 : isolate_(isolate),
284 capacity_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100285 capacity_executable_(0),
286 size_(0),
Ben Murdoch85b71792012-04-11 18:30:58 +0100287 size_executable_(0),
288 initial_chunk_(NULL),
289 chunks_(kEstimatedNumberOfChunks),
290 free_chunk_ids_(kEstimatedNumberOfChunks),
291 max_nof_chunks_(0),
292 top_(0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000293}
294
295
Ben Murdoch85b71792012-04-11 18:30:58 +0100296void MemoryAllocator::Push(int free_chunk_id) {
297 ASSERT(max_nof_chunks_ > 0);
298 ASSERT(top_ < max_nof_chunks_);
299 free_chunk_ids_[top_++] = free_chunk_id;
300}
301
302
303int MemoryAllocator::Pop() {
304 ASSERT(top_ > 0);
305 return free_chunk_ids_[--top_];
306}
307
308
309bool MemoryAllocator::Setup(intptr_t capacity, intptr_t capacity_executable) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 capacity_ = RoundUp(capacity, Page::kPageSize);
Russell Brenner90bac252010-11-18 13:33:46 -0800311 capacity_executable_ = RoundUp(capacity_executable, Page::kPageSize);
312 ASSERT_GE(capacity_, capacity_executable_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000313
Ben Murdoch85b71792012-04-11 18:30:58 +0100314 // Over-estimate the size of chunks_ array. It assumes the expansion of old
315 // space is always in the unit of a chunk (kChunkSize) except the last
316 // expansion.
317 //
318 // Due to alignment, allocated space might be one page less than required
319 // number (kPagesPerChunk) of pages for old spaces.
320 //
321 // Reserve two chunk ids for semispaces, one for map space, one for old
322 // space, and one for code space.
323 max_nof_chunks_ =
324 static_cast<int>((capacity_ / (kChunkSize - Page::kPageSize))) + 5;
325 if (max_nof_chunks_ > kMaxNofChunks) return false;
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 size_ = 0;
Steve Block791712a2010-08-27 10:21:07 +0100328 size_executable_ = 0;
Ben Murdoch85b71792012-04-11 18:30:58 +0100329 ChunkInfo info; // uninitialized element.
330 for (int i = max_nof_chunks_ - 1; i >= 0; i--) {
331 chunks_.Add(info);
332 free_chunk_ids_.Add(i);
333 }
334 top_ = max_nof_chunks_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 return true;
336}
337
338
339void MemoryAllocator::TearDown() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100340 for (int i = 0; i < max_nof_chunks_; i++) {
341 if (chunks_[i].address() != NULL) DeleteChunk(i);
342 }
343 chunks_.Clear();
344 free_chunk_ids_.Clear();
345
346 if (initial_chunk_ != NULL) {
347 LOG(isolate_, DeleteEvent("InitialChunk", initial_chunk_->address()));
348 delete initial_chunk_;
349 initial_chunk_ = NULL;
350 }
351
352 ASSERT(top_ == max_nof_chunks_); // all chunks are free
353 top_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 capacity_ = 0;
Russell Brenner90bac252010-11-18 13:33:46 -0800355 capacity_executable_ = 0;
Ben Murdoch85b71792012-04-11 18:30:58 +0100356 size_ = 0;
357 max_nof_chunks_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000358}
359
360
Ben Murdoch85b71792012-04-11 18:30:58 +0100361void* MemoryAllocator::AllocateRawMemory(const size_t requested,
362 size_t* allocated,
363 Executability executable) {
364 if (size_ + static_cast<size_t>(requested) > static_cast<size_t>(capacity_)) {
365 return NULL;
366 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000367
Ben Murdoch85b71792012-04-11 18:30:58 +0100368 void* mem;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000369 if (executable == EXECUTABLE) {
Russell Brenner90bac252010-11-18 13:33:46 -0800370 // Check executable memory limit.
Ben Murdoch85b71792012-04-11 18:30:58 +0100371 if (size_executable_ + requested >
372 static_cast<size_t>(capacity_executable_)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100373 LOG(isolate_,
374 StringEvent("MemoryAllocator::AllocateRawMemory",
Russell Brenner90bac252010-11-18 13:33:46 -0800375 "V8 Executable Allocation capacity exceeded"));
376 return NULL;
377 }
378 // Allocate executable memory either from code range or from the
379 // OS.
Steve Block44f0eee2011-05-26 01:26:41 +0100380 if (isolate_->code_range()->exists()) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100381 mem = isolate_->code_range()->AllocateRawMemory(requested, allocated);
Russell Brenner90bac252010-11-18 13:33:46 -0800382 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +0100383 mem = OS::Allocate(requested, allocated, true);
Russell Brenner90bac252010-11-18 13:33:46 -0800384 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100385 // Update executable memory size.
386 size_executable_ += static_cast<int>(*allocated);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000387 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +0100388 mem = OS::Allocate(requested, allocated, false);
389 }
390 int alloced = static_cast<int>(*allocated);
391 size_ += alloced;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000392
393#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +0100394 ZapBlock(reinterpret_cast<Address>(mem), alloced);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000395#endif
Ben Murdoch85b71792012-04-11 18:30:58 +0100396 isolate_->counters()->memory_allocated()->Increment(alloced);
397 return mem;
Steve Blocka7e24c12009-10-30 11:49:00 +0000398}
399
400
Ben Murdoch85b71792012-04-11 18:30:58 +0100401void MemoryAllocator::FreeRawMemory(void* mem,
402 size_t length,
Steve Block791712a2010-08-27 10:21:07 +0100403 Executability executable) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000404#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +0100405 // Do not try to zap the guard page.
406 size_t guard_size = (executable == EXECUTABLE) ? Page::kPageSize : 0;
407 ZapBlock(reinterpret_cast<Address>(mem) + guard_size, length - guard_size);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000408#endif
Ben Murdoch85b71792012-04-11 18:30:58 +0100409 if (isolate_->code_range()->contains(static_cast<Address>(mem))) {
410 isolate_->code_range()->FreeRawMemory(mem, length);
411 } else {
412 OS::Free(mem, length);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000413 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100414 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(length));
415 size_ -= static_cast<int>(length);
416 if (executable == EXECUTABLE) size_executable_ -= static_cast<int>(length);
417
418 ASSERT(size_ >= 0);
419 ASSERT(size_executable_ >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000420}
421
422
Iain Merrick9ac36c92010-09-13 15:29:50 +0100423void MemoryAllocator::PerformAllocationCallback(ObjectSpace space,
424 AllocationAction action,
425 size_t size) {
426 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
427 MemoryAllocationCallbackRegistration registration =
428 memory_allocation_callbacks_[i];
429 if ((registration.space & space) == space &&
430 (registration.action & action) == action)
431 registration.callback(space, action, static_cast<int>(size));
432 }
433}
434
435
436bool MemoryAllocator::MemoryAllocationCallbackRegistered(
437 MemoryAllocationCallback callback) {
438 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
439 if (memory_allocation_callbacks_[i].callback == callback) return true;
440 }
441 return false;
442}
443
444
445void MemoryAllocator::AddMemoryAllocationCallback(
446 MemoryAllocationCallback callback,
447 ObjectSpace space,
448 AllocationAction action) {
449 ASSERT(callback != NULL);
450 MemoryAllocationCallbackRegistration registration(callback, space, action);
451 ASSERT(!MemoryAllocator::MemoryAllocationCallbackRegistered(callback));
452 return memory_allocation_callbacks_.Add(registration);
453}
454
455
456void MemoryAllocator::RemoveMemoryAllocationCallback(
457 MemoryAllocationCallback callback) {
458 ASSERT(callback != NULL);
459 for (int i = 0; i < memory_allocation_callbacks_.length(); ++i) {
460 if (memory_allocation_callbacks_[i].callback == callback) {
461 memory_allocation_callbacks_.Remove(i);
462 return;
463 }
464 }
465 UNREACHABLE();
466}
467
Ben Murdoch85b71792012-04-11 18:30:58 +0100468void* MemoryAllocator::ReserveInitialChunk(const size_t requested) {
469 ASSERT(initial_chunk_ == NULL);
470
471 initial_chunk_ = new VirtualMemory(requested);
472 CHECK(initial_chunk_ != NULL);
473 if (!initial_chunk_->IsReserved()) {
474 delete initial_chunk_;
475 initial_chunk_ = NULL;
476 return NULL;
477 }
478
479 // We are sure that we have mapped a block of requested addresses.
480 ASSERT(initial_chunk_->size() == requested);
481 LOG(isolate_,
482 NewEvent("InitialChunk", initial_chunk_->address(), requested));
483 size_ += static_cast<int>(requested);
484 return initial_chunk_->address();
485}
486
487
488static int PagesInChunk(Address start, size_t size) {
489 // The first page starts on the first page-aligned address from start onward
490 // and the last page ends on the last page-aligned address before
491 // start+size. Page::kPageSize is a power of two so we can divide by
492 // shifting.
493 return static_cast<int>((RoundDown(start + size, Page::kPageSize)
494 - RoundUp(start, Page::kPageSize)) >> kPageSizeBits);
495}
496
497
498Page* MemoryAllocator::AllocatePages(int requested_pages,
499 int* allocated_pages,
500 PagedSpace* owner) {
501 if (requested_pages <= 0) return Page::FromAddress(NULL);
502 size_t chunk_size = requested_pages * Page::kPageSize;
503
504 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
505 if (chunk == NULL) return Page::FromAddress(NULL);
506 LOG(isolate_, NewEvent("PagedChunk", chunk, chunk_size));
507
508 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
509
510 // We may 'lose' a page due to alignment.
511 ASSERT(*allocated_pages >= kPagesPerChunk - 1);
512
513 size_t guard_size = (owner->executable() == EXECUTABLE) ? Page::kPageSize : 0;
514
515 // Check that we got at least one page that we can use.
516 if (*allocated_pages <= ((guard_size != 0) ? 1 : 0)) {
517 FreeRawMemory(chunk,
518 chunk_size,
519 owner->executable());
520 LOG(isolate_, DeleteEvent("PagedChunk", chunk));
521 return Page::FromAddress(NULL);
522 }
523
524 if (guard_size != 0) {
525 OS::Guard(chunk, guard_size);
526 chunk_size -= guard_size;
527 chunk = static_cast<Address>(chunk) + guard_size;
528 --*allocated_pages;
529 }
530
531 int chunk_id = Pop();
532 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
533
534 ObjectSpace space = static_cast<ObjectSpace>(1 << owner->identity());
535 PerformAllocationCallback(space, kAllocationActionAllocate, chunk_size);
536 Page* new_pages = InitializePagesInChunk(chunk_id, *allocated_pages, owner);
537
538 return new_pages;
539}
540
541
542Page* MemoryAllocator::CommitPages(Address start, size_t size,
543 PagedSpace* owner, int* num_pages) {
544 ASSERT(start != NULL);
545 *num_pages = PagesInChunk(start, size);
546 ASSERT(*num_pages > 0);
547 ASSERT(initial_chunk_ != NULL);
548 ASSERT(InInitialChunk(start));
549 ASSERT(InInitialChunk(start + size - 1));
550 if (!initial_chunk_->Commit(start, size, owner->executable() == EXECUTABLE)) {
551 return Page::FromAddress(NULL);
552 }
553#ifdef DEBUG
554 ZapBlock(start, size);
555#endif
556 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
557
558 // So long as we correctly overestimated the number of chunks we should not
559 // run out of chunk ids.
560 CHECK(!OutOfChunkIds());
561 int chunk_id = Pop();
562 chunks_[chunk_id].init(start, size, owner);
563 return InitializePagesInChunk(chunk_id, *num_pages, owner);
564}
565
566
567bool MemoryAllocator::CommitBlock(Address start,
568 size_t size,
569 Executability executable) {
570 ASSERT(start != NULL);
571 ASSERT(size > 0);
572 ASSERT(initial_chunk_ != NULL);
573 ASSERT(InInitialChunk(start));
574 ASSERT(InInitialChunk(start + size - 1));
575
576 if (!initial_chunk_->Commit(start, size, executable)) return false;
577#ifdef DEBUG
578 ZapBlock(start, size);
579#endif
580 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
581 return true;
582}
583
584
585bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
586 ASSERT(start != NULL);
587 ASSERT(size > 0);
588 ASSERT(initial_chunk_ != NULL);
589 ASSERT(InInitialChunk(start));
590 ASSERT(InInitialChunk(start + size - 1));
591
592 if (!initial_chunk_->Uncommit(start, size)) return false;
593 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
594 return true;
595}
596
597
598void MemoryAllocator::ZapBlock(Address start, size_t size) {
599 for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) {
600 Memory::Address_at(start + s) = kZapValue;
601 }
602}
603
604
605Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
606 PagedSpace* owner) {
607 ASSERT(IsValidChunk(chunk_id));
608 ASSERT(pages_in_chunk > 0);
609
610 Address chunk_start = chunks_[chunk_id].address();
611
612 Address low = RoundUp(chunk_start, Page::kPageSize);
613
614#ifdef DEBUG
615 size_t chunk_size = chunks_[chunk_id].size();
616 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
617 ASSERT(pages_in_chunk <=
618 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
619#endif
620
621 Address page_addr = low;
622 for (int i = 0; i < pages_in_chunk; i++) {
623 Page* p = Page::FromAddress(page_addr);
624 p->heap_ = owner->heap();
625 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
626 p->InvalidateWatermark(true);
627 p->SetIsLargeObjectPage(false);
628 p->SetAllocationWatermark(p->ObjectAreaStart());
629 p->SetCachedAllocationWatermark(p->ObjectAreaStart());
630 page_addr += Page::kPageSize;
631 }
632
633 // Set the next page of the last page to 0.
634 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
635 last_page->opaque_header = OffsetFrom(0) | chunk_id;
636
637 return Page::FromAddress(low);
638}
639
640
641Page* MemoryAllocator::FreePages(Page* p) {
642 if (!p->is_valid()) return p;
643
644 // Find the first page in the same chunk as 'p'
645 Page* first_page = FindFirstPageInSameChunk(p);
646 Page* page_to_return = Page::FromAddress(NULL);
647
648 if (p != first_page) {
649 // Find the last page in the same chunk as 'prev'.
650 Page* last_page = FindLastPageInSameChunk(p);
651 first_page = GetNextPage(last_page); // first page in next chunk
652
653 // set the next_page of last_page to NULL
654 SetNextPage(last_page, Page::FromAddress(NULL));
655 page_to_return = p; // return 'p' when exiting
656 }
657
658 while (first_page->is_valid()) {
659 int chunk_id = GetChunkId(first_page);
660 ASSERT(IsValidChunk(chunk_id));
661
662 // Find the first page of the next chunk before deleting this chunk.
663 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
664
665 // Free the current chunk.
666 DeleteChunk(chunk_id);
667 }
668
669 return page_to_return;
670}
671
672
673void MemoryAllocator::FreeAllPages(PagedSpace* space) {
674 for (int i = 0, length = chunks_.length(); i < length; i++) {
675 if (chunks_[i].owner() == space) {
676 DeleteChunk(i);
677 }
678 }
679}
680
681
682void MemoryAllocator::DeleteChunk(int chunk_id) {
683 ASSERT(IsValidChunk(chunk_id));
684
685 ChunkInfo& c = chunks_[chunk_id];
686
687 // We cannot free a chunk contained in the initial chunk because it was not
688 // allocated with AllocateRawMemory. Instead we uncommit the virtual
689 // memory.
690 if (InInitialChunk(c.address())) {
691 // TODO(1240712): VirtualMemory::Uncommit has a return value which
692 // is ignored here.
693 initial_chunk_->Uncommit(c.address(), c.size());
694 Counters* counters = isolate_->counters();
695 counters->memory_allocated()->Decrement(static_cast<int>(c.size()));
696 } else {
697 LOG(isolate_, DeleteEvent("PagedChunk", c.address()));
698 ObjectSpace space = static_cast<ObjectSpace>(1 << c.owner_identity());
699 size_t size = c.size();
700 size_t guard_size = (c.executable() == EXECUTABLE) ? Page::kPageSize : 0;
701 FreeRawMemory(c.address() - guard_size, size + guard_size, c.executable());
702 PerformAllocationCallback(space, kAllocationActionFree, size);
703 }
704 c.init(NULL, 0, NULL);
705 Push(chunk_id);
706}
707
708
709Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
710 int chunk_id = GetChunkId(p);
711 ASSERT(IsValidChunk(chunk_id));
712
713 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
714 return Page::FromAddress(low);
715}
716
717
718Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
719 int chunk_id = GetChunkId(p);
720 ASSERT(IsValidChunk(chunk_id));
721
722 Address chunk_start = chunks_[chunk_id].address();
723 size_t chunk_size = chunks_[chunk_id].size();
724
725 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
726 ASSERT(chunk_start <= p->address() && p->address() < high);
727
728 return Page::FromAddress(high - Page::kPageSize);
729}
730
Steve Blocka7e24c12009-10-30 11:49:00 +0000731
732#ifdef DEBUG
733void MemoryAllocator::ReportStatistics() {
734 float pct = static_cast<float>(capacity_ - size_) / capacity_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100735 PrintF(" capacity: %" V8_PTR_PREFIX "d"
736 ", used: %" V8_PTR_PREFIX "d"
737 ", available: %%%d\n\n",
Steve Blocka7e24c12009-10-30 11:49:00 +0000738 capacity_, size_, static_cast<int>(pct*100));
739}
740#endif
741
742
Ben Murdoch85b71792012-04-11 18:30:58 +0100743void MemoryAllocator::RelinkPageListInChunkOrder(PagedSpace* space,
744 Page** first_page,
745 Page** last_page,
746 Page** last_page_in_use) {
747 Page* first = NULL;
748 Page* last = NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100749
Ben Murdoch85b71792012-04-11 18:30:58 +0100750 for (int i = 0, length = chunks_.length(); i < length; i++) {
751 ChunkInfo& chunk = chunks_[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100752
Ben Murdoch85b71792012-04-11 18:30:58 +0100753 if (chunk.owner() == space) {
754 if (first == NULL) {
755 Address low = RoundUp(chunk.address(), Page::kPageSize);
756 first = Page::FromAddress(low);
757 }
758 last = RelinkPagesInChunk(i,
759 chunk.address(),
760 chunk.size(),
761 last,
762 last_page_in_use);
763 }
Steve Block6ded16b2010-05-10 14:33:55 +0100764 }
765
Ben Murdoch85b71792012-04-11 18:30:58 +0100766 if (first_page != NULL) {
767 *first_page = first;
Steve Block6ded16b2010-05-10 14:33:55 +0100768 }
769
Ben Murdoch85b71792012-04-11 18:30:58 +0100770 if (last_page != NULL) {
771 *last_page = last;
Steve Block6ded16b2010-05-10 14:33:55 +0100772 }
Steve Block6ded16b2010-05-10 14:33:55 +0100773}
774
775
Ben Murdoch85b71792012-04-11 18:30:58 +0100776Page* MemoryAllocator::RelinkPagesInChunk(int chunk_id,
777 Address chunk_start,
778 size_t chunk_size,
779 Page* prev,
780 Page** last_page_in_use) {
781 Address page_addr = RoundUp(chunk_start, Page::kPageSize);
782 int pages_in_chunk = PagesInChunk(chunk_start, chunk_size);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000783
Ben Murdoch85b71792012-04-11 18:30:58 +0100784 if (prev->is_valid()) {
785 SetNextPage(prev, Page::FromAddress(page_addr));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000786 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100787
788 for (int i = 0; i < pages_in_chunk; i++) {
789 Page* p = Page::FromAddress(page_addr);
790 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
791 page_addr += Page::kPageSize;
792
793 p->InvalidateWatermark(true);
794 if (p->WasInUseBeforeMC()) {
795 *last_page_in_use = p;
796 }
797 }
798
799 // Set the next page of the last page to 0.
800 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
801 last_page->opaque_header = OffsetFrom(0) | chunk_id;
802
803 if (last_page->WasInUseBeforeMC()) {
804 *last_page_in_use = last_page;
805 }
806
807 return last_page;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000808}
809
Ben Murdoch85b71792012-04-11 18:30:58 +0100810
Ben Murdochc7cc0282012-03-05 14:35:55 +0000811// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000812// PagedSpace implementation
813
Steve Block44f0eee2011-05-26 01:26:41 +0100814PagedSpace::PagedSpace(Heap* heap,
815 intptr_t max_capacity,
Steve Blocka7e24c12009-10-30 11:49:00 +0000816 AllocationSpace id,
817 Executability executable)
Ben Murdoch85b71792012-04-11 18:30:58 +0100818 : Space(heap, id, executable) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
Ben Murdoch85b71792012-04-11 18:30:58 +0100820 * Page::kObjectAreaSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000821 accounting_stats_.Clear();
822
823 allocation_info_.top = NULL;
824 allocation_info_.limit = NULL;
825
Ben Murdoch85b71792012-04-11 18:30:58 +0100826 mc_forwarding_info_.top = NULL;
827 mc_forwarding_info_.limit = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000828}
829
830
Ben Murdoch85b71792012-04-11 18:30:58 +0100831bool PagedSpace::Setup(Address start, size_t size) {
832 if (HasBeenSetup()) return false;
833
834 int num_pages = 0;
835 // Try to use the virtual memory range passed to us. If it is too small to
836 // contain at least one page, ignore it and allocate instead.
837 int pages_in_chunk = PagesInChunk(start, size);
838 if (pages_in_chunk > 0) {
839 first_page_ = Isolate::Current()->memory_allocator()->CommitPages(
840 RoundUp(start, Page::kPageSize),
841 Page::kPageSize * pages_in_chunk,
842 this, &num_pages);
843 } else {
844 int requested_pages =
845 Min(MemoryAllocator::kPagesPerChunk,
846 static_cast<int>(max_capacity_ / Page::kObjectAreaSize));
847 first_page_ =
848 Isolate::Current()->memory_allocator()->AllocatePages(
849 requested_pages, &num_pages, this);
850 if (!first_page_->is_valid()) return false;
851 }
852
853 // We are sure that the first page is valid and that we have at least one
854 // page.
855 ASSERT(first_page_->is_valid());
856 ASSERT(num_pages > 0);
857 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
858 ASSERT(Capacity() <= max_capacity_);
859
860 // Sequentially clear region marks in the newly allocated
861 // pages and cache the current last page in the space.
862 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
863 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
864 last_page_ = p;
865 }
866
867 // Use first_page_ for allocation.
868 SetAllocationInfo(&allocation_info_, first_page_);
869
870 page_list_is_chunk_ordered_ = true;
871
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 return true;
873}
874
875
Ben Murdoch85b71792012-04-11 18:30:58 +0100876bool PagedSpace::HasBeenSetup() {
877 return (Capacity() > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000878}
879
880
881void PagedSpace::TearDown() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100882 Isolate::Current()->memory_allocator()->FreeAllPages(this);
883 first_page_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000884 accounting_stats_.Clear();
885}
886
887
Ben Murdoch85b71792012-04-11 18:30:58 +0100888void PagedSpace::MarkAllPagesClean() {
889 PageIterator it(this, PageIterator::ALL_PAGES);
890 while (it.has_next()) {
891 it.next()->SetRegionMarks(Page::kAllRegionsCleanMarks);
892 }
893}
894
895
John Reck59135872010-11-02 12:39:01 -0700896MaybeObject* PagedSpace::FindObject(Address addr) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100897 // Note: this function can only be called before or after mark-compact GC
898 // because it accesses map pointers.
Steve Block44f0eee2011-05-26 01:26:41 +0100899 ASSERT(!heap()->mark_compact_collector()->in_use());
Steve Blocka7e24c12009-10-30 11:49:00 +0000900
901 if (!Contains(addr)) return Failure::Exception();
902
903 Page* p = Page::FromAddress(addr);
Ben Murdoch85b71792012-04-11 18:30:58 +0100904 ASSERT(IsUsed(p));
905 Address cur = p->ObjectAreaStart();
906 Address end = p->AllocationTop();
907 while (cur < end) {
908 HeapObject* obj = HeapObject::FromAddress(cur);
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 Address next = cur + obj->Size();
910 if ((cur <= addr) && (addr < next)) return obj;
Ben Murdoch85b71792012-04-11 18:30:58 +0100911 cur = next;
Steve Blocka7e24c12009-10-30 11:49:00 +0000912 }
913
914 UNREACHABLE();
915 return Failure::Exception();
916}
917
Ben Murdoch85b71792012-04-11 18:30:58 +0100918
919bool PagedSpace::IsUsed(Page* page) {
920 PageIterator it(this, PageIterator::PAGES_IN_USE);
921 while (it.has_next()) {
922 if (page == it.next()) return true;
923 }
924 return false;
925}
926
927
928void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
929 alloc_info->top = p->ObjectAreaStart();
930 alloc_info->limit = p->ObjectAreaEnd();
931 ASSERT(alloc_info->VerifyPagedAllocation());
932}
933
934
935void PagedSpace::MCResetRelocationInfo() {
936 // Set page indexes.
937 int i = 0;
938 PageIterator it(this, PageIterator::ALL_PAGES);
939 while (it.has_next()) {
940 Page* p = it.next();
941 p->mc_page_index = i++;
942 }
943
944 // Set mc_forwarding_info_ to the first page in the space.
945 SetAllocationInfo(&mc_forwarding_info_, first_page_);
946 // All the bytes in the space are 'available'. We will rediscover
947 // allocated and wasted bytes during GC.
948 accounting_stats_.Reset();
949}
950
951
952int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
953#ifdef DEBUG
954 // The Contains function considers the address at the beginning of a
955 // page in the page, MCSpaceOffsetForAddress considers it is in the
956 // previous page.
957 if (Page::IsAlignedToPageSize(addr)) {
958 ASSERT(Contains(addr - kPointerSize));
959 } else {
960 ASSERT(Contains(addr));
961 }
962#endif
963
964 // If addr is at the end of a page, it belongs to previous page
965 Page* p = Page::IsAlignedToPageSize(addr)
966 ? Page::FromAllocationTop(addr)
967 : Page::FromAddress(addr);
968 int index = p->mc_page_index;
969 return (index * Page::kPageSize) + p->Offset(addr);
970}
971
972
973// Slow case for reallocating and promoting objects during a compacting
974// collection. This function is not space-specific.
975HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
976 Page* current_page = TopPageOf(mc_forwarding_info_);
977 if (!current_page->next_page()->is_valid()) {
978 if (!Expand(current_page)) {
979 return NULL;
980 }
981 }
982
983 // There are surely more pages in the space now.
984 ASSERT(current_page->next_page()->is_valid());
985 // We do not add the top of page block for current page to the space's
986 // free list---the block may contain live objects so we cannot write
987 // bookkeeping information to it. Instead, we will recover top of page
988 // blocks when we move objects to their new locations.
989 //
990 // We do however write the allocation pointer to the page. The encoding
991 // of forwarding addresses is as an offset in terms of live bytes, so we
992 // need quick access to the allocation top of each page to decode
993 // forwarding addresses.
994 current_page->SetAllocationWatermark(mc_forwarding_info_.top);
995 current_page->next_page()->InvalidateWatermark(true);
996 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
997 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
998}
999
1000
1001bool PagedSpace::Expand(Page* last_page) {
1002 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
1003 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001004
1005 if (Capacity() == max_capacity_) return false;
1006
1007 ASSERT(Capacity() < max_capacity_);
Ben Murdoch85b71792012-04-11 18:30:58 +01001008 // Last page must be valid and its next page is invalid.
1009 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
Steve Blocka7e24c12009-10-30 11:49:00 +00001010
Ben Murdoch85b71792012-04-11 18:30:58 +01001011 int available_pages =
1012 static_cast<int>((max_capacity_ - Capacity()) / Page::kObjectAreaSize);
1013 // We don't want to have to handle small chunks near the end so if there are
1014 // not kPagesPerChunk pages available without exceeding the max capacity then
1015 // act as if memory has run out.
1016 if (available_pages < MemoryAllocator::kPagesPerChunk) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001017
Ben Murdoch85b71792012-04-11 18:30:58 +01001018 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
1019 Page* p = heap()->isolate()->memory_allocator()->AllocatePages(
1020 desired_pages, &desired_pages, this);
1021 if (!p->is_valid()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001022
Ben Murdoch85b71792012-04-11 18:30:58 +01001023 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001024 ASSERT(Capacity() <= max_capacity_);
1025
Ben Murdoch85b71792012-04-11 18:30:58 +01001026 heap()->isolate()->memory_allocator()->SetNextPage(last_page, p);
1027
1028 // Sequentially clear region marks of new pages and and cache the
1029 // new last page in the space.
1030 while (p->is_valid()) {
1031 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
1032 last_page_ = p;
1033 p = p->next_page();
1034 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001035
1036 return true;
1037}
1038
1039
Ben Murdoch85b71792012-04-11 18:30:58 +01001040#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001041int PagedSpace::CountTotalPages() {
1042 int count = 0;
Ben Murdoch85b71792012-04-11 18:30:58 +01001043 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001044 count++;
1045 }
1046 return count;
1047}
Ben Murdoch85b71792012-04-11 18:30:58 +01001048#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001049
1050
Ben Murdoch85b71792012-04-11 18:30:58 +01001051void PagedSpace::Shrink() {
1052 if (!page_list_is_chunk_ordered_) {
1053 // We can't shrink space if pages is not chunk-ordered
1054 // (see comment for class MemoryAllocator for definition).
1055 return;
Steve Block6ded16b2010-05-10 14:33:55 +01001056 }
1057
Ben Murdoch85b71792012-04-11 18:30:58 +01001058 // Release half of free pages.
1059 Page* top_page = AllocationTopPage();
1060 ASSERT(top_page->is_valid());
1061
1062 // Count the number of pages we would like to free.
1063 int pages_to_free = 0;
1064 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
1065 pages_to_free++;
Steve Blocka7e24c12009-10-30 11:49:00 +00001066 }
1067
Ben Murdoch85b71792012-04-11 18:30:58 +01001068 // Free pages after top_page.
1069 Page* p = heap()->isolate()->memory_allocator()->
1070 FreePages(top_page->next_page());
1071 heap()->isolate()->memory_allocator()->SetNextPage(top_page, p);
1072
1073 // Find out how many pages we failed to free and update last_page_.
1074 // Please note pages can only be freed in whole chunks.
1075 last_page_ = top_page;
1076 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
1077 pages_to_free--;
1078 last_page_ = p;
Steve Blocka7e24c12009-10-30 11:49:00 +00001079 }
1080
Ben Murdoch85b71792012-04-11 18:30:58 +01001081 accounting_stats_.ShrinkSpace(pages_to_free * Page::kObjectAreaSize);
1082 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001083}
1084
1085
Ben Murdoch85b71792012-04-11 18:30:58 +01001086bool PagedSpace::EnsureCapacity(int capacity) {
1087 if (Capacity() >= capacity) return true;
1088
1089 // Start from the allocation top and loop to the last page in the space.
1090 Page* last_page = AllocationTopPage();
1091 Page* next_page = last_page->next_page();
1092 while (next_page->is_valid()) {
1093 last_page = heap()->isolate()->memory_allocator()->
1094 FindLastPageInSameChunk(next_page);
1095 next_page = last_page->next_page();
Steve Blocka7e24c12009-10-30 11:49:00 +00001096 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001097
1098 // Expand the space until it has the required capacity or expansion fails.
1099 do {
1100 if (!Expand(last_page)) return false;
1101 ASSERT(last_page->next_page()->is_valid());
1102 last_page =
1103 heap()->isolate()->memory_allocator()->FindLastPageInSameChunk(
1104 last_page->next_page());
1105 } while (Capacity() < capacity);
1106
1107 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001108}
1109
1110
1111#ifdef DEBUG
1112void PagedSpace::Print() { }
1113#endif
1114
1115
1116#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +01001117// We do not assume that the PageIterator works, because it depends on the
1118// invariants we are checking during verification.
Steve Blocka7e24c12009-10-30 11:49:00 +00001119void PagedSpace::Verify(ObjectVisitor* visitor) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001120 // The allocation pointer should be valid, and it should be in a page in the
1121 // space.
1122 ASSERT(allocation_info_.VerifyPagedAllocation());
1123 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
1124 ASSERT(heap()->isolate()->memory_allocator()->IsPageInSpace(top_page, this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001125
Ben Murdoch85b71792012-04-11 18:30:58 +01001126 // Loop over all the pages.
1127 bool above_allocation_top = false;
1128 Page* current_page = first_page_;
1129 while (current_page->is_valid()) {
1130 if (above_allocation_top) {
1131 // We don't care what's above the allocation top.
1132 } else {
1133 Address top = current_page->AllocationTop();
1134 if (current_page == top_page) {
1135 ASSERT(top == allocation_info_.top);
1136 // The next page will be above the allocation top.
1137 above_allocation_top = true;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001138 }
1139
Ben Murdoch85b71792012-04-11 18:30:58 +01001140 // It should be packed with objects from the bottom to the top.
1141 Address current = current_page->ObjectAreaStart();
1142 while (current < top) {
1143 HeapObject* object = HeapObject::FromAddress(current);
1144
1145 // The first word should be a map, and we expect all map pointers to
1146 // be in map space.
1147 Map* map = object->map();
1148 ASSERT(map->IsMap());
1149 ASSERT(heap()->map_space()->Contains(map));
1150
1151 // Perform space-specific object verification.
1152 VerifyObject(object);
1153
1154 // The object itself should look OK.
1155 object->Verify();
1156
1157 // All the interior pointers should be contained in the heap and
1158 // have page regions covering intergenerational references should be
1159 // marked dirty.
1160 int size = object->Size();
1161 object->IterateBody(map->instance_type(), size, visitor);
1162
1163 current += size;
1164 }
1165
1166 // The allocation pointer should not be in the middle of an object.
1167 ASSERT(current == top);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001168 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001169
1170 current_page = current_page->next_page();
Steve Blocka7e24c12009-10-30 11:49:00 +00001171 }
1172}
1173#endif
1174
1175
1176// -----------------------------------------------------------------------------
1177// NewSpace implementation
1178
1179
Ben Murdoch85b71792012-04-11 18:30:58 +01001180bool NewSpace::Setup(Address start, int size) {
1181 // Setup new space based on the preallocated memory block defined by
Steve Blocka7e24c12009-10-30 11:49:00 +00001182 // start and size. The provided space is divided into two semi-spaces.
1183 // To support fast containment testing in the new space, the size of
1184 // this chunk must be a power of two and it must be aligned to its size.
Steve Block44f0eee2011-05-26 01:26:41 +01001185 int initial_semispace_capacity = heap()->InitialSemiSpaceSize();
Ben Murdoch85b71792012-04-11 18:30:58 +01001186 int maximum_semispace_capacity = heap()->MaxSemiSpaceSize();
Steve Blocka7e24c12009-10-30 11:49:00 +00001187
1188 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
1189 ASSERT(IsPowerOf2(maximum_semispace_capacity));
1190
Ben Murdoch85b71792012-04-11 18:30:58 +01001191 // Allocate and setup the histogram arrays if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +00001192 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1193 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1194
1195#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
1196 promoted_histogram_[name].set_name(#name);
1197 INSTANCE_TYPE_LIST(SET_NAME)
1198#undef SET_NAME
Steve Blocka7e24c12009-10-30 11:49:00 +00001199
Ben Murdoch85b71792012-04-11 18:30:58 +01001200 ASSERT(size == 2 * heap()->ReservedSemiSpaceSize());
1201 ASSERT(IsAddressAligned(start, size, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +00001202
Ben Murdoch85b71792012-04-11 18:30:58 +01001203 if (!to_space_.Setup(start,
1204 initial_semispace_capacity,
1205 maximum_semispace_capacity)) {
1206 return false;
1207 }
1208 if (!from_space_.Setup(start + maximum_semispace_capacity,
1209 initial_semispace_capacity,
1210 maximum_semispace_capacity)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001211 return false;
1212 }
1213
Ben Murdoch85b71792012-04-11 18:30:58 +01001214 start_ = start;
1215 address_mask_ = ~(size - 1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001216 object_mask_ = address_mask_ | kHeapObjectTagMask;
Ben Murdoch85b71792012-04-11 18:30:58 +01001217 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
Steve Blocka7e24c12009-10-30 11:49:00 +00001218
Ben Murdoch85b71792012-04-11 18:30:58 +01001219 allocation_info_.top = to_space_.low();
1220 allocation_info_.limit = to_space_.high();
1221 mc_forwarding_info_.top = NULL;
1222 mc_forwarding_info_.limit = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001223
Ben Murdoch85b71792012-04-11 18:30:58 +01001224 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001225 return true;
1226}
1227
1228
1229void NewSpace::TearDown() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001230 if (allocated_histogram_) {
1231 DeleteArray(allocated_histogram_);
1232 allocated_histogram_ = NULL;
1233 }
1234 if (promoted_histogram_) {
1235 DeleteArray(promoted_histogram_);
1236 promoted_histogram_ = NULL;
1237 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001238
1239 start_ = NULL;
1240 allocation_info_.top = NULL;
1241 allocation_info_.limit = NULL;
Ben Murdoch85b71792012-04-11 18:30:58 +01001242 mc_forwarding_info_.top = NULL;
1243 mc_forwarding_info_.limit = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001244
1245 to_space_.TearDown();
1246 from_space_.TearDown();
1247}
1248
1249
Steve Blocka7e24c12009-10-30 11:49:00 +00001250void NewSpace::Flip() {
Ben Murdoch85b71792012-04-11 18:30:58 +01001251 SemiSpace tmp = from_space_;
1252 from_space_ = to_space_;
1253 to_space_ = tmp;
Steve Blocka7e24c12009-10-30 11:49:00 +00001254}
1255
1256
1257void NewSpace::Grow() {
1258 ASSERT(Capacity() < MaximumCapacity());
Ben Murdoch85b71792012-04-11 18:30:58 +01001259 if (to_space_.Grow()) {
1260 // Only grow from space if we managed to grow to space.
1261 if (!from_space_.Grow()) {
1262 // If we managed to grow to space but couldn't grow from space,
1263 // attempt to shrink to space.
Steve Blocka7e24c12009-10-30 11:49:00 +00001264 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
1265 // We are in an inconsistent state because we could not
1266 // commit/uncommit memory from new space.
1267 V8::FatalProcessOutOfMemory("Failed to grow new space.");
1268 }
1269 }
1270 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001271 allocation_info_.limit = to_space_.high();
Steve Blocka7e24c12009-10-30 11:49:00 +00001272 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1273}
1274
1275
1276void NewSpace::Shrink() {
Ben Murdochf87a2032010-10-22 12:50:53 +01001277 int new_capacity = Max(InitialCapacity(), 2 * SizeAsInt());
Ben Murdoch85b71792012-04-11 18:30:58 +01001278 int rounded_new_capacity =
1279 RoundUp(new_capacity, static_cast<int>(OS::AllocateAlignment()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001280 if (rounded_new_capacity < Capacity() &&
1281 to_space_.ShrinkTo(rounded_new_capacity)) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001282 // Only shrink from space if we managed to shrink to space.
Steve Blocka7e24c12009-10-30 11:49:00 +00001283 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001284 // If we managed to shrink to space but couldn't shrink from
1285 // space, attempt to grow to space again.
Steve Blocka7e24c12009-10-30 11:49:00 +00001286 if (!to_space_.GrowTo(from_space_.Capacity())) {
1287 // We are in an inconsistent state because we could not
1288 // commit/uncommit memory from new space.
1289 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
1290 }
1291 }
1292 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001293 allocation_info_.limit = to_space_.high();
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1295}
1296
1297
1298void NewSpace::ResetAllocationInfo() {
Ben Murdoch85b71792012-04-11 18:30:58 +01001299 allocation_info_.top = to_space_.low();
1300 allocation_info_.limit = to_space_.high();
1301 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001302}
1303
1304
Ben Murdoch85b71792012-04-11 18:30:58 +01001305void NewSpace::MCResetRelocationInfo() {
1306 mc_forwarding_info_.top = from_space_.low();
1307 mc_forwarding_info_.limit = from_space_.high();
1308 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001309}
1310
1311
Ben Murdoch85b71792012-04-11 18:30:58 +01001312void NewSpace::MCCommitRelocationInfo() {
1313 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
1314 // valid allocation info for the to space.
1315 allocation_info_.top = mc_forwarding_info_.top;
1316 allocation_info_.limit = to_space_.high();
1317 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001318}
1319
1320
1321#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +01001322// We do not use the SemispaceIterator because verification doesn't assume
Steve Blocka7e24c12009-10-30 11:49:00 +00001323// that it works (it depends on the invariants we are checking).
1324void NewSpace::Verify() {
1325 // The allocation pointer should be in the space or at the very end.
1326 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1327
1328 // There should be objects packed in from the low address up to the
1329 // allocation pointer.
Ben Murdoch85b71792012-04-11 18:30:58 +01001330 Address current = to_space_.low();
1331 while (current < top()) {
1332 HeapObject* object = HeapObject::FromAddress(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00001333
Ben Murdoch85b71792012-04-11 18:30:58 +01001334 // The first word should be a map, and we expect all map pointers to
1335 // be in map space.
1336 Map* map = object->map();
1337 ASSERT(map->IsMap());
1338 ASSERT(heap()->map_space()->Contains(map));
Steve Blocka7e24c12009-10-30 11:49:00 +00001339
Ben Murdoch85b71792012-04-11 18:30:58 +01001340 // The object should not be code or a map.
1341 ASSERT(!object->IsMap());
1342 ASSERT(!object->IsCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00001343
Ben Murdoch85b71792012-04-11 18:30:58 +01001344 // The object itself should look OK.
1345 object->Verify();
Steve Blocka7e24c12009-10-30 11:49:00 +00001346
Ben Murdoch85b71792012-04-11 18:30:58 +01001347 // All the interior pointers should be contained in the heap.
1348 VerifyPointersVisitor visitor;
1349 int size = object->Size();
1350 object->IterateBody(map->instance_type(), size, &visitor);
Steve Blocka7e24c12009-10-30 11:49:00 +00001351
Ben Murdoch85b71792012-04-11 18:30:58 +01001352 current += size;
Steve Blocka7e24c12009-10-30 11:49:00 +00001353 }
1354
Ben Murdoch85b71792012-04-11 18:30:58 +01001355 // The allocation pointer should not be in the middle of an object.
1356 ASSERT(current == top());
Steve Blocka7e24c12009-10-30 11:49:00 +00001357}
1358#endif
1359
Ben Murdoch85b71792012-04-11 18:30:58 +01001360
1361bool SemiSpace::Commit() {
1362 ASSERT(!is_committed());
1363 if (!heap()->isolate()->memory_allocator()->CommitBlock(
1364 start_, capacity_, executable())) {
1365 return false;
1366 }
1367 committed_ = true;
1368 return true;
1369}
1370
1371
1372bool SemiSpace::Uncommit() {
1373 ASSERT(is_committed());
1374 if (!heap()->isolate()->memory_allocator()->UncommitBlock(
1375 start_, capacity_)) {
1376 return false;
1377 }
1378 committed_ = false;
1379 return true;
1380}
1381
1382
Steve Blocka7e24c12009-10-30 11:49:00 +00001383// -----------------------------------------------------------------------------
1384// SemiSpace implementation
1385
Ben Murdoch85b71792012-04-11 18:30:58 +01001386bool SemiSpace::Setup(Address start,
Steve Blocka7e24c12009-10-30 11:49:00 +00001387 int initial_capacity,
1388 int maximum_capacity) {
1389 // Creates a space in the young generation. The constructor does not
1390 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1391 // memory of size 'capacity' when set up, and does not grow or shrink
1392 // otherwise. In the mark-compact collector, the memory region of the from
1393 // space is used as the marking stack. It requires contiguous memory
1394 // addresses.
Ben Murdoch85b71792012-04-11 18:30:58 +01001395 initial_capacity_ = initial_capacity;
Steve Blocka7e24c12009-10-30 11:49:00 +00001396 capacity_ = initial_capacity;
Ben Murdoch85b71792012-04-11 18:30:58 +01001397 maximum_capacity_ = maximum_capacity;
Steve Blocka7e24c12009-10-30 11:49:00 +00001398 committed_ = false;
Ben Murdoch85b71792012-04-11 18:30:58 +01001399
Steve Blocka7e24c12009-10-30 11:49:00 +00001400 start_ = start;
1401 address_mask_ = ~(maximum_capacity - 1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001402 object_mask_ = address_mask_ | kHeapObjectTagMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00001403 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
1404 age_mark_ = start_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001405
1406 return Commit();
Steve Blocka7e24c12009-10-30 11:49:00 +00001407}
1408
1409
1410void SemiSpace::TearDown() {
1411 start_ = NULL;
1412 capacity_ = 0;
1413}
1414
1415
Ben Murdoch85b71792012-04-11 18:30:58 +01001416bool SemiSpace::Grow() {
1417 // Double the semispace size but only up to maximum capacity.
1418 int maximum_extra = maximum_capacity_ - capacity_;
1419 int extra = Min(RoundUp(capacity_, static_cast<int>(OS::AllocateAlignment())),
1420 maximum_extra);
1421 if (!heap()->isolate()->memory_allocator()->CommitBlock(
1422 high(), extra, executable())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 return false;
1424 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001425 capacity_ += extra;
Steve Blocka7e24c12009-10-30 11:49:00 +00001426 return true;
1427}
1428
1429
1430bool SemiSpace::GrowTo(int new_capacity) {
1431 ASSERT(new_capacity <= maximum_capacity_);
1432 ASSERT(new_capacity > capacity_);
1433 size_t delta = new_capacity - capacity_;
1434 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
Steve Block44f0eee2011-05-26 01:26:41 +01001435 if (!heap()->isolate()->memory_allocator()->CommitBlock(
Ben Murdoch85b71792012-04-11 18:30:58 +01001436 high(), delta, executable())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001437 return false;
1438 }
1439 capacity_ = new_capacity;
1440 return true;
1441}
1442
1443
1444bool SemiSpace::ShrinkTo(int new_capacity) {
1445 ASSERT(new_capacity >= initial_capacity_);
1446 ASSERT(new_capacity < capacity_);
Ben Murdoch85b71792012-04-11 18:30:58 +01001447 size_t delta = capacity_ - new_capacity;
1448 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1449 if (!heap()->isolate()->memory_allocator()->UncommitBlock(
1450 high() - delta, delta)) {
1451 return false;
Ben Murdochc7cc0282012-03-05 14:35:55 +00001452 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00001453 capacity_ = new_capacity;
Steve Blocka7e24c12009-10-30 11:49:00 +00001454 return true;
1455}
1456
1457
1458#ifdef DEBUG
1459void SemiSpace::Print() { }
1460
1461
Ben Murdoch85b71792012-04-11 18:30:58 +01001462void SemiSpace::Verify() { }
Steve Blocka7e24c12009-10-30 11:49:00 +00001463#endif
1464
1465
1466// -----------------------------------------------------------------------------
1467// SemiSpaceIterator implementation.
1468SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001469 Initialize(space, space->bottom(), space->top(), NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001470}
1471
1472
1473SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1474 HeapObjectCallback size_func) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001475 Initialize(space, space->bottom(), space->top(), size_func);
Steve Blocka7e24c12009-10-30 11:49:00 +00001476}
1477
1478
1479SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001480 Initialize(space, start, space->top(), NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001481}
1482
1483
Ben Murdoch85b71792012-04-11 18:30:58 +01001484void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
Steve Blocka7e24c12009-10-30 11:49:00 +00001485 Address end,
1486 HeapObjectCallback size_func) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001487 ASSERT(space->ToSpaceContains(start));
1488 ASSERT(space->ToSpaceLow() <= end
1489 && end <= space->ToSpaceHigh());
1490 space_ = &space->to_space_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001491 current_ = start;
1492 limit_ = end;
1493 size_func_ = size_func;
1494}
1495
1496
1497#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001498// heap_histograms is shared, always clear it before using it.
1499static void ClearHistograms() {
Steve Block44f0eee2011-05-26 01:26:41 +01001500 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001501 // We reset the name each time, though it hasn't changed.
Steve Block44f0eee2011-05-26 01:26:41 +01001502#define DEF_TYPE_NAME(name) isolate->heap_histograms()[name].set_name(#name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001503 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1504#undef DEF_TYPE_NAME
1505
Steve Block44f0eee2011-05-26 01:26:41 +01001506#define CLEAR_HISTOGRAM(name) isolate->heap_histograms()[name].clear();
Steve Blocka7e24c12009-10-30 11:49:00 +00001507 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1508#undef CLEAR_HISTOGRAM
1509
Steve Block44f0eee2011-05-26 01:26:41 +01001510 isolate->js_spill_information()->Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +00001511}
1512
1513
Steve Blocka7e24c12009-10-30 11:49:00 +00001514static void ClearCodeKindStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01001515 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001516 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001517 isolate->code_kind_statistics()[i] = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001518 }
1519}
1520
1521
1522static void ReportCodeKindStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01001523 Isolate* isolate = Isolate::Current();
Steve Block6ded16b2010-05-10 14:33:55 +01001524 const char* table[Code::NUMBER_OF_KINDS] = { NULL };
Steve Blocka7e24c12009-10-30 11:49:00 +00001525
1526#define CASE(name) \
1527 case Code::name: table[Code::name] = #name; \
1528 break
1529
1530 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1531 switch (static_cast<Code::Kind>(i)) {
1532 CASE(FUNCTION);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001533 CASE(OPTIMIZED_FUNCTION);
Steve Blocka7e24c12009-10-30 11:49:00 +00001534 CASE(STUB);
1535 CASE(BUILTIN);
1536 CASE(LOAD_IC);
1537 CASE(KEYED_LOAD_IC);
1538 CASE(STORE_IC);
1539 CASE(KEYED_STORE_IC);
1540 CASE(CALL_IC);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001541 CASE(KEYED_CALL_IC);
Ben Murdoch257744e2011-11-30 15:57:28 +00001542 CASE(UNARY_OP_IC);
1543 CASE(BINARY_OP_IC);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001544 CASE(COMPARE_IC);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001545 CASE(TO_BOOLEAN_IC);
Steve Blocka7e24c12009-10-30 11:49:00 +00001546 }
1547 }
1548
1549#undef CASE
1550
1551 PrintF("\n Code kind histograms: \n");
1552 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001553 if (isolate->code_kind_statistics()[i] > 0) {
1554 PrintF(" %-20s: %10d bytes\n", table[i],
1555 isolate->code_kind_statistics()[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +00001556 }
1557 }
1558 PrintF("\n");
1559}
1560
1561
1562static int CollectHistogramInfo(HeapObject* obj) {
Steve Block44f0eee2011-05-26 01:26:41 +01001563 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001564 InstanceType type = obj->map()->instance_type();
1565 ASSERT(0 <= type && type <= LAST_TYPE);
Steve Block44f0eee2011-05-26 01:26:41 +01001566 ASSERT(isolate->heap_histograms()[type].name() != NULL);
1567 isolate->heap_histograms()[type].increment_number(1);
1568 isolate->heap_histograms()[type].increment_bytes(obj->Size());
Steve Blocka7e24c12009-10-30 11:49:00 +00001569
1570 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001571 JSObject::cast(obj)->IncrementSpillStatistics(
1572 isolate->js_spill_information());
Steve Blocka7e24c12009-10-30 11:49:00 +00001573 }
1574
1575 return obj->Size();
1576}
1577
1578
1579static void ReportHistogram(bool print_spill) {
Steve Block44f0eee2011-05-26 01:26:41 +01001580 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001581 PrintF("\n Object Histogram:\n");
1582 for (int i = 0; i <= LAST_TYPE; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001583 if (isolate->heap_histograms()[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001584 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Block44f0eee2011-05-26 01:26:41 +01001585 isolate->heap_histograms()[i].name(),
1586 isolate->heap_histograms()[i].number(),
1587 isolate->heap_histograms()[i].bytes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001588 }
1589 }
1590 PrintF("\n");
1591
1592 // Summarize string types.
1593 int string_number = 0;
1594 int string_bytes = 0;
1595#define INCREMENT(type, size, name, camel_name) \
Steve Block44f0eee2011-05-26 01:26:41 +01001596 string_number += isolate->heap_histograms()[type].number(); \
1597 string_bytes += isolate->heap_histograms()[type].bytes();
Steve Blocka7e24c12009-10-30 11:49:00 +00001598 STRING_TYPE_LIST(INCREMENT)
1599#undef INCREMENT
1600 if (string_number > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001601 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
Steve Blocka7e24c12009-10-30 11:49:00 +00001602 string_bytes);
1603 }
1604
1605 if (FLAG_collect_heap_spill_statistics && print_spill) {
Steve Block44f0eee2011-05-26 01:26:41 +01001606 isolate->js_spill_information()->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +00001607 }
1608}
1609#endif // DEBUG
1610
1611
1612// Support for statistics gathering for --heap-stats and --log-gc.
Steve Blocka7e24c12009-10-30 11:49:00 +00001613void NewSpace::ClearHistograms() {
1614 for (int i = 0; i <= LAST_TYPE; i++) {
1615 allocated_histogram_[i].clear();
1616 promoted_histogram_[i].clear();
1617 }
1618}
1619
1620// Because the copying collector does not touch garbage objects, we iterate
1621// the new space before a collection to get a histogram of allocated objects.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001622// This only happens when --log-gc flag is set.
Steve Blocka7e24c12009-10-30 11:49:00 +00001623void NewSpace::CollectStatistics() {
1624 ClearHistograms();
1625 SemiSpaceIterator it(this);
Ben Murdoch85b71792012-04-11 18:30:58 +01001626 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next())
Leon Clarked91b9f72010-01-27 17:25:45 +00001627 RecordAllocation(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001628}
1629
1630
Steve Block44f0eee2011-05-26 01:26:41 +01001631static void DoReportStatistics(Isolate* isolate,
1632 HistogramInfo* info, const char* description) {
1633 LOG(isolate, HeapSampleBeginEvent("NewSpace", description));
Steve Blocka7e24c12009-10-30 11:49:00 +00001634 // Lump all the string types together.
1635 int string_number = 0;
1636 int string_bytes = 0;
1637#define INCREMENT(type, size, name, camel_name) \
1638 string_number += info[type].number(); \
1639 string_bytes += info[type].bytes();
1640 STRING_TYPE_LIST(INCREMENT)
1641#undef INCREMENT
1642 if (string_number > 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001643 LOG(isolate,
1644 HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
Steve Blocka7e24c12009-10-30 11:49:00 +00001645 }
1646
1647 // Then do the other types.
1648 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1649 if (info[i].number() > 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001650 LOG(isolate,
1651 HeapSampleItemEvent(info[i].name(), info[i].number(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001652 info[i].bytes()));
1653 }
1654 }
Steve Block44f0eee2011-05-26 01:26:41 +01001655 LOG(isolate, HeapSampleEndEvent("NewSpace", description));
Steve Blocka7e24c12009-10-30 11:49:00 +00001656}
Steve Blocka7e24c12009-10-30 11:49:00 +00001657
1658
1659void NewSpace::ReportStatistics() {
1660#ifdef DEBUG
1661 if (FLAG_heap_stats) {
1662 float pct = static_cast<float>(Available()) / Capacity();
Ben Murdochf87a2032010-10-22 12:50:53 +01001663 PrintF(" capacity: %" V8_PTR_PREFIX "d"
1664 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001665 Capacity(), Available(), static_cast<int>(pct*100));
1666 PrintF("\n Object Histogram:\n");
1667 for (int i = 0; i <= LAST_TYPE; i++) {
1668 if (allocated_histogram_[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001669 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001670 allocated_histogram_[i].name(),
1671 allocated_histogram_[i].number(),
1672 allocated_histogram_[i].bytes());
1673 }
1674 }
1675 PrintF("\n");
1676 }
1677#endif // DEBUG
1678
Steve Blocka7e24c12009-10-30 11:49:00 +00001679 if (FLAG_log_gc) {
Steve Block44f0eee2011-05-26 01:26:41 +01001680 Isolate* isolate = ISOLATE;
1681 DoReportStatistics(isolate, allocated_histogram_, "allocated");
1682 DoReportStatistics(isolate, promoted_histogram_, "promoted");
Steve Blocka7e24c12009-10-30 11:49:00 +00001683 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001684}
1685
1686
1687void NewSpace::RecordAllocation(HeapObject* obj) {
1688 InstanceType type = obj->map()->instance_type();
1689 ASSERT(0 <= type && type <= LAST_TYPE);
1690 allocated_histogram_[type].increment_number(1);
1691 allocated_histogram_[type].increment_bytes(obj->Size());
1692}
1693
1694
1695void NewSpace::RecordPromotion(HeapObject* obj) {
1696 InstanceType type = obj->map()->instance_type();
1697 ASSERT(0 <= type && type <= LAST_TYPE);
1698 promoted_histogram_[type].increment_number(1);
1699 promoted_histogram_[type].increment_bytes(obj->Size());
1700}
Steve Blocka7e24c12009-10-30 11:49:00 +00001701
Ben Murdoch85b71792012-04-11 18:30:58 +01001702
Steve Blocka7e24c12009-10-30 11:49:00 +00001703// -----------------------------------------------------------------------------
1704// Free lists for old object spaces implementation
1705
Steve Block44f0eee2011-05-26 01:26:41 +01001706void FreeListNode::set_size(Heap* heap, int size_in_bytes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001707 ASSERT(size_in_bytes > 0);
1708 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1709
1710 // We write a map and possibly size information to the block. If the block
Ben Murdoch85b71792012-04-11 18:30:58 +01001711 // is big enough to be a ByteArray with at least one extra word (the next
1712 // pointer), we set its map to be the byte array map and its size to an
Steve Blocka7e24c12009-10-30 11:49:00 +00001713 // appropriate array length for the desired size from HeapObject::Size().
1714 // If the block is too small (eg, one or two words), to hold both a size
1715 // field and a next pointer, we give it a filler map that gives it the
1716 // correct size.
Ben Murdoch85b71792012-04-11 18:30:58 +01001717 if (size_in_bytes > ByteArray::kHeaderSize) {
1718 set_map(heap->raw_unchecked_byte_array_map());
1719 // Can't use ByteArray::cast because it fails during deserialization.
1720 ByteArray* this_as_byte_array = reinterpret_cast<ByteArray*>(this);
1721 this_as_byte_array->set_length(ByteArray::LengthFor(size_in_bytes));
Steve Blocka7e24c12009-10-30 11:49:00 +00001722 } else if (size_in_bytes == kPointerSize) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001723 set_map(heap->raw_unchecked_one_pointer_filler_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00001724 } else if (size_in_bytes == 2 * kPointerSize) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001725 set_map(heap->raw_unchecked_two_pointer_filler_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00001726 } else {
1727 UNREACHABLE();
1728 }
Steve Blockd0582a62009-12-15 09:54:21 +00001729 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
Ben Murdoch85b71792012-04-11 18:30:58 +01001730 // deserialization because the byte array map is not done yet.
Steve Blocka7e24c12009-10-30 11:49:00 +00001731}
1732
1733
Ben Murdoch85b71792012-04-11 18:30:58 +01001734Address FreeListNode::next(Heap* heap) {
Steve Block3ce2e202009-11-05 08:53:23 +00001735 ASSERT(IsFreeListNode(this));
Ben Murdoch85b71792012-04-11 18:30:58 +01001736 if (map() == heap->raw_unchecked_byte_array_map()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001737 ASSERT(Size() >= kNextOffset + kPointerSize);
Ben Murdoch85b71792012-04-11 18:30:58 +01001738 return Memory::Address_at(address() + kNextOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001739 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +01001740 return Memory::Address_at(address() + kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001741 }
1742}
1743
1744
Ben Murdoch85b71792012-04-11 18:30:58 +01001745void FreeListNode::set_next(Heap* heap, Address next) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001746 ASSERT(IsFreeListNode(this));
Ben Murdoch85b71792012-04-11 18:30:58 +01001747 if (map() == heap->raw_unchecked_byte_array_map()) {
1748 ASSERT(Size() >= kNextOffset + kPointerSize);
1749 Memory::Address_at(address() + kNextOffset) = next;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001750 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +01001751 Memory::Address_at(address() + kPointerSize) = next;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001752 }
1753}
1754
1755
Ben Murdoch85b71792012-04-11 18:30:58 +01001756OldSpaceFreeList::OldSpaceFreeList(Heap* heap, AllocationSpace owner)
1757 : heap_(heap),
1758 owner_(owner) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001759 Reset();
1760}
1761
1762
Ben Murdoch85b71792012-04-11 18:30:58 +01001763void OldSpaceFreeList::Reset() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001764 available_ = 0;
Ben Murdoch85b71792012-04-11 18:30:58 +01001765 for (int i = 0; i < kFreeListsLength; i++) {
1766 free_[i].head_node_ = NULL;
1767 }
1768 needs_rebuild_ = false;
1769 finger_ = kHead;
1770 free_[kHead].next_size_ = kEnd;
Steve Blocka7e24c12009-10-30 11:49:00 +00001771}
1772
1773
Ben Murdoch85b71792012-04-11 18:30:58 +01001774void OldSpaceFreeList::RebuildSizeList() {
1775 ASSERT(needs_rebuild_);
1776 int cur = kHead;
1777 for (int i = cur + 1; i < kFreeListsLength; i++) {
1778 if (free_[i].head_node_ != NULL) {
1779 free_[cur].next_size_ = i;
1780 cur = i;
1781 }
1782 }
1783 free_[cur].next_size_ = kEnd;
1784 needs_rebuild_ = false;
1785}
1786
1787
1788int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1789#ifdef DEBUG
1790 Isolate::Current()->memory_allocator()->ZapBlock(start, size_in_bytes);
1791#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001792 FreeListNode* node = FreeListNode::FromAddress(start);
Steve Block44f0eee2011-05-26 01:26:41 +01001793 node->set_size(heap_, size_in_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001794
Ben Murdoch85b71792012-04-11 18:30:58 +01001795 // We don't use the freelists in compacting mode. This makes it more like a
1796 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1797 // collector.
1798 if (FLAG_always_compact) {
1799 return size_in_bytes;
Steve Blocka7e24c12009-10-30 11:49:00 +00001800 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001801
1802 // Early return to drop too-small blocks on the floor (one or two word
1803 // blocks cannot hold a map pointer, a size field, and a pointer to the
1804 // next block in the free list).
1805 if (size_in_bytes < kMinBlockSize) {
1806 return size_in_bytes;
1807 }
1808
1809 // Insert other blocks at the head of an exact free list.
1810 int index = size_in_bytes >> kPointerSizeLog2;
1811 node->set_next(heap_, free_[index].head_node_);
1812 free_[index].head_node_ = node->address();
Steve Blocka7e24c12009-10-30 11:49:00 +00001813 available_ += size_in_bytes;
Ben Murdoch85b71792012-04-11 18:30:58 +01001814 needs_rebuild_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001815 return 0;
1816}
1817
1818
Ben Murdoch85b71792012-04-11 18:30:58 +01001819MaybeObject* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001820 ASSERT(0 < size_in_bytes);
1821 ASSERT(size_in_bytes <= kMaxBlockSize);
1822 ASSERT(IsAligned(size_in_bytes, kPointerSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001823
Ben Murdoch85b71792012-04-11 18:30:58 +01001824 if (needs_rebuild_) RebuildSizeList();
1825 int index = size_in_bytes >> kPointerSizeLog2;
1826 // Check for a perfect fit.
1827 if (free_[index].head_node_ != NULL) {
1828 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1829 // If this was the last block of its size, remove the size.
1830 if ((free_[index].head_node_ = node->next(heap_)) == NULL)
1831 RemoveSize(index);
1832 available_ -= size_in_bytes;
1833 *wasted_bytes = 0;
1834 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1835 return node;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001836 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001837 // Search the size list for the best fit.
1838 int prev = finger_ < index ? finger_ : kHead;
1839 int cur = FindSize(index, &prev);
1840 ASSERT(index < cur);
1841 if (cur == kEnd) {
1842 // No large enough size in list.
1843 *wasted_bytes = 0;
1844 return Failure::RetryAfterGC(owner_);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001845 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001846 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1847 int rem = cur - index;
1848 int rem_bytes = rem << kPointerSizeLog2;
1849 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
1850 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
1851 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1852 size_in_bytes);
1853 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1854 // to avoid many redundant tests and calls to Insert/RemoveSize.
1855 if (prev < rem) {
1856 // Simple case: insert rem between prev and cur.
1857 finger_ = prev;
1858 free_[prev].next_size_ = rem;
1859 // If this was the last block of size cur, remove the size.
1860 if ((free_[cur].head_node_ = cur_node->next(heap_)) == NULL) {
1861 free_[rem].next_size_ = free_[cur].next_size_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001862 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +01001863 free_[rem].next_size_ = cur;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001864 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001865 // Add the remainder block.
1866 rem_node->set_size(heap_, rem_bytes);
1867 rem_node->set_next(heap_, free_[rem].head_node_);
1868 free_[rem].head_node_ = rem_node->address();
1869 } else {
1870 // If this was the last block of size cur, remove the size.
1871 if ((free_[cur].head_node_ = cur_node->next(heap_)) == NULL) {
1872 finger_ = prev;
1873 free_[prev].next_size_ = free_[cur].next_size_;
1874 }
1875 if (rem_bytes < kMinBlockSize) {
1876 // Too-small remainder is wasted.
1877 rem_node->set_size(heap_, rem_bytes);
1878 available_ -= size_in_bytes + rem_bytes;
1879 *wasted_bytes = rem_bytes;
1880 return cur_node;
1881 }
1882 // Add the remainder block and, if needed, insert its size.
1883 rem_node->set_size(heap_, rem_bytes);
1884 rem_node->set_next(heap_, free_[rem].head_node_);
1885 free_[rem].head_node_ = rem_node->address();
1886 if (rem_node->next(heap_) == NULL) InsertSize(rem);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001887 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001888 available_ -= size_in_bytes;
1889 *wasted_bytes = 0;
1890 return cur_node;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001891}
1892
1893
Ben Murdoch85b71792012-04-11 18:30:58 +01001894void OldSpaceFreeList::MarkNodes() {
1895 for (int i = 0; i < kFreeListsLength; i++) {
1896 Address cur_addr = free_[i].head_node_;
1897 while (cur_addr != NULL) {
1898 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1899 cur_addr = cur_node->next(heap_);
1900 cur_node->SetMark();
1901 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001902 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001903}
1904
1905
1906#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +01001907bool OldSpaceFreeList::Contains(FreeListNode* node) {
1908 for (int i = 0; i < kFreeListsLength; i++) {
1909 Address cur_addr = free_[i].head_node_;
1910 while (cur_addr != NULL) {
1911 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1912 if (cur_node == node) return true;
1913 cur_addr = cur_node->next(heap_);
1914 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001915 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001916 return false;
1917}
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001918#endif
1919
1920
Ben Murdoch85b71792012-04-11 18:30:58 +01001921FixedSizeFreeList::FixedSizeFreeList(Heap* heap,
1922 AllocationSpace owner,
1923 int object_size)
1924 : heap_(heap), owner_(owner), object_size_(object_size) {
1925 Reset();
1926}
1927
1928
1929void FixedSizeFreeList::Reset() {
1930 available_ = 0;
1931 head_ = tail_ = NULL;
1932}
1933
1934
1935void FixedSizeFreeList::Free(Address start) {
1936#ifdef DEBUG
1937 Isolate::Current()->memory_allocator()->ZapBlock(start, object_size_);
1938#endif
1939 // We only use the freelists with mark-sweep.
1940 ASSERT(!HEAP->mark_compact_collector()->IsCompacting());
1941 FreeListNode* node = FreeListNode::FromAddress(start);
1942 node->set_size(heap_, object_size_);
1943 node->set_next(heap_, NULL);
1944 if (head_ == NULL) {
1945 tail_ = head_ = node->address();
1946 } else {
1947 FreeListNode::FromAddress(tail_)->set_next(heap_, node->address());
1948 tail_ = node->address();
1949 }
1950 available_ += object_size_;
1951}
1952
1953
1954MaybeObject* FixedSizeFreeList::Allocate() {
1955 if (head_ == NULL) {
1956 return Failure::RetryAfterGC(owner_);
1957 }
1958
1959 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1960 FreeListNode* node = FreeListNode::FromAddress(head_);
1961 head_ = node->next(heap_);
1962 available_ -= object_size_;
1963 return node;
1964}
1965
1966
1967void FixedSizeFreeList::MarkNodes() {
1968 Address cur_addr = head_;
1969 while (cur_addr != NULL && cur_addr != tail_) {
1970 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1971 cur_addr = cur_node->next(heap_);
1972 cur_node->SetMark();
1973 }
1974}
1975
1976
Steve Blocka7e24c12009-10-30 11:49:00 +00001977// -----------------------------------------------------------------------------
1978// OldSpace implementation
1979
Ben Murdoch85b71792012-04-11 18:30:58 +01001980void OldSpace::PrepareForMarkCompact(bool will_compact) {
1981 // Call prepare of the super class.
1982 PagedSpace::PrepareForMarkCompact(will_compact);
1983
1984 if (will_compact) {
1985 // Reset relocation info. During a compacting collection, everything in
1986 // the space is considered 'available' and we will rediscover live data
1987 // and waste during the collection.
1988 MCResetRelocationInfo();
1989 ASSERT(Available() == Capacity());
1990 } else {
1991 // During a non-compacting collection, everything below the linear
1992 // allocation pointer is considered allocated (everything above is
1993 // available) and we will rediscover available and wasted bytes during
1994 // the collection.
1995 accounting_stats_.AllocateBytes(free_list_.available());
1996 accounting_stats_.FillWastedBytes(Waste());
Steve Blocka7e24c12009-10-30 11:49:00 +00001997 }
1998
1999 // Clear the free list before a full GC---it will be rebuilt afterward.
2000 free_list_.Reset();
2001}
2002
2003
Ben Murdoch85b71792012-04-11 18:30:58 +01002004void OldSpace::MCCommitRelocationInfo() {
2005 // Update fast allocation info.
2006 allocation_info_.top = mc_forwarding_info_.top;
2007 allocation_info_.limit = mc_forwarding_info_.limit;
2008 ASSERT(allocation_info_.VerifyPagedAllocation());
Steve Blocka7e24c12009-10-30 11:49:00 +00002009
Ben Murdoch85b71792012-04-11 18:30:58 +01002010 // The space is compacted and we haven't yet built free lists or
2011 // wasted any space.
2012 ASSERT(Waste() == 0);
2013 ASSERT(AvailableFree() == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002014
Ben Murdoch85b71792012-04-11 18:30:58 +01002015 // Build the free list for the space.
2016 int computed_size = 0;
2017 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2018 while (it.has_next()) {
2019 Page* p = it.next();
2020 // Space below the relocation pointer is allocated.
2021 computed_size +=
2022 static_cast<int>(p->AllocationWatermark() - p->ObjectAreaStart());
2023 if (it.has_next()) {
2024 // Free the space at the top of the page.
2025 int extra_size =
2026 static_cast<int>(p->ObjectAreaEnd() - p->AllocationWatermark());
2027 if (extra_size > 0) {
2028 int wasted_bytes = free_list_.Free(p->AllocationWatermark(),
2029 extra_size);
2030 // The bytes we have just "freed" to add to the free list were
2031 // already accounted as available.
2032 accounting_stats_.WasteBytes(wasted_bytes);
2033 }
2034 }
2035 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002036
Ben Murdoch85b71792012-04-11 18:30:58 +01002037 // Make sure the computed size - based on the used portion of the pages in
2038 // use - matches the size obtained while computing forwarding addresses.
2039 ASSERT(computed_size == Size());
2040}
2041
2042
2043bool NewSpace::ReserveSpace(int bytes) {
2044 // We can't reliably unpack a partial snapshot that needs more new space
2045 // space than the minimum NewSpace size.
2046 ASSERT(bytes <= InitialCapacity());
2047 Address limit = allocation_info_.limit;
2048 Address top = allocation_info_.top;
2049 return limit - top >= bytes;
2050}
2051
2052
2053void PagedSpace::FreePages(Page* prev, Page* last) {
2054 if (last == AllocationTopPage()) {
2055 // Pages are already at the end of used pages.
2056 return;
2057 }
2058
2059 Page* first = NULL;
2060
2061 // Remove pages from the list.
2062 if (prev == NULL) {
2063 first = first_page_;
2064 first_page_ = last->next_page();
2065 } else {
2066 first = prev->next_page();
2067 heap()->isolate()->memory_allocator()->SetNextPage(
2068 prev, last->next_page());
2069 }
2070
2071 // Attach it after the last page.
2072 heap()->isolate()->memory_allocator()->SetNextPage(last_page_, first);
2073 last_page_ = last;
2074 heap()->isolate()->memory_allocator()->SetNextPage(last, NULL);
2075
2076 // Clean them up.
2077 do {
2078 first->InvalidateWatermark(true);
2079 first->SetAllocationWatermark(first->ObjectAreaStart());
2080 first->SetCachedAllocationWatermark(first->ObjectAreaStart());
2081 first->SetRegionMarks(Page::kAllRegionsCleanMarks);
2082 first = first->next_page();
2083 } while (first != NULL);
2084
2085 // Order of pages in this space might no longer be consistent with
2086 // order of pages in chunks.
2087 page_list_is_chunk_ordered_ = false;
2088}
2089
2090
2091void PagedSpace::RelinkPageListInChunkOrder(bool deallocate_blocks) {
2092 const bool add_to_freelist = true;
2093
2094 // Mark used and unused pages to properly fill unused pages
2095 // after reordering.
2096 PageIterator all_pages_iterator(this, PageIterator::ALL_PAGES);
2097 Page* last_in_use = AllocationTopPage();
2098 bool in_use = true;
2099
2100 while (all_pages_iterator.has_next()) {
2101 Page* p = all_pages_iterator.next();
2102 p->SetWasInUseBeforeMC(in_use);
2103 if (p == last_in_use) {
2104 // We passed a page containing allocation top. All consequent
2105 // pages are not used.
2106 in_use = false;
2107 }
2108 }
2109
2110 if (page_list_is_chunk_ordered_) return;
2111
2112 Page* new_last_in_use = Page::FromAddress(NULL);
2113 heap()->isolate()->memory_allocator()->RelinkPageListInChunkOrder(
2114 this, &first_page_, &last_page_, &new_last_in_use);
2115 ASSERT(new_last_in_use->is_valid());
2116
2117 if (new_last_in_use != last_in_use) {
2118 // Current allocation top points to a page which is now in the middle
2119 // of page list. We should move allocation top forward to the new last
2120 // used page so various object iterators will continue to work properly.
2121 int size_in_bytes = static_cast<int>(PageAllocationLimit(last_in_use) -
2122 last_in_use->AllocationTop());
2123
2124 last_in_use->SetAllocationWatermark(last_in_use->AllocationTop());
2125 if (size_in_bytes > 0) {
2126 Address start = last_in_use->AllocationTop();
2127 if (deallocate_blocks) {
2128 accounting_stats_.AllocateBytes(size_in_bytes);
2129 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2130 } else {
2131 heap()->CreateFillerObjectAt(start, size_in_bytes);
2132 }
2133 }
2134
2135 // New last in use page was in the middle of the list before
2136 // sorting so it full.
2137 SetTop(new_last_in_use->AllocationTop());
2138
2139 ASSERT(AllocationTopPage() == new_last_in_use);
2140 ASSERT(AllocationTopPage()->WasInUseBeforeMC());
2141 }
2142
2143 PageIterator pages_in_use_iterator(this, PageIterator::PAGES_IN_USE);
2144 while (pages_in_use_iterator.has_next()) {
2145 Page* p = pages_in_use_iterator.next();
2146 if (!p->WasInUseBeforeMC()) {
2147 // Empty page is in the middle of a sequence of used pages.
2148 // Allocate it as a whole and deallocate immediately.
2149 int size_in_bytes = static_cast<int>(PageAllocationLimit(p) -
2150 p->ObjectAreaStart());
2151
2152 p->SetAllocationWatermark(p->ObjectAreaStart());
2153 Address start = p->ObjectAreaStart();
2154 if (deallocate_blocks) {
2155 accounting_stats_.AllocateBytes(size_in_bytes);
2156 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2157 } else {
2158 heap()->CreateFillerObjectAt(start, size_in_bytes);
2159 }
2160 }
2161 }
2162
2163 page_list_is_chunk_ordered_ = true;
2164}
2165
2166
2167void PagedSpace::PrepareForMarkCompact(bool will_compact) {
2168 if (will_compact) {
2169 RelinkPageListInChunkOrder(false);
2170 }
2171}
2172
2173
2174bool PagedSpace::ReserveSpace(int bytes) {
2175 Address limit = allocation_info_.limit;
2176 Address top = allocation_info_.top;
2177 if (limit - top >= bytes) return true;
2178
2179 // There wasn't enough space in the current page. Lets put the rest
2180 // of the page on the free list and start a fresh page.
2181 PutRestOfCurrentPageOnFreeList(TopPageOf(allocation_info_));
2182
2183 Page* reserved_page = TopPageOf(allocation_info_);
2184 int bytes_left_to_reserve = bytes;
2185 while (bytes_left_to_reserve > 0) {
2186 if (!reserved_page->next_page()->is_valid()) {
2187 if (heap()->OldGenerationAllocationLimitReached()) return false;
2188 Expand(reserved_page);
2189 }
2190 bytes_left_to_reserve -= Page::kPageSize;
2191 reserved_page = reserved_page->next_page();
2192 if (!reserved_page->is_valid()) return false;
2193 }
2194 ASSERT(TopPageOf(allocation_info_)->next_page()->is_valid());
2195 TopPageOf(allocation_info_)->next_page()->InvalidateWatermark(true);
2196 SetAllocationInfo(&allocation_info_,
2197 TopPageOf(allocation_info_)->next_page());
Leon Clarkee46be812010-01-19 14:06:41 +00002198 return true;
2199}
2200
2201
2202// You have to call this last, since the implementation from PagedSpace
2203// doesn't know that memory was 'promised' to large object space.
2204bool LargeObjectSpace::ReserveSpace(int bytes) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002205 return heap()->OldGenerationSpaceAvailable() >= bytes;
Leon Clarkee46be812010-01-19 14:06:41 +00002206}
2207
2208
Ben Murdoch85b71792012-04-11 18:30:58 +01002209// Slow case for normal allocation. Try in order: (1) allocate in the next
2210// page in the space, (2) allocate off the space's free list, (3) expand the
2211// space, (4) fail.
2212HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
2213 // Linear allocation in this space has failed. If there is another page
2214 // in the space, move to that page and allocate there. This allocation
2215 // should succeed (size_in_bytes should not be greater than a page's
2216 // object area size).
2217 Page* current_page = TopPageOf(allocation_info_);
2218 if (current_page->next_page()->is_valid()) {
2219 return AllocateInNextPage(current_page, size_in_bytes);
2220 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002221
Ben Murdoch85b71792012-04-11 18:30:58 +01002222 // There is no next page in this space. Try free list allocation unless that
2223 // is currently forbidden.
2224 if (!heap()->linear_allocation()) {
2225 int wasted_bytes;
2226 Object* result;
2227 MaybeObject* maybe = free_list_.Allocate(size_in_bytes, &wasted_bytes);
2228 accounting_stats_.WasteBytes(wasted_bytes);
2229 if (maybe->ToObject(&result)) {
2230 accounting_stats_.AllocateBytes(size_in_bytes);
2231
2232 HeapObject* obj = HeapObject::cast(result);
2233 Page* p = Page::FromAddress(obj->address());
2234
2235 if (obj->address() >= p->AllocationWatermark()) {
2236 // There should be no hole between the allocation watermark
2237 // and allocated object address.
2238 // Memory above the allocation watermark was not swept and
2239 // might contain garbage pointers to new space.
2240 ASSERT(obj->address() == p->AllocationWatermark());
2241 p->SetAllocationWatermark(obj->address() + size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002242 }
Ben Murdoch85b71792012-04-11 18:30:58 +01002243
2244 return obj;
Steve Blockd0582a62009-12-15 09:54:21 +00002245 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00002246 }
2247
Steve Blocka7e24c12009-10-30 11:49:00 +00002248 // Free list allocation failed and there is no next page. Fail if we have
2249 // hit the old generation size limit that should cause a garbage
2250 // collection.
Steve Block44f0eee2011-05-26 01:26:41 +01002251 if (!heap()->always_allocate() &&
2252 heap()->OldGenerationAllocationLimitReached()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002253 return NULL;
2254 }
2255
Ben Murdochc7cc0282012-03-05 14:35:55 +00002256 // Try to expand the space and allocate in the new next page.
Ben Murdoch85b71792012-04-11 18:30:58 +01002257 ASSERT(!current_page->next_page()->is_valid());
2258 if (Expand(current_page)) {
2259 return AllocateInNextPage(current_page, size_in_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00002260 }
2261
2262 // Finally, fail.
2263 return NULL;
2264}
2265
2266
Ben Murdoch85b71792012-04-11 18:30:58 +01002267void OldSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
2268 current_page->SetAllocationWatermark(allocation_info_.top);
2269 int free_size =
2270 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
2271 if (free_size > 0) {
2272 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
2273 accounting_stats_.WasteBytes(wasted_bytes);
2274 }
2275}
2276
2277
2278void FixedSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
2279 current_page->SetAllocationWatermark(allocation_info_.top);
2280 int free_size =
2281 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
2282 // In the fixed space free list all the free list items have the right size.
2283 // We use up the rest of the page while preserving this invariant.
2284 while (free_size >= object_size_in_bytes_) {
2285 free_list_.Free(allocation_info_.top);
2286 allocation_info_.top += object_size_in_bytes_;
2287 free_size -= object_size_in_bytes_;
2288 accounting_stats_.WasteBytes(object_size_in_bytes_);
2289 }
2290}
2291
2292
2293// Add the block at the top of the page to the space's free list, set the
2294// allocation info to the next page (assumed to be one), and allocate
2295// linearly there.
2296HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
2297 int size_in_bytes) {
2298 ASSERT(current_page->next_page()->is_valid());
2299 Page* next_page = current_page->next_page();
2300 next_page->ClearGCFields();
2301 PutRestOfCurrentPageOnFreeList(current_page);
2302 SetAllocationInfo(&allocation_info_, next_page);
2303 return AllocateLinearly(&allocation_info_, size_in_bytes);
2304}
2305
2306
2307void OldSpace::DeallocateBlock(Address start,
2308 int size_in_bytes,
2309 bool add_to_freelist) {
2310 Free(start, size_in_bytes, add_to_freelist);
2311}
2312
2313
Steve Blocka7e24c12009-10-30 11:49:00 +00002314#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002315void PagedSpace::ReportCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01002316 Isolate* isolate = Isolate::Current();
2317 CommentStatistic* comments_statistics =
2318 isolate->paged_space_comments_statistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00002319 ReportCodeKindStatistics();
2320 PrintF("Code comment statistics (\" [ comment-txt : size/ "
2321 "count (average)\"):\n");
Steve Block44f0eee2011-05-26 01:26:41 +01002322 for (int i = 0; i <= CommentStatistic::kMaxComments; i++) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002323 const CommentStatistic& cs = comments_statistics[i];
2324 if (cs.size > 0) {
2325 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
2326 cs.size/cs.count);
2327 }
2328 }
2329 PrintF("\n");
2330}
2331
2332
2333void PagedSpace::ResetCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01002334 Isolate* isolate = Isolate::Current();
2335 CommentStatistic* comments_statistics =
2336 isolate->paged_space_comments_statistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00002337 ClearCodeKindStatistics();
Steve Block44f0eee2011-05-26 01:26:41 +01002338 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
2339 comments_statistics[i].Clear();
2340 }
2341 comments_statistics[CommentStatistic::kMaxComments].comment = "Unknown";
2342 comments_statistics[CommentStatistic::kMaxComments].size = 0;
2343 comments_statistics[CommentStatistic::kMaxComments].count = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002344}
2345
2346
Steve Block44f0eee2011-05-26 01:26:41 +01002347// Adds comment to 'comment_statistics' table. Performance OK as long as
Steve Blocka7e24c12009-10-30 11:49:00 +00002348// 'kMaxComments' is small
Steve Block44f0eee2011-05-26 01:26:41 +01002349static void EnterComment(Isolate* isolate, const char* comment, int delta) {
2350 CommentStatistic* comments_statistics =
2351 isolate->paged_space_comments_statistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00002352 // Do not count empty comments
2353 if (delta <= 0) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002354 CommentStatistic* cs = &comments_statistics[CommentStatistic::kMaxComments];
Steve Blocka7e24c12009-10-30 11:49:00 +00002355 // Search for a free or matching entry in 'comments_statistics': 'cs'
2356 // points to result.
Steve Block44f0eee2011-05-26 01:26:41 +01002357 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002358 if (comments_statistics[i].comment == NULL) {
2359 cs = &comments_statistics[i];
2360 cs->comment = comment;
2361 break;
2362 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2363 cs = &comments_statistics[i];
2364 break;
2365 }
2366 }
2367 // Update entry for 'comment'
2368 cs->size += delta;
2369 cs->count += 1;
2370}
2371
2372
2373// Call for each nested comment start (start marked with '[ xxx', end marked
2374// with ']'. RelocIterator 'it' must point to a comment reloc info.
Steve Block44f0eee2011-05-26 01:26:41 +01002375static void CollectCommentStatistics(Isolate* isolate, RelocIterator* it) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002376 ASSERT(!it->done());
2377 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
2378 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2379 if (tmp[0] != '[') {
2380 // Not a nested comment; skip
2381 return;
2382 }
2383
2384 // Search for end of nested comment or a new nested comment
2385 const char* const comment_txt =
2386 reinterpret_cast<const char*>(it->rinfo()->data());
2387 const byte* prev_pc = it->rinfo()->pc();
2388 int flat_delta = 0;
2389 it->next();
2390 while (true) {
2391 // All nested comments must be terminated properly, and therefore exit
2392 // from loop.
2393 ASSERT(!it->done());
2394 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
2395 const char* const txt =
2396 reinterpret_cast<const char*>(it->rinfo()->data());
Steve Blockd0582a62009-12-15 09:54:21 +00002397 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002398 if (txt[0] == ']') break; // End of nested comment
2399 // A new comment
Steve Block44f0eee2011-05-26 01:26:41 +01002400 CollectCommentStatistics(isolate, it);
Steve Blocka7e24c12009-10-30 11:49:00 +00002401 // Skip code that was covered with previous comment
2402 prev_pc = it->rinfo()->pc();
2403 }
2404 it->next();
2405 }
Steve Block44f0eee2011-05-26 01:26:41 +01002406 EnterComment(isolate, comment_txt, flat_delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002407}
2408
2409
2410// Collects code size statistics:
2411// - by code kind
2412// - by code comment
2413void PagedSpace::CollectCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01002414 Isolate* isolate = heap()->isolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00002415 HeapObjectIterator obj_it(this);
Ben Murdoch85b71792012-04-11 18:30:58 +01002416 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002417 if (obj->IsCode()) {
2418 Code* code = Code::cast(obj);
Steve Block44f0eee2011-05-26 01:26:41 +01002419 isolate->code_kind_statistics()[code->kind()] += code->Size();
Steve Blocka7e24c12009-10-30 11:49:00 +00002420 RelocIterator it(code);
2421 int delta = 0;
2422 const byte* prev_pc = code->instruction_start();
2423 while (!it.done()) {
2424 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
Steve Blockd0582a62009-12-15 09:54:21 +00002425 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
Steve Block44f0eee2011-05-26 01:26:41 +01002426 CollectCommentStatistics(isolate, &it);
Steve Blocka7e24c12009-10-30 11:49:00 +00002427 prev_pc = it.rinfo()->pc();
2428 }
2429 it.next();
2430 }
2431
2432 ASSERT(code->instruction_start() <= prev_pc &&
Leon Clarkeac952652010-07-15 11:15:24 +01002433 prev_pc <= code->instruction_end());
2434 delta += static_cast<int>(code->instruction_end() - prev_pc);
Steve Block44f0eee2011-05-26 01:26:41 +01002435 EnterComment(isolate, "NoComment", delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002436 }
2437 }
2438}
2439
2440
Ben Murdoch85b71792012-04-11 18:30:58 +01002441void OldSpace::ReportStatistics() {
Ben Murdochf87a2032010-10-22 12:50:53 +01002442 int pct = static_cast<int>(Available() * 100 / Capacity());
2443 PrintF(" capacity: %" V8_PTR_PREFIX "d"
2444 ", waste: %" V8_PTR_PREFIX "d"
2445 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00002446 Capacity(), Waste(), Available(), pct);
2447
Steve Blocka7e24c12009-10-30 11:49:00 +00002448 ClearHistograms();
2449 HeapObjectIterator obj_it(this);
Ben Murdoch85b71792012-04-11 18:30:58 +01002450 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
Leon Clarked91b9f72010-01-27 17:25:45 +00002451 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002452 ReportHistogram(true);
2453}
Steve Blocka7e24c12009-10-30 11:49:00 +00002454#endif
2455
2456// -----------------------------------------------------------------------------
2457// FixedSpace implementation
2458
Ben Murdoch85b71792012-04-11 18:30:58 +01002459void FixedSpace::PrepareForMarkCompact(bool will_compact) {
Steve Block6ded16b2010-05-10 14:33:55 +01002460 // Call prepare of the super class.
Ben Murdoch85b71792012-04-11 18:30:58 +01002461 PagedSpace::PrepareForMarkCompact(will_compact);
Steve Block6ded16b2010-05-10 14:33:55 +01002462
Ben Murdoch85b71792012-04-11 18:30:58 +01002463 if (will_compact) {
2464 // Reset relocation info.
2465 MCResetRelocationInfo();
2466
2467 // During a compacting collection, everything in the space is considered
2468 // 'available' (set by the call to MCResetRelocationInfo) and we will
2469 // rediscover live and wasted bytes during the collection.
2470 ASSERT(Available() == Capacity());
2471 } else {
2472 // During a non-compacting collection, everything below the linear
2473 // allocation pointer except wasted top-of-page blocks is considered
2474 // allocated and we will rediscover available bytes during the
2475 // collection.
2476 accounting_stats_.AllocateBytes(free_list_.available());
2477 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002478
2479 // Clear the free list before a full GC---it will be rebuilt afterward.
2480 free_list_.Reset();
2481}
2482
2483
Ben Murdoch85b71792012-04-11 18:30:58 +01002484void FixedSpace::MCCommitRelocationInfo() {
2485 // Update fast allocation info.
2486 allocation_info_.top = mc_forwarding_info_.top;
2487 allocation_info_.limit = mc_forwarding_info_.limit;
2488 ASSERT(allocation_info_.VerifyPagedAllocation());
2489
2490 // The space is compacted and we haven't yet wasted any space.
2491 ASSERT(Waste() == 0);
2492
2493 // Update allocation_top of each page in use and compute waste.
2494 int computed_size = 0;
2495 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2496 while (it.has_next()) {
2497 Page* page = it.next();
2498 Address page_top = page->AllocationTop();
2499 computed_size += static_cast<int>(page_top - page->ObjectAreaStart());
2500 if (it.has_next()) {
2501 accounting_stats_.WasteBytes(
2502 static_cast<int>(page->ObjectAreaEnd() - page_top));
2503 page->SetAllocationWatermark(page_top);
2504 }
2505 }
2506
2507 // Make sure the computed size - based on the used portion of the
2508 // pages in use - matches the size we adjust during allocation.
2509 ASSERT(computed_size == Size());
2510}
2511
2512
2513// Slow case for normal allocation. Try in order: (1) allocate in the next
2514// page in the space, (2) allocate off the space's free list, (3) expand the
2515// space, (4) fail.
2516HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2517 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2518 // Linear allocation in this space has failed. If there is another page
2519 // in the space, move to that page and allocate there. This allocation
2520 // should succeed.
2521 Page* current_page = TopPageOf(allocation_info_);
2522 if (current_page->next_page()->is_valid()) {
2523 return AllocateInNextPage(current_page, size_in_bytes);
2524 }
2525
2526 // There is no next page in this space. Try free list allocation unless
2527 // that is currently forbidden. The fixed space free list implicitly assumes
2528 // that all free blocks are of the fixed size.
2529 if (!heap()->linear_allocation()) {
2530 Object* result;
2531 MaybeObject* maybe = free_list_.Allocate();
2532 if (maybe->ToObject(&result)) {
2533 accounting_stats_.AllocateBytes(size_in_bytes);
2534 HeapObject* obj = HeapObject::cast(result);
2535 Page* p = Page::FromAddress(obj->address());
2536
2537 if (obj->address() >= p->AllocationWatermark()) {
2538 // There should be no hole between the allocation watermark
2539 // and allocated object address.
2540 // Memory above the allocation watermark was not swept and
2541 // might contain garbage pointers to new space.
2542 ASSERT(obj->address() == p->AllocationWatermark());
2543 p->SetAllocationWatermark(obj->address() + size_in_bytes);
2544 }
2545
2546 return obj;
2547 }
2548 }
2549
2550 // Free list allocation failed and there is no next page. Fail if we have
2551 // hit the old generation size limit that should cause a garbage
2552 // collection.
2553 if (!heap()->always_allocate() &&
2554 heap()->OldGenerationAllocationLimitReached()) {
2555 return NULL;
2556 }
2557
2558 // Try to expand the space and allocate in the new next page.
2559 ASSERT(!current_page->next_page()->is_valid());
2560 if (Expand(current_page)) {
2561 return AllocateInNextPage(current_page, size_in_bytes);
2562 }
2563
2564 // Finally, fail.
2565 return NULL;
2566}
2567
2568
2569// Move to the next page (there is assumed to be one) and allocate there.
2570// The top of page block is always wasted, because it is too small to hold a
2571// map.
2572HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2573 int size_in_bytes) {
2574 ASSERT(current_page->next_page()->is_valid());
2575 ASSERT(allocation_info_.top == PageAllocationLimit(current_page));
2576 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2577 Page* next_page = current_page->next_page();
2578 next_page->ClearGCFields();
2579 current_page->SetAllocationWatermark(allocation_info_.top);
2580 accounting_stats_.WasteBytes(page_extra_);
2581 SetAllocationInfo(&allocation_info_, next_page);
2582 return AllocateLinearly(&allocation_info_, size_in_bytes);
2583}
2584
2585
2586void FixedSpace::DeallocateBlock(Address start,
2587 int size_in_bytes,
2588 bool add_to_freelist) {
2589 // Free-list elements in fixed space are assumed to have a fixed size.
2590 // We break the free block into chunks and add them to the free list
2591 // individually.
2592 int size = object_size_in_bytes();
2593 ASSERT(size_in_bytes % size == 0);
2594 Address end = start + size_in_bytes;
2595 for (Address a = start; a < end; a += size) {
2596 Free(a, add_to_freelist);
2597 }
2598}
2599
2600
2601#ifdef DEBUG
2602void FixedSpace::ReportStatistics() {
2603 int pct = static_cast<int>(Available() * 100 / Capacity());
2604 PrintF(" capacity: %" V8_PTR_PREFIX "d"
2605 ", waste: %" V8_PTR_PREFIX "d"
2606 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
2607 Capacity(), Waste(), Available(), pct);
2608
2609 ClearHistograms();
2610 HeapObjectIterator obj_it(this);
2611 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2612 CollectHistogramInfo(obj);
2613 ReportHistogram(false);
2614}
2615#endif
2616
2617
Steve Blocka7e24c12009-10-30 11:49:00 +00002618// -----------------------------------------------------------------------------
2619// MapSpace implementation
2620
Ben Murdoch85b71792012-04-11 18:30:58 +01002621void MapSpace::PrepareForMarkCompact(bool will_compact) {
2622 // Call prepare of the super class.
2623 FixedSpace::PrepareForMarkCompact(will_compact);
2624
2625 if (will_compact) {
2626 // Initialize map index entry.
2627 int page_count = 0;
2628 PageIterator it(this, PageIterator::ALL_PAGES);
2629 while (it.has_next()) {
2630 ASSERT_MAP_PAGE_INDEX(page_count);
2631
2632 Page* p = it.next();
2633 ASSERT(p->mc_page_index == page_count);
2634
2635 page_addresses_[page_count++] = p->address();
2636 }
2637 }
2638}
2639
2640
Steve Blocka7e24c12009-10-30 11:49:00 +00002641#ifdef DEBUG
2642void MapSpace::VerifyObject(HeapObject* object) {
2643 // The object should be a map or a free-list node.
Ben Murdoch85b71792012-04-11 18:30:58 +01002644 ASSERT(object->IsMap() || object->IsByteArray());
Steve Blocka7e24c12009-10-30 11:49:00 +00002645}
2646#endif
2647
2648
2649// -----------------------------------------------------------------------------
2650// GlobalPropertyCellSpace implementation
2651
2652#ifdef DEBUG
2653void CellSpace::VerifyObject(HeapObject* object) {
2654 // The object should be a global object property cell or a free-list node.
2655 ASSERT(object->IsJSGlobalPropertyCell() ||
Steve Block44f0eee2011-05-26 01:26:41 +01002656 object->map() == heap()->two_pointer_filler_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00002657}
2658#endif
2659
2660
2661// -----------------------------------------------------------------------------
2662// LargeObjectIterator
2663
2664LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002665 current_ = space->first_chunk_;
Steve Blocka7e24c12009-10-30 11:49:00 +00002666 size_func_ = NULL;
2667}
2668
2669
2670LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2671 HeapObjectCallback size_func) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002672 current_ = space->first_chunk_;
Steve Blocka7e24c12009-10-30 11:49:00 +00002673 size_func_ = size_func;
2674}
2675
2676
Ben Murdoch85b71792012-04-11 18:30:58 +01002677HeapObject* LargeObjectIterator::next() {
Leon Clarked91b9f72010-01-27 17:25:45 +00002678 if (current_ == NULL) return NULL;
2679
Steve Blocka7e24c12009-10-30 11:49:00 +00002680 HeapObject* object = current_->GetObject();
Ben Murdoch85b71792012-04-11 18:30:58 +01002681 current_ = current_->next();
Steve Blocka7e24c12009-10-30 11:49:00 +00002682 return object;
2683}
2684
2685
2686// -----------------------------------------------------------------------------
Ben Murdoch85b71792012-04-11 18:30:58 +01002687// LargeObjectChunk
2688
2689LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
2690 Executability executable) {
2691 size_t requested = ChunkSizeFor(size_in_bytes);
2692 size_t size;
2693 size_t guard_size = (executable == EXECUTABLE) ? Page::kPageSize : 0;
2694 Isolate* isolate = Isolate::Current();
2695 void* mem = isolate->memory_allocator()->AllocateRawMemory(
2696 requested + guard_size, &size, executable);
2697 if (mem == NULL) return NULL;
2698
2699 // The start of the chunk may be overlayed with a page so we have to
2700 // make sure that the page flags fit in the size field.
2701 ASSERT((size & Page::kPageFlagMask) == 0);
2702
2703 LOG(isolate, NewEvent("LargeObjectChunk", mem, size));
2704 if (size < requested + guard_size) {
2705 isolate->memory_allocator()->FreeRawMemory(
2706 mem, size, executable);
2707 LOG(isolate, DeleteEvent("LargeObjectChunk", mem));
2708 return NULL;
2709 }
2710
2711 if (guard_size != 0) {
2712 OS::Guard(mem, guard_size);
2713 size -= guard_size;
2714 mem = static_cast<Address>(mem) + guard_size;
2715 }
2716
2717 ObjectSpace space = (executable == EXECUTABLE)
2718 ? kObjectSpaceCodeSpace
2719 : kObjectSpaceLoSpace;
2720 isolate->memory_allocator()->PerformAllocationCallback(
2721 space, kAllocationActionAllocate, size);
2722
2723 LargeObjectChunk* chunk = reinterpret_cast<LargeObjectChunk*>(mem);
2724 chunk->size_ = size;
2725 chunk->GetPage()->heap_ = isolate->heap();
2726 return chunk;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002727}
2728
Steve Blocka7e24c12009-10-30 11:49:00 +00002729
Ben Murdoch85b71792012-04-11 18:30:58 +01002730void LargeObjectChunk::Free(Executability executable) {
2731 size_t guard_size = (executable == EXECUTABLE) ? Page::kPageSize : 0;
2732 ObjectSpace space =
2733 (executable == EXECUTABLE) ? kObjectSpaceCodeSpace : kObjectSpaceLoSpace;
2734 // Do not access instance fields after FreeRawMemory!
2735 Address my_address = address();
2736 size_t my_size = size();
2737 Isolate* isolate = GetPage()->heap_->isolate();
2738 MemoryAllocator* a = isolate->memory_allocator();
2739 a->FreeRawMemory(my_address - guard_size, my_size + guard_size, executable);
2740 a->PerformAllocationCallback(space, kAllocationActionFree, my_size);
2741 LOG(isolate, DeleteEvent("LargeObjectChunk", my_address));
2742}
2743
2744
2745int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
2746 int os_alignment = static_cast<int>(OS::AllocateAlignment());
2747 if (os_alignment < Page::kPageSize) {
2748 size_in_bytes += (Page::kPageSize - os_alignment);
2749 }
2750 return size_in_bytes + Page::kObjectStartOffset;
2751}
2752
2753// -----------------------------------------------------------------------------
2754// LargeObjectSpace
2755
2756LargeObjectSpace::LargeObjectSpace(Heap* heap, AllocationSpace id)
Steve Block44f0eee2011-05-26 01:26:41 +01002757 : Space(heap, id, NOT_EXECUTABLE), // Managed on a per-allocation basis
Ben Murdoch85b71792012-04-11 18:30:58 +01002758 first_chunk_(NULL),
Steve Blocka7e24c12009-10-30 11:49:00 +00002759 size_(0),
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002760 page_count_(0),
Ben Murdoch85b71792012-04-11 18:30:58 +01002761 objects_size_(0) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002762
2763
Ben Murdoch85b71792012-04-11 18:30:58 +01002764bool LargeObjectSpace::Setup() {
2765 first_chunk_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00002766 size_ = 0;
2767 page_count_ = 0;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002768 objects_size_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002769 return true;
2770}
2771
2772
2773void LargeObjectSpace::TearDown() {
Ben Murdoch85b71792012-04-11 18:30:58 +01002774 while (first_chunk_ != NULL) {
2775 LargeObjectChunk* chunk = first_chunk_;
2776 first_chunk_ = first_chunk_->next();
2777 chunk->Free(chunk->GetPage()->PageExecutability());
Steve Blocka7e24c12009-10-30 11:49:00 +00002778 }
Ben Murdoch85b71792012-04-11 18:30:58 +01002779 Setup();
Steve Blocka7e24c12009-10-30 11:49:00 +00002780}
2781
2782
Ben Murdoch85b71792012-04-11 18:30:58 +01002783MaybeObject* LargeObjectSpace::AllocateRawInternal(int requested_size,
2784 int object_size,
2785 Executability executable) {
2786 ASSERT(0 < object_size && object_size <= requested_size);
2787
Steve Blocka7e24c12009-10-30 11:49:00 +00002788 // Check if we want to force a GC before growing the old space further.
2789 // If so, fail the allocation.
Steve Block44f0eee2011-05-26 01:26:41 +01002790 if (!heap()->always_allocate() &&
2791 heap()->OldGenerationAllocationLimitReached()) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002792 return Failure::RetryAfterGC(identity());
Steve Blocka7e24c12009-10-30 11:49:00 +00002793 }
2794
Ben Murdoch85b71792012-04-11 18:30:58 +01002795 LargeObjectChunk* chunk = LargeObjectChunk::New(requested_size, executable);
2796 if (chunk == NULL) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002797 return Failure::RetryAfterGC(identity());
Steve Blocka7e24c12009-10-30 11:49:00 +00002798 }
2799
Ben Murdoch85b71792012-04-11 18:30:58 +01002800 size_ += static_cast<int>(chunk->size());
2801 objects_size_ += requested_size;
Steve Blocka7e24c12009-10-30 11:49:00 +00002802 page_count_++;
Ben Murdoch85b71792012-04-11 18:30:58 +01002803 chunk->set_next(first_chunk_);
2804 first_chunk_ = chunk;
Steve Blocka7e24c12009-10-30 11:49:00 +00002805
Ben Murdoch85b71792012-04-11 18:30:58 +01002806 // Initialize page header.
2807 Page* page = chunk->GetPage();
2808 Address object_address = page->ObjectAreaStart();
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002809
Ben Murdoch85b71792012-04-11 18:30:58 +01002810 // Clear the low order bit of the second word in the page to flag it as a
2811 // large object page. If the chunk_size happened to be written there, its
2812 // low order bit should already be clear.
2813 page->SetIsLargeObjectPage(true);
2814 page->SetPageExecutability(executable);
2815 page->SetRegionMarks(Page::kAllRegionsCleanMarks);
2816 return HeapObject::FromAddress(object_address);
2817}
Ben Murdochb0fe1622011-05-05 13:52:32 +01002818
Steve Blocka7e24c12009-10-30 11:49:00 +00002819
Ben Murdoch85b71792012-04-11 18:30:58 +01002820MaybeObject* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
2821 ASSERT(0 < size_in_bytes);
2822 return AllocateRawInternal(size_in_bytes,
2823 size_in_bytes,
2824 EXECUTABLE);
2825}
2826
2827
2828MaybeObject* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
2829 ASSERT(0 < size_in_bytes);
2830 return AllocateRawInternal(size_in_bytes,
2831 size_in_bytes,
2832 NOT_EXECUTABLE);
2833}
2834
2835
2836MaybeObject* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2837 ASSERT(0 < size_in_bytes);
2838 return AllocateRawInternal(size_in_bytes,
2839 size_in_bytes,
2840 NOT_EXECUTABLE);
Steve Blocka7e24c12009-10-30 11:49:00 +00002841}
2842
2843
2844// GC support
John Reck59135872010-11-02 12:39:01 -07002845MaybeObject* LargeObjectSpace::FindObject(Address a) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002846 for (LargeObjectChunk* chunk = first_chunk_;
2847 chunk != NULL;
2848 chunk = chunk->next()) {
2849 Address chunk_address = chunk->address();
2850 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2851 return chunk->GetObject();
2852 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002853 }
2854 return Failure::Exception();
2855}
2856
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002857
Ben Murdoch85b71792012-04-11 18:30:58 +01002858LargeObjectChunk* LargeObjectSpace::FindChunkContainingPc(Address pc) {
2859 // TODO(853): Change this implementation to only find executable
2860 // chunks and use some kind of hash-based approach to speed it up.
2861 for (LargeObjectChunk* chunk = first_chunk_;
2862 chunk != NULL;
2863 chunk = chunk->next()) {
2864 Address chunk_address = chunk->address();
2865 if (chunk_address <= pc && pc < chunk_address + chunk->size()) {
2866 return chunk;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002867 }
2868 }
2869 return NULL;
2870}
2871
2872
Ben Murdoch85b71792012-04-11 18:30:58 +01002873void LargeObjectSpace::IterateDirtyRegions(ObjectSlotCallback copy_object) {
2874 LargeObjectIterator it(this);
2875 for (HeapObject* object = it.next(); object != NULL; object = it.next()) {
2876 // We only have code, sequential strings, or fixed arrays in large
2877 // object space, and only fixed arrays can possibly contain pointers to
2878 // the young generation.
2879 if (object->IsFixedArray()) {
2880 Page* page = Page::FromAddress(object->address());
2881 uint32_t marks = page->GetRegionMarks();
2882 uint32_t newmarks = Page::kAllRegionsCleanMarks;
2883
2884 if (marks != Page::kAllRegionsCleanMarks) {
2885 // For a large page a single dirty mark corresponds to several
2886 // regions (modulo 32). So we treat a large page as a sequence of
2887 // normal pages of size Page::kPageSize having same dirty marks
2888 // and subsequently iterate dirty regions on each of these pages.
2889 Address start = object->address();
2890 Address end = page->ObjectAreaEnd();
2891 Address object_end = start + object->Size();
2892
2893 // Iterate regions of the first normal page covering object.
2894 uint32_t first_region_number = page->GetRegionNumberForAddress(start);
2895 newmarks |=
2896 heap()->IterateDirtyRegions(marks >> first_region_number,
2897 start,
2898 end,
2899 &Heap::IteratePointersInDirtyRegion,
2900 copy_object) << first_region_number;
2901
2902 start = end;
2903 end = start + Page::kPageSize;
2904 while (end <= object_end) {
2905 // Iterate next 32 regions.
2906 newmarks |=
2907 heap()->IterateDirtyRegions(marks,
2908 start,
2909 end,
2910 &Heap::IteratePointersInDirtyRegion,
2911 copy_object);
2912 start = end;
2913 end = start + Page::kPageSize;
2914 }
2915
2916 if (start != object_end) {
2917 // Iterate the last piece of an object which is less than
2918 // Page::kPageSize.
2919 newmarks |=
2920 heap()->IterateDirtyRegions(marks,
2921 start,
2922 object_end,
2923 &Heap::IteratePointersInDirtyRegion,
2924 copy_object);
2925 }
2926
2927 page->SetRegionMarks(newmarks);
2928 }
2929 }
2930 }
2931}
2932
2933
Steve Blocka7e24c12009-10-30 11:49:00 +00002934void LargeObjectSpace::FreeUnmarkedObjects() {
Ben Murdoch85b71792012-04-11 18:30:58 +01002935 LargeObjectChunk* previous = NULL;
2936 LargeObjectChunk* current = first_chunk_;
Steve Blocka7e24c12009-10-30 11:49:00 +00002937 while (current != NULL) {
2938 HeapObject* object = current->GetObject();
Ben Murdoch85b71792012-04-11 18:30:58 +01002939 if (object->IsMarked()) {
2940 object->ClearMark();
2941 heap()->mark_compact_collector()->tracer()->decrement_marked_count();
Steve Blocka7e24c12009-10-30 11:49:00 +00002942 previous = current;
Ben Murdoch85b71792012-04-11 18:30:58 +01002943 current = current->next();
Steve Blocka7e24c12009-10-30 11:49:00 +00002944 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00002945 // Cut the chunk out from the chunk list.
Ben Murdoch85b71792012-04-11 18:30:58 +01002946 LargeObjectChunk* current_chunk = current;
2947 current = current->next();
Steve Blocka7e24c12009-10-30 11:49:00 +00002948 if (previous == NULL) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002949 first_chunk_ = current;
Steve Blocka7e24c12009-10-30 11:49:00 +00002950 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +01002951 previous->set_next(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00002952 }
2953
2954 // Free the chunk.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002955 heap()->mark_compact_collector()->ReportDeleteIfNeeded(
2956 object, heap()->isolate());
Ben Murdoch85b71792012-04-11 18:30:58 +01002957 LiveObjectList::ProcessNonLive(object);
2958
2959 size_ -= static_cast<int>(current_chunk->size());
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002960 objects_size_ -= object->Size();
Steve Blocka7e24c12009-10-30 11:49:00 +00002961 page_count_--;
Ben Murdoch85b71792012-04-11 18:30:58 +01002962 current_chunk->Free(current_chunk->GetPage()->PageExecutability());
Steve Blocka7e24c12009-10-30 11:49:00 +00002963 }
2964 }
2965}
2966
2967
2968bool LargeObjectSpace::Contains(HeapObject* object) {
2969 Address address = object->address();
Ben Murdoch85b71792012-04-11 18:30:58 +01002970 if (heap()->new_space()->Contains(address)) {
2971 return false;
2972 }
2973 Page* page = Page::FromAddress(address);
Steve Blocka7e24c12009-10-30 11:49:00 +00002974
Ben Murdoch85b71792012-04-11 18:30:58 +01002975 SLOW_ASSERT(!page->IsLargeObjectPage()
2976 || !FindObject(address)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +00002977
Ben Murdoch85b71792012-04-11 18:30:58 +01002978 return page->IsLargeObjectPage();
Steve Blocka7e24c12009-10-30 11:49:00 +00002979}
2980
2981
2982#ifdef DEBUG
2983// We do not assume that the large object iterator works, because it depends
2984// on the invariants we are checking during verification.
2985void LargeObjectSpace::Verify() {
Ben Murdoch85b71792012-04-11 18:30:58 +01002986 for (LargeObjectChunk* chunk = first_chunk_;
Steve Blocka7e24c12009-10-30 11:49:00 +00002987 chunk != NULL;
Ben Murdoch85b71792012-04-11 18:30:58 +01002988 chunk = chunk->next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002989 // Each chunk contains an object that starts at the large object page's
2990 // object area start.
2991 HeapObject* object = chunk->GetObject();
2992 Page* page = Page::FromAddress(object->address());
Ben Murdoch85b71792012-04-11 18:30:58 +01002993 ASSERT(object->address() == page->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +00002994
2995 // The first word should be a map, and we expect all map pointers to be
2996 // in map space.
2997 Map* map = object->map();
2998 ASSERT(map->IsMap());
Steve Block44f0eee2011-05-26 01:26:41 +01002999 ASSERT(heap()->map_space()->Contains(map));
Steve Blocka7e24c12009-10-30 11:49:00 +00003000
3001 // We have only code, sequential strings, external strings
3002 // (sequential strings that have been morphed into external
3003 // strings), fixed arrays, and byte arrays in large object space.
3004 ASSERT(object->IsCode() || object->IsSeqString() ||
3005 object->IsExternalString() || object->IsFixedArray() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003006 object->IsFixedDoubleArray() || object->IsByteArray());
Steve Blocka7e24c12009-10-30 11:49:00 +00003007
3008 // The object itself should look OK.
3009 object->Verify();
3010
3011 // Byte arrays and strings don't have interior pointers.
3012 if (object->IsCode()) {
3013 VerifyPointersVisitor code_visitor;
3014 object->IterateBody(map->instance_type(),
3015 object->Size(),
3016 &code_visitor);
3017 } else if (object->IsFixedArray()) {
Ben Murdoch85b71792012-04-11 18:30:58 +01003018 // We loop over fixed arrays ourselves, rather then using the visitor,
3019 // because the visitor doesn't support the start/offset iteration
3020 // needed for IsRegionDirty.
Steve Blocka7e24c12009-10-30 11:49:00 +00003021 FixedArray* array = FixedArray::cast(object);
3022 for (int j = 0; j < array->length(); j++) {
3023 Object* element = array->get(j);
3024 if (element->IsHeapObject()) {
3025 HeapObject* element_object = HeapObject::cast(element);
Steve Block44f0eee2011-05-26 01:26:41 +01003026 ASSERT(heap()->Contains(element_object));
Steve Blocka7e24c12009-10-30 11:49:00 +00003027 ASSERT(element_object->map()->IsMap());
Ben Murdoch85b71792012-04-11 18:30:58 +01003028 if (heap()->InNewSpace(element_object)) {
3029 Address array_addr = object->address();
3030 Address element_addr = array_addr + FixedArray::kHeaderSize +
3031 j * kPointerSize;
3032
3033 ASSERT(Page::FromAddress(array_addr)->IsRegionDirty(element_addr));
3034 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003035 }
3036 }
3037 }
3038 }
3039}
3040
3041
3042void LargeObjectSpace::Print() {
3043 LargeObjectIterator it(this);
Ben Murdoch85b71792012-04-11 18:30:58 +01003044 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
Leon Clarked91b9f72010-01-27 17:25:45 +00003045 obj->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +00003046 }
3047}
3048
3049
3050void LargeObjectSpace::ReportStatistics() {
Ben Murdochf87a2032010-10-22 12:50:53 +01003051 PrintF(" size: %" V8_PTR_PREFIX "d\n", size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003052 int num_objects = 0;
3053 ClearHistograms();
3054 LargeObjectIterator it(this);
Ben Murdoch85b71792012-04-11 18:30:58 +01003055 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003056 num_objects++;
Leon Clarked91b9f72010-01-27 17:25:45 +00003057 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003058 }
3059
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003060 PrintF(" number of objects %d, "
3061 "size of objects %" V8_PTR_PREFIX "d\n", num_objects, objects_size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003062 if (num_objects > 0) ReportHistogram(false);
3063}
3064
3065
3066void LargeObjectSpace::CollectCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01003067 Isolate* isolate = heap()->isolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00003068 LargeObjectIterator obj_it(this);
Ben Murdoch85b71792012-04-11 18:30:58 +01003069 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003070 if (obj->IsCode()) {
3071 Code* code = Code::cast(obj);
Steve Block44f0eee2011-05-26 01:26:41 +01003072 isolate->code_kind_statistics()[code->kind()] += code->Size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003073 }
3074 }
3075}
Steve Blocka7e24c12009-10-30 11:49:00 +00003076#endif // DEBUG
3077
3078} } // namespace v8::internal