Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_HEAP_SPACES_INL_H_ |
| 6 | #define V8_HEAP_SPACES_INL_H_ |
| 7 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/heap/incremental-marking.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/heap/spaces.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/isolate.h" |
| 11 | #include "src/msan.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | #include "src/profiler/heap-profiler.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13 | #include "src/v8memory.h" |
| 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | |
| 18 | |
| 19 | // ----------------------------------------------------------------------------- |
| 20 | // Bitmap |
| 21 | |
| 22 | void Bitmap::Clear(MemoryChunk* chunk) { |
| 23 | Bitmap* bitmap = chunk->markbits(); |
| 24 | for (int i = 0; i < bitmap->CellsCount(); i++) bitmap->cells()[i] = 0; |
| 25 | chunk->ResetLiveBytes(); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | // ----------------------------------------------------------------------------- |
| 30 | // PageIterator |
| 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | PageIterator::PageIterator(PagedSpace* space) |
| 33 | : space_(space), |
| 34 | prev_page_(&space->anchor_), |
| 35 | next_page_(prev_page_->next_page()) {} |
| 36 | |
| 37 | |
| 38 | bool PageIterator::has_next() { return next_page_ != &space_->anchor_; } |
| 39 | |
| 40 | |
| 41 | Page* PageIterator::next() { |
| 42 | DCHECK(has_next()); |
| 43 | prev_page_ = next_page_; |
| 44 | next_page_ = next_page_->next_page(); |
| 45 | return prev_page_; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 50 | // SemiSpaceIterator |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 52 | HeapObject* SemiSpaceIterator::Next() { |
| 53 | while (current_ != limit_) { |
| 54 | if (NewSpacePage::IsAtEnd(current_)) { |
| 55 | NewSpacePage* page = NewSpacePage::FromLimit(current_); |
| 56 | page = page->next_page(); |
| 57 | DCHECK(!page->is_anchor()); |
| 58 | current_ = page->area_start(); |
| 59 | if (current_ == limit_) return nullptr; |
| 60 | } |
| 61 | HeapObject* object = HeapObject::FromAddress(current_); |
| 62 | current_ += object->Size(); |
| 63 | if (!object->IsFiller()) { |
| 64 | return object; |
| 65 | } |
| 66 | } |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | HeapObject* SemiSpaceIterator::next_object() { return Next(); } |
| 72 | |
| 73 | |
| 74 | // ----------------------------------------------------------------------------- |
| 75 | // NewSpacePageIterator |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | |
| 77 | NewSpacePageIterator::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 | |
| 82 | NewSpacePageIterator::NewSpacePageIterator(SemiSpace* space) |
| 83 | : prev_page_(space->anchor()), |
| 84 | next_page_(prev_page_->next_page()), |
| 85 | last_page_(prev_page_->prev_page()) {} |
| 86 | |
| 87 | NewSpacePageIterator::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); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | bool NewSpacePageIterator::has_next() { return prev_page_ != last_page_; } |
| 96 | |
| 97 | |
| 98 | NewSpacePage* NewSpacePageIterator::next() { |
| 99 | DCHECK(has_next()); |
| 100 | prev_page_ = next_page_; |
| 101 | next_page_ = next_page_->next_page(); |
| 102 | return prev_page_; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | // ----------------------------------------------------------------------------- |
| 107 | // HeapObjectIterator |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 108 | |
| 109 | HeapObject* HeapObjectIterator::Next() { |
| 110 | do { |
| 111 | HeapObject* next_obj = FromCurrentPage(); |
| 112 | if (next_obj != NULL) return next_obj; |
| 113 | } while (AdvanceToNextPage()); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | HeapObject* HeapObjectIterator::next_object() { return Next(); } |
| 119 | |
| 120 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | HeapObject* HeapObjectIterator::FromCurrentPage() { |
| 122 | while (cur_addr_ != cur_end_) { |
| 123 | if (cur_addr_ == space_->top() && cur_addr_ != space_->limit()) { |
| 124 | cur_addr_ = space_->limit(); |
| 125 | continue; |
| 126 | } |
| 127 | HeapObject* obj = HeapObject::FromAddress(cur_addr_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | int obj_size = obj->Size(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | cur_addr_ += obj_size; |
| 130 | DCHECK(cur_addr_ <= cur_end_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 131 | // TODO(hpayer): Remove the debugging code. |
| 132 | if (cur_addr_ > cur_end_) { |
| 133 | space_->heap()->isolate()->PushStackTraceAndDie(0xaaaaaaaa, obj, NULL, |
| 134 | obj_size); |
| 135 | } |
| 136 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | if (!obj->IsFiller()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | if (obj->IsCode()) { |
| 139 | DCHECK_EQ(space_, space_->heap()->code_space()); |
| 140 | DCHECK_CODEOBJECT_SIZE(obj_size, space_); |
| 141 | } else { |
| 142 | DCHECK_OBJECT_SIZE(obj_size); |
| 143 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | return obj; |
| 145 | } |
| 146 | } |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | // ----------------------------------------------------------------------------- |
| 152 | // MemoryAllocator |
| 153 | |
| 154 | #ifdef ENABLE_HEAP_PROTECTION |
| 155 | |
| 156 | void MemoryAllocator::Protect(Address start, size_t size) { |
| 157 | base::OS::Protect(start, size); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void MemoryAllocator::Unprotect(Address start, size_t size, |
| 162 | Executability executable) { |
| 163 | base::OS::Unprotect(start, size, executable); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void MemoryAllocator::ProtectChunkFromPage(Page* page) { |
| 168 | int id = GetChunkId(page); |
| 169 | base::OS::Protect(chunks_[id].address(), chunks_[id].size()); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | void MemoryAllocator::UnprotectChunkFromPage(Page* page) { |
| 174 | int id = GetChunkId(page); |
| 175 | base::OS::Unprotect(chunks_[id].address(), chunks_[id].size(), |
| 176 | chunks_[id].owner()->executable() == EXECUTABLE); |
| 177 | } |
| 178 | |
| 179 | #endif |
| 180 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 181 | // ----------------------------------------------------------------------------- |
| 182 | // SemiSpace |
| 183 | |
| 184 | bool SemiSpace::Contains(HeapObject* o) { |
| 185 | return id_ == kToSpace |
| 186 | ? MemoryChunk::FromAddress(o->address())->InToSpace() |
| 187 | : MemoryChunk::FromAddress(o->address())->InFromSpace(); |
| 188 | } |
| 189 | |
| 190 | bool SemiSpace::Contains(Object* o) { |
| 191 | return o->IsHeapObject() && Contains(HeapObject::cast(o)); |
| 192 | } |
| 193 | |
| 194 | bool SemiSpace::ContainsSlow(Address a) { |
| 195 | NewSpacePageIterator it(this); |
| 196 | while (it.has_next()) { |
| 197 | if (it.next() == MemoryChunk::FromAddress(a)) return true; |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------------- |
| 203 | // NewSpace |
| 204 | |
| 205 | bool NewSpace::Contains(HeapObject* o) { |
| 206 | return MemoryChunk::FromAddress(o->address())->InNewSpace(); |
| 207 | } |
| 208 | |
| 209 | bool NewSpace::Contains(Object* o) { |
| 210 | return o->IsHeapObject() && Contains(HeapObject::cast(o)); |
| 211 | } |
| 212 | |
| 213 | bool NewSpace::ContainsSlow(Address a) { |
| 214 | return from_space_.ContainsSlow(a) || to_space_.ContainsSlow(a); |
| 215 | } |
| 216 | |
| 217 | bool NewSpace::ToSpaceContainsSlow(Address a) { |
| 218 | return to_space_.ContainsSlow(a); |
| 219 | } |
| 220 | |
| 221 | bool NewSpace::FromSpaceContainsSlow(Address a) { |
| 222 | return from_space_.ContainsSlow(a); |
| 223 | } |
| 224 | |
| 225 | bool NewSpace::ToSpaceContains(Object* o) { return to_space_.Contains(o); } |
| 226 | bool NewSpace::FromSpaceContains(Object* o) { return from_space_.Contains(o); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 227 | |
| 228 | // -------------------------------------------------------------------------- |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 229 | // AllocationResult |
| 230 | |
| 231 | AllocationSpace AllocationResult::RetrySpace() { |
| 232 | DCHECK(IsRetry()); |
| 233 | return static_cast<AllocationSpace>(Smi::cast(object_)->value()); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | // -------------------------------------------------------------------------- |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 238 | // PagedSpace |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 239 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | Page* Page::Initialize(Heap* heap, MemoryChunk* chunk, Executability executable, |
| 241 | PagedSpace* owner) { |
| 242 | Page* page = reinterpret_cast<Page*>(chunk); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 243 | page->mutex_ = new base::Mutex(); |
| 244 | DCHECK(page->area_size() <= kAllocatableMemory); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 245 | DCHECK(chunk->owner() == owner); |
| 246 | owner->IncreaseCapacity(page->area_size()); |
| 247 | owner->Free(page->area_start(), page->area_size()); |
| 248 | |
| 249 | heap->incremental_marking()->SetOldSpacePageFlags(chunk); |
| 250 | |
| 251 | return page; |
| 252 | } |
| 253 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 254 | void MemoryChunk::IncrementLiveBytesFromGC(HeapObject* object, int by) { |
| 255 | MemoryChunk::FromAddress(object->address())->IncrementLiveBytes(by); |
| 256 | } |
| 257 | |
| 258 | void MemoryChunk::ResetLiveBytes() { |
| 259 | if (FLAG_trace_live_bytes) { |
| 260 | PrintIsolate(heap()->isolate(), "live-bytes: reset page=%p %d->0\n", this, |
| 261 | live_byte_count_); |
| 262 | } |
| 263 | live_byte_count_ = 0; |
| 264 | } |
| 265 | |
| 266 | void MemoryChunk::IncrementLiveBytes(int by) { |
| 267 | if (FLAG_trace_live_bytes) { |
| 268 | PrintIsolate(heap()->isolate(), |
| 269 | "live-bytes: update page=%p delta=%d %d->%d\n", this, by, |
| 270 | live_byte_count_, live_byte_count_ + by); |
| 271 | } |
| 272 | live_byte_count_ += by; |
| 273 | DCHECK_GE(live_byte_count_, 0); |
| 274 | DCHECK_LE(static_cast<size_t>(live_byte_count_), size_); |
| 275 | } |
| 276 | |
| 277 | void MemoryChunk::IncrementLiveBytesFromMutator(HeapObject* object, int by) { |
| 278 | MemoryChunk* chunk = MemoryChunk::FromAddress(object->address()); |
| 279 | if (!chunk->InNewSpace() && !static_cast<Page*>(chunk)->SweepingDone()) { |
| 280 | static_cast<PagedSpace*>(chunk->owner())->Allocate(by); |
| 281 | } |
| 282 | chunk->IncrementLiveBytes(by); |
| 283 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | |
| 285 | bool PagedSpace::Contains(Address addr) { |
| 286 | Page* p = Page::FromAddress(addr); |
| 287 | if (!p->is_valid()) return false; |
| 288 | return p->owner() == this; |
| 289 | } |
| 290 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 291 | bool PagedSpace::Contains(Object* o) { |
| 292 | if (!o->IsHeapObject()) return false; |
| 293 | Page* p = Page::FromAddress(HeapObject::cast(o)->address()); |
| 294 | if (!p->is_valid()) return false; |
| 295 | return p->owner() == this; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 298 | MemoryChunk* MemoryChunk::FromAnyPointerAddress(Heap* heap, Address addr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 299 | MemoryChunk* chunk = MemoryChunk::FromAddress(addr); |
| 300 | uintptr_t offset = addr - chunk->address(); |
| 301 | if (offset < MemoryChunk::kHeaderSize || !chunk->HasPageHeader()) { |
| 302 | chunk = heap->lo_space()->FindPage(addr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 303 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 304 | return chunk; |
| 305 | } |
| 306 | |
| 307 | Page* Page::FromAnyPointerAddress(Heap* heap, Address addr) { |
| 308 | return static_cast<Page*>(MemoryChunk::FromAnyPointerAddress(heap, addr)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 312 | PointerChunkIterator::PointerChunkIterator(Heap* heap) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 313 | : state_(kOldSpaceState), |
| 314 | old_iterator_(heap->old_space()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 315 | map_iterator_(heap->map_space()), |
| 316 | lo_iterator_(heap->lo_space()) {} |
| 317 | |
| 318 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 319 | MemoryChunk* PointerChunkIterator::next() { |
| 320 | switch (state_) { |
| 321 | case kOldSpaceState: { |
| 322 | if (old_iterator_.has_next()) { |
| 323 | return old_iterator_.next(); |
| 324 | } |
| 325 | state_ = kMapState; |
| 326 | // Fall through. |
| 327 | } |
| 328 | case kMapState: { |
| 329 | if (map_iterator_.has_next()) { |
| 330 | return map_iterator_.next(); |
| 331 | } |
| 332 | state_ = kLargeObjectState; |
| 333 | // Fall through. |
| 334 | } |
| 335 | case kLargeObjectState: { |
| 336 | HeapObject* heap_object; |
| 337 | do { |
| 338 | heap_object = lo_iterator_.Next(); |
| 339 | if (heap_object == NULL) { |
| 340 | state_ = kFinishedState; |
| 341 | return NULL; |
| 342 | } |
| 343 | // Fixed arrays are the only pointer-containing objects in large |
| 344 | // object space. |
| 345 | } while (!heap_object->IsFixedArray()); |
| 346 | MemoryChunk* answer = MemoryChunk::FromAddress(heap_object->address()); |
| 347 | return answer; |
| 348 | } |
| 349 | case kFinishedState: |
| 350 | return NULL; |
| 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | UNREACHABLE(); |
| 355 | return NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | void Page::set_next_page(Page* page) { |
| 360 | DCHECK(page->owner() == owner()); |
| 361 | set_next_chunk(page); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | void Page::set_prev_page(Page* page) { |
| 366 | DCHECK(page->owner() == owner()); |
| 367 | set_prev_chunk(page); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | // Try linear allocation in the page of alloc_info's allocation top. Does |
| 372 | // not contain slow case logic (e.g. move to the next page or try free list |
| 373 | // allocation) so it can be used by all the allocation functions and for all |
| 374 | // the paged spaces. |
| 375 | HeapObject* PagedSpace::AllocateLinearly(int size_in_bytes) { |
| 376 | Address current_top = allocation_info_.top(); |
| 377 | Address new_top = current_top + size_in_bytes; |
| 378 | if (new_top > allocation_info_.limit()) return NULL; |
| 379 | |
| 380 | allocation_info_.set_top(new_top); |
| 381 | return HeapObject::FromAddress(current_top); |
| 382 | } |
| 383 | |
| 384 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 385 | AllocationResult LocalAllocationBuffer::AllocateRawAligned( |
| 386 | int size_in_bytes, AllocationAlignment alignment) { |
| 387 | Address current_top = allocation_info_.top(); |
| 388 | int filler_size = Heap::GetFillToAlign(current_top, alignment); |
| 389 | |
| 390 | Address new_top = current_top + filler_size + size_in_bytes; |
| 391 | if (new_top > allocation_info_.limit()) return AllocationResult::Retry(); |
| 392 | |
| 393 | allocation_info_.set_top(new_top); |
| 394 | if (filler_size > 0) { |
| 395 | return heap_->PrecedeWithFiller(HeapObject::FromAddress(current_top), |
| 396 | filler_size); |
| 397 | } |
| 398 | |
| 399 | return AllocationResult(HeapObject::FromAddress(current_top)); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | HeapObject* PagedSpace::AllocateLinearlyAligned(int* size_in_bytes, |
| 404 | AllocationAlignment alignment) { |
| 405 | Address current_top = allocation_info_.top(); |
| 406 | int filler_size = Heap::GetFillToAlign(current_top, alignment); |
| 407 | |
| 408 | Address new_top = current_top + filler_size + *size_in_bytes; |
| 409 | if (new_top > allocation_info_.limit()) return NULL; |
| 410 | |
| 411 | allocation_info_.set_top(new_top); |
| 412 | if (filler_size > 0) { |
| 413 | *size_in_bytes += filler_size; |
| 414 | return heap()->PrecedeWithFiller(HeapObject::FromAddress(current_top), |
| 415 | filler_size); |
| 416 | } |
| 417 | |
| 418 | return HeapObject::FromAddress(current_top); |
| 419 | } |
| 420 | |
| 421 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 422 | // Raw allocation. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 423 | AllocationResult PagedSpace::AllocateRawUnaligned(int size_in_bytes) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 424 | HeapObject* object = AllocateLinearly(size_in_bytes); |
| 425 | |
| 426 | if (object == NULL) { |
| 427 | object = free_list_.Allocate(size_in_bytes); |
| 428 | if (object == NULL) { |
| 429 | object = SlowAllocateRaw(size_in_bytes); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (object != NULL) { |
| 434 | if (identity() == CODE_SPACE) { |
| 435 | SkipList::Update(object->address(), size_in_bytes); |
| 436 | } |
| 437 | MSAN_ALLOCATED_UNINITIALIZED_MEMORY(object->address(), size_in_bytes); |
| 438 | return object; |
| 439 | } |
| 440 | |
| 441 | return AllocationResult::Retry(identity()); |
| 442 | } |
| 443 | |
| 444 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 445 | AllocationResult PagedSpace::AllocateRawUnalignedSynchronized( |
| 446 | int size_in_bytes) { |
| 447 | base::LockGuard<base::Mutex> lock_guard(&space_mutex_); |
| 448 | return AllocateRawUnaligned(size_in_bytes); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | // Raw allocation. |
| 453 | AllocationResult PagedSpace::AllocateRawAligned(int size_in_bytes, |
| 454 | AllocationAlignment alignment) { |
| 455 | DCHECK(identity() == OLD_SPACE); |
| 456 | int allocation_size = size_in_bytes; |
| 457 | HeapObject* object = AllocateLinearlyAligned(&allocation_size, alignment); |
| 458 | |
| 459 | if (object == NULL) { |
| 460 | // We don't know exactly how much filler we need to align until space is |
| 461 | // allocated, so assume the worst case. |
| 462 | int filler_size = Heap::GetMaximumFillToAlign(alignment); |
| 463 | allocation_size += filler_size; |
| 464 | object = free_list_.Allocate(allocation_size); |
| 465 | if (object == NULL) { |
| 466 | object = SlowAllocateRaw(allocation_size); |
| 467 | } |
| 468 | if (object != NULL && filler_size != 0) { |
| 469 | object = heap()->AlignWithFiller(object, size_in_bytes, allocation_size, |
| 470 | alignment); |
| 471 | // Filler objects are initialized, so mark only the aligned object memory |
| 472 | // as uninitialized. |
| 473 | allocation_size = size_in_bytes; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (object != NULL) { |
| 478 | MSAN_ALLOCATED_UNINITIALIZED_MEMORY(object->address(), allocation_size); |
| 479 | return object; |
| 480 | } |
| 481 | |
| 482 | return AllocationResult::Retry(identity()); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | AllocationResult PagedSpace::AllocateRaw(int size_in_bytes, |
| 487 | AllocationAlignment alignment) { |
| 488 | #ifdef V8_HOST_ARCH_32_BIT |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 489 | AllocationResult result = |
| 490 | alignment == kDoubleAligned |
| 491 | ? AllocateRawAligned(size_in_bytes, kDoubleAligned) |
| 492 | : AllocateRawUnaligned(size_in_bytes); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 493 | #else |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 494 | AllocationResult result = AllocateRawUnaligned(size_in_bytes); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 495 | #endif |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 496 | HeapObject* heap_obj = nullptr; |
| 497 | if (!result.IsRetry() && result.To(&heap_obj)) { |
| 498 | AllocationStep(heap_obj->address(), size_in_bytes); |
| 499 | } |
| 500 | return result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 504 | // ----------------------------------------------------------------------------- |
| 505 | // NewSpace |
| 506 | |
| 507 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 508 | AllocationResult NewSpace::AllocateRawAligned(int size_in_bytes, |
| 509 | AllocationAlignment alignment) { |
| 510 | Address top = allocation_info_.top(); |
| 511 | int filler_size = Heap::GetFillToAlign(top, alignment); |
| 512 | int aligned_size_in_bytes = size_in_bytes + filler_size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 513 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 514 | if (allocation_info_.limit() - top < aligned_size_in_bytes) { |
| 515 | // See if we can create room. |
| 516 | if (!EnsureAllocation(size_in_bytes, alignment)) { |
| 517 | return AllocationResult::Retry(); |
| 518 | } |
| 519 | |
| 520 | top = allocation_info_.top(); |
| 521 | filler_size = Heap::GetFillToAlign(top, alignment); |
| 522 | aligned_size_in_bytes = size_in_bytes + filler_size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 525 | HeapObject* obj = HeapObject::FromAddress(top); |
| 526 | allocation_info_.set_top(top + aligned_size_in_bytes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 527 | DCHECK_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 528 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 529 | if (filler_size > 0) { |
| 530 | obj = heap()->PrecedeWithFiller(obj, filler_size); |
| 531 | } |
| 532 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 533 | MSAN_ALLOCATED_UNINITIALIZED_MEMORY(obj->address(), size_in_bytes); |
| 534 | |
| 535 | return obj; |
| 536 | } |
| 537 | |
| 538 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 539 | AllocationResult NewSpace::AllocateRawUnaligned(int size_in_bytes) { |
| 540 | Address top = allocation_info_.top(); |
| 541 | if (allocation_info_.limit() < top + size_in_bytes) { |
| 542 | // See if we can create room. |
| 543 | if (!EnsureAllocation(size_in_bytes, kWordAligned)) { |
| 544 | return AllocationResult::Retry(); |
| 545 | } |
| 546 | |
| 547 | top = allocation_info_.top(); |
| 548 | } |
| 549 | |
| 550 | HeapObject* obj = HeapObject::FromAddress(top); |
| 551 | allocation_info_.set_top(top + size_in_bytes); |
| 552 | DCHECK_SEMISPACE_ALLOCATION_INFO(allocation_info_, to_space_); |
| 553 | |
| 554 | MSAN_ALLOCATED_UNINITIALIZED_MEMORY(obj->address(), size_in_bytes); |
| 555 | |
| 556 | return obj; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | AllocationResult NewSpace::AllocateRaw(int size_in_bytes, |
| 561 | AllocationAlignment alignment) { |
| 562 | #ifdef V8_HOST_ARCH_32_BIT |
| 563 | return alignment == kDoubleAligned |
| 564 | ? AllocateRawAligned(size_in_bytes, kDoubleAligned) |
| 565 | : AllocateRawUnaligned(size_in_bytes); |
| 566 | #else |
| 567 | return AllocateRawUnaligned(size_in_bytes); |
| 568 | #endif |
| 569 | } |
| 570 | |
| 571 | |
| 572 | MUST_USE_RESULT inline AllocationResult NewSpace::AllocateRawSynchronized( |
| 573 | int size_in_bytes, AllocationAlignment alignment) { |
| 574 | base::LockGuard<base::Mutex> guard(&mutex_); |
| 575 | return AllocateRaw(size_in_bytes, alignment); |
| 576 | } |
| 577 | |
| 578 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 579 | LargePage* LargePage::Initialize(Heap* heap, MemoryChunk* chunk) { |
| 580 | heap->incremental_marking()->SetOldSpacePageFlags(chunk); |
| 581 | return static_cast<LargePage*>(chunk); |
| 582 | } |
| 583 | |
| 584 | |
| 585 | intptr_t LargeObjectSpace::Available() { |
| 586 | return ObjectSizeFor(heap()->isolate()->memory_allocator()->Available()); |
| 587 | } |
| 588 | |
| 589 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 590 | LocalAllocationBuffer LocalAllocationBuffer::InvalidBuffer() { |
| 591 | return LocalAllocationBuffer(nullptr, AllocationInfo(nullptr, nullptr)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 592 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 593 | |
| 594 | |
| 595 | LocalAllocationBuffer LocalAllocationBuffer::FromResult(Heap* heap, |
| 596 | AllocationResult result, |
| 597 | intptr_t size) { |
| 598 | if (result.IsRetry()) return InvalidBuffer(); |
| 599 | HeapObject* obj = nullptr; |
| 600 | bool ok = result.To(&obj); |
| 601 | USE(ok); |
| 602 | DCHECK(ok); |
| 603 | Address top = HeapObject::cast(obj)->address(); |
| 604 | return LocalAllocationBuffer(heap, AllocationInfo(top, top + size)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 605 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 606 | |
| 607 | |
| 608 | bool LocalAllocationBuffer::TryMerge(LocalAllocationBuffer* other) { |
| 609 | if (allocation_info_.top() == other->allocation_info_.limit()) { |
| 610 | allocation_info_.set_top(other->allocation_info_.top()); |
| 611 | other->allocation_info_.Reset(nullptr, nullptr); |
| 612 | return true; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | } // namespace internal |
| 618 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 619 | |
| 620 | #endif // V8_HEAP_SPACES_INL_H_ |