__sso_allocator revision 278724
1367SN/A// -*- C++ -*-
2462SN/A//===----------------------------------------------------------------------===//
3367SN/A//
4367SN/A//                     The LLVM Compiler Infrastructure
5367SN/A//
6367SN/A// This file is dual licensed under the MIT and the University of Illinois Open
7367SN/A// Source Licenses. See LICENSE.TXT for details.
8367SN/A//
9367SN/A//===----------------------------------------------------------------------===//
10367SN/A
11367SN/A#ifndef _LIBCPP___SSO_ALLOCATOR
12367SN/A#define _LIBCPP___SSO_ALLOCATOR
13367SN/A
14367SN/A#include <__config>
15367SN/A#include <type_traits>
16367SN/A#include <new>
17367SN/A
18367SN/A#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19367SN/A#pragma GCC system_header
20367SN/A#endif
21367SN/A
22367SN/A_LIBCPP_BEGIN_NAMESPACE_STD
23367SN/A
24367SN/Atemplate <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
25367SN/A
26367SN/Atemplate <size_t _Np>
27367SN/Aclass _LIBCPP_HIDDEN __sso_allocator<void, _Np>
28367SN/A{
29367SN/Apublic:
30367SN/A    typedef const void*       const_pointer;
31367SN/A    typedef void              value_type;
32367SN/A};
33367SN/A
34367SN/Atemplate <class _Tp, size_t _Np>
35367SN/Aclass _LIBCPP_HIDDEN __sso_allocator
36367SN/A{
37367SN/A    typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
38367SN/A    bool __allocated_;
39367SN/Apublic:
40367SN/A    typedef size_t            size_type;
41367SN/A    typedef _Tp*              pointer;
42367SN/A    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) {}
46    template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
47         : __allocated_(false) {}
48private:
49    __sso_allocator& operator=(const __sso_allocator&);
50public:
51    _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0)
52    {
53        if (!__allocated_ && __n <= _Np)
54        {
55            __allocated_ = true;
56            return (pointer)&buf_;
57        }
58        return static_cast<pointer>(_VSTD::__allocate(__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            _VSTD::__deallocate(__p);
66    }
67    _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
68
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_;}
73};
74
75_LIBCPP_END_NAMESPACE_STD
76
77#endif  // _LIBCPP___SSO_ALLOCATOR
78