diff --git a/src/zone/accounting-allocator.cc b/src/zone/accounting-allocator.cc index ae9c983310..a6071ab6fa 100644 --- a/src/zone/accounting-allocator.cc +++ b/src/zone/accounting-allocator.cc @@ -18,7 +18,7 @@ Segment* AccountingAllocator::AllocateSegment(size_t bytes) { if (memory == nullptr) return nullptr; size_t current = - current_memory_usage_.fetch_add(bytes, std::memory_order_relaxed); + current_memory_usage_.fetch_add(bytes, std::memory_order_relaxed) + bytes; size_t max = max_memory_usage_.load(std::memory_order_relaxed); while (current > max && !max_memory_usage_.compare_exchange_weak( max, current, std::memory_order_relaxed)) { diff --git a/test/cctest/test-allocation.cc b/test/cctest/test-allocation.cc index dd06535a98..bde7ef5df6 100644 --- a/test/cctest/test-allocation.cc +++ b/test/cctest/test-allocation.cc @@ -105,6 +105,34 @@ TEST(AccountingAllocatorOOM) { CHECK_EQ(result == nullptr, platform.oom_callback_called); } +TEST(AccountingAllocatorCurrentAndMax) { + AllocationPlatform platform; + v8::internal::AccountingAllocator allocator; + static constexpr size_t kAllocationSizes[] = {51, 231, 27}; + std::vector segments; + CHECK_EQ(0, allocator.GetCurrentMemoryUsage()); + CHECK_EQ(0, allocator.GetMaxMemoryUsage()); + size_t expected_current = 0; + size_t expected_max = 0; + for (size_t size : kAllocationSizes) { + segments.push_back(allocator.AllocateSegment(size)); + CHECK_NOT_NULL(segments.back()); + CHECK_EQ(size, segments.back()->total_size()); + expected_current += size; + if (expected_current > expected_max) expected_max = expected_current; + CHECK_EQ(expected_current, allocator.GetCurrentMemoryUsage()); + CHECK_EQ(expected_max, allocator.GetMaxMemoryUsage()); + } + for (auto* segment : segments) { + expected_current -= segment->total_size(); + allocator.ReturnSegment(segment); + CHECK_EQ(expected_current, allocator.GetCurrentMemoryUsage()); + } + CHECK_EQ(expected_max, allocator.GetMaxMemoryUsage()); + CHECK_EQ(0, allocator.GetCurrentMemoryUsage()); + CHECK(!platform.oom_callback_called); +} + TEST(MallocedOperatorNewOOM) { AllocationPlatform platform; CHECK(!platform.oom_callback_called);