wpiutil: Add unique_function (#1761)

This is a move-only variant of std::function to support move-only captures.

Imported from LLVM with some small tweaks (changed to 4 pointer internal storage, warnings fixes).
This commit is contained in:
Peter Johnson
2019-07-15 20:13:57 -05:00
committed by GitHub
parent 73ec940786
commit 85f2f87400
6 changed files with 1306 additions and 0 deletions

View File

@@ -15,6 +15,9 @@
#ifndef WPIUTIL_WPI_COMPILER_H
#define WPIUTIL_WPI_COMPILER_H
#include <new>
#include <stddef.h>
#if defined(_MSC_VER)
#include <sal.h>
#endif
@@ -468,4 +471,46 @@
#endif
#endif
namespace wpi {
/// Allocate a buffer of memory with the given size and alignment.
///
/// When the compiler supports aligned operator new, this will use it to to
/// handle even over-aligned allocations.
///
/// However, this doesn't make any attempt to leverage the fancier techniques
/// like posix_memalign due to portability. It is mostly intended to allow
/// compatibility with platforms that, after aligned allocation was added, use
/// reduced default alignment.
inline void *allocate_buffer(size_t Size, size_t Alignment) {
return ::operator new(Size
#ifdef __cpp_aligned_new
,
std::align_val_t(Alignment)
#endif
);
}
/// Deallocate a buffer of memory with the given size and alignment.
///
/// If supported, this will used the sized delete operator. Also if supported,
/// this will pass the alignment to the delete operator.
///
/// The pointer must have been allocated with the corresponding new operator,
/// most likely using the above helper.
inline void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {
::operator delete(Ptr
#ifdef __cpp_sized_deallocation
,
Size
#endif
#ifdef __cpp_aligned_new
,
std::align_val_t(Alignment)
#endif
);
}
} // End namespace wpi
#endif