utils.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/types.h>
  21. #include <sys/time.h>
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include <unistd.h>
  25. #include <time.h>
  26. /*
  27. * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
  28. *
  29. * allocate a block of memory big enough to hold multiple aligned objects.
  30. * the pointer to the full object (starting with the first chunk) is returned,
  31. * all other pointers are stored in the locations behind extra addr arguments.
  32. * the last argument needs to be a NULL pointer
  33. */
  34. #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
  35. void *__calloc_a(size_t len, ...);
  36. #ifndef ARRAY_SIZE
  37. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  38. #endif
  39. #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  40. #ifdef __OPTIMIZE__
  41. extern int __BUILD_BUG_ON_CONDITION_FAILED;
  42. #define BUILD_BUG_ON(condition) \
  43. do { \
  44. __BUILD_BUG_ON(condition); \
  45. if (condition) \
  46. __BUILD_BUG_ON_CONDITION_FAILED = 1; \
  47. } while(0)
  48. #else
  49. #define BUILD_BUG_ON __BUILD_BUG_ON
  50. #endif
  51. #if defined(__APPLE__) && !defined(CLOCK_MONOTONIC)
  52. #define LIBUBOX_COMPAT_CLOCK_GETTIME
  53. #include <mach/clock_types.h>
  54. #define CLOCK_REALTIME CALENDAR_CLOCK
  55. #define CLOCK_MONOTONIC SYSTEM_CLOCK
  56. int clock_gettime(int type, struct timespec *tv);
  57. #endif
  58. #ifdef __GNUC__
  59. #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
  60. #else
  61. #define _GNUC_MIN_VER(maj, min) 0
  62. #endif
  63. #if defined(__linux__) || defined(__CYGWIN__)
  64. #include <byteswap.h>
  65. #include <endian.h>
  66. #elif defined(__APPLE__)
  67. #include <machine/endian.h>
  68. #include <machine/byte_order.h>
  69. #elif defined(__FreeBSD__)
  70. #include <sys/endian.h>
  71. #else
  72. #include <machine/endian.h>
  73. #endif
  74. #ifndef __BYTE_ORDER
  75. #define __BYTE_ORDER BYTE_ORDER
  76. #endif
  77. #ifndef __BIG_ENDIAN
  78. #define __BIG_ENDIAN BIG_ENDIAN
  79. #endif
  80. #ifndef __LITTLE_ENDIAN
  81. #define __LITTLE_ENDIAN LITTLE_ENDIAN
  82. #endif
  83. #define __constant_swap16(x) ((uint16_t)( \
  84. (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
  85. (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
  86. #define __constant_swap32(x) ((uint32_t)( \
  87. (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
  88. (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
  89. (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
  90. (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
  91. #define __constant_swap64(x) ((uint64_t)( \
  92. (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
  93. (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
  94. (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
  95. (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
  96. (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
  97. (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
  98. (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
  99. (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
  100. /*
  101. * This returns a constant expression while determining if an argument is
  102. * a constant expression, most importantly without evaluating the argument.
  103. */
  104. #define __is_constant(x) \
  105. (sizeof(int) == sizeof(*(1 ? ((void*)((long)(x) * 0l)) : (int*)1)))
  106. #define __eval_once(func, x) \
  107. ({ __typeof__(x) __x = x; func(__x); })
  108. #ifdef __cplusplus
  109. /*
  110. * g++ does not support __builtin_choose_expr, so always use __eval_once.
  111. * Unfortunately this means that the byte order functions can't be used
  112. * as a constant expression anymore
  113. */
  114. #define __eval_safe(func, x) __eval_once(func, x)
  115. #else
  116. #define __eval_safe(func, x) \
  117. __builtin_choose_expr(__is_constant(x), \
  118. func(x), __eval_once(func, x))
  119. #endif
  120. #if __BYTE_ORDER == __LITTLE_ENDIAN
  121. #define const_cpu_to_be64(x) __constant_swap64(x)
  122. #define const_cpu_to_be32(x) __constant_swap32(x)
  123. #define const_cpu_to_be16(x) __constant_swap16(x)
  124. #define const_be64_to_cpu(x) __constant_swap64(x)
  125. #define const_be32_to_cpu(x) __constant_swap32(x)
  126. #define const_be16_to_cpu(x) __constant_swap16(x)
  127. #define const_cpu_to_le64(x) (x)
  128. #define const_cpu_to_le32(x) (x)
  129. #define const_cpu_to_le16(x) (x)
  130. #define const_le64_to_cpu(x) (x)
  131. #define const_le32_to_cpu(x) (x)
  132. #define const_le16_to_cpu(x) (x)
  133. #define cpu_to_be64(x) __eval_safe(__constant_swap64, x)
  134. #define cpu_to_be32(x) __eval_safe(__constant_swap32, x)
  135. #define cpu_to_be16(x) __eval_safe(__constant_swap16, x)
  136. #define be64_to_cpu(x) __eval_safe(__constant_swap64, x)
  137. #define be32_to_cpu(x) __eval_safe(__constant_swap32, x)
  138. #define be16_to_cpu(x) __eval_safe(__constant_swap16, x)
  139. #define cpu_to_le64(x) (x)
  140. #define cpu_to_le32(x) (x)
  141. #define cpu_to_le16(x) (x)
  142. #define le64_to_cpu(x) (x)
  143. #define le32_to_cpu(x) (x)
  144. #define le16_to_cpu(x) (x)
  145. #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
  146. #define const_cpu_to_le64(x) __constant_swap64(x)
  147. #define const_cpu_to_le32(x) __constant_swap32(x)
  148. #define const_cpu_to_le16(x) __constant_swap16(x)
  149. #define const_le64_to_cpu(x) __constant_swap64(x)
  150. #define const_le32_to_cpu(x) __constant_swap32(x)
  151. #define const_le16_to_cpu(x) __constant_swap16(x)
  152. #define const_cpu_to_be64(x) (x)
  153. #define const_cpu_to_be32(x) (x)
  154. #define const_cpu_to_be16(x) (x)
  155. #define const_be64_to_cpu(x) (x)
  156. #define const_be32_to_cpu(x) (x)
  157. #define const_be16_to_cpu(x) (x)
  158. #define cpu_to_le64(x) __eval_safe(__constant_swap64, x)
  159. #define cpu_to_le32(x) __eval_safe(__constant_swap32, x)
  160. #define cpu_to_le16(x) __eval_safe(__constant_swap16, x)
  161. #define le64_to_cpu(x) __eval_safe(__constant_swap64, x)
  162. #define le32_to_cpu(x) __eval_safe(__constant_swap32, x)
  163. #define le16_to_cpu(x) __eval_safe(__constant_swap16, x)
  164. #define cpu_to_be64(x) (x)
  165. #define cpu_to_be32(x) (x)
  166. #define cpu_to_be16(x) (x)
  167. #define be64_to_cpu(x) (x)
  168. #define be32_to_cpu(x) (x)
  169. #define be16_to_cpu(x) (x)
  170. #endif
  171. #ifndef __packed
  172. #define __packed __attribute__((packed))
  173. #endif
  174. #ifndef __constructor
  175. #define __constructor __attribute__((constructor))
  176. #endif
  177. #ifndef __destructor
  178. #define __destructor __attribute__((destructor))
  179. #endif
  180. #ifndef __hidden
  181. #define __hidden __attribute__((visibility("hidden")))
  182. #endif
  183. int b64_encode(const void *src, size_t src_len,
  184. void *dest, size_t dest_len);
  185. int b64_decode(const void *src, void *dest, size_t dest_len);
  186. #define B64_ENCODE_LEN(_len) ((((_len) + 2) / 3) * 4 + 1)
  187. #define B64_DECODE_LEN(_len) (((_len) / 4) * 3 + 1)
  188. static inline unsigned int cbuf_order(unsigned int x)
  189. {
  190. return 32 - __builtin_clz(x - 1);
  191. }
  192. static inline unsigned long cbuf_size(int order)
  193. {
  194. unsigned long page_size = sysconf(_SC_PAGESIZE);
  195. unsigned long ret = 1ULL << order;
  196. if (ret < page_size)
  197. ret = page_size;
  198. return ret;
  199. }
  200. void *cbuf_alloc(unsigned int order);
  201. void cbuf_free(void *ptr, unsigned int order);
  202. #endif