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 : #ifndef V8_ZONE_ACCOUNTING_ALLOCATOR_H_
6 : #define V8_ZONE_ACCOUNTING_ALLOCATOR_H_
7 :
8 : #include <atomic>
9 :
10 : #include "src/base/macros.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : class Segment;
16 : class Zone;
17 :
18 2664558 : class V8_EXPORT_PRIVATE AccountingAllocator {
19 : public:
20 2675016 : AccountingAllocator() = default;
21 : virtual ~AccountingAllocator();
22 :
23 : // Allocates a new segment. Returns nullptr on failed allocation.
24 : virtual Segment* AllocateSegment(size_t bytes);
25 :
26 : // Return unneeded segments to either insert them into the pool or release
27 : // them if the pool is already full or memory pressure is high.
28 : virtual void ReturnSegment(Segment* memory);
29 :
30 : size_t GetCurrentMemoryUsage() const {
31 : return current_memory_usage_.load(std::memory_order_relaxed);
32 : }
33 :
34 : size_t GetMaxMemoryUsage() const {
35 : return max_memory_usage_.load(std::memory_order_relaxed);
36 : }
37 :
38 92122297 : virtual void ZoneCreation(const Zone* zone) {}
39 92062697 : virtual void ZoneDestruction(const Zone* zone) {}
40 :
41 : private:
42 : std::atomic<size_t> current_memory_usage_{0};
43 : std::atomic<size_t> max_memory_usage_{0};
44 :
45 : DISALLOW_COPY_AND_ASSIGN(AccountingAllocator);
46 : };
47 :
48 : } // namespace internal
49 : } // namespace v8
50 :
51 : #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_
|