Memory Read & Write
STX provides four fundamental functions for typed memory access: copy-based operations usingstd::memcpy for well-defined behavior, and raw pointer operations for performance-critical scenarios.
Copy-Based Read
read()
Safely reads a value from memory usingstd::memcpy, providing well-defined behavior under the C++ object model.
The type to read. Must satisfy
std::is_trivially_copyable_v<Type>.Address-like type (satisfies
address_like concept). Can be raw pointers, va_t, rva_t, or other address types.Base address to read from.
Offset from base address in bytes.
Type read from base + off.
Characteristics
- Uses
std::memcpyfor defined behavior - Safe for unaligned memory access
- Compliant with strict aliasing rules
- Zero runtime overhead when inlined
- Requires trivially copyable types
When to Use
| Scenario | Recommended |
|---|---|
| Parsing binary structures | ✓ Yes |
| Packed/unaligned memory | ✓ Yes |
| Portable code | ✓ Yes |
| Memory-mapped files | ✓ Yes |
| Known-aligned buffers | Consider read_raw() |
Example
Raw Pointer Read
read_raw()
Directly dereferences a pointer for maximum performance. Requires proper alignment and careful usage.The type to read. Must satisfy
std::is_trivially_copyable_v<Type>.Base address to read from.
Offset from base address in bytes.
Type read from base + off via direct dereference.
Characteristics
- Direct pointer dereference (no
memcpy) - Requires properly aligned memory
- May violate strict aliasing if misused
- Maximum performance in tight loops
- Requires trivially copyable types
When to Use
| Scenario | Recommended |
|---|---|
| Known-aligned internal buffers | ✓ Yes |
| Performance-critical tight loops | ✓ Yes |
| Memory-mapped files | ✗ No |
| Arbitrary external memory | ✗ No |
Example
Copy-Based Write
write()
Safely writes a value to memory usingstd::memcpy, providing well-defined behavior.
The type to write. Must satisfy
std::is_trivially_copyable_v<Type>.Address-like type (satisfies
address_like concept).Base address to write to.
Offset from base address in bytes.
Value to write to memory.
Characteristics
- Uses
std::memcpyfor defined behavior - Safe for unaligned memory access
- Portable across platforms
- Zero runtime overhead when inlined
- Requires trivially copyable types
Example
Raw Pointer Write
write_raw()
Directly writes through a pointer for maximum performance. Requires proper alignment.The type to write. Must satisfy
std::is_trivially_copyable_v<Type>.Base address to write to.
Offset from base address in bytes.
Value to write to memory.
Characteristics
- Direct assignment through cast pointer
- Requires properly aligned memory
- No aliasing guarantees
- Maximum performance in controlled regions
- Requires trivially copyable types
Example
Comparison: Copy vs. Raw
| Feature | read() / write() | read_raw() / write_raw() |
|---|---|---|
| Implementation | std::memcpy | Direct dereference |
| Alignment requirement | None | Must be aligned |
| Strict aliasing | Safe | Potential issues |
| Unaligned access | ✓ Safe | ✗ Undefined behavior |
| Performance | Excellent (optimized away) | Maximum |
| Portability | High | Medium |
| Use case | General purpose | Performance-critical |
Safety Considerations
| Responsibility | Owner |
|---|---|
| Memory validity | Caller |
| Alignment correctness | Caller (raw operations) |
| Concurrency safety | Caller |
| Bounds checking | Caller |
Performance Notes
- Modern compilers optimize
std::memcpyto single instructions for aligned access - The
STX_FORCE_INLINEattribute ensures zero abstraction overhead - For small, known-aligned types,
read()andread_raw()generate identical code - Use raw operations only when profiling shows measurable benefit
Integration with Strong Types
All functions work seamlessly with STX strong types:Casting Utilities
STX provides three inline casting utilities for common type conversions in low-level code.rcast()
Wrapper forreinterpret_cast with shorter syntax.
Target type to cast to.
Value to cast.
scast()
Wrapper forstatic_cast with shorter syntax and constexpr support.
Target type to cast to.
Value to cast.
bcast()
Wrapper forstd::bit_cast with shorter syntax and constexpr support.
Target type. Must be same size as
From and both must be trivially copyable.Source type.
Value to bit-cast.
bcast provides safe type-punning with compile-time size checks, making it the preferred alternative to union-based or pointer-based type reinterpretation.Design Notes
- All casting utilities are marked
STX_FORCE_INLINEfor zero overhead scastandbcastareconstexpr, enabling compile-time conversions- These utilities maintain the same semantics as their standard library counterparts while providing shorter syntax for low-level code