Line data Source code
1 : // Copyright 2016 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 : #include "src/zone/accounting-allocator.h"
6 :
7 : #include "src/allocation.h"
8 : #include "src/base/logging.h"
9 : #include "src/zone/zone-segment.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 : AccountingAllocator::~AccountingAllocator() = default;
15 :
16 59494732 : Segment* AccountingAllocator::AllocateSegment(size_t bytes) {
17 59494732 : void* memory = AllocWithRetry(bytes);
18 59544195 : if (memory == nullptr) return nullptr;
19 :
20 : size_t current =
21 59548600 : current_memory_usage_.fetch_add(bytes, std::memory_order_relaxed) + bytes;
22 59548600 : size_t max = max_memory_usage_.load(std::memory_order_relaxed);
23 61870821 : while (current > max && !max_memory_usage_.compare_exchange_weak(
24 : max, current, std::memory_order_relaxed)) {
25 : // {max} was updated by {compare_exchange_weak}; retry.
26 : }
27 : DCHECK_LE(sizeof(Segment), bytes);
28 59548600 : return new (memory) Segment(bytes);
29 : }
30 :
31 59496763 : void AccountingAllocator::ReturnSegment(Segment* segment) {
32 59496763 : segment->ZapContents();
33 : current_memory_usage_.fetch_sub(segment->total_size(),
34 : std::memory_order_relaxed);
35 59494515 : segment->ZapHeader();
36 59520273 : free(segment);
37 59520273 : }
38 :
39 : } // namespace internal
40 122004 : } // namespace v8
|