Crinit -- Configurable Rootfs Init
common.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
6 #ifndef __COMMON_H__
7 #define __COMMON_H__
8 
16 #define CRINIT_PARAM_UNUSED(par) \
17  do { \
18  (void)(par); \
19  } while (0)
20 
33 #define crinitParamCheck(inputParam, cmpShort, cmpLong) \
34  ((strncmp(inputParam, cmpShort, sizeof(cmpShort)) == 0) || (strncmp(inputParam, cmpLong, sizeof(cmpLong)) == 0))
35 
43 #define crinitIsAbsPath(path) (((path) != NULL) && ((path)[0] == '/'))
44 
48 #define crinitNumElements(p) (sizeof(p) / sizeof(*(p)))
49 
63 #define crinitNullCheck(errcode, ...) \
64  do { \
65  _Pragma("GCC diagnostic push"); \
66  _Pragma("GCC diagnostic error \"-Wshadow\""); \
67  const void *_macroPtrsToCheck[] = {__VA_ARGS__}; \
68  for (size_t _macroI = 0; _macroI < crinitNumElements(_macroPtrsToCheck); _macroI++) { \
69  if (_macroPtrsToCheck[_macroI] == NULL) { \
70  crinitErrPrint("Input parameters must not be NULL."); \
71  return (errcode); \
72  } \
73  } \
74  _Pragma("GCC diagnostic pop"); \
75  } while (0)
76 
83 // clang-format off
84 // Rationale: Used version of clang-format does not format _Generic macros correctly. This is a known bug and has been
85 // fixed very recently. We may remove this exemption once we are on the new clang version as standard.
86 // See: https://github.com/llvm/llvm-project/issues/18080
87 #define crinitStrtoGenericInteger(resType, str, endptr, base) \
88  _Generic((resType), \
89  int : strtol, \
90  long : strtol, \
91  long long : strtoll, \
92  unsigned int : strtoul, \
93  unsigned long : strtoul, \
94  unsigned long long : strtoull) \
95  ((str), (endptr), (base))
96 // clang-format on
97 
103 #define crinitNullify(ptr) \
104  do { \
105  free(ptr); \
106  (ptr) = NULL; \
107  } while (0)
108 
109 #endif /* __COMMON_H__ */