WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
fallback_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_FALLBACK_ALLOCATOR_HPP_INCLUDED
5#define WPI_MEMORY_FALLBACK_ALLOCATOR_HPP_INCLUDED
6
7/// \file
8//// Class template \ref wpi::memory::fallback_allocator.
9
11#include "detail/utility.hpp"
12#include "allocator_traits.hpp"
13#include "config.hpp"
14
15namespace wpi
16{
17 namespace memory
18 {
19 /// A RawAllocator with a fallback.
20 /// Allocation first tries `Default`, if it fails,
21 /// it uses `Fallback`.
22 /// \requires `Default` must be a composable RawAllocator,
23 /// `Fallback` must be a RawAllocator.
24 /// \ingroup memory_adapter
25 template <class Default, class Fallback>
27 : WPI_EBO(detail::ebo_storage<0, typename allocator_traits<Default>::allocator_type>),
28 WPI_EBO(detail::ebo_storage<1, typename allocator_traits<Fallback>::allocator_type>)
29 {
36
37 public:
40
42 std::integral_constant<bool, default_traits::is_stateful::value
43 || fallback_traits::is_stateful::value>;
44
45 /// \effects Default constructs both allocators.
46 /// \notes This function only participates in overload resolution, if both allocators are not stateful.
47 WPI_ENABLE_IF(!is_stateful::value)
53
54 /// \effects Constructs the allocator by passing in the two allocators it has.
56 fallback_allocator_type&& fallback_alloc = {})
57 : detail::ebo_storage<0, default_allocator_type>(detail::move(default_alloc)),
58 detail::ebo_storage<1, fallback_allocator_type>(detail::move(fallback_alloc))
59 {
60 }
61
62 /// @{
63 /// \effects First calls the compositioning (de)allocation function on the `default_allocator_type`.
64 /// If that fails, uses the non-compositioning function of the `fallback_allocator_type`.
65 void* allocate_node(std::size_t size, std::size_t alignment)
66 {
68 size, alignment);
69 if (!ptr)
71 return ptr;
72 }
73
74 void* allocate_array(std::size_t count, std::size_t size, std::size_t alignment)
75 {
77 count, size, alignment);
78 if (!ptr)
80 alignment);
81 return ptr;
82 }
83
84 void deallocate_node(void* ptr, std::size_t size, std::size_t alignment) noexcept
85 {
87 ptr, size, alignment);
88 if (!res)
90 alignment);
91 }
92
93 void deallocate_array(void* ptr, std::size_t count, std::size_t size,
94 std::size_t alignment) noexcept
95 {
96 auto res =
98 count, size, alignment);
99 if (!res)
101 alignment);
102 }
103 /// @}
104
105 /// @{
106 /// \effects First calls the compositioning (de)allocation function on the `default_allocator_type`.
107 /// If that fails, uses the compositioning function of the `fallback_allocator_type`.
108 /// \requires The `fallback_allocator_type` msut be composable.
109 WPI_ENABLE_IF(fallback_composable::value)
110 void* try_allocate_node(std::size_t size, std::size_t alignment) noexcept
111 {
113 size, alignment);
114 if (!ptr)
116 size, alignment);
117 return ptr;
118 }
119
120 WPI_ENABLE_IF(fallback_composable::value)
121 void* allocate_array(std::size_t count, std::size_t size,
122 std::size_t alignment) noexcept
123 {
125 count, size, alignment);
126 if (!ptr)
128 count, size, alignment);
129 return ptr;
130 }
131
132 WPI_ENABLE_IF(fallback_composable::value)
133 bool try_deallocate_node(void* ptr, std::size_t size, std::size_t alignment) noexcept
134 {
136 ptr, size, alignment);
137 if (!res)
139 ptr, size, alignment);
140 return res;
141 }
142
143 WPI_ENABLE_IF(fallback_composable::value)
144 bool try_deallocate_array(void* ptr, std::size_t count, std::size_t size,
145 std::size_t alignment) noexcept
146 {
147 auto res =
149 count, size, alignment);
150 if (!res)
152 ptr, count, size,
153 alignment);
154 return res;
155 }
156 /// @}
157
158 /// @{
159 /// \returns The maximum of the two values from both allocators.
160 std::size_t max_node_size() const
161 {
164 return fallback > def ? fallback : def;
165 }
166
167 std::size_t max_array_size() const
168 {
171 return fallback > def ? fallback : def;
172 }
173
174 std::size_t max_alignment() const
175 {
178 return fallback > def ? fallback : def;
179 }
180 /// @}
181
182 /// @{
183 /// \returns A (`const`) reference to the default allocator.
188
193 /// @}
194
195 /// @{
196 /// \returns A (`const`) reference to the fallback allocator.
201
206 /// @}
207 };
208 } // namespace memory
209} // namespace wpi
210
211#endif // WPI_MEMORY_FALLBACK_ALLOCATOR_HPP_INCLUDED
The default specialization of the wpi::memory::allocator_traits.
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
Definition ebo_storage.hpp:18
T & get() noexcept
Definition ebo_storage.hpp:27
A RawAllocator with a fallback.
Definition fallback_allocator.hpp:29
default_allocator_type & get_default_allocator() noexcept
Definition fallback_allocator.hpp:184
const fallback_allocator_type & get_fallback_allocator() const noexcept
Definition fallback_allocator.hpp:202
std::size_t max_node_size() const
Definition fallback_allocator.hpp:160
void * try_allocate_node(std::size_t size, std::size_t alignment) noexcept
Definition fallback_allocator.hpp:110
fallback_allocator_type & get_fallback_allocator() noexcept
Definition fallback_allocator.hpp:197
typename allocator_traits< Default >::allocator_type default_allocator_type
Definition fallback_allocator.hpp:38
std::integral_constant< bool, default_traits::is_stateful::value||fallback_traits::is_stateful::value > is_stateful
Definition fallback_allocator.hpp:41
typename allocator_traits< Fallback >::allocator_type fallback_allocator_type
Definition fallback_allocator.hpp:39
void * allocate_array(std::size_t count, std::size_t size, std::size_t alignment)
Definition fallback_allocator.hpp:74
void deallocate_node(void *ptr, std::size_t size, std::size_t alignment) noexcept
Definition fallback_allocator.hpp:84
bool try_deallocate_array(void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition fallback_allocator.hpp:144
void * allocate_node(std::size_t size, std::size_t alignment)
Definition fallback_allocator.hpp:65
bool try_deallocate_node(void *ptr, std::size_t size, std::size_t alignment) noexcept
Definition fallback_allocator.hpp:133
void deallocate_array(void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition fallback_allocator.hpp:93
std::size_t max_array_size() const
Definition fallback_allocator.hpp:167
const default_allocator_type & get_default_allocator() const noexcept
Definition fallback_allocator.hpp:189
std::size_t max_alignment() const
Definition fallback_allocator.hpp:174
fallback_allocator(default_allocator_type &&default_alloc, fallback_allocator_type &&fallback_alloc={})
Definition fallback_allocator.hpp:55
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
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