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
11 #define _LIBCPP___SSO_ALLOCATOR
14 #include <type_traits>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #pragma GCC system_header
21 _LIBCPP_BEGIN_NAMESPACE_STD
23 template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
26 class _LIBCPP_HIDDEN __sso_allocator<void, _Np>
29 typedef const void* const_pointer;
30 typedef void value_type;
33 template <class _Tp, size_t _Np>
34 class _LIBCPP_HIDDEN __sso_allocator
36 typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
39 typedef size_t size_type;
41 typedef _Tp value_type;
43 _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {}
44 _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {}
45 template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
46 : __allocated_(false) {}
48 __sso_allocator& operator=(const __sso_allocator&);
50 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0)
52 if (!__allocated_ && __n <= _Np)
55 return (pointer)&buf_;
57 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
59 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n)
61 if (__p == (pointer)&buf_)
64 _VSTD::__libcpp_deallocate(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
66 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
68 _LIBCPP_INLINE_VISIBILITY
69 bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;}
70 _LIBCPP_INLINE_VISIBILITY
71 bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;}
74 _LIBCPP_END_NAMESPACE_STD
76 #endif // _LIBCPP___SSO_ALLOCATOR