unaligned.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Kaspar Schleiser <kaspar@schleiser.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
37 #ifndef UNALIGNED_H
38 #define UNALIGNED_H
39 
40 #include <stdint.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
47 typedef struct __attribute__((packed)) {
48  uint16_t val;
49 } uint16_una_t;
50 
52 typedef struct __attribute__((packed)) {
53  uint32_t val;
54 } uint32_una_t;
55 
57 typedef struct __attribute__((packed)) {
58  uint64_t val;
59 } uint64_una_t;
60 
68 static inline uint16_t unaligned_get_u16(const void *ptr)
69 {
70  const uint16_una_t *tmp = (const uint16_una_t *)ptr;
71  return tmp->val;
72 }
73 
81 static inline uint32_t unaligned_get_u32(const void *ptr)
82 {
83  const uint32_una_t *tmp = (const uint32_una_t *)ptr;
84  return tmp->val;
85 }
86 
94 static inline uint64_t unaligned_get_u64(const void *ptr)
95 {
96  const uint64_una_t *tmp = (const uint64_una_t *)ptr;
97  return tmp->val;
98 }
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
105 #endif /* UNALIGNED_H */
uint64_una_t
Unaligned access helper struct (uint64_t version)
Definition: unaligned.h:57
unaligned_get_u64
static uint64_t unaligned_get_u64(const void *ptr)
Get uint64_t from possibly unaligned pointer.
Definition: unaligned.h:94
uint32_una_t::val
uint32_t val
value
Definition: unaligned.h:53
unaligned_get_u16
static uint16_t unaligned_get_u16(const void *ptr)
Get uint16_t from possibly unaligned pointer.
Definition: unaligned.h:68
uint64_una_t::val
uint64_t val
value
Definition: unaligned.h:58
unaligned_get_u32
static uint32_t unaligned_get_u32(const void *ptr)
Get uint32_t from possibly unaligned pointer.
Definition: unaligned.h:81
uint16_una_t
Unaligned access helper struct (uint16_t version)
Definition: unaligned.h:47
uint32_una_t
Unaligned access helper struct (uint32_t version)
Definition: unaligned.h:52
uint16_una_t::val
uint16_t val
value
Definition: unaligned.h:48