WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
aligned_allocator.hpp
Go to the documentation of this file.
1// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors
2// SPDX-License-Identifier: Zlib
3
4#ifndef WPI_MEMORY_ALIGNED_ALLOCATOR_HPP_INCLUDED
5#define WPI_MEMORY_ALIGNED_ALLOCATOR_HPP_INCLUDED
6
7/// \file
8/// Class \ref wpi::memory::aligned_allocator and related functions.
9
10#include <type_traits>
11
12#include "detail/assert.hpp"
13#include "detail/utility.hpp"
14#include "allocator_traits.hpp"
15#include "config.hpp"
16
17namespace wpi
18{
19 namespace memory
20 {
21 /// A RawAllocator adapter that ensures a minimum alignment.
22 /// It adjusts the alignment value so that it is always larger than the minimum and forwards to the specified allocator.
23 /// \ingroup memory_adapter
24 template <class RawAllocator>
25 class aligned_allocator : WPI_EBO(allocator_traits<RawAllocator>::allocator_type)
26 {
30
31 public:
33 using is_stateful = std::true_type;
34
35 /// \effects Creates it passing it the minimum alignment value and the allocator object.
36 /// \requires \c min_alignment must be less than \c this->max_alignment().
37 explicit aligned_allocator(std::size_t min_alignment, allocator_type&& alloc = {})
38 : allocator_type(detail::move(alloc)), min_alignment_(min_alignment)
39 {
40 WPI_MEMORY_ASSERT(min_alignment_ <= max_alignment());
41 }
42
43 /// @{
44 /// \effects Moves the \c aligned_allocator object.
45 /// It simply moves the underlying allocator.
47 : allocator_type(detail::move(other)), min_alignment_(other.min_alignment_)
48 {
49 }
50
52 {
53 allocator_type::operator=(detail::move(other));
54 min_alignment_ = other.min_alignment_;
55 return *this;
56 }
57 /// @}
58
59 /// @{
60 /// \effects Forwards to the underlying allocator through the \ref allocator_traits.
61 /// If the \c alignment is less than the \c min_alignment(), it is set to the minimum alignment.
62 void* allocate_node(std::size_t size, std::size_t alignment)
63 {
64 if (min_alignment_ > alignment)
65 alignment = min_alignment_;
66 return traits::allocate_node(get_allocator(), size, alignment);
67 }
68
69 void* allocate_array(std::size_t count, std::size_t size, std::size_t alignment)
70 {
71 if (min_alignment_ > alignment)
72 alignment = min_alignment_;
73 return traits::allocate_array(get_allocator(), count, size, alignment);
74 }
75
76 void deallocate_node(void* ptr, std::size_t size, std::size_t alignment) noexcept
77 {
78 if (min_alignment_ > alignment)
79 alignment = min_alignment_;
80 traits::deallocate_node(get_allocator(), ptr, size, alignment);
81 }
82
83 void deallocate_array(void* ptr, std::size_t count, std::size_t size,
84 std::size_t alignment) noexcept
85 {
86 if (min_alignment_ > alignment)
87 alignment = min_alignment_;
88 traits::deallocate_array(get_allocator(), ptr, count, size, alignment);
89 }
90 /// @}
91
92 /// @{
93 /// \effects Forwards to the underlying allocator through the \ref composable_allocator_traits.
94 /// If the \c alignment is less than the \c min_alignment(), it is set to the minimum alignment.
95 /// \requires The underyling allocator must be composable.
96 WPI_ENABLE_IF(composable::value)
97 void* try_allocate_node(std::size_t size, std::size_t alignment) noexcept
98 {
99 if (min_alignment_ > alignment)
100 alignment = min_alignment_;
101 return composable_traits::try_allocate_node(get_allocator(), size, alignment);
102 }
103
104 WPI_ENABLE_IF(composable::value)
105 void* try_allocate_array(std::size_t count, std::size_t size,
106 std::size_t alignment) noexcept
107 {
108 if (min_alignment_ > alignment)
109 alignment = min_alignment_;
111 alignment);
112 }
113
114 WPI_ENABLE_IF(composable::value)
115 bool try_deallocate_node(void* ptr, std::size_t size, std::size_t alignment) noexcept
116 {
117 if (min_alignment_ > alignment)
118 alignment = min_alignment_;
120 alignment);
121 }
122
123 WPI_ENABLE_IF(composable::value)
124 bool try_deallocate_array(void* ptr, std::size_t count, std::size_t size,
125 std::size_t alignment) noexcept
126 {
127 if (min_alignment_ > alignment)
128 alignment = min_alignment_;
130 alignment);
131 }
132 /// @}
133
134 /// @{
135 /// \returns The value returned by the \ref allocator_traits for the underlying allocator.
136 std::size_t max_node_size() const
137 {
139 }
140
141 std::size_t max_array_size() const
142 {
144 }
145
146 std::size_t max_alignment() const
147 {
149 }
150 /// @}
151
152 /// @{
153 /// \returns A reference to the underlying allocator.
155 {
156 return *this;
157 }
158
159 const allocator_type& get_allocator() const noexcept
160 {
161 return *this;
162 }
163 /// @}
164
165 /// \returns The minimum alignment.
166 std::size_t min_alignment() const noexcept
167 {
168 return min_alignment_;
169 }
170
171 /// \effects Sets the minimum alignment to a new value.
172 /// \requires \c min_alignment must be less than \c this->max_alignment().
174 {
176 min_alignment_ = min_alignment;
177 }
178
179 private:
180 std::size_t min_alignment_;
181 };
182
183 /// \returns A new \ref aligned_allocator created by forwarding the parameters to the constructor.
184 /// \relates aligned_allocator
185 template <class RawAllocator>
186 auto make_aligned_allocator(std::size_t min_alignment, RawAllocator&& allocator) noexcept
188 {
189 return aligned_allocator<
190 typename std::decay<RawAllocator>::type>{min_alignment,
192 }
193 } // namespace memory
194} // namespace wpi
195
196#endif // WPI_MEMORY_ALIGNED_ALLOCATOR_HPP_INCLUDED
The default specialization of the wpi::memory::allocator_traits.
A RawAllocator adapter that ensures a minimum alignment.
Definition aligned_allocator.hpp:26
void set_min_alignment(std::size_t min_alignment)
Definition aligned_allocator.hpp:173
std::size_t max_node_size() const
Definition aligned_allocator.hpp:136
void deallocate_node(void *ptr, std::size_t size, std::size_t alignment) noexcept
Definition aligned_allocator.hpp:76
auto make_aligned_allocator(std::size_t min_alignment, RawAllocator &&allocator) noexcept -> aligned_allocator< typename std::decay< RawAllocator >::type >
Definition aligned_allocator.hpp:186
aligned_allocator & operator=(aligned_allocator &&other) noexcept
Definition aligned_allocator.hpp:51
bool try_deallocate_node(void *ptr, std::size_t size, std::size_t alignment) noexcept
Definition aligned_allocator.hpp:115
void * allocate_node(std::size_t size, std::size_t alignment)
Definition aligned_allocator.hpp:62
aligned_allocator(aligned_allocator &&other) noexcept
Definition aligned_allocator.hpp:46
typename allocator_traits< RawAllocator >::allocator_type allocator_type
Definition aligned_allocator.hpp:32
std::size_t min_alignment() const noexcept
Definition aligned_allocator.hpp:166
std::true_type is_stateful
Definition aligned_allocator.hpp:33
void * allocate_array(std::size_t count, std::size_t size, std::size_t alignment)
Definition aligned_allocator.hpp:69
allocator_type & get_allocator() noexcept
Definition aligned_allocator.hpp:154
void * try_allocate_node(std::size_t size, std::size_t alignment) noexcept
Definition aligned_allocator.hpp:97
void deallocate_array(void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition aligned_allocator.hpp:83
void * try_allocate_array(std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition aligned_allocator.hpp:105
const allocator_type & get_allocator() const noexcept
Definition aligned_allocator.hpp:159
std::size_t max_alignment() const
Definition aligned_allocator.hpp:146
aligned_allocator(std::size_t min_alignment, allocator_type &&alloc={})
Definition aligned_allocator.hpp:37
bool try_deallocate_array(void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition aligned_allocator.hpp:124
std::size_t max_array_size() const
Definition aligned_allocator.hpp:141
The default specialization of the allocator_traits for a RawAllocator.
Definition allocator_traits.hpp:292
static void deallocate_array(allocator_type &state, void *array, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:328
traits_detail::allocator_type< Allocator > allocator_type
Definition allocator_traits.hpp:294
static void * allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment)
Definition allocator_traits.hpp:308
static std::size_t max_array_size(const allocator_type &state)
Definition allocator_traits.hpp:346
static std::size_t max_node_size(const allocator_type &state)
Definition allocator_traits.hpp:338
static std::size_t max_alignment(const allocator_type &state)
Definition allocator_traits.hpp:354
static void * allocate_node(allocator_type &state, std::size_t size, std::size_t alignment)
Definition allocator_traits.hpp:298
static void deallocate_node(allocator_type &state, void *node, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:318
The default specialization of the composable_allocator_traits for a ComposableAllocator.
Definition allocator_traits.hpp:500
static void * try_allocate_node(allocator_type &state, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:504
static void * try_allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:513
static bool try_deallocate_node(allocator_type &state, void *node, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:522
static bool try_deallocate_array(allocator_type &state, void *array, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:531
Configuration macros.
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3821
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
T && forward(typename std::remove_reference< T >::type &t) noexcept
Definition utility.hpp:31
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Traits that check whether a type models concept ComposableAllocator.
Definition allocator_traits.hpp:596
#define WPI_ENABLE_IF(Expr)
Definition utility.hpp:78
#define WPI_MEMORY_ASSERT(Expr)
Definition assert.hpp:46