ReXGlue SDK
Loading...
Searching...
No Matches
guest_allocator.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <memory>
14#include <mutex>
15
16#include <rex/platform.h>
17#include <rex/result.h>
18#include <rex/types.h>
19#include <rex/utility.h>
20
21namespace rex {
22namespace kernel {
23class GuestMemory;
24} // namespace kernel
25} // namespace rex
26
28class GuestHeap;
29} // namespace rex::kernel::shim
30
31namespace rex::kernel {
32
34class BestFitRangeAllocator;
35class WorkspaceHeap;
36
38enum class GuestPool : u8 {
40 kWorkspace,
41 kTitle,
43};
44
49class REX_KERNEL_API GuestAllocator : public NonCopyable {
51 struct Key {};
52
53 public:
55 static constexpr u32 kBlockAlignment = 16;
56
58 u64 capacity;
59 u64 allocated;
60 u64 peak_allocated;
61 u64 peak_request_size;
62 u64 oom_count;
63 };
64
71 GuestAllocator(Key, GuestAddressSpace& memory, u32 base, u32 size, void* state, GuestPool pool);
72
84 rex::kernel::GuestMemory& memory, u32 size, u32 alignment, bool physical, u32 page_size,
85 u32 flags, GuestPool pool);
86
88 [[nodiscard]] static const char* GetPoolPlotName(GuestPool pool);
89
91
92 [[nodiscard]] u32 GetBase() const;
93
94 [[nodiscard]] u32 GetSize() const;
95
96 [[nodiscard]] GuestPool GetPool() const;
97
98 [[nodiscard]] bool Contains(u32 address) const;
99
105 [[nodiscard]] void* Allocate(u32 size);
106
114 [[nodiscard]] void* Reallocate(void* block, u32 size);
115
121 void Free(void* block);
122
123 [[nodiscard]] AllocatorStats GetStats() const;
124
125 [[nodiscard]] bool IsConsistent() const;
126
127 private:
128 friend class rex::kernel::shim::GuestHeap;
129
131 u32 size, u32 alignment,
132 bool physical, u32 page_size,
133 u32 flags, GuestPool pool);
134
135 GuestAddressSpace& memory_;
136 u32 base_;
137 u32 size_;
138 void* state_;
139 GuestPool pool_;
140 mutable std::mutex mutex_;
141 u64 capacity_ = 0;
142 u64 allocated_ = 0;
143 u64 peak_allocated_ = 0;
144 u64 peak_request_size_ = 0;
145 u64 oom_count_ = 0;
146};
147
148} // namespace rex::kernel
149
150namespace rex::kernel {
151
152class GuestAddressSpace;
153
156 public:
157 virtual ~ITitleWorkspace() = default;
158
165 [[nodiscard]] virtual Result<u32> Allocate(u32 size, bool zero) = 0;
166
172 virtual void Free(u32 address) = 0;
173};
174
179class REX_KERNEL_API WorkspaceHeap : public ITitleWorkspace, public NonCopyable {
181 struct Key {};
182
183 public:
185 WorkspaceHeap(Key, GuestAddressSpace& memory, u32 base, u32 size);
186
187 ~WorkspaceHeap() override;
188
195 u32 size);
196
202 [[nodiscard]] static u32 ResolveSize(u32 header_size);
203
204 [[nodiscard]] u32 GetSize() const;
205
207 [[nodiscard]] u32 GetAllocated() const;
208
215 [[nodiscard]] Result<u32> Allocate(u32 size, bool zero) override;
216
223 void Free(u32 address) override;
224
225 private:
226 GuestAddressSpace& memory_;
227 u32 base_;
228 u32 size_;
229 std::unique_ptr<BestFitRangeAllocator> ranges_;
230 mutable std::mutex mutex_;
231};
232
233} // namespace rex::kernel
Definition heap.h:594
static const char * GetPoolPlotName(GuestPool pool)
static constexpr u32 kBlockAlignment
Definition guest_allocator.h:55
void * Reallocate(void *block, u32 size)
GuestAllocator(Key, GuestAddressSpace &memory, u32 base, u32 size, void *state, GuestPool pool)
void Free(void *block)
void * Allocate(u32 size)
static Result< std::unique_ptr< GuestAllocator > > Create(rex::kernel::GuestMemory &memory, u32 size, u32 alignment, bool physical, u32 page_size, u32 flags, GuestPool pool)
Definition guest_memory.h:46
Definition guest_allocator.h:155
virtual Result< u32 > Allocate(u32 size, bool zero)=0
virtual void Free(u32 address)=0
Definition guest_allocator.h:179
void Free(u32 address) override
static u32 ResolveSize(u32 header_size)
WorkspaceHeap(Key, GuestAddressSpace &memory, u32 base, u32 size)
Result< u32 > Allocate(u32 size, bool zero) override
static Result< std::unique_ptr< WorkspaceHeap > > Create(GuestAddressSpace &memory, u32 size)
Definition guest_allocator.h:27
Definition export.h:24
GuestPool
Definition guest_allocator.h:38
@ kHeap
Definition guest_allocator.h:39
@ kCount
Definition guest_allocator.h:42
GuestHeap
Definition guest_memory.h:35
Definition endian.h:27
Definition app.h:36
std::expected< T, Error > Result
Definition result.h:98
Platform flags, packing macros and export markers.
#define REX_KERNEL_API
Definition platform.h:297
Definition guest_allocator.h:57
Integer types, byte order wrappers and size literals.
Move only function, const propagating pointer and scope guard.