xref: /third_party/node/deps/v8/src/heap/base-space.h (revision 1cb0ef41)
1// Copyright 2020 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_HEAP_BASE_SPACE_H_
6#define V8_HEAP_BASE_SPACE_H_
7
8#include <atomic>
9
10#include "src/base/macros.h"
11#include "src/common/globals.h"
12#include "src/logging/log.h"
13#include "src/utils/allocation.h"
14
15namespace v8 {
16namespace internal {
17
18class Heap;
19
20// ----------------------------------------------------------------------------
21// BaseSpace is the abstract superclass for all allocation spaces.
22class V8_EXPORT_PRIVATE BaseSpace : public Malloced {
23 public:
24  BaseSpace(const BaseSpace&) = delete;
25  BaseSpace& operator=(const BaseSpace&) = delete;
26
27  Heap* heap() const {
28    DCHECK_NOT_NULL(heap_);
29    return heap_;
30  }
31
32  AllocationSpace identity() const { return id_; }
33
34  // Returns name of the space.
35  static const char* GetSpaceName(AllocationSpace space);
36
37  const char* name() const { return GetSpaceName(id_); }
38
39  void AccountCommitted(size_t bytes) {
40    DCHECK_GE(committed_ + bytes, committed_);
41    committed_ += bytes;
42    if (committed_ > max_committed_) {
43      max_committed_ = committed_;
44    }
45  }
46
47  void AccountUncommitted(size_t bytes) {
48    DCHECK_GE(committed_, committed_ - bytes);
49    committed_ -= bytes;
50  }
51
52  // Return the total amount committed memory for this space, i.e., allocatable
53  // memory and page headers.
54  virtual size_t CommittedMemory() const { return committed_; }
55
56  virtual size_t MaximumCommittedMemory() const { return max_committed_; }
57
58  // Approximate amount of physical memory committed for this space.
59  virtual size_t CommittedPhysicalMemory() const = 0;
60
61  // Returns allocated size.
62  virtual size_t Size() const = 0;
63
64 protected:
65  BaseSpace(Heap* heap, AllocationSpace id)
66      : heap_(heap), id_(id), committed_(0), max_committed_(0) {}
67
68  virtual ~BaseSpace() = default;
69
70 protected:
71  Heap* heap_;
72  AllocationSpace id_;
73
74  // Keeps track of committed memory in a space.
75  std::atomic<size_t> committed_;
76  size_t max_committed_;
77};
78
79}  // namespace internal
80}  // namespace v8
81
82#endif  // V8_HEAP_BASE_SPACE_H_
83