xref: /freebsd/contrib/llvm-project/libcxx/src/include/sso_allocator.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP_SSO_ALLOCATOR_H
11 #define _LIBCPP_SSO_ALLOCATOR_H
12 
13 #include <__config>
14 #include <memory>
15 #include <new>
16 #include <type_traits>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
25 
26 template <size_t _Np>
27 class _LIBCPP_HIDDEN __sso_allocator<void, _Np>
28 {
29 public:
30     typedef const void*       const_pointer;
31     typedef void              value_type;
32 };
33 
34 template <class _Tp, size_t _Np>
35 class _LIBCPP_HIDDEN __sso_allocator
36 {
37     typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
38     bool __allocated_;
39 public:
40     typedef size_t            size_type;
41     typedef _Tp*              pointer;
42     typedef _Tp               value_type;
43 
44     template <class U>
45     struct rebind {
46         using other = __sso_allocator<U, _Np>;
47     };
48 
49     _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {}
50     _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {}
51     template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
52          : __allocated_(false) {}
53 private:
54     __sso_allocator& operator=(const __sso_allocator&);
55 public:
56     _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = nullptr)
57     {
58         if (!__allocated_ && __n <= _Np)
59         {
60             __allocated_ = true;
61             return (pointer)&buf_;
62         }
63         return allocator<_Tp>().allocate(__n);
64     }
65     _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n)
66     {
67         if (__p == (pointer)&buf_)
68             __allocated_ = false;
69         else
70             allocator<_Tp>().deallocate(__p, __n);
71     }
72     _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
73 
74     _LIBCPP_INLINE_VISIBILITY
75     bool operator==(const __sso_allocator& __a) const {return &buf_ == &__a.buf_;}
76     _LIBCPP_INLINE_VISIBILITY
77     bool operator!=(const __sso_allocator& __a) const {return &buf_ != &__a.buf_;}
78 };
79 
80 _LIBCPP_END_NAMESPACE_STD
81 
82 #endif // _LIBCPP_SSO_ALLOCATOR_H
83