blob: 0f80496b0af62738a04603dfea3a51f621d4e3a4 [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
38// 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())
44
Steve Blocka7e24c12009-10-30 11:49:00 +000045// ----------------------------------------------------------------------------
46// HeapObjectIterator
47
48HeapObjectIterator::HeapObjectIterator(PagedSpace* space) {
49 Initialize(space->bottom(), space->top(), NULL);
50}
51
52
53HeapObjectIterator::HeapObjectIterator(PagedSpace* space,
54 HeapObjectCallback size_func) {
55 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);
67}
68
69
Kristian Monsen80d68ea2010-09-08 11:05:35 +010070HeapObjectIterator::HeapObjectIterator(Page* page,
71 HeapObjectCallback size_func) {
72 Initialize(page->ObjectAreaStart(), page->AllocationTop(), size_func);
73}
74
75
Steve Blocka7e24c12009-10-30 11:49:00 +000076void HeapObjectIterator::Initialize(Address cur, Address end,
77 HeapObjectCallback size_f) {
78 cur_addr_ = cur;
79 end_addr_ = end;
80 end_page_ = Page::FromAllocationTop(end);
81 size_func_ = size_f;
82 Page* p = Page::FromAllocationTop(cur_addr_);
83 cur_limit_ = (p == end_page_) ? end_addr_ : p->AllocationTop();
84
85#ifdef DEBUG
86 Verify();
87#endif
88}
89
90
Leon Clarked91b9f72010-01-27 17:25:45 +000091HeapObject* HeapObjectIterator::FromNextPage() {
92 if (cur_addr_ == end_addr_) return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000093
94 Page* cur_page = Page::FromAllocationTop(cur_addr_);
95 cur_page = cur_page->next_page();
96 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
Leon Clarked91b9f72010-01-27 17:25:45 +0000101 if (cur_addr_ == end_addr_) return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 ASSERT(cur_addr_ < cur_limit_);
103#ifdef DEBUG
104 Verify();
105#endif
Leon Clarked91b9f72010-01-27 17:25:45 +0000106 return FromCurrentPage();
Steve Blocka7e24c12009-10-30 11:49:00 +0000107}
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 }
144}
145
146
147// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000148// CodeRange
149
Steve Block44f0eee2011-05-26 01:26:41 +0100150
Ben Murdoch257744e2011-11-30 15:57:28 +0000151CodeRange::CodeRange()
152 : code_range_(NULL),
Steve Block44f0eee2011-05-26 01:26:41 +0100153 free_list_(0),
154 allocation_list_(0),
Ben Murdoch257744e2011-11-30 15:57:28 +0000155 current_allocation_block_index_(0),
156 isolate_(NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100157}
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
159
160bool CodeRange::Setup(const size_t requested) {
161 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));
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 allocation_list_.Add(FreeBlock(code_range_->address(), code_range_->size()));
175 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
231void* CodeRange::AllocateRawMemory(const size_t requested, size_t* allocated) {
232 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.
239 *allocated = RoundUp(requested, Page::kPageSize);
240 FreeBlock current = allocation_list_[current_allocation_block_index_];
241 if (*allocated >= current.size - Page::kPageSize) {
242 // Don't leave a small free block, useless for a large object or chunk.
243 *allocated = current.size;
244 }
245 ASSERT(*allocated <= current.size);
246 if (!code_range_->Commit(current.start, *allocated, true)) {
247 *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
259void CodeRange::FreeRawMemory(void* address, size_t length) {
260 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
277// 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;
Steve Block44f0eee2011-05-26 01:26:41 +0100280
281
Ben Murdoch257744e2011-11-30 15:57:28 +0000282MemoryAllocator::MemoryAllocator()
283 : capacity_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100284 capacity_executable_(0),
285 size_(0),
286 size_executable_(0),
287 initial_chunk_(NULL),
288 chunks_(kEstimatedNumberOfChunks),
289 free_chunk_ids_(kEstimatedNumberOfChunks),
290 max_nof_chunks_(0),
Ben Murdoch257744e2011-11-30 15:57:28 +0000291 top_(0),
292 isolate_(NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100293}
Steve Blocka7e24c12009-10-30 11:49:00 +0000294
295
296void 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
Russell Brenner90bac252010-11-18 13:33:46 -0800309bool 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
314 // 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.
Ben Murdochf87a2032010-10-22 12:50:53 +0100323 max_nof_chunks_ =
324 static_cast<int>((capacity_ / (kChunkSize - Page::kPageSize))) + 5;
Steve Blocka7e24c12009-10-30 11:49:00 +0000325 if (max_nof_chunks_ > kMaxNofChunks) return false;
326
327 size_ = 0;
Steve Block791712a2010-08-27 10:21:07 +0100328 size_executable_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 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_;
335 return true;
336}
337
338
339void MemoryAllocator::TearDown() {
340 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) {
Steve Block44f0eee2011-05-26 01:26:41 +0100347 LOG(isolate_, DeleteEvent("InitialChunk", initial_chunk_->address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 delete initial_chunk_;
349 initial_chunk_ = NULL;
350 }
351
352 ASSERT(top_ == max_nof_chunks_); // all chunks are free
353 top_ = 0;
354 capacity_ = 0;
Russell Brenner90bac252010-11-18 13:33:46 -0800355 capacity_executable_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 size_ = 0;
357 max_nof_chunks_ = 0;
358}
359
360
361void* MemoryAllocator::AllocateRawMemory(const size_t requested,
362 size_t* allocated,
363 Executability executable) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100364 if (size_ + static_cast<size_t>(requested) > static_cast<size_t>(capacity_)) {
365 return NULL;
366 }
Russell Brenner90bac252010-11-18 13:33:46 -0800367
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 void* mem;
Russell Brenner90bac252010-11-18 13:33:46 -0800369 if (executable == EXECUTABLE) {
370 // Check executable memory limit.
371 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()) {
381 mem = isolate_->code_range()->AllocateRawMemory(requested, allocated);
Russell Brenner90bac252010-11-18 13:33:46 -0800382 } else {
383 mem = OS::Allocate(requested, allocated, true);
384 }
385 // Update executable memory size.
386 size_executable_ += static_cast<int>(*allocated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 } else {
Russell Brenner90bac252010-11-18 13:33:46 -0800388 mem = OS::Allocate(requested, allocated, false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 }
Steve Blockd0582a62009-12-15 09:54:21 +0000390 int alloced = static_cast<int>(*allocated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 size_ += alloced;
Steve Block791712a2010-08-27 10:21:07 +0100392
Leon Clarke4515c472010-02-03 11:58:03 +0000393#ifdef DEBUG
394 ZapBlock(reinterpret_cast<Address>(mem), alloced);
395#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100396 isolate_->counters()->memory_allocated()->Increment(alloced);
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 return mem;
398}
399
400
Steve Block791712a2010-08-27 10:21:07 +0100401void MemoryAllocator::FreeRawMemory(void* mem,
402 size_t length,
403 Executability executable) {
Leon Clarke4515c472010-02-03 11:58:03 +0000404#ifdef DEBUG
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000405 // 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);
Leon Clarke4515c472010-02-03 11:58:03 +0000408#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100409 if (isolate_->code_range()->contains(static_cast<Address>(mem))) {
410 isolate_->code_range()->FreeRawMemory(mem, length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 } else {
412 OS::Free(mem, length);
413 }
Steve Block44f0eee2011-05-26 01:26:41 +0100414 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(length));
Steve Blockd0582a62009-12-15 09:54:21 +0000415 size_ -= static_cast<int>(length);
Steve Block791712a2010-08-27 10:21:07 +0100416 if (executable == EXECUTABLE) size_executable_ -= static_cast<int>(length);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100417
Steve Blocka7e24c12009-10-30 11:49:00 +0000418 ASSERT(size_ >= 0);
Russell Brenner90bac252010-11-18 13:33:46 -0800419 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000468void* 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);
Steve Block44f0eee2011-05-26 01:26:41 +0100481 LOG(isolate_,
482 NewEvent("InitialChunk", initial_chunk_->address(), requested));
Steve Blockd0582a62009-12-15 09:54:21 +0000483 size_ += static_cast<int>(requested);
Steve Blocka7e24c12009-10-30 11:49:00 +0000484 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.
Steve Blockd0582a62009-12-15 09:54:21 +0000493 return static_cast<int>((RoundDown(start + size, Page::kPageSize)
Leon Clarkee46be812010-01-19 14:06:41 +0000494 - RoundUp(start, Page::kPageSize)) >> kPageSizeBits);
Steve Blocka7e24c12009-10-30 11:49:00 +0000495}
496
497
Ben Murdochb0fe1622011-05-05 13:52:32 +0100498Page* MemoryAllocator::AllocatePages(int requested_pages,
499 int* allocated_pages,
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 PagedSpace* owner) {
501 if (requested_pages <= 0) return Page::FromAddress(NULL);
502 size_t chunk_size = requested_pages * Page::kPageSize;
503
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
505 if (chunk == NULL) return Page::FromAddress(NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100506 LOG(isolate_, NewEvent("PagedChunk", chunk, chunk_size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000507
508 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000509
Ben Murdochb0fe1622011-05-05 13:52:32 +0100510 // We may 'lose' a page due to alignment.
511 ASSERT(*allocated_pages >= kPagesPerChunk - 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000512
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());
Steve Block44f0eee2011-05-26 01:26:41 +0100520 LOG(isolate_, DeleteEvent("PagedChunk", chunk));
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 return Page::FromAddress(NULL);
522 }
523
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000524 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 int chunk_id = Pop();
532 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
533
Iain Merrick9ac36c92010-09-13 15:29:50 +0100534 ObjectSpace space = static_cast<ObjectSpace>(1 << owner->identity());
535 PerformAllocationCallback(space, kAllocationActionAllocate, chunk_size);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100536 Page* new_pages = InitializePagesInChunk(chunk_id, *allocated_pages, owner);
537
Ben Murdochb0fe1622011-05-05 13:52:32 +0100538 return new_pages;
Steve Blocka7e24c12009-10-30 11:49:00 +0000539}
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 }
Leon Clarke4515c472010-02-03 11:58:03 +0000553#ifdef DEBUG
554 ZapBlock(start, size);
555#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100556 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000557
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;
Leon Clarke4515c472010-02-03 11:58:03 +0000577#ifdef DEBUG
578 ZapBlock(start, size);
579#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100580 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 return true;
582}
583
Leon Clarke4515c472010-02-03 11:58:03 +0000584
Steve Blocka7e24c12009-10-30 11:49:00 +0000585bool 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;
Steve Block44f0eee2011-05-26 01:26:41 +0100593 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 return true;
595}
596
Leon Clarke4515c472010-02-03 11:58:03 +0000597
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
Steve Blocka7e24c12009-10-30 11:49:00 +0000605Page* 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);
Steve Block44f0eee2011-05-26 01:26:41 +0100624 p->heap_ = owner->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100626 p->InvalidateWatermark(true);
Steve Block6ded16b2010-05-10 14:33:55 +0100627 p->SetIsLargeObjectPage(false);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100628 p->SetAllocationWatermark(p->ObjectAreaStart());
629 p->SetCachedAllocationWatermark(p->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 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
Steve Block6ded16b2010-05-10 14:33:55 +0100673void 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000682void 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());
Steve Block44f0eee2011-05-26 01:26:41 +0100694 Counters* counters = isolate_->counters();
695 counters->memory_allocated()->Decrement(static_cast<int>(c.size()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000696 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100697 LOG(isolate_, DeleteEvent("PagedChunk", c.address()));
698 ObjectSpace space = static_cast<ObjectSpace>(1 << c.owner_identity());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100699 size_t size = c.size();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000700 size_t guard_size = (c.executable() == EXECUTABLE) ? Page::kPageSize : 0;
701 FreeRawMemory(c.address() - guard_size, size + guard_size, c.executable());
Iain Merrick9ac36c92010-09-13 15:29:50 +0100702 PerformAllocationCallback(space, kAllocationActionFree, size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000703 }
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
731
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
Steve Block6ded16b2010-05-10 14:33:55 +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;
749
750 for (int i = 0, length = chunks_.length(); i < length; i++) {
751 ChunkInfo& chunk = chunks_[i];
752
753 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 }
764 }
765
766 if (first_page != NULL) {
767 *first_page = first;
768 }
769
770 if (last_page != NULL) {
771 *last_page = last;
772 }
773}
774
775
776Page* 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);
783
784 if (prev->is_valid()) {
785 SetNextPage(prev, Page::FromAddress(page_addr));
786 }
787
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
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100793 p->InvalidateWatermark(true);
Steve Block6ded16b2010-05-10 14:33:55 +0100794 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;
808}
809
810
Steve Blocka7e24c12009-10-30 11:49:00 +0000811// -----------------------------------------------------------------------------
812// 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)
Steve Block44f0eee2011-05-26 01:26:41 +0100818 : Space(heap, id, executable) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
820 * Page::kObjectAreaSize;
821 accounting_stats_.Clear();
822
823 allocation_info_.top = NULL;
824 allocation_info_.limit = NULL;
825
826 mc_forwarding_info_.top = NULL;
827 mc_forwarding_info_.limit = NULL;
828}
829
830
831bool 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) {
Steve Block44f0eee2011-05-26 01:26:41 +0100839 first_page_ = Isolate::Current()->memory_allocator()->CommitPages(
840 RoundUp(start, Page::kPageSize),
841 Page::kPageSize * pages_in_chunk,
842 this, &num_pages);
Steve Blocka7e24c12009-10-30 11:49:00 +0000843 } else {
Ben Murdochf87a2032010-10-22 12:50:53 +0100844 int requested_pages =
845 Min(MemoryAllocator::kPagesPerChunk,
846 static_cast<int>(max_capacity_ / Page::kObjectAreaSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000847 first_page_ =
Steve Block44f0eee2011-05-26 01:26:41 +0100848 Isolate::Current()->memory_allocator()->AllocatePages(
849 requested_pages, &num_pages, this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000850 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
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100860 // Sequentially clear region marks in the newly allocated
Steve Blocka7e24c12009-10-30 11:49:00 +0000861 // pages and cache the current last page in the space.
862 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100863 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000864 last_page_ = p;
865 }
866
867 // Use first_page_ for allocation.
868 SetAllocationInfo(&allocation_info_, first_page_);
869
Steve Block6ded16b2010-05-10 14:33:55 +0100870 page_list_is_chunk_ordered_ = true;
871
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 return true;
873}
874
875
876bool PagedSpace::HasBeenSetup() {
877 return (Capacity() > 0);
878}
879
880
881void PagedSpace::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100882 Isolate::Current()->memory_allocator()->FreeAllPages(this);
Steve Block6ded16b2010-05-10 14:33:55 +0100883 first_page_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000884 accounting_stats_.Clear();
885}
886
887
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100888void PagedSpace::MarkAllPagesClean() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000889 PageIterator it(this, PageIterator::ALL_PAGES);
890 while (it.has_next()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100891 it.next()->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 }
893}
894
895
John Reck59135872010-11-02 12:39:01 -0700896MaybeObject* PagedSpace::FindObject(Address addr) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000897 // 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);
904 ASSERT(IsUsed(p));
905 Address cur = p->ObjectAreaStart();
906 Address end = p->AllocationTop();
907 while (cur < end) {
908 HeapObject* obj = HeapObject::FromAddress(cur);
909 Address next = cur + obj->Size();
910 if ((cur <= addr) && (addr < next)) return obj;
911 cur = next;
912 }
913
914 UNREACHABLE();
915 return Failure::Exception();
916}
917
918
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.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100994 current_page->SetAllocationWatermark(mc_forwarding_info_.top);
995 current_page->next_page()->InvalidateWatermark(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000996 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);
1004
1005 if (Capacity() == max_capacity_) return false;
1006
1007 ASSERT(Capacity() < max_capacity_);
1008 // Last page must be valid and its next page is invalid.
1009 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
1010
Ben Murdochf87a2032010-10-22 12:50:53 +01001011 int available_pages =
1012 static_cast<int>((max_capacity_ - Capacity()) / Page::kObjectAreaSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001013 // 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
1018 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
Steve Block44f0eee2011-05-26 01:26:41 +01001019 Page* p = heap()->isolate()->memory_allocator()->AllocatePages(
1020 desired_pages, &desired_pages, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001021 if (!p->is_valid()) return false;
1022
1023 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
1024 ASSERT(Capacity() <= max_capacity_);
1025
Steve Block44f0eee2011-05-26 01:26:41 +01001026 heap()->isolate()->memory_allocator()->SetNextPage(last_page, p);
Steve Blocka7e24c12009-10-30 11:49:00 +00001027
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001028 // Sequentially clear region marks of new pages and and cache the
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 // new last page in the space.
1030 while (p->is_valid()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001031 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00001032 last_page_ = p;
1033 p = p->next_page();
1034 }
1035
1036 return true;
1037}
1038
1039
1040#ifdef DEBUG
1041int PagedSpace::CountTotalPages() {
1042 int count = 0;
1043 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
1044 count++;
1045 }
1046 return count;
1047}
1048#endif
1049
1050
1051void PagedSpace::Shrink() {
Steve Block6ded16b2010-05-10 14:33:55 +01001052 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;
1056 }
1057
Steve Blocka7e24c12009-10-30 11:49:00 +00001058 // 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++;
1066 }
1067
1068 // Free pages after top_page.
Steve Block44f0eee2011-05-26 01:26:41 +01001069 Page* p = heap()->isolate()->memory_allocator()->
1070 FreePages(top_page->next_page());
1071 heap()->isolate()->memory_allocator()->SetNextPage(top_page, p);
Steve Blocka7e24c12009-10-30 11:49:00 +00001072
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;
1079 }
1080
1081 accounting_stats_.ShrinkSpace(pages_to_free * Page::kObjectAreaSize);
1082 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
1083}
1084
1085
1086bool 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()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001093 last_page = heap()->isolate()->memory_allocator()->
1094 FindLastPageInSameChunk(next_page);
Steve Blocka7e24c12009-10-30 11:49:00 +00001095 next_page = last_page->next_page();
1096 }
1097
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 =
Steve Block44f0eee2011-05-26 01:26:41 +01001103 heap()->isolate()->memory_allocator()->FindLastPageInSameChunk(
1104 last_page->next_page());
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 } while (Capacity() < capacity);
1106
1107 return true;
1108}
1109
1110
1111#ifdef DEBUG
1112void PagedSpace::Print() { }
1113#endif
1114
1115
1116#ifdef DEBUG
1117// We do not assume that the PageIterator works, because it depends on the
1118// invariants we are checking during verification.
1119void PagedSpace::Verify(ObjectVisitor* visitor) {
1120 // 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);
Steve Block44f0eee2011-05-26 01:26:41 +01001124 ASSERT(heap()->isolate()->memory_allocator()->IsPageInSpace(top_page, this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001125
1126 // 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 {
Steve Blocka7e24c12009-10-30 11:49:00 +00001133 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;
Steve Blocka7e24c12009-10-30 11:49:00 +00001138 }
1139
1140 // 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());
Steve Block44f0eee2011-05-26 01:26:41 +01001149 ASSERT(heap()->map_space()->Contains(map));
Steve Blocka7e24c12009-10-30 11:49:00 +00001150
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
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001158 // have page regions covering intergenerational references should be
1159 // marked dirty.
Steve Blocka7e24c12009-10-30 11:49:00 +00001160 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);
1168 }
1169
1170 current_page = current_page->next_page();
1171 }
1172}
1173#endif
1174
1175
1176// -----------------------------------------------------------------------------
1177// NewSpace implementation
1178
1179
1180bool NewSpace::Setup(Address start, int size) {
1181 // Setup new space based on the preallocated memory block defined by
1182 // 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();
1186 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
1191 // 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
Steve Block44f0eee2011-05-26 01:26:41 +01001200 ASSERT(size == 2 * heap()->ReservedSemiSpaceSize());
Steve Blocka7e24c12009-10-30 11:49:00 +00001201 ASSERT(IsAddressAligned(start, size, 0));
1202
1203 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)) {
1211 return false;
1212 }
1213
1214 start_ = start;
1215 address_mask_ = ~(size - 1);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001216 object_mask_ = address_mask_ | kHeapObjectTagMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00001217 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
1218
1219 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;
1223
1224 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1225 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;
1242 mc_forwarding_info_.top = NULL;
1243 mc_forwarding_info_.limit = NULL;
1244
1245 to_space_.TearDown();
1246 from_space_.TearDown();
1247}
1248
1249
Steve Blocka7e24c12009-10-30 11:49:00 +00001250void NewSpace::Flip() {
1251 SemiSpace tmp = from_space_;
1252 from_space_ = to_space_;
1253 to_space_ = tmp;
1254}
1255
1256
1257void NewSpace::Grow() {
1258 ASSERT(Capacity() < MaximumCapacity());
1259 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.
1264 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 }
1271 allocation_info_.limit = to_space_.high();
1272 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());
Steve Blockd0582a62009-12-15 09:54:21 +00001278 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)) {
1282 // Only shrink from space if we managed to shrink to space.
1283 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
1284 // If we managed to shrink to space but couldn't shrink from
1285 // space, attempt to grow to space again.
1286 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 }
1293 allocation_info_.limit = to_space_.high();
1294 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1295}
1296
1297
1298void NewSpace::ResetAllocationInfo() {
1299 allocation_info_.top = to_space_.low();
1300 allocation_info_.limit = to_space_.high();
1301 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1302}
1303
1304
1305void 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_);
1309}
1310
1311
1312void 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_);
1318}
1319
1320
1321#ifdef DEBUG
1322// We do not use the SemispaceIterator because verification doesn't assume
1323// 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.
1330 Address current = to_space_.low();
1331 while (current < top()) {
1332 HeapObject* object = HeapObject::FromAddress(current);
1333
1334 // 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());
Steve Block44f0eee2011-05-26 01:26:41 +01001338 ASSERT(heap()->map_space()->Contains(map));
Steve Blocka7e24c12009-10-30 11:49:00 +00001339
1340 // The object should not be code or a map.
1341 ASSERT(!object->IsMap());
1342 ASSERT(!object->IsCode());
1343
1344 // The object itself should look OK.
1345 object->Verify();
1346
1347 // 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);
1351
1352 current += size;
1353 }
1354
1355 // The allocation pointer should not be in the middle of an object.
1356 ASSERT(current == top());
1357}
1358#endif
1359
1360
1361bool SemiSpace::Commit() {
1362 ASSERT(!is_committed());
Steve Block44f0eee2011-05-26 01:26:41 +01001363 if (!heap()->isolate()->memory_allocator()->CommitBlock(
1364 start_, capacity_, executable())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001365 return false;
1366 }
1367 committed_ = true;
1368 return true;
1369}
1370
1371
1372bool SemiSpace::Uncommit() {
1373 ASSERT(is_committed());
Steve Block44f0eee2011-05-26 01:26:41 +01001374 if (!heap()->isolate()->memory_allocator()->UncommitBlock(
1375 start_, capacity_)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001376 return false;
1377 }
1378 committed_ = false;
1379 return true;
1380}
1381
1382
1383// -----------------------------------------------------------------------------
1384// SemiSpace implementation
1385
1386bool SemiSpace::Setup(Address start,
1387 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.
1395 initial_capacity_ = initial_capacity;
1396 capacity_ = initial_capacity;
1397 maximum_capacity_ = maximum_capacity;
1398 committed_ = false;
1399
1400 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_;
1405
1406 return Commit();
1407}
1408
1409
1410void SemiSpace::TearDown() {
1411 start_ = NULL;
1412 capacity_ = 0;
1413}
1414
1415
1416bool SemiSpace::Grow() {
1417 // Double the semispace size but only up to maximum capacity.
1418 int maximum_extra = maximum_capacity_ - capacity_;
Steve Blockd0582a62009-12-15 09:54:21 +00001419 int extra = Min(RoundUp(capacity_, static_cast<int>(OS::AllocateAlignment())),
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 maximum_extra);
Steve Block44f0eee2011-05-26 01:26:41 +01001421 if (!heap()->isolate()->memory_allocator()->CommitBlock(
1422 high(), extra, executable())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 return false;
1424 }
1425 capacity_ += extra;
1426 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(
1436 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_);
1447 size_t delta = capacity_ - new_capacity;
1448 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
Steve Block44f0eee2011-05-26 01:26:41 +01001449 if (!heap()->isolate()->memory_allocator()->UncommitBlock(
1450 high() - delta, delta)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001451 return false;
1452 }
1453 capacity_ = new_capacity;
1454 return true;
1455}
1456
1457
1458#ifdef DEBUG
1459void SemiSpace::Print() { }
1460
1461
1462void SemiSpace::Verify() { }
1463#endif
1464
1465
1466// -----------------------------------------------------------------------------
1467// SemiSpaceIterator implementation.
1468SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
1469 Initialize(space, space->bottom(), space->top(), NULL);
1470}
1471
1472
1473SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1474 HeapObjectCallback size_func) {
1475 Initialize(space, space->bottom(), space->top(), size_func);
1476}
1477
1478
1479SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1480 Initialize(space, start, space->top(), NULL);
1481}
1482
1483
1484void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1485 Address end,
1486 HeapObjectCallback size_func) {
1487 ASSERT(space->ToSpaceContains(start));
1488 ASSERT(space->ToSpaceLow() <= end
1489 && end <= space->ToSpaceHigh());
1490 space_ = &space->to_space_;
1491 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);
Steve Blocka7e24c12009-10-30 11:49:00 +00001545 }
1546 }
1547
1548#undef CASE
1549
1550 PrintF("\n Code kind histograms: \n");
1551 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001552 if (isolate->code_kind_statistics()[i] > 0) {
1553 PrintF(" %-20s: %10d bytes\n", table[i],
1554 isolate->code_kind_statistics()[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 }
1556 }
1557 PrintF("\n");
1558}
1559
1560
1561static int CollectHistogramInfo(HeapObject* obj) {
Steve Block44f0eee2011-05-26 01:26:41 +01001562 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 InstanceType type = obj->map()->instance_type();
1564 ASSERT(0 <= type && type <= LAST_TYPE);
Steve Block44f0eee2011-05-26 01:26:41 +01001565 ASSERT(isolate->heap_histograms()[type].name() != NULL);
1566 isolate->heap_histograms()[type].increment_number(1);
1567 isolate->heap_histograms()[type].increment_bytes(obj->Size());
Steve Blocka7e24c12009-10-30 11:49:00 +00001568
1569 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001570 JSObject::cast(obj)->IncrementSpillStatistics(
1571 isolate->js_spill_information());
Steve Blocka7e24c12009-10-30 11:49:00 +00001572 }
1573
1574 return obj->Size();
1575}
1576
1577
1578static void ReportHistogram(bool print_spill) {
Steve Block44f0eee2011-05-26 01:26:41 +01001579 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001580 PrintF("\n Object Histogram:\n");
1581 for (int i = 0; i <= LAST_TYPE; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01001582 if (isolate->heap_histograms()[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001583 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Block44f0eee2011-05-26 01:26:41 +01001584 isolate->heap_histograms()[i].name(),
1585 isolate->heap_histograms()[i].number(),
1586 isolate->heap_histograms()[i].bytes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001587 }
1588 }
1589 PrintF("\n");
1590
1591 // Summarize string types.
1592 int string_number = 0;
1593 int string_bytes = 0;
1594#define INCREMENT(type, size, name, camel_name) \
Steve Block44f0eee2011-05-26 01:26:41 +01001595 string_number += isolate->heap_histograms()[type].number(); \
1596 string_bytes += isolate->heap_histograms()[type].bytes();
Steve Blocka7e24c12009-10-30 11:49:00 +00001597 STRING_TYPE_LIST(INCREMENT)
1598#undef INCREMENT
1599 if (string_number > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001600 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
Steve Blocka7e24c12009-10-30 11:49:00 +00001601 string_bytes);
1602 }
1603
1604 if (FLAG_collect_heap_spill_statistics && print_spill) {
Steve Block44f0eee2011-05-26 01:26:41 +01001605 isolate->js_spill_information()->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +00001606 }
1607}
1608#endif // DEBUG
1609
1610
1611// Support for statistics gathering for --heap-stats and --log-gc.
Steve Blocka7e24c12009-10-30 11:49:00 +00001612void NewSpace::ClearHistograms() {
1613 for (int i = 0; i <= LAST_TYPE; i++) {
1614 allocated_histogram_[i].clear();
1615 promoted_histogram_[i].clear();
1616 }
1617}
1618
1619// Because the copying collector does not touch garbage objects, we iterate
1620// the new space before a collection to get a histogram of allocated objects.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001621// This only happens when --log-gc flag is set.
Steve Blocka7e24c12009-10-30 11:49:00 +00001622void NewSpace::CollectStatistics() {
1623 ClearHistograms();
1624 SemiSpaceIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00001625 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next())
1626 RecordAllocation(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001627}
1628
1629
Steve Block44f0eee2011-05-26 01:26:41 +01001630static void DoReportStatistics(Isolate* isolate,
1631 HistogramInfo* info, const char* description) {
1632 LOG(isolate, HeapSampleBeginEvent("NewSpace", description));
Steve Blocka7e24c12009-10-30 11:49:00 +00001633 // Lump all the string types together.
1634 int string_number = 0;
1635 int string_bytes = 0;
1636#define INCREMENT(type, size, name, camel_name) \
1637 string_number += info[type].number(); \
1638 string_bytes += info[type].bytes();
1639 STRING_TYPE_LIST(INCREMENT)
1640#undef INCREMENT
1641 if (string_number > 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001642 LOG(isolate,
1643 HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
Steve Blocka7e24c12009-10-30 11:49:00 +00001644 }
1645
1646 // Then do the other types.
1647 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1648 if (info[i].number() > 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001649 LOG(isolate,
1650 HeapSampleItemEvent(info[i].name(), info[i].number(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001651 info[i].bytes()));
1652 }
1653 }
Steve Block44f0eee2011-05-26 01:26:41 +01001654 LOG(isolate, HeapSampleEndEvent("NewSpace", description));
Steve Blocka7e24c12009-10-30 11:49:00 +00001655}
Steve Blocka7e24c12009-10-30 11:49:00 +00001656
1657
1658void NewSpace::ReportStatistics() {
1659#ifdef DEBUG
1660 if (FLAG_heap_stats) {
1661 float pct = static_cast<float>(Available()) / Capacity();
Ben Murdochf87a2032010-10-22 12:50:53 +01001662 PrintF(" capacity: %" V8_PTR_PREFIX "d"
1663 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001664 Capacity(), Available(), static_cast<int>(pct*100));
1665 PrintF("\n Object Histogram:\n");
1666 for (int i = 0; i <= LAST_TYPE; i++) {
1667 if (allocated_histogram_[i].number() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001668 PrintF(" %-34s%10d (%10d bytes)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00001669 allocated_histogram_[i].name(),
1670 allocated_histogram_[i].number(),
1671 allocated_histogram_[i].bytes());
1672 }
1673 }
1674 PrintF("\n");
1675 }
1676#endif // DEBUG
1677
Steve Blocka7e24c12009-10-30 11:49:00 +00001678 if (FLAG_log_gc) {
Steve Block44f0eee2011-05-26 01:26:41 +01001679 Isolate* isolate = ISOLATE;
1680 DoReportStatistics(isolate, allocated_histogram_, "allocated");
1681 DoReportStatistics(isolate, promoted_histogram_, "promoted");
Steve Blocka7e24c12009-10-30 11:49:00 +00001682 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001683}
1684
1685
1686void NewSpace::RecordAllocation(HeapObject* obj) {
1687 InstanceType type = obj->map()->instance_type();
1688 ASSERT(0 <= type && type <= LAST_TYPE);
1689 allocated_histogram_[type].increment_number(1);
1690 allocated_histogram_[type].increment_bytes(obj->Size());
1691}
1692
1693
1694void NewSpace::RecordPromotion(HeapObject* obj) {
1695 InstanceType type = obj->map()->instance_type();
1696 ASSERT(0 <= type && type <= LAST_TYPE);
1697 promoted_histogram_[type].increment_number(1);
1698 promoted_histogram_[type].increment_bytes(obj->Size());
1699}
Steve Blocka7e24c12009-10-30 11:49:00 +00001700
1701
1702// -----------------------------------------------------------------------------
1703// Free lists for old object spaces implementation
1704
Steve Block44f0eee2011-05-26 01:26:41 +01001705void FreeListNode::set_size(Heap* heap, int size_in_bytes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001706 ASSERT(size_in_bytes > 0);
1707 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1708
1709 // We write a map and possibly size information to the block. If the block
1710 // is big enough to be a ByteArray with at least one extra word (the next
1711 // pointer), we set its map to be the byte array map and its size to an
1712 // appropriate array length for the desired size from HeapObject::Size().
1713 // If the block is too small (eg, one or two words), to hold both a size
1714 // field and a next pointer, we give it a filler map that gives it the
1715 // correct size.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001716 if (size_in_bytes > ByteArray::kHeaderSize) {
Steve Block44f0eee2011-05-26 01:26:41 +01001717 set_map(heap->raw_unchecked_byte_array_map());
Steve Blockd0582a62009-12-15 09:54:21 +00001718 // Can't use ByteArray::cast because it fails during deserialization.
1719 ByteArray* this_as_byte_array = reinterpret_cast<ByteArray*>(this);
1720 this_as_byte_array->set_length(ByteArray::LengthFor(size_in_bytes));
Steve Blocka7e24c12009-10-30 11:49:00 +00001721 } else if (size_in_bytes == kPointerSize) {
Steve Block44f0eee2011-05-26 01:26:41 +01001722 set_map(heap->raw_unchecked_one_pointer_filler_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00001723 } else if (size_in_bytes == 2 * kPointerSize) {
Steve Block44f0eee2011-05-26 01:26:41 +01001724 set_map(heap->raw_unchecked_two_pointer_filler_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00001725 } else {
1726 UNREACHABLE();
1727 }
Steve Blockd0582a62009-12-15 09:54:21 +00001728 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
1729 // deserialization because the byte array map is not done yet.
Steve Blocka7e24c12009-10-30 11:49:00 +00001730}
1731
1732
Steve Block44f0eee2011-05-26 01:26:41 +01001733Address FreeListNode::next(Heap* heap) {
Steve Block3ce2e202009-11-05 08:53:23 +00001734 ASSERT(IsFreeListNode(this));
Steve Block44f0eee2011-05-26 01:26:41 +01001735 if (map() == heap->raw_unchecked_byte_array_map()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001736 ASSERT(Size() >= kNextOffset + kPointerSize);
1737 return Memory::Address_at(address() + kNextOffset);
1738 } else {
1739 return Memory::Address_at(address() + kPointerSize);
1740 }
1741}
1742
1743
Steve Block44f0eee2011-05-26 01:26:41 +01001744void FreeListNode::set_next(Heap* heap, Address next) {
Steve Block3ce2e202009-11-05 08:53:23 +00001745 ASSERT(IsFreeListNode(this));
Steve Block44f0eee2011-05-26 01:26:41 +01001746 if (map() == heap->raw_unchecked_byte_array_map()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001747 ASSERT(Size() >= kNextOffset + kPointerSize);
1748 Memory::Address_at(address() + kNextOffset) = next;
1749 } else {
1750 Memory::Address_at(address() + kPointerSize) = next;
1751 }
1752}
1753
1754
Steve Block44f0eee2011-05-26 01:26:41 +01001755OldSpaceFreeList::OldSpaceFreeList(Heap* heap, AllocationSpace owner)
1756 : heap_(heap),
1757 owner_(owner) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001758 Reset();
1759}
1760
1761
1762void OldSpaceFreeList::Reset() {
1763 available_ = 0;
1764 for (int i = 0; i < kFreeListsLength; i++) {
1765 free_[i].head_node_ = NULL;
1766 }
1767 needs_rebuild_ = false;
1768 finger_ = kHead;
1769 free_[kHead].next_size_ = kEnd;
1770}
1771
1772
1773void OldSpaceFreeList::RebuildSizeList() {
1774 ASSERT(needs_rebuild_);
1775 int cur = kHead;
1776 for (int i = cur + 1; i < kFreeListsLength; i++) {
1777 if (free_[i].head_node_ != NULL) {
1778 free_[cur].next_size_ = i;
1779 cur = i;
1780 }
1781 }
1782 free_[cur].next_size_ = kEnd;
1783 needs_rebuild_ = false;
1784}
1785
1786
1787int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1788#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +01001789 Isolate::Current()->memory_allocator()->ZapBlock(start, size_in_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001790#endif
1791 FreeListNode* node = FreeListNode::FromAddress(start);
Steve Block44f0eee2011-05-26 01:26:41 +01001792 node->set_size(heap_, size_in_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001793
1794 // We don't use the freelists in compacting mode. This makes it more like a
1795 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1796 // collector.
1797 if (FLAG_always_compact) {
1798 return size_in_bytes;
1799 }
1800
1801 // Early return to drop too-small blocks on the floor (one or two word
1802 // blocks cannot hold a map pointer, a size field, and a pointer to the
1803 // next block in the free list).
1804 if (size_in_bytes < kMinBlockSize) {
1805 return size_in_bytes;
1806 }
1807
1808 // Insert other blocks at the head of an exact free list.
1809 int index = size_in_bytes >> kPointerSizeLog2;
Steve Block44f0eee2011-05-26 01:26:41 +01001810 node->set_next(heap_, free_[index].head_node_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001811 free_[index].head_node_ = node->address();
1812 available_ += size_in_bytes;
1813 needs_rebuild_ = true;
1814 return 0;
1815}
1816
1817
John Reck59135872010-11-02 12:39:01 -07001818MaybeObject* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001819 ASSERT(0 < size_in_bytes);
1820 ASSERT(size_in_bytes <= kMaxBlockSize);
1821 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1822
1823 if (needs_rebuild_) RebuildSizeList();
1824 int index = size_in_bytes >> kPointerSizeLog2;
1825 // Check for a perfect fit.
1826 if (free_[index].head_node_ != NULL) {
1827 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1828 // If this was the last block of its size, remove the size.
Steve Block44f0eee2011-05-26 01:26:41 +01001829 if ((free_[index].head_node_ = node->next(heap_)) == NULL)
1830 RemoveSize(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001831 available_ -= size_in_bytes;
1832 *wasted_bytes = 0;
1833 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1834 return node;
1835 }
1836 // Search the size list for the best fit.
1837 int prev = finger_ < index ? finger_ : kHead;
1838 int cur = FindSize(index, &prev);
1839 ASSERT(index < cur);
1840 if (cur == kEnd) {
1841 // No large enough size in list.
1842 *wasted_bytes = 0;
Ben Murdochf87a2032010-10-22 12:50:53 +01001843 return Failure::RetryAfterGC(owner_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001844 }
1845 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1846 int rem = cur - index;
1847 int rem_bytes = rem << kPointerSizeLog2;
1848 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
1849 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
1850 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1851 size_in_bytes);
1852 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1853 // to avoid many redundant tests and calls to Insert/RemoveSize.
1854 if (prev < rem) {
1855 // Simple case: insert rem between prev and cur.
1856 finger_ = prev;
1857 free_[prev].next_size_ = rem;
1858 // If this was the last block of size cur, remove the size.
Steve Block44f0eee2011-05-26 01:26:41 +01001859 if ((free_[cur].head_node_ = cur_node->next(heap_)) == NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001860 free_[rem].next_size_ = free_[cur].next_size_;
1861 } else {
1862 free_[rem].next_size_ = cur;
1863 }
1864 // Add the remainder block.
Steve Block44f0eee2011-05-26 01:26:41 +01001865 rem_node->set_size(heap_, rem_bytes);
1866 rem_node->set_next(heap_, free_[rem].head_node_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001867 free_[rem].head_node_ = rem_node->address();
1868 } else {
1869 // If this was the last block of size cur, remove the size.
Steve Block44f0eee2011-05-26 01:26:41 +01001870 if ((free_[cur].head_node_ = cur_node->next(heap_)) == NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001871 finger_ = prev;
1872 free_[prev].next_size_ = free_[cur].next_size_;
1873 }
1874 if (rem_bytes < kMinBlockSize) {
1875 // Too-small remainder is wasted.
Steve Block44f0eee2011-05-26 01:26:41 +01001876 rem_node->set_size(heap_, rem_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001877 available_ -= size_in_bytes + rem_bytes;
1878 *wasted_bytes = rem_bytes;
1879 return cur_node;
1880 }
1881 // Add the remainder block and, if needed, insert its size.
Steve Block44f0eee2011-05-26 01:26:41 +01001882 rem_node->set_size(heap_, rem_bytes);
1883 rem_node->set_next(heap_, free_[rem].head_node_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001884 free_[rem].head_node_ = rem_node->address();
Steve Block44f0eee2011-05-26 01:26:41 +01001885 if (rem_node->next(heap_) == NULL) InsertSize(rem);
Steve Blocka7e24c12009-10-30 11:49:00 +00001886 }
1887 available_ -= size_in_bytes;
1888 *wasted_bytes = 0;
1889 return cur_node;
1890}
1891
1892
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001893void OldSpaceFreeList::MarkNodes() {
1894 for (int i = 0; i < kFreeListsLength; i++) {
1895 Address cur_addr = free_[i].head_node_;
1896 while (cur_addr != NULL) {
1897 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
Steve Block44f0eee2011-05-26 01:26:41 +01001898 cur_addr = cur_node->next(heap_);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001899 cur_node->SetMark();
1900 }
1901 }
1902}
1903
1904
Steve Blocka7e24c12009-10-30 11:49:00 +00001905#ifdef DEBUG
1906bool OldSpaceFreeList::Contains(FreeListNode* node) {
1907 for (int i = 0; i < kFreeListsLength; i++) {
1908 Address cur_addr = free_[i].head_node_;
1909 while (cur_addr != NULL) {
1910 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1911 if (cur_node == node) return true;
Steve Block44f0eee2011-05-26 01:26:41 +01001912 cur_addr = cur_node->next(heap_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001913 }
1914 }
1915 return false;
1916}
1917#endif
1918
1919
Steve Block44f0eee2011-05-26 01:26:41 +01001920FixedSizeFreeList::FixedSizeFreeList(Heap* heap,
1921 AllocationSpace owner,
1922 int object_size)
1923 : heap_(heap), owner_(owner), object_size_(object_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001924 Reset();
1925}
1926
1927
1928void FixedSizeFreeList::Reset() {
1929 available_ = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001930 head_ = tail_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001931}
1932
1933
1934void FixedSizeFreeList::Free(Address start) {
1935#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +01001936 Isolate::Current()->memory_allocator()->ZapBlock(start, object_size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001937#endif
Leon Clarkee46be812010-01-19 14:06:41 +00001938 // We only use the freelists with mark-sweep.
Steve Block44f0eee2011-05-26 01:26:41 +01001939 ASSERT(!HEAP->mark_compact_collector()->IsCompacting());
Steve Blocka7e24c12009-10-30 11:49:00 +00001940 FreeListNode* node = FreeListNode::FromAddress(start);
Steve Block44f0eee2011-05-26 01:26:41 +01001941 node->set_size(heap_, object_size_);
1942 node->set_next(heap_, NULL);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001943 if (head_ == NULL) {
1944 tail_ = head_ = node->address();
1945 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001946 FreeListNode::FromAddress(tail_)->set_next(heap_, node->address());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001947 tail_ = node->address();
1948 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001949 available_ += object_size_;
1950}
1951
1952
John Reck59135872010-11-02 12:39:01 -07001953MaybeObject* FixedSizeFreeList::Allocate() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001954 if (head_ == NULL) {
Ben Murdochf87a2032010-10-22 12:50:53 +01001955 return Failure::RetryAfterGC(owner_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001956 }
1957
1958 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
1959 FreeListNode* node = FreeListNode::FromAddress(head_);
Steve Block44f0eee2011-05-26 01:26:41 +01001960 head_ = node->next(heap_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001961 available_ -= object_size_;
1962 return node;
1963}
1964
1965
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001966void FixedSizeFreeList::MarkNodes() {
1967 Address cur_addr = head_;
1968 while (cur_addr != NULL && cur_addr != tail_) {
1969 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
Steve Block44f0eee2011-05-26 01:26:41 +01001970 cur_addr = cur_node->next(heap_);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001971 cur_node->SetMark();
1972 }
1973}
1974
1975
Steve Blocka7e24c12009-10-30 11:49:00 +00001976// -----------------------------------------------------------------------------
1977// OldSpace implementation
1978
1979void OldSpace::PrepareForMarkCompact(bool will_compact) {
Steve Block6ded16b2010-05-10 14:33:55 +01001980 // Call prepare of the super class.
1981 PagedSpace::PrepareForMarkCompact(will_compact);
1982
Steve Blocka7e24c12009-10-30 11:49:00 +00001983 if (will_compact) {
1984 // Reset relocation info. During a compacting collection, everything in
1985 // the space is considered 'available' and we will rediscover live data
1986 // and waste during the collection.
1987 MCResetRelocationInfo();
1988 ASSERT(Available() == Capacity());
1989 } else {
1990 // During a non-compacting collection, everything below the linear
1991 // allocation pointer is considered allocated (everything above is
1992 // available) and we will rediscover available and wasted bytes during
1993 // the collection.
1994 accounting_stats_.AllocateBytes(free_list_.available());
1995 accounting_stats_.FillWastedBytes(Waste());
1996 }
1997
1998 // Clear the free list before a full GC---it will be rebuilt afterward.
1999 free_list_.Reset();
2000}
2001
2002
2003void OldSpace::MCCommitRelocationInfo() {
2004 // Update fast allocation info.
2005 allocation_info_.top = mc_forwarding_info_.top;
2006 allocation_info_.limit = mc_forwarding_info_.limit;
2007 ASSERT(allocation_info_.VerifyPagedAllocation());
2008
2009 // The space is compacted and we haven't yet built free lists or
2010 // wasted any space.
2011 ASSERT(Waste() == 0);
2012 ASSERT(AvailableFree() == 0);
2013
2014 // Build the free list for the space.
2015 int computed_size = 0;
2016 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2017 while (it.has_next()) {
2018 Page* p = it.next();
2019 // Space below the relocation pointer is allocated.
Steve Blockd0582a62009-12-15 09:54:21 +00002020 computed_size +=
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002021 static_cast<int>(p->AllocationWatermark() - p->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +00002022 if (it.has_next()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002023 // Free the space at the top of the page.
Steve Blockd0582a62009-12-15 09:54:21 +00002024 int extra_size =
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002025 static_cast<int>(p->ObjectAreaEnd() - p->AllocationWatermark());
Steve Blocka7e24c12009-10-30 11:49:00 +00002026 if (extra_size > 0) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002027 int wasted_bytes = free_list_.Free(p->AllocationWatermark(),
2028 extra_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002029 // The bytes we have just "freed" to add to the free list were
2030 // already accounted as available.
2031 accounting_stats_.WasteBytes(wasted_bytes);
2032 }
2033 }
2034 }
2035
2036 // Make sure the computed size - based on the used portion of the pages in
2037 // use - matches the size obtained while computing forwarding addresses.
2038 ASSERT(computed_size == Size());
2039}
2040
2041
Leon Clarkee46be812010-01-19 14:06:41 +00002042bool NewSpace::ReserveSpace(int bytes) {
2043 // We can't reliably unpack a partial snapshot that needs more new space
2044 // space than the minimum NewSpace size.
2045 ASSERT(bytes <= InitialCapacity());
2046 Address limit = allocation_info_.limit;
2047 Address top = allocation_info_.top;
2048 return limit - top >= bytes;
2049}
2050
2051
Steve Block6ded16b2010-05-10 14:33:55 +01002052void PagedSpace::FreePages(Page* prev, Page* last) {
2053 if (last == AllocationTopPage()) {
2054 // Pages are already at the end of used pages.
2055 return;
2056 }
2057
2058 Page* first = NULL;
2059
2060 // Remove pages from the list.
2061 if (prev == NULL) {
2062 first = first_page_;
2063 first_page_ = last->next_page();
2064 } else {
2065 first = prev->next_page();
Steve Block44f0eee2011-05-26 01:26:41 +01002066 heap()->isolate()->memory_allocator()->SetNextPage(
2067 prev, last->next_page());
Steve Block6ded16b2010-05-10 14:33:55 +01002068 }
2069
2070 // Attach it after the last page.
Steve Block44f0eee2011-05-26 01:26:41 +01002071 heap()->isolate()->memory_allocator()->SetNextPage(last_page_, first);
Steve Block6ded16b2010-05-10 14:33:55 +01002072 last_page_ = last;
Steve Block44f0eee2011-05-26 01:26:41 +01002073 heap()->isolate()->memory_allocator()->SetNextPage(last, NULL);
Steve Block6ded16b2010-05-10 14:33:55 +01002074
2075 // Clean them up.
2076 do {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002077 first->InvalidateWatermark(true);
2078 first->SetAllocationWatermark(first->ObjectAreaStart());
2079 first->SetCachedAllocationWatermark(first->ObjectAreaStart());
2080 first->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Block6ded16b2010-05-10 14:33:55 +01002081 first = first->next_page();
2082 } while (first != NULL);
2083
2084 // Order of pages in this space might no longer be consistent with
2085 // order of pages in chunks.
2086 page_list_is_chunk_ordered_ = false;
2087}
2088
2089
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002090void PagedSpace::RelinkPageListInChunkOrder(bool deallocate_blocks) {
2091 const bool add_to_freelist = true;
2092
2093 // Mark used and unused pages to properly fill unused pages
2094 // after reordering.
2095 PageIterator all_pages_iterator(this, PageIterator::ALL_PAGES);
2096 Page* last_in_use = AllocationTopPage();
2097 bool in_use = true;
2098
2099 while (all_pages_iterator.has_next()) {
2100 Page* p = all_pages_iterator.next();
2101 p->SetWasInUseBeforeMC(in_use);
2102 if (p == last_in_use) {
2103 // We passed a page containing allocation top. All consequent
2104 // pages are not used.
2105 in_use = false;
2106 }
2107 }
2108
2109 if (page_list_is_chunk_ordered_) return;
2110
2111 Page* new_last_in_use = Page::FromAddress(NULL);
Steve Block44f0eee2011-05-26 01:26:41 +01002112 heap()->isolate()->memory_allocator()->RelinkPageListInChunkOrder(
2113 this, &first_page_, &last_page_, &new_last_in_use);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002114 ASSERT(new_last_in_use->is_valid());
2115
2116 if (new_last_in_use != last_in_use) {
2117 // Current allocation top points to a page which is now in the middle
2118 // of page list. We should move allocation top forward to the new last
2119 // used page so various object iterators will continue to work properly.
2120 int size_in_bytes = static_cast<int>(PageAllocationLimit(last_in_use) -
2121 last_in_use->AllocationTop());
2122
2123 last_in_use->SetAllocationWatermark(last_in_use->AllocationTop());
2124 if (size_in_bytes > 0) {
2125 Address start = last_in_use->AllocationTop();
2126 if (deallocate_blocks) {
2127 accounting_stats_.AllocateBytes(size_in_bytes);
2128 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2129 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002130 heap()->CreateFillerObjectAt(start, size_in_bytes);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002131 }
2132 }
2133
2134 // New last in use page was in the middle of the list before
2135 // sorting so it full.
2136 SetTop(new_last_in_use->AllocationTop());
2137
2138 ASSERT(AllocationTopPage() == new_last_in_use);
2139 ASSERT(AllocationTopPage()->WasInUseBeforeMC());
2140 }
2141
2142 PageIterator pages_in_use_iterator(this, PageIterator::PAGES_IN_USE);
2143 while (pages_in_use_iterator.has_next()) {
2144 Page* p = pages_in_use_iterator.next();
2145 if (!p->WasInUseBeforeMC()) {
2146 // Empty page is in the middle of a sequence of used pages.
2147 // Allocate it as a whole and deallocate immediately.
2148 int size_in_bytes = static_cast<int>(PageAllocationLimit(p) -
2149 p->ObjectAreaStart());
2150
2151 p->SetAllocationWatermark(p->ObjectAreaStart());
2152 Address start = p->ObjectAreaStart();
2153 if (deallocate_blocks) {
2154 accounting_stats_.AllocateBytes(size_in_bytes);
2155 DeallocateBlock(start, size_in_bytes, add_to_freelist);
2156 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002157 heap()->CreateFillerObjectAt(start, size_in_bytes);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002158 }
2159 }
2160 }
2161
2162 page_list_is_chunk_ordered_ = true;
2163}
2164
2165
Steve Block6ded16b2010-05-10 14:33:55 +01002166void PagedSpace::PrepareForMarkCompact(bool will_compact) {
2167 if (will_compact) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002168 RelinkPageListInChunkOrder(false);
Steve Block6ded16b2010-05-10 14:33:55 +01002169 }
2170}
2171
2172
Leon Clarkee46be812010-01-19 14:06:41 +00002173bool PagedSpace::ReserveSpace(int bytes) {
2174 Address limit = allocation_info_.limit;
2175 Address top = allocation_info_.top;
2176 if (limit - top >= bytes) return true;
2177
2178 // There wasn't enough space in the current page. Lets put the rest
2179 // of the page on the free list and start a fresh page.
2180 PutRestOfCurrentPageOnFreeList(TopPageOf(allocation_info_));
2181
2182 Page* reserved_page = TopPageOf(allocation_info_);
2183 int bytes_left_to_reserve = bytes;
2184 while (bytes_left_to_reserve > 0) {
2185 if (!reserved_page->next_page()->is_valid()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002186 if (heap()->OldGenerationAllocationLimitReached()) return false;
Leon Clarkee46be812010-01-19 14:06:41 +00002187 Expand(reserved_page);
2188 }
2189 bytes_left_to_reserve -= Page::kPageSize;
2190 reserved_page = reserved_page->next_page();
2191 if (!reserved_page->is_valid()) return false;
2192 }
2193 ASSERT(TopPageOf(allocation_info_)->next_page()->is_valid());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002194 TopPageOf(allocation_info_)->next_page()->InvalidateWatermark(true);
Leon Clarkee46be812010-01-19 14:06:41 +00002195 SetAllocationInfo(&allocation_info_,
2196 TopPageOf(allocation_info_)->next_page());
2197 return true;
2198}
2199
2200
2201// You have to call this last, since the implementation from PagedSpace
2202// doesn't know that memory was 'promised' to large object space.
2203bool LargeObjectSpace::ReserveSpace(int bytes) {
Steve Block44f0eee2011-05-26 01:26:41 +01002204 return heap()->OldGenerationSpaceAvailable() >= bytes;
Leon Clarkee46be812010-01-19 14:06:41 +00002205}
2206
2207
Steve Blocka7e24c12009-10-30 11:49:00 +00002208// Slow case for normal allocation. Try in order: (1) allocate in the next
2209// page in the space, (2) allocate off the space's free list, (3) expand the
2210// space, (4) fail.
2211HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
2212 // Linear allocation in this space has failed. If there is another page
2213 // in the space, move to that page and allocate there. This allocation
2214 // should succeed (size_in_bytes should not be greater than a page's
2215 // object area size).
2216 Page* current_page = TopPageOf(allocation_info_);
2217 if (current_page->next_page()->is_valid()) {
2218 return AllocateInNextPage(current_page, size_in_bytes);
2219 }
2220
Steve Blockd0582a62009-12-15 09:54:21 +00002221 // There is no next page in this space. Try free list allocation unless that
2222 // is currently forbidden.
Steve Block44f0eee2011-05-26 01:26:41 +01002223 if (!heap()->linear_allocation()) {
Steve Blockd0582a62009-12-15 09:54:21 +00002224 int wasted_bytes;
John Reck59135872010-11-02 12:39:01 -07002225 Object* result;
2226 MaybeObject* maybe = free_list_.Allocate(size_in_bytes, &wasted_bytes);
Steve Blockd0582a62009-12-15 09:54:21 +00002227 accounting_stats_.WasteBytes(wasted_bytes);
John Reck59135872010-11-02 12:39:01 -07002228 if (maybe->ToObject(&result)) {
Steve Blockd0582a62009-12-15 09:54:21 +00002229 accounting_stats_.AllocateBytes(size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002230
2231 HeapObject* obj = HeapObject::cast(result);
2232 Page* p = Page::FromAddress(obj->address());
2233
2234 if (obj->address() >= p->AllocationWatermark()) {
2235 // There should be no hole between the allocation watermark
2236 // and allocated object address.
2237 // Memory above the allocation watermark was not swept and
2238 // might contain garbage pointers to new space.
2239 ASSERT(obj->address() == p->AllocationWatermark());
2240 p->SetAllocationWatermark(obj->address() + size_in_bytes);
2241 }
2242
2243 return obj;
Steve Blockd0582a62009-12-15 09:54:21 +00002244 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002245 }
2246
2247 // Free list allocation failed and there is no next page. Fail if we have
2248 // hit the old generation size limit that should cause a garbage
2249 // collection.
Steve Block44f0eee2011-05-26 01:26:41 +01002250 if (!heap()->always_allocate() &&
2251 heap()->OldGenerationAllocationLimitReached()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002252 return NULL;
2253 }
2254
2255 // Try to expand the space and allocate in the new next page.
2256 ASSERT(!current_page->next_page()->is_valid());
2257 if (Expand(current_page)) {
2258 return AllocateInNextPage(current_page, size_in_bytes);
2259 }
2260
2261 // Finally, fail.
2262 return NULL;
2263}
2264
2265
Leon Clarkee46be812010-01-19 14:06:41 +00002266void OldSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002267 current_page->SetAllocationWatermark(allocation_info_.top);
Steve Blockd0582a62009-12-15 09:54:21 +00002268 int free_size =
2269 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002270 if (free_size > 0) {
2271 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
2272 accounting_stats_.WasteBytes(wasted_bytes);
2273 }
Leon Clarkee46be812010-01-19 14:06:41 +00002274}
2275
2276
2277void FixedSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002278 current_page->SetAllocationWatermark(allocation_info_.top);
Leon Clarkee46be812010-01-19 14:06:41 +00002279 int free_size =
2280 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
2281 // In the fixed space free list all the free list items have the right size.
2282 // We use up the rest of the page while preserving this invariant.
2283 while (free_size >= object_size_in_bytes_) {
2284 free_list_.Free(allocation_info_.top);
2285 allocation_info_.top += object_size_in_bytes_;
2286 free_size -= object_size_in_bytes_;
2287 accounting_stats_.WasteBytes(object_size_in_bytes_);
2288 }
2289}
2290
2291
2292// Add the block at the top of the page to the space's free list, set the
2293// allocation info to the next page (assumed to be one), and allocate
2294// linearly there.
2295HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
2296 int size_in_bytes) {
2297 ASSERT(current_page->next_page()->is_valid());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002298 Page* next_page = current_page->next_page();
2299 next_page->ClearGCFields();
Leon Clarkee46be812010-01-19 14:06:41 +00002300 PutRestOfCurrentPageOnFreeList(current_page);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002301 SetAllocationInfo(&allocation_info_, next_page);
Steve Blocka7e24c12009-10-30 11:49:00 +00002302 return AllocateLinearly(&allocation_info_, size_in_bytes);
2303}
2304
2305
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002306void OldSpace::DeallocateBlock(Address start,
2307 int size_in_bytes,
2308 bool add_to_freelist) {
2309 Free(start, size_in_bytes, add_to_freelist);
2310}
2311
2312
Steve Blocka7e24c12009-10-30 11:49:00 +00002313#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002314void PagedSpace::ReportCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01002315 Isolate* isolate = Isolate::Current();
2316 CommentStatistic* comments_statistics =
2317 isolate->paged_space_comments_statistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00002318 ReportCodeKindStatistics();
2319 PrintF("Code comment statistics (\" [ comment-txt : size/ "
2320 "count (average)\"):\n");
Steve Block44f0eee2011-05-26 01:26:41 +01002321 for (int i = 0; i <= CommentStatistic::kMaxComments; i++) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002322 const CommentStatistic& cs = comments_statistics[i];
2323 if (cs.size > 0) {
2324 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
2325 cs.size/cs.count);
2326 }
2327 }
2328 PrintF("\n");
2329}
2330
2331
2332void PagedSpace::ResetCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01002333 Isolate* isolate = Isolate::Current();
2334 CommentStatistic* comments_statistics =
2335 isolate->paged_space_comments_statistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00002336 ClearCodeKindStatistics();
Steve Block44f0eee2011-05-26 01:26:41 +01002337 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
2338 comments_statistics[i].Clear();
2339 }
2340 comments_statistics[CommentStatistic::kMaxComments].comment = "Unknown";
2341 comments_statistics[CommentStatistic::kMaxComments].size = 0;
2342 comments_statistics[CommentStatistic::kMaxComments].count = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002343}
2344
2345
Steve Block44f0eee2011-05-26 01:26:41 +01002346// Adds comment to 'comment_statistics' table. Performance OK as long as
Steve Blocka7e24c12009-10-30 11:49:00 +00002347// 'kMaxComments' is small
Steve Block44f0eee2011-05-26 01:26:41 +01002348static void EnterComment(Isolate* isolate, const char* comment, int delta) {
2349 CommentStatistic* comments_statistics =
2350 isolate->paged_space_comments_statistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00002351 // Do not count empty comments
2352 if (delta <= 0) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002353 CommentStatistic* cs = &comments_statistics[CommentStatistic::kMaxComments];
Steve Blocka7e24c12009-10-30 11:49:00 +00002354 // Search for a free or matching entry in 'comments_statistics': 'cs'
2355 // points to result.
Steve Block44f0eee2011-05-26 01:26:41 +01002356 for (int i = 0; i < CommentStatistic::kMaxComments; i++) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002357 if (comments_statistics[i].comment == NULL) {
2358 cs = &comments_statistics[i];
2359 cs->comment = comment;
2360 break;
2361 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2362 cs = &comments_statistics[i];
2363 break;
2364 }
2365 }
2366 // Update entry for 'comment'
2367 cs->size += delta;
2368 cs->count += 1;
2369}
2370
2371
2372// Call for each nested comment start (start marked with '[ xxx', end marked
2373// with ']'. RelocIterator 'it' must point to a comment reloc info.
Steve Block44f0eee2011-05-26 01:26:41 +01002374static void CollectCommentStatistics(Isolate* isolate, RelocIterator* it) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002375 ASSERT(!it->done());
2376 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
2377 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2378 if (tmp[0] != '[') {
2379 // Not a nested comment; skip
2380 return;
2381 }
2382
2383 // Search for end of nested comment or a new nested comment
2384 const char* const comment_txt =
2385 reinterpret_cast<const char*>(it->rinfo()->data());
2386 const byte* prev_pc = it->rinfo()->pc();
2387 int flat_delta = 0;
2388 it->next();
2389 while (true) {
2390 // All nested comments must be terminated properly, and therefore exit
2391 // from loop.
2392 ASSERT(!it->done());
2393 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
2394 const char* const txt =
2395 reinterpret_cast<const char*>(it->rinfo()->data());
Steve Blockd0582a62009-12-15 09:54:21 +00002396 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002397 if (txt[0] == ']') break; // End of nested comment
2398 // A new comment
Steve Block44f0eee2011-05-26 01:26:41 +01002399 CollectCommentStatistics(isolate, it);
Steve Blocka7e24c12009-10-30 11:49:00 +00002400 // Skip code that was covered with previous comment
2401 prev_pc = it->rinfo()->pc();
2402 }
2403 it->next();
2404 }
Steve Block44f0eee2011-05-26 01:26:41 +01002405 EnterComment(isolate, comment_txt, flat_delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002406}
2407
2408
2409// Collects code size statistics:
2410// - by code kind
2411// - by code comment
2412void PagedSpace::CollectCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01002413 Isolate* isolate = heap()->isolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00002414 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002415 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002416 if (obj->IsCode()) {
2417 Code* code = Code::cast(obj);
Steve Block44f0eee2011-05-26 01:26:41 +01002418 isolate->code_kind_statistics()[code->kind()] += code->Size();
Steve Blocka7e24c12009-10-30 11:49:00 +00002419 RelocIterator it(code);
2420 int delta = 0;
2421 const byte* prev_pc = code->instruction_start();
2422 while (!it.done()) {
2423 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
Steve Blockd0582a62009-12-15 09:54:21 +00002424 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
Steve Block44f0eee2011-05-26 01:26:41 +01002425 CollectCommentStatistics(isolate, &it);
Steve Blocka7e24c12009-10-30 11:49:00 +00002426 prev_pc = it.rinfo()->pc();
2427 }
2428 it.next();
2429 }
2430
2431 ASSERT(code->instruction_start() <= prev_pc &&
Leon Clarkeac952652010-07-15 11:15:24 +01002432 prev_pc <= code->instruction_end());
2433 delta += static_cast<int>(code->instruction_end() - prev_pc);
Steve Block44f0eee2011-05-26 01:26:41 +01002434 EnterComment(isolate, "NoComment", delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002435 }
2436 }
2437}
2438
2439
2440void OldSpace::ReportStatistics() {
Ben Murdochf87a2032010-10-22 12:50:53 +01002441 int pct = static_cast<int>(Available() * 100 / Capacity());
2442 PrintF(" capacity: %" V8_PTR_PREFIX "d"
2443 ", waste: %" V8_PTR_PREFIX "d"
2444 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00002445 Capacity(), Waste(), Available(), pct);
2446
Steve Blocka7e24c12009-10-30 11:49:00 +00002447 ClearHistograms();
2448 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002449 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2450 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002451 ReportHistogram(true);
2452}
Steve Blocka7e24c12009-10-30 11:49:00 +00002453#endif
2454
2455// -----------------------------------------------------------------------------
2456// FixedSpace implementation
2457
2458void FixedSpace::PrepareForMarkCompact(bool will_compact) {
Steve Block6ded16b2010-05-10 14:33:55 +01002459 // Call prepare of the super class.
2460 PagedSpace::PrepareForMarkCompact(will_compact);
2461
Steve Blocka7e24c12009-10-30 11:49:00 +00002462 if (will_compact) {
2463 // Reset relocation info.
2464 MCResetRelocationInfo();
2465
2466 // During a compacting collection, everything in the space is considered
2467 // 'available' (set by the call to MCResetRelocationInfo) and we will
2468 // rediscover live and wasted bytes during the collection.
2469 ASSERT(Available() == Capacity());
2470 } else {
2471 // During a non-compacting collection, everything below the linear
2472 // allocation pointer except wasted top-of-page blocks is considered
2473 // allocated and we will rediscover available bytes during the
2474 // collection.
2475 accounting_stats_.AllocateBytes(free_list_.available());
2476 }
2477
2478 // Clear the free list before a full GC---it will be rebuilt afterward.
2479 free_list_.Reset();
2480}
2481
2482
2483void FixedSpace::MCCommitRelocationInfo() {
2484 // Update fast allocation info.
2485 allocation_info_.top = mc_forwarding_info_.top;
2486 allocation_info_.limit = mc_forwarding_info_.limit;
2487 ASSERT(allocation_info_.VerifyPagedAllocation());
2488
2489 // The space is compacted and we haven't yet wasted any space.
2490 ASSERT(Waste() == 0);
2491
2492 // Update allocation_top of each page in use and compute waste.
2493 int computed_size = 0;
2494 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2495 while (it.has_next()) {
2496 Page* page = it.next();
2497 Address page_top = page->AllocationTop();
Steve Blockd0582a62009-12-15 09:54:21 +00002498 computed_size += static_cast<int>(page_top - page->ObjectAreaStart());
Steve Blocka7e24c12009-10-30 11:49:00 +00002499 if (it.has_next()) {
Steve Blockd0582a62009-12-15 09:54:21 +00002500 accounting_stats_.WasteBytes(
2501 static_cast<int>(page->ObjectAreaEnd() - page_top));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002502 page->SetAllocationWatermark(page_top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002503 }
2504 }
2505
2506 // Make sure the computed size - based on the used portion of the
2507 // pages in use - matches the size we adjust during allocation.
2508 ASSERT(computed_size == Size());
2509}
2510
2511
2512// Slow case for normal allocation. Try in order: (1) allocate in the next
2513// page in the space, (2) allocate off the space's free list, (3) expand the
2514// space, (4) fail.
2515HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2516 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2517 // Linear allocation in this space has failed. If there is another page
2518 // in the space, move to that page and allocate there. This allocation
2519 // should succeed.
2520 Page* current_page = TopPageOf(allocation_info_);
2521 if (current_page->next_page()->is_valid()) {
2522 return AllocateInNextPage(current_page, size_in_bytes);
2523 }
2524
Steve Blockd0582a62009-12-15 09:54:21 +00002525 // There is no next page in this space. Try free list allocation unless
2526 // that is currently forbidden. The fixed space free list implicitly assumes
2527 // that all free blocks are of the fixed size.
Steve Block44f0eee2011-05-26 01:26:41 +01002528 if (!heap()->linear_allocation()) {
John Reck59135872010-11-02 12:39:01 -07002529 Object* result;
2530 MaybeObject* maybe = free_list_.Allocate();
2531 if (maybe->ToObject(&result)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002532 accounting_stats_.AllocateBytes(size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002533 HeapObject* obj = HeapObject::cast(result);
2534 Page* p = Page::FromAddress(obj->address());
2535
2536 if (obj->address() >= p->AllocationWatermark()) {
2537 // There should be no hole between the allocation watermark
2538 // and allocated object address.
2539 // Memory above the allocation watermark was not swept and
2540 // might contain garbage pointers to new space.
2541 ASSERT(obj->address() == p->AllocationWatermark());
2542 p->SetAllocationWatermark(obj->address() + size_in_bytes);
2543 }
2544
2545 return obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00002546 }
2547 }
2548
2549 // Free list allocation failed and there is no next page. Fail if we have
2550 // hit the old generation size limit that should cause a garbage
2551 // collection.
Steve Block44f0eee2011-05-26 01:26:41 +01002552 if (!heap()->always_allocate() &&
2553 heap()->OldGenerationAllocationLimitReached()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002554 return NULL;
2555 }
2556
2557 // Try to expand the space and allocate in the new next page.
2558 ASSERT(!current_page->next_page()->is_valid());
2559 if (Expand(current_page)) {
2560 return AllocateInNextPage(current_page, size_in_bytes);
2561 }
2562
2563 // Finally, fail.
2564 return NULL;
2565}
2566
2567
2568// Move to the next page (there is assumed to be one) and allocate there.
2569// The top of page block is always wasted, because it is too small to hold a
2570// map.
2571HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2572 int size_in_bytes) {
2573 ASSERT(current_page->next_page()->is_valid());
Steve Block6ded16b2010-05-10 14:33:55 +01002574 ASSERT(allocation_info_.top == PageAllocationLimit(current_page));
Steve Blocka7e24c12009-10-30 11:49:00 +00002575 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002576 Page* next_page = current_page->next_page();
2577 next_page->ClearGCFields();
2578 current_page->SetAllocationWatermark(allocation_info_.top);
Steve Blocka7e24c12009-10-30 11:49:00 +00002579 accounting_stats_.WasteBytes(page_extra_);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002580 SetAllocationInfo(&allocation_info_, next_page);
Steve Blocka7e24c12009-10-30 11:49:00 +00002581 return AllocateLinearly(&allocation_info_, size_in_bytes);
2582}
2583
2584
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002585void FixedSpace::DeallocateBlock(Address start,
2586 int size_in_bytes,
2587 bool add_to_freelist) {
2588 // Free-list elements in fixed space are assumed to have a fixed size.
2589 // We break the free block into chunks and add them to the free list
2590 // individually.
2591 int size = object_size_in_bytes();
2592 ASSERT(size_in_bytes % size == 0);
2593 Address end = start + size_in_bytes;
2594 for (Address a = start; a < end; a += size) {
2595 Free(a, add_to_freelist);
2596 }
2597}
2598
2599
Steve Blocka7e24c12009-10-30 11:49:00 +00002600#ifdef DEBUG
2601void FixedSpace::ReportStatistics() {
Ben Murdochf87a2032010-10-22 12:50:53 +01002602 int pct = static_cast<int>(Available() * 100 / Capacity());
2603 PrintF(" capacity: %" V8_PTR_PREFIX "d"
2604 ", waste: %" V8_PTR_PREFIX "d"
2605 ", available: %" V8_PTR_PREFIX "d, %%%d\n",
Steve Blocka7e24c12009-10-30 11:49:00 +00002606 Capacity(), Waste(), Available(), pct);
2607
Steve Blocka7e24c12009-10-30 11:49:00 +00002608 ClearHistograms();
2609 HeapObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002610 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2611 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002612 ReportHistogram(false);
2613}
Steve Blocka7e24c12009-10-30 11:49:00 +00002614#endif
2615
2616
2617// -----------------------------------------------------------------------------
2618// MapSpace implementation
2619
2620void MapSpace::PrepareForMarkCompact(bool will_compact) {
2621 // Call prepare of the super class.
2622 FixedSpace::PrepareForMarkCompact(will_compact);
2623
2624 if (will_compact) {
2625 // Initialize map index entry.
2626 int page_count = 0;
2627 PageIterator it(this, PageIterator::ALL_PAGES);
2628 while (it.has_next()) {
2629 ASSERT_MAP_PAGE_INDEX(page_count);
2630
2631 Page* p = it.next();
2632 ASSERT(p->mc_page_index == page_count);
2633
2634 page_addresses_[page_count++] = p->address();
2635 }
2636 }
2637}
2638
2639
2640#ifdef DEBUG
2641void MapSpace::VerifyObject(HeapObject* object) {
2642 // The object should be a map or a free-list node.
2643 ASSERT(object->IsMap() || object->IsByteArray());
2644}
2645#endif
2646
2647
2648// -----------------------------------------------------------------------------
2649// GlobalPropertyCellSpace implementation
2650
2651#ifdef DEBUG
2652void CellSpace::VerifyObject(HeapObject* object) {
2653 // The object should be a global object property cell or a free-list node.
2654 ASSERT(object->IsJSGlobalPropertyCell() ||
Steve Block44f0eee2011-05-26 01:26:41 +01002655 object->map() == heap()->two_pointer_filler_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00002656}
2657#endif
2658
2659
2660// -----------------------------------------------------------------------------
2661// LargeObjectIterator
2662
2663LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2664 current_ = space->first_chunk_;
2665 size_func_ = NULL;
2666}
2667
2668
2669LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2670 HeapObjectCallback size_func) {
2671 current_ = space->first_chunk_;
2672 size_func_ = size_func;
2673}
2674
2675
2676HeapObject* LargeObjectIterator::next() {
Leon Clarked91b9f72010-01-27 17:25:45 +00002677 if (current_ == NULL) return NULL;
2678
Steve Blocka7e24c12009-10-30 11:49:00 +00002679 HeapObject* object = current_->GetObject();
2680 current_ = current_->next();
2681 return object;
2682}
2683
2684
2685// -----------------------------------------------------------------------------
2686// LargeObjectChunk
2687
2688LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
Steve Blocka7e24c12009-10-30 11:49:00 +00002689 Executability executable) {
2690 size_t requested = ChunkSizeFor(size_in_bytes);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002691 size_t size;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002692 size_t guard_size = (executable == EXECUTABLE) ? Page::kPageSize : 0;
Steve Block44f0eee2011-05-26 01:26:41 +01002693 Isolate* isolate = Isolate::Current();
2694 void* mem = isolate->memory_allocator()->AllocateRawMemory(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002695 requested + guard_size, &size, executable);
Steve Blocka7e24c12009-10-30 11:49:00 +00002696 if (mem == NULL) return NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002697
2698 // The start of the chunk may be overlayed with a page so we have to
2699 // make sure that the page flags fit in the size field.
2700 ASSERT((size & Page::kPageFlagMask) == 0);
2701
Steve Block44f0eee2011-05-26 01:26:41 +01002702 LOG(isolate, NewEvent("LargeObjectChunk", mem, size));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002703 if (size < requested + guard_size) {
Steve Block44f0eee2011-05-26 01:26:41 +01002704 isolate->memory_allocator()->FreeRawMemory(
2705 mem, size, executable);
2706 LOG(isolate, DeleteEvent("LargeObjectChunk", mem));
Steve Blocka7e24c12009-10-30 11:49:00 +00002707 return NULL;
2708 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002709
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002710 if (guard_size != 0) {
2711 OS::Guard(mem, guard_size);
2712 size -= guard_size;
2713 mem = static_cast<Address>(mem) + guard_size;
2714 }
2715
Ben Murdochb0fe1622011-05-05 13:52:32 +01002716 ObjectSpace space = (executable == EXECUTABLE)
2717 ? kObjectSpaceCodeSpace
2718 : kObjectSpaceLoSpace;
Steve Block44f0eee2011-05-26 01:26:41 +01002719 isolate->memory_allocator()->PerformAllocationCallback(
Ben Murdochb0fe1622011-05-05 13:52:32 +01002720 space, kAllocationActionAllocate, size);
2721
2722 LargeObjectChunk* chunk = reinterpret_cast<LargeObjectChunk*>(mem);
2723 chunk->size_ = size;
Steve Block44f0eee2011-05-26 01:26:41 +01002724 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2725 page->heap_ = isolate->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002726 return chunk;
Steve Blocka7e24c12009-10-30 11:49:00 +00002727}
2728
2729
2730int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
Steve Blockd0582a62009-12-15 09:54:21 +00002731 int os_alignment = static_cast<int>(OS::AllocateAlignment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002732 if (os_alignment < Page::kPageSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002733 size_in_bytes += (Page::kPageSize - os_alignment);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002734 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002735 return size_in_bytes + Page::kObjectStartOffset;
2736}
2737
2738// -----------------------------------------------------------------------------
2739// LargeObjectSpace
2740
Steve Block44f0eee2011-05-26 01:26:41 +01002741LargeObjectSpace::LargeObjectSpace(Heap* heap, AllocationSpace id)
2742 : Space(heap, id, NOT_EXECUTABLE), // Managed on a per-allocation basis
Steve Blocka7e24c12009-10-30 11:49:00 +00002743 first_chunk_(NULL),
2744 size_(0),
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002745 page_count_(0),
2746 objects_size_(0) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002747
2748
2749bool LargeObjectSpace::Setup() {
2750 first_chunk_ = NULL;
2751 size_ = 0;
2752 page_count_ = 0;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002753 objects_size_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002754 return true;
2755}
2756
2757
2758void LargeObjectSpace::TearDown() {
2759 while (first_chunk_ != NULL) {
2760 LargeObjectChunk* chunk = first_chunk_;
2761 first_chunk_ = first_chunk_->next();
Steve Block44f0eee2011-05-26 01:26:41 +01002762 LOG(heap()->isolate(), DeleteEvent("LargeObjectChunk", chunk->address()));
Steve Block791712a2010-08-27 10:21:07 +01002763 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2764 Executability executable =
2765 page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE;
Iain Merrick9ac36c92010-09-13 15:29:50 +01002766 ObjectSpace space = kObjectSpaceLoSpace;
2767 if (executable == EXECUTABLE) space = kObjectSpaceCodeSpace;
2768 size_t size = chunk->size();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002769 size_t guard_size = (executable == EXECUTABLE) ? Page::kPageSize : 0;
2770 heap()->isolate()->memory_allocator()->FreeRawMemory(
2771 chunk->address() - guard_size,
2772 size + guard_size,
2773 executable);
Steve Block44f0eee2011-05-26 01:26:41 +01002774 heap()->isolate()->memory_allocator()->PerformAllocationCallback(
Iain Merrick9ac36c92010-09-13 15:29:50 +01002775 space, kAllocationActionFree, size);
Steve Blocka7e24c12009-10-30 11:49:00 +00002776 }
2777
2778 size_ = 0;
2779 page_count_ = 0;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002780 objects_size_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002781}
2782
2783
John Reck59135872010-11-02 12:39:01 -07002784MaybeObject* LargeObjectSpace::AllocateRawInternal(int requested_size,
2785 int object_size,
2786 Executability executable) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002787 ASSERT(0 < object_size && object_size <= requested_size);
2788
2789 // Check if we want to force a GC before growing the old space further.
2790 // If so, fail the allocation.
Steve Block44f0eee2011-05-26 01:26:41 +01002791 if (!heap()->always_allocate() &&
2792 heap()->OldGenerationAllocationLimitReached()) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002793 return Failure::RetryAfterGC(identity());
Steve Blocka7e24c12009-10-30 11:49:00 +00002794 }
2795
Ben Murdochb0fe1622011-05-05 13:52:32 +01002796 LargeObjectChunk* chunk = LargeObjectChunk::New(requested_size, executable);
Steve Blocka7e24c12009-10-30 11:49:00 +00002797 if (chunk == NULL) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002798 return Failure::RetryAfterGC(identity());
Steve Blocka7e24c12009-10-30 11:49:00 +00002799 }
2800
Ben Murdochb0fe1622011-05-05 13:52:32 +01002801 size_ += static_cast<int>(chunk->size());
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002802 objects_size_ += requested_size;
Steve Blocka7e24c12009-10-30 11:49:00 +00002803 page_count_++;
2804 chunk->set_next(first_chunk_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002805 first_chunk_ = chunk;
2806
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002807 // Initialize page header.
Steve Blocka7e24c12009-10-30 11:49:00 +00002808 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2809 Address object_address = page->ObjectAreaStart();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002810
Steve Blocka7e24c12009-10-30 11:49:00 +00002811 // Clear the low order bit of the second word in the page to flag it as a
2812 // large object page. If the chunk_size happened to be written there, its
2813 // low order bit should already be clear.
Steve Block6ded16b2010-05-10 14:33:55 +01002814 page->SetIsLargeObjectPage(true);
Steve Block791712a2010-08-27 10:21:07 +01002815 page->SetIsPageExecutable(executable);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002816 page->SetRegionMarks(Page::kAllRegionsCleanMarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00002817 return HeapObject::FromAddress(object_address);
2818}
2819
2820
John Reck59135872010-11-02 12:39:01 -07002821MaybeObject* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002822 ASSERT(0 < size_in_bytes);
2823 return AllocateRawInternal(size_in_bytes,
2824 size_in_bytes,
2825 EXECUTABLE);
2826}
2827
2828
John Reck59135872010-11-02 12:39:01 -07002829MaybeObject* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002830 ASSERT(0 < size_in_bytes);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002831 return AllocateRawInternal(size_in_bytes,
Steve Blocka7e24c12009-10-30 11:49:00 +00002832 size_in_bytes,
2833 NOT_EXECUTABLE);
2834}
2835
2836
John Reck59135872010-11-02 12:39:01 -07002837MaybeObject* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002838 ASSERT(0 < size_in_bytes);
2839 return AllocateRawInternal(size_in_bytes,
2840 size_in_bytes,
2841 NOT_EXECUTABLE);
2842}
2843
2844
2845// GC support
John Reck59135872010-11-02 12:39:01 -07002846MaybeObject* LargeObjectSpace::FindObject(Address a) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002847 for (LargeObjectChunk* chunk = first_chunk_;
2848 chunk != NULL;
2849 chunk = chunk->next()) {
2850 Address chunk_address = chunk->address();
2851 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2852 return chunk->GetObject();
2853 }
2854 }
2855 return Failure::Exception();
2856}
2857
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002858
2859LargeObjectChunk* LargeObjectSpace::FindChunkContainingPc(Address pc) {
2860 // TODO(853): Change this implementation to only find executable
2861 // chunks and use some kind of hash-based approach to speed it up.
2862 for (LargeObjectChunk* chunk = first_chunk_;
2863 chunk != NULL;
2864 chunk = chunk->next()) {
2865 Address chunk_address = chunk->address();
2866 if (chunk_address <= pc && pc < chunk_address + chunk->size()) {
2867 return chunk;
2868 }
2869 }
2870 return NULL;
2871}
2872
2873
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002874void LargeObjectSpace::IterateDirtyRegions(ObjectSlotCallback copy_object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002875 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00002876 for (HeapObject* object = it.next(); object != NULL; object = it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002877 // We only have code, sequential strings, or fixed arrays in large
2878 // object space, and only fixed arrays can possibly contain pointers to
2879 // the young generation.
Steve Blocka7e24c12009-10-30 11:49:00 +00002880 if (object->IsFixedArray()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002881 Page* page = Page::FromAddress(object->address());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002882 uint32_t marks = page->GetRegionMarks();
2883 uint32_t newmarks = Page::kAllRegionsCleanMarks;
Steve Blocka7e24c12009-10-30 11:49:00 +00002884
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002885 if (marks != Page::kAllRegionsCleanMarks) {
2886 // For a large page a single dirty mark corresponds to several
2887 // regions (modulo 32). So we treat a large page as a sequence of
2888 // normal pages of size Page::kPageSize having same dirty marks
2889 // and subsequently iterate dirty regions on each of these pages.
2890 Address start = object->address();
2891 Address end = page->ObjectAreaEnd();
2892 Address object_end = start + object->Size();
2893
2894 // Iterate regions of the first normal page covering object.
2895 uint32_t first_region_number = page->GetRegionNumberForAddress(start);
2896 newmarks |=
Steve Block44f0eee2011-05-26 01:26:41 +01002897 heap()->IterateDirtyRegions(marks >> first_region_number,
2898 start,
2899 end,
2900 &Heap::IteratePointersInDirtyRegion,
2901 copy_object) << first_region_number;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002902
2903 start = end;
2904 end = start + Page::kPageSize;
2905 while (end <= object_end) {
2906 // Iterate next 32 regions.
2907 newmarks |=
Steve Block44f0eee2011-05-26 01:26:41 +01002908 heap()->IterateDirtyRegions(marks,
2909 start,
2910 end,
2911 &Heap::IteratePointersInDirtyRegion,
2912 copy_object);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002913 start = end;
2914 end = start + Page::kPageSize;
2915 }
2916
2917 if (start != object_end) {
2918 // Iterate the last piece of an object which is less than
2919 // Page::kPageSize.
2920 newmarks |=
Steve Block44f0eee2011-05-26 01:26:41 +01002921 heap()->IterateDirtyRegions(marks,
2922 start,
2923 object_end,
2924 &Heap::IteratePointersInDirtyRegion,
2925 copy_object);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002926 }
2927
2928 page->SetRegionMarks(newmarks);
Steve Blocka7e24c12009-10-30 11:49:00 +00002929 }
2930 }
2931 }
2932}
2933
2934
2935void LargeObjectSpace::FreeUnmarkedObjects() {
2936 LargeObjectChunk* previous = NULL;
2937 LargeObjectChunk* current = first_chunk_;
2938 while (current != NULL) {
2939 HeapObject* object = current->GetObject();
2940 if (object->IsMarked()) {
2941 object->ClearMark();
Steve Block44f0eee2011-05-26 01:26:41 +01002942 heap()->mark_compact_collector()->tracer()->decrement_marked_count();
Steve Blocka7e24c12009-10-30 11:49:00 +00002943 previous = current;
2944 current = current->next();
2945 } else {
Steve Block791712a2010-08-27 10:21:07 +01002946 Page* page = Page::FromAddress(RoundUp(current->address(),
2947 Page::kPageSize));
2948 Executability executable =
2949 page->IsPageExecutable() ? EXECUTABLE : NOT_EXECUTABLE;
Steve Blocka7e24c12009-10-30 11:49:00 +00002950 Address chunk_address = current->address();
2951 size_t chunk_size = current->size();
2952
2953 // Cut the chunk out from the chunk list.
2954 current = current->next();
2955 if (previous == NULL) {
2956 first_chunk_ = current;
2957 } else {
2958 previous->set_next(current);
2959 }
2960
2961 // Free the chunk.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002962 heap()->mark_compact_collector()->ReportDeleteIfNeeded(
2963 object, heap()->isolate());
Steve Block1e0659c2011-05-24 12:43:12 +01002964 LiveObjectList::ProcessNonLive(object);
2965
Steve Blockd0582a62009-12-15 09:54:21 +00002966 size_ -= static_cast<int>(chunk_size);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002967 objects_size_ -= object->Size();
Steve Blocka7e24c12009-10-30 11:49:00 +00002968 page_count_--;
Iain Merrick9ac36c92010-09-13 15:29:50 +01002969 ObjectSpace space = kObjectSpaceLoSpace;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002970 size_t guard_size = 0;
2971 if (executable == EXECUTABLE) {
2972 space = kObjectSpaceCodeSpace;
2973 guard_size = Page::kPageSize;
2974 }
2975 heap()->isolate()->memory_allocator()->FreeRawMemory(
2976 chunk_address - guard_size,
2977 chunk_size + guard_size,
2978 executable);
Steve Block44f0eee2011-05-26 01:26:41 +01002979 heap()->isolate()->memory_allocator()->PerformAllocationCallback(
2980 space, kAllocationActionFree, size_);
2981 LOG(heap()->isolate(), DeleteEvent("LargeObjectChunk", chunk_address));
Steve Blocka7e24c12009-10-30 11:49:00 +00002982 }
2983 }
2984}
2985
2986
2987bool LargeObjectSpace::Contains(HeapObject* object) {
2988 Address address = object->address();
Steve Block44f0eee2011-05-26 01:26:41 +01002989 if (heap()->new_space()->Contains(address)) {
Steve Block6ded16b2010-05-10 14:33:55 +01002990 return false;
2991 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002992 Page* page = Page::FromAddress(address);
2993
2994 SLOW_ASSERT(!page->IsLargeObjectPage()
2995 || !FindObject(address)->IsFailure());
2996
2997 return page->IsLargeObjectPage();
2998}
2999
3000
3001#ifdef DEBUG
3002// We do not assume that the large object iterator works, because it depends
3003// on the invariants we are checking during verification.
3004void LargeObjectSpace::Verify() {
3005 for (LargeObjectChunk* chunk = first_chunk_;
3006 chunk != NULL;
3007 chunk = chunk->next()) {
3008 // Each chunk contains an object that starts at the large object page's
3009 // object area start.
3010 HeapObject* object = chunk->GetObject();
3011 Page* page = Page::FromAddress(object->address());
3012 ASSERT(object->address() == page->ObjectAreaStart());
3013
3014 // The first word should be a map, and we expect all map pointers to be
3015 // in map space.
3016 Map* map = object->map();
3017 ASSERT(map->IsMap());
Steve Block44f0eee2011-05-26 01:26:41 +01003018 ASSERT(heap()->map_space()->Contains(map));
Steve Blocka7e24c12009-10-30 11:49:00 +00003019
3020 // We have only code, sequential strings, external strings
3021 // (sequential strings that have been morphed into external
3022 // strings), fixed arrays, and byte arrays in large object space.
3023 ASSERT(object->IsCode() || object->IsSeqString() ||
3024 object->IsExternalString() || object->IsFixedArray() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003025 object->IsFixedDoubleArray() || object->IsByteArray());
Steve Blocka7e24c12009-10-30 11:49:00 +00003026
3027 // The object itself should look OK.
3028 object->Verify();
3029
3030 // Byte arrays and strings don't have interior pointers.
3031 if (object->IsCode()) {
3032 VerifyPointersVisitor code_visitor;
3033 object->IterateBody(map->instance_type(),
3034 object->Size(),
3035 &code_visitor);
3036 } else if (object->IsFixedArray()) {
3037 // We loop over fixed arrays ourselves, rather then using the visitor,
3038 // because the visitor doesn't support the start/offset iteration
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003039 // needed for IsRegionDirty.
Steve Blocka7e24c12009-10-30 11:49:00 +00003040 FixedArray* array = FixedArray::cast(object);
3041 for (int j = 0; j < array->length(); j++) {
3042 Object* element = array->get(j);
3043 if (element->IsHeapObject()) {
3044 HeapObject* element_object = HeapObject::cast(element);
Steve Block44f0eee2011-05-26 01:26:41 +01003045 ASSERT(heap()->Contains(element_object));
Steve Blocka7e24c12009-10-30 11:49:00 +00003046 ASSERT(element_object->map()->IsMap());
Steve Block44f0eee2011-05-26 01:26:41 +01003047 if (heap()->InNewSpace(element_object)) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003048 Address array_addr = object->address();
3049 Address element_addr = array_addr + FixedArray::kHeaderSize +
3050 j * kPointerSize;
3051
3052 ASSERT(Page::FromAddress(array_addr)->IsRegionDirty(element_addr));
Steve Blocka7e24c12009-10-30 11:49:00 +00003053 }
3054 }
3055 }
3056 }
3057 }
3058}
3059
3060
3061void LargeObjectSpace::Print() {
3062 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00003063 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
3064 obj->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +00003065 }
3066}
3067
3068
3069void LargeObjectSpace::ReportStatistics() {
Ben Murdochf87a2032010-10-22 12:50:53 +01003070 PrintF(" size: %" V8_PTR_PREFIX "d\n", size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003071 int num_objects = 0;
3072 ClearHistograms();
3073 LargeObjectIterator it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00003074 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003075 num_objects++;
Leon Clarked91b9f72010-01-27 17:25:45 +00003076 CollectHistogramInfo(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003077 }
3078
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003079 PrintF(" number of objects %d, "
3080 "size of objects %" V8_PTR_PREFIX "d\n", num_objects, objects_size_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003081 if (num_objects > 0) ReportHistogram(false);
3082}
3083
3084
3085void LargeObjectSpace::CollectCodeStatistics() {
Steve Block44f0eee2011-05-26 01:26:41 +01003086 Isolate* isolate = heap()->isolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00003087 LargeObjectIterator obj_it(this);
Leon Clarked91b9f72010-01-27 17:25:45 +00003088 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003089 if (obj->IsCode()) {
3090 Code* code = Code::cast(obj);
Steve Block44f0eee2011-05-26 01:26:41 +01003091 isolate->code_kind_statistics()[code->kind()] += code->Size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003092 }
3093 }
3094}
Steve Blocka7e24c12009-10-30 11:49:00 +00003095#endif // DEBUG
3096
3097} } // namespace v8::internal