blob: aaa9da5a87ce12d541a371e55614be4e0f2b82b9 [file]
// -*- 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
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___MEMORY_ALLOCATOR_H
#define _LIBCPP___MEMORY_ALLOCATOR_H
#include <__config>
#include <__cstddef/ptrdiff_t.h>
#include <__cstddef/size_t.h>
#include <__memory/addressof.h>
#include <__memory/allocator_traits.h>
#include <__new/align_val_t.h>
#include <__new/exceptions.h>
#include <__type_traits/is_const.h>
#include <__type_traits/is_constant_evaluated.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_void.h>
#include <__type_traits/is_volatile.h>
#include <__utility/forward.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _Tp>
class allocator;
#if _LIBCPP_STD_VER <= 17
// These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.
// Specializing allocator<void> is deprecated, but not using it.
template <>
class allocator<void> {
public:
_LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
template <class _Up>
struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
typedef allocator<_Up> other;
};
};
#endif // _LIBCPP_STD_VER <= 17
template <bool, class _Unique>
struct __non_trivially_default_constructible_if {};
template <class _Unique>
struct __non_trivially_default_constructible_if<true, _Unique> {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivially_default_constructible_if() {}
};
// TODO(LLVM 26): Remove these wrappers and call them unconditionally in `allocator::allocate` and
// `allocator::deallocate`. These are here to the `__builtin_operator`s are never in a constant
// context due to a Clang bug.
template <class _Tp, class... _Args>
_Tp* __operator_new_wrapper(size_t __count, _Args&&... __args) {
return static_cast<_Tp*>(__builtin_operator_new(__count * sizeof(_Tp), std::forward<_Args>(__args)...));
}
template <class... _Args>
void __operator_delete_wrapper(_Args&&... __args) {
return __builtin_operator_delete(std::forward<_Args>(__args)...);
}
template <class _Tp>
class allocator
// TODO(LLVM 25): Remove the opt-out
#ifdef _LIBCPP_DEPRECATED_ABI_NON_TRIVIAL_ALLOCATOR
: __non_trivially_default_constructible_if<!is_void<_Tp>::value, allocator<_Tp> >
#endif
{
static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp value_type;
typedef true_type propagate_on_container_move_assignment;
#if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
_LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
#endif
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
template <class _Up>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {
static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
if (__n > allocator_traits<allocator>::max_size(*this))
std::__throw_bad_array_new_length();
if (__libcpp_is_constant_evaluated())
return static_cast<_Tp*>(::operator new(sizeof(_Tp) * __n));
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
if _LIBCPP_CONSTEXPR (_LIBCPP_ALIGNOF(value_type) > _LIBCPP_DEFAULT_NEW_ALIGNMENT)
return std::__operator_new_wrapper<value_type>(__n, static_cast<align_val_t>(_LIBCPP_ALIGNOF(value_type)));
#endif
return std::__operator_new_wrapper<value_type>(__n);
}
#if _LIBCPP_STD_VER >= 23
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {
static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
return {allocate(__n), __n};
}
#endif
#if defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309L
# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) __VA_ARGS__
#else
# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) /* nothing */
#endif
_LIBCPP_HIDE_FROM_ABI
_LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, [[__maybe_unused__]] size_t __n) _NOEXCEPT {
if (__libcpp_is_constant_evaluated())
return ::operator delete(__p);
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
if (_LIBCPP_ALIGNOF(value_type) > _LIBCPP_DEFAULT_NEW_ALIGNMENT)
return std::__operator_delete_wrapper(__p _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __n * sizeof(value_type)),
static_cast<align_val_t>(_LIBCPP_ALIGNOF(value_type)));
#endif
return std::__operator_delete_wrapper(__p _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __n * sizeof(value_type)));
}
#undef _LIBCPP_ONLY_IF_SIZED_DEALLOCATION
// C++20 Removed members
#if _LIBCPP_STD_VER <= 17
_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
template <class _Up>
struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
typedef allocator<_Up> other;
};
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
return std::addressof(__x);
}
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer
address(const_reference __x) const _NOEXCEPT {
return std::addressof(__x);
}
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
return allocate(__n);
}
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
return size_type(~0) / sizeof(_Tp);
}
template <class _Up, class... _Args>
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
}
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
#endif
};
template <class _Tp, class _Up>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
return true;
}
#if _LIBCPP_STD_VER <= 17
template <class _Tp, class _Up>
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
return false;
}
#endif
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___MEMORY_ALLOCATOR_H