utils.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #define __eval_safe(func, x) \
  109. __builtin_choose_expr(__is_constant(x), \
  110. func(x), __eval_once(func, x))
  111. #if __BYTE_ORDER == __LITTLE_ENDIAN
  112. #define cpu_to_be64(x) __eval_safe(__constant_swap64, x)
  113. #define cpu_to_be32(x) __eval_safe(__constant_swap32, x)
  114. #define cpu_to_be16(x) __eval_safe(__constant_swap16, x)
  115. #define be64_to_cpu(x) __eval_safe(__constant_swap64, x)
  116. #define be32_to_cpu(x) __eval_safe(__constant_swap32, x)
  117. #define be16_to_cpu(x) __eval_safe(__constant_swap16, x)
  118. #define cpu_to_le64(x) (x)
  119. #define cpu_to_le32(x) (x)
  120. #define cpu_to_le16(x) (x)
  121. #define le64_to_cpu(x) (x)
  122. #define le32_to_cpu(x) (x)
  123. #define le16_to_cpu(x) (x)
  124. #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
  125. #define cpu_to_le64(x) __eval_safe(__constant_swap64, x)
  126. #define cpu_to_le32(x) __eval_safe(__constant_swap32, x)
  127. #define cpu_to_le16(x) __eval_safe(__constant_swap16, x)
  128. #define le64_to_cpu(x) __eval_safe(__constant_swap64, x)
  129. #define le32_to_cpu(x) __eval_safe(__constant_swap32, x)
  130. #define le16_to_cpu(x) __eval_safe(__constant_swap16, x)
  131. #define cpu_to_be64(x) (x)
  132. #define cpu_to_be32(x) (x)
  133. #define cpu_to_be16(x) (x)
  134. #define be64_to_cpu(x) (x)
  135. #define be32_to_cpu(x) (x)
  136. #define be16_to_cpu(x) (x)
  137. #endif
  138. #ifndef __packed
  139. #define __packed __attribute__((packed))
  140. #endif
  141. #ifndef __constructor
  142. #define __constructor __attribute__((constructor))
  143. #endif
  144. #ifndef __destructor
  145. #define __destructor __attribute__((destructor))
  146. #endif
  147. #ifndef __hidden
  148. #define __hidden __attribute__((visibility("hidden")))
  149. #endif
  150. int b64_encode(const void *src, size_t src_len,
  151. void *dest, size_t dest_len);
  152. int b64_decode(const void *src, void *dest, size_t dest_len);
  153. #define B64_ENCODE_LEN(_len) ((((_len) + 2) / 3) * 4 + 1)
  154. #define B64_DECODE_LEN(_len) (((_len) / 4) * 3 + 1)
  155. static inline unsigned int cbuf_order(unsigned int x)
  156. {
  157. return 32 - __builtin_clz(x - 1);
  158. }
  159. static inline unsigned long cbuf_size(int order)
  160. {
  161. unsigned long page_size = sysconf(_SC_PAGESIZE);
  162. unsigned long ret = 1ULL << order;
  163. if (ret < page_size)
  164. ret = page_size;
  165. return ret;
  166. }
  167. void *cbuf_alloc(unsigned int order);
  168. void cbuf_free(void *ptr, unsigned int order);
  169. #endif