Lazuli
Macros | Typedefs
common.h File Reference

Basic type definitions and useful macros. More...

#include <stdint.h>

Go to the source code of this file.

Macros

#define _EXTERN_C_DECL_BEGIN
 Open C++ header file declarations.
 
#define _EXTERN_C_DECL_END
 Close C++ header file declarations.
 
#define UNUSED(X)   ((void)(X))
 Tell the compiler that the variable X is left unsed. More...
 
#define STATIC_ASSERT(C, M)   extern char _static_assertion_##M[(C) ? 1 : -1]
 Perform an assertion at compile time. More...
 
#define NULL   ((void *)0)
 NULL pointer.
 
#define true   ((bool)1)
 Boolean TRUE constant value.
 
#define false   ((bool)0)
 Boolean FALSE constant value.
 
#define ALLOW_ARITHM(X)   ((uint8_t *)(X))
 Allow arithmetic on a void pointer. More...
 
#define STATIC_CHECK_TYPE(V, T)   UNUSED(1 ? (T*)0 : &(V))
 Check that the lvalue V is of type T at compile time. More...
 
#define SET_BITS(V, T, X)
 Set the corresponding bits of X in the value V of type T. More...
 
#define CLEAR_BITS(V, T, X)
 Clear the corresponding bits of X in the value V of type T. More...
 
#define POSITION(X)   (1U << (X))
 Define a constant bit at position X, starting from index 0. More...
 
#define INDIRECT_T(X, T)   ((volatile T *)(X))
 Define an indirect read/write variable at an absolute address. More...
 
#define INDIRECT(X)   INDIRECT_T(X, uint8_t)
 Define an indirect read/write register at an absolute address. More...
 
#define DIRECT_T(X, T)   (*INDIRECT_T(X, T))
 Define a direct read/write variable at an absolute address. More...
 
#define DIRECT(X)   DIRECT_T(X, uint8_t)
 Define a direct read/write register at an absolute address. More...
 
#define LO8(X)   ((uint8_t)(X))
 Take the low byte of a 16-bit value. More...
 
#define HI8(X)   LO8((X) >> 8U)
 Take the high byte of a 16-bit value. More...
 
#define MIN(A, B)   (((A) < (B)) ? (A) : (B))
 Obtain the minimum between two integer values. More...
 
#define MAX(A, B)   (((A) < (B)) ? (B) : (A))
 Obtain the maximum between two integer values. More...
 
#define ABS(A)   (((A) < 0) ? (-(A)) : (A))
 Obtain the absolute value of an integer. More...
 
#define OFFSET_OF(M, T)   ((size_t)(&(((T*)0)->M)))
 Get the offset of a member in a structure. More...
 
#define CONTAINER_OF(P, M, T)   ((T*) ((uint8_t*) (1 ? (P) : &(((T*)0)->M)) - OFFSET_OF(M, T)))
 Get a pointer to the structure T containing the member M pointed by P. More...
 
#define NAME_OF(X)   #X
 Convert the name of a symbol to a string. More...
 
#define ELEMENTS_COUNT(X)   ((size_t)(sizeof(X) / sizeof((X)[0])))
 Get the number of elements in a statically initialized array. More...
 
#define DEPENDENCY_ON_MODULE(X)   STATIC_ASSERT(LZ_CONFIG_MODULE_ ## X ## _USED, Module_ ## X ## _must_be_used)
 Perform a static assertion to check if a given module is activated. More...
 

Typedefs

typedef int ptrdiff_t
 Represents the difference between two pointers.
 
typedef uint8_t bool
 Boolean type.
 
typedef uint16_t size_t
 Represents the size of an object.
 
typedef uint8_t u_read_write_atomic_t
 Represents an unsigned integer that can be read and written atomically. More...
 

Detailed Description

Basic type definitions and useful macros.

This file describes basic type definitions and useful macros used in the Lazuli project.

Definition in file common.h.

Macro Definition Documentation

◆ UNUSED

#define UNUSED (   X)    ((void)(X))

Tell the compiler that the variable X is left unsed.

Parameters
XThe unused variable.

Definition at line 51 of file common.h.

◆ STATIC_ASSERT

#define STATIC_ASSERT (   C,
 
)    extern char _static_assertion_##M[(C) ? 1 : -1]

Perform an assertion at compile time.

Won't compile if the assertion is false.

Parameters
CThe condition to check.
MA C-language identifier used as "message/name" for the static assertion. It's used here to display an informative message directly in the source code and compiler output in case of a failure.

Definition at line 63 of file common.h.

◆ ALLOW_ARITHM

#define ALLOW_ARITHM (   X)    ((uint8_t *)(X))

Allow arithmetic on a void pointer.

Arithmetic on void pointers isn't allowed in standard C.

Parameters
XThe void pointer on which to perform arithmetic.

Definition at line 116 of file common.h.

◆ STATIC_CHECK_TYPE

#define STATIC_CHECK_TYPE (   V,
 
)    UNUSED(1 ? (T*)0 : &(V))

Check that the lvalue V is of type T at compile time.

Parameters
VThe lvalue to check.
TThe supposed type of the lvalue.

