btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / libs / libc++ / __sso_allocator
blobca3b937c013846a8016969daf01cadc1c79ad47c
1 // -*- 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 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___SSO_ALLOCATOR
12 #define _LIBCPP___SSO_ALLOCATOR
14 #include <__config>
15 #include <type_traits>
16 #include <new>
18 #include <__undef___deallocate>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
22 #endif
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
28 template <size_t _Np>
29 class _LIBCPP_HIDDEN __sso_allocator<void, _Np>
31 public:
32     typedef const void*       const_pointer;
33     typedef void              value_type;
36 template <class _Tp, size_t _Np>
37 class _LIBCPP_HIDDEN __sso_allocator
39     typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
40     bool __allocated_;
41 public:
42     typedef size_t            size_type;
43     typedef _Tp*              pointer;
44     typedef _Tp               value_type;
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) {}
50 private:
51     __sso_allocator& operator=(const __sso_allocator&);
52 public:
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);}
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_;}
77 _LIBCPP_END_NAMESPACE_STD
79 #endif  // _LIBCPP___SSO_ALLOCATOR