1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#ifndef FOONATHAN_MEMORY_CONTAINER_HPP_INCLUDED
#define FOONATHAN_MEMORY_CONTAINER_HPP_INCLUDED
/// \file
/// Aliasas for STL containers using a certain \concept{concept_rawallocator,RawAllocator}.
/// \note Only available on a hosted implementation.
#include "config.hpp"
#if !FOONATHAN_HOSTED_IMPLEMENTATION
#error "This header is only available for a hosted implementation."
#endif
#include <functional>
#include <utility>
#include <deque>
#include <forward_list>
#include <list>
#include <map>
#include <queue>
#include <scoped_allocator>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "std_allocator.hpp"
#include "threading.hpp"
namespace foonathan
{
namespace memory
{
/// \ingroup adapter
/// @{
/// Alias template for an STL container that uses a certain
/// \concept{concept_rawallocator,RawAllocator}. It is just a shorthand for a passing in the \c
/// RawAllocator wrapped in a \ref foonathan::memory::std_allocator.
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(vector, std::vector<T, std_allocator<T, RawAllocator>>);
/// Same as above but uses \c std::scoped_allocator_adaptor so the allocator is inherited by all
/// nested containers.
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
vector_scoped_alloc,
std::vector<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(deque, std::deque<T, std_allocator<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
deque_scoped_alloc,
std::deque<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(list, std::list<T, std_allocator<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
list_scoped_alloc,
std::list<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(forward_list,
std::forward_list<T, std_allocator<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
forward_list_scoped_alloc,
std::forward_list<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(set, std::set<T, std::less<T>, std_allocator<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
set_scoped_alloc,
std::set<T, std::less<T>,
std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(multiset,
std::multiset<T, std::less<T>, std_allocator<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
multiset_scoped_alloc,
std::multiset<T, std::less<T>,
std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
map, std::map<Key, Value, std::less<Key>,
std_allocator<std::pair<const Key, Value>, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
map_scoped_alloc,
std::map<Key, Value, std::less<Key>,
std::scoped_allocator_adaptor<
std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
/// \copydoc vector
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
multimap, std::multimap<Key, Value, std::less<Key>,
std_allocator<std::pair<const Key, Value>, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
multimap_scoped_alloc,
std::multimap<Key, Value, std::less<Key>,
std::scoped_allocator_adaptor<
std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
unordered_set,
std::unordered_set<T, std::hash<T>, std::equal_to<T>, std_allocator<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
unordered_set_scoped_alloc,
std::unordered_set<T, std::hash<T>, std::equal_to<T>,
std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(unordered_multiset,
std::unordered_multiset<T, std::hash<T>, std::equal_to<T>,
std_allocator<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
unordered_multiset_scoped_alloc,
std::unordered_multiset<T, std::hash<T>, std::equal_to<T>,
std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
/// \copydoc vector
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
unordered_map,
std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>,
std_allocator<std::pair<const Key, Value>, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
unordered_map_scoped_alloc,
std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>,
std::scoped_allocator_adaptor<
std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
/// \copydoc vector
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
unordered_multimap,
std::unordered_multimap<Key, Value, std::hash<Key>, std::equal_to<Key>,
std_allocator<std::pair<const Key, Value>, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename Key, typename Value, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
unordered_multimap_scoped_alloc,
std::unordered_multimap<Key, Value, std::hash<Key>, std::equal_to<Key>,
std::scoped_allocator_adaptor<
std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(stack, std::stack<T, deque<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(stack_scoped_alloc,
std::stack<T, deque_scoped_alloc<T, RawAllocator>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(queue, std::queue<T, deque<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(queue_scoped_alloc,
std::queue<T, deque_scoped_alloc<T, RawAllocator>>);
/// \copydoc vector
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(priority_queue, std::priority_queue<T, deque<T, RawAllocator>>);
/// \copydoc vector_scoped_alloc
template <typename T, class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(priority_queue_scoped_alloc,
std::priority_queue<T, deque_scoped_alloc<T, RawAllocator>>);
/// \copydoc vector
template <class RawAllocator>
FOONATHAN_ALIAS_TEMPLATE(
string,
std::basic_string<char, std::char_traits<char>, std_allocator<char, RawAllocator>>);
/// @}
/// @{
/// Convenience function to create a container adapter using a certain
/// \concept{concept_rawallocator,RawAllocator}. \returns An empty adapter with an
/// implementation container using a reference to a given allocator. \ingroup adapter
template <typename T, class RawAllocator, class Container = deque<T, RawAllocator>>
std::stack<T, Container> make_stack(RawAllocator& allocator)
{
return std::stack<T, Container>{Container(allocator)};
}
/// \copydoc make_stack
template <typename T, class RawAllocator, class Container = deque<T, RawAllocator>>
std::queue<T, Container> make_queue(RawAllocator& allocator)
{
return std::queue<T, Container>{Container(allocator)};
}
/// \copydoc make_stack
template <typename T, class RawAllocator, class Container = deque<T, RawAllocator>,
class Compare = std::less<T>>
std::priority_queue<T, Container, Compare> make_priority_queue(RawAllocator& allocator,
Compare comp = {})
{
return std::priority_queue<T, Container, Compare>{detail::move(comp),
Container(allocator)};
}
/// @}
#if !defined(DOXYGEN)
#include "detail/container_node_sizes.hpp"
#if !defined(FOONATHAN_MEMORY_NO_NODE_SIZE)
/// \exclude
namespace detail
{
template <typename T, class StdAllocator>
struct shared_ptr_node_size
{
static_assert(sizeof(T) != sizeof(T), "unsupported allocator type");
};
template <typename T, class RawAllocator>
struct shared_ptr_node_size<T, std_allocator<T, RawAllocator>>
: std::conditional<allocator_traits<RawAllocator>::is_stateful::value,
memory::shared_ptr_stateful_node_size<T>,
memory::shared_ptr_stateless_node_size<T>>::type
{
static_assert(sizeof(std_allocator<T, RawAllocator>) <= sizeof(void*),
"fix node size debugger");
};
} // namespace detail
template <typename T, class StdAllocator>
struct shared_ptr_node_size : detail::shared_ptr_node_size<T, StdAllocator>
{
};
#endif
#else
/// \ingroup adapter
/// @{
/// Contains the node size of a node based STL container with a specific type.
///
/// This trait is auto-generated and may not be available depending on the build configuration,
/// especially when doing cross compilation.
template <typename T>
struct forward_list_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc forward_list_node_size
template <typename T>
struct list_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc forward_list_node_size
template <typename T>
struct set_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc forward_list_node_size
template <typename T>
struct multiset_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc forward_list_node_size
template <typename T>
struct unordered_set_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc forward_list_node_size
template <typename T>
struct unordered_multiset_node_size
: std::integral_constant<std::size_t, implementation_defined>
{
};
/// Contains the node size of a node based STL container with a specific type.
///
/// This trait is auto-generated and may not be available depending on the build configuration,
/// especially when doing cross compilation.
///
/// \notes `T` is always the `value_type` of the container, e.g. `std::pair<const Key, Value>`.
template <typename T>
struct map_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc map_node_size
template <typename T>
struct multimap_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc map_node_size
template <typename T>
struct unordered_map_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc map_node_size
template <typename T>
struct unordered_multimap_node_size
: std::integral_constant<std::size_t, implementation_defined>
{
};
/// \copydoc forward_list_node_size
template <typename T, class StdAllocator>
struct shared_ptr_node_size : std::integral_constant<std::size_t, implementation_defined>
{
};
/// @}
#endif
#if !defined(FOONATHAN_MEMORY_NO_NODE_SIZE)
/// The node size required by \ref allocate_shared.
/// \note This is similar to \ref shared_ptr_node_size but takes a
/// \concept{concept_rawallocator,RawAllocator} instead.
template <typename T, class RawAllocator>
struct allocate_shared_node_size : shared_ptr_node_size<T, std_allocator<T, RawAllocator>>
{
};
#endif
} // namespace memory
} // namespace foonathan
#endif // FOONATHAN_MEMORY_CONTAINER_HPP_INCLUDED
|