Path: blob/main/contrib/llvm-project/libcxx/include/__utility/small_buffer.h
35236 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___UTILITY_SMALL_BUFFER_H9#define _LIBCPP___UTILITY_SMALL_BUFFER_H1011#include <__config>12#include <__memory/construct_at.h>13#include <__type_traits/decay.h>14#include <__type_traits/is_trivially_constructible.h>15#include <__type_traits/is_trivially_destructible.h>16#include <__utility/exception_guard.h>17#include <__utility/forward.h>18#include <cstddef>19#include <new>2021#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22# pragma GCC system_header23#endif2425#if _LIBCPP_STD_VER >= 232627// __small_buffer is a helper class to perform the well known SBO (small buffer optimization). It is mainly useful to28// allow type-erasing classes like move_only_function to store small objects in a local buffer without requiring an29// allocation.30//31// This small buffer class only allows storing trivially relocatable objects inside the local storage to allow32// __small_buffer to be trivially relocatable itself. Since the buffer doesn't know what's stored inside it, the user33// has to manage the object's lifetime, in particular the destruction of the object.3435_LIBCPP_BEGIN_NAMESPACE_STD3637template <size_t _BufferSize, size_t _BufferAlignment>38requires(_BufferSize > 0 && _BufferAlignment > 0)39class __small_buffer {40public:41template <class _Tp, class _Decayed = decay_t<_Tp>>42static constexpr bool __fits_in_buffer =43is_trivially_move_constructible_v<_Decayed> && is_trivially_destructible_v<_Decayed> &&44sizeof(_Decayed) <= _BufferSize && alignof(_Decayed) <= _BufferAlignment;4546_LIBCPP_HIDE_FROM_ABI __small_buffer() = default;47__small_buffer(const __small_buffer&) = delete;48__small_buffer& operator=(const __small_buffer&) = delete;49_LIBCPP_HIDE_FROM_ABI ~__small_buffer() = default;5051// Relocates the buffer - __delete() should never be called on a moved-from __small_buffer52_LIBCPP_HIDE_FROM_ABI __small_buffer(__small_buffer&&) = default;53_LIBCPP_HIDE_FROM_ABI __small_buffer& operator=(__small_buffer&&) = default;5455template <class _Stored>56_LIBCPP_HIDE_FROM_ABI _Stored* __get() {57if constexpr (__fits_in_buffer<_Stored>)58return std::launder(reinterpret_cast<_Stored*>(__buffer_));59else60return *std::launder(reinterpret_cast<_Stored**>(__buffer_));61}6263template <class _Stored>64_LIBCPP_HIDE_FROM_ABI _Stored* __alloc() {65if constexpr (__fits_in_buffer<_Stored>) {66return std::launder(reinterpret_cast<_Stored*>(__buffer_));67} else {68byte* __allocation = static_cast<byte*>(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));69std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);70return std::launder(reinterpret_cast<_Stored*>(__allocation));71}72}7374template <class _Stored>75_LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {76if constexpr (!__fits_in_buffer<_Stored>)77::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});78}7980template <class _Stored, class... _Args>81_LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) {82_Stored* __buffer = __alloc<_Stored>();83auto __guard = std::__make_exception_guard([&] { __dealloc<_Stored>(); });84std::construct_at(__buffer, std::forward<_Args>(__args)...);85__guard.__complete();86}8788private:89alignas(_BufferAlignment) byte __buffer_[_BufferSize];90};9192# undef _LIBCPP_SMALL_BUFFER_TRIVIAL_ABI9394_LIBCPP_END_NAMESPACE_STD9596#endif // _LIBCPP_STD_VER >= 239798#endif // _LIBCPP___UTILITY_SMALL_BUFFER_H99100101