Go to the documentation of this file.
9 #ifndef EAGINE_MEMORY_ADDRESS_HPP
10 #define EAGINE_MEMORY_ADDRESS_HPP
12 #include "../assert.hpp"
13 #include "../types.hpp"
16 #include <type_traits>
18 namespace eagine::memory {
22 template <
bool IsConst>
26 using pointer = std::conditional_t<IsConst, const void*, void*>;
29 using byte_pointer = std::conditional_t<IsConst, const byte*, byte*>;
55 : _addr(
reinterpret_cast<pointer>(addr)) {}
63 typename = std::enable_if_t<
64 std::is_integral_v<Int> && std::is_convertible_v<Int, std::ptrdiff_t>>>
66 : _addr(that.ptr() + offs) {}
68 template <
bool IsConst2,
typename = std::enable_if_t<IsConst && !IsConst2>>
74 return _addr ==
nullptr;
79 constexpr
explicit operator bool() const noexcept {
86 constexpr
auto ptr() const noexcept {
93 constexpr
auto get() const noexcept {
99 constexpr
explicit operator pointer() const noexcept {
110 std::enable_if_t<!std::is_void_v<T> && (std::is_const_v<T> || !IsConst)>>
111 constexpr
explicit operator T*()
const noexcept {
112 return EAGINE_CONSTEXPR_ASSERT(
113 is_aligned_as<T>(),
static_cast<T*
>(_addr));
119 constexpr
auto value() const noexcept {
120 return reinterpret_cast<std::intptr_t
>(_addr);
140 template <
typename T>
142 return memory::is_aligned_as<T>(
value(), tid);
147 return a.ptr() == b.ptr();
152 return a.ptr() != b.ptr();
157 return a.ptr() < b.ptr();
162 return a.ptr() <= b.ptr();
167 return a.ptr() > b.ptr();
172 return a.ptr() >= b.ptr();
178 return a.ptr() - b.ptr();
207 template <
typename T>
214 static constexpr
auto
216 return addr.misalignment(alignment);
221 static constexpr
auto
228 static constexpr
auto
230 return addr.is_aligned_to(alignment);
235 static constexpr
auto
242 template <
typename T>
243 static constexpr
auto
248 static constexpr
auto
250 return (misalign ? align - misalign : 0);
253 template <
bool IsConst>
254 static constexpr
auto align_up_by(
255 basic_address<IsConst> addr,
258 return EAGINE_CONSTEXPR_ASSERT(
259 (offs <= max), basic_address<IsConst>(addr, offs));
265 template <
bool IsConst>
271 addr, align_up_offs(align,
misalignment(addr, align)), max);
274 static constexpr
auto
279 template <
bool IsConst>
280 static constexpr
auto align_down_by(
281 basic_address<IsConst> addr,
284 return EAGINE_CONSTEXPR_ASSERT(
285 (offs <= max), basic_address<IsConst>(addr, -offs));
291 template <
bool IsConst>
297 return align_down_by(
298 addr, align_down_offs(align,
misalignment(addr, align)), max);
303 template <
bool IsConst>
304 static constexpr
auto
319 template <
typename T>
326 if(align < span_align_of<T>()) {
327 align = span_align_of<T>();
330 return static_cast<T*
>(
align_up(addr, align, max));
336 template <
typename T>
342 if(align < span_align_of<T>()) {
343 align = span_align_of<T>();
346 return static_cast<T*
>(
align_down(addr, align, max));
351 #endif // EAGINE_MEMORY_ADDRESS_HPP
constexpr basic_address(pointer addr) noexcept
Construction from untyped pointer type.
Definition: address.hpp:58
static constexpr auto align_up_to(basic_address< std::is_const_v< T >> addr, type_identity< T >={}, span_size_t align=span_align_of< T >(), span_size_t max=span_size_of< T >()) noexcept
Aligns a memory address up to the required alignment of type T.
Definition: address.hpp:320
Class for handling memory addresses as integer values.
Definition: address.hpp:23
constexpr basic_address(std::intptr_t addr) noexcept
Construction from signed integer type.
Definition: address.hpp:54
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
constexpr friend auto operator<=(basic_address a, basic_address b) noexcept
Less-equal comparison.
Definition: address.hpp:161
constexpr auto is_aligned_as(type_identity< T > tid={}) const noexcept
Indicates if this address is aligned to the alignment or type T.
Definition: address.hpp:141
static constexpr auto as_address(T *addr) noexcept
Casts a pointer to basic_address.
Definition: address.hpp:208
constexpr auto get() const noexcept
Returns the untyped pointer for this address.
Definition: address.hpp:93
static constexpr auto is_aligned_as(const_address addr, type_identity< T > tid={}) noexcept
Indicates if a memory address aligned as the specified type T.
Definition: address.hpp:244
constexpr friend auto operator+(basic_address a, std::ptrdiff_t o) noexcept -> basic_address
Byte offset addition.
Definition: address.hpp:182
constexpr friend auto operator<(basic_address a, basic_address b) noexcept
Less-than comparison.
Definition: address.hpp:156
constexpr auto misalignment(span_size_t alignment) const noexcept
Returns the misalignment of this address to the specified alignment.
Definition: address.hpp:126
constexpr auto value() const noexcept
Returns this address as an signed integer.
Definition: address.hpp:119
constexpr friend auto operator-(basic_address a, std::ptrdiff_t o) noexcept -> basic_address
Byte offset subtraction.
Definition: address.hpp:188
std::conditional_t< IsConst, const void *, void * > pointer
The associated untyped pointer type.
Definition: address.hpp:26
constexpr friend auto operator>=(basic_address a, basic_address b) noexcept
Greater-equal comparison.
Definition: address.hpp:171
static constexpr auto is_aligned_to(const void *ptr, span_size_t alignment) noexcept
Indicates if a pointer is aligned to the specified alignment.
Definition: address.hpp:236
static constexpr auto align_up(basic_address< IsConst > addr, span_size_t align, span_size_t max) noexcept -> basic_address< IsConst >
Aligns a memory address up to the specified alignment.
Definition: address.hpp:266
basic_address< true > const_address
Type alias for const memory address values.
Definition: address.hpp:199
constexpr basic_address() noexcept=default
Default constructor.
constexpr friend auto operator==(basic_address a, basic_address b) noexcept
Equality comparison.
Definition: address.hpp:146
static constexpr auto align_down_to(basic_address< std::is_const_v< T >> addr, type_identity< T >={}, span_size_t align=span_align_of< T >(), span_size_t max=span_size_of< T >()) noexcept
Aligns a memory address down to the required alignment of type T.
Definition: address.hpp:337
static constexpr auto misalignment(const void *ptr, span_size_t alignment) noexcept
Returns the misalignment of a pointer to the specified alignment.
Definition: address.hpp:222
static auto align_down(const byte *ptr, span_size_t align) -> const byte *
Aligns a byte pointer down to the specified alignment.
Definition: address.hpp:311
Template type used mostly for function type-tag dispatching.
Definition: type_identity.hpp:19
constexpr friend auto operator-(basic_address a, basic_address b) noexcept -> std::ptrdiff_t
Byte difference between two addresses.
Definition: address.hpp:176
constexpr friend auto operator>(basic_address a, basic_address b) noexcept
Greater-than comparison.
Definition: address.hpp:166
std::conditional_t< IsConst, const byte *, byte * > byte_pointer
The associated byte pointer type.
Definition: address.hpp:29
constexpr auto is_aligned_to(span_size_t alignment) const noexcept
Indicates if this address is aligned to the specified alignment.
Definition: address.hpp:133
constexpr auto is_null() const noexcept
Indicates if the stored address is null.
Definition: address.hpp:73
constexpr friend auto operator!=(basic_address a, basic_address b) noexcept
Non-equality comparison.
Definition: address.hpp:151
constexpr auto ptr() const noexcept
Returns the byte pointer for this address.
Definition: address.hpp:86