Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: src/heap/spaces.cc

Issue 1853783002: [heap] Non-contiguous young generation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addressed offline comment (not passing size/executability for pooled allocation) Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/heap/spaces.h" 5 #include "src/heap/spaces.h"
6 6
7 #include "src/base/bits.h" 7 #include "src/base/bits.h"
8 #include "src/base/platform/platform.h" 8 #include "src/base/platform/platform.h"
9 #include "src/full-codegen/full-codegen.h" 9 #include "src/full-codegen/full-codegen.h"
10 #include "src/heap/slot-set.h" 10 #include "src/heap/slot-set.h"
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 DCHECK_GE(capacity_, capacity_executable_); 308 DCHECK_GE(capacity_, capacity_executable_);
309 309
310 size_ = 0; 310 size_ = 0;
311 size_executable_ = 0; 311 size_executable_ = 0;
312 312
313 return true; 313 return true;
314 } 314 }
315 315
316 316
317 void MemoryAllocator::TearDown() { 317 void MemoryAllocator::TearDown() {
318 for (MemoryChunk* chunk : chunk_pool_) {
319 FreeMemory(reinterpret_cast<Address>(chunk), MemoryChunk::kPageSize,
320 NOT_EXECUTABLE);
321 }
318 // Check that spaces were torn down before MemoryAllocator. 322 // Check that spaces were torn down before MemoryAllocator.
319 DCHECK(size_.Value() == 0); 323 DCHECK_EQ(size_.Value(), 0);
320 // TODO(gc) this will be true again when we fix FreeMemory. 324 // TODO(gc) this will be true again when we fix FreeMemory.
321 // DCHECK(size_executable_ == 0); 325 // DCHECK(size_executable_ == 0);
322 capacity_ = 0; 326 capacity_ = 0;
323 capacity_executable_ = 0; 327 capacity_executable_ = 0;
324 } 328 }
325 329
326
327 bool MemoryAllocator::CommitMemory(Address base, size_t size, 330 bool MemoryAllocator::CommitMemory(Address base, size_t size,
328 Executability executable) { 331 Executability executable) {
329 if (!base::VirtualMemory::CommitRegion(base, size, 332 if (!base::VirtualMemory::CommitRegion(base, size,
330 executable == EXECUTABLE)) { 333 executable == EXECUTABLE)) {
331 return false; 334 return false;
332 } 335 }
333 UpdateAllocatedSpaceLimits(base, base + size); 336 UpdateAllocatedSpaceLimits(base, base + size);
334 return true; 337 return true;
335 } 338 }
336 339
337 340
338 void MemoryAllocator::FreeNewSpaceMemory(Address addr,
339 base::VirtualMemory* reservation,
340 Executability executable) {
341 LOG(isolate_, DeleteEvent("NewSpace", addr));
342
343 DCHECK(reservation->IsReserved());
344 const intptr_t size = static_cast<intptr_t>(reservation->size());
345 DCHECK(size_.Value() >= size);
346 size_.Increment(-size);
347 isolate_->counters()->memory_allocated()->Decrement(static_cast<int>(size));
348 FreeMemory(reservation, NOT_EXECUTABLE);
349 }
350
351
352 void MemoryAllocator::FreeMemory(base::VirtualMemory* reservation, 341 void MemoryAllocator::FreeMemory(base::VirtualMemory* reservation,
353 Executability executable) { 342 Executability executable) {
354 // TODO(gc) make code_range part of memory allocator? 343 // TODO(gc) make code_range part of memory allocator?
355 // Code which is part of the code-range does not have its own VirtualMemory. 344 // Code which is part of the code-range does not have its own VirtualMemory.
356 DCHECK(isolate_->code_range() == NULL || 345 DCHECK(isolate_->code_range() == NULL ||
357 !isolate_->code_range()->contains( 346 !isolate_->code_range()->contains(
358 static_cast<Address>(reservation->address()))); 347 static_cast<Address>(reservation->address())));
359 DCHECK(executable == NOT_EXECUTABLE || isolate_->code_range() == NULL || 348 DCHECK(executable == NOT_EXECUTABLE || isolate_->code_range() == NULL ||
360 !isolate_->code_range()->valid() || 349 !isolate_->code_range()->valid() ||
361 reservation->size() <= Page::kPageSize); 350 reservation->size() <= Page::kPageSize);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 return base; 415 return base;
427 } 416 }
428 417
429 418
430 void Page::InitializeAsAnchor(PagedSpace* owner) { 419 void Page::InitializeAsAnchor(PagedSpace* owner) {
431 set_owner(owner); 420 set_owner(owner);
432 set_prev_page(this); 421 set_prev_page(this);
433 set_next_page(this); 422 set_next_page(this);
434 } 423 }
435 424
436
437 NewSpacePage* NewSpacePage::Initialize(Heap* heap, Address start,
438 SemiSpace* semi_space) {
439 Address area_start = start + NewSpacePage::kObjectStartOffset;
440 Address area_end = start + Page::kPageSize;
441
442 MemoryChunk* chunk =
443 MemoryChunk::Initialize(heap, start, Page::kPageSize, area_start,
444 area_end, NOT_EXECUTABLE, semi_space, nullptr);
445 bool in_to_space = (semi_space->id() != kFromSpace);
446 chunk->SetFlag(in_to_space ? MemoryChunk::IN_TO_SPACE
447 : MemoryChunk::IN_FROM_SPACE);
448 DCHECK(!chunk->IsFlagSet(in_to_space ? MemoryChunk::IN_FROM_SPACE
449 : MemoryChunk::IN_TO_SPACE));
450 NewSpacePage* page = static_cast<NewSpacePage*>(chunk);
451 heap->incremental_marking()->SetNewSpacePageFlags(page);
452 return page;
453 }
454
455
456 void NewSpacePage::InitializeAsAnchor(SemiSpace* semi_space) { 425 void NewSpacePage::InitializeAsAnchor(SemiSpace* semi_space) {
457 set_owner(semi_space); 426 set_owner(semi_space);
458 set_next_chunk(this); 427 set_next_chunk(this);
459 set_prev_chunk(this); 428 set_prev_chunk(this);
460 // Flags marks this invalid page as not being in new-space. 429 // Flags marks this invalid page as not being in new-space.
461 // All real new-space pages will be in new-space. 430 // All real new-space pages will be in new-space.
462 SetFlags(0, ~0); 431 SetFlags(0, ~0);
463 } 432 }
464 433
465 MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size, 434 MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size,
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 return MemoryChunk::Initialize(heap, base, chunk_size, area_start, area_end, 677 return MemoryChunk::Initialize(heap, base, chunk_size, area_start, area_end,
709 executable, owner, &reservation); 678 executable, owner, &reservation);
710 } 679 }
711 680
712 681
713 void Page::ResetFreeListStatistics() { 682 void Page::ResetFreeListStatistics() {
714 wasted_memory_ = 0; 683 wasted_memory_ = 0;
715 available_in_free_list_ = 0; 684 available_in_free_list_ = 0;
716 } 685 }
717 686
718
719 Page* MemoryAllocator::AllocatePage(intptr_t size, PagedSpace* owner,
720 Executability executable) {
721 MemoryChunk* chunk = AllocateChunk(size, size, executable, owner);
722 if (chunk == NULL) return NULL;
723 return Page::Initialize(isolate_->heap(), chunk, executable, owner);
724 }
725
726
727 LargePage* MemoryAllocator::AllocateLargePage(intptr_t object_size, 687 LargePage* MemoryAllocator::AllocateLargePage(intptr_t object_size,
728 Space* owner, 688 Space* owner,
729 Executability executable) { 689 Executability executable) {
730 MemoryChunk* chunk = 690 MemoryChunk* chunk =
731 AllocateChunk(object_size, object_size, executable, owner); 691 AllocateChunk(object_size, object_size, executable, owner);
732 if (chunk == NULL) return NULL; 692 if (chunk == NULL) return NULL;
733 if (executable && chunk->size() > LargePage::kMaxCodePageSize) { 693 if (executable && chunk->size() > LargePage::kMaxCodePageSize) {
734 STATIC_ASSERT(LargePage::kMaxCodePageSize <= TypedSlotSet::kMaxOffset); 694 STATIC_ASSERT(LargePage::kMaxCodePageSize <= TypedSlotSet::kMaxOffset);
735 FATAL("Code page is too large."); 695 FATAL("Code page is too large.");
736 } 696 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 FreeMemory(chunk->address(), chunk->size(), chunk->executable()); 741 FreeMemory(chunk->address(), chunk->size(), chunk->executable());
782 } 742 }
783 } 743 }
784 744
785 745
786 void MemoryAllocator::Free(MemoryChunk* chunk) { 746 void MemoryAllocator::Free(MemoryChunk* chunk) {
787 PreFreeMemory(chunk); 747 PreFreeMemory(chunk);
788 PerformFreeMemory(chunk); 748 PerformFreeMemory(chunk);
789 } 749 }
790 750
751 template <typename PageType, MemoryAllocator::AllocationMode mode,
752 typename SpaceType>
753 PageType* MemoryAllocator::AllocatePage(intptr_t size, SpaceType* owner,
754 Executability executable) {
755 MemoryChunk* chunk = nullptr;
756 if (mode == kPooled) {
757 DCHECK_EQ(size, static_cast<intptr_t>(MemoryChunk::kAllocatableMemory));
758 DCHECK_EQ(executable, NOT_EXECUTABLE);
759 chunk = AllocatePagePooled(owner);
760 }
761 if (chunk == nullptr) {
762 chunk = AllocateChunk(size, size, executable, owner);
763 }
764 if (chunk == nullptr) return nullptr;
765 return PageType::Initialize(isolate_->heap(), chunk, executable, owner);
766 }
767
768 template Page* MemoryAllocator::AllocatePage<Page, MemoryAllocator::kRegular,
769 PagedSpace>(intptr_t, PagedSpace*,
770 Executability);
771
772 template NewSpacePage* MemoryAllocator::AllocatePage<
773 NewSpacePage, MemoryAllocator::kPooled, SemiSpace>(intptr_t, SemiSpace*,
774 Executability);
775
776 template <typename SpaceType>
777 MemoryChunk* MemoryAllocator::AllocatePagePooled(SpaceType* owner) {
778 if (chunk_pool_.is_empty()) return nullptr;
779 const int size = MemoryChunk::kPageSize;
780 MemoryChunk* chunk = chunk_pool_.RemoveLast();
781 const Address start = reinterpret_cast<Address>(chunk);
782 const Address area_start = start + MemoryChunk::kObjectStartOffset;
783 const Address area_end = start + size;
784 CommitBlock(reinterpret_cast<Address>(chunk), size, NOT_EXECUTABLE);
785 base::VirtualMemory reservation(start, size);
786 MemoryChunk::Initialize(isolate_->heap(), start, size, area_start, area_end,
787 NOT_EXECUTABLE, owner, &reservation);
788 size_.Increment(size);
789 return chunk;
790 }
791
792 void MemoryAllocator::FreePooled(MemoryChunk* chunk) {
793 DCHECK_EQ(chunk->size(), static_cast<size_t>(MemoryChunk::kPageSize));
794 DCHECK_EQ(chunk->executable(), NOT_EXECUTABLE);
795 chunk_pool_.Add(chunk);
796 intptr_t chunk_size = static_cast<intptr_t>(chunk->size());
797 if (chunk->executable() == EXECUTABLE) {
798 size_executable_.Increment(-chunk_size);
799 }
800 size_.Increment(-chunk_size);
801 UncommitBlock(reinterpret_cast<Address>(chunk), MemoryChunk::kPageSize);
802 }
791 803
792 bool MemoryAllocator::CommitBlock(Address start, size_t size, 804 bool MemoryAllocator::CommitBlock(Address start, size_t size,
793 Executability executable) { 805 Executability executable) {
794 if (!CommitMemory(start, size, executable)) return false; 806 if (!CommitMemory(start, size, executable)) return false;
795 807
796 if (Heap::ShouldZapGarbage()) { 808 if (Heap::ShouldZapGarbage()) {
797 ZapBlock(start, size); 809 ZapBlock(start, size);
798 } 810 }
799 811
800 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size)); 812 isolate_->counters()->memory_allocated()->Increment(static_cast<int>(size));
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 1164
1153 1165
1154 bool PagedSpace::Expand() { 1166 bool PagedSpace::Expand() {
1155 intptr_t size = AreaSize(); 1167 intptr_t size = AreaSize();
1156 if (snapshotable() && !HasPages()) { 1168 if (snapshotable() && !HasPages()) {
1157 size = Snapshot::SizeOfFirstPage(heap()->isolate(), identity()); 1169 size = Snapshot::SizeOfFirstPage(heap()->isolate(), identity());
1158 } 1170 }
1159 1171
1160 if (!CanExpand(size)) return false; 1172 if (!CanExpand(size)) return false;
1161 1173
1162 Page* p = heap()->isolate()->memory_allocator()->AllocatePage(size, this, 1174 Page* p = heap()->isolate()->memory_allocator()->AllocatePage<Page>(
1163 executable()); 1175 size, this, executable());
1164 if (p == NULL) return false; 1176 if (p == NULL) return false;
1165 1177
1166 AccountCommitted(static_cast<intptr_t>(p->size())); 1178 AccountCommitted(static_cast<intptr_t>(p->size()));
1167 1179
1168 // Pages created during bootstrapping may contain immortal immovable objects. 1180 // Pages created during bootstrapping may contain immortal immovable objects.
1169 if (!heap()->deserialization_complete()) p->MarkNeverEvacuate(); 1181 if (!heap()->deserialization_complete()) p->MarkNeverEvacuate();
1170 1182
1171 // When incremental marking was activated, old generation pages are allocated 1183 // When incremental marking was activated, old generation pages are allocated
1172 // black. 1184 // black.
1173 if (heap()->incremental_marking()->black_allocation()) { 1185 if (heap()->incremental_marking()->black_allocation()) {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1283 } 1295 }
1284 CHECK_LE(black_size, page->LiveBytes()); 1296 CHECK_LE(black_size, page->LiveBytes());
1285 } 1297 }
1286 CHECK(allocation_pointer_found_in_space); 1298 CHECK(allocation_pointer_found_in_space);
1287 } 1299 }
1288 #endif // VERIFY_HEAP 1300 #endif // VERIFY_HEAP
1289 1301
1290 // ----------------------------------------------------------------------------- 1302 // -----------------------------------------------------------------------------
1291 // NewSpace implementation 1303 // NewSpace implementation
1292 1304
1293 1305 bool NewSpace::SetUp(int initial_semispace_capacity,
1294 bool NewSpace::SetUp(int reserved_semispace_capacity,
1295 int maximum_semispace_capacity) { 1306 int maximum_semispace_capacity) {
1296 // Set up new space based on the preallocated memory block defined by
1297 // start and size. The provided space is divided into two semi-spaces.
1298 // To support fast containment testing in the new space, the size of
1299 // this chunk must be a power of two and it must be aligned to its size.
1300 int initial_semispace_capacity = heap()->InitialSemiSpaceSize();
1301
1302 size_t size = 2 * reserved_semispace_capacity;
1303 Address base = heap()->isolate()->memory_allocator()->ReserveAlignedMemory(
1304 size, size, &reservation_);
1305 if (base == NULL) return false;
1306
1307 chunk_base_ = base;
1308 chunk_size_ = static_cast<uintptr_t>(size);
1309 LOG(heap()->isolate(), NewEvent("InitialChunk", chunk_base_, chunk_size_));
1310
1311 DCHECK(initial_semispace_capacity <= maximum_semispace_capacity); 1307 DCHECK(initial_semispace_capacity <= maximum_semispace_capacity);
1312 DCHECK(base::bits::IsPowerOfTwo32(maximum_semispace_capacity)); 1308 DCHECK(base::bits::IsPowerOfTwo32(maximum_semispace_capacity));
1313 1309
1310 to_space_.SetUp(initial_semispace_capacity, maximum_semispace_capacity);
1311 from_space_.SetUp(initial_semispace_capacity, maximum_semispace_capacity);
1312 if (!to_space_.Commit()) {
1313 return false;
1314 }
1315 DCHECK(!from_space_.is_committed()); // No need to use memory yet.
1316 ResetAllocationInfo();
1317
1314 // Allocate and set up the histogram arrays if necessary. 1318 // Allocate and set up the histogram arrays if necessary.
1315 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1); 1319 allocated_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1316 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1); 1320 promoted_histogram_ = NewArray<HistogramInfo>(LAST_TYPE + 1);
1317
1318 #define SET_NAME(name) \ 1321 #define SET_NAME(name) \
1319 allocated_histogram_[name].set_name(#name); \ 1322 allocated_histogram_[name].set_name(#name); \
1320 promoted_histogram_[name].set_name(#name); 1323 promoted_histogram_[name].set_name(#name);
1321 INSTANCE_TYPE_LIST(SET_NAME) 1324 INSTANCE_TYPE_LIST(SET_NAME)
1322 #undef SET_NAME 1325 #undef SET_NAME
1323 1326
1324 DCHECK(reserved_semispace_capacity == heap()->ReservedSemiSpaceSize());
1325 DCHECK(static_cast<intptr_t>(chunk_size_) >=
1326 2 * heap()->ReservedSemiSpaceSize());
1327 DCHECK(IsAddressAligned(chunk_base_, 2 * reserved_semispace_capacity, 0));
1328
1329 to_space_.SetUp(chunk_base_, initial_semispace_capacity,
1330 maximum_semispace_capacity);
1331 from_space_.SetUp(chunk_base_ + reserved_semispace_capacity,
1332 initial_semispace_capacity, maximum_semispace_capacity);
1333 if (!to_space_.Commit()) {
1334 return false;
1335 }
1336 DCHECK(!from_space_.is_committed()); // No need to use memory yet.
1337
1338 ResetAllocationInfo();
1339
1340 return true; 1327 return true;
1341 } 1328 }
1342 1329
1343 1330
1344 void NewSpace::TearDown() { 1331 void NewSpace::TearDown() {
1345 if (allocated_histogram_) { 1332 if (allocated_histogram_) {
1346 DeleteArray(allocated_histogram_); 1333 DeleteArray(allocated_histogram_);
1347 allocated_histogram_ = NULL; 1334 allocated_histogram_ = NULL;
1348 } 1335 }
1349 if (promoted_histogram_) { 1336 if (promoted_histogram_) {
1350 DeleteArray(promoted_histogram_); 1337 DeleteArray(promoted_histogram_);
1351 promoted_histogram_ = NULL; 1338 promoted_histogram_ = NULL;
1352 } 1339 }
1353 1340
1354 allocation_info_.Reset(nullptr, nullptr); 1341 allocation_info_.Reset(nullptr, nullptr);
1355 1342
1356 to_space_.TearDown(); 1343 to_space_.TearDown();
1357 from_space_.TearDown(); 1344 from_space_.TearDown();
1358
1359 heap()->isolate()->memory_allocator()->FreeNewSpaceMemory(
1360 chunk_base_, &reservation_, NOT_EXECUTABLE);
1361
1362 chunk_base_ = NULL;
1363 chunk_size_ = 0;
1364 } 1345 }
1365 1346
1366 1347
1367 void NewSpace::Flip() { SemiSpace::Swap(&from_space_, &to_space_); } 1348 void NewSpace::Flip() { SemiSpace::Swap(&from_space_, &to_space_); }
1368 1349
1369 1350
1370 void NewSpace::Grow() { 1351 void NewSpace::Grow() {
1371 // Double the semispace size but only up to maximum capacity. 1352 // Double the semispace size but only up to maximum capacity.
1372 DCHECK(TotalCapacity() < MaximumCapacity()); 1353 DCHECK(TotalCapacity() < MaximumCapacity());
1373 int new_capacity = 1354 int new_capacity =
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1670 CHECK_EQ(from_space_.id(), kFromSpace); 1651 CHECK_EQ(from_space_.id(), kFromSpace);
1671 CHECK_EQ(to_space_.id(), kToSpace); 1652 CHECK_EQ(to_space_.id(), kToSpace);
1672 from_space_.Verify(); 1653 from_space_.Verify();
1673 to_space_.Verify(); 1654 to_space_.Verify();
1674 } 1655 }
1675 #endif 1656 #endif
1676 1657
1677 // ----------------------------------------------------------------------------- 1658 // -----------------------------------------------------------------------------
1678 // SemiSpace implementation 1659 // SemiSpace implementation
1679 1660
1680 void SemiSpace::SetUp(Address start, int initial_capacity, 1661 void SemiSpace::SetUp(int initial_capacity, int maximum_capacity) {
1681 int maximum_capacity) {
1682 DCHECK_GE(maximum_capacity, Page::kPageSize); 1662 DCHECK_GE(maximum_capacity, Page::kPageSize);
1683 minimum_capacity_ = RoundDown(initial_capacity, Page::kPageSize); 1663 minimum_capacity_ = RoundDown(initial_capacity, Page::kPageSize);
1684 current_capacity_ = minimum_capacity_; 1664 current_capacity_ = minimum_capacity_;
1685 maximum_capacity_ = RoundDown(maximum_capacity, Page::kPageSize); 1665 maximum_capacity_ = RoundDown(maximum_capacity, Page::kPageSize);
1686 committed_ = false; 1666 committed_ = false;
1687 start_ = start;
1688 age_mark_ = start_ + NewSpacePage::kObjectStartOffset;
1689 } 1667 }
1690 1668
1691 1669
1692 void SemiSpace::TearDown() { 1670 void SemiSpace::TearDown() {
1693 start_ = nullptr; 1671 // Properly uncommit memory to keep the allocator counters in sync.
1694 current_capacity_ = 0; 1672 if (is_committed()) Uncommit();
1673 current_capacity_ = maximum_capacity_ = 0;
1695 } 1674 }
1696 1675
1697 1676
1698 bool SemiSpace::Commit() { 1677 bool SemiSpace::Commit() {
1699 DCHECK(!is_committed()); 1678 DCHECK(!is_committed());
1700 if (!heap()->isolate()->memory_allocator()->CommitBlock(
1701 start_, current_capacity_, executable())) {
1702 return false;
1703 }
1704 AccountCommitted(current_capacity_);
1705
1706 NewSpacePage* current = anchor(); 1679 NewSpacePage* current = anchor();
1707 const int num_pages = current_capacity_ / Page::kPageSize; 1680 const int num_pages = current_capacity_ / Page::kPageSize;
1708 for (int i = 0; i < num_pages; i++) { 1681 for (int i = 0; i < num_pages; i++) {
1709 NewSpacePage* new_page = 1682 NewSpacePage* new_page =
1710 NewSpacePage::Initialize(heap(), start_ + i * Page::kPageSize, this); 1683 heap()
1684 ->isolate()
1685 ->memory_allocator()
1686 ->AllocatePage<NewSpacePage, MemoryAllocator::kPooled>(
1687 NewSpacePage::kAllocatableMemory, this, executable());
1711 new_page->InsertAfter(current); 1688 new_page->InsertAfter(current);
1712 current = new_page; 1689 current = new_page;
1713 } 1690 }
1714 Reset(); 1691 Reset();
1715 1692 AccountCommitted(current_capacity_);
1716 set_current_capacity(current_capacity_); 1693 if (age_mark_ == nullptr) {
1694 age_mark_ = first_page()->area_start();
1695 }
1717 committed_ = true; 1696 committed_ = true;
1718 return true; 1697 return true;
1719 } 1698 }
1720 1699
1721 1700
1722 bool SemiSpace::Uncommit() { 1701 bool SemiSpace::Uncommit() {
1723 DCHECK(is_committed()); 1702 DCHECK(is_committed());
1724 Address start = start_ + maximum_capacity_ - current_capacity_; 1703 NewSpacePageIterator it(this);
1725 if (!heap()->isolate()->memory_allocator()->UncommitBlock( 1704 while (it.has_next()) {
1726 start, current_capacity_)) { 1705 heap()->isolate()->memory_allocator()->FreePooled(it.next());
1727 return false;
1728 } 1706 }
1729 AccountUncommitted(current_capacity_);
1730
1731 anchor()->set_next_page(anchor()); 1707 anchor()->set_next_page(anchor());
1732 anchor()->set_prev_page(anchor()); 1708 anchor()->set_prev_page(anchor());
1733 1709 AccountUncommitted(current_capacity_);
1734 committed_ = false; 1710 committed_ = false;
1735 return true; 1711 return true;
1736 } 1712 }
1737 1713
1738 1714
1739 size_t SemiSpace::CommittedPhysicalMemory() { 1715 size_t SemiSpace::CommittedPhysicalMemory() {
1740 if (!is_committed()) return 0; 1716 if (!is_committed()) return 0;
1741 size_t size = 0; 1717 size_t size = 0;
1742 NewSpacePageIterator it(this); 1718 NewSpacePageIterator it(this);
1743 while (it.has_next()) { 1719 while (it.has_next()) {
1744 size += it.next()->CommittedPhysicalMemory(); 1720 size += it.next()->CommittedPhysicalMemory();
1745 } 1721 }
1746 return size; 1722 return size;
1747 } 1723 }
1748 1724
1749 1725
1750 bool SemiSpace::GrowTo(int new_capacity) { 1726 bool SemiSpace::GrowTo(int new_capacity) {
1751 if (!is_committed()) { 1727 if (!is_committed()) {
1752 if (!Commit()) return false; 1728 if (!Commit()) return false;
1753 } 1729 }
1754 DCHECK_EQ(new_capacity & Page::kPageAlignmentMask, 0); 1730 DCHECK_EQ(new_capacity & NewSpacePage::kPageAlignmentMask, 0);
1755 DCHECK_LE(new_capacity, maximum_capacity_); 1731 DCHECK_LE(new_capacity, maximum_capacity_);
1756 DCHECK_GT(new_capacity, current_capacity_); 1732 DCHECK_GT(new_capacity, current_capacity_);
1757 int pages_before = current_capacity_ / Page::kPageSize; 1733 const int delta = new_capacity - current_capacity_;
1758 int pages_after = new_capacity / Page::kPageSize;
1759
1760 size_t delta = new_capacity - current_capacity_;
1761
1762 DCHECK(IsAligned(delta, base::OS::AllocateAlignment())); 1734 DCHECK(IsAligned(delta, base::OS::AllocateAlignment()));
1763 if (!heap()->isolate()->memory_allocator()->CommitBlock( 1735 int delta_pages = delta / NewSpacePage::kPageSize;
1764 start_ + current_capacity_, delta, executable())) {
1765 return false;
1766 }
1767 AccountCommitted(static_cast<intptr_t>(delta));
1768 set_current_capacity(new_capacity);
1769 NewSpacePage* last_page = anchor()->prev_page(); 1736 NewSpacePage* last_page = anchor()->prev_page();
1770 DCHECK_NE(last_page, anchor()); 1737 DCHECK_NE(last_page, anchor());
1771 for (int i = pages_before; i < pages_after; i++) { 1738 while (delta_pages > 0) {
1772 Address page_address = start_ + i * Page::kPageSize;
1773 NewSpacePage* new_page = 1739 NewSpacePage* new_page =
1774 NewSpacePage::Initialize(heap(), page_address, this); 1740 heap()
1741 ->isolate()
1742 ->memory_allocator()
1743 ->AllocatePage<NewSpacePage, MemoryAllocator::kPooled>(
1744 NewSpacePage::kAllocatableMemory, this, executable());
1775 new_page->InsertAfter(last_page); 1745 new_page->InsertAfter(last_page);
1776 Bitmap::Clear(new_page); 1746 Bitmap::Clear(new_page);
1777 // Duplicate the flags that was set on the old page. 1747 // Duplicate the flags that was set on the old page.
1778 new_page->SetFlags(last_page->GetFlags(), 1748 new_page->SetFlags(last_page->GetFlags(),
1779 NewSpacePage::kCopyOnFlipFlagsMask); 1749 NewSpacePage::kCopyOnFlipFlagsMask);
1780 last_page = new_page; 1750 last_page = new_page;
1751 delta_pages--;
1781 } 1752 }
1753 AccountCommitted(static_cast<intptr_t>(delta));
1754 current_capacity_ = new_capacity;
1782 return true; 1755 return true;
1783 } 1756 }
1784 1757
1785 1758
1786 bool SemiSpace::ShrinkTo(int new_capacity) { 1759 bool SemiSpace::ShrinkTo(int new_capacity) {
1787 DCHECK_EQ(new_capacity & Page::kPageAlignmentMask, 0); 1760 DCHECK_EQ(new_capacity & NewSpacePage::kPageAlignmentMask, 0);
1788 DCHECK_GE(new_capacity, minimum_capacity_); 1761 DCHECK_GE(new_capacity, minimum_capacity_);
1789 DCHECK_LT(new_capacity, current_capacity_); 1762 DCHECK_LT(new_capacity, current_capacity_);
1790 if (is_committed()) { 1763 if (is_committed()) {
1791 size_t delta = current_capacity_ - new_capacity; 1764 const int delta = current_capacity_ - new_capacity;
1792 DCHECK(IsAligned(delta, base::OS::AllocateAlignment())); 1765 DCHECK(IsAligned(delta, base::OS::AllocateAlignment()));
1793 1766 int delta_pages = delta / NewSpacePage::kPageSize;
1794 MemoryAllocator* allocator = heap()->isolate()->memory_allocator(); 1767 NewSpacePage* new_last_page;
1795 if (!allocator->UncommitBlock(start_ + new_capacity, delta)) { 1768 NewSpacePage* last_page;
1796 return false; 1769 while (delta_pages > 0) {
1770 last_page = anchor()->prev_page();
1771 new_last_page = last_page->prev_page();
1772 new_last_page->set_next_page(anchor());
1773 anchor()->set_prev_page(new_last_page);
1774 heap()->isolate()->memory_allocator()->FreePooled(last_page);
1775 delta_pages--;
1797 } 1776 }
1798 AccountUncommitted(static_cast<intptr_t>(delta)); 1777 AccountUncommitted(static_cast<intptr_t>(delta));
1799
1800 int pages_after = new_capacity / Page::kPageSize;
1801 NewSpacePage* new_last_page =
1802 NewSpacePage::FromAddress(start_ + (pages_after - 1) * Page::kPageSize);
1803 new_last_page->set_next_page(anchor());
1804 anchor()->set_prev_page(new_last_page);
1805 DCHECK((current_page_ >= first_page()) && (current_page_ <= new_last_page));
1806 } 1778 }
1807 1779 current_capacity_ = new_capacity;
1808 set_current_capacity(new_capacity);
1809
1810 return true; 1780 return true;
1811 } 1781 }
1812 1782
1813 void SemiSpace::FixPagesFlags(intptr_t flags, intptr_t mask) { 1783 void SemiSpace::FixPagesFlags(intptr_t flags, intptr_t mask) {
1814 anchor_.set_owner(this); 1784 anchor_.set_owner(this);
1815 // Fixup back-pointers to anchor. Address of anchor changes when we swap. 1785 // Fixup back-pointers to anchor. Address of anchor changes when we swap.
1816 anchor_.prev_page()->set_next_page(&anchor_); 1786 anchor_.prev_page()->set_next_page(&anchor_);
1817 anchor_.next_page()->set_prev_page(&anchor_); 1787 anchor_.next_page()->set_prev_page(&anchor_);
1818 1788
1819 NewSpacePageIterator it(this); 1789 NewSpacePageIterator it(this);
(...skipping 26 matching lines...) Expand all
1846 // We won't be swapping semispaces without data in them. 1816 // We won't be swapping semispaces without data in them.
1847 DCHECK_NE(from->anchor_.next_page(), &from->anchor_); 1817 DCHECK_NE(from->anchor_.next_page(), &from->anchor_);
1848 DCHECK_NE(to->anchor_.next_page(), &to->anchor_); 1818 DCHECK_NE(to->anchor_.next_page(), &to->anchor_);
1849 1819
1850 intptr_t saved_to_space_flags = to->current_page()->GetFlags(); 1820 intptr_t saved_to_space_flags = to->current_page()->GetFlags();
1851 1821
1852 // We swap all properties but id_. 1822 // We swap all properties but id_.
1853 std::swap(from->current_capacity_, to->current_capacity_); 1823 std::swap(from->current_capacity_, to->current_capacity_);
1854 std::swap(from->maximum_capacity_, to->maximum_capacity_); 1824 std::swap(from->maximum_capacity_, to->maximum_capacity_);
1855 std::swap(from->minimum_capacity_, to->minimum_capacity_); 1825 std::swap(from->minimum_capacity_, to->minimum_capacity_);
1856 std::swap(from->start_, to->start_);
1857 std::swap(from->age_mark_, to->age_mark_); 1826 std::swap(from->age_mark_, to->age_mark_);
1858 std::swap(from->committed_, to->committed_); 1827 std::swap(from->committed_, to->committed_);
1859 std::swap(from->anchor_, to->anchor_); 1828 std::swap(from->anchor_, to->anchor_);
1860 std::swap(from->current_page_, to->current_page_); 1829 std::swap(from->current_page_, to->current_page_);
1861 1830
1862 to->FixPagesFlags(saved_to_space_flags, NewSpacePage::kCopyOnFlipFlagsMask); 1831 to->FixPagesFlags(saved_to_space_flags, NewSpacePage::kCopyOnFlipFlagsMask);
1863 from->FixPagesFlags(0, 0); 1832 from->FixPagesFlags(0, 0);
1864 } 1833 }
1865 1834
1866 1835
(...skipping 1295 matching lines...) Expand 10 before | Expand all | Expand 10 after
3162 object->ShortPrint(); 3131 object->ShortPrint();
3163 PrintF("\n"); 3132 PrintF("\n");
3164 } 3133 }
3165 printf(" --------------------------------------\n"); 3134 printf(" --------------------------------------\n");
3166 printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes()); 3135 printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes());
3167 } 3136 }
3168 3137
3169 #endif // DEBUG 3138 #endif // DEBUG
3170 } // namespace internal 3139 } // namespace internal
3171 } // namespace v8 3140 } // namespace v8
OLDNEW
« src/heap/spaces.h ('K') | « src/heap/spaces.h ('k') | src/heap/spaces-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698