Definition at line 134 of file common.h.

◆ SET_BITS

#define SET_BITS (   V,
  T,
 
)
Value:
do { \
STATIC_CHECK_TYPE(V, T); \
(V) |= (T)(X); \
} while (0)

Set the corresponding bits of X in the value V of type T.

Parameters
VThe value to set bits.
TThe type of the value V.
XA value containing the bits (at logical 1) to set.

Definition at line 143 of file common.h.

◆ CLEAR_BITS

#define CLEAR_BITS (   V,
  T,
 
)
Value:
do { \
STATIC_CHECK_TYPE(V, T); \
(V) &= (T)(~(X)); \
} while (0)

Clear the corresponding bits of X in the value V of type T.

Parameters
VThe value to set bits.
TThe type of the value V.
XA value containing the bits (at logical 1) to clear.

Definition at line 156 of file common.h.

◆ POSITION

#define POSITION (   X)    (1U << (X))

Define a constant bit at position X, starting from index 0.

Parameters
XAn unsigned integer constant representing the position of the bit, starting from index 0.
Warning
The constant must be specified using an unsigned integer litteral in uppercase. e.g. POSITION(2U) This is to make static analyzers not complain about using a shift operator with an unsigned value.

Definition at line 174 of file common.h.

◆ INDIRECT_T

#define INDIRECT_T (   X,
 
)    ((volatile T *)(X))

Define an indirect read/write variable at an absolute address.

i.e. A variable that is accessed through a pointer.

Parameters
XThe address of the variable.
TThe type of the variable.

Definition at line 183 of file common.h.

◆ INDIRECT

#define INDIRECT (   X)    INDIRECT_T(X, uint8_t)

Define an indirect read/write register at an absolute address.

i.e. A register that is accessed through a pointer.

Parameters
XThe address of the register.

Definition at line 191 of file common.h.

◆ DIRECT_T

#define DIRECT_T (   X,
 
)    (*INDIRECT_T(X, T))

Define a direct read/write variable at an absolute address.

Parameters
XThe address of the variable.
TThe type of the variable.

Definition at line 199 of file common.h.

◆ DIRECT

#define DIRECT (   X)    DIRECT_T(X, uint8_t)

Define a direct read/write register at an absolute address.

Parameters
XThe address of the register.

Definition at line 206 of file common.h.

◆ LO8

#define LO8 (   X)    ((uint8_t)(X))

Take the low byte of a 16-bit value.

Parameters
XThe 16-bit value.

Definition at line 213 of file common.h.

◆ HI8

#define HI8 (   X)    LO8((X) >> 8U)

Take the high byte of a 16-bit value.

Parameters
XThe 16-bit value.

Definition at line 220 of file common.h.

◆ MIN

#define MIN (   A,
 
)    (((A) < (B)) ? (A) : (B))

Obtain the minimum between two integer values.

Parameters
AThe first value.
BThe second value.

Definition at line 228 of file common.h.

◆ MAX

#define MAX (   A,
 
)    (((A) < (B)) ? (B) : (A))

Obtain the maximum between two integer values.

Parameters
AThe first value.
BThe second value.

Definition at line 236 of file common.h.

◆ ABS

#define ABS (   A)    (((A) < 0) ? (-(A)) : (A))

Obtain the absolute value of an integer.

Parameters
AThe integer value.

Definition at line 243 of file common.h.

◆ OFFSET_OF

#define OFFSET_OF (   M,
 
)    ((size_t)(&(((T*)0)->M)))

Get the offset of a member in a structure.

Parameters
MThe name of the member.
TThe type of the structure.

Definition at line 251 of file common.h.

◆ CONTAINER_OF

#define CONTAINER_OF (   P,
  M,
 
)    ((T*) ((uint8_t*) (1 ? (P) : &(((T*)0)->M)) - OFFSET_OF(M, T)))

Get a pointer to the structure T containing the member M pointed by P.

May not compile if P doesn't point to the type defined by M.

Parameters
PThe pointer to the member.
MThe name of the member.
TThe type of the structure.

Definition at line 263 of file common.h.

◆ NAME_OF

#define NAME_OF (   X)    #X

Convert the name of a symbol to a string.

Parameters
XThe symbol name to convert.

Definition at line 271 of file common.h.

◆ ELEMENTS_COUNT

#define ELEMENTS_COUNT (   X)    ((size_t)(sizeof(X) / sizeof((X)[0])))

Get the number of elements in a statically initialized array.

Parameters
XThe array variable.

Definition at line 278 of file common.h.

◆ DEPENDENCY_ON_MODULE

#define DEPENDENCY_ON_MODULE (   X)    STATIC_ASSERT(LZ_CONFIG_MODULE_ ## X ## _USED, Module_ ## X ## _must_be_used)

Perform a static assertion to check if a given module is activated.

Parameters
XThe module name, in uppercase.

Definition at line 285 of file common.h.

Typedef Documentation

◆ u_read_write_atomic_t

Represents an unsigned integer that can be read and written atomically.

This type is the equivalent of libc's sig_atomic_t (signal.h).

Use this type with volatile.

Definition at line 107 of file common.h.