batmat 0.0.17
Batched linear algebra routines
Loading...
Searching...
No Matches
storage.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// Aligned allocation for matrix storage.
5/// @ingroup topic-matrix-utils
6
7#include <cstddef>
8#include <memory>
9#include <new>
10#include <tuple>
11#include <type_traits>
12
13namespace batmat::matrix {
14
15template <class A>
16constexpr std::align_val_t as_align_val(A a) {
17 if constexpr (std::is_integral_v<A>)
18 return std::align_val_t{a};
19 else
20 return std::align_val_t{a()};
21}
22
23/// @addtogroup topic-matrix-utils
24/// @{
25
27} inline constexpr uninitialized; ///< Tag type to indicate that memory should not be initialized.
28
29/// Deleter for aligned memory allocated with `operator new(size, align_val)`.
30template <class T, class A>
32 size_t size = 0;
33 [[no_unique_address]] A align = {};
34 void operator()(T *p) const {
35 std::destroy_n(p, size);
36 ::operator delete(p, size * sizeof(T), as_align_val(align));
37 }
38};
39
40template <class A>
41struct aligned_deleter<void, A> {
42 size_t size = 0;
43 [[no_unique_address]] A align = {};
44 void operator()(void *p) const { ::operator delete(p, size, as_align_val(align)); }
45};
46
47/// @}
48
49namespace detail {
50template <class T, class A, bool Init>
51auto make_aligned_unique_ptr(size_t size, A align) {
52 if (size == 0)
53 return std::unique_ptr<T[], aligned_deleter<T, A>>{};
54 std::unique_ptr<void, aligned_deleter<void, A>> raw{
55 ::operator new(size * sizeof(T), as_align_val(align)),
56 {.size = size * sizeof(T), .align = align},
57 };
58 auto *uninitialized = std::launder(static_cast<T *>(raw.get()));
59 if constexpr (Init)
60 std::uninitialized_value_construct_n(uninitialized, size);
61 else
62 std::uninitialized_default_construct_n(uninitialized, size);
63 std::ignore = raw.release();
64 return std::unique_ptr<T[], aligned_deleter<T, A>>{
66 {.size = size, .align = align},
67 };
68}
69} // namespace detail
70
71/// @addtogroup topic-matrix-utils
72/// @{
73
74/// Returns a smart pointer to an array of @p T that satisfies the given
75/// alignment requirements.
76template <class T, class A>
77auto make_aligned_unique_ptr(size_t size, A align) {
79}
80
81/// Returns a smart pointer to an array of @p T that satisfies the given
82/// alignment requirements.
83template <class T, class A>
84auto make_aligned_unique_ptr(size_t size, A align, uninitialized_t) {
86}
87
88/// @}
89
90} // namespace batmat::matrix
auto make_aligned_unique_ptr(size_t size, A align)
Returns a smart pointer to an array of T that satisfies the given alignment requirements.
Definition storage.hpp:77
struct batmat::matrix::uninitialized_t uninitialized
Tag type to indicate that memory should not be initialized.
auto make_aligned_unique_ptr(size_t size, A align)
Definition storage.hpp:51
constexpr std::align_val_t as_align_val(A a)
Definition storage.hpp:16
Deleter for aligned memory allocated with operator new(size, align_val).
Definition storage.hpp:31
void operator()(T *p) const
Definition storage.hpp:34