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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#include <foonathan/memory/allocator_traits.hpp>
#include <doctest/doctest.h>
#include <type_traits>
#include <foonathan/memory/heap_allocator.hpp>
using namespace foonathan::memory;
static_assert(is_raw_allocator<std::allocator<char>>::value, "");
static_assert(allocator_is_raw_allocator<std::allocator<char>>::value, "");
static_assert(is_raw_allocator<std::allocator<int>>::value, "");
static_assert(allocator_is_raw_allocator<std::allocator<int>>::value, "");
static_assert(is_raw_allocator<heap_allocator>::value, "");
static_assert(!is_raw_allocator<int>::value, "");
template <typename T>
struct std_allocator_construct
{
using value_type = T;
using pointer = T;
void construct(pointer, T) {}
};
static_assert(!allocator_is_raw_allocator<std_allocator_construct<int>>::value, "");
static_assert(!is_raw_allocator<std_allocator_construct<int>>::value, "");
struct raw_allocator_specialized
{
};
namespace foonathan
{
namespace memory
{
template <>
struct allocator_traits<raw_allocator_specialized>
{
};
} // namespace memory
} // namespace foonathan
static_assert(is_raw_allocator<raw_allocator_specialized>::value, "");
template <class Allocator, class Type, bool Stateful>
void test_type_statefulness()
{
using type = typename allocator_traits<Allocator>::allocator_type;
static_assert(std::is_same<type, Type>::value, "allocator_type failed");
using stateful = typename allocator_traits<Allocator>::is_stateful;
static_assert(stateful::value == Stateful, "is_stateful failed");
}
template <typename T>
struct standard_alloc
{
using value_type = T;
};
template <typename T, typename... Dummy>
struct standard_multi_arg
{
using value_type = T;
};
template <typename T, typename Dummy>
struct standard_with_rebind
{
using value_type = T;
template <typename U>
struct rebind
{
using other = standard_with_rebind<U, int>;
};
};
void instantiate_test_type_statefulness()
{
struct empty_raw
{
};
test_type_statefulness<empty_raw, empty_raw, false>();
struct non_empty_raw
{
int i;
};
test_type_statefulness<non_empty_raw, non_empty_raw, true>();
struct explicit_stateful_raw
{
using is_stateful = std::true_type;
};
(void)explicit_stateful_raw::is_stateful();
test_type_statefulness<explicit_stateful_raw, explicit_stateful_raw, true>();
struct explicit_stateless_raw
{
using is_stateful = std::false_type;
int i;
};
(void)explicit_stateless_raw::is_stateful();
test_type_statefulness<explicit_stateless_raw, explicit_stateless_raw, false>();
test_type_statefulness<standard_alloc<char>, standard_alloc<char>, false>();
test_type_statefulness<standard_alloc<int>, standard_alloc<char>, false>();
test_type_statefulness<standard_multi_arg<char, int, int>, standard_multi_arg<char, int, int>,
false>();
test_type_statefulness<standard_multi_arg<int, int, int>, standard_multi_arg<char, int, int>,
false>();
test_type_statefulness<standard_with_rebind<char, char>, standard_with_rebind<char, int>,
false>();
test_type_statefulness<standard_with_rebind<int, char>, standard_with_rebind<char, int>,
false>();
}
template <class Allocator>
void test_node(Allocator& alloc)
{
auto ptr = allocator_traits<Allocator>::allocate_node(alloc, 1, 1);
allocator_traits<Allocator>::deallocate_node(alloc, ptr, 1, 1);
}
template <class Allocator>
void test_array(Allocator& alloc)
{
auto ptr = allocator_traits<Allocator>::allocate_array(alloc, 1, 1, 1);
allocator_traits<Allocator>::deallocate_array(alloc, ptr, 1, 1, 1);
}
template <class Allocator>
void test_max_getter(const Allocator& alloc, std::size_t alignment, std::size_t node,
std::size_t array)
{
auto i = allocator_traits<Allocator>::max_alignment(alloc);
REQUIRE(i == alignment);
i = allocator_traits<Allocator>::max_node_size(alloc);
REQUIRE(i == node);
i = allocator_traits<Allocator>::max_array_size(alloc);
REQUIRE(i == array);
}
TEST_CASE("allocator_traits")
{
struct min_raw_allocator
{
bool alloc_node = false, dealloc_node = false;
void* allocate_node(std::size_t, std::size_t)
{
alloc_node = true;
return nullptr;
}
void deallocate_node(void*, std::size_t, std::size_t) noexcept
{
dealloc_node = true;
}
};
static_assert(is_raw_allocator<min_raw_allocator>::value, "");
struct standard_allocator
{
using value_type = char;
bool alloc = false, dealloc = false;
char* allocate(std::size_t)
{
alloc = true;
return nullptr;
}
void deallocate(char*, std::size_t) noexcept
{
dealloc = true;
}
};
static_assert(is_raw_allocator<standard_allocator>::value, "");
SUBCASE("node")
{
// minimum interface works
min_raw_allocator min;
test_node(min);
REQUIRE(min.alloc_node);
REQUIRE(min.dealloc_node);
// standard interface works
standard_allocator std;
test_node(std);
REQUIRE(std.alloc);
REQUIRE(std.dealloc);
struct both_alloc : min_raw_allocator, standard_allocator
{
};
static_assert(is_raw_allocator<both_alloc>::value, "");
// raw is preferred over standard
both_alloc both;
test_node(both);
REQUIRE(both.alloc_node);
REQUIRE(both.dealloc_node);
REQUIRE(!both.alloc);
REQUIRE(!both.dealloc);
}
SUBCASE("array")
{
// minimum interface works
min_raw_allocator min;
test_array(min);
REQUIRE(min.alloc_node);
REQUIRE(min.dealloc_node);
// standard interface works
standard_allocator std;
test_array(std);
REQUIRE(std.alloc);
REQUIRE(std.dealloc);
struct array_raw
{
bool alloc_array = false, dealloc_array = false;
void* allocate_array(std::size_t, std::size_t, std::size_t)
{
alloc_array = true;
return nullptr;
}
void deallocate_array(void*, std::size_t, std::size_t, std::size_t) noexcept
{
dealloc_array = true;
}
};
// array works
array_raw array;
test_array(array);
REQUIRE(array.alloc_array);
REQUIRE(array.dealloc_array);
struct array_node : min_raw_allocator, array_raw
{
};
static_assert(is_raw_allocator<array_node>::value, "");
// array works over node
array_node array2;
test_array(array2);
REQUIRE(array2.alloc_array);
REQUIRE(array2.dealloc_array);
REQUIRE(!array2.alloc_node);
REQUIRE(!array2.dealloc_node);
struct array_std : standard_allocator, array_raw
{
};
// array works over standard
array_std array3;
test_array(array3);
REQUIRE(array3.alloc_array);
REQUIRE(array3.dealloc_array);
REQUIRE(!array3.alloc);
REQUIRE(!array3.dealloc);
struct array_node_std : standard_allocator, array_raw, min_raw_allocator
{
};
// array works over everything
array_node_std array4;
test_array(array4);
REQUIRE(array4.alloc_array);
REQUIRE(array4.dealloc_array);
REQUIRE(!array4.alloc_node);
REQUIRE(!array4.dealloc_node);
REQUIRE(!array4.alloc);
REQUIRE(!array4.dealloc);
}
SUBCASE("max getter")
{
min_raw_allocator min;
test_max_getter(min, detail::max_alignment, std::size_t(-1), std::size_t(-1));
struct with_alignment
{
std::size_t max_alignment() const
{
return detail::max_alignment * 2;
}
};
with_alignment alignment;
test_max_getter(alignment, detail::max_alignment * 2, std::size_t(-1), std::size_t(-1));
struct with_node
{
std::size_t max_node_size() const
{
return 1;
}
};
with_node node;
test_max_getter(node, detail::max_alignment, 1, 1);
struct with_array
{
std::size_t max_array_size() const
{
return 2;
}
};
with_array array;
test_max_getter(array, detail::max_alignment, std::size_t(-1), 2);
struct with_node_array : with_node, with_array
{
};
with_node_array node_array;
test_max_getter(node_array, detail::max_alignment, 1, 2);
struct with_everything : with_node_array, with_alignment
{
};
with_everything everything;
test_max_getter(everything, detail::max_alignment * 2, 1, 2);
}
}
template <class Allocator>
void test_try_node(Allocator& alloc)
{
auto ptr = composable_allocator_traits<Allocator>::try_allocate_node(alloc, 1, 1);
composable_allocator_traits<Allocator>::try_deallocate_node(alloc, ptr, 1, 1);
}
template <class Allocator>
void test_try_array(Allocator& alloc)
{
auto ptr = composable_allocator_traits<Allocator>::try_allocate_array(alloc, 1, 1, 1);
composable_allocator_traits<Allocator>::try_deallocate_array(alloc, ptr, 1, 1, 1);
}
TEST_CASE("composable_allocator_traits")
{
struct min_composable_allocator
{
bool alloc_node = false, dealloc_node = false;
void* allocate_node(std::size_t, std::size_t)
{
return nullptr;
}
void deallocate_node(void*, std::size_t, std::size_t) noexcept {}
void* try_allocate_node(std::size_t, std::size_t)
{
alloc_node = true;
return nullptr;
}
bool try_deallocate_node(void*, std::size_t, std::size_t) noexcept
{
dealloc_node = true;
return true;
}
};
static_assert(is_composable_allocator<min_composable_allocator>::value, "");
SUBCASE("node")
{
min_composable_allocator alloc;
test_try_node(alloc);
REQUIRE(alloc.alloc_node);
REQUIRE(alloc.dealloc_node);
}
SUBCASE("array")
{
min_composable_allocator min;
test_try_array(min);
REQUIRE(min.alloc_node);
REQUIRE(min.dealloc_node);
struct array_composable : min_composable_allocator
{
bool alloc_array = false, dealloc_array = false;
void* try_allocate_array(std::size_t, std::size_t, std::size_t)
{
alloc_array = true;
return nullptr;
}
bool try_deallocate_array(void*, std::size_t, std::size_t, std::size_t) noexcept
{
dealloc_array = true;
return true;
}
} array;
test_try_array(array);
REQUIRE(array.alloc_array);
REQUIRE(array.dealloc_array);
REQUIRE(!array.alloc_node);
REQUIRE(!array.dealloc_node);
}
}
|