2021-04-10 00:48:34 +08:00
|
|
|
// -*- 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 <__memory/allocator_traits.h>
|
2021-06-05 10:47:47 +08:00
|
|
|
#include <__utility/forward.h>
|
2021-04-10 00:48:34 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <new>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
|
|
|
#pragma GCC system_header
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
template <class _Tp> class allocator;
|
|
|
|
|
2021-06-16 04:08:38 +08:00
|
|
|
#if _LIBCPP_STD_VER <= 17
|
2021-04-10 00:48:34 +08:00
|
|
|
template <>
|
2021-06-16 04:08:38 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS allocator<void>
|
2021-04-10 00:48:34 +08:00
|
|
|
{
|
|
|
|
public:
|
2021-06-16 04:08:38 +08:00
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
|
2021-04-10 00:48:34 +08:00
|
|
|
|
2021-06-16 04:08:38 +08:00
|
|
|
template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
|
2021-04-10 00:48:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2021-06-16 04:08:38 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS allocator<const void>
|
2021-04-10 00:48:34 +08:00
|
|
|
{
|
|
|
|
public:
|
2021-06-16 04:08:38 +08:00
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type;
|
2021-04-10 00:48:34 +08:00
|
|
|
|
2021-06-16 04:08:38 +08:00
|
|
|
template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
|
2021-04-10 00:48:34 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-06-17 00:35:00 +08:00
|
|
|
// This class provides a non-trivial default constructor to the class that derives from it
|
|
|
|
// if the condition is satisfied.
|
|
|
|
//
|
|
|
|
// The second template parameter exists to allow giving a unique type to __non_trivial_if,
|
|
|
|
// which makes it possible to avoid breaking the ABI when making this a base class of an
|
|
|
|
// existing class. Without that, imagine we have classes D1 and D2, both of which used to
|
|
|
|
// have no base classes, but which now derive from __non_trivial_if. The layout of a class
|
|
|
|
// that inherits from both D1 and D2 will change because the two __non_trivial_if base
|
|
|
|
// classes are not allowed to share the same address.
|
|
|
|
//
|
|
|
|
// By making those __non_trivial_if base classes unique, we work around this problem and
|
|
|
|
// it is safe to start deriving from __non_trivial_if in existing classes.
|
|
|
|
template <bool _Cond, class _Unique>
|
|
|
|
struct __non_trivial_if { };
|
|
|
|
|
|
|
|
template <class _Unique>
|
|
|
|
struct __non_trivial_if<true, _Unique> {
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
_LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT { }
|
|
|
|
};
|
|
|
|
|
2021-04-10 00:48:34 +08:00
|
|
|
// allocator
|
2021-06-17 00:35:00 +08:00
|
|
|
//
|
|
|
|
// Note: For ABI compatibility between C++20 and previous standards, we make
|
|
|
|
// allocator<void> trivial in C++20.
|
2021-04-10 00:48:34 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS allocator
|
2021-06-17 00:35:00 +08:00
|
|
|
: private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> >
|
2021-04-10 00:48:34 +08:00
|
|
|
{
|
2021-09-22 23:46:21 +08:00
|
|
|
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
|
2021-04-10 00:48:34 +08:00
|
|
|
public:
|
|
|
|
typedef size_t size_type;
|
|
|
|
typedef ptrdiff_t difference_type;
|
|
|
|
typedef _Tp value_type;
|
|
|
|
typedef true_type propagate_on_container_move_assignment;
|
|
|
|
typedef true_type is_always_equal;
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
2021-06-17 00:35:00 +08:00
|
|
|
allocator() _NOEXCEPT _LIBCPP_DEFAULT
|
2021-04-10 00:48:34 +08:00
|
|
|
|
|
|
|
template <class _Up>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
allocator(const allocator<_Up>&) _NOEXCEPT { }
|
|
|
|
|
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
_Tp* allocate(size_t __n) {
|
|
|
|
if (__n > allocator_traits<allocator>::max_size(*this))
|
|
|
|
__throw_length_error("allocator<T>::allocate(size_t n)"
|
|
|
|
" 'n' exceeds maximum supported size");
|
|
|
|
if (__libcpp_is_constant_evaluated()) {
|
|
|
|
return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
|
|
|
|
} else {
|
|
|
|
return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
|
|
|
|
if (__libcpp_is_constant_evaluated()) {
|
|
|
|
::operator delete(__p);
|
|
|
|
} else {
|
|
|
|
_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++20 Removed members
|
|
|
|
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
|
|
|
|
_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;
|
|
|
|
};
|
|
|
|
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
|
|
|
|
pointer address(reference __x) const _NOEXCEPT {
|
|
|
|
return _VSTD::addressof(__x);
|
|
|
|
}
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
|
|
|
|
const_pointer address(const_reference __x) const _NOEXCEPT {
|
|
|
|
return _VSTD::addressof(__x);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
|
|
|
|
_Tp* allocate(size_t __n, const void*) {
|
|
|
|
return allocate(__n);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
|
|
|
|
return size_type(~0) / sizeof(_Tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Up, class... _Args>
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void construct(_Up* __p, _Args&&... __args) {
|
|
|
|
::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void destroy(pointer __p) {
|
|
|
|
__p->~_Tp();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
|
2021-06-17 00:35:00 +08:00
|
|
|
: private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> >
|
2021-04-10 00:48:34 +08:00
|
|
|
{
|
2021-09-22 23:46:21 +08:00
|
|
|
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
|
2021-04-10 00:48:34 +08:00
|
|
|
public:
|
|
|
|
typedef size_t size_type;
|
|
|
|
typedef ptrdiff_t difference_type;
|
|
|
|
typedef const _Tp value_type;
|
|
|
|
typedef true_type propagate_on_container_move_assignment;
|
|
|
|
typedef true_type is_always_equal;
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
2021-06-17 00:35:00 +08:00
|
|
|
allocator() _NOEXCEPT _LIBCPP_DEFAULT
|
2021-04-10 00:48:34 +08:00
|
|
|
|
|
|
|
template <class _Up>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
allocator(const allocator<_Up>&) _NOEXCEPT { }
|
|
|
|
|
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
const _Tp* allocate(size_t __n) {
|
|
|
|
if (__n > allocator_traits<allocator>::max_size(*this))
|
|
|
|
__throw_length_error("allocator<const T>::allocate(size_t n)"
|
|
|
|
" 'n' exceeds maximum supported size");
|
|
|
|
if (__libcpp_is_constant_evaluated()) {
|
|
|
|
return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
|
|
|
|
} else {
|
|
|
|
return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
void deallocate(const _Tp* __p, size_t __n) {
|
|
|
|
if (__libcpp_is_constant_evaluated()) {
|
|
|
|
::operator delete(const_cast<_Tp*>(__p));
|
|
|
|
} else {
|
|
|
|
_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++20 Removed members
|
|
|
|
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
|
|
|
|
|
|
|
|
template <class _Up>
|
|
|
|
struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
|
|
|
|
typedef allocator<_Up> other;
|
|
|
|
};
|
|
|
|
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
|
|
|
|
const_pointer address(const_reference __x) const _NOEXCEPT {
|
|
|
|
return _VSTD::addressof(__x);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
|
|
|
|
const _Tp* allocate(size_t __n, const void*) {
|
|
|
|
return allocate(__n);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
|
|
|
|
return size_type(~0) / sizeof(_Tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Up, class... _Args>
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void construct(_Up* __p, _Args&&... __args) {
|
|
|
|
::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void destroy(pointer __p) {
|
|
|
|
__p->~_Tp();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp, class _Up>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
|
|
|
|
|
|
|
|
template <class _Tp, class _Up>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
|
|
|
bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP___MEMORY_ALLOCATOR_H
|