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 
9 #pragma once
10 
39 #include <stdint.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
46 typedef struct __attribute__((packed)) {
47  uint16_t val;
48 } uint16_una_t;
49 
51 typedef struct __attribute__((packed)) {
52  uint32_t val;
53 } uint32_una_t;
54 
56 typedef struct __attribute__((packed)) {
57  uint64_t val;
58 } uint64_una_t;
59 
67 static inline uint16_t unaligned_get_u16(const void *ptr)
68 {
69  const uint16_una_t *tmp = (const uint16_una_t *)ptr;
70  return tmp->val;
71 }
72 
80 static inline uint32_t unaligned_get_u32(const void *ptr)
81 {
82  const uint32_una_t *tmp = (const uint32_una_t *)ptr;
83  return tmp->val;
84 }
85 
93 static inline uint64_t unaligned_get_u64(const void *ptr)
94 {
95  const uint64_una_t *tmp = (const uint64_una_t *)ptr;
96  return tmp->val;
97 }
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
static uint16_t unaligned_get_u16(const void *ptr)
Get uint16_t from possibly unaligned pointer.
Definition: unaligned.h:67
static uint64_t unaligned_get_u64(const void *ptr)
Get uint64_t from possibly unaligned pointer.
Definition: unaligned.h:93
static uint32_t unaligned_get_u32(const void *ptr)
Get uint32_t from possibly unaligned pointer.
Definition: unaligned.h:80
Unaligned access helper struct (uint16_t version)
Definition: unaligned.h:46
uint16_t val
value
Definition: unaligned.h:47
Unaligned access helper struct (uint32_t version)
Definition: unaligned.h:51
uint32_t val
value
Definition: unaligned.h:52
Unaligned access helper struct (uint64_t version)
Definition: unaligned.h:56
uint64_t val
value
Definition: unaligned.h:57