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 #include <__undef___deallocate>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
29 class _LIBCPP_HIDDEN __sso_allocator<void, _Np>
32 typedef const void* const_pointer;
33 typedef void value_type;
36 template <class _Tp, size_t _Np>
37 class _LIBCPP_HIDDEN __sso_allocator
39 typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
42 typedef size_t size_type;
44 typedef _Tp value_type;
46 _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {}
47 _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {}
48 template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
49 : __allocated_(false) {}
51 __sso_allocator& operator=(const __sso_allocator&);
53 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0)
55 if (!__allocated_ && __n <= _Np)
58 return (pointer)&buf_;
60 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
62 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type)
64 if (__p == (pointer)&buf_)
67 _VSTD::__deallocate(__p);
69 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
71 _LIBCPP_INLINE_VISIBILITY
72 bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;}
73 _LIBCPP_INLINE_VISIBILITY
74 bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;}
77 _LIBCPP_END_NAMESPACE_STD
79 #endif // _LIBCPP___SSO_ALLOCATOR