2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_SSO_ALLOCATOR_H
11 #define _LIBCPP_SSO_ALLOCATOR_H
17 #include <type_traits>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
23 _LIBCPP_BEGIN_NAMESPACE_STD
25 template <class _Tp
, size_t _Np
>
26 class _LIBCPP_HIDDEN __sso_allocator
;
29 class _LIBCPP_HIDDEN __sso_allocator
<void, _Np
> {
31 typedef const void* const_pointer
;
32 typedef void value_type
;
35 template <class _Tp
, size_t _Np
>
36 class _LIBCPP_HIDDEN __sso_allocator
{
37 alignas(_Tp
) std::byte buf_
[sizeof(_Tp
) * _Np
];
41 typedef size_t size_type
;
43 typedef _Tp value_type
;
47 using other
= __sso_allocator
<U
, _Np
>;
50 _LIBCPP_HIDE_FROM_ABI
__sso_allocator() throw() : __allocated_(false) {}
51 _LIBCPP_HIDE_FROM_ABI
__sso_allocator(const __sso_allocator
&) throw() : __allocated_(false) {}
53 _LIBCPP_HIDE_FROM_ABI
__sso_allocator(const __sso_allocator
<_Up
, _Np
>&) throw() : __allocated_(false) {}
56 __sso_allocator
& operator=(const __sso_allocator
&);
59 _LIBCPP_HIDE_FROM_ABI pointer
allocate(size_type __n
, typename __sso_allocator
<void, _Np
>::const_pointer
= nullptr) {
60 if (!__allocated_
&& __n
<= _Np
) {
62 return (pointer
)&buf_
;
64 return allocator
<_Tp
>().allocate(__n
);
66 _LIBCPP_HIDE_FROM_ABI
void deallocate(pointer __p
, size_type __n
) {
67 if (__p
== (pointer
)&buf_
)
70 allocator
<_Tp
>().deallocate(__p
, __n
);
72 _LIBCPP_HIDE_FROM_ABI size_type
max_size() const throw() { return size_type(~0) / sizeof(_Tp
); }
74 _LIBCPP_HIDE_FROM_ABI
bool operator==(const __sso_allocator
& __a
) const { return &buf_
== &__a
.buf_
; }
75 _LIBCPP_HIDE_FROM_ABI
bool operator!=(const __sso_allocator
& __a
) const { return &buf_
!= &__a
.buf_
; }
78 _LIBCPP_END_NAMESPACE_STD
80 #endif // _LIBCPP_SSO_ALLOCATOR_H