cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
allocation.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_CONTAINER_ALLOCATION_H
4#define pRC_CORE_CONTAINER_ALLOCATION_H
5
6#include <cstddef>
7#include <cstdlib>
8#include <new>
9
10#include <prc/config.hpp>
13#include <prc/core/log/log.hpp>
14#include <prc/pragma.hpp>
15
16namespace pRC
17{
18 enum class Allocation
19 {
20 Stack,
21 Heap,
22 };
23
24 static inline constexpr auto isProperAlignment(Size const alignment)
25 {
26 if(alignment == 0 || !isPowerOfTwo(alignment))
27 {
28 return false;
29 }
30 return true;
31 }
32
33 template<Size S, Size P = alignof(std::max_align_t)>
34 static inline constexpr auto alignment()
35 {
36 static_assert(isProperAlignment(P),
37 "Proposed alignment is no proper alignment.");
38
39 if constexpr(S == 0)
40 {
41 return P;
42 }
43 else if constexpr(P >= cHugepageSizeByte)
44 {
45 return P;
46 }
47 else
48 {
49 if constexpr(S % (P * 2) == 0)
50 {
51 return alignment<S, P * 2>();
52 }
53 else
54 {
55 return P;
56 }
57 }
58 }
59
60 static inline auto alloc(Size size,
61 Size const alignment = alignof(std::max_align_t))
62 {
63 if constexpr(cDebugLevel >= DebugLevel::Low)
64 {
66 {
67 Logging::error("Alignment is no proper alignment.");
68 }
69 }
70
71 auto p = std::aligned_alloc(alignment, size);
72 if(p == nullptr)
73 {
74 Logging::error("Unable to allocate memory.");
75 }
76
77 if constexpr(cDebugLevel >= DebugLevel::Low)
78 {
79 if(reinterpret_cast<std::uintptr_t>(p) % alignment != 0)
80 {
82 "Allocated memory is not aligned to alignment "
83 "requirement.");
84 }
85 }
86
87 return p;
88 }
89
90 static inline auto dealloc(void *ptr)
91 {
92 std::free(ptr);
93 }
94}
95
96BEGIN_IGNORE_DIAGNOSTIC_CLANG("-Winline-new-delete")
97
98inline void *operator new(std::size_t size)
99{
100 return operator new(size,
101 static_cast<std::align_val_t>(alignof(std::max_align_t)));
102}
103
104inline void *operator new[](std::size_t size)
105{
106 return operator new(size);
107}
108
109inline void *operator new(std::size_t size, std::align_val_t alignment)
110{
111 return pRC::alloc(size, static_cast<pRC::Size>(alignment));
112}
113
114inline void *operator new[](std::size_t size, std::align_val_t alignment)
115{
116 return operator new(size, alignment);
117}
118
119inline void *operator new(std::size_t size,
120 [[maybe_unused]] std::nothrow_t const &) noexcept
121{
122 return operator new(size);
123}
124
125inline void *operator new[](std::size_t size,
126 [[maybe_unused]] std::nothrow_t const &) noexcept
127{
128 return operator new[](size);
129}
130
131inline void *operator new(std::size_t size, std::align_val_t alignment,
132 [[maybe_unused]] std::nothrow_t const &) noexcept
133{
134 return operator new(size, alignment);
135}
136
137inline void *operator new[](std::size_t size, std::align_val_t alignment,
138 [[maybe_unused]] std::nothrow_t const &) noexcept
139{
140 return operator new[](size, alignment);
141}
142
143inline void operator delete(void *ptr) noexcept
144{
145 return pRC::dealloc(ptr);
146}
147
148inline void operator delete[](void *ptr) noexcept
149{
150 return operator delete(ptr);
151}
152
153inline void operator delete(void *ptr, std::align_val_t) noexcept
154{
155 return operator delete(ptr);
156}
157
158inline void operator delete[](void *ptr, std::align_val_t) noexcept
159{
160 return operator delete[](ptr);
161}
162
163inline void operator delete(void *ptr, std::size_t)
164{
165 return operator delete(ptr);
166}
167
168inline void operator delete[](void *ptr, std::size_t)
169{
170 return operator delete[](ptr);
171}
172
173inline void operator delete(void *ptr, std::size_t, std::align_val_t)
174{
175 return operator delete(ptr);
176}
177
178inline void operator delete[](void *ptr, std::size_t, std::align_val_t)
179{
180 return operator delete[](ptr);
181}
182
183inline void operator delete(void *ptr, std::nothrow_t const &) noexcept
184{
185 return operator delete(ptr);
186}
187
188inline void operator delete[](void *ptr, std::nothrow_t const &) noexcept
189{
190 return operator delete[](ptr);
191}
192
193inline void operator delete(void *ptr, std::nothrow_t const &, std::align_val_t)
194{
195 return operator delete(ptr);
196}
197
198inline void operator delete[](void *ptr, std::nothrow_t const &,
199 std::align_val_t)
200{
201 return operator delete[](ptr);
202}
203
205#endif // pRC_CORE_CONTAINER_ALLOCATION_H
TN::Subscripts S
Definition externs_nonTT.hpp:9
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
std::size_t Size
Definition type_traits.hpp:20
static constexpr auto isProperAlignment(Size const alignment)
Definition allocation.hpp:24
constexpr Size cHugepageSizeByte
Definition config.hpp:49
static auto alloc(Size size, Size const alignment=alignof(std::max_align_t))
Definition allocation.hpp:60
constexpr auto cDebugLevel
Definition config.hpp:46
static constexpr auto isPowerOfTwo(T const v)
Definition is_power_of_two.hpp:11
static auto dealloc(void *ptr)
Definition allocation.hpp:90
Allocation
Definition allocation.hpp:19
static constexpr auto alignment()
Definition allocation.hpp:34
#define BEGIN_IGNORE_DIAGNOSTIC_CLANG(warning)
Definition pragma.hpp:45
#define END_IGNORE_DIAGNOSTIC_CLANG
Definition pragma.hpp:46