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