DDC 0.1.0
Loading...
Searching...
No Matches
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
64#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
65// In C++20, `a!=b` shall be automatically translated by the compiler to `!(a==b)`
66template <class T, std::size_t NT, class U, std::size_t NU>
67constexpr bool operator!=(AlignedAllocator<T, NT> const&, AlignedAllocator<U, NU> const&) noexcept
68{
70}
71#endif
72
73} // namespace ddc
constexpr AlignedAllocator(AlignedAllocator< U, N > const &) noexcept
~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
constexpr AlignedAllocator & operator=(AlignedAllocator &&x) noexcept=default
constexpr AlignedAllocator & operator=(AlignedAllocator< U, N > const &) noexcept
T * allocate(std::size_t n) const
The top-level namespace of DDC.
constexpr bool operator==(AlignedAllocator< T, NT > const &, AlignedAllocator< U, NU > const &) noexcept