| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- | 
|  | 2 | //===----------------------------------------------------------------------===// | 
|  | 3 | // | 
| Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | //                     The LLVM Compiler Infrastructure | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // | 
| Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open | 
|  | 7 | // Source Licenses. See LICENSE.TXT for details. | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // | 
|  | 9 | //===----------------------------------------------------------------------===// | 
|  | 10 |  | 
|  | 11 | #ifndef _LIBCPP___SSO_ALLOCATOR | 
|  | 12 | #define _LIBCPP___SSO_ALLOCATOR | 
|  | 13 |  | 
|  | 14 | #include <__config> | 
|  | 15 | #include <type_traits> | 
|  | 16 | #include <new> | 
|  | 17 |  | 
| Saleem Abdulrasool | 8e5ce33 | 2015-02-13 22:15:32 +0000 | [diff] [blame] | 18 | #include <__undef___deallocate> | 
|  | 19 |  | 
| Howard Hinnant | 073458b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 20 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | #pragma GCC system_header | 
| Howard Hinnant | 073458b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 22 | #endif | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 |  | 
|  | 24 | _LIBCPP_BEGIN_NAMESPACE_STD | 
|  | 25 |  | 
| Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 26 | template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator; | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 |  | 
| Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 28 | template <size_t _Np> | 
|  | 29 | class _LIBCPP_HIDDEN __sso_allocator<void, _Np> | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | { | 
|  | 31 | public: | 
|  | 32 | typedef const void*       const_pointer; | 
|  | 33 | typedef void              value_type; | 
|  | 34 | }; | 
|  | 35 |  | 
| Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 36 | template <class _Tp, size_t _Np> | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | class _LIBCPP_HIDDEN __sso_allocator | 
|  | 38 | { | 
| Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 39 | typename aligned_storage<sizeof(_Tp) * _Np>::type buf_; | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | bool __allocated_; | 
|  | 41 | public: | 
|  | 42 | typedef size_t            size_type; | 
|  | 43 | typedef _Tp*              pointer; | 
|  | 44 | typedef _Tp               value_type; | 
|  | 45 |  | 
|  | 46 | _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {} | 
|  | 47 | _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {} | 
| Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 48 | template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw() | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | : __allocated_(false) {} | 
|  | 50 | private: | 
|  | 51 | __sso_allocator& operator=(const __sso_allocator&); | 
|  | 52 | public: | 
| Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 53 | _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0) | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | { | 
| Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 55 | if (!__allocated_ && __n <= _Np) | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | { | 
|  | 57 | __allocated_ = true; | 
|  | 58 | return (pointer)&buf_; | 
|  | 59 | } | 
| Richard Smith | ff0aff3 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 60 | return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp))); | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 61 | } | 
|  | 62 | _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) | 
|  | 63 | { | 
|  | 64 | if (__p == (pointer)&buf_) | 
|  | 65 | __allocated_ = false; | 
|  | 66 | else | 
| Richard Smith | ff0aff3 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 67 | _VSTD::__deallocate(__p); | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 68 | } | 
|  | 69 | _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);} | 
|  | 70 |  | 
| Howard Hinnant | f5ab703 | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 71 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;} | 
| Howard Hinnant | f5ab703 | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 73 | _LIBCPP_INLINE_VISIBILITY | 
| Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;} | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | _LIBCPP_END_NAMESPACE_STD | 
|  | 78 |  | 
|  | 79 | #endif  // _LIBCPP___SSO_ALLOCATOR |