Commit d65ea662 authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

cppgc: Allocation cleanups

Bug: chromium:1056170
Change-Id: I99d073e268f5779f0985d6197432c50036060b60
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2192663
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarAnton Bikineev <bikineev@chromium.org>
Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67713}
parent 6a6c151d
...@@ -9,7 +9,10 @@ ...@@ -9,7 +9,10 @@
namespace cppgc { namespace cppgc {
using CustomSpaceIndex = size_t; struct CustomSpaceIndex {
CustomSpaceIndex(size_t value) : value(value) {} // NOLINT
size_t value;
};
/** /**
* Top-level base class for custom spaces. Users must inherit from CustomSpace * Top-level base class for custom spaces. Users must inherit from CustomSpace
......
...@@ -24,7 +24,7 @@ void VerifyCustomSpaces( ...@@ -24,7 +24,7 @@ void VerifyCustomSpaces(
// starting at 0. // starting at 0.
#ifdef DEBUG #ifdef DEBUG
for (size_t i = 0; i < custom_spaces.size(); ++i) { for (size_t i = 0; i < custom_spaces.size(); ++i) {
DCHECK_EQ(i, custom_spaces[i]->GetCustomSpaceIndex()); DCHECK_EQ(i, custom_spaces[i]->GetCustomSpaceIndex().value);
} }
#endif // DEBUG #endif // DEBUG
} }
......
...@@ -18,7 +18,7 @@ namespace internal { ...@@ -18,7 +18,7 @@ namespace internal {
void* ObjectAllocator::AllocateObject(size_t size, GCInfoIndex gcinfo) { void* ObjectAllocator::AllocateObject(size_t size, GCInfoIndex gcinfo) {
const size_t allocation_size = const size_t allocation_size =
RoundUp(size + sizeof(HeapObjectHeader), kAllocationGranularity); RoundUp<kAllocationGranularity>(size + sizeof(HeapObjectHeader));
const RawHeap::RegularSpaceType type = const RawHeap::RegularSpaceType type =
GetInitialSpaceIndexForSize(allocation_size); GetInitialSpaceIndexForSize(allocation_size);
return AllocateObjectOnSpace(NormalPageSpace::From(raw_heap_->Space(type)), return AllocateObjectOnSpace(NormalPageSpace::From(raw_heap_->Space(type)),
...@@ -28,11 +28,9 @@ void* ObjectAllocator::AllocateObject(size_t size, GCInfoIndex gcinfo) { ...@@ -28,11 +28,9 @@ void* ObjectAllocator::AllocateObject(size_t size, GCInfoIndex gcinfo) {
void* ObjectAllocator::AllocateObject(size_t size, GCInfoIndex gcinfo, void* ObjectAllocator::AllocateObject(size_t size, GCInfoIndex gcinfo,
CustomSpaceIndex space_index) { CustomSpaceIndex space_index) {
const size_t allocation_size = const size_t allocation_size =
RoundUp(size + sizeof(HeapObjectHeader), kAllocationGranularity); RoundUp<kAllocationGranularity>(size + sizeof(HeapObjectHeader));
const size_t internal_space_index =
raw_heap_->SpaceIndexForCustomSpace(space_index);
return AllocateObjectOnSpace( return AllocateObjectOnSpace(
NormalPageSpace::From(raw_heap_->Space(internal_space_index)), NormalPageSpace::From(raw_heap_->CustomSpace(space_index)),
allocation_size, gcinfo); allocation_size, gcinfo);
} }
......
...@@ -64,14 +64,28 @@ class V8_EXPORT_PRIVATE RawHeap final { ...@@ -64,14 +64,28 @@ class V8_EXPORT_PRIVATE RawHeap final {
BaseSpace* Space(RegularSpaceType type) { BaseSpace* Space(RegularSpaceType type) {
const size_t index = static_cast<size_t>(type); const size_t index = static_cast<size_t>(type);
DCHECK_GT(kNumberOfRegularSpaces, index); DCHECK_GT(kNumberOfRegularSpaces, index);
BaseSpace* space = spaces_[index].get(); return Space(index);
DCHECK(space);
return space;
} }
const BaseSpace* Space(RegularSpaceType space) const { const BaseSpace* Space(RegularSpaceType space) const {
return const_cast<RawHeap&>(*this).Space(space); return const_cast<RawHeap&>(*this).Space(space);
} }
BaseSpace* CustomSpace(CustomSpaceIndex space_index) {
return Space(SpaceIndexForCustomSpace(space_index));
}
const BaseSpace* CustomSpace(CustomSpaceIndex space_index) const {
return const_cast<RawHeap&>(*this).CustomSpace(space_index);
}
Heap* heap() { return main_heap_; }
const Heap* heap() const { return main_heap_; }
private:
size_t SpaceIndexForCustomSpace(CustomSpaceIndex space_index) const {
DCHECK_LT(space_index.value, spaces_.size() - kNumberOfRegularSpaces);
return kNumberOfRegularSpaces + space_index.value;
}
BaseSpace* Space(size_t space_index) { BaseSpace* Space(size_t space_index) {
DCHECK_GT(spaces_.size(), space_index); DCHECK_GT(spaces_.size(), space_index);
BaseSpace* space = spaces_[space_index].get(); BaseSpace* space = spaces_[space_index].get();
...@@ -82,15 +96,6 @@ class V8_EXPORT_PRIVATE RawHeap final { ...@@ -82,15 +96,6 @@ class V8_EXPORT_PRIVATE RawHeap final {
return const_cast<RawHeap&>(*this).Space(space_index); return const_cast<RawHeap&>(*this).Space(space_index);
} }
size_t SpaceIndexForCustomSpace(CustomSpaceIndex space_index) const {
DCHECK_LT(space_index, spaces_.size() - kNumberOfRegularSpaces);
return kNumberOfRegularSpaces + space_index;
}
Heap* heap() { return main_heap_; }
const Heap* heap() const { return main_heap_; }
private:
Heap* main_heap_; Heap* main_heap_;
Spaces spaces_; Spaces spaces_;
}; };
......
...@@ -46,7 +46,6 @@ TEST_F(PageTest, SpaceIndexing) { ...@@ -46,7 +46,6 @@ TEST_F(PageTest, SpaceIndexing) {
RawHeap& heap = GetRawHeap(); RawHeap& heap = GetRawHeap();
size_t space = 0u; size_t space = 0u;
for (const auto& ptr : heap) { for (const auto& ptr : heap) {
EXPECT_EQ(ptr.get(), heap.Space(space));
EXPECT_EQ(&heap, ptr.get()->raw_heap()); EXPECT_EQ(&heap, ptr.get()->raw_heap());
EXPECT_EQ(space, ptr->index()); EXPECT_EQ(space, ptr->index());
++space; ++space;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment