unaligned memory access methods

Provides functions for safe unaligned memory accesses. More...

Detailed Description

Provides functions for safe unaligned memory accesses.

This header provides functions to read values from pointers that are not necessarily aligned to the type's alignment requirements.

E.g.,

uint16_t *foo = 0x123;
printf("%u\n", *foo);

... might cause an unaligned access, if uint16_t is usually aligned at 2-byte-boundaries, as foo has an odd address.

The current implementation casts a pointer to a packed struct, which forces the compiler to deal with possibly unalignedness. Idea taken from linux kernel sources.

Files

file  unaligned.h
 Unaligned but safe memory access functions.
 

Data Structures

struct  uint16_una_t
 Unaligned access helper struct (uint16_t version) More...
 
struct  uint32_una_t
 Unaligned access helper struct (uint32_t version) More...
 
struct  uint64_una_t
 Unaligned access helper struct (uint64_t version) More...
 

Functions

static uint16_t unaligned_get_u16 (const void *ptr)
 Get uint16_t from possibly unaligned pointer. More...
 
static uint32_t unaligned_get_u32 (const void *ptr)
 Get uint32_t from possibly unaligned pointer. More...
 
static uint64_t unaligned_get_u64 (const void *ptr)
 Get uint64_t from possibly unaligned pointer. More...
 

Function Documentation

◆ unaligned_get_u16()

static uint16_t unaligned_get_u16 ( const void *  ptr)
inlinestatic

Get uint16_t from possibly unaligned pointer.

Parameters
[in]ptrpointer to read from
Returns
value read from ptr

Definition at line 68 of file unaligned.h.

◆ unaligned_get_u32()

static uint32_t unaligned_get_u32 ( const void *  ptr)
inlinestatic

Get uint32_t from possibly unaligned pointer.

Parameters
[in]ptrpointer to read from
Returns
value read from ptr

Definition at line 81 of file unaligned.h.

◆ unaligned_get_u64()

static uint64_t unaligned_get_u64 ( const void *  ptr)
inlinestatic

Get uint64_t from possibly unaligned pointer.

Parameters
[in]ptrpointer to read from
Returns
value read from ptr

Definition at line 94 of file unaligned.h.