From 75f8b84794fc52fecefa31b4f6c610ec98b78c0c Mon Sep 17 00:00:00 2001 From: tobid7 Date: Thu, 2 May 2024 20:17:37 +0200 Subject: [PATCH] Add C++ Allocator for libctru linear memory --- include/rd7.hpp | 1 + include/renderd7/Allocator.hpp | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 include/renderd7/Allocator.hpp diff --git a/include/rd7.hpp b/include/rd7.hpp index 50812c5..edc5898 100644 --- a/include/rd7.hpp +++ b/include/rd7.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/include/renderd7/Allocator.hpp b/include/renderd7/Allocator.hpp new file mode 100644 index 0000000..f32e6d4 --- /dev/null +++ b/include/renderd7/Allocator.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include +#include <3ds.h> + +#include + +// Write own LinearAllocator for learning + +namespace RenderD7 { + template + class LinearAllocator : public std::allocator { + public: + typedef size_t size_type; + typedef T* pointer; + typedef const T* const_pointer; + + template + struct rebind {typedef LinearAllocator other;}; + + pointer allocate(size_type n, const void* hint=nullptr) { + if(n > this->max_size()) { + RenderD7::Error("Linear Allocator: \nBad Alloc -> size is larger than free space!"); + return nullptr; + } + return (pointer)linearAlloc(n*sizeof(T)); + } + + void deallocate(pointer p, size_type) { + linearFree((void*)p); + } + + size_type max_size() { + return linearSpaceFree(); + } + + LinearAllocator() throw() {} + LinearAllocator(const LinearAllocator& a) throw(): std::allocator(a) {} + ~LinearAllocator() throw() {} + }; +} \ No newline at end of file