utils.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * SPDX-FileCopyrightText: 2011-2014 Wind River Systems, Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Misc utilities
  9. *
  10. * Misc utilities usable by the kernel and application code.
  11. */
  12. #ifndef _BLE_MESH_UTILS_H_
  13. #define _BLE_MESH_UTILS_H_
  14. #include <stddef.h>
  15. #include "esp_bit_defs.h"
  16. #include "mesh/types.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* Helper to pass a int as a pointer or vice-versa.
  21. * Those are available for 32 bits architectures:
  22. */
  23. #ifndef POINTER_TO_UINT
  24. #define POINTER_TO_UINT(x) ((uint32_t) (x))
  25. #endif
  26. #ifndef UINT_TO_POINTER
  27. #define UINT_TO_POINTER(x) ((void *) (x))
  28. #endif
  29. #ifndef POINTER_TO_INT
  30. #define POINTER_TO_INT(x) ((int32_t) (x))
  31. #endif
  32. #ifndef INT_TO_POINTER
  33. #define INT_TO_POINTER(x) ((void *) (x))
  34. #endif
  35. /* Evaluates to 0 if cond is true-ish; compile error otherwise */
  36. #ifndef ZERO_OR_COMPILE_ERROR
  37. #define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
  38. #endif
  39. /* Evaluates to 0 if array is an array; compile error if not array (e.g.
  40. * pointer)
  41. */
  42. #ifndef IS_ARRAY
  43. #define IS_ARRAY(array) \
  44. ZERO_OR_COMPILE_ERROR( \
  45. !__builtin_types_compatible_p(__typeof__(array), \
  46. __typeof__(&(array)[0])))
  47. #endif
  48. /* Evaluates to number of elements in an array; compile error if not
  49. * an array (e.g. pointer)
  50. */
  51. #ifndef ARRAY_SIZE
  52. #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
  53. #endif
  54. /* Evaluates to 1 if ptr is part of array, 0 otherwise; compile error if
  55. * "array" argument is not an array (e.g. "ptr" and "array" mixed up)
  56. */
  57. #ifndef PART_OF_ARRAY
  58. #define PART_OF_ARRAY(array, ptr) \
  59. ((ptr) && ((ptr) >= &array[0] && (ptr) < &array[ARRAY_SIZE(array)]))
  60. #endif
  61. #ifndef CONTAINER_OF
  62. #define CONTAINER_OF(ptr, type, field) \
  63. ((type *)(((char *)(ptr)) - offsetof(type, field)))
  64. #endif
  65. /* round "x" up/down to next multiple of "align" (which must be a power of 2) */
  66. #ifndef ROUND_UP
  67. #define ROUND_UP(x, align) \
  68. (((unsigned long)(x) + ((unsigned long)align - 1)) & \
  69. ~((unsigned long)align - 1))
  70. #endif
  71. #ifndef ROUND_DOWN
  72. #define ROUND_DOWN(x, align) ((unsigned long)(x) & ~((unsigned long)align - 1))
  73. #endif
  74. /* round up/down to the next word boundary */
  75. #ifndef WB_UP
  76. #define WB_UP(x) ROUND_UP(x, sizeof(void *))
  77. #endif
  78. #ifndef WB_DN
  79. #define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
  80. #endif
  81. #ifndef ceiling_fraction
  82. #define ceiling_fraction(numerator, divider) \
  83. (((numerator) + ((divider) - 1)) / (divider))
  84. #endif
  85. #ifndef CHECKIF
  86. #define CHECKIF(expr) if (expr)
  87. #endif
  88. /** @brief Return larger value of two provided expressions.
  89. *
  90. * @note Arguments are evaluated twice. See Z_MAX for GCC only, single
  91. * evaluation version.
  92. */
  93. #ifndef MAX
  94. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  95. #endif
  96. /** @brief Return smaller value of two provided expressions.
  97. *
  98. * @note Arguments are evaluated twice. See Z_MIN for GCC only, single
  99. * evaluation version.
  100. */
  101. #ifndef MIN
  102. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  103. #endif
  104. #ifndef BIT
  105. #define BIT(n) (1UL << (n))
  106. #endif
  107. #ifndef BIT_MASK
  108. #define BIT_MASK(n) (BIT(n) - 1)
  109. #endif
  110. /**
  111. * @brief Check for macro definition in compiler-visible expressions
  112. *
  113. * This trick was pioneered in Linux as the config_enabled() macro.
  114. * The madness has the effect of taking a macro value that may be
  115. * defined to "1" (e.g. CONFIG_MYFEATURE), or may not be defined at
  116. * all and turning it into a literal expression that can be used at
  117. * "runtime". That is, it works similarly to
  118. * "defined(CONFIG_MYFEATURE)" does except that it is an expansion
  119. * that can exist in a standard expression and be seen by the compiler
  120. * and optimizer. Thus much ifdef usage can be replaced with cleaner
  121. * expressions like:
  122. *
  123. * if (IS_ENABLED(CONFIG_MYFEATURE))
  124. * myfeature_enable();
  125. *
  126. * INTERNAL
  127. * First pass just to expand any existing macros, we need the macro
  128. * value to be e.g. a literal "1" at expansion time in the next macro,
  129. * not "(1)", etc... Standard recursive expansion does not work.
  130. */
  131. #define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro)
  132. /* Now stick on a "_XXXX" prefix, it will now be "_XXXX1" if config_macro
  133. * is "1", or just "_XXXX" if it's undefined.
  134. * ENABLED: Z_IS_ENABLED2(_XXXX1)
  135. * DISABLED Z_IS_ENABLED2(_XXXX)
  136. */
  137. #define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro)
  138. /* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string
  139. * with a trailing comma), so it has the effect of making this a
  140. * two-argument tuple to the preprocessor only in the case where the
  141. * value is defined to "1"
  142. * ENABLED: _YYYY, <--- note comma!
  143. * DISABLED: _XXXX
  144. */
  145. #define _XXXX1 _YYYY,
  146. /* Then we append an extra argument to fool the gcc preprocessor into
  147. * accepting it as a varargs macro.
  148. * arg1 arg2 arg3
  149. * ENABLED: Z_IS_ENABLED3(_YYYY, 1, 0)
  150. * DISABLED Z_IS_ENABLED3(_XXXX 1, 0)
  151. */
  152. #define Z_IS_ENABLED2(one_or_two_args) Z_IS_ENABLED3(one_or_two_args true, false)
  153. /* And our second argument is thus now cooked to be 1 in the case
  154. * where the value is defined to 1, and 0 if not:
  155. */
  156. #define Z_IS_ENABLED3(ignore_this, val, ...) val
  157. const char *bt_hex(const void *buf, size_t len);
  158. void mem_rcopy(uint8_t *dst, uint8_t const *src, uint16_t len);
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif /* _BLE_MESH_UTILS_H_ */