CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
189 lines
5.3 KiB
C++
189 lines
5.3 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
|
|
// <memory>
|
|
|
|
// To allow checking that self-move works correctly.
|
|
// ADDITIONAL_COMPILE_FLAGS: -Wno-self-move
|
|
|
|
// template<class _Alloc>
|
|
// struct __allocation_guard;
|
|
|
|
#include <__cxx03/__memory/allocation_guard.h>
|
|
#include <cassert>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
#include "test_allocator.h"
|
|
|
|
using A = test_allocator<int>;
|
|
|
|
// A trimmed-down version of `test_allocator` that is copy-assignable (in general allocators don't have to support copy
|
|
// assignment).
|
|
template <class T>
|
|
struct AssignableAllocator {
|
|
using size_type = unsigned;
|
|
using difference_type = int;
|
|
using value_type = T;
|
|
using pointer = value_type*;
|
|
using const_pointer = const value_type*;
|
|
using reference = typename std::add_lvalue_reference<value_type>::type;
|
|
using const_reference = typename std::add_lvalue_reference<const value_type>::type;
|
|
|
|
template <class U>
|
|
struct rebind {
|
|
using other = test_allocator<U>;
|
|
};
|
|
|
|
test_allocator_statistics* stats_ = nullptr;
|
|
|
|
explicit AssignableAllocator(test_allocator_statistics& stats) : stats_(&stats) {
|
|
++stats_->count;
|
|
}
|
|
|
|
TEST_CONSTEXPR_CXX14 AssignableAllocator(const AssignableAllocator& rhs) TEST_NOEXCEPT
|
|
: stats_(rhs.stats_) {
|
|
if (stats_ != nullptr) {
|
|
++stats_->count;
|
|
++stats_->copied;
|
|
}
|
|
}
|
|
|
|
TEST_CONSTEXPR_CXX14 AssignableAllocator& operator=(const AssignableAllocator& rhs) TEST_NOEXCEPT {
|
|
stats_ = rhs.stats_;
|
|
if (stats_ != nullptr) {
|
|
++stats_->count;
|
|
++stats_->copied;
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
TEST_CONSTEXPR_CXX14 pointer allocate(size_type n, const void* = nullptr) {
|
|
if (stats_ != nullptr) {
|
|
++stats_->alloc_count;
|
|
}
|
|
return std::allocator<value_type>().allocate(n);
|
|
}
|
|
|
|
TEST_CONSTEXPR_CXX14 void deallocate(pointer p, size_type s) {
|
|
if (stats_ != nullptr) {
|
|
--stats_->alloc_count;
|
|
}
|
|
std::allocator<value_type>().deallocate(p, s);
|
|
}
|
|
|
|
TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT { return UINT_MAX / sizeof(T); }
|
|
|
|
template <class U>
|
|
TEST_CONSTEXPR_CXX20 void construct(pointer p, U&& val) {
|
|
if (stats_ != nullptr)
|
|
++stats_->construct_count;
|
|
::new (static_cast<void*>(p)) T(std::forward<U>(val));
|
|
}
|
|
|
|
TEST_CONSTEXPR_CXX14 void destroy(pointer p) {
|
|
if (stats_ != nullptr) {
|
|
++stats_->destroy_count;
|
|
}
|
|
p->~T();
|
|
}
|
|
};
|
|
|
|
// Move-only.
|
|
static_assert(!std::is_copy_constructible<std::__allocation_guard<A> >::value, "");
|
|
static_assert(std::is_move_constructible<std::__allocation_guard<A> >::value, "");
|
|
static_assert(!std::is_copy_assignable<std::__allocation_guard<A> >::value, "");
|
|
static_assert(std::is_move_assignable<std::__allocation_guard<A> >::value, "");
|
|
|
|
int main(int, char**) {
|
|
const int size = 42;
|
|
|
|
{ // The constructor allocates using the given allocator.
|
|
test_allocator_statistics stats;
|
|
std::__allocation_guard<A> guard(A(&stats), size);
|
|
assert(stats.alloc_count == 1);
|
|
assert(guard.__get() != nullptr);
|
|
}
|
|
|
|
{ // The destructor deallocates using the given allocator.
|
|
test_allocator_statistics stats;
|
|
{
|
|
std::__allocation_guard<A> guard(A(&stats), size);
|
|
assert(stats.alloc_count == 1);
|
|
}
|
|
assert(stats.alloc_count == 0);
|
|
}
|
|
|
|
{ // `__release_ptr` prevents deallocation.
|
|
test_allocator_statistics stats;
|
|
A alloc(&stats);
|
|
int* ptr = nullptr;
|
|
{
|
|
std::__allocation_guard<A> guard(alloc, size);
|
|
assert(stats.alloc_count == 1);
|
|
ptr = guard.__release_ptr();
|
|
}
|
|
assert(stats.alloc_count == 1);
|
|
alloc.deallocate(ptr, size);
|
|
}
|
|
|
|
{ // Using the move constructor doesn't lead to double deletion.
|
|
test_allocator_statistics stats;
|
|
{
|
|
std::__allocation_guard<A> guard1(A(&stats), size);
|
|
assert(stats.alloc_count == 1);
|
|
auto* ptr1 = guard1.__get();
|
|
|
|
std::__allocation_guard<A> guard2 = std::move(guard1);
|
|
assert(stats.alloc_count == 1);
|
|
assert(guard1.__get() == nullptr);
|
|
assert(guard2.__get() == ptr1);
|
|
}
|
|
assert(stats.alloc_count == 0);
|
|
}
|
|
|
|
{ // Using the move assignment operator doesn't lead to double deletion.
|
|
using A2 = AssignableAllocator<int>;
|
|
|
|
test_allocator_statistics stats;
|
|
{
|
|
std::__allocation_guard<A2> guard1(A2(stats), size);
|
|
assert(stats.alloc_count == 1);
|
|
std::__allocation_guard<A2> guard2(A2(stats), size);
|
|
assert(stats.alloc_count == 2);
|
|
auto* ptr1 = guard1.__get();
|
|
|
|
guard2 = std::move(guard1);
|
|
assert(stats.alloc_count == 1);
|
|
assert(guard1.__get() == nullptr);
|
|
assert(guard2.__get() == ptr1);
|
|
}
|
|
assert(stats.alloc_count == 0);
|
|
}
|
|
|
|
{ // Self-assignment is a no-op.
|
|
using A2 = AssignableAllocator<int>;
|
|
|
|
test_allocator_statistics stats;
|
|
{
|
|
std::__allocation_guard<A2> guard(A2(stats), size);
|
|
assert(stats.alloc_count == 1);
|
|
auto* ptr = guard.__get();
|
|
|
|
guard = std::move(guard);
|
|
assert(stats.alloc_count == 1);
|
|
assert(guard.__get() == ptr);
|
|
}
|
|
assert(stats.alloc_count == 0);
|
|
}
|
|
|
|
return 0;
|
|
}
|