blob: 5f191ed3a55c003dca4785d3ea04216f3c18058e [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "macro-assembler.h"
31#include "mark-compact.h"
32#include "platform.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037// For contiguous spaces, top should be in the space (or at the end) and limit
38// should be the end of the space.
39#define ASSERT_SEMISPACE_ALLOCATION_INFO(info, space) \
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000040 ASSERT((space).low() <= (info).top \
41 && (info).top <= (space).high() \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000042 && (info).limit == (space).high())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
45// ----------------------------------------------------------------------------
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
70void HeapObjectIterator::Initialize(Address cur, Address end,
71 HeapObjectCallback size_f) {
72 cur_addr_ = cur;
73 end_addr_ = end;
74 end_page_ = Page::FromAllocationTop(end);
75 size_func_ = size_f;
76 Page* p = Page::FromAllocationTop(cur_addr_);
77 cur_limit_ = (p == end_page_) ? end_addr_ : p->AllocationTop();
78
79#ifdef DEBUG
80 Verify();
81#endif
82}
83
84
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000085HeapObject* HeapObjectIterator::FromNextPage() {
86 if (cur_addr_ == end_addr_) return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087
88 Page* cur_page = Page::FromAllocationTop(cur_addr_);
89 cur_page = cur_page->next_page();
90 ASSERT(cur_page->is_valid());
91
92 cur_addr_ = cur_page->ObjectAreaStart();
93 cur_limit_ = (cur_page == end_page_) ? end_addr_ : cur_page->AllocationTop();
94
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000095 if (cur_addr_ == end_addr_) return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 ASSERT(cur_addr_ < cur_limit_);
97#ifdef DEBUG
98 Verify();
99#endif
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000100 return FromCurrentPage();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101}
102
103
104#ifdef DEBUG
105void HeapObjectIterator::Verify() {
106 Page* p = Page::FromAllocationTop(cur_addr_);
107 ASSERT(p == Page::FromAllocationTop(cur_limit_));
108 ASSERT(p->Offset(cur_addr_) <= p->Offset(cur_limit_));
109}
110#endif
111
112
113// -----------------------------------------------------------------------------
114// PageIterator
115
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000116PageIterator::PageIterator(PagedSpace* space, Mode mode) : space_(space) {
117 prev_page_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 switch (mode) {
119 case PAGES_IN_USE:
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000120 stop_page_ = space->AllocationTopPage();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 break;
122 case PAGES_USED_BY_MC:
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000123 stop_page_ = space->MCRelocationTopPage();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 break;
125 case ALL_PAGES:
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000126#ifdef DEBUG
127 // Verify that the cached last page in the space is actually the
128 // last page.
129 for (Page* p = space->first_page_; p->is_valid(); p = p->next_page()) {
130 if (!p->next_page()->is_valid()) {
131 ASSERT(space->last_page_ == p);
132 }
133 }
134#endif
135 stop_page_ = space->last_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 }
138}
139
140
141// -----------------------------------------------------------------------------
142// Page
143
144#ifdef DEBUG
145Page::RSetState Page::rset_state_ = Page::IN_USE;
146#endif
147
148// -----------------------------------------------------------------------------
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000149// CodeRange
150
151List<CodeRange::FreeBlock> CodeRange::free_list_(0);
152List<CodeRange::FreeBlock> CodeRange::allocation_list_(0);
153int CodeRange::current_allocation_block_index_ = 0;
154VirtualMemory* CodeRange::code_range_ = NULL;
155
156
157bool CodeRange::Setup(const size_t requested) {
158 ASSERT(code_range_ == NULL);
159
160 code_range_ = new VirtualMemory(requested);
161 CHECK(code_range_ != NULL);
162 if (!code_range_->IsReserved()) {
163 delete code_range_;
164 code_range_ = NULL;
165 return false;
166 }
167
168 // We are sure that we have mapped a block of requested addresses.
169 ASSERT(code_range_->size() == requested);
170 LOG(NewEvent("CodeRange", code_range_->address(), requested));
171 allocation_list_.Add(FreeBlock(code_range_->address(), code_range_->size()));
172 current_allocation_block_index_ = 0;
173 return true;
174}
175
176
177int CodeRange::CompareFreeBlockAddress(const FreeBlock* left,
178 const FreeBlock* right) {
179 // The entire point of CodeRange is that the difference between two
180 // addresses in the range can be represented as a signed 32-bit int,
181 // so the cast is semantically correct.
182 return static_cast<int>(left->start - right->start);
183}
184
185
186void CodeRange::GetNextAllocationBlock(size_t requested) {
187 for (current_allocation_block_index_++;
188 current_allocation_block_index_ < allocation_list_.length();
189 current_allocation_block_index_++) {
190 if (requested <= allocation_list_[current_allocation_block_index_].size) {
191 return; // Found a large enough allocation block.
192 }
193 }
194
195 // Sort and merge the free blocks on the free list and the allocation list.
196 free_list_.AddAll(allocation_list_);
197 allocation_list_.Clear();
198 free_list_.Sort(&CompareFreeBlockAddress);
199 for (int i = 0; i < free_list_.length();) {
200 FreeBlock merged = free_list_[i];
201 i++;
202 // Add adjacent free blocks to the current merged block.
203 while (i < free_list_.length() &&
204 free_list_[i].start == merged.start + merged.size) {
205 merged.size += free_list_[i].size;
206 i++;
207 }
208 if (merged.size > 0) {
209 allocation_list_.Add(merged);
210 }
211 }
212 free_list_.Clear();
213
214 for (current_allocation_block_index_ = 0;
215 current_allocation_block_index_ < allocation_list_.length();
216 current_allocation_block_index_++) {
217 if (requested <= allocation_list_[current_allocation_block_index_].size) {
218 return; // Found a large enough allocation block.
219 }
220 }
221
222 // Code range is full or too fragmented.
223 V8::FatalProcessOutOfMemory("CodeRange::GetNextAllocationBlock");
224}
225
226
227
228void* CodeRange::AllocateRawMemory(const size_t requested, size_t* allocated) {
229 ASSERT(current_allocation_block_index_ < allocation_list_.length());
230 if (requested > allocation_list_[current_allocation_block_index_].size) {
231 // Find an allocation block large enough. This function call may
232 // call V8::FatalProcessOutOfMemory if it cannot find a large enough block.
233 GetNextAllocationBlock(requested);
234 }
235 // Commit the requested memory at the start of the current allocation block.
236 *allocated = RoundUp(requested, Page::kPageSize);
237 FreeBlock current = allocation_list_[current_allocation_block_index_];
238 if (*allocated >= current.size - Page::kPageSize) {
239 // Don't leave a small free block, useless for a large object or chunk.
240 *allocated = current.size;
241 }
242 ASSERT(*allocated <= current.size);
243 if (!code_range_->Commit(current.start, *allocated, true)) {
244 *allocated = 0;
245 return NULL;
246 }
247 allocation_list_[current_allocation_block_index_].start += *allocated;
248 allocation_list_[current_allocation_block_index_].size -= *allocated;
249 if (*allocated == current.size) {
250 GetNextAllocationBlock(0); // This block is used up, get the next one.
251 }
252 return current.start;
253}
254
255
256void CodeRange::FreeRawMemory(void* address, size_t length) {
257 free_list_.Add(FreeBlock(address, length));
258 code_range_->Uncommit(address, length);
259}
260
261
262void CodeRange::TearDown() {
263 delete code_range_; // Frees all memory in the virtual memory range.
264 code_range_ = NULL;
265 free_list_.Free();
266 allocation_list_.Free();
267}
268
269
270// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271// MemoryAllocator
272//
273int MemoryAllocator::capacity_ = 0;
274int MemoryAllocator::size_ = 0;
275
276VirtualMemory* MemoryAllocator::initial_chunk_ = NULL;
277
278// 270 is an estimate based on the static default heap size of a pair of 256K
279// semispaces and a 64M old generation.
280const int kEstimatedNumberOfChunks = 270;
281List<MemoryAllocator::ChunkInfo> MemoryAllocator::chunks_(
282 kEstimatedNumberOfChunks);
283List<int> MemoryAllocator::free_chunk_ids_(kEstimatedNumberOfChunks);
284int MemoryAllocator::max_nof_chunks_ = 0;
285int MemoryAllocator::top_ = 0;
286
287
288void MemoryAllocator::Push(int free_chunk_id) {
289 ASSERT(max_nof_chunks_ > 0);
290 ASSERT(top_ < max_nof_chunks_);
291 free_chunk_ids_[top_++] = free_chunk_id;
292}
293
294
295int MemoryAllocator::Pop() {
296 ASSERT(top_ > 0);
297 return free_chunk_ids_[--top_];
298}
299
300
301bool MemoryAllocator::Setup(int capacity) {
302 capacity_ = RoundUp(capacity, Page::kPageSize);
303
304 // Over-estimate the size of chunks_ array. It assumes the expansion of old
305 // space is always in the unit of a chunk (kChunkSize) except the last
306 // expansion.
307 //
308 // Due to alignment, allocated space might be one page less than required
309 // number (kPagesPerChunk) of pages for old spaces.
310 //
kasper.lund7276f142008-07-30 08:49:36 +0000311 // Reserve two chunk ids for semispaces, one for map space, one for old
312 // space, and one for code space.
313 max_nof_chunks_ = (capacity_ / (kChunkSize - Page::kPageSize)) + 5;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 if (max_nof_chunks_ > kMaxNofChunks) return false;
315
316 size_ = 0;
317 ChunkInfo info; // uninitialized element.
318 for (int i = max_nof_chunks_ - 1; i >= 0; i--) {
319 chunks_.Add(info);
320 free_chunk_ids_.Add(i);
321 }
322 top_ = max_nof_chunks_;
323 return true;
324}
325
326
327void MemoryAllocator::TearDown() {
328 for (int i = 0; i < max_nof_chunks_; i++) {
329 if (chunks_[i].address() != NULL) DeleteChunk(i);
330 }
331 chunks_.Clear();
332 free_chunk_ids_.Clear();
333
334 if (initial_chunk_ != NULL) {
335 LOG(DeleteEvent("InitialChunk", initial_chunk_->address()));
336 delete initial_chunk_;
337 initial_chunk_ = NULL;
338 }
339
340 ASSERT(top_ == max_nof_chunks_); // all chunks are free
341 top_ = 0;
342 capacity_ = 0;
343 size_ = 0;
344 max_nof_chunks_ = 0;
345}
346
347
348void* MemoryAllocator::AllocateRawMemory(const size_t requested,
kasper.lund7276f142008-07-30 08:49:36 +0000349 size_t* allocated,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000350 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 if (size_ + static_cast<int>(requested) > capacity_) return NULL;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000352 void* mem;
353 if (executable == EXECUTABLE && CodeRange::exists()) {
354 mem = CodeRange::AllocateRawMemory(requested, allocated);
355 } else {
356 mem = OS::Allocate(requested, allocated, (executable == EXECUTABLE));
357 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000358 int alloced = static_cast<int>(*allocated);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 size_ += alloced;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000360#ifdef DEBUG
361 ZapBlock(reinterpret_cast<Address>(mem), alloced);
362#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 Counters::memory_allocated.Increment(alloced);
364 return mem;
365}
366
367
368void MemoryAllocator::FreeRawMemory(void* mem, size_t length) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000369#ifdef DEBUG
370 ZapBlock(reinterpret_cast<Address>(mem), length);
371#endif
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000372 if (CodeRange::contains(static_cast<Address>(mem))) {
373 CodeRange::FreeRawMemory(mem, length);
374 } else {
375 OS::Free(mem, length);
376 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000377 Counters::memory_allocated.Decrement(static_cast<int>(length));
378 size_ -= static_cast<int>(length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 ASSERT(size_ >= 0);
380}
381
382
383void* MemoryAllocator::ReserveInitialChunk(const size_t requested) {
384 ASSERT(initial_chunk_ == NULL);
385
386 initial_chunk_ = new VirtualMemory(requested);
387 CHECK(initial_chunk_ != NULL);
388 if (!initial_chunk_->IsReserved()) {
389 delete initial_chunk_;
390 initial_chunk_ = NULL;
391 return NULL;
392 }
393
394 // We are sure that we have mapped a block of requested addresses.
395 ASSERT(initial_chunk_->size() == requested);
396 LOG(NewEvent("InitialChunk", initial_chunk_->address(), requested));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000397 size_ += static_cast<int>(requested);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 return initial_chunk_->address();
399}
400
401
402static int PagesInChunk(Address start, size_t size) {
403 // The first page starts on the first page-aligned address from start onward
404 // and the last page ends on the last page-aligned address before
405 // start+size. Page::kPageSize is a power of two so we can divide by
406 // shifting.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000407 return static_cast<int>((RoundDown(start + size, Page::kPageSize)
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000408 - RoundUp(start, Page::kPageSize)) >> kPageSizeBits);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409}
410
411
412Page* MemoryAllocator::AllocatePages(int requested_pages, int* allocated_pages,
413 PagedSpace* owner) {
414 if (requested_pages <= 0) return Page::FromAddress(NULL);
415 size_t chunk_size = requested_pages * Page::kPageSize;
416
417 // There is not enough space to guarantee the desired number pages can be
418 // allocated.
419 if (size_ + static_cast<int>(chunk_size) > capacity_) {
420 // Request as many pages as we can.
421 chunk_size = capacity_ - size_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000422 requested_pages = static_cast<int>(chunk_size >> kPageSizeBits);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423
424 if (requested_pages <= 0) return Page::FromAddress(NULL);
425 }
kasper.lund7276f142008-07-30 08:49:36 +0000426 void* chunk = AllocateRawMemory(chunk_size, &chunk_size, owner->executable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 if (chunk == NULL) return Page::FromAddress(NULL);
428 LOG(NewEvent("PagedChunk", chunk, chunk_size));
429
430 *allocated_pages = PagesInChunk(static_cast<Address>(chunk), chunk_size);
431 if (*allocated_pages == 0) {
432 FreeRawMemory(chunk, chunk_size);
433 LOG(DeleteEvent("PagedChunk", chunk));
434 return Page::FromAddress(NULL);
435 }
436
437 int chunk_id = Pop();
438 chunks_[chunk_id].init(static_cast<Address>(chunk), chunk_size, owner);
439
440 return InitializePagesInChunk(chunk_id, *allocated_pages, owner);
441}
442
443
444Page* MemoryAllocator::CommitPages(Address start, size_t size,
445 PagedSpace* owner, int* num_pages) {
446 ASSERT(start != NULL);
447 *num_pages = PagesInChunk(start, size);
448 ASSERT(*num_pages > 0);
449 ASSERT(initial_chunk_ != NULL);
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000450 ASSERT(InInitialChunk(start));
451 ASSERT(InInitialChunk(start + size - 1));
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000452 if (!initial_chunk_->Commit(start, size, owner->executable() == EXECUTABLE)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 return Page::FromAddress(NULL);
454 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000455#ifdef DEBUG
456 ZapBlock(start, size);
457#endif
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000458 Counters::memory_allocated.Increment(static_cast<int>(size));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 // So long as we correctly overestimated the number of chunks we should not
461 // run out of chunk ids.
462 CHECK(!OutOfChunkIds());
463 int chunk_id = Pop();
464 chunks_[chunk_id].init(start, size, owner);
465 return InitializePagesInChunk(chunk_id, *num_pages, owner);
466}
467
468
kasper.lund7276f142008-07-30 08:49:36 +0000469bool MemoryAllocator::CommitBlock(Address start,
470 size_t size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000471 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 ASSERT(start != NULL);
473 ASSERT(size > 0);
474 ASSERT(initial_chunk_ != NULL);
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000475 ASSERT(InInitialChunk(start));
476 ASSERT(InInitialChunk(start + size - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477
kasper.lund7276f142008-07-30 08:49:36 +0000478 if (!initial_chunk_->Commit(start, size, executable)) return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000479#ifdef DEBUG
480 ZapBlock(start, size);
481#endif
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000482 Counters::memory_allocated.Increment(static_cast<int>(size));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 return true;
484}
485
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000486
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000487bool MemoryAllocator::UncommitBlock(Address start, size_t size) {
488 ASSERT(start != NULL);
489 ASSERT(size > 0);
490 ASSERT(initial_chunk_ != NULL);
491 ASSERT(InInitialChunk(start));
492 ASSERT(InInitialChunk(start + size - 1));
493
494 if (!initial_chunk_->Uncommit(start, size)) return false;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000495 Counters::memory_allocated.Decrement(static_cast<int>(size));
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000496 return true;
497}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000499
500void MemoryAllocator::ZapBlock(Address start, size_t size) {
501 for (size_t s = 0; s + kPointerSize <= size; s += kPointerSize) {
502 Memory::Address_at(start + s) = kZapValue;
503 }
504}
505
506
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507Page* MemoryAllocator::InitializePagesInChunk(int chunk_id, int pages_in_chunk,
508 PagedSpace* owner) {
509 ASSERT(IsValidChunk(chunk_id));
510 ASSERT(pages_in_chunk > 0);
511
512 Address chunk_start = chunks_[chunk_id].address();
513
514 Address low = RoundUp(chunk_start, Page::kPageSize);
515
516#ifdef DEBUG
517 size_t chunk_size = chunks_[chunk_id].size();
518 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
519 ASSERT(pages_in_chunk <=
520 ((OffsetFrom(high) - OffsetFrom(low)) / Page::kPageSize));
521#endif
522
523 Address page_addr = low;
524 for (int i = 0; i < pages_in_chunk; i++) {
525 Page* p = Page::FromAddress(page_addr);
526 p->opaque_header = OffsetFrom(page_addr + Page::kPageSize) | chunk_id;
527 p->is_normal_page = 1;
528 page_addr += Page::kPageSize;
529 }
530
531 // Set the next page of the last page to 0.
532 Page* last_page = Page::FromAddress(page_addr - Page::kPageSize);
533 last_page->opaque_header = OffsetFrom(0) | chunk_id;
534
535 return Page::FromAddress(low);
536}
537
538
539Page* MemoryAllocator::FreePages(Page* p) {
540 if (!p->is_valid()) return p;
541
542 // Find the first page in the same chunk as 'p'
543 Page* first_page = FindFirstPageInSameChunk(p);
544 Page* page_to_return = Page::FromAddress(NULL);
545
546 if (p != first_page) {
547 // Find the last page in the same chunk as 'prev'.
548 Page* last_page = FindLastPageInSameChunk(p);
549 first_page = GetNextPage(last_page); // first page in next chunk
550
551 // set the next_page of last_page to NULL
552 SetNextPage(last_page, Page::FromAddress(NULL));
553 page_to_return = p; // return 'p' when exiting
554 }
555
556 while (first_page->is_valid()) {
557 int chunk_id = GetChunkId(first_page);
558 ASSERT(IsValidChunk(chunk_id));
559
560 // Find the first page of the next chunk before deleting this chunk.
561 first_page = GetNextPage(FindLastPageInSameChunk(first_page));
562
563 // Free the current chunk.
564 DeleteChunk(chunk_id);
565 }
566
567 return page_to_return;
568}
569
570
571void MemoryAllocator::DeleteChunk(int chunk_id) {
572 ASSERT(IsValidChunk(chunk_id));
573
574 ChunkInfo& c = chunks_[chunk_id];
575
576 // We cannot free a chunk contained in the initial chunk because it was not
577 // allocated with AllocateRawMemory. Instead we uncommit the virtual
578 // memory.
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000579 if (InInitialChunk(c.address())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 // TODO(1240712): VirtualMemory::Uncommit has a return value which
581 // is ignored here.
582 initial_chunk_->Uncommit(c.address(), c.size());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000583 Counters::memory_allocated.Decrement(static_cast<int>(c.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 } else {
585 LOG(DeleteEvent("PagedChunk", c.address()));
586 FreeRawMemory(c.address(), c.size());
587 }
588 c.init(NULL, 0, NULL);
589 Push(chunk_id);
590}
591
592
593Page* MemoryAllocator::FindFirstPageInSameChunk(Page* p) {
594 int chunk_id = GetChunkId(p);
595 ASSERT(IsValidChunk(chunk_id));
596
597 Address low = RoundUp(chunks_[chunk_id].address(), Page::kPageSize);
598 return Page::FromAddress(low);
599}
600
601
602Page* MemoryAllocator::FindLastPageInSameChunk(Page* p) {
603 int chunk_id = GetChunkId(p);
604 ASSERT(IsValidChunk(chunk_id));
605
606 Address chunk_start = chunks_[chunk_id].address();
607 size_t chunk_size = chunks_[chunk_id].size();
608
609 Address high = RoundDown(chunk_start + chunk_size, Page::kPageSize);
610 ASSERT(chunk_start <= p->address() && p->address() < high);
611
612 return Page::FromAddress(high - Page::kPageSize);
613}
614
615
616#ifdef DEBUG
617void MemoryAllocator::ReportStatistics() {
618 float pct = static_cast<float>(capacity_ - size_) / capacity_;
619 PrintF(" capacity: %d, used: %d, available: %%%d\n\n",
620 capacity_, size_, static_cast<int>(pct*100));
621}
622#endif
623
624
625// -----------------------------------------------------------------------------
626// PagedSpace implementation
627
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000628PagedSpace::PagedSpace(int max_capacity,
629 AllocationSpace id,
630 Executability executable)
kasper.lund7276f142008-07-30 08:49:36 +0000631 : Space(id, executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 max_capacity_ = (RoundDown(max_capacity, Page::kPageSize) / Page::kPageSize)
633 * Page::kObjectAreaSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 accounting_stats_.Clear();
635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 allocation_info_.top = NULL;
637 allocation_info_.limit = NULL;
638
639 mc_forwarding_info_.top = NULL;
640 mc_forwarding_info_.limit = NULL;
641}
642
643
644bool PagedSpace::Setup(Address start, size_t size) {
645 if (HasBeenSetup()) return false;
646
647 int num_pages = 0;
648 // Try to use the virtual memory range passed to us. If it is too small to
649 // contain at least one page, ignore it and allocate instead.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000650 int pages_in_chunk = PagesInChunk(start, size);
651 if (pages_in_chunk > 0) {
652 first_page_ = MemoryAllocator::CommitPages(RoundUp(start, Page::kPageSize),
653 Page::kPageSize * pages_in_chunk,
654 this, &num_pages);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 } else {
656 int requested_pages = Min(MemoryAllocator::kPagesPerChunk,
657 max_capacity_ / Page::kObjectAreaSize);
658 first_page_ =
659 MemoryAllocator::AllocatePages(requested_pages, &num_pages, this);
660 if (!first_page_->is_valid()) return false;
661 }
662
663 // We are sure that the first page is valid and that we have at least one
664 // page.
665 ASSERT(first_page_->is_valid());
666 ASSERT(num_pages > 0);
667 accounting_stats_.ExpandSpace(num_pages * Page::kObjectAreaSize);
668 ASSERT(Capacity() <= max_capacity_);
669
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000670 // Sequentially initialize remembered sets in the newly allocated
671 // pages and cache the current last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
673 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000674 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 }
676
677 // Use first_page_ for allocation.
678 SetAllocationInfo(&allocation_info_, first_page_);
679
680 return true;
681}
682
683
684bool PagedSpace::HasBeenSetup() {
685 return (Capacity() > 0);
686}
687
688
689void PagedSpace::TearDown() {
690 first_page_ = MemoryAllocator::FreePages(first_page_);
691 ASSERT(!first_page_->is_valid());
692
693 accounting_stats_.Clear();
694}
695
696
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000697#ifdef ENABLE_HEAP_PROTECTION
698
699void PagedSpace::Protect() {
700 Page* page = first_page_;
701 while (page->is_valid()) {
702 MemoryAllocator::ProtectChunkFromPage(page);
703 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
704 }
705}
706
707
708void PagedSpace::Unprotect() {
709 Page* page = first_page_;
710 while (page->is_valid()) {
711 MemoryAllocator::UnprotectChunkFromPage(page);
712 page = MemoryAllocator::FindLastPageInSameChunk(page)->next_page();
713 }
714}
715
716#endif
717
718
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719void PagedSpace::ClearRSet() {
720 PageIterator it(this, PageIterator::ALL_PAGES);
721 while (it.has_next()) {
722 it.next()->ClearRSet();
723 }
724}
725
726
727Object* PagedSpace::FindObject(Address addr) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728 // Note: this function can only be called before or after mark-compact GC
729 // because it accesses map pointers.
730 ASSERT(!MarkCompactCollector::in_use());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731
732 if (!Contains(addr)) return Failure::Exception();
733
734 Page* p = Page::FromAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +0000735 ASSERT(IsUsed(p));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736 Address cur = p->ObjectAreaStart();
737 Address end = p->AllocationTop();
738 while (cur < end) {
739 HeapObject* obj = HeapObject::FromAddress(cur);
740 Address next = cur + obj->Size();
741 if ((cur <= addr) && (addr < next)) return obj;
742 cur = next;
743 }
744
kasper.lund7276f142008-07-30 08:49:36 +0000745 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 return Failure::Exception();
747}
748
749
kasper.lund7276f142008-07-30 08:49:36 +0000750bool PagedSpace::IsUsed(Page* page) {
751 PageIterator it(this, PageIterator::PAGES_IN_USE);
752 while (it.has_next()) {
753 if (page == it.next()) return true;
754 }
755 return false;
756}
757
758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759void PagedSpace::SetAllocationInfo(AllocationInfo* alloc_info, Page* p) {
760 alloc_info->top = p->ObjectAreaStart();
761 alloc_info->limit = p->ObjectAreaEnd();
kasper.lund7276f142008-07-30 08:49:36 +0000762 ASSERT(alloc_info->VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763}
764
765
766void PagedSpace::MCResetRelocationInfo() {
767 // Set page indexes.
768 int i = 0;
769 PageIterator it(this, PageIterator::ALL_PAGES);
770 while (it.has_next()) {
771 Page* p = it.next();
772 p->mc_page_index = i++;
773 }
774
775 // Set mc_forwarding_info_ to the first page in the space.
776 SetAllocationInfo(&mc_forwarding_info_, first_page_);
777 // All the bytes in the space are 'available'. We will rediscover
778 // allocated and wasted bytes during GC.
779 accounting_stats_.Reset();
780}
781
782
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783int PagedSpace::MCSpaceOffsetForAddress(Address addr) {
784#ifdef DEBUG
785 // The Contains function considers the address at the beginning of a
786 // page in the page, MCSpaceOffsetForAddress considers it is in the
787 // previous page.
788 if (Page::IsAlignedToPageSize(addr)) {
789 ASSERT(Contains(addr - kPointerSize));
790 } else {
791 ASSERT(Contains(addr));
792 }
793#endif
794
795 // If addr is at the end of a page, it belongs to previous page
796 Page* p = Page::IsAlignedToPageSize(addr)
797 ? Page::FromAllocationTop(addr)
798 : Page::FromAddress(addr);
799 int index = p->mc_page_index;
800 return (index * Page::kPageSize) + p->Offset(addr);
801}
802
803
kasper.lund7276f142008-07-30 08:49:36 +0000804// Slow case for reallocating and promoting objects during a compacting
805// collection. This function is not space-specific.
806HeapObject* PagedSpace::SlowMCAllocateRaw(int size_in_bytes) {
807 Page* current_page = TopPageOf(mc_forwarding_info_);
808 if (!current_page->next_page()->is_valid()) {
809 if (!Expand(current_page)) {
810 return NULL;
811 }
812 }
813
814 // There are surely more pages in the space now.
815 ASSERT(current_page->next_page()->is_valid());
816 // We do not add the top of page block for current page to the space's
817 // free list---the block may contain live objects so we cannot write
818 // bookkeeping information to it. Instead, we will recover top of page
819 // blocks when we move objects to their new locations.
820 //
821 // We do however write the allocation pointer to the page. The encoding
822 // of forwarding addresses is as an offset in terms of live bytes, so we
823 // need quick access to the allocation top of each page to decode
824 // forwarding addresses.
825 current_page->mc_relocation_top = mc_forwarding_info_.top;
826 SetAllocationInfo(&mc_forwarding_info_, current_page->next_page());
827 return AllocateLinearly(&mc_forwarding_info_, size_in_bytes);
828}
829
830
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831bool PagedSpace::Expand(Page* last_page) {
832 ASSERT(max_capacity_ % Page::kObjectAreaSize == 0);
833 ASSERT(Capacity() % Page::kObjectAreaSize == 0);
834
835 if (Capacity() == max_capacity_) return false;
836
837 ASSERT(Capacity() < max_capacity_);
838 // Last page must be valid and its next page is invalid.
839 ASSERT(last_page->is_valid() && !last_page->next_page()->is_valid());
840
841 int available_pages = (max_capacity_ - Capacity()) / Page::kObjectAreaSize;
842 if (available_pages <= 0) return false;
843
844 int desired_pages = Min(available_pages, MemoryAllocator::kPagesPerChunk);
845 Page* p = MemoryAllocator::AllocatePages(desired_pages, &desired_pages, this);
846 if (!p->is_valid()) return false;
847
848 accounting_stats_.ExpandSpace(desired_pages * Page::kObjectAreaSize);
849 ASSERT(Capacity() <= max_capacity_);
850
851 MemoryAllocator::SetNextPage(last_page, p);
852
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000853 // Sequentially clear remembered set of new pages and and cache the
854 // new last page in the space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 while (p->is_valid()) {
856 p->ClearRSet();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000857 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 p = p->next_page();
859 }
860
861 return true;
862}
863
864
865#ifdef DEBUG
866int PagedSpace::CountTotalPages() {
867 int count = 0;
868 for (Page* p = first_page_; p->is_valid(); p = p->next_page()) {
869 count++;
870 }
871 return count;
872}
873#endif
874
875
876void PagedSpace::Shrink() {
877 // Release half of free pages.
878 Page* top_page = AllocationTopPage();
879 ASSERT(top_page->is_valid());
880
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000881 // Count the number of pages we would like to free.
882 int pages_to_free = 0;
883 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
884 pages_to_free++;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 }
886
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000887 // Free pages after top_page.
888 Page* p = MemoryAllocator::FreePages(top_page->next_page());
889 MemoryAllocator::SetNextPage(top_page, p);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000891 // Find out how many pages we failed to free and update last_page_.
892 // Please note pages can only be freed in whole chunks.
893 last_page_ = top_page;
894 for (Page* p = top_page->next_page(); p->is_valid(); p = p->next_page()) {
895 pages_to_free--;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000896 last_page_ = p;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 }
898
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000899 accounting_stats_.ShrinkSpace(pages_to_free * Page::kObjectAreaSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 ASSERT(Capacity() == CountTotalPages() * Page::kObjectAreaSize);
901}
902
903
904bool PagedSpace::EnsureCapacity(int capacity) {
905 if (Capacity() >= capacity) return true;
906
907 // Start from the allocation top and loop to the last page in the space.
908 Page* last_page = AllocationTopPage();
909 Page* next_page = last_page->next_page();
910 while (next_page->is_valid()) {
911 last_page = MemoryAllocator::FindLastPageInSameChunk(next_page);
912 next_page = last_page->next_page();
913 }
914
915 // Expand the space until it has the required capacity or expansion fails.
916 do {
917 if (!Expand(last_page)) return false;
918 ASSERT(last_page->next_page()->is_valid());
919 last_page =
920 MemoryAllocator::FindLastPageInSameChunk(last_page->next_page());
921 } while (Capacity() < capacity);
922
923 return true;
924}
925
926
927#ifdef DEBUG
928void PagedSpace::Print() { }
929#endif
930
931
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000932#ifdef DEBUG
933// We do not assume that the PageIterator works, because it depends on the
934// invariants we are checking during verification.
935void PagedSpace::Verify(ObjectVisitor* visitor) {
936 // The allocation pointer should be valid, and it should be in a page in the
937 // space.
938 ASSERT(allocation_info_.VerifyPagedAllocation());
939 Page* top_page = Page::FromAllocationTop(allocation_info_.top);
940 ASSERT(MemoryAllocator::IsPageInSpace(top_page, this));
941
942 // Loop over all the pages.
943 bool above_allocation_top = false;
944 Page* current_page = first_page_;
945 while (current_page->is_valid()) {
946 if (above_allocation_top) {
947 // We don't care what's above the allocation top.
948 } else {
949 // Unless this is the last page in the space containing allocated
950 // objects, the allocation top should be at a constant offset from the
951 // object area end.
952 Address top = current_page->AllocationTop();
953 if (current_page == top_page) {
954 ASSERT(top == allocation_info_.top);
955 // The next page will be above the allocation top.
956 above_allocation_top = true;
957 } else {
958 ASSERT(top == current_page->ObjectAreaEnd() - page_extra_);
959 }
960
961 // It should be packed with objects from the bottom to the top.
962 Address current = current_page->ObjectAreaStart();
963 while (current < top) {
964 HeapObject* object = HeapObject::FromAddress(current);
965
966 // The first word should be a map, and we expect all map pointers to
967 // be in map space.
968 Map* map = object->map();
969 ASSERT(map->IsMap());
970 ASSERT(Heap::map_space()->Contains(map));
971
972 // Perform space-specific object verification.
973 VerifyObject(object);
974
975 // The object itself should look OK.
976 object->Verify();
977
978 // All the interior pointers should be contained in the heap and
979 // have their remembered set bits set if required as determined
980 // by the visitor.
981 int size = object->Size();
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000982 object->IterateBody(map->instance_type(), size, visitor);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000983
984 current += size;
985 }
986
987 // The allocation pointer should not be in the middle of an object.
988 ASSERT(current == top);
989 }
990
991 current_page = current_page->next_page();
992 }
993}
994#endif
995
996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997// -----------------------------------------------------------------------------
998// NewSpace implementation
999
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001000
1001bool NewSpace::Setup(Address start, int size) {
1002 // Setup new space based on the preallocated memory block defined by
1003 // start and size. The provided space is divided into two semi-spaces.
1004 // To support fast containment testing in the new space, the size of
1005 // this chunk must be a power of two and it must be aligned to its size.
1006 int initial_semispace_capacity = Heap::InitialSemiSpaceSize();
ager@chromium.org3811b432009-10-28 14:53:37 +00001007 int maximum_semispace_capacity = Heap::MaxSemiSpaceSize();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001008
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 ASSERT(initial_semispace_capacity <= maximum_semispace_capacity);
1010 ASSERT(IsPowerOf2(maximum_semispace_capacity));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011
1012 // Allocate and setup the histogram arrays if necessary.
1013#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1014 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1015 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1016
1017#define SET_NAME(name) allocated_histogram_[name].set_name(#name); \
1018 promoted_histogram_[name].set_name(#name);
1019 INSTANCE_TYPE_LIST(SET_NAME)
1020#undef SET_NAME
1021#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
ager@chromium.org3811b432009-10-28 14:53:37 +00001023 ASSERT(size == 2 * Heap::ReservedSemiSpaceSize());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 ASSERT(IsAddressAligned(start, size, 0));
1025
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001026 if (!to_space_.Setup(start,
1027 initial_semispace_capacity,
1028 maximum_semispace_capacity)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 return false;
1030 }
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001031 if (!from_space_.Setup(start + maximum_semispace_capacity,
1032 initial_semispace_capacity,
1033 maximum_semispace_capacity)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 return false;
1035 }
1036
1037 start_ = start;
1038 address_mask_ = ~(size - 1);
1039 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +00001040 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001042 allocation_info_.top = to_space_.low();
1043 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 mc_forwarding_info_.top = NULL;
1045 mc_forwarding_info_.limit = NULL;
1046
1047 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1048 return true;
1049}
1050
1051
1052void NewSpace::TearDown() {
1053#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1054 if (allocated_histogram_) {
1055 DeleteArray(allocated_histogram_);
1056 allocated_histogram_ = NULL;
1057 }
1058 if (promoted_histogram_) {
1059 DeleteArray(promoted_histogram_);
1060 promoted_histogram_ = NULL;
1061 }
1062#endif
1063
1064 start_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 allocation_info_.top = NULL;
1066 allocation_info_.limit = NULL;
1067 mc_forwarding_info_.top = NULL;
1068 mc_forwarding_info_.limit = NULL;
1069
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001070 to_space_.TearDown();
1071 from_space_.TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072}
1073
1074
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00001075#ifdef ENABLE_HEAP_PROTECTION
1076
1077void NewSpace::Protect() {
1078 MemoryAllocator::Protect(ToSpaceLow(), Capacity());
1079 MemoryAllocator::Protect(FromSpaceLow(), Capacity());
1080}
1081
1082
1083void NewSpace::Unprotect() {
1084 MemoryAllocator::Unprotect(ToSpaceLow(), Capacity(),
1085 to_space_.executable());
1086 MemoryAllocator::Unprotect(FromSpaceLow(), Capacity(),
1087 from_space_.executable());
1088}
1089
1090#endif
1091
1092
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093void NewSpace::Flip() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001094 SemiSpace tmp = from_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 from_space_ = to_space_;
1096 to_space_ = tmp;
1097}
1098
1099
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001100void NewSpace::Grow() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001101 ASSERT(Capacity() < MaximumCapacity());
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001102 if (to_space_.Grow()) {
1103 // Only grow from space if we managed to grow to space.
1104 if (!from_space_.Grow()) {
1105 // If we managed to grow to space but couldn't grow from space,
1106 // attempt to shrink to space.
1107 if (!to_space_.ShrinkTo(from_space_.Capacity())) {
1108 // We are in an inconsistent state because we could not
1109 // commit/uncommit memory from new space.
1110 V8::FatalProcessOutOfMemory("Failed to grow new space.");
1111 }
1112 }
1113 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001114 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001116}
1117
1118
1119void NewSpace::Shrink() {
1120 int new_capacity = Max(InitialCapacity(), 2 * Size());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001121 int rounded_new_capacity =
1122 RoundUp(new_capacity, static_cast<int>(OS::AllocateAlignment()));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001123 if (rounded_new_capacity < Capacity() &&
1124 to_space_.ShrinkTo(rounded_new_capacity)) {
1125 // Only shrink from space if we managed to shrink to space.
1126 if (!from_space_.ShrinkTo(rounded_new_capacity)) {
1127 // If we managed to shrink to space but couldn't shrink from
1128 // space, attempt to grow to space again.
1129 if (!to_space_.GrowTo(from_space_.Capacity())) {
1130 // We are in an inconsistent state because we could not
1131 // commit/uncommit memory from new space.
1132 V8::FatalProcessOutOfMemory("Failed to shrink new space.");
1133 }
1134 }
1135 }
1136 allocation_info_.limit = to_space_.high();
1137 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138}
1139
1140
1141void NewSpace::ResetAllocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001142 allocation_info_.top = to_space_.low();
1143 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1145}
1146
1147
1148void NewSpace::MCResetRelocationInfo() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001149 mc_forwarding_info_.top = from_space_.low();
1150 mc_forwarding_info_.limit = from_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 ASSERT_SEMISPACE_ALLOCATION_INFO(mc_forwarding_info_, from_space_);
1152}
1153
1154
1155void NewSpace::MCCommitRelocationInfo() {
1156 // Assumes that the spaces have been flipped so that mc_forwarding_info_ is
1157 // valid allocation info for the to space.
1158 allocation_info_.top = mc_forwarding_info_.top;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001159 allocation_info_.limit = to_space_.high();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1161}
1162
1163
1164#ifdef DEBUG
1165// We do not use the SemispaceIterator because verification doesn't assume
1166// that it works (it depends on the invariants we are checking).
1167void NewSpace::Verify() {
1168 // The allocation pointer should be in the space or at the very end.
1169 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
1170
1171 // There should be objects packed in from the low address up to the
1172 // allocation pointer.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001173 Address current = to_space_.low();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 while (current < top()) {
1175 HeapObject* object = HeapObject::FromAddress(current);
1176
1177 // The first word should be a map, and we expect all map pointers to
1178 // be in map space.
1179 Map* map = object->map();
1180 ASSERT(map->IsMap());
1181 ASSERT(Heap::map_space()->Contains(map));
1182
1183 // The object should not be code or a map.
1184 ASSERT(!object->IsMap());
1185 ASSERT(!object->IsCode());
1186
1187 // The object itself should look OK.
1188 object->Verify();
1189
1190 // All the interior pointers should be contained in the heap.
1191 VerifyPointersVisitor visitor;
1192 int size = object->Size();
1193 object->IterateBody(map->instance_type(), size, &visitor);
1194
1195 current += size;
1196 }
1197
1198 // The allocation pointer should not be in the middle of an object.
1199 ASSERT(current == top());
1200}
1201#endif
1202
1203
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001204bool SemiSpace::Commit() {
1205 ASSERT(!is_committed());
1206 if (!MemoryAllocator::CommitBlock(start_, capacity_, executable())) {
1207 return false;
1208 }
1209 committed_ = true;
1210 return true;
1211}
1212
1213
1214bool SemiSpace::Uncommit() {
1215 ASSERT(is_committed());
1216 if (!MemoryAllocator::UncommitBlock(start_, capacity_)) {
1217 return false;
1218 }
1219 committed_ = false;
1220 return true;
1221}
1222
1223
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224// -----------------------------------------------------------------------------
1225// SemiSpace implementation
1226
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001227bool SemiSpace::Setup(Address start,
1228 int initial_capacity,
1229 int maximum_capacity) {
1230 // Creates a space in the young generation. The constructor does not
1231 // allocate memory from the OS. A SemiSpace is given a contiguous chunk of
1232 // memory of size 'capacity' when set up, and does not grow or shrink
1233 // otherwise. In the mark-compact collector, the memory region of the from
1234 // space is used as the marking stack. It requires contiguous memory
1235 // addresses.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001236 initial_capacity_ = initial_capacity;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001237 capacity_ = initial_capacity;
1238 maximum_capacity_ = maximum_capacity;
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001239 committed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240
1241 start_ = start;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001242 address_mask_ = ~(maximum_capacity - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243 object_mask_ = address_mask_ | kHeapObjectTag;
ager@chromium.org9085a012009-05-11 19:22:57 +00001244 object_expected_ = reinterpret_cast<uintptr_t>(start) | kHeapObjectTag;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 age_mark_ = start_;
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001246
1247 return Commit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248}
1249
1250
1251void SemiSpace::TearDown() {
1252 start_ = NULL;
1253 capacity_ = 0;
1254}
1255
1256
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001257bool SemiSpace::Grow() {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00001258 // Double the semispace size but only up to maximum capacity.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001259 int maximum_extra = maximum_capacity_ - capacity_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001260 int extra = Min(RoundUp(capacity_, static_cast<int>(OS::AllocateAlignment())),
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001261 maximum_extra);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001262 if (!MemoryAllocator::CommitBlock(high(), extra, executable())) {
kasper.lund7276f142008-07-30 08:49:36 +00001263 return false;
1264 }
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001265 capacity_ += extra;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 return true;
1267}
1268
1269
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001270bool SemiSpace::GrowTo(int new_capacity) {
1271 ASSERT(new_capacity <= maximum_capacity_);
1272 ASSERT(new_capacity > capacity_);
1273 size_t delta = new_capacity - capacity_;
1274 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1275 if (!MemoryAllocator::CommitBlock(high(), delta, executable())) {
1276 return false;
1277 }
1278 capacity_ = new_capacity;
1279 return true;
1280}
1281
1282
1283bool SemiSpace::ShrinkTo(int new_capacity) {
1284 ASSERT(new_capacity >= initial_capacity_);
1285 ASSERT(new_capacity < capacity_);
1286 size_t delta = capacity_ - new_capacity;
1287 ASSERT(IsAligned(delta, OS::AllocateAlignment()));
1288 if (!MemoryAllocator::UncommitBlock(high() - delta, delta)) {
1289 return false;
1290 }
1291 capacity_ = new_capacity;
1292 return true;
1293}
1294
1295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296#ifdef DEBUG
1297void SemiSpace::Print() { }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001298
1299
1300void SemiSpace::Verify() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301#endif
1302
1303
1304// -----------------------------------------------------------------------------
1305// SemiSpaceIterator implementation.
1306SemiSpaceIterator::SemiSpaceIterator(NewSpace* space) {
1307 Initialize(space, space->bottom(), space->top(), NULL);
1308}
1309
1310
1311SemiSpaceIterator::SemiSpaceIterator(NewSpace* space,
1312 HeapObjectCallback size_func) {
1313 Initialize(space, space->bottom(), space->top(), size_func);
1314}
1315
1316
1317SemiSpaceIterator::SemiSpaceIterator(NewSpace* space, Address start) {
1318 Initialize(space, start, space->top(), NULL);
1319}
1320
1321
1322void SemiSpaceIterator::Initialize(NewSpace* space, Address start,
1323 Address end,
1324 HeapObjectCallback size_func) {
1325 ASSERT(space->ToSpaceContains(start));
1326 ASSERT(space->ToSpaceLow() <= end
1327 && end <= space->ToSpaceHigh());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001328 space_ = &space->to_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 current_ = start;
1330 limit_ = end;
1331 size_func_ = size_func;
1332}
1333
1334
1335#ifdef DEBUG
1336// A static array of histogram info for each type.
1337static HistogramInfo heap_histograms[LAST_TYPE+1];
1338static JSObject::SpillInformation js_spill_information;
1339
1340// heap_histograms is shared, always clear it before using it.
1341static void ClearHistograms() {
1342 // We reset the name each time, though it hasn't changed.
1343#define DEF_TYPE_NAME(name) heap_histograms[name].set_name(#name);
1344 INSTANCE_TYPE_LIST(DEF_TYPE_NAME)
1345#undef DEF_TYPE_NAME
1346
1347#define CLEAR_HISTOGRAM(name) heap_histograms[name].clear();
1348 INSTANCE_TYPE_LIST(CLEAR_HISTOGRAM)
1349#undef CLEAR_HISTOGRAM
1350
1351 js_spill_information.Clear();
1352}
1353
1354
1355static int code_kind_statistics[Code::NUMBER_OF_KINDS];
1356
1357
1358static void ClearCodeKindStatistics() {
1359 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1360 code_kind_statistics[i] = 0;
1361 }
1362}
1363
1364
1365static void ReportCodeKindStatistics() {
1366 const char* table[Code::NUMBER_OF_KINDS];
1367
1368#define CASE(name) \
1369 case Code::name: table[Code::name] = #name; \
1370 break
1371
1372 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1373 switch (static_cast<Code::Kind>(i)) {
1374 CASE(FUNCTION);
1375 CASE(STUB);
1376 CASE(BUILTIN);
1377 CASE(LOAD_IC);
1378 CASE(KEYED_LOAD_IC);
1379 CASE(STORE_IC);
1380 CASE(KEYED_STORE_IC);
1381 CASE(CALL_IC);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001382 CASE(BINARY_OP_IC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 }
1384 }
1385
1386#undef CASE
1387
1388 PrintF("\n Code kind histograms: \n");
1389 for (int i = 0; i < Code::NUMBER_OF_KINDS; i++) {
1390 if (code_kind_statistics[i] > 0) {
1391 PrintF(" %-20s: %10d bytes\n", table[i], code_kind_statistics[i]);
1392 }
1393 }
1394 PrintF("\n");
1395}
1396
1397
1398static int CollectHistogramInfo(HeapObject* obj) {
1399 InstanceType type = obj->map()->instance_type();
1400 ASSERT(0 <= type && type <= LAST_TYPE);
1401 ASSERT(heap_histograms[type].name() != NULL);
1402 heap_histograms[type].increment_number(1);
1403 heap_histograms[type].increment_bytes(obj->Size());
1404
1405 if (FLAG_collect_heap_spill_statistics && obj->IsJSObject()) {
1406 JSObject::cast(obj)->IncrementSpillStatistics(&js_spill_information);
1407 }
1408
1409 return obj->Size();
1410}
1411
1412
1413static void ReportHistogram(bool print_spill) {
1414 PrintF("\n Object Histogram:\n");
1415 for (int i = 0; i <= LAST_TYPE; i++) {
1416 if (heap_histograms[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001417 PrintF(" %-34s%10d (%10d bytes)\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 heap_histograms[i].name(),
1419 heap_histograms[i].number(),
1420 heap_histograms[i].bytes());
1421 }
1422 }
1423 PrintF("\n");
1424
1425 // Summarize string types.
1426 int string_number = 0;
1427 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001428#define INCREMENT(type, size, name, camel_name) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429 string_number += heap_histograms[type].number(); \
1430 string_bytes += heap_histograms[type].bytes();
1431 STRING_TYPE_LIST(INCREMENT)
1432#undef INCREMENT
1433 if (string_number > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001434 PrintF(" %-34s%10d (%10d bytes)\n\n", "STRING_TYPE", string_number,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 string_bytes);
1436 }
1437
1438 if (FLAG_collect_heap_spill_statistics && print_spill) {
1439 js_spill_information.Print();
1440 }
1441}
1442#endif // DEBUG
1443
1444
1445// Support for statistics gathering for --heap-stats and --log-gc.
1446#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1447void NewSpace::ClearHistograms() {
1448 for (int i = 0; i <= LAST_TYPE; i++) {
1449 allocated_histogram_[i].clear();
1450 promoted_histogram_[i].clear();
1451 }
1452}
1453
1454// Because the copying collector does not touch garbage objects, we iterate
1455// the new space before a collection to get a histogram of allocated objects.
1456// This only happens (1) when compiled with DEBUG and the --heap-stats flag is
1457// set, or when compiled with ENABLE_LOGGING_AND_PROFILING and the --log-gc
1458// flag is set.
1459void NewSpace::CollectStatistics() {
1460 ClearHistograms();
1461 SemiSpaceIterator it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001462 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next())
1463 RecordAllocation(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464}
1465
1466
1467#ifdef ENABLE_LOGGING_AND_PROFILING
1468static void DoReportStatistics(HistogramInfo* info, const char* description) {
1469 LOG(HeapSampleBeginEvent("NewSpace", description));
1470 // Lump all the string types together.
1471 int string_number = 0;
1472 int string_bytes = 0;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001473#define INCREMENT(type, size, name, camel_name) \
1474 string_number += info[type].number(); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 string_bytes += info[type].bytes();
1476 STRING_TYPE_LIST(INCREMENT)
1477#undef INCREMENT
1478 if (string_number > 0) {
1479 LOG(HeapSampleItemEvent("STRING_TYPE", string_number, string_bytes));
1480 }
1481
1482 // Then do the other types.
1483 for (int i = FIRST_NONSTRING_TYPE; i <= LAST_TYPE; ++i) {
1484 if (info[i].number() > 0) {
1485 LOG(HeapSampleItemEvent(info[i].name(), info[i].number(),
1486 info[i].bytes()));
1487 }
1488 }
1489 LOG(HeapSampleEndEvent("NewSpace", description));
1490}
1491#endif // ENABLE_LOGGING_AND_PROFILING
1492
1493
1494void NewSpace::ReportStatistics() {
1495#ifdef DEBUG
1496 if (FLAG_heap_stats) {
1497 float pct = static_cast<float>(Available()) / Capacity();
1498 PrintF(" capacity: %d, available: %d, %%%d\n",
1499 Capacity(), Available(), static_cast<int>(pct*100));
1500 PrintF("\n Object Histogram:\n");
1501 for (int i = 0; i <= LAST_TYPE; i++) {
1502 if (allocated_histogram_[i].number() > 0) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001503 PrintF(" %-34s%10d (%10d bytes)\n",
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 allocated_histogram_[i].name(),
1505 allocated_histogram_[i].number(),
1506 allocated_histogram_[i].bytes());
1507 }
1508 }
1509 PrintF("\n");
1510 }
1511#endif // DEBUG
1512
1513#ifdef ENABLE_LOGGING_AND_PROFILING
1514 if (FLAG_log_gc) {
1515 DoReportStatistics(allocated_histogram_, "allocated");
1516 DoReportStatistics(promoted_histogram_, "promoted");
1517 }
1518#endif // ENABLE_LOGGING_AND_PROFILING
1519}
1520
1521
1522void NewSpace::RecordAllocation(HeapObject* obj) {
1523 InstanceType type = obj->map()->instance_type();
1524 ASSERT(0 <= type && type <= LAST_TYPE);
1525 allocated_histogram_[type].increment_number(1);
1526 allocated_histogram_[type].increment_bytes(obj->Size());
1527}
1528
1529
1530void NewSpace::RecordPromotion(HeapObject* obj) {
1531 InstanceType type = obj->map()->instance_type();
1532 ASSERT(0 <= type && type <= LAST_TYPE);
1533 promoted_histogram_[type].increment_number(1);
1534 promoted_histogram_[type].increment_bytes(obj->Size());
1535}
1536#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1537
1538
1539// -----------------------------------------------------------------------------
1540// Free lists for old object spaces implementation
1541
1542void FreeListNode::set_size(int size_in_bytes) {
1543 ASSERT(size_in_bytes > 0);
1544 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1545
1546 // We write a map and possibly size information to the block. If the block
1547 // is big enough to be a ByteArray with at least one extra word (the next
1548 // pointer), we set its map to be the byte array map and its size to an
1549 // appropriate array length for the desired size from HeapObject::Size().
1550 // If the block is too small (eg, one or two words), to hold both a size
1551 // field and a next pointer, we give it a filler map that gives it the
1552 // correct size.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001553 if (size_in_bytes > ByteArray::kAlignedSize) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001554 set_map(Heap::raw_unchecked_byte_array_map());
ager@chromium.org3811b432009-10-28 14:53:37 +00001555 // Can't use ByteArray::cast because it fails during deserialization.
1556 ByteArray* this_as_byte_array = reinterpret_cast<ByteArray*>(this);
1557 this_as_byte_array->set_length(ByteArray::LengthFor(size_in_bytes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 } else if (size_in_bytes == kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001559 set_map(Heap::raw_unchecked_one_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 } else if (size_in_bytes == 2 * kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001561 set_map(Heap::raw_unchecked_two_pointer_filler_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 } else {
1563 UNREACHABLE();
1564 }
ager@chromium.org3811b432009-10-28 14:53:37 +00001565 // We would like to ASSERT(Size() == size_in_bytes) but this would fail during
1566 // deserialization because the byte array map is not done yet.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567}
1568
1569
1570Address FreeListNode::next() {
ager@chromium.org3811b432009-10-28 14:53:37 +00001571 ASSERT(IsFreeListNode(this));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001572 if (map() == Heap::raw_unchecked_byte_array_map()) {
1573 ASSERT(Size() >= kNextOffset + kPointerSize);
1574 return Memory::Address_at(address() + kNextOffset);
1575 } else {
1576 return Memory::Address_at(address() + kPointerSize);
1577 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578}
1579
1580
1581void FreeListNode::set_next(Address next) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001582 ASSERT(IsFreeListNode(this));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001583 if (map() == Heap::raw_unchecked_byte_array_map()) {
1584 ASSERT(Size() >= kNextOffset + kPointerSize);
1585 Memory::Address_at(address() + kNextOffset) = next;
1586 } else {
1587 Memory::Address_at(address() + kPointerSize) = next;
1588 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589}
1590
1591
1592OldSpaceFreeList::OldSpaceFreeList(AllocationSpace owner) : owner_(owner) {
1593 Reset();
1594}
1595
1596
1597void OldSpaceFreeList::Reset() {
1598 available_ = 0;
1599 for (int i = 0; i < kFreeListsLength; i++) {
1600 free_[i].head_node_ = NULL;
1601 }
1602 needs_rebuild_ = false;
1603 finger_ = kHead;
1604 free_[kHead].next_size_ = kEnd;
1605}
1606
1607
1608void OldSpaceFreeList::RebuildSizeList() {
1609 ASSERT(needs_rebuild_);
1610 int cur = kHead;
1611 for (int i = cur + 1; i < kFreeListsLength; i++) {
1612 if (free_[i].head_node_ != NULL) {
1613 free_[cur].next_size_ = i;
1614 cur = i;
1615 }
1616 }
1617 free_[cur].next_size_ = kEnd;
1618 needs_rebuild_ = false;
1619}
1620
1621
1622int OldSpaceFreeList::Free(Address start, int size_in_bytes) {
1623#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001624 MemoryAllocator::ZapBlock(start, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001625#endif
1626 FreeListNode* node = FreeListNode::FromAddress(start);
1627 node->set_size(size_in_bytes);
1628
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001629 // We don't use the freelists in compacting mode. This makes it more like a
1630 // GC that only has mark-sweep-compact and doesn't have a mark-sweep
1631 // collector.
1632 if (FLAG_always_compact) {
1633 return size_in_bytes;
1634 }
1635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 // Early return to drop too-small blocks on the floor (one or two word
1637 // blocks cannot hold a map pointer, a size field, and a pointer to the
1638 // next block in the free list).
1639 if (size_in_bytes < kMinBlockSize) {
1640 return size_in_bytes;
1641 }
1642
1643 // Insert other blocks at the head of an exact free list.
1644 int index = size_in_bytes >> kPointerSizeLog2;
1645 node->set_next(free_[index].head_node_);
1646 free_[index].head_node_ = node->address();
1647 available_ += size_in_bytes;
1648 needs_rebuild_ = true;
1649 return 0;
1650}
1651
1652
1653Object* OldSpaceFreeList::Allocate(int size_in_bytes, int* wasted_bytes) {
1654 ASSERT(0 < size_in_bytes);
1655 ASSERT(size_in_bytes <= kMaxBlockSize);
1656 ASSERT(IsAligned(size_in_bytes, kPointerSize));
1657
1658 if (needs_rebuild_) RebuildSizeList();
1659 int index = size_in_bytes >> kPointerSizeLog2;
1660 // Check for a perfect fit.
1661 if (free_[index].head_node_ != NULL) {
1662 FreeListNode* node = FreeListNode::FromAddress(free_[index].head_node_);
1663 // If this was the last block of its size, remove the size.
1664 if ((free_[index].head_node_ = node->next()) == NULL) RemoveSize(index);
1665 available_ -= size_in_bytes;
1666 *wasted_bytes = 0;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001667 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 return node;
1669 }
1670 // Search the size list for the best fit.
1671 int prev = finger_ < index ? finger_ : kHead;
1672 int cur = FindSize(index, &prev);
1673 ASSERT(index < cur);
1674 if (cur == kEnd) {
1675 // No large enough size in list.
1676 *wasted_bytes = 0;
1677 return Failure::RetryAfterGC(size_in_bytes, owner_);
1678 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001679 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 int rem = cur - index;
1681 int rem_bytes = rem << kPointerSizeLog2;
1682 FreeListNode* cur_node = FreeListNode::FromAddress(free_[cur].head_node_);
kasper.lund7276f142008-07-30 08:49:36 +00001683 ASSERT(cur_node->Size() == (cur << kPointerSizeLog2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 FreeListNode* rem_node = FreeListNode::FromAddress(free_[cur].head_node_ +
1685 size_in_bytes);
1686 // Distinguish the cases prev < rem < cur and rem <= prev < cur
1687 // to avoid many redundant tests and calls to Insert/RemoveSize.
1688 if (prev < rem) {
1689 // Simple case: insert rem between prev and cur.
1690 finger_ = prev;
1691 free_[prev].next_size_ = rem;
1692 // If this was the last block of size cur, remove the size.
1693 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1694 free_[rem].next_size_ = free_[cur].next_size_;
1695 } else {
1696 free_[rem].next_size_ = cur;
1697 }
1698 // Add the remainder block.
1699 rem_node->set_size(rem_bytes);
1700 rem_node->set_next(free_[rem].head_node_);
1701 free_[rem].head_node_ = rem_node->address();
1702 } else {
1703 // If this was the last block of size cur, remove the size.
1704 if ((free_[cur].head_node_ = cur_node->next()) == NULL) {
1705 finger_ = prev;
1706 free_[prev].next_size_ = free_[cur].next_size_;
1707 }
1708 if (rem_bytes < kMinBlockSize) {
1709 // Too-small remainder is wasted.
1710 rem_node->set_size(rem_bytes);
1711 available_ -= size_in_bytes + rem_bytes;
1712 *wasted_bytes = rem_bytes;
1713 return cur_node;
1714 }
1715 // Add the remainder block and, if needed, insert its size.
1716 rem_node->set_size(rem_bytes);
1717 rem_node->set_next(free_[rem].head_node_);
1718 free_[rem].head_node_ = rem_node->address();
1719 if (rem_node->next() == NULL) InsertSize(rem);
1720 }
1721 available_ -= size_in_bytes;
1722 *wasted_bytes = 0;
1723 return cur_node;
1724}
1725
1726
kasper.lund7276f142008-07-30 08:49:36 +00001727#ifdef DEBUG
1728bool OldSpaceFreeList::Contains(FreeListNode* node) {
1729 for (int i = 0; i < kFreeListsLength; i++) {
1730 Address cur_addr = free_[i].head_node_;
1731 while (cur_addr != NULL) {
1732 FreeListNode* cur_node = FreeListNode::FromAddress(cur_addr);
1733 if (cur_node == node) return true;
1734 cur_addr = cur_node->next();
1735 }
1736 }
1737 return false;
1738}
1739#endif
1740
1741
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001742FixedSizeFreeList::FixedSizeFreeList(AllocationSpace owner, int object_size)
1743 : owner_(owner), object_size_(object_size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 Reset();
1745}
1746
1747
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001748void FixedSizeFreeList::Reset() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 available_ = 0;
1750 head_ = NULL;
1751}
1752
1753
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001754void FixedSizeFreeList::Free(Address start) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755#ifdef DEBUG
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001756 MemoryAllocator::ZapBlock(start, object_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757#endif
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001758 // We only use the freelists with mark-sweep.
1759 ASSERT(!MarkCompactCollector::IsCompacting());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 FreeListNode* node = FreeListNode::FromAddress(start);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001761 node->set_size(object_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 node->set_next(head_);
1763 head_ = node->address();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001764 available_ += object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765}
1766
1767
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001768Object* FixedSizeFreeList::Allocate() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 if (head_ == NULL) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001770 return Failure::RetryAfterGC(object_size_, owner_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 }
1772
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001773 ASSERT(!FLAG_always_compact); // We only use the freelists with mark-sweep.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 FreeListNode* node = FreeListNode::FromAddress(head_);
1775 head_ = node->next();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001776 available_ -= object_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 return node;
1778}
1779
1780
1781// -----------------------------------------------------------------------------
1782// OldSpace implementation
1783
1784void OldSpace::PrepareForMarkCompact(bool will_compact) {
1785 if (will_compact) {
1786 // Reset relocation info. During a compacting collection, everything in
1787 // the space is considered 'available' and we will rediscover live data
1788 // and waste during the collection.
1789 MCResetRelocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 ASSERT(Available() == Capacity());
1791 } else {
1792 // During a non-compacting collection, everything below the linear
1793 // allocation pointer is considered allocated (everything above is
1794 // available) and we will rediscover available and wasted bytes during
1795 // the collection.
1796 accounting_stats_.AllocateBytes(free_list_.available());
1797 accounting_stats_.FillWastedBytes(Waste());
1798 }
1799
kasper.lund7276f142008-07-30 08:49:36 +00001800 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802}
1803
1804
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805void OldSpace::MCCommitRelocationInfo() {
1806 // Update fast allocation info.
1807 allocation_info_.top = mc_forwarding_info_.top;
1808 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00001809 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810
1811 // The space is compacted and we haven't yet built free lists or
1812 // wasted any space.
1813 ASSERT(Waste() == 0);
1814 ASSERT(AvailableFree() == 0);
1815
1816 // Build the free list for the space.
1817 int computed_size = 0;
1818 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
1819 while (it.has_next()) {
1820 Page* p = it.next();
1821 // Space below the relocation pointer is allocated.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001822 computed_size +=
1823 static_cast<int>(p->mc_relocation_top - p->ObjectAreaStart());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 if (it.has_next()) {
1825 // Free the space at the top of the page. We cannot use
1826 // p->mc_relocation_top after the call to Free (because Free will clear
1827 // remembered set bits).
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001828 int extra_size =
1829 static_cast<int>(p->ObjectAreaEnd() - p->mc_relocation_top);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830 if (extra_size > 0) {
1831 int wasted_bytes = free_list_.Free(p->mc_relocation_top, extra_size);
1832 // The bytes we have just "freed" to add to the free list were
1833 // already accounted as available.
1834 accounting_stats_.WasteBytes(wasted_bytes);
1835 }
1836 }
1837 }
1838
1839 // Make sure the computed size - based on the used portion of the pages in
1840 // use - matches the size obtained while computing forwarding addresses.
1841 ASSERT(computed_size == Size());
1842}
1843
1844
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001845bool NewSpace::ReserveSpace(int bytes) {
1846 // We can't reliably unpack a partial snapshot that needs more new space
1847 // space than the minimum NewSpace size.
1848 ASSERT(bytes <= InitialCapacity());
1849 Address limit = allocation_info_.limit;
1850 Address top = allocation_info_.top;
1851 return limit - top >= bytes;
1852}
1853
1854
1855bool PagedSpace::ReserveSpace(int bytes) {
1856 Address limit = allocation_info_.limit;
1857 Address top = allocation_info_.top;
1858 if (limit - top >= bytes) return true;
1859
1860 // There wasn't enough space in the current page. Lets put the rest
1861 // of the page on the free list and start a fresh page.
1862 PutRestOfCurrentPageOnFreeList(TopPageOf(allocation_info_));
1863
1864 Page* reserved_page = TopPageOf(allocation_info_);
1865 int bytes_left_to_reserve = bytes;
1866 while (bytes_left_to_reserve > 0) {
1867 if (!reserved_page->next_page()->is_valid()) {
1868 if (Heap::OldGenerationAllocationLimitReached()) return false;
1869 Expand(reserved_page);
1870 }
1871 bytes_left_to_reserve -= Page::kPageSize;
1872 reserved_page = reserved_page->next_page();
1873 if (!reserved_page->is_valid()) return false;
1874 }
1875 ASSERT(TopPageOf(allocation_info_)->next_page()->is_valid());
1876 SetAllocationInfo(&allocation_info_,
1877 TopPageOf(allocation_info_)->next_page());
1878 return true;
1879}
1880
1881
1882// You have to call this last, since the implementation from PagedSpace
1883// doesn't know that memory was 'promised' to large object space.
1884bool LargeObjectSpace::ReserveSpace(int bytes) {
1885 return Heap::OldGenerationSpaceAvailable() >= bytes;
1886}
1887
1888
kasper.lund7276f142008-07-30 08:49:36 +00001889// Slow case for normal allocation. Try in order: (1) allocate in the next
1890// page in the space, (2) allocate off the space's free list, (3) expand the
1891// space, (4) fail.
1892HeapObject* OldSpace::SlowAllocateRaw(int size_in_bytes) {
1893 // Linear allocation in this space has failed. If there is another page
1894 // in the space, move to that page and allocate there. This allocation
1895 // should succeed (size_in_bytes should not be greater than a page's
1896 // object area size).
1897 Page* current_page = TopPageOf(allocation_info_);
1898 if (current_page->next_page()->is_valid()) {
1899 return AllocateInNextPage(current_page, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 }
kasper.lund7276f142008-07-30 08:49:36 +00001901
ager@chromium.org3811b432009-10-28 14:53:37 +00001902 // There is no next page in this space. Try free list allocation unless that
1903 // is currently forbidden.
1904 if (!Heap::linear_allocation()) {
1905 int wasted_bytes;
1906 Object* result = free_list_.Allocate(size_in_bytes, &wasted_bytes);
1907 accounting_stats_.WasteBytes(wasted_bytes);
1908 if (!result->IsFailure()) {
1909 accounting_stats_.AllocateBytes(size_in_bytes);
1910 return HeapObject::cast(result);
1911 }
kasper.lund7276f142008-07-30 08:49:36 +00001912 }
1913
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001914 // Free list allocation failed and there is no next page. Fail if we have
1915 // hit the old generation size limit that should cause a garbage
1916 // collection.
1917 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
1918 return NULL;
1919 }
1920
1921 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00001922 ASSERT(!current_page->next_page()->is_valid());
1923 if (Expand(current_page)) {
1924 return AllocateInNextPage(current_page, size_in_bytes);
1925 }
1926
1927 // Finally, fail.
1928 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929}
1930
1931
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001932void OldSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001933 int free_size =
1934 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
kasper.lund7276f142008-07-30 08:49:36 +00001935 if (free_size > 0) {
1936 int wasted_bytes = free_list_.Free(allocation_info_.top, free_size);
1937 accounting_stats_.WasteBytes(wasted_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001939}
1940
1941
1942void FixedSpace::PutRestOfCurrentPageOnFreeList(Page* current_page) {
1943 int free_size =
1944 static_cast<int>(current_page->ObjectAreaEnd() - allocation_info_.top);
1945 // In the fixed space free list all the free list items have the right size.
1946 // We use up the rest of the page while preserving this invariant.
1947 while (free_size >= object_size_in_bytes_) {
1948 free_list_.Free(allocation_info_.top);
1949 allocation_info_.top += object_size_in_bytes_;
1950 free_size -= object_size_in_bytes_;
1951 accounting_stats_.WasteBytes(object_size_in_bytes_);
1952 }
1953}
1954
1955
1956// Add the block at the top of the page to the space's free list, set the
1957// allocation info to the next page (assumed to be one), and allocate
1958// linearly there.
1959HeapObject* OldSpace::AllocateInNextPage(Page* current_page,
1960 int size_in_bytes) {
1961 ASSERT(current_page->next_page()->is_valid());
1962 PutRestOfCurrentPageOnFreeList(current_page);
kasper.lund7276f142008-07-30 08:49:36 +00001963 SetAllocationInfo(&allocation_info_, current_page->next_page());
1964 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965}
1966
1967
1968#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969struct CommentStatistic {
1970 const char* comment;
1971 int size;
1972 int count;
1973 void Clear() {
1974 comment = NULL;
1975 size = 0;
1976 count = 0;
1977 }
1978};
1979
1980
1981// must be small, since an iteration is used for lookup
1982const int kMaxComments = 64;
1983static CommentStatistic comments_statistics[kMaxComments+1];
1984
1985
1986void PagedSpace::ReportCodeStatistics() {
1987 ReportCodeKindStatistics();
1988 PrintF("Code comment statistics (\" [ comment-txt : size/ "
1989 "count (average)\"):\n");
1990 for (int i = 0; i <= kMaxComments; i++) {
1991 const CommentStatistic& cs = comments_statistics[i];
1992 if (cs.size > 0) {
1993 PrintF(" %-30s: %10d/%6d (%d)\n", cs.comment, cs.size, cs.count,
1994 cs.size/cs.count);
1995 }
1996 }
1997 PrintF("\n");
1998}
1999
2000
2001void PagedSpace::ResetCodeStatistics() {
2002 ClearCodeKindStatistics();
2003 for (int i = 0; i < kMaxComments; i++) comments_statistics[i].Clear();
2004 comments_statistics[kMaxComments].comment = "Unknown";
2005 comments_statistics[kMaxComments].size = 0;
2006 comments_statistics[kMaxComments].count = 0;
2007}
2008
2009
2010// Adds comment to 'comment_statistics' table. Performance OK sa long as
2011// 'kMaxComments' is small
2012static void EnterComment(const char* comment, int delta) {
2013 // Do not count empty comments
2014 if (delta <= 0) return;
2015 CommentStatistic* cs = &comments_statistics[kMaxComments];
2016 // Search for a free or matching entry in 'comments_statistics': 'cs'
2017 // points to result.
2018 for (int i = 0; i < kMaxComments; i++) {
2019 if (comments_statistics[i].comment == NULL) {
2020 cs = &comments_statistics[i];
2021 cs->comment = comment;
2022 break;
2023 } else if (strcmp(comments_statistics[i].comment, comment) == 0) {
2024 cs = &comments_statistics[i];
2025 break;
2026 }
2027 }
2028 // Update entry for 'comment'
2029 cs->size += delta;
2030 cs->count += 1;
2031}
2032
2033
2034// Call for each nested comment start (start marked with '[ xxx', end marked
2035// with ']'. RelocIterator 'it' must point to a comment reloc info.
2036static void CollectCommentStatistics(RelocIterator* it) {
2037 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002038 ASSERT(it->rinfo()->rmode() == RelocInfo::COMMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 const char* tmp = reinterpret_cast<const char*>(it->rinfo()->data());
2040 if (tmp[0] != '[') {
2041 // Not a nested comment; skip
2042 return;
2043 }
2044
2045 // Search for end of nested comment or a new nested comment
2046 const char* const comment_txt =
2047 reinterpret_cast<const char*>(it->rinfo()->data());
2048 const byte* prev_pc = it->rinfo()->pc();
2049 int flat_delta = 0;
2050 it->next();
2051 while (true) {
2052 // All nested comments must be terminated properly, and therefore exit
2053 // from loop.
2054 ASSERT(!it->done());
ager@chromium.org236ad962008-09-25 09:45:57 +00002055 if (it->rinfo()->rmode() == RelocInfo::COMMENT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 const char* const txt =
2057 reinterpret_cast<const char*>(it->rinfo()->data());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002058 flat_delta += static_cast<int>(it->rinfo()->pc() - prev_pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059 if (txt[0] == ']') break; // End of nested comment
2060 // A new comment
2061 CollectCommentStatistics(it);
2062 // Skip code that was covered with previous comment
2063 prev_pc = it->rinfo()->pc();
2064 }
2065 it->next();
2066 }
2067 EnterComment(comment_txt, flat_delta);
2068}
2069
2070
2071// Collects code size statistics:
2072// - by code kind
2073// - by code comment
2074void PagedSpace::CollectCodeStatistics() {
2075 HeapObjectIterator obj_it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002076 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 if (obj->IsCode()) {
2078 Code* code = Code::cast(obj);
2079 code_kind_statistics[code->kind()] += code->Size();
2080 RelocIterator it(code);
2081 int delta = 0;
2082 const byte* prev_pc = code->instruction_start();
2083 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002084 if (it.rinfo()->rmode() == RelocInfo::COMMENT) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002085 delta += static_cast<int>(it.rinfo()->pc() - prev_pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 CollectCommentStatistics(&it);
2087 prev_pc = it.rinfo()->pc();
2088 }
2089 it.next();
2090 }
2091
2092 ASSERT(code->instruction_start() <= prev_pc &&
2093 prev_pc <= code->relocation_start());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002094 delta += static_cast<int>(code->relocation_start() - prev_pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 EnterComment("NoComment", delta);
2096 }
2097 }
2098}
2099
2100
2101void OldSpace::ReportStatistics() {
2102 int pct = Available() * 100 / Capacity();
2103 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2104 Capacity(), Waste(), Available(), pct);
2105
2106 // Report remembered set statistics.
2107 int rset_marked_pointers = 0;
2108 int rset_marked_arrays = 0;
2109 int rset_marked_array_elements = 0;
2110 int cross_gen_pointers = 0;
2111 int cross_gen_array_elements = 0;
2112
2113 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2114 while (page_it.has_next()) {
2115 Page* p = page_it.next();
2116
2117 for (Address rset_addr = p->RSetStart();
2118 rset_addr < p->RSetEnd();
2119 rset_addr += kIntSize) {
2120 int rset = Memory::int_at(rset_addr);
2121 if (rset != 0) {
2122 // Bits were set
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002123 int intoff =
2124 static_cast<int>(rset_addr - p->address() - Page::kRSetOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 int bitoff = 0;
2126 for (; bitoff < kBitsPerInt; ++bitoff) {
2127 if ((rset & (1 << bitoff)) != 0) {
2128 int bitpos = intoff*kBitsPerByte + bitoff;
2129 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2130 Object** obj = reinterpret_cast<Object**>(slot);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002131 if (*obj == Heap::raw_unchecked_fixed_array_map()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132 rset_marked_arrays++;
2133 FixedArray* fa = FixedArray::cast(HeapObject::FromAddress(slot));
2134
2135 rset_marked_array_elements += fa->length();
2136 // Manually inline FixedArray::IterateBody
2137 Address elm_start = slot + FixedArray::kHeaderSize;
2138 Address elm_stop = elm_start + fa->length() * kPointerSize;
2139 for (Address elm_addr = elm_start;
2140 elm_addr < elm_stop; elm_addr += kPointerSize) {
2141 // Filter non-heap-object pointers
2142 Object** elm_p = reinterpret_cast<Object**>(elm_addr);
2143 if (Heap::InNewSpace(*elm_p))
2144 cross_gen_array_elements++;
2145 }
2146 } else {
2147 rset_marked_pointers++;
2148 if (Heap::InNewSpace(*obj))
2149 cross_gen_pointers++;
2150 }
2151 }
2152 }
2153 }
2154 }
2155 }
2156
2157 pct = rset_marked_pointers == 0 ?
2158 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2159 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2160 rset_marked_pointers, cross_gen_pointers, pct);
2161 PrintF(" rset_marked arrays %d, ", rset_marked_arrays);
2162 PrintF(" elements %d, ", rset_marked_array_elements);
2163 pct = rset_marked_array_elements == 0 ? 0
2164 : cross_gen_array_elements * 100 / rset_marked_array_elements;
2165 PrintF(" pointers to new space %d (%%%d)\n", cross_gen_array_elements, pct);
2166 PrintF(" total rset-marked bits %d\n",
2167 (rset_marked_pointers + rset_marked_arrays));
2168 pct = (rset_marked_pointers + rset_marked_array_elements) == 0 ? 0
2169 : (cross_gen_pointers + cross_gen_array_elements) * 100 /
2170 (rset_marked_pointers + rset_marked_array_elements);
2171 PrintF(" total rset pointers %d, true cross generation ones %d (%%%d)\n",
2172 (rset_marked_pointers + rset_marked_array_elements),
2173 (cross_gen_pointers + cross_gen_array_elements),
2174 pct);
2175
2176 ClearHistograms();
2177 HeapObjectIterator obj_it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002178 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2179 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 ReportHistogram(true);
2181}
2182
2183
2184// Dump the range of remembered set words between [start, end) corresponding
2185// to the pointers starting at object_p. The allocation_top is an object
2186// pointer which should not be read past. This is important for large object
2187// pages, where some bits in the remembered set range do not correspond to
2188// allocated addresses.
2189static void PrintRSetRange(Address start, Address end, Object** object_p,
2190 Address allocation_top) {
2191 Address rset_address = start;
2192
2193 // If the range starts on on odd numbered word (eg, for large object extra
2194 // remembered set ranges), print some spaces.
ager@chromium.org9085a012009-05-11 19:22:57 +00002195 if ((reinterpret_cast<uintptr_t>(start) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 PrintF(" ");
2197 }
2198
2199 // Loop over all the words in the range.
2200 while (rset_address < end) {
2201 uint32_t rset_word = Memory::uint32_at(rset_address);
2202 int bit_position = 0;
2203
2204 // Loop over all the bits in the word.
2205 while (bit_position < kBitsPerInt) {
2206 if (object_p == reinterpret_cast<Object**>(allocation_top)) {
2207 // Print a bar at the allocation pointer.
2208 PrintF("|");
2209 } else if (object_p > reinterpret_cast<Object**>(allocation_top)) {
2210 // Do not dereference object_p past the allocation pointer.
2211 PrintF("#");
2212 } else if ((rset_word & (1 << bit_position)) == 0) {
2213 // Print a dot for zero bits.
2214 PrintF(".");
2215 } else if (Heap::InNewSpace(*object_p)) {
2216 // Print an X for one bits for pointers to new space.
2217 PrintF("X");
2218 } else {
2219 // Print a circle for one bits for pointers to old space.
2220 PrintF("o");
2221 }
2222
2223 // Print a space after every 8th bit except the last.
2224 if (bit_position % 8 == 7 && bit_position != (kBitsPerInt - 1)) {
2225 PrintF(" ");
2226 }
2227
2228 // Advance to next bit.
2229 bit_position++;
2230 object_p++;
2231 }
2232
2233 // Print a newline after every odd numbered word, otherwise a space.
ager@chromium.org9085a012009-05-11 19:22:57 +00002234 if ((reinterpret_cast<uintptr_t>(rset_address) / kIntSize) % 2 == 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 PrintF("\n");
2236 } else {
2237 PrintF(" ");
2238 }
2239
2240 // Advance to next remembered set word.
2241 rset_address += kIntSize;
2242 }
2243}
2244
2245
2246void PagedSpace::DoPrintRSet(const char* space_name) {
2247 PageIterator it(this, PageIterator::PAGES_IN_USE);
2248 while (it.has_next()) {
2249 Page* p = it.next();
2250 PrintF("%s page 0x%x:\n", space_name, p);
2251 PrintRSetRange(p->RSetStart(), p->RSetEnd(),
2252 reinterpret_cast<Object**>(p->ObjectAreaStart()),
2253 p->AllocationTop());
2254 PrintF("\n");
2255 }
2256}
2257
2258
2259void OldSpace::PrintRSet() { DoPrintRSet("old"); }
2260#endif
2261
2262// -----------------------------------------------------------------------------
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002263// FixedSpace implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002265void FixedSpace::PrepareForMarkCompact(bool will_compact) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 if (will_compact) {
2267 // Reset relocation info.
2268 MCResetRelocationInfo();
2269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 // During a compacting collection, everything in the space is considered
2271 // 'available' (set by the call to MCResetRelocationInfo) and we will
2272 // rediscover live and wasted bytes during the collection.
2273 ASSERT(Available() == Capacity());
2274 } else {
2275 // During a non-compacting collection, everything below the linear
2276 // allocation pointer except wasted top-of-page blocks is considered
2277 // allocated and we will rediscover available bytes during the
2278 // collection.
2279 accounting_stats_.AllocateBytes(free_list_.available());
2280 }
2281
kasper.lund7276f142008-07-30 08:49:36 +00002282 // Clear the free list before a full GC---it will be rebuilt afterward.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 free_list_.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284}
2285
2286
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002287void FixedSpace::MCCommitRelocationInfo() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 // Update fast allocation info.
2289 allocation_info_.top = mc_forwarding_info_.top;
2290 allocation_info_.limit = mc_forwarding_info_.limit;
kasper.lund7276f142008-07-30 08:49:36 +00002291 ASSERT(allocation_info_.VerifyPagedAllocation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292
2293 // The space is compacted and we haven't yet wasted any space.
2294 ASSERT(Waste() == 0);
2295
2296 // Update allocation_top of each page in use and compute waste.
2297 int computed_size = 0;
2298 PageIterator it(this, PageIterator::PAGES_USED_BY_MC);
2299 while (it.has_next()) {
2300 Page* page = it.next();
2301 Address page_top = page->AllocationTop();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002302 computed_size += static_cast<int>(page_top - page->ObjectAreaStart());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 if (it.has_next()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002304 accounting_stats_.WasteBytes(
2305 static_cast<int>(page->ObjectAreaEnd() - page_top));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 }
2307 }
2308
2309 // Make sure the computed size - based on the used portion of the
2310 // pages in use - matches the size we adjust during allocation.
2311 ASSERT(computed_size == Size());
2312}
2313
2314
kasper.lund7276f142008-07-30 08:49:36 +00002315// Slow case for normal allocation. Try in order: (1) allocate in the next
2316// page in the space, (2) allocate off the space's free list, (3) expand the
2317// space, (4) fail.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002318HeapObject* FixedSpace::SlowAllocateRaw(int size_in_bytes) {
2319 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002320 // Linear allocation in this space has failed. If there is another page
2321 // in the space, move to that page and allocate there. This allocation
2322 // should succeed.
2323 Page* current_page = TopPageOf(allocation_info_);
2324 if (current_page->next_page()->is_valid()) {
2325 return AllocateInNextPage(current_page, size_in_bytes);
2326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327
ager@chromium.org3811b432009-10-28 14:53:37 +00002328 // There is no next page in this space. Try free list allocation unless
2329 // that is currently forbidden. The fixed space free list implicitly assumes
2330 // that all free blocks are of the fixed size.
2331 if (!Heap::linear_allocation()) {
kasper.lund7276f142008-07-30 08:49:36 +00002332 Object* result = free_list_.Allocate();
2333 if (!result->IsFailure()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 accounting_stats_.AllocateBytes(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002335 return HeapObject::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 }
2337 }
kasper.lund7276f142008-07-30 08:49:36 +00002338
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002339 // Free list allocation failed and there is no next page. Fail if we have
2340 // hit the old generation size limit that should cause a garbage
2341 // collection.
2342 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2343 return NULL;
2344 }
2345
2346 // Try to expand the space and allocate in the new next page.
kasper.lund7276f142008-07-30 08:49:36 +00002347 ASSERT(!current_page->next_page()->is_valid());
2348 if (Expand(current_page)) {
2349 return AllocateInNextPage(current_page, size_in_bytes);
2350 }
2351
2352 // Finally, fail.
2353 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354}
2355
2356
kasper.lund7276f142008-07-30 08:49:36 +00002357// Move to the next page (there is assumed to be one) and allocate there.
2358// The top of page block is always wasted, because it is too small to hold a
2359// map.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002360HeapObject* FixedSpace::AllocateInNextPage(Page* current_page,
2361 int size_in_bytes) {
kasper.lund7276f142008-07-30 08:49:36 +00002362 ASSERT(current_page->next_page()->is_valid());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002363 ASSERT(current_page->ObjectAreaEnd() - allocation_info_.top == page_extra_);
2364 ASSERT_EQ(object_size_in_bytes_, size_in_bytes);
2365 accounting_stats_.WasteBytes(page_extra_);
kasper.lund7276f142008-07-30 08:49:36 +00002366 SetAllocationInfo(&allocation_info_, current_page->next_page());
2367 return AllocateLinearly(&allocation_info_, size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368}
2369
2370
2371#ifdef DEBUG
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002372void FixedSpace::ReportStatistics() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373 int pct = Available() * 100 / Capacity();
2374 PrintF(" capacity: %d, waste: %d, available: %d, %%%d\n",
2375 Capacity(), Waste(), Available(), pct);
2376
2377 // Report remembered set statistics.
2378 int rset_marked_pointers = 0;
2379 int cross_gen_pointers = 0;
2380
2381 PageIterator page_it(this, PageIterator::PAGES_IN_USE);
2382 while (page_it.has_next()) {
2383 Page* p = page_it.next();
2384
2385 for (Address rset_addr = p->RSetStart();
2386 rset_addr < p->RSetEnd();
2387 rset_addr += kIntSize) {
2388 int rset = Memory::int_at(rset_addr);
2389 if (rset != 0) {
2390 // Bits were set
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002391 int intoff =
2392 static_cast<int>(rset_addr - p->address() - Page::kRSetOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 int bitoff = 0;
2394 for (; bitoff < kBitsPerInt; ++bitoff) {
2395 if ((rset & (1 << bitoff)) != 0) {
2396 int bitpos = intoff*kBitsPerByte + bitoff;
2397 Address slot = p->OffsetToAddress(bitpos << kObjectAlignmentBits);
2398 Object** obj = reinterpret_cast<Object**>(slot);
2399 rset_marked_pointers++;
2400 if (Heap::InNewSpace(*obj))
2401 cross_gen_pointers++;
2402 }
2403 }
2404 }
2405 }
2406 }
2407
2408 pct = rset_marked_pointers == 0 ?
2409 0 : cross_gen_pointers * 100 / rset_marked_pointers;
2410 PrintF(" rset-marked pointers %d, to-new-space %d (%%%d)\n",
2411 rset_marked_pointers, cross_gen_pointers, pct);
2412
2413 ClearHistograms();
2414 HeapObjectIterator obj_it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002415 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next())
2416 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417 ReportHistogram(false);
2418}
2419
2420
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002421void FixedSpace::PrintRSet() { DoPrintRSet(name_); }
2422#endif
2423
2424
2425// -----------------------------------------------------------------------------
2426// MapSpace implementation
2427
2428void MapSpace::PrepareForMarkCompact(bool will_compact) {
2429 // Call prepare of the super class.
2430 FixedSpace::PrepareForMarkCompact(will_compact);
2431
2432 if (will_compact) {
2433 // Initialize map index entry.
2434 int page_count = 0;
2435 PageIterator it(this, PageIterator::ALL_PAGES);
2436 while (it.has_next()) {
2437 ASSERT_MAP_PAGE_INDEX(page_count);
2438
2439 Page* p = it.next();
2440 ASSERT(p->mc_page_index == page_count);
2441
2442 page_addresses_[page_count++] = p->address();
2443 }
2444 }
2445}
2446
2447
2448#ifdef DEBUG
2449void MapSpace::VerifyObject(HeapObject* object) {
2450 // The object should be a map or a free-list node.
2451 ASSERT(object->IsMap() || object->IsByteArray());
2452}
2453#endif
2454
2455
2456// -----------------------------------------------------------------------------
2457// GlobalPropertyCellSpace implementation
2458
2459#ifdef DEBUG
2460void CellSpace::VerifyObject(HeapObject* object) {
2461 // The object should be a global object property cell or a free-list node.
2462 ASSERT(object->IsJSGlobalPropertyCell() ||
2463 object->map() == Heap::two_pointer_filler_map());
2464}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465#endif
2466
2467
2468// -----------------------------------------------------------------------------
2469// LargeObjectIterator
2470
2471LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space) {
2472 current_ = space->first_chunk_;
2473 size_func_ = NULL;
2474}
2475
2476
2477LargeObjectIterator::LargeObjectIterator(LargeObjectSpace* space,
2478 HeapObjectCallback size_func) {
2479 current_ = space->first_chunk_;
2480 size_func_ = size_func;
2481}
2482
2483
2484HeapObject* LargeObjectIterator::next() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002485 if (current_ == NULL) return NULL;
2486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 HeapObject* object = current_->GetObject();
2488 current_ = current_->next();
2489 return object;
2490}
2491
2492
2493// -----------------------------------------------------------------------------
2494// LargeObjectChunk
2495
2496LargeObjectChunk* LargeObjectChunk::New(int size_in_bytes,
kasper.lund7276f142008-07-30 08:49:36 +00002497 size_t* chunk_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002498 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 size_t requested = ChunkSizeFor(size_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00002500 void* mem = MemoryAllocator::AllocateRawMemory(requested,
2501 chunk_size,
2502 executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503 if (mem == NULL) return NULL;
2504 LOG(NewEvent("LargeObjectChunk", mem, *chunk_size));
2505 if (*chunk_size < requested) {
2506 MemoryAllocator::FreeRawMemory(mem, *chunk_size);
2507 LOG(DeleteEvent("LargeObjectChunk", mem));
2508 return NULL;
2509 }
2510 return reinterpret_cast<LargeObjectChunk*>(mem);
2511}
2512
2513
2514int LargeObjectChunk::ChunkSizeFor(int size_in_bytes) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002515 int os_alignment = static_cast<int>(OS::AllocateAlignment());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 if (os_alignment < Page::kPageSize)
2517 size_in_bytes += (Page::kPageSize - os_alignment);
2518 return size_in_bytes + Page::kObjectStartOffset;
2519}
2520
2521// -----------------------------------------------------------------------------
2522// LargeObjectSpace
2523
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002524LargeObjectSpace::LargeObjectSpace(AllocationSpace id)
2525 : Space(id, NOT_EXECUTABLE), // Managed on a per-allocation basis
kasper.lund7276f142008-07-30 08:49:36 +00002526 first_chunk_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 size_(0),
2528 page_count_(0) {}
2529
2530
2531bool LargeObjectSpace::Setup() {
2532 first_chunk_ = NULL;
2533 size_ = 0;
2534 page_count_ = 0;
2535 return true;
2536}
2537
2538
2539void LargeObjectSpace::TearDown() {
2540 while (first_chunk_ != NULL) {
2541 LargeObjectChunk* chunk = first_chunk_;
2542 first_chunk_ = first_chunk_->next();
2543 LOG(DeleteEvent("LargeObjectChunk", chunk->address()));
2544 MemoryAllocator::FreeRawMemory(chunk->address(), chunk->size());
2545 }
2546
2547 size_ = 0;
2548 page_count_ = 0;
2549}
2550
2551
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00002552#ifdef ENABLE_HEAP_PROTECTION
2553
2554void LargeObjectSpace::Protect() {
2555 LargeObjectChunk* chunk = first_chunk_;
2556 while (chunk != NULL) {
2557 MemoryAllocator::Protect(chunk->address(), chunk->size());
2558 chunk = chunk->next();
2559 }
2560}
2561
2562
2563void LargeObjectSpace::Unprotect() {
2564 LargeObjectChunk* chunk = first_chunk_;
2565 while (chunk != NULL) {
2566 bool is_code = chunk->GetObject()->IsCode();
2567 MemoryAllocator::Unprotect(chunk->address(), chunk->size(),
2568 is_code ? EXECUTABLE : NOT_EXECUTABLE);
2569 chunk = chunk->next();
2570 }
2571}
2572
2573#endif
2574
2575
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576Object* LargeObjectSpace::AllocateRawInternal(int requested_size,
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002577 int object_size,
2578 Executability executable) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 ASSERT(0 < object_size && object_size <= requested_size);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002580
2581 // Check if we want to force a GC before growing the old space further.
2582 // If so, fail the allocation.
2583 if (!Heap::always_allocate() && Heap::OldGenerationAllocationLimitReached()) {
2584 return Failure::RetryAfterGC(requested_size, identity());
2585 }
2586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 size_t chunk_size;
2588 LargeObjectChunk* chunk =
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002589 LargeObjectChunk::New(requested_size, &chunk_size, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 if (chunk == NULL) {
kasper.lund7276f142008-07-30 08:49:36 +00002591 return Failure::RetryAfterGC(requested_size, identity());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 }
2593
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002594 size_ += static_cast<int>(chunk_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595 page_count_++;
2596 chunk->set_next(first_chunk_);
2597 chunk->set_size(chunk_size);
2598 first_chunk_ = chunk;
2599
2600 // Set the object address and size in the page header and clear its
2601 // remembered set.
2602 Page* page = Page::FromAddress(RoundUp(chunk->address(), Page::kPageSize));
2603 Address object_address = page->ObjectAreaStart();
2604 // Clear the low order bit of the second word in the page to flag it as a
2605 // large object page. If the chunk_size happened to be written there, its
2606 // low order bit should already be clear.
2607 ASSERT((chunk_size & 0x1) == 0);
2608 page->is_normal_page &= ~0x1;
2609 page->ClearRSet();
2610 int extra_bytes = requested_size - object_size;
2611 if (extra_bytes > 0) {
2612 // The extra memory for the remembered set should be cleared.
2613 memset(object_address + object_size, 0, extra_bytes);
2614 }
2615
2616 return HeapObject::FromAddress(object_address);
2617}
2618
2619
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002620Object* LargeObjectSpace::AllocateRawCode(int size_in_bytes) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 ASSERT(0 < size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002622 return AllocateRawInternal(size_in_bytes,
2623 size_in_bytes,
2624 EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625}
2626
2627
2628Object* LargeObjectSpace::AllocateRawFixedArray(int size_in_bytes) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002629 ASSERT(0 < size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 int extra_rset_bytes = ExtraRSetBytesFor(size_in_bytes);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002631 return AllocateRawInternal(size_in_bytes + extra_rset_bytes,
2632 size_in_bytes,
2633 NOT_EXECUTABLE);
2634}
2635
2636
2637Object* LargeObjectSpace::AllocateRaw(int size_in_bytes) {
2638 ASSERT(0 < size_in_bytes);
2639 return AllocateRawInternal(size_in_bytes,
2640 size_in_bytes,
2641 NOT_EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642}
2643
2644
2645// GC support
2646Object* LargeObjectSpace::FindObject(Address a) {
2647 for (LargeObjectChunk* chunk = first_chunk_;
2648 chunk != NULL;
2649 chunk = chunk->next()) {
2650 Address chunk_address = chunk->address();
2651 if (chunk_address <= a && a < chunk_address + chunk->size()) {
2652 return chunk->GetObject();
2653 }
2654 }
2655 return Failure::Exception();
2656}
2657
2658
2659void LargeObjectSpace::ClearRSet() {
2660 ASSERT(Page::is_rset_in_use());
2661
2662 LargeObjectIterator it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002663 for (HeapObject* object = it.next(); object != NULL; object = it.next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 // We only have code, sequential strings, or fixed arrays in large
2665 // object space, and only fixed arrays need remembered set support.
2666 if (object->IsFixedArray()) {
2667 // Clear the normal remembered set region of the page;
2668 Page* page = Page::FromAddress(object->address());
2669 page->ClearRSet();
2670
2671 // Clear the extra remembered set.
2672 int size = object->Size();
2673 int extra_rset_bytes = ExtraRSetBytesFor(size);
2674 memset(object->address() + size, 0, extra_rset_bytes);
2675 }
2676 }
2677}
2678
2679
2680void LargeObjectSpace::IterateRSet(ObjectSlotCallback copy_object_func) {
2681 ASSERT(Page::is_rset_in_use());
2682
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002683 static void* lo_rset_histogram = StatsTable::CreateHistogram(
2684 "V8.RSetLO",
2685 0,
2686 // Keeping this histogram's buckets the same as the paged space histogram.
2687 Page::kObjectAreaSize / kPointerSize,
2688 30);
2689
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 LargeObjectIterator it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002691 for (HeapObject* object = it.next(); object != NULL; object = it.next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 // We only have code, sequential strings, or fixed arrays in large
2693 // object space, and only fixed arrays can possibly contain pointers to
2694 // the young generation.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 if (object->IsFixedArray()) {
2696 // Iterate the normal page remembered set range.
2697 Page* page = Page::FromAddress(object->address());
2698 Address object_end = object->address() + object->Size();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002699 int count = Heap::IterateRSetRange(page->ObjectAreaStart(),
2700 Min(page->ObjectAreaEnd(), object_end),
2701 page->RSetStart(),
2702 copy_object_func);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703
2704 // Iterate the extra array elements.
2705 if (object_end > page->ObjectAreaEnd()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002706 count += Heap::IterateRSetRange(page->ObjectAreaEnd(), object_end,
2707 object_end, copy_object_func);
2708 }
2709 if (lo_rset_histogram != NULL) {
2710 StatsTable::AddHistogramSample(lo_rset_histogram, count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 }
2712 }
2713 }
2714}
2715
2716
2717void LargeObjectSpace::FreeUnmarkedObjects() {
2718 LargeObjectChunk* previous = NULL;
2719 LargeObjectChunk* current = first_chunk_;
2720 while (current != NULL) {
2721 HeapObject* object = current->GetObject();
kasper.lund7276f142008-07-30 08:49:36 +00002722 if (object->IsMarked()) {
2723 object->ClearMark();
2724 MarkCompactCollector::tracer()->decrement_marked_count();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 previous = current;
2726 current = current->next();
2727 } else {
2728 Address chunk_address = current->address();
2729 size_t chunk_size = current->size();
2730
2731 // Cut the chunk out from the chunk list.
2732 current = current->next();
2733 if (previous == NULL) {
2734 first_chunk_ = current;
2735 } else {
2736 previous->set_next(current);
2737 }
2738
2739 // Free the chunk.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002740 MarkCompactCollector::ReportDeleteIfNeeded(object);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002741 size_ -= static_cast<int>(chunk_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002742 page_count_--;
2743 MemoryAllocator::FreeRawMemory(chunk_address, chunk_size);
2744 LOG(DeleteEvent("LargeObjectChunk", chunk_address));
2745 }
2746 }
2747}
2748
2749
2750bool LargeObjectSpace::Contains(HeapObject* object) {
2751 Address address = object->address();
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002752 if (Heap::new_space()->Contains(address)) {
2753 return false;
2754 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755 Page* page = Page::FromAddress(address);
2756
2757 SLOW_ASSERT(!page->IsLargeObjectPage()
2758 || !FindObject(address)->IsFailure());
2759
2760 return page->IsLargeObjectPage();
2761}
2762
2763
2764#ifdef DEBUG
2765// We do not assume that the large object iterator works, because it depends
2766// on the invariants we are checking during verification.
2767void LargeObjectSpace::Verify() {
2768 for (LargeObjectChunk* chunk = first_chunk_;
2769 chunk != NULL;
2770 chunk = chunk->next()) {
2771 // Each chunk contains an object that starts at the large object page's
2772 // object area start.
2773 HeapObject* object = chunk->GetObject();
2774 Page* page = Page::FromAddress(object->address());
2775 ASSERT(object->address() == page->ObjectAreaStart());
2776
2777 // The first word should be a map, and we expect all map pointers to be
2778 // in map space.
2779 Map* map = object->map();
2780 ASSERT(map->IsMap());
2781 ASSERT(Heap::map_space()->Contains(map));
2782
ager@chromium.orga1645e22009-09-09 19:27:10 +00002783 // We have only code, sequential strings, external strings
2784 // (sequential strings that have been morphed into external
2785 // strings), fixed arrays, and byte arrays in large object space.
2786 ASSERT(object->IsCode() || object->IsSeqString() ||
2787 object->IsExternalString() || object->IsFixedArray() ||
2788 object->IsByteArray());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789
2790 // The object itself should look OK.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002791 object->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792
2793 // Byte arrays and strings don't have interior pointers.
2794 if (object->IsCode()) {
2795 VerifyPointersVisitor code_visitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796 object->IterateBody(map->instance_type(),
2797 object->Size(),
2798 &code_visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799 } else if (object->IsFixedArray()) {
2800 // We loop over fixed arrays ourselves, rather then using the visitor,
2801 // because the visitor doesn't support the start/offset iteration
2802 // needed for IsRSetSet.
2803 FixedArray* array = FixedArray::cast(object);
2804 for (int j = 0; j < array->length(); j++) {
2805 Object* element = array->get(j);
2806 if (element->IsHeapObject()) {
2807 HeapObject* element_object = HeapObject::cast(element);
2808 ASSERT(Heap::Contains(element_object));
2809 ASSERT(element_object->map()->IsMap());
2810 if (Heap::InNewSpace(element_object)) {
2811 ASSERT(Page::IsRSetSet(object->address(),
2812 FixedArray::kHeaderSize + j * kPointerSize));
2813 }
2814 }
2815 }
2816 }
2817 }
2818}
2819
2820
2821void LargeObjectSpace::Print() {
2822 LargeObjectIterator it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002823 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
2824 obj->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 }
2826}
2827
2828
2829void LargeObjectSpace::ReportStatistics() {
2830 PrintF(" size: %d\n", size_);
2831 int num_objects = 0;
2832 ClearHistograms();
2833 LargeObjectIterator it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002834 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 num_objects++;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002836 CollectHistogramInfo(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837 }
2838
2839 PrintF(" number of objects %d\n", num_objects);
2840 if (num_objects > 0) ReportHistogram(false);
2841}
2842
2843
2844void LargeObjectSpace::CollectCodeStatistics() {
2845 LargeObjectIterator obj_it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002846 for (HeapObject* obj = obj_it.next(); obj != NULL; obj = obj_it.next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 if (obj->IsCode()) {
2848 Code* code = Code::cast(obj);
2849 code_kind_statistics[code->kind()] += code->Size();
2850 }
2851 }
2852}
2853
2854
2855void LargeObjectSpace::PrintRSet() {
2856 LargeObjectIterator it(this);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002857 for (HeapObject* object = it.next(); object != NULL; object = it.next()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858 if (object->IsFixedArray()) {
2859 Page* page = Page::FromAddress(object->address());
2860
2861 Address allocation_top = object->address() + object->Size();
2862 PrintF("large page 0x%x:\n", page);
2863 PrintRSetRange(page->RSetStart(), page->RSetEnd(),
2864 reinterpret_cast<Object**>(object->address()),
2865 allocation_top);
2866 int extra_array_bytes = object->Size() - Page::kObjectAreaSize;
2867 int extra_rset_bits = RoundUp(extra_array_bytes / kPointerSize,
2868 kBitsPerInt);
2869 PrintF("------------------------------------------------------------"
2870 "-----------\n");
2871 PrintRSetRange(allocation_top,
2872 allocation_top + extra_rset_bits / kBitsPerByte,
2873 reinterpret_cast<Object**>(object->address()
2874 + Page::kObjectAreaSize),
2875 allocation_top);
2876 PrintF("\n");
2877 }
2878 }
2879}
2880#endif // DEBUG
2881
2882} } // namespace v8::internal