BAN: Add allocator/deallocator references for kernel/userspace

This commit is contained in:
Bananymous 2022-12-13 13:40:54 +02:00
parent 52c793bb5c
commit b49a873d54
2 changed files with 25 additions and 20 deletions

18
BAN/include/BAN/Memory.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#if defined(__is_kernel)
#include <kernel/kmalloc.h>
#else
#include <stdlib.h>
#endif
namespace BAN
{
#if defined(__is_kernel)
static constexpr auto& allocator = kmalloc;
static constexpr auto& deallocator = kfree;
#else
static constexpr auto& allocator = malloc;
static constexpr auto& deallocator = free;
#endif
}

View File

@ -1,12 +1,7 @@
#pragma once #pragma once
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Memory.h>
#if defined(__is_kernel)
#include <kernel/kmalloc.h>
#else
#include <stdlib.h>
#endif
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
@ -19,16 +14,6 @@ namespace BAN
template<typename T> template<typename T>
class Queue class Queue
{ {
private:
#if defined(__is_kernel)
static constexpr auto& allocator = kmalloc;
static constexpr auto& deallocator = kfree;
#else
static constexpr auto& allocator = malloc;
static constexpr auto& deallocator = free;
#endif
public: public:
using size_type = uint32_t; using size_type = uint32_t;
using value_type = T; using value_type = T;
@ -58,7 +43,9 @@ namespace BAN
template<typename T> template<typename T>
Queue<T>::~Queue() Queue<T>::~Queue()
{ {
Queue<T>::deallocator(m_data); for (size_type i = 0; i < m_size; i++)
m_data[i].~T();
BAN::deallocator(m_data);
} }
template<typename T> template<typename T>
@ -110,12 +97,12 @@ namespace BAN
return {}; return {};
size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1); size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1);
void* new_data = Queue<T>::allocator(new_cap * sizeof(T)); void* new_data = BAN::allocator(new_cap * sizeof(T));
if (new_data == nullptr) if (new_data == nullptr)
return Error::FromString("Queue: out of memory"); return Error::FromString("Queue: Could not allocate memory");
memcpy(new_data, m_data, m_size * sizeof(T)); memcpy(new_data, m_data, m_size * sizeof(T));
Queue<T>::deallocator(m_data); BAN::deallocator(m_data);
m_data = (T*)new_data; m_data = (T*)new_data;
m_capacity = new_cap; m_capacity = new_cap;