accounting-allocator.h 2.24 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_ZONE_ACCOUNTING_ALLOCATOR_H_
#define V8_ZONE_ACCOUNTING_ALLOCATOR_H_

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 17

namespace base {
class BoundedPageAllocator;
18
}  // namespace base
19

20 21
namespace internal {

22
class Segment;
23
class VirtualMemory;
24 25
class Zone;

26
class V8_EXPORT_PRIVATE AccountingAllocator {
27
 public:
28
  AccountingAllocator();
29 30
  AccountingAllocator(const AccountingAllocator&) = delete;
  AccountingAllocator& operator=(const AccountingAllocator&) = delete;
31 32
  virtual ~AccountingAllocator();

33
  // Allocates a new segment. Returns nullptr on failed allocation.
34
  Segment* AllocateSegment(size_t bytes, bool supports_compression);
35

36 37
  // Return unneeded segments to either insert them into the pool or release
  // them if the pool is already full or memory pressure is high.
38
  void ReturnSegment(Segment* memory, bool supports_compression);
39

40 41 42 43 44 45 46
  size_t GetCurrentMemoryUsage() const {
    return current_memory_usage_.load(std::memory_order_relaxed);
  }

  size_t GetMaxMemoryUsage() const {
    return max_memory_usage_.load(std::memory_order_relaxed);
  }
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  void TraceZoneCreation(const Zone* zone) {
    if (V8_LIKELY(!TracingFlags::is_zone_stats_enabled())) return;
    TraceZoneCreationImpl(zone);
  }

  void TraceZoneDestruction(const Zone* zone) {
    if (V8_LIKELY(!TracingFlags::is_zone_stats_enabled())) return;
    TraceZoneDestructionImpl(zone);
  }

  void TraceAllocateSegment(Segment* segment) {
    if (V8_LIKELY(!TracingFlags::is_zone_stats_enabled())) return;
    TraceAllocateSegmentImpl(segment);
  }

 protected:
  virtual void TraceZoneCreationImpl(const Zone* zone) {}
  virtual void TraceZoneDestructionImpl(const Zone* zone) {}
  virtual void TraceAllocateSegmentImpl(Segment* segment) {}
heimbuef's avatar
heimbuef committed
67

68
 private:
69 70
  std::atomic<size_t> current_memory_usage_{0};
  std::atomic<size_t> max_memory_usage_{0};
71

72 73
  std::unique_ptr<VirtualMemory> reserved_area_;
  std::unique_ptr<base::BoundedPageAllocator> bounded_page_allocator_;
74 75 76 77 78 79
};

}  // namespace internal
}  // namespace v8

#endif  // V8_ZONE_ACCOUNTING_ALLOCATOR_H_