Path: blob/main/contrib/llvm-project/libcxx/include/__memory/allocator.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_ALLOCATOR_H10#define _LIBCPP___MEMORY_ALLOCATOR_H1112#include <__config>13#include <__memory/addressof.h>14#include <__memory/allocate_at_least.h>15#include <__memory/allocator_traits.h>16#include <__type_traits/is_const.h>17#include <__type_traits/is_constant_evaluated.h>18#include <__type_traits/is_same.h>19#include <__type_traits/is_void.h>20#include <__type_traits/is_volatile.h>21#include <__utility/forward.h>22#include <cstddef>23#include <new>2425#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif2829_LIBCPP_BEGIN_NAMESPACE_STD3031template <class _Tp>32class allocator;3334#if _LIBCPP_STD_VER <= 1735// These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.36// Specializing allocator<void> is deprecated, but not using it.37template <>38class _LIBCPP_TEMPLATE_VIS allocator<void> {39public:40_LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;41_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;42_LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;4344template <class _Up>45struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {46typedef allocator<_Up> other;47};48};4950// TODO(LLVM 20): Remove the escape hatch51# ifdef _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST52template <>53class _LIBCPP_TEMPLATE_VIS allocator<const void> {54public:55_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;56_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;57_LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type;5859template <class _Up>60struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {61typedef allocator<_Up> other;62};63};64# endif // _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST65#endif // _LIBCPP_STD_VER <= 176667// This class provides a non-trivial default constructor to the class that derives from it68// if the condition is satisfied.69//70// The second template parameter exists to allow giving a unique type to __non_trivial_if,71// which makes it possible to avoid breaking the ABI when making this a base class of an72// existing class. Without that, imagine we have classes D1 and D2, both of which used to73// have no base classes, but which now derive from __non_trivial_if. The layout of a class74// that inherits from both D1 and D2 will change because the two __non_trivial_if base75// classes are not allowed to share the same address.76//77// By making those __non_trivial_if base classes unique, we work around this problem and78// it is safe to start deriving from __non_trivial_if in existing classes.79template <bool _Cond, class _Unique>80struct __non_trivial_if {};8182template <class _Unique>83struct __non_trivial_if<true, _Unique> {84_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT {}85};8687// allocator88//89// Note: For ABI compatibility between C++20 and previous standards, we make90// allocator<void> trivial in C++20.9192template <class _Tp>93class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > {94static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");95static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");9697public:98typedef size_t size_type;99typedef ptrdiff_t difference_type;100typedef _Tp value_type;101typedef true_type propagate_on_container_move_assignment;102#if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)103_LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;104#endif105106_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;107108template <class _Up>109_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}110111_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {112if (__n > allocator_traits<allocator>::max_size(*this))113__throw_bad_array_new_length();114if (__libcpp_is_constant_evaluated()) {115return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));116} else {117return static_cast<_Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));118}119}120121#if _LIBCPP_STD_VER >= 23122[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {123return {allocate(__n), __n};124}125#endif126127_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {128if (__libcpp_is_constant_evaluated()) {129::operator delete(__p);130} else {131std::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));132}133}134135// C++20 Removed members136#if _LIBCPP_STD_VER <= 17137_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;138_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;139_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;140_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;141142template <class _Up>143struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {144typedef allocator<_Up> other;145};146147_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {148return std::addressof(__x);149}150_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {151return std::addressof(__x);152}153154_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {155return allocate(__n);156}157158_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {159return size_type(~0) / sizeof(_Tp);160}161162template <class _Up, class... _Args>163_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {164::new ((void*)__p) _Up(std::forward<_Args>(__args)...);165}166167_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }168#endif169};170171// TODO(LLVM 20): Remove the escape hatch172#ifdef _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST173template <class _Tp>174class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>175: private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> > {176static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");177178public:179typedef size_t size_type;180typedef ptrdiff_t difference_type;181typedef const _Tp value_type;182typedef true_type propagate_on_container_move_assignment;183# if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)184_LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;185# endif186187_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;188189template <class _Up>190_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}191192_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const _Tp* allocate(size_t __n) {193if (__n > allocator_traits<allocator>::max_size(*this))194__throw_bad_array_new_length();195if (__libcpp_is_constant_evaluated()) {196return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));197} else {198return static_cast<const _Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));199}200}201202# if _LIBCPP_STD_VER >= 23203[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<const _Tp*> allocate_at_least(size_t __n) {204return {allocate(__n), __n};205}206# endif207208_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(const _Tp* __p, size_t __n) {209if (__libcpp_is_constant_evaluated()) {210::operator delete(const_cast<_Tp*>(__p));211} else {212std::__libcpp_deallocate((void*)const_cast<_Tp*>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));213}214}215216// C++20 Removed members217# if _LIBCPP_STD_VER <= 17218_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;219_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;220_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;221_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;222223template <class _Up>224struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {225typedef allocator<_Up> other;226};227228_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {229return std::addressof(__x);230}231232_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 const _Tp* allocate(size_t __n, const void*) {233return allocate(__n);234}235236_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {237return size_type(~0) / sizeof(_Tp);238}239240template <class _Up, class... _Args>241_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {242::new ((void*)__p) _Up(std::forward<_Args>(__args)...);243}244245_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }246# endif247};248#endif // _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST249250template <class _Tp, class _Up>251inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool252operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {253return true;254}255256#if _LIBCPP_STD_VER <= 17257258template <class _Tp, class _Up>259inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {260return false;261}262263#endif264265_LIBCPP_END_NAMESPACE_STD266267#endif // _LIBCPP___MEMORY_ALLOCATOR_H268269270