utils.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * utils - misc libubox utility functions
  3. *
  4. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef __LIBUBOX_UTILS_H
  19. #define __LIBUBOX_UTILS_H
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <sys/time.h>
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include <unistd.h>
  26. #include <time.h>
  27. /*
  28. * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
  29. *
  30. * allocate a block of memory big enough to hold multiple aligned objects.
  31. * the pointer to the full object (starting with the first chunk) is returned,
  32. * all other pointers are stored in the locations behind extra addr arguments.
  33. * the last argument needs to be a NULL pointer
  34. */
  35. #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
  36. void *__calloc_a(size_t len, ...);
  37. #ifndef ARRAY_SIZE
  38. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  39. #endif
  40. #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  41. #ifdef __OPTIMIZE__
  42. extern int __BUILD_BUG_ON_CONDITION_FAILED;
  43. #define BUILD_BUG_ON(condition) \
  44. do { \
  45. __BUILD_BUG_ON(condition); \
  46. if (condition) \
  47. __BUILD_BUG_ON_CONDITION_FAILED = 1; \
  48. } while(0)
  49. #else
  50. #define BUILD_BUG_ON __BUILD_BUG_ON
  51. #endif
  52. #if defined(__APPLE__) && !defined(CLOCK_MONOTONIC)
  53. #define LIBUBOX_COMPAT_CLOCK_GETTIME
  54. #include <mach/clock_types.h>
  55. #define CLOCK_REALTIME CALENDAR_CLOCK
  56. #define CLOCK_MONOTONIC SYSTEM_CLOCK
  57. int clock_gettime(int type, struct timespec *tv);
  58. #endif
  59. #ifdef __GNUC__
  60. #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
  61. #else
  62. #define _GNUC_MIN_VER(maj, min) 0
  63. #endif
  64. #if defined(__linux__) || defined(__CYGWIN__)
  65. #include <byteswap.h>
  66. #include <endian.h>
  67. #elif defined(__APPLE__)
  68. #include <machine/endian.h>
  69. #include <machine/byte_order.h>
  70. #elif defined(__FreeBSD__)
  71. #include <sys/endian.h>
  72. #else
  73. #include <machine/endian.h>
  74. #endif
  75. #ifndef __BYTE_ORDER
  76. #define __BYTE_ORDER BYTE_ORDER
  77. #endif
  78. #ifndef __BIG_ENDIAN
  79. #define __BIG_ENDIAN BIG_ENDIAN
  80. #endif
  81. #ifndef __LITTLE_ENDIAN
  82. #define __LITTLE_ENDIAN LITTLE_ENDIAN
  83. #endif
  84. #define __constant_swap16(x) ((uint16_t)( \
  85. (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
  86. (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
  87. #define __constant_swap32(x) ((uint32_t)( \
  88. (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
  89. (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
  90. (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
  91. (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
  92. #define __constant_swap64(x) ((uint64_t)( \
  93. (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
  94. (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
  95. (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
  96. (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
  97. (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
  98. (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
  99. (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
  100. (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
  101. /*
  102. * This returns a constant expression while determining if an argument is
  103. * a constant expression, most importantly without evaluating the argument.
  104. */
  105. #define __is_constant(x) \
  106. (sizeof(int) == sizeof(*(1 ? ((void*)((long)(x) * 0l)) : (int*)1)))
  107. #define __eval_once(func, x) \
  108. ({ __typeof__(x) __x = x; func(__x); })
  109. #ifdef __cplusplus
  110. /*
  111. * g++ does not support __builtin_choose_expr, so always use __eval_once.
  112. * Unfortunately this means that the byte order functions can't be used
  113. * as a constant expression anymore
  114. */
  115. #define __eval_safe(func, x) __eval_once(func, x)
  116. #else
  117. #define __eval_safe(func, x) \
  118. __builtin_choose_expr(__is_constant(x), \
  119. func(x), __eval_once(func, x))
  120. #endif
  121. #if __BYTE_ORDER == __LITTLE_ENDIAN
  122. #define const_cpu_to_be64(x) __constant_swap64(x)
  123. #define const_cpu_to_be32(x) __constant_swap32(x)
  124. #define const_cpu_to_be16(x) __constant_swap16(x)
  125. #define const_be64_to_cpu(x) __constant_swap64(x)
  126. #define const_be32_to_cpu(x) __constant_swap32(x)
  127. #define const_be16_to_cpu(x) __constant_swap16(x)
  128. #define const_cpu_to_le64(x) (x)
  129. #define const_cpu_to_le32(x) (x)
  130. #define const_cpu_to_le16(x) (x)
  131. #define const_le64_to_cpu(x) (x)
  132. #define const_le32_to_cpu(x) (x)
  133. #define const_le16_to_cpu(x) (x)
  134. #define cpu_to_be64(x) __eval_safe(__constant_swap64, x)
  135. #define cpu_to_be32(x) __eval_safe(__constant_swap32, x)
  136. #define cpu_to_be16(x) __eval_safe(__constant_swap16, x)
  137. #define be64_to_cpu(x) __eval_safe(__constant_swap64, x)
  138. #define be32_to_cpu(x) __eval_safe(__constant_swap32, x)
  139. #define be16_to_cpu(x) __eval_safe(__constant_swap16, x)
  140. #define cpu_to_le64(x) (x)
  141. #define cpu_to_le32(x) (x)
  142. #define cpu_to_le16(x) (x)
  143. #define le64_to_cpu(x) (x)
  144. #define le32_to_cpu(x) (x)
  145. #define le16_to_cpu(x) (x)
  146. #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
  147. #define const_cpu_to_le64(x) __constant_swap64(x)
  148. #define const_cpu_to_le32(x) __constant_swap32(x)
  149. #define const_cpu_to_le16(x) __constant_swap16(x)
  150. #define const_le64_to_cpu(x) __constant_swap64(x)
  151. #define const_le32_to_cpu(x) __constant_swap32(x)
  152. #define const_le16_to_cpu(x) __constant_swap16(x)
  153. #define const_cpu_to_be64(x) (x)
  154. #define const_cpu_to_be32(x) (x)
  155. #define const_cpu_to_be16(x) (x)
  156. #define const_be64_to_cpu(x) (x)
  157. #define const_be32_to_cpu(x) (x)
  158. #define const_be16_to_cpu(x) (x)
  159. #define cpu_to_le64(x) __eval_safe(__constant_swap64, x)
  160. #define cpu_to_le32(x) __eval_safe(__constant_swap32, x)
  161. #define cpu_to_le16(x) __eval_safe(__constant_swap16, x)
  162. #define le64_to_cpu(x) __eval_safe(__constant_swap64, x)
  163. #define le32_to_cpu(x) __eval_safe(__constant_swap32, x)
  164. #define le16_to_cpu(x) __eval_safe(__constant_swap16, x)
  165. #define cpu_to_be64(x) (x)
  166. #define cpu_to_be32(x) (x)
  167. #define cpu_to_be16(x) (x)
  168. #define be64_to_cpu(x) (x)
  169. #define be32_to_cpu(x) (x)
  170. #define be16_to_cpu(x) (x)
  171. #endif
  172. #ifndef __packed
  173. #define __packed __attribute__((packed))
  174. #endif
  175. #ifndef __constructor
  176. #define __constructor __attribute__((constructor))
  177. #endif
  178. #ifndef __destructor
  179. #define __destructor __attribute__((destructor))
  180. #endif
  181. #ifndef __hidden
  182. #define __hidden __attribute__((visibility("hidden")))
  183. #endif
  184. #ifndef __has_attribute
  185. # define __has_attribute(x) 0
  186. #endif
  187. #ifndef fallthrough
  188. # if __has_attribute(__fallthrough__)
  189. # define fallthrough __attribute__((__fallthrough__))
  190. # else
  191. # define fallthrough do {} while (0) /* fallthrough */
  192. # endif
  193. #endif
  194. int b64_encode(const void *src, size_t src_len,
  195. void *dest, size_t dest_len);
  196. int b64_decode(const void *src, void *dest, size_t dest_len);
  197. #define B64_ENCODE_LEN(_len) ((((_len) + 2) / 3) * 4 + 1)
  198. #define B64_DECODE_LEN(_len) (((_len) / 4) * 3 + 1)
  199. static inline unsigned int cbuf_order(unsigned int x)
  200. {
  201. return 32 - __builtin_clz(x - 1);
  202. }
  203. static inline unsigned long cbuf_size(int order)
  204. {
  205. unsigned long page_size = sysconf(_SC_PAGESIZE);
  206. unsigned long ret = 1ULL << order;
  207. if (ret < page_size)
  208. ret = page_size;
  209. return ret;
  210. }
  211. void *cbuf_alloc(unsigned int order);
  212. void cbuf_free(void *ptr, unsigned int order);
  213. int mkdir_p(char *dir, mode_t mask);
  214. #endif