Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
bump-allocator.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2024 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <EASTL/utility.h>
30#include <stdint.h>
31
32#include "psyqo/fragments.hh"
33#include "psyqo/kernel.hh"
35#include "psyqo/shared.hh"
36
37namespace psyqo {
38
55template <size_t N, Safe safety = Safe::Yes>
57 public:
58 template <Primitive P, typename... Args>
60 static constexpr size_t size = sizeof(Fragments::SimpleFragment<P>);
61 if constexpr (safety == Safe::Yes) {
62 psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
63 }
64 uint8_t *ptr = m_current;
65 m_current += size;
66 return *new (ptr) Fragments::SimpleFragment<P>(eastl::forward<Args>(args)...);
67 }
68 template <typename T, typename... Args>
69 T &allocate(Args &&...args) {
70 // Rounding to 4 keeps the cursor 4-aligned, so fragments allocated
71 // afterwards stay aligned.
72 size_t size = (sizeof(T) + 3) & ~size_t(3);
73 uint8_t *ptr = m_current;
74 if constexpr (alignof(T) > 1) {
75 static constexpr size_t a = alignof(T) - 1;
76 auto alignedptr = reinterpret_cast<uint8_t *>((reinterpret_cast<uintptr_t>(ptr) + a) & ~a);
77 size += alignedptr - ptr;
78 ptr = alignedptr;
79 }
80 if constexpr (safety == Safe::Yes) {
81 psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
82 }
83 m_current += size;
84 return *new (ptr) T(eastl::forward<Args>(args)...);
85 }
86 void reset() { m_current = m_memory; }
87 size_t remaining() const { return N - (m_current - m_memory); }
88 size_t used() const { return m_current - m_memory; }
89
90 private:
91 uint8_t m_memory[N] __attribute__((aligned(4)));
92 uint8_t *m_current = m_memory;
93};
94
102template <Safe safety = Safe::Yes>
104 public:
105 DynamicBumpAllocator(void *storage, size_t size)
106 : m_memory(reinterpret_cast<uint8_t *>(storage)), m_current(m_memory), m_size(size) {
107 if constexpr (safety == Safe::Yes) {
108 psyqo::Kernel::assert(storage && (reinterpret_cast<uintptr_t>(storage) & 3) == 0,
109 "DynamicBumpAllocator: storage must be non-null and 4-byte aligned");
110 }
111 }
112
113 template <Primitive P, typename... Args>
115 static constexpr size_t size = sizeof(Fragments::SimpleFragment<P>);
116 if constexpr (safety == Safe::Yes) {
117 psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
118 }
119 uint8_t *ptr = m_current;
120 m_current += size;
121 return *new (ptr) Fragments::SimpleFragment<P>(eastl::forward<Args>(args)...);
122 }
123
124 template <typename T, typename... Args>
125 T &allocate(Args &&...args) {
126 // Rounding to 4 keeps the cursor 4-aligned, so fragments allocated
127 // afterwards stay aligned.
128 size_t size = (sizeof(T) + 3) & ~size_t(3);
129 uint8_t *ptr = m_current;
130 if constexpr (alignof(T) > 1) {
131 static constexpr size_t a = alignof(T) - 1;
132 auto alignedptr = reinterpret_cast<uint8_t *>((reinterpret_cast<uintptr_t>(ptr) + a) & ~a);
133 size += alignedptr - ptr;
134 ptr = alignedptr;
135 }
136 if constexpr (safety == Safe::Yes) {
137 psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
138 }
139 m_current += size;
140 return *new (ptr) T(eastl::forward<Args>(args)...);
141 }
142
143 void reset() { m_current = m_memory; }
144 size_t remaining() const { return m_size - (m_current - m_memory); }
145 size_t used() const { return m_current - m_memory; }
146 size_t capacity() const { return m_size; }
147
148 private:
149 uint8_t *m_memory;
150 uint8_t *m_current;
151 size_t m_size;
152};
153
154} // namespace psyqo
A bump allocator for fragments.
Definition bump-allocator.hh:56
size_t remaining() const
Definition bump-allocator.hh:87
Fragments::SimpleFragment< P > & allocateFragment(Args &&...args)
Definition bump-allocator.hh:59
T & allocate(Args &&...args)
Definition bump-allocator.hh:69
size_t used() const
Definition bump-allocator.hh:88
void reset()
Definition bump-allocator.hh:86
A bump allocator whose capacity is chosen at runtime.
Definition bump-allocator.hh:103
T & allocate(Args &&...args)
Definition bump-allocator.hh:125
void reset()
Definition bump-allocator.hh:143
Fragments::SimpleFragment< P > & allocateFragment(Args &&...args)
Definition bump-allocator.hh:114
size_t used() const
Definition bump-allocator.hh:145
size_t capacity() const
Definition bump-allocator.hh:146
DynamicBumpAllocator(void *storage, size_t size)
Definition bump-allocator.hh:105
size_t remaining() const
Definition bump-allocator.hh:144
static int size
Definition string.h:149
The Primitive concept.
Definition primitive-concept.hh:38
volatile uint32_t * ptr
Definition cop0.c:80
#define N
Definition main.c:37
void assert(bool condition, const char *message, std::source_location location=std::source_location::current())
A simple assert macro.
Definition kernel.hh:366
Definition lua.hh:40
Definition fragments.hh:95