2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___SSO_ALLOCATOR
12 #define _LIBCPP___SSO_ALLOCATOR
15 #include <type_traits>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
22 _LIBCPP_BEGIN_NAMESPACE_STD
24 template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
27 class _LIBCPP_HIDDEN __sso_allocator<void, _Np>
30 typedef const void* const_pointer;
31 typedef void value_type;
34 template <class _Tp, size_t _Np>
35 class _LIBCPP_HIDDEN __sso_allocator
37 typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
40 typedef size_t size_type;
42 typedef _Tp value_type;
44 _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {}
45 _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {}
46 template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
47 : __allocated_(false) {}
49 __sso_allocator& operator=(const __sso_allocator&);
51 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0)
53 if (!__allocated_ && __n <= _Np)
56 return (pointer)&buf_;
58 return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));
60 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type)
62 if (__p == (pointer)&buf_)
65 ::operator delete(__p);
67 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
69 _LIBCPP_INLINE_VISIBILITY
70 bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;}
71 _LIBCPP_INLINE_VISIBILITY
72 bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;}
75 _LIBCPP_END_NAMESPACE_STD
77 #endif // _LIBCPP___SSO_ALLOCATOR