Lazuli
Loading...
Searching...
No Matches
Macros | Typedefs
common.h File Reference

Basic type definitions and useful macros. More...

#include <stdint.h>
#include <Lazuli/common_asm.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 unused.
 
#define STATIC_ASSERT(C, M)   extern char _static_assertion_##M[(C) ? 1 : -1]
 Perform an assertion at compile time.
 
#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.
 
#define STATIC_CHECK_TYPE(V, T)   UNUSED(1 ? (T*)0 : &(V))
 Check that the lvalue V is of type T at compile time.
 
#define SET_BITS(V, T, X)
 Set the corresponding bits of X in the value V of type T.
 
#define CLEAR_BITS(V, T, X)
 Clear the corresponding bits of X in the value V of type T.
 
#define INDIRECT_T(X, T)   ((volatile T *)(X))
 Define an indirect read/write variable at an absolute address.
 
#define INDIRECT(X)   INDIRECT_T(X, uint8_t)
 Define an indirect read/write register at an absolute address.
 
#define DIRECT_T(X, T)   (*INDIRECT_T(X, T))
 Define a direct read/write variable at an absolute address.
 
#define DIRECT(X)   DIRECT_T(X, uint8_t)
 Define a direct read/write register at an absolute address.
 
#define LO8(X)   ((uint8_t)(X))
 Take the low byte of a 16-bit value.
 
#define HI8(X)   LO8((X) >> 8U)
 Take the high byte of a 16-bit value.
 
#define MIN(A, B)   (((A) < (B)) ? (A) : (B))
 Obtain the minimum between two integer values.
 
#define MAX(A, B)   (((A) < (B)) ? (B) : (A))
 Obtain the maximum between two integer values.
 
#define ABS(A)   (((A) < 0) ? (-(A)) : (A))
 Obtain the absolute value of an integer.
 
#define OFFSET_OF(M, T)    ((size_t)(&(((T*)0)->M)))
 Get the offset of a member in a structure.
 
#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.
 
#define NAME_OF(X)   #X
 Convert the name of a symbol to a string.
 
#define ELEMENTS_COUNT(X)   ((size_t)(sizeof(X) / sizeof((X)[0])))
 Get the number of elements in a statically initialized array.
 
#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.
 

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.
 

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

◆ _EXTERN_C_DECL_BEGIN

#define _EXTERN_C_DECL_BEGIN

Open C++ header file declarations.

Definition at line 39 of file common.h.

◆ _EXTERN_C_DECL_END

#define _EXTERN_C_DECL_END

Close C++ header file declarations.

Definition at line 44 of file common.h.

◆ UNUSED

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

Tell the compiler that the variable X is left unused.

Parameters
XThe unused variable.

Definition at line 53 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 65 of file common.h.

◆ NULL

#define NULL   ((void *)0)

NULL pointer.

Definition at line 70 of file common.h.

◆ true

#define true   ((bool)1)

Boolean TRUE constant value.

Definition at line 89 of file common.h.

◆ false

#define false   ((bool)0)

Boolean FALSE constant value.

Definition at line 94 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 118 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 136 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 145 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 158 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 171 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 179 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 187 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 194 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 201 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 208 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 216 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 224 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 231 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 239 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 251 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 259 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 266 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 273 of file common.h.

Typedef Documentation

◆ ptrdiff_t

typedef int ptrdiff_t

Represents the difference between two pointers.

Definition at line 75 of file common.h.

◆ bool

typedef uint8_t bool

Boolean type.

Definition at line 80 of file common.h.

◆ size_t

typedef uint16_t size_t

Represents the size of an object.

Definition at line 100 of file common.h.

◆ 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 109 of file common.h.