unaligned.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2019 Kaspar Schleiser <kaspar@schleiser.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
36 #include <stdint.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
43 typedef struct __attribute__((packed)) {
44  uint16_t val;
45 } uint16_una_t;
46 
48 typedef struct __attribute__((packed)) {
49  uint32_t val;
50 } uint32_una_t;
51 
53 typedef struct __attribute__((packed)) {
54  uint64_t val;
55 } uint64_una_t;
56 
64 static inline uint16_t unaligned_get_u16(const void *ptr)
65 {
66  const uint16_una_t *tmp = (const uint16_una_t *)ptr;
67  return tmp->val;
68 }
69 
77 static inline uint32_t unaligned_get_u32(const void *ptr)
78 {
79  const uint32_una_t *tmp = (const uint32_una_t *)ptr;
80  return tmp->val;
81 }
82 
90 static inline uint64_t unaligned_get_u64(const void *ptr)
91 {
92  const uint64_una_t *tmp = (const uint64_una_t *)ptr;
93  return tmp->val;
94 }
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
static uint16_t unaligned_get_u16(const void *ptr)
Get uint16_t from possibly unaligned pointer.
Definition: unaligned.h:64
static uint64_t unaligned_get_u64(const void *ptr)
Get uint64_t from possibly unaligned pointer.
Definition: unaligned.h:90
static uint32_t unaligned_get_u32(const void *ptr)
Get uint32_t from possibly unaligned pointer.
Definition: unaligned.h:77
Unaligned access helper struct (uint16_t version)
Definition: unaligned.h:43
uint16_t val
value
Definition: unaligned.h:44
Unaligned access helper struct (uint32_t version)
Definition: unaligned.h:48
uint32_t val
value
Definition: unaligned.h:49
Unaligned access helper struct (uint64_t version)
Definition: unaligned.h:53
uint64_t val
value
Definition: unaligned.h:54