|
| 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___FUNCTIONAL_MOVE_ONLY_FUNCTION_H |
| 10 | +#define _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | + |
| 14 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 15 | +# pragma GCC system_header |
| 16 | +#endif |
| 17 | + |
| 18 | +#if _LIBCPP_STD_VER >= 23 && defined(_LIBCPP_ENABLE_EXPERIMENTAL) |
| 19 | + |
| 20 | +// move_only_function design: |
| 21 | +// |
| 22 | +// move_only_function has a small buffer with a size of `3 * sizeof(void*)` bytes. This buffer can only be used when the |
| 23 | +// object that should be stored is trivially relocatable (currently only when it is trivially move constructible and |
| 24 | +// trivially destructible). There is also a bool in the lower bits of the vptr stored which is set when the contained |
| 25 | +// object is not trivially destructible. |
| 26 | +// |
| 27 | +// trivially relocatable: It would also be possible to store nothrow_move_constructible types, but that would mean |
| 28 | +// that move_only_function itself would not be trivially relocatable anymore. The decision to keep move_only_function |
| 29 | +// trivially relocatable was made because we expect move_only_function to be mostly used to store a functor. To only |
| 30 | +// forward functors there is C++26's std::function_ref. |
| 31 | +// |
| 32 | +// buffer size: We did a survey of six implementations from various vendors. Three of them had a buffer size of 24 bytes |
| 33 | +// on 64 bit systems. This also allows storing a std::string or std::vector inside the small buffer (once the compiler |
| 34 | +// has full support of trivially_relocatable annotations). |
| 35 | +// |
| 36 | +// trivially-destructible bit: This allows us to keep the overall binary size smaller because we don't have to store |
| 37 | +// a pointer to a noop function inside the vtable. It also avoids loading the vtable during destruction, potentially |
| 38 | +// resulting in fewer cache misses. The downside is that calling the function now also requires setting the lower bits |
| 39 | +// of the pointer to zero, but this is a very fast operation on modern CPUs. |
| 40 | +// |
| 41 | +// interaction with copyable_function: When converting a copyable_function into a move_only_function we want to avoid |
| 42 | +// wrapping the copyable_function inside the move_only_function to avoid a double indirection. Instead, we copy the |
| 43 | +// small buffer and use copyable_function's vtable. |
| 44 | + |
| 45 | +// NOLINTBEGIN(readability-duplicate-include) |
| 46 | +# define _LIBCPP_IN_MOVE_ONLY_FUNCTION_H |
| 47 | + |
| 48 | +# include <__functional/move_only_function_impl.h> |
| 49 | + |
| 50 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & |
| 51 | +# include <__functional/move_only_function_impl.h> |
| 52 | + |
| 53 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && |
| 54 | +# include <__functional/move_only_function_impl.h> |
| 55 | + |
| 56 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const |
| 57 | +# include <__functional/move_only_function_impl.h> |
| 58 | + |
| 59 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const |
| 60 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & |
| 61 | +# include <__functional/move_only_function_impl.h> |
| 62 | + |
| 63 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const |
| 64 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && |
| 65 | +# include <__functional/move_only_function_impl.h> |
| 66 | + |
| 67 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true |
| 68 | +# include <__functional/move_only_function_impl.h> |
| 69 | + |
| 70 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true |
| 71 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & |
| 72 | +# include <__functional/move_only_function_impl.h> |
| 73 | + |
| 74 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true |
| 75 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && |
| 76 | +# include <__functional/move_only_function_impl.h> |
| 77 | + |
| 78 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true |
| 79 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const |
| 80 | +# include <__functional/move_only_function_impl.h> |
| 81 | + |
| 82 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true |
| 83 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const |
| 84 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF & |
| 85 | +# include <__functional/move_only_function_impl.h> |
| 86 | + |
| 87 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_NOEXCEPT true |
| 88 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV const |
| 89 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF && |
| 90 | +# include <__functional/move_only_function_impl.h> |
| 91 | + |
| 92 | +# undef _LIBCPP_IN_MOVE_ONLY_FUNCTION_H |
| 93 | +// NOLINTEND(readability-duplicate-include) |
| 94 | + |
| 95 | +#endif // _LIBCPP_STD_VER >= 23 && defined(_LIBCPP_ENABLE_EXPERIMENTAL) |
| 96 | + |
| 97 | +#endif // _LIBCPP___FUNCTIONAL_MOVE_ONLY_FUNCTION_H |
0 commit comments