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