based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 63aeca13c6a6ac9fcc06d31e1c9e7de93b505cee
parent a0d7367e41db6fcb4dbc602c1023bea128d5b54a
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Fri, 27 Jun 2025 19:32:19 +0200

Add basic allocator

Diffstat:
A include/based/memory/allocator.hpp | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1 files changed, 63 insertions(+), 0 deletions(-)


diff --git a/ include/based/memory/allocator.hpp b/ include/based/memory/allocator.hpp

@@ -0,0 +1,63 @@

#pragma once

/*
* Standard: https://eel.is/c++draft/default.allocator
*/

#include "based/integral/types.hpp"
#include "based/trait/integral_constant.hpp"

namespace based
{

template<class T>
class Allocator;

template<>
class Allocator<void>
{
};

template<class T>
class Allocator
{
public:
using value_type = T;
using size_type = SizeT;
using difference_type = PtrDiffT;
using propagate_on_container_move_assignment = TrueType;

constexpr Allocator() noexcept = default;

template<class U>
// NOLINTNEXTLINE(*explicit*,*unused*)
constexpr Allocator(const Allocator<U>& that) noexcept
{
}

constexpr Allocator(const Allocator&) noexcept = default;
constexpr Allocator(Allocator&&) noexcept = default;

constexpr Allocator& operator=(const Allocator&) noexcept = default;
constexpr Allocator& operator=(Allocator&&) noexcept = default;

constexpr ~Allocator() = default;

constexpr T* allocate(size_type size);
constexpr void deallocate(T* ptr, size_type size);
};

template<class T>
constexpr T* Allocator<T>::allocate(Allocator::size_type size)
{
return static_cast<value_type*>(::operator new(size * sizeof(T)));
}

template<class T>
constexpr void Allocator<T>::deallocate(T* ptr, size_type size)
{
(void)size;
::operator delete(static_cast<void*>(ptr));
}

} // namespace based