Mjolnir Core
Core functionality of the Mjolnir API
pointer_operations.h
Go to the documentation of this file.
1 
7 
8 #pragma once
9 
10 
11 // === DECLARATIONS ===================================================================================================
12 
14 
15 #include <cstdint>
16 
17 
18 namespace mjolnir
19 {
22 
37 template <typename T_Type = void>
38 [[nodiscard]] inline auto integer_to_pointer(UPT integer) noexcept -> T_Type*;
39 
40 
54 template <UST t_alignment, typename T_Type>
55 [[nodiscard]] inline auto is_aligned(const volatile T_Type* pointer) noexcept -> bool;
56 
57 
94 template <typename T_Type>
95 [[nodiscard]] inline auto is_aligned(const volatile T_Type* pointer, UST alignment) noexcept -> bool;
96 
97 
111 template <UST t_alignment, typename T_Type>
112 [[nodiscard]] inline auto misalignment(const volatile T_Type* pointer) noexcept -> UST;
113 
114 
150 template <typename T_Type>
151 [[nodiscard]] inline auto misalignment(const volatile T_Type* pointer, UST alignment) noexcept -> UST;
152 
153 
168 template <typename T_Type>
169 [[nodiscard]] inline auto pointer_to_integer(const volatile T_Type* pointer) noexcept -> UPT;
170 
171 
173 } // namespace mjolnir
174 
175 
176 // === DEFINITIONS ====================================================================================================
177 
178 namespace mjolnir
179 {
180 template <typename T_Type>
181 [[nodiscard]] inline auto integer_to_pointer(UPT integer) noexcept -> T_Type*
182 {
183  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr)
184  return reinterpret_cast<T_Type*>(integer);
185 }
186 
187 
188 // --------------------------------------------------------------------------------------------------------------------
189 
190 template <UST t_alignment, typename T_Type>
191 [[nodiscard]] inline auto is_aligned(const volatile T_Type* pointer) noexcept -> bool
192 {
193  // https://stackoverflow.com/a/28678613
194  return misalignment<t_alignment>(pointer) == 0;
195 }
196 
197 
198 // --------------------------------------------------------------------------------------------------------------------
199 
200 template <typename T_Type>
201 [[nodiscard]] inline auto is_aligned(const volatile T_Type* pointer, UST alignment) noexcept -> bool
202 {
203  // https://stackoverflow.com/a/28678613
204  return misalignment(pointer, alignment) == 0;
205 }
206 
207 
208 // --------------------------------------------------------------------------------------------------------------------
209 
210 template <UST t_alignment, typename T_Type>
211 [[nodiscard]] inline auto misalignment(const volatile T_Type* pointer) noexcept -> UST
212 {
213  return pointer_to_integer(pointer) % t_alignment;
214 }
215 
216 
217 // --------------------------------------------------------------------------------------------------------------------
218 
219 template <typename T_Type>
220 [[nodiscard]] inline auto misalignment(const volatile T_Type* pointer, UST alignment) noexcept -> UST
221 {
222  return pointer_to_integer(pointer) % alignment;
223 }
224 
225 
226 // --------------------------------------------------------------------------------------------------------------------
227 
228 template <typename T_Type>
229 [[nodiscard]] inline auto pointer_to_integer(const volatile T_Type* pointer) noexcept -> UPT
230 {
231  // https://stackoverflow.com/a/26586211
232  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) intentional, see link above
233  return reinterpret_cast<UPT>(pointer);
234 }
235 
236 
237 } // namespace mjolnir
Defines the fundamental data types.
std::uintptr_t UPT
Unsigned integer type that has the same size as a pointer.
Definition: fundamental_types.h:30
std::size_t UST
Unsigned integer type that is returned by sizeof operations.
Definition: fundamental_types.h:29
auto pointer_to_integer(const volatile T_Type *pointer) noexcept -> UPT
Turn a pointer into an integer.
Definition: pointer_operations.h:229
auto misalignment(const volatile T_Type *pointer, UST alignment) noexcept -> UST
Calculate the misalignment of a pointer.
Definition: pointer_operations.h:220
auto is_aligned(const volatile T_Type *pointer, UST alignment) noexcept -> bool
Check if a passed pointer is aligned.
Definition: pointer_operations.h:201
auto integer_to_pointer(UPT integer) noexcept -> T_Type *
Turn an integer into a pointer of the chosen type.
Definition: pointer_operations.h:181