Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 18 additions & 28 deletions libcxx/include/any
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ namespace std {
# include <__cxx03/__config>
#else
# include <__config>
# include <__memory/allocator.h>
# include <__memory/allocator_destructor.h>
# include <__memory/allocator_traits.h>
# include <__memory/unique_ptr.h>
# include <__memory/construct_at.h>
# include <__new/allocate.h>
# include <__type_traits/add_cv_quals.h>
# include <__type_traits/add_pointer.h>
# include <__type_traits/aligned_storage.h>
Expand All @@ -103,6 +101,7 @@ namespace std {
# include <__type_traits/remove_cv.h>
# include <__type_traits/remove_cvref.h>
# include <__type_traits/remove_reference.h>
# include <__utility/exception_guard.h>
# include <__utility/forward.h>
# include <__utility/in_place.h>
# include <__utility/move.h>
Expand Down Expand Up @@ -339,22 +338,14 @@ struct _SmallHandler {

template <class... _Args>
_LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) {
typedef allocator<_Tp> _Alloc;
typedef allocator_traits<_Alloc> _ATraits;
_Alloc __a;
_Tp* __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf));
_ATraits::construct(__a, __ret, std::forward<_Args>(__args)...);
auto __ret = std::__construct_at(reinterpret_cast<_Tp*>(&__dest.__s_.__buf), std::forward<_Args>(__args)...);
__dest.__h_ = &_SmallHandler::__handle;
return *__ret;
}

private:
_LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) {
typedef allocator<_Tp> _Alloc;
typedef allocator_traits<_Alloc> _ATraits;
_Alloc __a;
_Tp* __p = static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf));
_ATraits::destroy(__a, __p);
std::__destroy_at(reinterpret_cast<_Tp*>(&__this.__s_.__buf));
__this.__h_ = nullptr;
}

Expand Down Expand Up @@ -406,26 +397,20 @@ struct _LargeHandler {

template <class... _Args>
_LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) {
typedef allocator<_Tp> _Alloc;
typedef allocator_traits<_Alloc> _ATraits;
typedef __allocator_destructor<_Alloc> _Dp;
_Alloc __a;
unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1));
_Tp* __ret = __hold.get();
_ATraits::construct(__a, __ret, std::forward<_Args>(__args)...);
__dest.__s_.__ptr = __hold.release();
_Tp* __ptr = static_cast<_Tp*>(std::__libcpp_allocate<_Tp>(__element_count(1)));
std::__exception_guard __guard([&] { std::__libcpp_deallocate<_Tp>(__ptr, __element_count(1)); });
std::__construct_at(__ptr, std::forward<_Args>(__args)...);
__guard.__complete();
__dest.__s_.__ptr = __ptr;
__dest.__h_ = &_LargeHandler::__handle;
return *__ret;
return *__ptr;
}

private:
_LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) {
typedef allocator<_Tp> _Alloc;
typedef allocator_traits<_Alloc> _ATraits;
_Alloc __a;
_Tp* __p = static_cast<_Tp*>(__this.__s_.__ptr);
_ATraits::destroy(__a, __p);
_ATraits::deallocate(__a, __p, 1);
std::__destroy_at(__p);
std::__libcpp_deallocate<_Tp>(__p, __element_count(1));
__this.__h_ = nullptr;
}

Expand Down Expand Up @@ -613,6 +598,11 @@ _LIBCPP_POP_MACROS
# include <type_traits>
# include <variant>
# endif

# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23
# include <cstring>
# include <limits>
# endif
#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)

#endif // _LIBCPP_ANY
2 changes: 0 additions & 2 deletions libcxx/test/libcxx/transitive_includes/cxx26.csv
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ algorithm ratio
algorithm tuple
algorithm version
any cstdint
any cstring
any initializer_list
any limits
any typeinfo
any version
array cctype
Expand Down
127 changes: 0 additions & 127 deletions libcxx/test/libcxx/utilities/any/allocator.pass.cpp

This file was deleted.

83 changes: 83 additions & 0 deletions libcxx/test/std/utilities/any/any.class/allocator.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

// <any>

// Check that we're consistently using the same allocation functions to
// allocate/deallocate/construct/destroy objects in std::any.
// See https://llvm.org/PR45099 for details.

#include <any>
#include <cassert>
#include <cstddef>
#include <new>

// Make sure we don't fit in std::any's SBO
int allocated_count = 0;
int constructed_count = 0;

struct Large {
Large() { ++constructed_count; }

Large(const Large&) { ++constructed_count; }

~Large() { --constructed_count; }

char big[sizeof(std::any) + 1];

static void* operator new(size_t n) {
++allocated_count;
return ::operator new(n);
}

static void operator delete(void* ptr) {
--allocated_count;
::operator delete(ptr);
}
};

// Make sure we fit in std::any's SBO
struct Small {
Small() { ++constructed_count; }

Small(const Small&) { ++constructed_count; }

~Small() { --constructed_count; }

static void* operator new(size_t n) {
++allocated_count;
return ::operator new(n);
}

static void operator delete(void* ptr) {
--allocated_count;
::operator delete(ptr);
}
};

int main(int, char**) {
// Test large types
{
[[maybe_unused]] std::any a = Large();
assert(constructed_count == 1);
}
assert(allocated_count == 0);
assert(constructed_count == 0);

// Test small types
{
[[maybe_unused]] std::any a = Small();
assert(constructed_count == 1);
}
assert(allocated_count == 0);
assert(constructed_count == 0);

return 0;
}
Loading