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