xref: /freebsd-src/contrib/llvm-project/libcxx/include/__utility/small_buffer.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
25f757f3fSDimitry Andric //
35f757f3fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f757f3fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f757f3fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f757f3fSDimitry Andric //
75f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
85f757f3fSDimitry Andric 
95f757f3fSDimitry Andric #ifndef _LIBCPP___UTILITY_SMALL_BUFFER_H
105f757f3fSDimitry Andric #define _LIBCPP___UTILITY_SMALL_BUFFER_H
115f757f3fSDimitry Andric 
125f757f3fSDimitry Andric #include <__config>
135f757f3fSDimitry Andric #include <__memory/construct_at.h>
145f757f3fSDimitry Andric #include <__type_traits/decay.h>
15*0fca6ea1SDimitry Andric #include <__type_traits/is_trivially_constructible.h>
165f757f3fSDimitry Andric #include <__type_traits/is_trivially_destructible.h>
175f757f3fSDimitry Andric #include <__utility/exception_guard.h>
185f757f3fSDimitry Andric #include <__utility/forward.h>
195f757f3fSDimitry Andric #include <cstddef>
205f757f3fSDimitry Andric #include <new>
215f757f3fSDimitry Andric 
225f757f3fSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
235f757f3fSDimitry Andric #  pragma GCC system_header
245f757f3fSDimitry Andric #endif
255f757f3fSDimitry Andric 
265f757f3fSDimitry Andric #if _LIBCPP_STD_VER >= 23
275f757f3fSDimitry Andric 
285f757f3fSDimitry Andric // __small_buffer is a helper class to perform the well known SBO (small buffer optimization). It is mainly useful to
295f757f3fSDimitry Andric // allow type-erasing classes like move_only_function to store small objects in a local buffer without requiring an
305f757f3fSDimitry Andric // allocation.
315f757f3fSDimitry Andric //
325f757f3fSDimitry Andric // This small buffer class only allows storing trivially relocatable objects inside the local storage to allow
335f757f3fSDimitry Andric // __small_buffer to be trivially relocatable itself. Since the buffer doesn't know what's stored inside it, the user
345f757f3fSDimitry Andric // has to manage the object's lifetime, in particular the destruction of the object.
355f757f3fSDimitry Andric 
365f757f3fSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
375f757f3fSDimitry Andric 
385f757f3fSDimitry Andric template <size_t _BufferSize, size_t _BufferAlignment>
395f757f3fSDimitry Andric   requires(_BufferSize > 0 && _BufferAlignment > 0)
405f757f3fSDimitry Andric class __small_buffer {
415f757f3fSDimitry Andric public:
425f757f3fSDimitry Andric   template <class _Tp, class _Decayed = decay_t<_Tp>>
435f757f3fSDimitry Andric   static constexpr bool __fits_in_buffer =
445f757f3fSDimitry Andric       is_trivially_move_constructible_v<_Decayed> && is_trivially_destructible_v<_Decayed> &&
455f757f3fSDimitry Andric       sizeof(_Decayed) <= _BufferSize && alignof(_Decayed) <= _BufferAlignment;
465f757f3fSDimitry Andric 
475f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI __small_buffer()           = default;
485f757f3fSDimitry Andric   __small_buffer(const __small_buffer&)            = delete;
495f757f3fSDimitry Andric   __small_buffer& operator=(const __small_buffer&) = delete;
505f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI ~__small_buffer()          = default;
515f757f3fSDimitry Andric 
525f757f3fSDimitry Andric   // Relocates the buffer - __delete() should never be called on a moved-from __small_buffer
535f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI __small_buffer(__small_buffer&&)            = default;
545f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI __small_buffer& operator=(__small_buffer&&) = default;
555f757f3fSDimitry Andric 
565f757f3fSDimitry Andric   template <class _Stored>
575f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Stored* __get() {
585f757f3fSDimitry Andric     if constexpr (__fits_in_buffer<_Stored>)
595f757f3fSDimitry Andric       return std::launder(reinterpret_cast<_Stored*>(__buffer_));
605f757f3fSDimitry Andric     else
615f757f3fSDimitry Andric       return *std::launder(reinterpret_cast<_Stored**>(__buffer_));
625f757f3fSDimitry Andric   }
635f757f3fSDimitry Andric 
645f757f3fSDimitry Andric   template <class _Stored>
655f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Stored* __alloc() {
665f757f3fSDimitry Andric     if constexpr (__fits_in_buffer<_Stored>) {
675f757f3fSDimitry Andric       return std::launder(reinterpret_cast<_Stored*>(__buffer_));
685f757f3fSDimitry Andric     } else {
695f757f3fSDimitry Andric       byte* __allocation = static_cast<byte*>(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));
705f757f3fSDimitry Andric       std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);
715f757f3fSDimitry Andric       return std::launder(reinterpret_cast<_Stored*>(__allocation));
725f757f3fSDimitry Andric     }
735f757f3fSDimitry Andric   }
745f757f3fSDimitry Andric 
755f757f3fSDimitry Andric   template <class _Stored>
765f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
775f757f3fSDimitry Andric     if constexpr (!__fits_in_buffer<_Stored>)
785f757f3fSDimitry Andric       ::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
795f757f3fSDimitry Andric   }
805f757f3fSDimitry Andric 
815f757f3fSDimitry Andric   template <class _Stored, class... _Args>
825f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) {
835f757f3fSDimitry Andric     _Stored* __buffer = __alloc<_Stored>();
845f757f3fSDimitry Andric     auto __guard      = std::__make_exception_guard([&] { __dealloc<_Stored>(); });
855f757f3fSDimitry Andric     std::construct_at(__buffer, std::forward<_Args>(__args)...);
865f757f3fSDimitry Andric     __guard.__complete();
875f757f3fSDimitry Andric   }
885f757f3fSDimitry Andric 
895f757f3fSDimitry Andric private:
905f757f3fSDimitry Andric   alignas(_BufferAlignment) byte __buffer_[_BufferSize];
915f757f3fSDimitry Andric };
925f757f3fSDimitry Andric 
935f757f3fSDimitry Andric #  undef _LIBCPP_SMALL_BUFFER_TRIVIAL_ABI
945f757f3fSDimitry Andric 
955f757f3fSDimitry Andric _LIBCPP_END_NAMESPACE_STD
965f757f3fSDimitry Andric 
975f757f3fSDimitry Andric #endif // _LIBCPP_STD_VER >= 23
985f757f3fSDimitry Andric 
995f757f3fSDimitry Andric #endif // _LIBCPP___UTILITY_SMALL_BUFFER_H
100