1*4d6fc14bSjoerg // -*- C++ -*- 2*4d6fc14bSjoerg //===----------------------------------------------------------------------===// 3*4d6fc14bSjoerg // 4*4d6fc14bSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*4d6fc14bSjoerg // See https://llvm.org/LICENSE.txt for license information. 6*4d6fc14bSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*4d6fc14bSjoerg // 8*4d6fc14bSjoerg //===----------------------------------------------------------------------===// 9*4d6fc14bSjoerg 10*4d6fc14bSjoerg #ifndef _LIBCPP_SSO_ALLOCATOR_H 11*4d6fc14bSjoerg #define _LIBCPP_SSO_ALLOCATOR_H 12*4d6fc14bSjoerg 13*4d6fc14bSjoerg #include <__config> 14*4d6fc14bSjoerg #include <memory> 15*4d6fc14bSjoerg #include <new> 16*4d6fc14bSjoerg #include <type_traits> 17*4d6fc14bSjoerg 18*4d6fc14bSjoerg #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19*4d6fc14bSjoerg #pragma GCC system_header 20*4d6fc14bSjoerg #endif 21*4d6fc14bSjoerg 22*4d6fc14bSjoerg _LIBCPP_BEGIN_NAMESPACE_STD 23*4d6fc14bSjoerg 24*4d6fc14bSjoerg template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator; 25*4d6fc14bSjoerg 26*4d6fc14bSjoerg template <size_t _Np> 27*4d6fc14bSjoerg class _LIBCPP_HIDDEN __sso_allocator<void, _Np> 28*4d6fc14bSjoerg { 29*4d6fc14bSjoerg public: 30*4d6fc14bSjoerg typedef const void* const_pointer; 31*4d6fc14bSjoerg typedef void value_type; 32*4d6fc14bSjoerg }; 33*4d6fc14bSjoerg 34*4d6fc14bSjoerg template <class _Tp, size_t _Np> 35*4d6fc14bSjoerg class _LIBCPP_HIDDEN __sso_allocator 36*4d6fc14bSjoerg { 37*4d6fc14bSjoerg typename aligned_storage<sizeof(_Tp) * _Np>::type buf_; 38*4d6fc14bSjoerg bool __allocated_; 39*4d6fc14bSjoerg public: 40*4d6fc14bSjoerg typedef size_t size_type; 41*4d6fc14bSjoerg typedef _Tp* pointer; 42*4d6fc14bSjoerg typedef _Tp value_type; 43*4d6fc14bSjoerg __sso_allocator()44*4d6fc14bSjoerg _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {} __sso_allocator(const __sso_allocator &)45*4d6fc14bSjoerg _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {} __sso_allocator(const __sso_allocator<_Up,_Np> &)46*4d6fc14bSjoerg template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw() 47*4d6fc14bSjoerg : __allocated_(false) {} 48*4d6fc14bSjoerg private: 49*4d6fc14bSjoerg __sso_allocator& operator=(const __sso_allocator&); 50*4d6fc14bSjoerg public: 51*4d6fc14bSjoerg _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = nullptr) 52*4d6fc14bSjoerg { 53*4d6fc14bSjoerg if (!__allocated_ && __n <= _Np) 54*4d6fc14bSjoerg { 55*4d6fc14bSjoerg __allocated_ = true; 56*4d6fc14bSjoerg return (pointer)&buf_; 57*4d6fc14bSjoerg } 58*4d6fc14bSjoerg return allocator<_Tp>().allocate(__n); 59*4d6fc14bSjoerg } deallocate(pointer __p,size_type __n)60*4d6fc14bSjoerg _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) 61*4d6fc14bSjoerg { 62*4d6fc14bSjoerg if (__p == (pointer)&buf_) 63*4d6fc14bSjoerg __allocated_ = false; 64*4d6fc14bSjoerg else 65*4d6fc14bSjoerg allocator<_Tp>().deallocate(__p, __n); 66*4d6fc14bSjoerg } max_size()67*4d6fc14bSjoerg _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);} 68*4d6fc14bSjoerg 69*4d6fc14bSjoerg _LIBCPP_INLINE_VISIBILITY 70*4d6fc14bSjoerg bool operator==(const __sso_allocator& __a) const {return &buf_ == &__a.buf_;} 71*4d6fc14bSjoerg _LIBCPP_INLINE_VISIBILITY 72*4d6fc14bSjoerg bool operator!=(const __sso_allocator& __a) const {return &buf_ != &__a.buf_;} 73*4d6fc14bSjoerg }; 74*4d6fc14bSjoerg 75*4d6fc14bSjoerg _LIBCPP_END_NAMESPACE_STD 76*4d6fc14bSjoerg 77*4d6fc14bSjoerg #endif // _LIBCPP_SSO_ALLOCATOR_H 78