blob: 7178b5783b94be7a063171fcc15445bd47d3c0f4 [file] [log] [blame]
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_SPACES_INL_H_
29#define V8_SPACES_INL_H_
30
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +000031#include "heap-profiler.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000032#include "isolate.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "spaces.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000034#include "v8memory.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
39
40// -----------------------------------------------------------------------------
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000041// Bitmap
42
43void Bitmap::Clear(MemoryChunk* chunk) {
44 Bitmap* bitmap = chunk->markbits();
45 for (int i = 0; i < bitmap->CellsCount(); i++) bitmap->cells()[i] = 0;
46 chunk->ResetLiveBytes();
47}
48
49
50// -----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051// PageIterator
52
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000053
54PageIterator::PageIterator(PagedSpace* space)
55 : space_(space),
56 prev_page_(&space->anchor_),
57 next_page_(prev_page_->next_page()) { }
58
59
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060bool PageIterator::has_next() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000061 return next_page_ != &space_->anchor_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062}
63
64
65Page* PageIterator::next() {
66 ASSERT(has_next());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000067 prev_page_ = next_page_;
68 next_page_ = next_page_->next_page();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000069 return prev_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070}
71
72
73// -----------------------------------------------------------------------------
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000074// NewSpacePageIterator
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000076
77NewSpacePageIterator::NewSpacePageIterator(NewSpace* space)
78 : prev_page_(NewSpacePage::FromAddress(space->ToSpaceStart())->prev_page()),
79 next_page_(NewSpacePage::FromAddress(space->ToSpaceStart())),
80 last_page_(NewSpacePage::FromLimit(space->ToSpaceEnd())) { }
81
82NewSpacePageIterator::NewSpacePageIterator(SemiSpace* space)
83 : prev_page_(space->anchor()),
84 next_page_(prev_page_->next_page()),
85 last_page_(prev_page_->prev_page()) { }
86
87NewSpacePageIterator::NewSpacePageIterator(Address start, Address limit)
88 : prev_page_(NewSpacePage::FromAddress(start)->prev_page()),
89 next_page_(NewSpacePage::FromAddress(start)),
90 last_page_(NewSpacePage::FromLimit(limit)) {
91 SemiSpace::AssertValidRange(start, limit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092}
93
94
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000095bool NewSpacePageIterator::has_next() {
96 return prev_page_ != last_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097}
98
99
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000100NewSpacePage* NewSpacePageIterator::next() {
101 ASSERT(has_next());
102 prev_page_ = next_page_;
103 next_page_ = next_page_->next_page();
104 return prev_page_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105}
106
107
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000108// -----------------------------------------------------------------------------
109// HeapObjectIterator
110HeapObject* HeapObjectIterator::FromCurrentPage() {
111 while (cur_addr_ != cur_end_) {
112 if (cur_addr_ == space_->top() && cur_addr_ != space_->limit()) {
113 cur_addr_ = space_->limit();
114 continue;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000115 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000116 HeapObject* obj = HeapObject::FromAddress(cur_addr_);
117 int obj_size = (size_func_ == NULL) ? obj->Size() : size_func_(obj);
118 cur_addr_ += obj_size;
119 ASSERT(cur_addr_ <= cur_end_);
120 if (!obj->IsFiller()) {
121 ASSERT_OBJECT_SIZE(obj_size);
122 return obj;
123 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000124 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000125 return NULL;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000126}
127
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129// -----------------------------------------------------------------------------
130// MemoryAllocator
131
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000132#ifdef ENABLE_HEAP_PROTECTION
133
134void MemoryAllocator::Protect(Address start, size_t size) {
135 OS::Protect(start, size);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000136}
137
138
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000139void MemoryAllocator::Unprotect(Address start,
140 size_t size,
141 Executability executable) {
142 OS::Unprotect(start, size, executable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143}
144
145
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000146void MemoryAllocator::ProtectChunkFromPage(Page* page) {
147 int id = GetChunkId(page);
148 OS::Protect(chunks_[id].address(), chunks_[id].size());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149}
150
151
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000152void MemoryAllocator::UnprotectChunkFromPage(Page* page) {
153 int id = GetChunkId(page);
154 OS::Unprotect(chunks_[id].address(), chunks_[id].size(),
155 chunks_[id].owner()->executable() == EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156}
157
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000158#endif
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000159
160
kasper.lund7276f142008-07-30 08:49:36 +0000161// --------------------------------------------------------------------------
162// PagedSpace
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000163Page* Page::Initialize(Heap* heap,
164 MemoryChunk* chunk,
165 Executability executable,
166 PagedSpace* owner) {
167 Page* page = reinterpret_cast<Page*>(chunk);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000168 ASSERT(page->area_size() <= kNonCodeObjectAreaSize);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000169 ASSERT(chunk->owner() == owner);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000170 owner->IncreaseCapacity(page->area_size());
171 owner->Free(page->area_start(), page->area_size());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000172
173 heap->incremental_marking()->SetOldSpacePageFlags(chunk);
174
175 return page;
176}
177
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178
179bool PagedSpace::Contains(Address addr) {
180 Page* p = Page::FromAddress(addr);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000181 if (!p->is_valid()) return false;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000182 return p->owner() == this;
183}
184
185
186void MemoryChunk::set_scan_on_scavenge(bool scan) {
187 if (scan) {
188 if (!scan_on_scavenge()) heap_->increment_scan_on_scavenge_pages();
189 SetFlag(SCAN_ON_SCAVENGE);
190 } else {
191 if (scan_on_scavenge()) heap_->decrement_scan_on_scavenge_pages();
192 ClearFlag(SCAN_ON_SCAVENGE);
193 }
194 heap_->incremental_marking()->SetOldSpacePageFlags(this);
195}
196
197
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000198MemoryChunk* MemoryChunk::FromAnyPointerAddress(Heap* heap, Address addr) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000199 MemoryChunk* maybe = reinterpret_cast<MemoryChunk*>(
200 OffsetFrom(addr) & ~Page::kPageAlignmentMask);
201 if (maybe->owner() != NULL) return maybe;
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000202 LargeObjectIterator iterator(heap->lo_space());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000203 for (HeapObject* o = iterator.Next(); o != NULL; o = iterator.Next()) {
204 // Fixed arrays are the only pointer-containing objects in large object
205 // space.
206 if (o->IsFixedArray()) {
207 MemoryChunk* chunk = MemoryChunk::FromAddress(o->address());
208 if (chunk->Contains(addr)) {
209 return chunk;
210 }
211 }
212 }
213 UNREACHABLE();
214 return NULL;
215}
216
217
danno@chromium.org72204d52012-10-31 10:02:10 +0000218void MemoryChunk::UpdateHighWaterMark(Address mark) {
219 if (mark == NULL) return;
220 // Need to subtract one from the mark because when a chunk is full the
221 // top points to the next address after the chunk, which effectively belongs
222 // to another chunk. See the comment to Page::FromAllocationTop.
223 MemoryChunk* chunk = MemoryChunk::FromAddress(mark - 1);
224 int new_mark = static_cast<int>(mark - chunk->address());
225 if (new_mark > chunk->high_water_mark_) {
226 chunk->high_water_mark_ = new_mark;
227 }
228}
229
230
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000231PointerChunkIterator::PointerChunkIterator(Heap* heap)
232 : state_(kOldPointerState),
233 old_pointer_iterator_(heap->old_pointer_space()),
234 map_iterator_(heap->map_space()),
235 lo_iterator_(heap->lo_space()) { }
236
237
238Page* Page::next_page() {
239 ASSERT(next_chunk()->owner() == owner());
240 return static_cast<Page*>(next_chunk());
241}
242
243
244Page* Page::prev_page() {
245 ASSERT(prev_chunk()->owner() == owner());
246 return static_cast<Page*>(prev_chunk());
247}
248
249
250void Page::set_next_page(Page* page) {
251 ASSERT(page->owner() == owner());
252 set_next_chunk(page);
253}
254
255
256void Page::set_prev_page(Page* page) {
257 ASSERT(page->owner() == owner());
258 set_prev_chunk(page);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000259}
260
261
kasper.lund7276f142008-07-30 08:49:36 +0000262// Try linear allocation in the page of alloc_info's allocation top. Does
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000263// not contain slow case logic (e.g. move to the next page or try free list
kasper.lund7276f142008-07-30 08:49:36 +0000264// allocation) so it can be used by all the allocation functions and for all
265// the paged spaces.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000266HeapObject* PagedSpace::AllocateLinearly(int size_in_bytes) {
267 Address current_top = allocation_info_.top;
kasper.lund7276f142008-07-30 08:49:36 +0000268 Address new_top = current_top + size_in_bytes;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000269 if (new_top > allocation_info_.limit) return NULL;
kasper.lund7276f142008-07-30 08:49:36 +0000270
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000271 allocation_info_.top = new_top;
kasper.lund7276f142008-07-30 08:49:36 +0000272 return HeapObject::FromAddress(current_top);
273}
274
275
276// Raw allocation.
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000277MaybeObject* PagedSpace::AllocateRaw(int size_in_bytes,
278 AllocationType event) {
279 HeapProfiler* profiler = heap()->isolate()->heap_profiler();
280
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000281 HeapObject* object = AllocateLinearly(size_in_bytes);
282 if (object != NULL) {
283 if (identity() == CODE_SPACE) {
284 SkipList::Update(object->address(), size_in_bytes);
285 }
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000286 if (event == NEW_OBJECT && profiler->is_tracking_allocations()) {
287 profiler->NewObjectEvent(object->address(), size_in_bytes);
288 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000289 return object;
290 }
291
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000292 ASSERT(!heap()->linear_allocation() ||
293 (anchor_.next_chunk() == &anchor_ &&
294 anchor_.prev_chunk() == &anchor_));
295
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000296 object = free_list_.Allocate(size_in_bytes);
297 if (object != NULL) {
298 if (identity() == CODE_SPACE) {
299 SkipList::Update(object->address(), size_in_bytes);
300 }
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000301 if (event == NEW_OBJECT && profiler->is_tracking_allocations()) {
302 profiler->NewObjectEvent(object->address(), size_in_bytes);
303 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000304 return object;
305 }
kasper.lund7276f142008-07-30 08:49:36 +0000306
307 object = SlowAllocateRaw(size_in_bytes);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000308 if (object != NULL) {
309 if (identity() == CODE_SPACE) {
310 SkipList::Update(object->address(), size_in_bytes);
311 }
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000312 if (event == NEW_OBJECT && profiler->is_tracking_allocations()) {
313 profiler->NewObjectEvent(object->address(), size_in_bytes);
314 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000315 return object;
316 }
kasper.lund7276f142008-07-30 08:49:36 +0000317
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000318 return Failure::RetryAfterGC(identity());
kasper.lund7276f142008-07-30 08:49:36 +0000319}
320
321
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322// -----------------------------------------------------------------------------
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000323// NewSpace
danno@chromium.orgc612e022011-11-10 11:38:15 +0000324
325
326MaybeObject* NewSpace::AllocateRaw(int size_in_bytes) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000327 Address old_top = allocation_info_.top;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000328#ifdef DEBUG
329 // If we are stressing compaction we waste some memory in new space
330 // in order to get more frequent GCs.
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000331 if (FLAG_stress_compaction && !heap()->linear_allocation()) {
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000332 if (allocation_info_.limit - old_top >= size_in_bytes * 4) {
333 int filler_size = size_in_bytes * 4;
334 for (int i = 0; i < filler_size; i += kPointerSize) {
335 *(reinterpret_cast<Object**>(old_top + i)) =
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000336 heap()->one_pointer_filler_map();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000337 }
338 old_top += filler_size;
339 allocation_info_.top += filler_size;
340 }
341 }
342#endif
343
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000344 if (allocation_info_.limit - old_top < size_in_bytes) {
danno@chromium.orgc612e022011-11-10 11:38:15 +0000345 return SlowAllocateRaw(size_in_bytes);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000346 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000348 HeapObject* obj = HeapObject::FromAddress(old_top);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000349 allocation_info_.top += size_in_bytes;
350 ASSERT_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000352 HeapProfiler* profiler = heap()->isolate()->heap_profiler();
353 if (profiler != NULL && profiler->is_tracking_allocations()) {
354 profiler->NewObjectEvent(obj->address(), size_in_bytes);
355 }
356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 return obj;
358}
359
ager@chromium.org3811b432009-10-28 14:53:37 +0000360
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000361LargePage* LargePage::Initialize(Heap* heap, MemoryChunk* chunk) {
362 heap->incremental_marking()->SetOldSpacePageFlags(chunk);
363 return static_cast<LargePage*>(chunk);
364}
365
366
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000367intptr_t LargeObjectSpace::Available() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000368 return ObjectSizeFor(heap()->isolate()->memory_allocator()->Available());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000369}
370
371
ager@chromium.org3811b432009-10-28 14:53:37 +0000372bool FreeListNode::IsFreeListNode(HeapObject* object) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000373 Map* map = object->map();
374 Heap* heap = object->GetHeap();
375 return map == heap->raw_unchecked_free_space_map()
376 || map == heap->raw_unchecked_one_pointer_filler_map()
377 || map == heap->raw_unchecked_two_pointer_filler_map();
ager@chromium.org3811b432009-10-28 14:53:37 +0000378}
379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380} } // namespace v8::internal
381
382#endif // V8_SPACES_INL_H_