Add C++ Allocator for libctru linear memory
This commit is contained in:
parent
e78ac834db
commit
75f8b84794
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <renderd7/Allocator.hpp>
|
||||
#include <renderd7/DrawV2.hpp>
|
||||
#include <renderd7/Error.hpp>
|
||||
#include <renderd7/FileSystem.hpp>
|
||||
|
41
include/renderd7/Allocator.hpp
Normal file
41
include/renderd7/Allocator.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <3ds.h>
|
||||
|
||||
#include <renderd7/Error.hpp>
|
||||
|
||||
// Write own LinearAllocator for learning
|
||||
|
||||
namespace RenderD7 {
|
||||
template<typename T>
|
||||
class LinearAllocator : public std::allocator<T> {
|
||||
public:
|
||||
typedef size_t size_type;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
|
||||
template<typename T1>
|
||||
struct rebind {typedef LinearAllocator<T1> 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<T>& a) throw(): std::allocator<T>(a) {}
|
||||
~LinearAllocator() throw() {}
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user