blob: d84207c2cd4473ddbaaee7c2adfe1e4444ed2b69 (
plain)
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
|
// Copyright 2020 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_CPPGC_HEAP_SPACE_H_
#define V8_HEAP_CPPGC_HEAP_SPACE_H_
#include <vector>
#include "src/base/logging.h"
#include "src/base/macros.h"
#include "src/heap/cppgc/free-list.h"
namespace cppgc {
namespace internal {
class RawHeap;
class BasePage;
// BaseSpace is responsible for page management.
class V8_EXPORT_PRIVATE BaseSpace {
public:
using Pages = std::vector<BasePage*>;
using iterator = Pages::iterator;
using const_iterator = Pages::const_iterator;
BaseSpace(const BaseSpace&) = delete;
BaseSpace& operator=(const BaseSpace&) = delete;
iterator begin() { return pages_.begin(); }
const_iterator begin() const { return pages_.begin(); }
iterator end() { return pages_.end(); }
const_iterator end() const { return pages_.end(); }
size_t size() const { return pages_.size(); }
bool is_large() const { return type_ == PageType::kLarge; }
size_t index() const { return index_; }
RawHeap* raw_heap() { return heap_; }
const RawHeap* raw_heap() const { return heap_; }
// Page manipulation functions.
void AddPage(BasePage*);
void RemovePage(BasePage*);
Pages RemoveAllPages();
protected:
enum class PageType { kNormal, kLarge };
explicit BaseSpace(RawHeap* heap, size_t index, PageType type);
private:
RawHeap* heap_;
Pages pages_;
const size_t index_;
const PageType type_;
};
class V8_EXPORT_PRIVATE NormalPageSpace final : public BaseSpace {
public:
class LinearAllocationBuffer {
public:
Address Allocate(size_t alloc_size) {
DCHECK_GE(size_, alloc_size);
Address result = start_;
start_ += alloc_size;
size_ -= alloc_size;
return result;
}
void Set(Address ptr, size_t size) {
start_ = ptr;
size_ = size;
}
Address start() const { return start_; }
size_t size() const { return size_; }
private:
Address start_ = nullptr;
size_t size_ = 0;
};
static NormalPageSpace* From(BaseSpace* space) {
DCHECK(!space->is_large());
return static_cast<NormalPageSpace*>(space);
}
static const NormalPageSpace* From(const BaseSpace* space) {
return From(const_cast<BaseSpace*>(space));
}
NormalPageSpace(RawHeap* heap, size_t index);
void AddToFreeList(void*, size_t);
void ResetLinearAllocationBuffer();
LinearAllocationBuffer& linear_allocation_buffer() { return current_lab_; }
const LinearAllocationBuffer& linear_allocation_buffer() const {
return current_lab_;
}
FreeList& free_list() { return free_list_; }
const FreeList& free_list() const { return free_list_; }
private:
LinearAllocationBuffer current_lab_;
FreeList free_list_;
};
class V8_EXPORT_PRIVATE LargePageSpace final : public BaseSpace {
public:
static LargePageSpace* From(BaseSpace* space) {
DCHECK(space->is_large());
return static_cast<LargePageSpace*>(space);
}
static const LargePageSpace* From(const BaseSpace* space) {
return From(const_cast<BaseSpace*>(space));
}
LargePageSpace(RawHeap* heap, size_t index);
};
} // namespace internal
} // namespace cppgc
#endif // V8_HEAP_CPPGC_HEAP_SPACE_H_
|