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