DDC 0.0.0

a discrete domain computation library

aligned_allocator.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <cstddef>
8#include <new>
9#include <type_traits>
10
11namespace ddc {
12
13template <class T, std::size_t N>
15{
16public:
17 using value_type = T;
18
19 template <class U>
20 struct rebind
21 {
22 using other = AlignedAllocator<U, N>;
23 };
24
25 constexpr AlignedAllocator() = default;
26
27 constexpr AlignedAllocator(AlignedAllocator const& x) = default;
28
29 constexpr AlignedAllocator(AlignedAllocator&& x) noexcept = default;
30
31 template <class U>
32 constexpr explicit AlignedAllocator(AlignedAllocator<U, N> const&) noexcept
33 {
34 }
35
36 ~AlignedAllocator() = default;
37
38 constexpr AlignedAllocator& operator=(AlignedAllocator const& x) = default;
39
40 constexpr AlignedAllocator& operator=(AlignedAllocator&& x) noexcept = default;
41
42 template <class U>
43 constexpr AlignedAllocator& operator=(AlignedAllocator<U, N> const&) noexcept
44 {
45 }
46
47 [[nodiscard]] T* allocate(std::size_t n) const
48 {
49 return new (std::align_val_t(N)) value_type[n];
50 }
51
52 void deallocate(T* p, std::size_t) const
53 {
54 operator delete[](p, std::align_val_t(N));
55 }
56};
57
58template <class T, std::size_t NT, class U, std::size_t NU>
59constexpr bool operator==(AlignedAllocator<T, NT> const&, AlignedAllocator<U, NU> const&) noexcept
60{
61 return std::is_same_v<AlignedAllocator<T, NT>, AlignedAllocator<U, NU>>;
62}
63
64template <class T, std::size_t NT, class U, std::size_t NU>
65constexpr bool operator!=(AlignedAllocator<T, NT> const&, AlignedAllocator<U, NU> const&) noexcept
66{
67 return !std::is_same_v<AlignedAllocator<T, NT>, AlignedAllocator<U, NU>>;
68}
69
70} // namespace ddc
Definition: aligned_allocator.hpp:15
constexpr AlignedAllocator(AlignedAllocator< U, N > const &) noexcept
Definition: aligned_allocator.hpp:32
~AlignedAllocator()=default
constexpr AlignedAllocator(AlignedAllocator const &x)=default
constexpr AlignedAllocator & operator=(AlignedAllocator const &x)=default
constexpr AlignedAllocator(AlignedAllocator &&x) noexcept=default
constexpr AlignedAllocator()=default
void deallocate(T *p, std::size_t) const
Definition: aligned_allocator.hpp:52
constexpr AlignedAllocator & operator=(AlignedAllocator &&x) noexcept=default
constexpr AlignedAllocator & operator=(AlignedAllocator< U, N > const &) noexcept
Definition: aligned_allocator.hpp:43
T * allocate(std::size_t n) const
Definition: aligned_allocator.hpp:47
Definition: aligned_allocator.hpp:21
Definition: aligned_allocator.hpp:11
constexpr bool operator!=(AlignedAllocator< T, NT > const &, AlignedAllocator< U, NU > const &) noexcept
Definition: aligned_allocator.hpp:65
constexpr bool operator==(AlignedAllocator< T, NT > const &, AlignedAllocator< U, NU > const &) noexcept
Definition: aligned_allocator.hpp:59