32#ifndef FENNEC_MEMORY_ALLOCATOR_H
33#define FENNEC_MEMORY_ALLOCATOR_H
35#include <fennec/memory/pointer_traits.h>
47#ifdef FENNEC_COMPILER_GCC
48#pragma GCC diagnostic push
49#pragma GCC diagnostic ignored "-Wchanges-meaning"
64 template<
typename ClassT>
using _pointer =
typename ClassT::pointer_t;
65 template<
typename ClassT>
using _const_pointer =
typename ClassT::const_pointer_t;
66 template<
typename ClassT>
using _void_pointer =
typename ClassT::void_pointer_t;
67 template<
typename ClassT>
using _void_const_pointer =
typename ClassT::void_const_pointer_t;
70 template<
typename ClassT>
using _propagate_on_containter_copy_assignment =
typename ClassT::propagate_on_containter_copy_assignment;
71 template<
typename ClassT>
using _propagate_on_containter_move_assignment =
typename ClassT::propagate_on_containter_move_assignment;
72 template<
typename ClassT>
using _propagate_on_containter_swap =
typename ClassT::propagate_on_containter_swap;
74 template<
typename ClassT>
using _is_always_equal =
typename ClassT::is_always_equal;
76 template<
typename AllocT,
typename TypeT>
79 template<
typename AllocT,
typename TypeT>
87 template<
typename AllocT,
typename PtrT,
typename =
void>
90 template<
typename AllocT,
typename PtrT>
93 template<
typename AllocT,
typename DiffT,
typename =
void>
96 template<
typename AllocT,
typename DiffT>
120 using diff_t =
typename _diff<Alloc, pointer_t>::type;
123 using size_t =
typename _size<Alloc, pointer_t>::type;
134 template<
typename TypeT>
using rebind =
typename _rebind<Alloc, TypeT>::type;
193 return static_cast<T*
>(
::operator new(
n *
sizeof(
T), align));
261 return static_cast<T*
>(
::operator new[](
n *
sizeof(
T), align));
283template<
typename T,
class AllocT = allocator<T>>
305 : _data(
nullptr), _capacity(0), _alignment(zero<align_t>()) {
312 : _data(
nullptr), _capacity(0), _alignment(zero<align_t>()) {
323 fennec::memmove(_data,
data, n);
333 , _alignment(align) {
345 fennec::memmove(
static_cast<void*
>(_data),
data, n);
357 , _alignment(zero<align_t>()){
370 , _alignment(zero<align_t>()) {
384 fennec::memmove(
static_cast<void*
>(_data),
data, n);
398 , _alignment(zero<align_t>()) {
413 fennec::memmove(_data,
data, n);
420 : _alloc(alloc._alloc)
421 , _data(_alloc.allocate(alloc._capacity))
422 , _capacity(alloc._capacity)
423 , _alignment(alloc._alignment) {
424 fennec::memmove(
static_cast<void*
>(_data), alloc._data, alloc._capacity *
sizeof(T));
432 : _alloc(alloc._alloc)
434 , _capacity(alloc._capacity)
435 , _alignment(alloc._alignment) {
436 alloc._data =
nullptr; alloc._capacity = 0;
444 _alloc.deallocate(_data);
458 fennec::memmove(_data, alloc,
size());
484 constexpr void allocate(
size_t n, align_t align = zero<align_t>()) noexcept {
487 if (align != zero<align_t>()) {
488 _data = _alloc.allocate(_capacity = n, _alignment = align);
490 _data = _alloc.allocate(_capacity = n);
498 constexpr void callocate(
size_t n, align_t align = zero<align_t>()) noexcept {
500 fennec::memset(
static_cast<void*
>(_data), 0, _capacity *
sizeof(T));
507 if (_alignment != zero<align_t>()) {
508 _alloc.deallocate(_data, _alignment);
510 _alloc.deallocate(_data);
516 _alignment = zero<align_t>();
522 constexpr void reallocate(
size_t n, align_t align = zero<align_t>()) noexcept {
523 if (_data ==
nullptr) {
528 value_t* old = _data;
size_t old_cap = _capacity;
532 fennec::memmove(
static_cast<void*
>(_data), old,
min(_capacity, old_cap) *
sizeof(T));
534 _alloc.deallocate(old);
541 constexpr void creallocate(
size_t n, align_t align = zero<align_t>()) noexcept {
542 if (_data ==
nullptr) {
547 value_t* old = _data;
size_t old_cap = _capacity;
551 fennec::memmove(
static_cast<void*
>(_data), old,
min(_capacity, old_cap) *
sizeof(T));
553 if (_capacity > old_cap) {
554 fennec::memset(
static_cast<void*
>(_data + old_cap), 0, _capacity - old_cap);
557 _alloc.deallocate(old);
564 constexpr value_t& operator[](
size_t i) {
565 assertd(i <
capacity(),
"Array Out of Bounds");
569 constexpr const value_t& operator[](
size_t i)
const {
570 assertd(i <
capacity(),
"Array Out of Bounds");
574 constexpr operator value_t*() {
578 constexpr operator const value_t*()
const {
604 fennec::memset(_data, 0, _capacity *
sizeof(T));
610 constexpr size_t size()
const {
611 return _capacity *
sizeof(T);
635 constexpr align_t alignment()
const {
646#ifdef FENNEC_COMPILER_GCC
647#pragma GCC diagnostic pop
constexpr bool_t operator!=(const allocator< U > &)
Inequality operator for allocators of same type but with different data type.
Definition allocator.h:250
constexpr allocator(const allocator &)=default
Copy Constructor.
constexpr bool_t operator==(const allocator &)
Equality operator.
Definition allocator.h:235
constexpr T * allocate(size_t n)
Allocate a block of memory large enough to hold n elements of type T
Definition allocator.h:255
T value_t
Alias for the data type used for metaprogramming.
Definition allocator.h:216
constexpr bool_t operator==(const allocator< U > &)
Equality operator for allocators of same type but with different data type.
Definition allocator.h:245
constexpr void deallocate(T *ptr, align_t align)
Deallocate a block of memory with type T
Definition allocator.h:270
allocator< R > rebind
Metaprogramming utility to rebind an allocator to a different data type.
Definition allocator.h:219
constexpr void deallocate(T *ptr)
Deallocate a block of memory with type T
Definition allocator.h:265
constexpr T * allocate(size_t n, align_t align)
Allocate a block of memory large enough to hold n elements of type T
Definition allocator.h:260
constexpr allocator()=default
Default Constructor.
constexpr ~allocator()=default
Default Destructor.
constexpr bool_t operator!=(const allocator &)
Inequality operator.
Definition allocator.h:240
constexpr allocator & operator=(const allocator &)=default
Copy Assignment.
Allocator implementation, uses new and delete operators.
Definition allocator.h:145
constexpr bool_t operator==(const allocator< U > &)
Equality operator for allocators of same type but with different data type.
Definition allocator.h:177
constexpr void deallocate(T *ptr)
Deallocate a block of memory with type T
Definition allocator.h:197
allocator< R > rebind
Metaprogramming utility to rebind an allocator to a different data type.
Definition allocator.h:151
T value_t
Alias for the data type used for metaprogramming.
Definition allocator.h:148
constexpr bool_t operator==(const allocator &)
Equality operator.
Definition allocator.h:167
constexpr T * allocate(size_t n, align_t align)
Allocate a block of memory large enough to hold n elements of type T
Definition allocator.h:192
constexpr void deallocate(T *ptr, align_t align)
Deallocate a block of memory with type T
Definition allocator.h:202
constexpr ~allocator()=default
Default Destructor.
constexpr bool_t operator!=(const allocator &)
Inequality operator.
Definition allocator.h:172
constexpr allocator()=default
Default Constructor.
constexpr allocator & operator=(const allocator &)=default
Copy Assignment.
constexpr bool_t operator!=(const allocator< U > &)
Inequality operator for allocators of same type but with different data type.
Definition allocator.h:182
constexpr T * allocate(size_t n)
Allocate a block of memory large enough to hold n elements of type T
Definition allocator.h:187
constexpr allocator(const allocator &)=default
Copy Constructor.
constexpr genType min(genType x, genType y)
Returns if otherwise it returns .
Definition common.h:688
This header contains functions related to analyzing, modifying or copying buffers interpreted as byte...
Container to hold a memory allocation.
Definition allocator.h:285
constexpr void callocate(size_t n, align_t align=zero< align_t >()) noexcept
Allocate a block of memory for the allocation. If there is already an allocated block of memory,...
Definition allocator.h:498
constexpr void clear() noexcept
Clear the block of memory, setting all bytes to 0.
Definition allocator.h:603
constexpr allocation(const T *data, size_t n, align_t align)
Buffer Copy Constructor, initializes the allocation with a block of size n * sizeof(T) bytes....
Definition allocator.h:343
constexpr allocation(const alloc_t &alloc) noexcept
Allocator Constructor.
Definition allocator.h:353
constexpr allocation & operator=(allocation &&alloc) noexcept
Move Assignment Operator.
Definition allocator.h:466
constexpr allocation(const T *data, size_t n, align_t align, const alloc_t &alloc)
Buffer Copy Allocator Constructor, initializes the allocation with a block of size n * sizeof(T) byte...
Definition allocator.h:411
constexpr const value_t * data() const
Getter for the real pointer to the allocated block of memory.
Definition allocator.h:631
constexpr value_t * data()
Getter for the real pointer to the allocated block of memory.
Definition allocator.h:624
typename allocator_traits< AllocT >::template rebind< T > alloc_t
alias for the allocator type
Definition allocator.h:288
T value_t
alias for the data type
Definition allocator.h:291
constexpr allocation(const allocation &alloc) noexcept
Copy Constructor, creates an allocation of equal size and performs a byte-wise copy.
Definition allocator.h:419
constexpr size_t capacity() const
Getter for the number of elements n of type T that the allocation can hold.
Definition allocator.h:617
constexpr allocation(allocation &&alloc) noexcept
Move Constructor, moves the data in alloc to the new object and cleans alloc so that it can safely de...
Definition allocator.h:431
constexpr void allocate(size_t n, align_t align=zero< align_t >()) noexcept
Allocate a block of memory for the allocation. If there is already an allocated block of memory,...
Definition allocator.h:484
ptrdiff_t diff_t
diff type definition for ptr_traits
Definition allocator.h:297
constexpr allocation(const T *data, size_t n)
Buffer Copy Constructor, initializes the allocation with a block of size n * sizeof(T) bytes....
Definition allocator.h:321
constexpr allocation(const T *data, size_t n, const alloc_t &alloc)
Buffer Copy Allocator Constructor, initializes the allocation with a block of size n * sizeof(T) byte...
Definition allocator.h:382
constexpr allocation(size_t n, align_t align) noexcept
Sized Constructor, initializes the allocation with a block of size n * sizeof(T) bytes.
Definition allocator.h:330
size_t size_t
size type definition for ptr_traits
Definition allocator.h:294
constexpr ~allocation() noexcept
Default Destructor, releases the memory block if still present.
Definition allocator.h:442
constexpr allocation(size_t n) noexcept
Sized Constructor, initializes the allocation with a block of size n * sizeof(T) bytes.
Definition allocator.h:311
constexpr void reallocate(size_t n, align_t align=zero< align_t >()) noexcept
Reallocate the block with a new size. Contents are copied to the new allocation.
Definition allocator.h:522
constexpr allocation(size_t n, align_t align, const alloc_t &alloc) noexcept
Sized Allocator Constructor.
Definition allocator.h:394
constexpr size_t size() const
Getter for the byte size of the allocation.
Definition allocator.h:610
constexpr allocation(size_t n, const alloc_t &alloc) noexcept
Sized Allocator Constructor.
Definition allocator.h:366
constexpr void deallocate() noexcept
Release the block of memory.
Definition allocator.h:505
constexpr allocation() noexcept
Default Constructor, initializes internal data to null and the capacity to 0
Definition allocator.h:304
constexpr void creallocate(size_t n, align_t align=zero< align_t >()) noexcept
Reallocate the block with a new size. Contents are copied to the new allocation.
Definition allocator.h:541
constexpr allocation & operator=(const allocation &alloc)
Copy Assignment Operator.
Definition allocator.h:456
Helper structure for obtaining traits of an allocator class.
Definition allocator.h:61
detect_t< false_type, _is_always_equal, Alloc > is_always_equal
Checks if this allocator type is always equal to another allocator of similar type.
Definition allocator.h:131
detect_t< value_t *, _pointer, Alloc > pointer_t
Alias for a pointer to the value type. Will use Alloc::pointer_t if present.
Definition allocator.h:108
detect_t< const value_t *, _const_pointer, Alloc > const_pointer_t
Alias for a const pointer to the value type. Will use Alloc::const_pointer_t if present.
Definition allocator.h:111
detect_t< void *, _void_pointer, Alloc > void_pointer_t
Alias for a pointer to void. Will use Alloc::void_pointer_t if present.
Definition allocator.h:114
typename _diff< Alloc, pointer_t >::type diff_t
Alias for differences between pointers. Will use Alloc::diff_t if present.
Definition allocator.h:120
Alloc alloc_t
Alias for the allocator type.
Definition allocator.h:102
typename _size< Alloc, pointer_t >::type size_t
Alias for the size of allocations. Will use Alloc::size_t if present.
Definition allocator.h:123
typename Alloc::value_t value_t
Alias for the value type of the allocator.
Definition allocator.h:105
detect_t< const void *, _void_const_pointer, Alloc > const_void_pointer_t
Alias for a const pointer to void. Will use Alloc::const_void_pointer_t if present.
Definition allocator.h:117
typename _rebind< Alloc, TypeT >::type rebind
Rebinds the allocator type to produce an element type of type TypeT
Definition allocator.h:134
Get the corresponding unsigned integral type of TypeT.
Definition numeric_transforms.h:76
Class for retrieving the traits of Pointer-like types.
Definition pointer_traits.h:33
Take a Template with a Pack ClassT<ArgsT...> and replace the first ArgT of ArgsT.....
Definition type_sequences.h:101
bool bool_t
A conditional type.
Definition types.h:214
void void_t
Void type used for SFINAE.
Definition types.h:256
__PTRDIFF_TYPE__ ptrdiff_t
Signed Integer Type Returned by the Subtraction of two Pointers.
Definition types.h:251
constexpr void swap(T &x, T &y) noexcept
Swaps x and y.
Definition utility.h:114