Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 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 | bc8d3f9 | 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 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 18 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 20 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | |
| 22 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 24 | template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 25 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 26 | template <size_t _Np> |
| 27 | class _LIBCPP_HIDDEN __sso_allocator<void, _Np> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | { |
| 29 | public: |
| 30 | typedef const void* const_pointer; |
| 31 | typedef void value_type; |
| 32 | }; |
| 33 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 34 | template <class _Tp, size_t _Np> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 35 | class _LIBCPP_HIDDEN __sso_allocator |
| 36 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 37 | typename aligned_storage<sizeof(_Tp) * _Np>::type buf_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | bool __allocated_; |
| 39 | public: |
| 40 | typedef size_t size_type; |
| 41 | typedef _Tp* pointer; |
| 42 | typedef _Tp value_type; |
| 43 | |
| 44 | _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {} |
| 45 | _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {} |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 46 | template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw() |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | : __allocated_(false) {} |
| 48 | private: |
| 49 | __sso_allocator& operator=(const __sso_allocator&); |
| 50 | public: |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 51 | _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 52 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 53 | if (!__allocated_ && __n <= _Np) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | { |
| 55 | __allocated_ = true; |
| 56 | return (pointer)&buf_; |
| 57 | } |
| 58 | return static_cast<pointer>(::operator new(__n * sizeof(_Tp))); |
| 59 | } |
| 60 | _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) |
| 61 | { |
| 62 | if (__p == (pointer)&buf_) |
| 63 | __allocated_ = false; |
| 64 | else |
| 65 | ::operator delete(__p); |
| 66 | } |
| 67 | _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);} |
| 68 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 69 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 71 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;} |
| 73 | }; |
| 74 | |
| 75 | _LIBCPP_END_NAMESPACE_STD |
| 76 | |
| 77 | #endif // _LIBCPP___SSO_ALLOCATOR |