Skip to content

Commit

Permalink
Merge pull request #1789 from nicolasnoble/bump-allocator-refactor
Browse files Browse the repository at this point in the history
Making the bump allocator more generic.
  • Loading branch information
nicolasnoble authored Oct 23, 2024
2 parents 28613a5 + 6345979 commit 4b98d6b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 0 additions & 2 deletions src/mips/psyqo/advancedpad.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ SOFTWARE.
#include <EASTL/functional.h>
#include <stdint.h>

#include "psyqo/application.hh"

namespace psyqo {

/**
Expand Down
27 changes: 16 additions & 11 deletions src/mips/psyqo/bump-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ SOFTWARE.

#include <stdint.h>

#include "psyqo/fragment-concept.hh"
#include "psyqo/fragments.hh"
#include "psyqo/primitive-concept.hh"

Expand All @@ -53,19 +52,25 @@ namespace psyqo {
template <size_t N>
class BumpAllocator {
public:
template <Primitive P>
Fragments::SimpleFragment<P> &allocate() {
constexpr size_t size = sizeof(Fragments::SimpleFragment<P>);
auto *ptr = m_current;
template <Primitive P, typename ...Args>
Fragments::SimpleFragment<P> &allocateFragment(Args ...args) {
static constexpr size_t size = sizeof(Fragments::SimpleFragment<P>);
uint8_t *ptr = m_current;
m_current += size;
return *new (ptr) Fragments::SimpleFragment<P>();
return *new (ptr) Fragments::SimpleFragment<P>(args...);
}
template <Fragment F>
F &allocate() {
constexpr size_t size = sizeof(F);
auto *ptr = m_current;
template <typename T, typename ...Args>
T &allocate(Args ...args) {
size_t size = sizeof(T);
uint8_t *ptr = m_current;
if constexpr (alignof(T) > 1) {
static constexpr size_t a = alignof(T) - 1;
auto alignedptr = reinterpret_cast<uint8_t *>((reinterpret_cast<uintptr_t>(ptr) + a) & ~a);
size += alignedptr - ptr;
ptr = alignedptr;
}
m_current += size;
return *new (ptr) F();
return *new (ptr) T(args...);
}
void reset() { m_current = m_memory; }

Expand Down
3 changes: 2 additions & 1 deletion src/mips/psyqo/fragment-concept.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ SOFTWARE.

#pragma once

#include <stddef.h>

#include <concepts>
#include <cstddef>
#include <type_traits>

namespace psyqo {
Expand Down
3 changes: 0 additions & 3 deletions src/mips/psyqo/primitive-concept.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ SOFTWARE.

#pragma once

#include <concepts>
#include <cstddef>

namespace psyqo {

/**
Expand Down

0 comments on commit 4b98d6b

Please sign in to comment.