blob: ca3b937c013846a8016969daf01cadc1c79ad47c [file] [log] [blame]
Dan Albert287553d2017-02-16 10:47:51 -08001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
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#include <__undef___deallocate>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21#pragma GCC system_header
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
27
28template <size_t _Np>
29class _LIBCPP_HIDDEN __sso_allocator<void, _Np>
30{
31public:
32 typedef const void* const_pointer;
33 typedef void value_type;
34};
35
36template <class _Tp, size_t _Np>
37class _LIBCPP_HIDDEN __sso_allocator
38{
39 typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
40 bool __allocated_;
41public:
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) {}
48 template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
49 : __allocated_(false) {}
50private:
51 __sso_allocator& operator=(const __sso_allocator&);
52public:
53 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0)
54 {
55 if (!__allocated_ && __n <= _Np)
56 {
57 __allocated_ = true;
58 return (pointer)&buf_;
59 }
60 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
61 }
62 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type)
63 {
64 if (__p == (pointer)&buf_)
65 __allocated_ = false;
66 else
67 _VSTD::__deallocate(__p);
68 }
69 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
70
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_;}
75};
76
77_LIBCPP_END_NAMESPACE_STD
78
79#endif // _LIBCPP___SSO_ALLOCATOR