Skip to content

Commit

Permalink
Added LocalAllocatorRef and GlobalAllocatorRef
Browse files Browse the repository at this point in the history
  • Loading branch information
R533-Code authored Sep 24, 2024
1 parent ff26480 commit 2926700
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
89 changes: 89 additions & 0 deletions include/colt/mem/allocator_ref.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef HG_ALLOCATOR_REF
#define HG_ALLOCATOR_REF

#include "composable_alloc.h"

namespace clt::mem
{
template<fn_alloc_t ALLOC, fn_dealloc_t DEALLOC, fn_owns_t OWNS,
fn_expand_t EXPAND, fn_realloc_t REALLOC>
requires (ALLOC != nullptr) && (DEALLOC != nullptr)
struct GlobalAllocatorRef
{
MemBlock alloc(u64 sz)
{
return ALLOC(sz);
}

void dealloc(MemBlock sz)
{
DEALLOC(sz);
}

bool owns(MemBlock sz)
requires (OWNS != nullptr)
{
return OWNS(sz);
}

mem::MemBlock expand(mem::MemBlock sz, u64 dt)
requires (EXPAND != nullptr)
{
return EXPAND(sz, dt);
}

mem::MemBlock realloc(mem::MemBlock sz, u64 dt)
requires (REALLOC != nullptr)
{
return REALLOC(sz, dt);
}
};

template<meta::Allocator T>
class LocalAllocatorRef
{
T* ptr;

public:
LocalAllocatorRef() = delete;
LocalAllocatorRef(T& ptr) noexcept
: ptr(&ptr)
{
}

LocalAllocatorRef(LocalAllocatorRef&&) = default;
LocalAllocatorRef& operator=(LocalAllocatorRef&&) = default;
LocalAllocatorRef(const LocalAllocatorRef&) = default;
LocalAllocatorRef& operator=(const LocalAllocatorRef&) = default;

MemBlock alloc(u64 sz)
{
return ptr->alloc(sz);
}

void dealloc(MemBlock sz)
{
ptr->dealloc(sz);
}

bool owns(MemBlock sz)
requires meta::OwningAllocator<T>
{
return ptr->owns(sz);
}

mem::MemBlock expand(mem::MemBlock sz, u64 dt)
requires meta::ExpandingAllocator<T>
{
return ptr->expand(sz, dt);
}

mem::MemBlock realloc(mem::MemBlock sz, u64 dt)
requires meta::ReallocatableAllocator<T>
{
return ptr->realloc(sz, dt);
}
};
}

#endif //!HG_ALLOCATOR_REF
14 changes: 14 additions & 0 deletions include/colt/mem/allocator_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ namespace clt::meta
};
} // namespace clt::meta

namespace clt::mem
{
/// @brief Represents an allocation function
using fn_alloc_t = mem::MemBlock(*)(u64);
/// @brief Represents a deallocation function
using fn_dealloc_t = void(*)(mem::MemBlock);
/// @brief Represents an owning function
using fn_owns_t = bool(*)(mem::MemBlock);
/// @brief Represents an expansion function
using fn_expand_t = mem::MemBlock(*)(mem::MemBlock, u64);
/// @brief Represents a reallocation function
using fn_realloc_t = mem::MemBlock(*)(mem::MemBlock, u64);
}

#endif //!HG_COLT_ALLOCATOR_TRAITS

0 comments on commit 2926700

Please sign in to comment.