microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
compat32.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
10#include "address_space.hpp"
11#include <cstdint>
12#include <type_traits>
13
14namespace microfmt {
15
16// ============================================================================
17// 32-bit Compatibility Pointer Primitive
18// ============================================================================
19
24template <typename T> class alignas(uint32_t) compat32_ptr {
25public:
27 using element_type = T;
28
30 constexpr compat32_ptr() noexcept : raw_addr_(0) {}
32 explicit constexpr compat32_ptr(std::nullptr_t) noexcept : raw_addr_(0) {}
34 constexpr explicit compat32_ptr(uint32_t addr) noexcept : raw_addr_(addr) {}
35
40 [[nodiscard]] constexpr uint32_t raw_value() const noexcept { return raw_addr_; }
45 [[nodiscard]] constexpr uintptr_t to_uintptr() const noexcept { return static_cast<uintptr_t>(raw_addr_); }
50 [[nodiscard]] constexpr bool is_null() const noexcept { return raw_addr_ == 0; }
55 [[nodiscard]] constexpr explicit operator bool() const noexcept { return raw_addr_ != 0; }
56
58 constexpr bool operator==(const compat32_ptr &other) const noexcept { return raw_addr_ == other.raw_addr_; }
60 constexpr bool operator!=(const compat32_ptr &other) const noexcept { return raw_addr_ != other.raw_addr_; }
62 constexpr bool operator==(std::nullptr_t) const noexcept { return raw_addr_ == 0; }
64 constexpr bool operator!=(std::nullptr_t) const noexcept { return raw_addr_ != 0; }
65
66private:
68 uint32_t raw_addr_{0};
69};
70
71static_assert(sizeof(compat32_ptr<void>) == 4, "compat32_ptr must be exactly 4 bytes");
72static_assert(alignof(compat32_ptr<void>) == 4, "compat32_ptr must be 4-byte aligned");
73
74// ============================================================================
75// Formatter for compat32_ptr<T>
76// ============================================================================
77
85template <typename T> struct formatter<compat32_ptr<T>> {
89 constexpr void parse(format_parse_context &) noexcept {}
90
96 void format(const compat32_ptr<T> &ptr, const sink &out) const noexcept {
97 if (ptr.is_null()) {
98 out.write("(null)");
99 } else {
100 microfmt::format_to(out, "{:#010x}", ptr.raw_value());
101 }
102 }
103};
104
105// ============================================================================
106// Helpers for remote views
107// ============================================================================
108
118template <size_t N>
120 char (&scratch)[N], size_t max_limit = 4096) noexcept {
121 return remote_string_view(ptr.to_uintptr(), space, scratch, max_limit);
122}
123
133template <typename T, size_t N>
134[[nodiscard]] constexpr auto make_remote_ref32(compat32_ptr<T> ptr, address_space_ref space,
135 std::byte (&scratch)[N]) noexcept {
136 return remote_ref<T>(ptr.to_uintptr(), space, scratch);
137}
138
139} // namespace microfmt
Type-erased remote address-space access and remote string/object views.
Type-erased, two-word handle to a remote address space.
Definition address_space.hpp:204
Value-type pointer storing a 32-bit address (for 64-bit hosts).
Definition compat32.hpp:24
constexpr bool operator==(std::nullptr_t) const noexcept
Compares against null.
Definition compat32.hpp:62
T element_type
Referenced element type.
Definition compat32.hpp:27
constexpr bool operator==(const compat32_ptr &other) const noexcept
Compares two pointers for equality.
Definition compat32.hpp:58
constexpr bool operator!=(const compat32_ptr &other) const noexcept
Compares two pointers for inequality.
Definition compat32.hpp:60
constexpr compat32_ptr() noexcept
Constructs a null pointer.
Definition compat32.hpp:30
constexpr uint32_t raw_value() const noexcept
Returns the raw 32-bit address.
Definition compat32.hpp:40
constexpr compat32_ptr(uint32_t addr) noexcept
Constructs a pointer from a 32-bit address.
Definition compat32.hpp:34
constexpr bool is_null() const noexcept
Reports whether the pointer is null.
Definition compat32.hpp:50
constexpr compat32_ptr(std::nullptr_t) noexcept
Constructs a null pointer.
Definition compat32.hpp:32
constexpr bool operator!=(std::nullptr_t) const noexcept
Compares against null.
Definition compat32.hpp:64
constexpr uintptr_t to_uintptr() const noexcept
Widens the address to a host pointer-size integer.
Definition compat32.hpp:45
Definition microfmt.hpp:881
Lazily-loaded reference to a trivially-copyable remote object.
Definition address_space.hpp:577
Streaming view of a remote NUL-terminated string.
Definition address_space.hpp:506
Definition assert.hpp:19
constexpr auto make_remote_ref32(compat32_ptr< T > ptr, address_space_ref space, std::byte(&scratch)[N]) noexcept
Builds a remote_ref over a 32-bit object pointer.
Definition compat32.hpp:134
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
RELOCO_CONSTEXPR20 void format_to(const sink &out, microfmt::string_view fmt, const Args &...args) noexcept
Definition microfmt.hpp:1766
constexpr auto make_remote_string32(compat32_ptr< const char > ptr, address_space_ref space, char(&scratch)[N], size_t max_limit=4096) noexcept
Builds a remote_string_view over a 32-bit string pointer.
Definition compat32.hpp:119
constexpr void parse(format_parse_context &) noexcept
No-op parse.
Definition compat32.hpp:89
void format(const compat32_ptr< T > &ptr, const sink &out) const noexcept
Renders the 32-bit pointer.
Definition compat32.hpp:96
Definition microfmt.hpp:985