jaulib v1.3.0
Jau Support Library (C++, Java, ..)
|
Support aligned memory transfer from and to potentially unaligned memory. More...
#include <packed_attribute.hpp>
Public Attributes | |
T | store |
Support aligned memory transfer from and to potentially unaligned memory.
This template causes little to no runtime costs.
A casted data pointer to packed_t<T>*
is similar to 'T * p = (T *) buffer', having uint8_t * buffer
. However, packed_t<T>*
doesn't have any intrinsic alignment corrections due to its used __attribute__((__packed__))
.
packed_t is used in Byte Utilities.
Background for using packed_t:
Due to the potentially unaligned memory address of buffer
, we can't just directly use pointer arithmetic like:
// return uint16_t from memory return *( (uint16_t *) ( buffer ) ); // store uint16_t to memory *( (uint16_t *) ( buffer ) ) = v;
The universal alternative using memcpy()
is costly:
// return uint16_t from memory memcpy(&v, buffer, sizeof(v)); return v; // store uint16_t to memory memcpy(buffer, &v, sizeof(v));
Solution is to use the free of costs high performance compiler magic 'struct attribute((packed))'.
The offset memory is pointer_cast() into the desired packed_t type and its packed_t::store accessed thereafter:
// return uint16_t from memory return pointer_cast<const packed_t<uint16_t>*>( buffer )->store; // store uint16_t to memory pointer_cast<packed_t<uint16_t>*>( buffer )->store = v;
Definition at line 116 of file packed_attribute.hpp.
T jau::packed_t< T >::store |
Definition at line 116 of file packed_attribute.hpp.