small_buffer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef _LIBCPP___UTILITY_SMALL_BUFFER_H
  9. #define _LIBCPP___UTILITY_SMALL_BUFFER_H
  10. #include <__config>
  11. #include <__memory/construct_at.h>
  12. #include <__type_traits/decay.h>
  13. #include <__type_traits/is_trivially_constructible.h>
  14. #include <__type_traits/is_trivially_destructible.h>
  15. #include <__utility/exception_guard.h>
  16. #include <__utility/forward.h>
  17. #include <cstddef>
  18. #include <new>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. #if _LIBCPP_STD_VER >= 23
  23. // __small_buffer is a helper class to perform the well known SBO (small buffer optimization). It is mainly useful to
  24. // allow type-erasing classes like move_only_function to store small objects in a local buffer without requiring an
  25. // allocation.
  26. //
  27. // This small buffer class only allows storing trivially relocatable objects inside the local storage to allow
  28. // __small_buffer to be trivially relocatable itself. Since the buffer doesn't know what's stored inside it, the user
  29. // has to manage the object's lifetime, in particular the destruction of the object.
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. template <size_t _BufferSize, size_t _BufferAlignment>
  32. requires(_BufferSize > 0 && _BufferAlignment > 0)
  33. class __small_buffer {
  34. public:
  35. template <class _Tp, class _Decayed = decay_t<_Tp>>
  36. static constexpr bool __fits_in_buffer =
  37. is_trivially_move_constructible_v<_Decayed> && is_trivially_destructible_v<_Decayed> &&
  38. sizeof(_Decayed) <= _BufferSize && alignof(_Decayed) <= _BufferAlignment;
  39. _LIBCPP_HIDE_FROM_ABI __small_buffer() = default;
  40. __small_buffer(const __small_buffer&) = delete;
  41. __small_buffer& operator=(const __small_buffer&) = delete;
  42. _LIBCPP_HIDE_FROM_ABI ~__small_buffer() = default;
  43. // Relocates the buffer - __delete() should never be called on a moved-from __small_buffer
  44. _LIBCPP_HIDE_FROM_ABI __small_buffer(__small_buffer&&) = default;
  45. _LIBCPP_HIDE_FROM_ABI __small_buffer& operator=(__small_buffer&&) = default;
  46. template <class _Stored>
  47. _LIBCPP_HIDE_FROM_ABI _Stored* __get() {
  48. if constexpr (__fits_in_buffer<_Stored>)
  49. return std::launder(reinterpret_cast<_Stored*>(__buffer_));
  50. else
  51. return *std::launder(reinterpret_cast<_Stored**>(__buffer_));
  52. }
  53. template <class _Stored>
  54. _LIBCPP_HIDE_FROM_ABI _Stored* __alloc() {
  55. if constexpr (__fits_in_buffer<_Stored>) {
  56. return std::launder(reinterpret_cast<_Stored*>(__buffer_));
  57. } else {
  58. byte* __allocation = static_cast<byte*>(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));
  59. std::construct_at(reinterpret_cast<byte**>(__buffer_), __allocation);
  60. return std::launder(reinterpret_cast<_Stored*>(__allocation));
  61. }
  62. }
  63. template <class _Stored>
  64. _LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
  65. if constexpr (!__fits_in_buffer<_Stored>)
  66. ::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
  67. }
  68. template <class _Stored, class... _Args>
  69. _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) {
  70. _Stored* __buffer = __alloc<_Stored>();
  71. auto __guard = std::__make_exception_guard([&] { __dealloc<_Stored>(); });
  72. std::construct_at(__buffer, std::forward<_Args>(__args)...);
  73. __guard.__complete();
  74. }
  75. private:
  76. alignas(_BufferAlignment) byte __buffer_[_BufferSize];
  77. };
  78. # undef _LIBCPP_SMALL_BUFFER_TRIVIAL_ABI
  79. _LIBCPP_END_NAMESPACE_STD
  80. #endif // _LIBCPP_STD_VER >= 23
  81. #endif // _LIBCPP___UTILITY_SMALL_BUFFER_H