14template <
typename Func>
auto defer(Func &&func)
noexcept {
15 using F = std::decay_t<Func>;
16 class [[nodiscard]] Defer {
18 Defer(F func) : func_(std::move(func)) {}
21 Defer(
const Defer &) =
delete;
22 Defer &operator=(
const Defer &) =
delete;
23 Defer(Defer &&) =
delete;
24 Defer &operator=(Defer &&) =
delete;
29 return Defer(std::forward<Func>(func));
32template <
typename T,
typename Alloc>
33using AllocVector = std::vector<
34 T,
typename std::allocator_traits<Alloc>::template rebind_alloc<T>>;
36template <
typename T>
inline T align_up(T value, T alignment)
noexcept {
38 assert(alignment > 0 && (alignment & (alignment - 1)) == 0);
39 return (value + alignment - 1) & ~(alignment - 1);
43concept is_pmr_allocator =
requires(
const T &a) {
44 { a.resource() } -> std::convertible_to<std::pmr::memory_resource *>;
47template <
typename Alloc>
48[[nodiscard]]
inline void *alloc_aligned(
const Alloc &alloc,
size_t bytes,
50 if constexpr (is_pmr_allocator<Alloc>) {
51 return alloc.resource()->allocate(bytes, alignment);
53 return ::operator
new(bytes, std::align_val_t{alignment});
57template <
typename Alloc>
58inline void free_aligned(
const Alloc &alloc,
void *ptr,
size_t bytes,
59 size_t alignment)
noexcept {
60 if constexpr (is_pmr_allocator<Alloc>) {
61 alloc.resource()->deallocate(ptr, bytes, alignment);
63 ::operator
delete(ptr, bytes, std::align_val_t{alignment});
The main namespace of the ublk-cpp library.