Crinit -- Configurable Rootfs Init
common.h File Reference

(2023-11-21, commit: e767693)

Header for common definitions not related to other specific features. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define CRINIT_PARAM_UNUSED(par)
 
#define crinitParamCheck(inputParam, cmpShort, cmpLong)    ((strncmp(inputParam, cmpShort, sizeof(cmpShort)) == 0) || (strncmp(inputParam, cmpLong, sizeof(cmpLong)) == 0))
 
#define crinitIsAbsPath(path)   (((path) != NULL) && ((path)[0] == '/'))
 
#define crinitNumElements(p)   (sizeof(p) / sizeof(*(p)))
 
#define crinitNullCheck(errcode, ...)
 
#define crinitStrtoGenericInteger(resType, str, endptr, base)
 
#define crinitNullify(ptr)
 

Detailed Description

Header for common definitions not related to other specific features.

Macro Definition Documentation

◆ CRINIT_PARAM_UNUSED

#define CRINIT_PARAM_UNUSED (   par)
Value:
do { \
(void)(par); \
} while (0)

Suppress unused parameter warning for variable as per coding guideline [OS_C_UNUSED_010].

May be needed if an external interface is implemented.

Parameters
parUnused variable that should not be warned about.

◆ crinitIsAbsPath

#define crinitIsAbsPath (   path)    (((path) != NULL) && ((path)[0] == '/'))

Check if path is absolute (i.e. starts with '/').

Parameters
pathThe path to check.
Returns
true if path i*s absolute, false otherwise

◆ crinitNullCheck

#define crinitNullCheck (   errcode,
  ... 
)
Value:
do { \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic error \"-Wshadow\""); \
const void *_macroPtrsToCheck[] = {__VA_ARGS__}; \
for (size_t _macroI = 0; _macroI < crinitNumElements(_macroPtrsToCheck); _macroI++) { \
if (_macroPtrsToCheck[_macroI] == NULL) { \
crinitErrPrint("Input parameters must not be NULL."); \
return (errcode); \
} \
} \
_Pragma("GCC diagnostic pop"); \
} while (0)
#define crinitNumElements(p)
Definition: common.h:48

Macro to simplify checking for null pointer inputs at the start of a function.

Will print an error message and return from the calling function with the given error code if any of the given variables are NULL.

Declares the variables _macroPtrsToCheck and _macroI internally. These names must not be used as parameter names to functions which use this macro.

Parameters
errcodeThe error code to return if expr is false. Must be a compatible type to the return type of the encompassing function.
...Variadic list of parameter names to check if they are NULL.

◆ crinitNullify

#define crinitNullify (   ptr)
Value:
do { \
free(ptr); \
(ptr) = NULL; \
} while (0)

Convenience macro to free memory and also set the pointer to it to NULL.

Parameters
ptrThe pointer to be "nullified".

◆ crinitNumElements

#define crinitNumElements (   p)    (sizeof(p) / sizeof(*(p)))

Calculate the number of elements of an array at compile-time.

◆ crinitParamCheck

#define crinitParamCheck (   inputParam,
  cmpShort,
  cmpLong 
)     ((strncmp(inputParam, cmpShort, sizeof(cmpShort)) == 0) || (strncmp(inputParam, cmpLong, sizeof(cmpLong)) == 0))

Checks if a string is equal to at least one of two comparison literals.

Meant to be used in a loop to check argv for long and short options. Parameters must not be NULL.

Parameters
inputParampointer to a c string to be compared, may be const
cmpShortfirst comparison string literal, meant for the short form of the option
cmpLongsecond comparison string literal, meant for the long form of the option
Returns
true if inputParam is lexicographically equal to at least one of cmpShort and cmpLong, false otherwise

◆ crinitStrtoGenericInteger

#define crinitStrtoGenericInteger (   resType,
  str,
  endptr,
  base 
)
Value:
_Generic((resType), \
int : strtol, \
long : strtol, \
long long : strtoll, \
unsigned int : strtoul, \
unsigned long : strtoul, \
unsigned long long : strtoull) \
((str), (endptr), (base))

Macro for a type-generic implementation of strto*().

Example: unsigned long x = strtoGenericInteger(x, "0xFF", NULL, 16); will map to unsigned long x = strtoul("0xFF", NULL, 16);.