master
  1//===----------------------------------------------------------------------===//
  2//
  3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4// See https://llvm.org/LICENSE.txt for license information.
  5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6//
  7//===----------------------------------------------------------------------===//
  8
  9#ifndef _LIBCPP___UTILITY_SMALL_BUFFER_H
 10#define _LIBCPP___UTILITY_SMALL_BUFFER_H
 11
 12#include <__config>
 13#include <__cstddef/byte.h>
 14#include <__cstddef/size_t.h>
 15#include <__memory/construct_at.h>
 16#include <__new/allocate.h>
 17#include <__new/launder.h>
 18#include <__type_traits/decay.h>
 19#include <__type_traits/is_trivially_constructible.h>
 20#include <__type_traits/is_trivially_destructible.h>
 21#include <__utility/exception_guard.h>
 22#include <__utility/forward.h>
 23
 24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 25#  pragma GCC system_header
 26#endif
 27
 28#if _LIBCPP_STD_VER >= 23
 29
 30// __small_buffer is a helper class to perform the well known SBO (small buffer optimization). It is mainly useful to
 31// allow type-erasing classes like move_only_function to store small objects in a local buffer without requiring an
 32// allocation.
 33//
 34// This small buffer class only allows storing trivially relocatable objects inside the local storage to allow
 35// __small_buffer to be trivially relocatable itself. Since the buffer doesn't know what's stored inside it, the user
 36// has to manage the object's lifetime, in particular the destruction of the object.
 37
 38_LIBCPP_BEGIN_NAMESPACE_STD
 39
 40template <size_t _BufferSize, size_t _BufferAlignment>
 41  requires(_BufferSize > 0 && _BufferAlignment > 0)
 42class __small_buffer {
 43public:
 44  template <class _Tp, class _Decayed = decay_t<_Tp>>
 45  static constexpr bool __fits_in_buffer =
 46      is_trivially_move_constructible_v<_Decayed> && is_trivially_destructible_v<_Decayed> &&
 47      sizeof(_Decayed) <= _BufferSize && alignof(_Decayed) <= _BufferAlignment;
 48
 49  _LIBCPP_HIDE_FROM_ABI __small_buffer()           = default;
 50  __small_buffer(const __small_buffer&)            = delete;
 51  __small_buffer& operator=(const __small_buffer&) = delete;
 52  _LIBCPP_HIDE_FROM_ABI ~__small_buffer()          = default;
 53
 54  // Relocates the buffer - __delete() should never be called on a moved-from __small_buffer
 55  _LIBCPP_HIDE_FROM_ABI __small_buffer(__small_buffer&&)            = default;
 56  _LIBCPP_HIDE_FROM_ABI __small_buffer& operator=(__small_buffer&&) = default;
 57
 58  template <class _Stored>
 59  _LIBCPP_HIDE_FROM_ABI _Stored* __get() {
 60    if constexpr (__fits_in_buffer<_Stored>)
 61      return std::launder(reinterpret_cast<_Stored*>(__buffer_));
 62    else
 63      return *std::launder(reinterpret_cast<_Stored**>(__buffer_));
 64  }
 65
 66  template <class _Stored>
 67  _LIBCPP_HIDE_FROM_ABI _Stored* __alloc() {
 68    if constexpr (__fits_in_buffer<_Stored>) {
 69      return std::launder(reinterpret_cast<_Stored*>(__buffer_));
 70    } else {
 71      byte* __allocation = reinterpret_cast<byte*>(std::__libcpp_allocate<_Stored>(__element_count(1)));
 72      std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);
 73      return std::launder(reinterpret_cast<_Stored*>(__allocation));
 74    }
 75  }
 76
 77  template <class _Stored>
 78  _LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
 79    if constexpr (!__fits_in_buffer<_Stored>)
 80      std::__libcpp_deallocate<_Stored>(__get<_Stored>(), __element_count(1));
 81  }
 82
 83  template <class _Stored, class... _Args>
 84  _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) {
 85    _Stored* __buffer = __alloc<_Stored>();
 86    auto __guard      = std::__make_exception_guard([&] { __dealloc<_Stored>(); });
 87    std::construct_at(__buffer, std::forward<_Args>(__args)...);
 88    __guard.__complete();
 89  }
 90
 91private:
 92  alignas(_BufferAlignment) byte __buffer_[_BufferSize];
 93};
 94
 95#  undef _LIBCPP_SMALL_BUFFER_TRIVIAL_ABI
 96
 97_LIBCPP_END_NAMESPACE_STD
 98
 99#endif // _LIBCPP_STD_VER >= 23
100
101#endif // _LIBCPP___UTILITY_SMALL_BUFFER_H