Lazuli
common.h
Go to the documentation of this file.
1 /*
2  * SPDX-License-Identifier: GPL-3.0-only
3  * This file is part of Lazuli.
4  */
5 
15 #ifndef LAZULI_COMMON_H
16 #define LAZULI_COMMON_H
17 
18 #include <stdint.h>
19 
20 #ifdef __cplusplus
21 
25 #define _EXTERN_C_DECL_BEGIN extern "C" {
26 
30 #define _EXTERN_C_DECL_END }
31 
32 #else /* __cplusplus */
33 
37 #define _EXTERN_C_DECL_BEGIN
38 
42 #define _EXTERN_C_DECL_END
43 
44 #endif /* __cplusplus */
45 
51 #define UNUSED(X) ((void)(X))
52 
63 #define STATIC_ASSERT(C, M) extern char _static_assertion_##M[(C) ? 1 : -1]
64 
68 #define NULL ((void *)0)
69 
73 typedef int ptrdiff_t;
74 
78 typedef uint8_t bool;
79 
81 STATIC_ASSERT(sizeof(bool) == 1, bool_MUST_be_1_byte_long);
87 #define true ((bool)1)
88 
92 #define false ((bool)0)
93 
94 /* TODO: Not portable!!! */
98 typedef uint16_t size_t;
99 
100 /* TODO: Arch specific */
108 
116 #define ALLOW_ARITHM(X) ((uint8_t *)(X))
117 
124 STATIC_ASSERT(sizeof(uint8_t) == 1,
125  uint8_t_MUST_be_1_byte_long_to_allow_arithmetic_on_void_pointers);
134 #define STATIC_CHECK_TYPE(V, T) UNUSED(1 ? (T*)0 : &(V))
135 
143 #define SET_BITS(V, T, X) \
144  do { \
145  STATIC_CHECK_TYPE(V, T); \
146  (V) |= (T)(X); \
147  } while (0)
148 
156 #define CLEAR_BITS(V, T, X) \
157  do { \
158  STATIC_CHECK_TYPE(V, T); \
159  (V) &= (T)(~(X)); \
160  } while (0)
161 
174 #define POSITION(X) (1U << (X))
175 
183 #define INDIRECT_T(X, T) ((volatile T *)(X))
184 
191 #define INDIRECT(X) INDIRECT_T(X, uint8_t)
192 
199 #define DIRECT_T(X, T) (*INDIRECT_T(X, T))
200 
206 #define DIRECT(X) DIRECT_T(X, uint8_t)
207 
213 #define LO8(X) ((uint8_t)(X))
214 
220 #define HI8(X) LO8((X) >> 8U)
221 
228 #define MIN(A, B) (((A) < (B)) ? (A) : (B))
229 
236 #define MAX(A, B) (((A) < (B)) ? (B) : (A))
237 
243 #define ABS(A) (((A) < 0) ? (-(A)) : (A))
244 
251 #define OFFSET_OF(M, T) \
252  ((size_t)(&(((T*)0)->M)))
253 
263 #define CONTAINER_OF(P, M, T) \
264  ((T*) ((uint8_t*) (1 ? (P) : &(((T*)0)->M)) - OFFSET_OF(M, T)))
265 
271 #define NAME_OF(X) #X
272 
278 #define ELEMENTS_COUNT(X) ((size_t)(sizeof(X) / sizeof((X)[0])))
279 
285 #define DEPENDENCY_ON_MODULE(X) \
286  STATIC_ASSERT(LZ_CONFIG_MODULE_ ## X ## _USED, Module_ ## X ## _must_be_used)
287 
288 #endif /* LAZULI_COMMON_H */
#define STATIC_ASSERT(C, M)
Perform an assertion at compile time.
Definition: common.h:63
unsigned int uint16_t
Represents a unsigned integer type with width of exactly 16 bits.
Definition: stdint.h:94
int ptrdiff_t
Represents the difference between two pointers.
Definition: common.h:73
uint8_t bool
Boolean type.
Definition: common.h:78
unsigned char uint8_t
Represents a unsigned integer type with width of exactly 8 bits.
Definition: stdint.h:89
uint16_t size_t
Represents the size of an object.
Definition: common.h:98
uint8_t u_read_write_atomic_t
Represents an unsigned integer that can be read and written atomically.
Definition: common.h:107