Nugget
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 size_t size = sizeof(T);
71 uint8_t *ptr = m_current;
72 if constexpr (alignof(T) > 1) {
73 static constexpr size_t a = alignof(T) - 1;
74 auto alignedptr = reinterpret_cast<uint8_t *>((reinterpret_cast<uintptr_t>(ptr) + a) & ~a);
75 size += alignedptr - ptr;
76 ptr = alignedptr;
77 }
78 if constexpr (safety == Safe::Yes) {
79 psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
80 }
81 m_current += size;
82 return *new (ptr) T(eastl::forward<Args>(args)...);
83 }
84 void reset() { m_current = m_memory; }
85 size_t remaining() const { return N - (m_current - m_memory); }
86 size_t used() const { return m_current - m_memory; }
87
88 private:
89 uint8_t m_memory[N] __attribute__((aligned(4)));
90 uint8_t *m_current = m_memory;
91};
92
93} // namespace psyqo
A bump allocator for fragments.
Definition bump-allocator.hh:56
size_t remaining() const
Definition bump-allocator.hh:85
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:86
void reset()
Definition bump-allocator.hh:84
The Primitive concept.
Definition primitive-concept.hh:38
volatile uint32_t * ptr
Definition cop0.c:80
void assert(bool condition, const char *message, std::source_location location=std::source_location::current())
A simple assert macro.
Definition kernel.hh:300
Definition cdrom-loader.hh:39
static int size
Definition string.h:32
A fragment containing a single primitive.
Definition fragments.hh:67