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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
/*
Copyright (c) 2005-2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TBB_test_common_custom_allocators_H
#define __TBB_test_common_custom_allocators_H
#include "test.h"
#include <oneapi/tbb/detail/_allocator_traits.h>
#include <memory>
#include <atomic>
#include <scoped_allocator>
template <typename CounterType>
struct ArenaData {
char* const my_buffer;
const std::size_t my_size;
CounterType my_allocated; // in bytes
template <typename T>
ArenaData( T* buf, std::size_t sz ) noexcept
: my_buffer(reinterpret_cast<char*>(buf)),
my_size(sz * sizeof(T))
{
my_allocated = 0;
}
ArenaData& operator=( const ArenaData& ) = delete;
}; // struct ArenaData
template <typename T, typename POCMA = std::false_type, typename CounterType = std::size_t>
struct ArenaAllocator {
using arena_data_type = ArenaData<CounterType>;
arena_data_type* my_data;
using value_type = T;
using propagate_on_container_move_assignment = POCMA;
template <typename U>
struct rebind {
using other = ArenaAllocator<U, POCMA, CounterType>;
};
ArenaAllocator() = default;
ArenaAllocator( arena_data_type& data ) noexcept : my_data(&data) {}
template <typename U, typename POCMA2>
ArenaAllocator( const ArenaAllocator<U, POCMA2, CounterType>& other ) noexcept
: my_data(other.my_data) {}
friend void swap( ArenaAllocator& lhs, ArenaAllocator& rhs ) {
using std::swap;
swap(lhs.my_data, rhs.my_data);
}
value_type* address( value_type& x ) const { return &x; }
const value_type* address( const value_type& x ) const { return &x; }
value_type* allocate( std::size_t n ) {
std::size_t new_size = (my_data->my_allocated += n * sizeof(T));
REQUIRE_MESSAGE(my_data->my_allocated <= my_data->my_size, "Trying to allocate more than was reserved");
char* result = &(my_data->my_buffer[new_size - n * sizeof(T)]);
return reinterpret_cast<value_type*>(result);
}
void deallocate( value_type* ptr, std::size_t n ) {
char* p = reinterpret_cast<char*>(ptr);
REQUIRE_MESSAGE((p >= my_data->my_buffer && p <= my_data->my_buffer + my_data->my_size),
"Trying to deallocate pointer not from arena");
REQUIRE_MESSAGE((p + n * sizeof(T) <= my_data->my_buffer + my_data->my_size),
"Trying to deallocate pointer not from arena");
// utils::suppress_unused_warning(p, n);
}
std::size_t max_size() const noexcept {
return my_data->my_size / sizeof(T);
}
}; // class ArenaAllocator
template <typename T, typename U, typename POCMA, typename C>
bool operator==( const ArenaAllocator<T, POCMA, C>& lhs, const ArenaAllocator<U, POCMA, C>& rhs ) {
return lhs.my_data == rhs.my_data;
}
template <typename T, typename U, typename POCMA, typename C>
bool operator!=( const ArenaAllocator<T, POCMA, C>& lhs, const ArenaAllocator<U, POCMA, C>& rhs ) {
return !(lhs == rhs);
}
template <typename BaseAllocatorType>
class LocalCountingAllocator : public BaseAllocatorType {
using base_type = BaseAllocatorType;
using base_traits = tbb::detail::allocator_traits<base_type>;
using counter_type = std::atomic<std::size_t>;
public:
using value_type = typename base_type::value_type;
std::size_t max_items;
counter_type items_allocated;
counter_type items_freed;
counter_type items_constructed;
counter_type items_destroyed;
counter_type allocations;
counter_type frees;
void set_counters( std::size_t it_allocated, std::size_t it_freed,
std::size_t it_constructed, std::size_t it_destroyed,
std::size_t allocs, std::size_t fres ) {
items_allocated = it_allocated; // TODO: may be store
items_freed = it_freed;
items_constructed = it_constructed;
items_destroyed = it_destroyed;
allocations = allocs;
frees = fres;
}
template <typename Allocator>
void set_counters( const Allocator& alloc ) {
set_counters(alloc.items_allocated, alloc.items_freed, alloc.items_constructed,
alloc.items_destroyed, alloc.allocations, alloc.frees);
}
void clear_counters() {
set_counters(0, 0, 0, 0, 0, 0);
}
template <typename U>
struct rebind {
using other = LocalCountingAllocator<typename base_traits::template rebind_alloc<U>>;
};
LocalCountingAllocator() : max_items{0} { clear_counters(); }
LocalCountingAllocator( const LocalCountingAllocator& other )
: base_type(other), max_items{other.max_items} { set_counters(other); }
template <typename U>
LocalCountingAllocator( const LocalCountingAllocator<U>& other )
: base_type(other), max_items{other.max_items} { set_counters(other); }
LocalCountingAllocator& operator=( const LocalCountingAllocator& other ) {
base_type::operator=(other);
max_items = other.max_items;
set_counters(other);
return *this;
}
value_type* allocate( std::size_t n ) {
if (max_items != 0 && items_allocated + n >= max_items) {
TBB_TEST_THROW(std::bad_alloc());
}
value_type* ptr = static_cast<base_type*>(this)->allocate(n);
++allocations;
items_allocated += n;
return ptr;
}
void deallocate( value_type* ptr, std::size_t n ) {
++frees;
items_freed += n;
static_cast<base_type*>(this)->deallocate(ptr, n);
}
template <typename U, typename... Args>
void construct( U* ptr, Args&&... args ) {
base_traits::construct(*this, ptr, std::forward<Args>(args)...);
++items_constructed;
}
template <typename U>
void destroy( U* ptr ) {
base_traits::destroy(*this, ptr);
++items_destroyed;
}
void set_limits( std::size_t max ) {
max_items = max;
}
}; // class LocalCountingAllocator
struct AllocatorCounters {
using counter_type = std::atomic<std::size_t>;
counter_type items_allocated;
counter_type items_freed;
counter_type items_constructed;
counter_type items_destroyed;
counter_type allocations;
counter_type frees;
AllocatorCounters() = default;
AllocatorCounters( std::size_t it_allocated, std::size_t it_freed, std::size_t it_constructed,
std::size_t it_destroyed, std::size_t allocs, std::size_t fres )
: items_allocated(it_allocated), items_freed(it_freed),
items_constructed(it_constructed), items_destroyed(it_destroyed),
allocations(allocs), frees(fres) {}
AllocatorCounters( const AllocatorCounters& other )
: items_allocated(other.items_allocated.load()),
items_freed(other.items_allocated.load()),
items_constructed(other.items_constructed.load()),
items_destroyed(other.items_destroyed.load()),
allocations(other.allocations.load()),
frees(other.allocations.load()) {}
AllocatorCounters& operator=( const AllocatorCounters& other ) {
items_allocated.store(other.items_allocated.load());
items_freed.store(other.items_freed.load());
items_constructed.store(other.items_constructed.load());
items_destroyed.store(other.items_destroyed.load());
allocations.store(other.allocations.load());
frees.store(other.frees.load());
return *this;
}
friend bool operator==( const AllocatorCounters& lhs, const AllocatorCounters& rhs ) {
return lhs.items_allocated == rhs.items_allocated &&
lhs.items_freed == rhs.items_freed &&
lhs.items_constructed == rhs.items_constructed &&
lhs.items_destroyed == rhs.items_destroyed &&
lhs.allocations == rhs.allocations &&
lhs.frees == rhs.frees;
}
}; // struct AllocatorCounters
template <typename BaseAllocatorType>
class StaticCountingAllocator : public BaseAllocatorType {
using base_type = BaseAllocatorType;
using base_traits = tbb::detail::allocator_traits<BaseAllocatorType>;
using counter_type = std::atomic<std::size_t>;
public:
using value_type = typename base_type::value_type;
using pointer = value_type*;
using counters_type = AllocatorCounters;
static std::size_t max_items;
static counter_type items_allocated;
static counter_type items_freed;
static counter_type items_constructed;
static counter_type items_destroyed;
static counter_type allocations;
static counter_type frees;
static bool throwing;
template <typename U>
struct rebind {
using other = StaticCountingAllocator<typename base_traits::template rebind_alloc<U>>;
};
StaticCountingAllocator() = default;
template <typename U>
StaticCountingAllocator( const StaticCountingAllocator<U>& other ) : base_type(other) {}
value_type* allocate( std::size_t n ) {
if (max_items != 0 && items_allocated + n >= max_items) {
if (throwing) {
TBB_TEST_THROW(std::bad_alloc{});
}
return nullptr;
}
value_type* ptr = static_cast<base_type*>(this)->allocate(n);
++allocations;
items_allocated += n;
return ptr;
}
void deallocate(const pointer ptr, const std::size_t n){
++frees;
items_freed += n;
static_cast<base_type*>(this)->deallocate(ptr, n);
}
template <typename U, typename... Args>
void construct( U* ptr, Args&&... args ) {
++items_constructed;
base_traits::construct(*this, ptr, std::forward<Args>(args)...);
}
template <typename U>
void destroy( U* ptr ) {
++items_destroyed;
base_traits::destroy(*this, ptr);
}
static AllocatorCounters counters() {
return {items_allocated, items_freed, items_constructed, items_destroyed, allocations, frees};
}
static void init_counters() {
items_allocated = 0;
items_freed = 0;
items_constructed = 0;
items_destroyed = 0;
allocations = 0;
frees = 0;
}
static void set_limits( std::size_t max = 0, bool do_throw = true ) {
max_items = max;
throwing = do_throw;
}
}; // class StaticCountingAllocator
template <typename T>
std::size_t StaticCountingAllocator<T>::max_items;
template <typename T>
std::atomic<std::size_t> StaticCountingAllocator<T>::items_allocated;
template <typename T>
std::atomic<std::size_t> StaticCountingAllocator<T>::items_freed;
template <typename T>
std::atomic<std::size_t> StaticCountingAllocator<T>::items_constructed;
template <typename T>
std::atomic<std::size_t> StaticCountingAllocator<T>::items_destroyed;
template <typename T>
std::atomic<std::size_t> StaticCountingAllocator<T>::allocations;
template <typename T>
std::atomic<std::size_t> StaticCountingAllocator<T>::frees;
template <typename T>
bool StaticCountingAllocator<T>::throwing;
struct StaticSharedCountingAllocatorBase {
using counter_type = std::atomic<std::size_t>;
using counters_type = AllocatorCounters;
static std::size_t max_items;
static counter_type items_allocated;
static counter_type items_freed;
static counter_type items_constructed;
static counter_type items_destroyed;
static counter_type allocations;
static counter_type frees;
static bool throwing;
static counters_type counters() {
return { items_allocated.load(), items_freed.load(), items_constructed.load(),
items_destroyed.load(), allocations.load(), frees.load() };
}
static void init_counters() {
items_allocated = 0;
items_freed = 0;
items_constructed = 0;
items_destroyed = 0;
allocations = 0;
frees = 0;
}
static void set_limits( std::size_t max = 0, bool do_throw = true ) {
max_items = max;
throwing = do_throw;
}
}; // class StaticSharedCountingAllocatorBase
std::size_t StaticSharedCountingAllocatorBase::max_items;
std::atomic<std::size_t> StaticSharedCountingAllocatorBase::items_constructed;
std::atomic<std::size_t> StaticSharedCountingAllocatorBase::items_destroyed;
std::atomic<std::size_t> StaticSharedCountingAllocatorBase::items_allocated;
std::atomic<std::size_t> StaticSharedCountingAllocatorBase::items_freed;
std::atomic<std::size_t> StaticSharedCountingAllocatorBase::allocations;
std::atomic<std::size_t> StaticSharedCountingAllocatorBase::frees;
bool StaticSharedCountingAllocatorBase::throwing;
template <typename BaseAllocatorType>
class StaticSharedCountingAllocator
: public StaticSharedCountingAllocatorBase, public BaseAllocatorType
{
using base_type = StaticSharedCountingAllocatorBase;
using alloc_base_type = BaseAllocatorType;
using base_traits = tbb::detail::allocator_traits<BaseAllocatorType>;
public:
using value_type = typename alloc_base_type::value_type;
using counters_type = AllocatorCounters;
template <typename U>
struct rebind {
using other = StaticSharedCountingAllocator<typename base_traits::template rebind_alloc<U>>;
};
StaticSharedCountingAllocator() = default;
StaticSharedCountingAllocator( const StaticSharedCountingAllocator& ) = default;
StaticSharedCountingAllocator& operator=( const StaticSharedCountingAllocator& ) = default;
template <typename U>
StaticSharedCountingAllocator( const StaticSharedCountingAllocator<U>& other) : alloc_base_type(other) {}
// Constructor from the base allocator with any type
template <typename Alloc>
StaticSharedCountingAllocator( const Alloc& src ) noexcept
: alloc_base_type(src) {}
value_type* allocate( std::size_t n ) {
if (base_type::max_items != 0 &&
base_type::items_allocated + n >= base_type::max_items) {
if (base_type::throwing) {
TBB_TEST_THROW(std::bad_alloc());
}
return nullptr;
}
++base_type::allocations;
base_type::items_allocated += n;
return static_cast<alloc_base_type*>(this)->allocate(n);
}
void deallocate( value_type* ptr, std::size_t n ) {
++base_type::frees;
base_type::items_freed += n;
static_cast<alloc_base_type*>(this)->deallocate(ptr, n);
}
template <typename U, typename... Args>
void construct( U* ptr, Args&&... args ) {
base_traits::construct(*this, ptr, std::forward<Args>(args)...);
++base_type::items_constructed;
}
template <typename U>
void destroy( U* ptr ) {
base_traits::destroy(*this, ptr);
++base_type::items_destroyed;
}
}; // class StaticSharedCountingAllocator
template <typename Allocator>
class AllocatorAwareData {
public:
static bool assert_on_constructions;
using allocator_type = Allocator;
AllocatorAwareData( const allocator_type& allocator = allocator_type() )
: my_allocator(allocator), my_value(0) {}
AllocatorAwareData( int v, const allocator_type& allocator = allocator_type() )
: my_allocator(allocator), my_value(v) {}
AllocatorAwareData( const AllocatorAwareData& rhs )
: my_allocator(rhs.my_allocator), my_value(rhs.my_value)
{
REQUIRE_MESSAGE(!assert_on_constructions, "Allocator should propagate to the data during copy construction");
}
AllocatorAwareData( AllocatorAwareData&& rhs)
: my_allocator(rhs.my_allocator), my_value(rhs.my_value)
{
REQUIRE_MESSAGE(!assert_on_constructions, "Allocator should propagate to the data during move construction");
}
AllocatorAwareData( const AllocatorAwareData& rhs, const allocator_type& allocator )
: my_allocator(allocator), my_value(rhs.my_value) {}
AllocatorAwareData( AllocatorAwareData&& rhs, const allocator_type& allocator )
: my_allocator(allocator), my_value(rhs.my_value) {}
AllocatorAwareData& operator=( const AllocatorAwareData& other ) {
my_value = other.my_value;
return *this;
}
int value() const { return my_value; }
static void activate() { assert_on_constructions = true; }
static void deactivate() { assert_on_constructions = false; }
private:
allocator_type my_allocator;
int my_value;
}; // class AllocatorAwareData
template <typename Allocator>
bool AllocatorAwareData<Allocator>::assert_on_constructions = false;
template <typename Allocator>
bool operator==( const AllocatorAwareData<Allocator>& lhs, const AllocatorAwareData<Allocator>& rhs ) {
return lhs.value() == rhs.value();
}
template <typename Allocator>
bool operator<( const AllocatorAwareData<Allocator>& lhs, const AllocatorAwareData<Allocator>& rhs ) {
return lhs.value() < rhs.value();
}
namespace std {
template <typename Allocator>
struct hash<AllocatorAwareData<Allocator>> {
std::size_t operator()(const AllocatorAwareData<Allocator>& obj) const {
return std::hash<int>()(obj.value());
}
};
}
template <typename Allocator, typename POCMA = std::false_type, typename POCCA = std::false_type,
typename POCS = std::false_type>
struct PropagatingAllocator : Allocator {
using base_allocator_traits = std::allocator_traits<Allocator>;
using propagate_on_container_copy_assignment = POCCA;
using propagate_on_container_move_assignment = POCMA;
using propagate_on_container_swap = POCS;
bool* propagated_on_copy_assignment;
bool* propagated_on_move_assignment;
bool* propagated_on_swap;
bool* selected_on_copy_construction;
template <typename U>
struct rebind {
using other = PropagatingAllocator<typename base_allocator_traits::template rebind_alloc<U>,
POCMA, POCCA, POCS>;
};
PropagatingAllocator()
: propagated_on_copy_assignment(nullptr),
propagated_on_move_assignment(nullptr),
propagated_on_swap(nullptr),
selected_on_copy_construction(nullptr) {}
PropagatingAllocator( bool& poca, bool& poma, bool& pos, bool& soc )
: propagated_on_copy_assignment(&poca),
propagated_on_move_assignment(&poma),
propagated_on_swap(&pos),
selected_on_copy_construction(&soc) {}
PropagatingAllocator( const PropagatingAllocator& other )
: Allocator(other),
propagated_on_copy_assignment(other.propagated_on_copy_assignment),
propagated_on_move_assignment(other.propagated_on_move_assignment),
propagated_on_swap(other.propagated_on_swap),
selected_on_copy_construction(other.selected_on_copy_construction) {}
template <typename Allocator2>
PropagatingAllocator( const PropagatingAllocator<Allocator2, POCMA, POCCA, POCS>& other )
: Allocator(other),
propagated_on_copy_assignment(other.propagated_on_copy_assignment),
propagated_on_move_assignment(other.propagated_on_move_assignment),
propagated_on_swap(other.propagated_on_swap),
selected_on_copy_construction(other.selected_on_copy_construction) {}
PropagatingAllocator& operator=( const PropagatingAllocator& ) {
REQUIRE_MESSAGE(POCCA::value, "Allocator should not copy assign if POCCA is false");
if (propagated_on_copy_assignment)
*propagated_on_copy_assignment = true;
return *this;
}
PropagatingAllocator& operator=( PropagatingAllocator&& ) {
REQUIRE_MESSAGE(POCMA::value, "Allocator should not move assign if POCMA is false");
if (propagated_on_move_assignment)
*propagated_on_move_assignment = true;
return *this;
}
PropagatingAllocator select_on_container_copy_construction() const {
if (selected_on_copy_construction)
*selected_on_copy_construction = true;
return *this;
}
}; // struct PropagatingAllocator
template <typename Allocator, typename POCMA, typename POCCA, typename POCS>
void swap( PropagatingAllocator<Allocator, POCMA, POCCA, POCS>& lhs,
PropagatingAllocator<Allocator, POCMA, POCCA, POCS>& )
{
REQUIRE_MESSAGE(POCS::value, "Allocator should not swap if POCS is false");
if (lhs.propagated_on_swap)
*lhs.propagated_on_swap = true;
}
template <typename T>
using AlwaysPropagatingAllocator = PropagatingAllocator<std::allocator<T>, /*POCMA = */std::true_type,
/*POCCA = */std::true_type, /*POCS = */std::true_type>;
template <typename T>
using NeverPropagatingAllocator = PropagatingAllocator<std::allocator<T>>;
template <typename T>
using PocmaAllocator = PropagatingAllocator<std::allocator<T>, /*POCMA = */std::true_type>;
template <typename T>
using PoccaAllocator = PropagatingAllocator<std::allocator<T>, /*POCMA = */std::false_type, /*POCCA = */std::true_type>;
template <typename T>
using PocsAllocator = PropagatingAllocator<std::allocator<T>, /*POCMA = */std::false_type, /*POCCA = */std::false_type,
/*POCS = */std::true_type>;
template <typename T>
class AlwaysEqualAllocator : public std::allocator<T> {
using base_allocator = std::allocator<T>;
public:
using is_always_equal = std::true_type;
using value_type = typename base_allocator::value_type;
using propagate_on_container_move_assignment = std::false_type;
template <typename U>
struct rebind {
using other = AlwaysEqualAllocator<U>;
};
AlwaysEqualAllocator() = default;
AlwaysEqualAllocator( const AlwaysEqualAllocator& ) = default;
template <typename U>
AlwaysEqualAllocator( const AlwaysEqualAllocator<U>& other )
: base_allocator(other) {}
}; // class AlwaysEqualAllocator
template <typename T>
class NotAlwaysEqualAllocator : public std::allocator<T> {
using base_allocator = std::allocator<T>;
public:
using is_always_equal = std::false_type;
using value_type = typename base_allocator::value_type;
using propagate_on_container_swap = std::false_type;
template <typename U>
struct rebind {
using other = NotAlwaysEqualAllocator<U>;
};
NotAlwaysEqualAllocator() = default;
NotAlwaysEqualAllocator( const NotAlwaysEqualAllocator& ) = default;
template <typename U>
NotAlwaysEqualAllocator( const NotAlwaysEqualAllocator<U>& other )
: base_allocator(other) {}
};
template <typename T>
bool operator==( const AlwaysEqualAllocator<T>&, const AlwaysEqualAllocator<T>& ) {
#ifndef __TBB_TEST_SKIP_IS_ALWAYS_EQUAL_CHECK
REQUIRE_MESSAGE(false, "operator== should not be called if is_always_equal is true");
#endif
return true;
}
#endif // __TBB_test_common_custom_allocators_H
|