Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
Public Attributes | List of all members
jau::packed_t< T > Struct Template Reference

Support aligned memory transfer from and to potentially unaligned memory. More...

#include <packed_attribute.hpp>

Collaboration diagram for jau::packed_t< T >:

Public Attributes

store
 

Detailed Description

template<typename T>
struct jau::packed_t< T >

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.

Member Data Documentation

◆ store

template<typename T >
T jau::packed_t< T >::store

Definition at line 116 of file packed_attribute.hpp.


The documentation for this struct was generated from the following file: