v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
spaces.h
Go to the documentation of this file.
1// Copyright 2011 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_SPACES_H_
6#define V8_HEAP_SPACES_H_
7
8#include <atomic>
9#include <memory>
10
11#include "src/base/iterator.h"
12#include "src/base/macros.h"
13#include "src/common/globals.h"
15#include "src/heap/base-space.h"
17#include "src/heap/free-list.h"
19#include "src/heap/list.h"
25#include "src/heap/slot-set.h"
26#include "src/objects/objects.h"
28#include "src/utils/utils.h"
29#include "testing/gtest/include/gtest/gtest_prod.h" // nogncheck
30
31namespace v8 {
32namespace internal {
33
34namespace heap {
35class HeapTester;
36class TestCodePageAllocatorScope;
37} // namespace heap
38
39class AllocationObserver;
40class FreeList;
41class Heap;
42class Isolate;
43class LargeObjectSpace;
44class LargePageMetadata;
45class ObjectIterator;
46class PagedSpaceBase;
47class SemiSpace;
48
49// Some assertion macros used in the debugging mode.
50
51#define DCHECK_OBJECT_SIZE(size) \
52 DCHECK((0 < size) && (size <= kMaxRegularHeapObjectSize))
53
54#define DCHECK_CODEOBJECT_SIZE(size) \
55 DCHECK((0 < size) && (size <= MemoryChunkLayout::MaxRegularCodeObjectSize()))
56
57template <typename Enum, typename Callback>
58void ForAll(Callback callback) {
59 for (int i = 0; i < static_cast<int>(Enum::kNumValues); i++) {
60 callback(static_cast<Enum>(i), i);
61 }
62}
63
64// ----------------------------------------------------------------------------
65// Space is the abstract superclass for all allocation spaces that are not
66// sealed after startup (i.e. not ReadOnlySpace).
68 public:
69 static inline void MoveExternalBackingStoreBytes(
70 ExternalBackingStoreType type, Space* from, Space* to, size_t amount);
71
72 Space(Heap* heap, AllocationSpace id, std::unique_ptr<FreeList> free_list)
73 : BaseSpace(heap, id), free_list_(std::move(free_list)) {}
74
75 ~Space() override = default;
76
77 Space(const Space&) = delete;
78 Space& operator=(const Space&) = delete;
79
80 // Returns size of objects. Can differ from the allocated size
81 // (e.g. see OldLargeObjectSpace).
82 virtual size_t SizeOfObjects() const { return Size(); }
83
84 // Return the available bytes without growing.
85 virtual size_t Available() const = 0;
86
87 virtual std::unique_ptr<ObjectIterator> GetObjectIterator(Heap* heap) = 0;
88
89 inline void IncrementExternalBackingStoreBytes(ExternalBackingStoreType type,
90 size_t amount);
91 inline void DecrementExternalBackingStoreBytes(ExternalBackingStoreType type,
92 size_t amount);
93
94 // Returns amount of off-heap memory in-use by objects in this Space.
96 ExternalBackingStoreType type) const {
97 return external_backing_store_bytes_[static_cast<int>(type)];
98 }
99
101 return memory_chunk_list_.front();
102 }
103 virtual MutablePageMetadata* last_page() { return memory_chunk_list_.back(); }
104
105 virtual const MutablePageMetadata* first_page() const {
106 return memory_chunk_list_.front();
107 }
108 virtual const MutablePageMetadata* last_page() const {
109 return memory_chunk_list_.back();
110 }
111
113 return memory_chunk_list_;
114 }
115
119
120 virtual void NotifyBlackAreaCreated(size_t size) {}
121 virtual void NotifyBlackAreaDestroyed(size_t size) {}
122
123 FreeList* free_list() { return free_list_.get(); }
124
125 Address FirstPageAddress() const {
126 DCHECK_NOT_NULL(first_page());
127 return first_page()->ChunkAddress();
128 }
129
130#ifdef DEBUG
131 virtual void Print() = 0;
132#endif
133
134 protected:
135 // The List manages the pages that belong to the given space.
137 // Tracks off-heap memory used by this space.
138 std::atomic<size_t> external_backing_store_bytes_[static_cast<int>(
139 ExternalBackingStoreType::kNumValues)] = {0};
140 std::unique_ptr<FreeList> free_list_;
141};
142
143static_assert(sizeof(std::atomic<intptr_t>) == kSystemPointerSize);
144
145// -----------------------------------------------------------------------------
146// Interface for heap object iterator to be implemented by all object space
147// object iterators.
148
150 public:
151 // Note: The destructor can not be marked as `= default` as this causes
152 // the compiler on C++20 to define it as `constexpr` resulting in the
153 // compiler producing warnings about undefined inlines for Next()
154 // on classes inheriting from it.
155 virtual ~ObjectIterator() {}
157};
158
159template <class PageType>
161 : public base::iterator<std::forward_iterator_tag, PageType> {
162 public:
163 explicit PageIteratorImpl(PageType* p) : p_(p) {}
166
167 PageType* operator*() { return p_; }
168 bool operator==(const PageIteratorImpl<PageType>& rhs) const {
169 return rhs.p_ == p_;
170 }
171 bool operator!=(const PageIteratorImpl<PageType>& rhs) const {
172 return rhs.p_ != p_;
173 }
176
177 private:
178 PageType* p_;
179};
180
185
187 public:
191 inline explicit PageRange(PageMetadata* page);
192
194 iterator end() { return iterator(end_); }
195
196 private:
199};
200
202 public:
206 inline explicit ConstPageRange(const PageMetadata* page);
207
209 iterator end() { return iterator(end_); }
210
211 private:
214};
215
217 public:
218 // Creates this space and uses the existing `allocator`. It doesn't create a
219 // new MainAllocator instance.
221 std::unique_ptr<FreeList> free_list);
222
224
225 friend class MainAllocator;
226};
227
229 public:
230 explicit SpaceIterator(Heap* heap);
231 virtual ~SpaceIterator();
232
233 bool HasNext();
234 Space* Next();
235
236 private:
238 int current_space_; // from enum AllocationSpace.
239};
240
241// Iterates over all memory chunks in the heap (across all spaces).
253
254} // namespace internal
255} // namespace v8
256
257#endif // V8_HEAP_SPACES_H_
const PageMetadata * begin_
Definition spaces.h:212
const PageMetadata * end_
Definition spaces.h:213
ConstPageRange(const PageMetadata *begin, const PageMetadata *end)
Definition spaces.h:204
ConstPageIterator iterator
Definition spaces.h:203
MutablePageMetadata * current_chunk_
Definition spaces.h:251
V8_INLINE MutablePageMetadata * Next()
Definition spaces-inl.h:128
virtual Tagged< HeapObject > Next()=0
PageIteratorImpl(PageType *p)
Definition spaces.h:163
PageIteratorImpl(const PageIteratorImpl &) V8_NOEXCEPT=default
PageIteratorImpl< PageType > & operator++()
Definition spaces-inl.h:24
bool operator==(const PageIteratorImpl< PageType > &rhs) const
Definition spaces.h:168
bool operator!=(const PageIteratorImpl< PageType > &rhs) const
Definition spaces.h:171
PageIteratorImpl & operator=(const PageIteratorImpl &) V8_NOEXCEPT=default
PageMetadata * end_
Definition spaces.h:198
PageIterator iterator
Definition spaces.h:188
PageRange(PageMetadata *begin, PageMetadata *end)
Definition spaces.h:189
PageMetadata * begin_
Definition spaces.h:197
virtual AllocatorPolicy * CreateAllocatorPolicy(MainAllocator *allocator)=0
FreeList * free_list()
Definition spaces.h:123
Address FirstPageAddress() const
Definition spaces.h:125
virtual MutablePageMetadata * last_page()
Definition spaces.h:103
virtual MutablePageMetadata * first_page()
Definition spaces.h:100
virtual void NotifyBlackAreaDestroyed(size_t size)
Definition spaces.h:121
virtual void NotifyBlackAreaCreated(size_t size)
Definition spaces.h:120
virtual PageMetadata * InitializePage(MutablePageMetadata *chunk)
Definition spaces.h:116
Space & operator=(const Space &)=delete
virtual const MutablePageMetadata * first_page() const
Definition spaces.h:105
virtual std::unique_ptr< ObjectIterator > GetObjectIterator(Heap *heap)=0
Space(const Space &)=delete
virtual const MutablePageMetadata * last_page() const
Definition spaces.h:108
virtual size_t ExternalBackingStoreBytes(ExternalBackingStoreType type) const
Definition spaces.h:95
Space(Heap *heap, AllocationSpace id, std::unique_ptr< FreeList > free_list)
Definition spaces.h:72
virtual size_t Available() const =0
std::unique_ptr< FreeList > free_list_
Definition spaces.h:140
~Space() override=default
virtual heap::List< MutablePageMetadata > & memory_chunk_list()
Definition spaces.h:112
heap::List< MutablePageMetadata > memory_chunk_list_
Definition spaces.h:136
virtual size_t SizeOfObjects() const
Definition spaces.h:82
FreeList & free_list_
Definition sweeper.cc:156
TNode< Object > callback
STL namespace.
constexpr int kSystemPointerSize
Definition globals.h:410
void ForAll(Callback callback)
Definition spaces.h:58
ExternalBackingStoreType
Definition globals.h:1605
PageIteratorImpl< PageMetadata > PageIterator
Definition spaces.h:181
PageIteratorImpl< const PageMetadata > ConstPageIterator
Definition spaces.h:182
#define V8_NOEXCEPT
#define UNREACHABLE()
Definition logging.h:67
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_INLINE
Definition v8config.h:500
wasm::ValueType type