Lazuli
Loading...
Searching...
No Matches
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#include <Lazuli/common_asm.h>
21
22#ifdef __cplusplus
23
27#define _EXTERN_C_DECL_BEGIN extern "C" {
28
32#define _EXTERN_C_DECL_END }
33
34#else /* __cplusplus */
35
39#define _EXTERN_C_DECL_BEGIN
40
44#define _EXTERN_C_DECL_END
45
46#endif /* __cplusplus */
47
53#define UNUSED(X) ((void)(X))
54
65#define STATIC_ASSERT(C, M) extern char _static_assertion_##M[(C) ? 1 : -1]
66
70#define NULL ((void *)0)
71
75typedef int ptrdiff_t;
76
80typedef uint8_t bool;
81
83STATIC_ASSERT(sizeof(bool) == 1, bool_MUST_be_1_byte_long);
89#define true ((bool)1)
90
94#define false ((bool)0)
95
96/* TODO: Not portable!!! */
101
102/* TODO: Arch specific */
110
118#define ALLOW_ARITHM(X) ((uint8_t *)(X))
119
126STATIC_ASSERT(sizeof(uint8_t) == 1,
127 uint8_t_MUST_be_1_byte_long_to_allow_arithmetic_on_void_pointers);
136#define STATIC_CHECK_TYPE(V, T) UNUSED(1 ? (T*)0 : &(V))
137
145#define SET_BITS(V, T, X) \
146 do { \
147 STATIC_CHECK_TYPE(V, T); \
148 (V) |= (T)(X); \
149 } while (0)
150
158#define CLEAR_BITS(V, T, X) \
159 do { \
160 STATIC_CHECK_TYPE(V, T); \
161 (V) &= (T)(~(X)); \
162 } while (0)
163
171#define INDIRECT_T(X, T) ((volatile T *)(X))
172
179#define INDIRECT(X) INDIRECT_T(X, uint8_t)
180
187#define DIRECT_T(X, T) (*INDIRECT_T(X, T))
188
194#define DIRECT(X) DIRECT_T(X, uint8_t)
195
201#define LO8(X) ((uint8_t)(X))
202
208#define HI8(X) LO8((X) >> 8U)
209
216#define MIN(A, B) (((A) < (B)) ? (A) : (B))
217
224#define MAX(A, B) (((A) < (B)) ? (B) : (A))
225
231#define ABS(A) (((A) < 0) ? (-(A)) : (A))
232
239#define OFFSET_OF(M, T) \
240 ((size_t)(&(((T*)0)->M)))
241
251#define CONTAINER_OF(P, M, T) \
252 ((T*) ((uint8_t*) (1 ? (P) : &(((T*)0)->M)) - OFFSET_OF(M, T)))
253
259#define NAME_OF(X) #X
260
266#define ELEMENTS_COUNT(X) ((size_t)(sizeof(X) / sizeof((X)[0])))
267
273#define DEPENDENCY_ON_MODULE(X) \
274 STATIC_ASSERT(LZ_CONFIG_MODULE_ ## X ## _USED, Module_ ## X ## _must_be_used)
275
276#endif /* LAZULI_COMMON_H */
unsigned int uint16_t
Represents a unsigned integer type with width of exactly 16 bits.
Definition stdint.h:94
unsigned char uint8_t
Represents a unsigned integer type with width of exactly 8 bits.
Definition stdint.h:89
uint8_t bool
Boolean type.
Definition common.h:80
#define STATIC_ASSERT(C, M)
Perform an assertion at compile time.
Definition common.h:65
uint8_t u_read_write_atomic_t
Represents an unsigned integer that can be read and written atomically.
Definition common.h:109
uint16_t size_t
Represents the size of an object.
Definition common.h:100
int ptrdiff_t
Represents the difference between two pointers.
Definition common.h:75
Basic type definitions and useful macros usable in ASM source files.