xref: /aosp_15_r20/external/libcxx/include/__sso_allocator (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker// -*- C++ -*-
2*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
3*58b9f456SAndroid Build Coastguard Worker//
4*58b9f456SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
5*58b9f456SAndroid Build Coastguard Worker//
6*58b9f456SAndroid Build Coastguard Worker// This file is dual licensed under the MIT and the University of Illinois Open
7*58b9f456SAndroid Build Coastguard Worker// Source Licenses. See LICENSE.TXT for details.
8*58b9f456SAndroid Build Coastguard Worker//
9*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
10*58b9f456SAndroid Build Coastguard Worker
11*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP___SSO_ALLOCATOR
12*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP___SSO_ALLOCATOR
13*58b9f456SAndroid Build Coastguard Worker
14*58b9f456SAndroid Build Coastguard Worker#include <__config>
15*58b9f456SAndroid Build Coastguard Worker#include <type_traits>
16*58b9f456SAndroid Build Coastguard Worker#include <new>
17*58b9f456SAndroid Build Coastguard Worker
18*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19*58b9f456SAndroid Build Coastguard Worker#pragma GCC system_header
20*58b9f456SAndroid Build Coastguard Worker#endif
21*58b9f456SAndroid Build Coastguard Worker
22*58b9f456SAndroid Build Coastguard Worker_LIBCPP_BEGIN_NAMESPACE_STD
23*58b9f456SAndroid Build Coastguard Worker
24*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
25*58b9f456SAndroid Build Coastguard Worker
26*58b9f456SAndroid Build Coastguard Workertemplate <size_t _Np>
27*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_HIDDEN __sso_allocator<void, _Np>
28*58b9f456SAndroid Build Coastguard Worker{
29*58b9f456SAndroid Build Coastguard Workerpublic:
30*58b9f456SAndroid Build Coastguard Worker    typedef const void*       const_pointer;
31*58b9f456SAndroid Build Coastguard Worker    typedef void              value_type;
32*58b9f456SAndroid Build Coastguard Worker};
33*58b9f456SAndroid Build Coastguard Worker
34*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, size_t _Np>
35*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_HIDDEN __sso_allocator
36*58b9f456SAndroid Build Coastguard Worker{
37*58b9f456SAndroid Build Coastguard Worker    typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
38*58b9f456SAndroid Build Coastguard Worker    bool __allocated_;
39*58b9f456SAndroid Build Coastguard Workerpublic:
40*58b9f456SAndroid Build Coastguard Worker    typedef size_t            size_type;
41*58b9f456SAndroid Build Coastguard Worker    typedef _Tp*              pointer;
42*58b9f456SAndroid Build Coastguard Worker    typedef _Tp               value_type;
43*58b9f456SAndroid Build Coastguard Worker
44*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {}
45*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {}
46*58b9f456SAndroid Build Coastguard Worker    template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
47*58b9f456SAndroid Build Coastguard Worker         : __allocated_(false) {}
48*58b9f456SAndroid Build Coastguard Workerprivate:
49*58b9f456SAndroid Build Coastguard Worker    __sso_allocator& operator=(const __sso_allocator&);
50*58b9f456SAndroid Build Coastguard Workerpublic:
51*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0)
52*58b9f456SAndroid Build Coastguard Worker    {
53*58b9f456SAndroid Build Coastguard Worker        if (!__allocated_ && __n <= _Np)
54*58b9f456SAndroid Build Coastguard Worker        {
55*58b9f456SAndroid Build Coastguard Worker            __allocated_ = true;
56*58b9f456SAndroid Build Coastguard Worker            return (pointer)&buf_;
57*58b9f456SAndroid Build Coastguard Worker        }
58*58b9f456SAndroid Build Coastguard Worker        return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
59*58b9f456SAndroid Build Coastguard Worker    }
60*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n)
61*58b9f456SAndroid Build Coastguard Worker    {
62*58b9f456SAndroid Build Coastguard Worker        if (__p == (pointer)&buf_)
63*58b9f456SAndroid Build Coastguard Worker            __allocated_ = false;
64*58b9f456SAndroid Build Coastguard Worker        else
65*58b9f456SAndroid Build Coastguard Worker            _VSTD::__libcpp_deallocate(__p, __n * sizeof(_Tp), __alignof(_Tp));
66*58b9f456SAndroid Build Coastguard Worker    }
67*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
68*58b9f456SAndroid Build Coastguard Worker
69*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
70*58b9f456SAndroid Build Coastguard Worker    bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;}
71*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
72*58b9f456SAndroid Build Coastguard Worker    bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;}
73*58b9f456SAndroid Build Coastguard Worker};
74*58b9f456SAndroid Build Coastguard Worker
75*58b9f456SAndroid Build Coastguard Worker_LIBCPP_END_NAMESPACE_STD
76*58b9f456SAndroid Build Coastguard Worker
77*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP___SSO_ALLOCATOR
78