blob: 40027363a1850b0c7e5a8d105cacbc0e65c10a97 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___SSO_ALLOCATOR
12#define _LIBCPP___SSO_ALLOCATOR
13
14#include <__config>
15#include <type_traits>
16#include <new>
17
Howard Hinnant08e17472011-10-17 20:05:10 +000018#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000020#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021
22_LIBCPP_BEGIN_NAMESPACE_STD
23
Howard Hinnant99968442011-11-29 18:15:50 +000024template <class _Tp, size_t _Np> class _LIBCPP_HIDDEN __sso_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025
Howard Hinnant99968442011-11-29 18:15:50 +000026template <size_t _Np>
27class _LIBCPP_HIDDEN __sso_allocator<void, _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028{
29public:
30 typedef const void* const_pointer;
31 typedef void value_type;
32};
33
Howard Hinnant99968442011-11-29 18:15:50 +000034template <class _Tp, size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035class _LIBCPP_HIDDEN __sso_allocator
36{
Howard Hinnant99968442011-11-29 18:15:50 +000037 typename aligned_storage<sizeof(_Tp) * _Np>::type buf_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038 bool __allocated_;
39public:
40 typedef size_t size_type;
41 typedef _Tp* pointer;
42 typedef _Tp value_type;
43
44 _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {}
45 _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {}
Howard Hinnant99968442011-11-29 18:15:50 +000046 template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _Np>&) throw()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 : __allocated_(false) {}
48private:
49 __sso_allocator& operator=(const __sso_allocator&);
50public:
Howard Hinnant99968442011-11-29 18:15:50 +000051 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052 {
Howard Hinnant99968442011-11-29 18:15:50 +000053 if (!__allocated_ && __n <= _Np)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054 {
55 __allocated_ = true;
56 return (pointer)&buf_;
57 }
Eric Fiseliera8312872018-03-22 04:42:56 +000058 return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059 }
60 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type)
61 {
62 if (__p == (pointer)&buf_)
63 __allocated_ = false;
64 else
Eric Fiseliera8312872018-03-22 04:42:56 +000065 _VSTD::__libcpp_deallocate(__p, __alignof(_Tp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066 }
67 _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}
68
Howard Hinnant333f50d2010-09-21 20:16:37 +000069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070 bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +000071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;}
73};
74
75_LIBCPP_END_NAMESPACE_STD
76
77#endif // _LIBCPP___SSO_ALLOCATOR