• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 namespace v8 {
16 namespace internal {
17 
18 class Heap;
19 
20 // ----------------------------------------------------------------------------
21 // BaseSpace is the abstract superclass for all allocation spaces.
22 class V8_EXPORT_PRIVATE BaseSpace : public Malloced {
23  public:
heap()24   Heap* heap() const {
25     DCHECK_NOT_NULL(heap_);
26     return heap_;
27   }
28 
identity()29   AllocationSpace identity() { return id_; }
30 
31   // Returns name of the space.
32   static const char* GetSpaceName(AllocationSpace space);
33 
name()34   const char* name() { return GetSpaceName(id_); }
35 
AccountCommitted(size_t bytes)36   void AccountCommitted(size_t bytes) {
37     DCHECK_GE(committed_ + bytes, committed_);
38     committed_ += bytes;
39     if (committed_ > max_committed_) {
40       max_committed_ = committed_;
41     }
42   }
43 
AccountUncommitted(size_t bytes)44   void AccountUncommitted(size_t bytes) {
45     DCHECK_GE(committed_, committed_ - bytes);
46     committed_ -= bytes;
47   }
48 
49   // Return the total amount committed memory for this space, i.e., allocatable
50   // memory and page headers.
CommittedMemory()51   virtual size_t CommittedMemory() { return committed_; }
52 
MaximumCommittedMemory()53   virtual size_t MaximumCommittedMemory() { return max_committed_; }
54 
55   // Approximate amount of physical memory committed for this space.
56   virtual size_t CommittedPhysicalMemory() = 0;
57 
58   // Returns allocated size.
59   virtual size_t Size() = 0;
60 
61  protected:
BaseSpace(Heap * heap,AllocationSpace id)62   BaseSpace(Heap* heap, AllocationSpace id)
63       : heap_(heap), id_(id), committed_(0), max_committed_(0) {}
64 
65   virtual ~BaseSpace() = default;
66 
67  protected:
68   Heap* heap_;
69   AllocationSpace id_;
70 
71   // Keeps track of committed memory in a space.
72   std::atomic<size_t> committed_;
73   size_t max_committed_;
74 
75   DISALLOW_COPY_AND_ASSIGN(BaseSpace);
76 };
77 
78 }  // namespace internal
79 }  // namespace v8
80 
81 #endif  // V8_HEAP_BASE_SPACE_H_
82