• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <memory>
10 
11 #include "src/base/macros.h"
12 #include "src/logging/tracing-flags.h"
13 
14 namespace v8 {
15 
16 namespace base {
17 class BoundedPageAllocator;
18 }  // namespace base
19 
20 namespace internal {
21 
22 class Segment;
23 class VirtualMemory;
24 class Zone;
25 
26 class V8_EXPORT_PRIVATE AccountingAllocator {
27  public:
28   AccountingAllocator();
29   virtual ~AccountingAllocator();
30 
31   // Allocates a new segment. Returns nullptr on failed allocation.
32   Segment* AllocateSegment(size_t bytes, bool supports_compression);
33 
34   // Return unneeded segments to either insert them into the pool or release
35   // them if the pool is already full or memory pressure is high.
36   void ReturnSegment(Segment* memory, bool supports_compression);
37 
GetCurrentMemoryUsage()38   size_t GetCurrentMemoryUsage() const {
39     return current_memory_usage_.load(std::memory_order_relaxed);
40   }
41 
GetMaxMemoryUsage()42   size_t GetMaxMemoryUsage() const {
43     return max_memory_usage_.load(std::memory_order_relaxed);
44   }
45 
TraceZoneCreation(const Zone * zone)46   void TraceZoneCreation(const Zone* zone) {
47     if (V8_LIKELY(!TracingFlags::is_zone_stats_enabled())) return;
48     TraceZoneCreationImpl(zone);
49   }
50 
TraceZoneDestruction(const Zone * zone)51   void TraceZoneDestruction(const Zone* zone) {
52     if (V8_LIKELY(!TracingFlags::is_zone_stats_enabled())) return;
53     TraceZoneDestructionImpl(zone);
54   }
55 
TraceAllocateSegment(Segment * segment)56   void TraceAllocateSegment(Segment* segment) {
57     if (V8_LIKELY(!TracingFlags::is_zone_stats_enabled())) return;
58     TraceAllocateSegmentImpl(segment);
59   }
60 
61  protected:
TraceZoneCreationImpl(const Zone * zone)62   virtual void TraceZoneCreationImpl(const Zone* zone) {}
TraceZoneDestructionImpl(const Zone * zone)63   virtual void TraceZoneDestructionImpl(const Zone* zone) {}
TraceAllocateSegmentImpl(Segment * segment)64   virtual void TraceAllocateSegmentImpl(Segment* segment) {}
65 
66  private:
67   std::atomic<size_t> current_memory_usage_{0};
68   std::atomic<size_t> max_memory_usage_{0};
69 
70   std::unique_ptr<VirtualMemory> reserved_area_;
71   std::unique_ptr<base::BoundedPageAllocator> bounded_page_allocator_;
72 
73   DISALLOW_COPY_AND_ASSIGN(AccountingAllocator);
74 };
75 
76 }  // namespace internal
77 }  // namespace v8
78 
79 #endif  // V8_ZONE_ACCOUNTING_ALLOCATOR_H_
80