mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
allocator_base.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <type_traits>
6
7
8namespace m {
9
10template <typename T>
11concept is_allocator = requires (T t, size_t size, size_t alignment, void *ptr) {
12 { t.allocate(size, alignment) } -> std::same_as<void*>;
13 { t.deallocate(ptr, size) } -> std::same_as<void>;
14};
15
16template<typename Actual>
18{
19 using size_type = std::size_t;
20
21 /*------------------------------------------------------------------------------------------------------------------
22 * Core routines
23 *----------------------------------------------------------------------------------------------------------------*/
24
26 void * allocate(size_type size, size_type alignment = 0) {
27 return reinterpret_cast<Actual*>(this)->allocate(size, alignment);
28 }
29
31 void deallocate(void *ptr, size_type size) { reinterpret_cast<Actual*>(this)->deallocate(ptr, size); }
32
33 /*------------------------------------------------------------------------------------------------------------------
34 * Convenience overloads
35 *----------------------------------------------------------------------------------------------------------------*/
36
38 template<typename T>
39 std::enable_if_t<not std::is_void_v<T>, T*>
40 allocate() { return reinterpret_cast<T*>(allocate(sizeof(T), alignof(T))); }
41
44 template<typename T>
45 std::enable_if_t<not std::is_void_v<T>, T*>
46 allocate(size_type n) { return reinterpret_cast<T*>(allocate(n * sizeof(T), alignof(T))); }
47
49 template<typename T>
50 std::enable_if_t<not std::is_void_v<T>, void>
51 deallocate(T *ptr) { deallocate(reinterpret_cast<void*>(ptr), sizeof(T)); }
52
54 template<typename T>
55 std::enable_if_t<not std::is_void_v<T>, void>
56 deallocate(T *arr, size_type n) { deallocate(reinterpret_cast<void*>(arr), n * sizeof(T)); }
57
58 /*------------------------------------------------------------------------------------------------------------------
59 * make_unique
60 *----------------------------------------------------------------------------------------------------------------*/
61
62 template<typename T>
63 std::enable_if_t<not std::is_array_v<T>, std::unique_ptr<T>>
64 make_unique() { return std::unique_ptr<T>(allocate<T>()); }
65
66 template<typename T>
67 std::enable_if_t<std::is_array_v<T>, std::unique_ptr<T>>
68 make_unique(size_type n) { return std::unique_ptr<T>(allocate<std::remove_extent_t<T>>(n)); }
69
70 template<typename T>
71 void dispose(std::unique_ptr<T> ptr) { deallocate<T>(ptr.release()); }
72
73 template<typename T>
74 void dispose(std::unique_ptr<T> ptr, size_type n) { deallocate<std::remove_extent_t<T>>(ptr.release(), n); }
75};
76
77}
‍mutable namespace
Definition: Backend.hpp:10
T(x)
std::enable_if_t< not std::is_void_v< T >, T * > allocate()
Allocate space for a single entity of type T that is aligned according to Ts alignment requirement.
void * allocate(size_type size, size_type alignment=0)
Allocate size bytes aligned to alignment.
void deallocate(void *ptr, size_type size)
Deallocate the allocation at ptr of size size.
std::enable_if_t< not std::is_array_v< T >, std::unique_ptr< T > > make_unique()
std::enable_if_t< not std::is_void_v< T >, void > deallocate(T *arr, size_type n)
Deallocate the space for an array of n entities of type T.
std::size_t size_type
std::enable_if_t< not std::is_void_v< T >, void > deallocate(T *ptr)
Deallocate the space for an entity of type T at ptr.
std::enable_if_t< std::is_array_v< T >, std::unique_ptr< T > > make_unique(size_type n)
void dispose(std::unique_ptr< T > ptr, size_type n)
void dispose(std::unique_ptr< T > ptr)
std::enable_if_t< not std::is_void_v< T >, T * > allocate(size_type n)
Allocate space for an array of n entities of type T.