util.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef BACKEND_UTIL_H_
  2. #define BACKEND_UTIL_H_
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <unistd.h>
  7. #include "list.h"
  8. #define __stringify(x) #x
  9. #define stringify(x) __stringify(x)
  10. #define min(x, y) ({ \
  11. __typeof__(x) __x = (x); \
  12. __typeof__(y) __y = (y); \
  13. __x < __y ? __x : __y; \
  14. })
  15. #define max(a, b) ({ \
  16. __typeof__(a) __a = (a); \
  17. __typeof__(b) __b = (b); \
  18. __a > __b ? __a : __b; \
  19. })
  20. #define clamp(v, mi, ma) max(min(v, ma), mi)
  21. #define round_up(n, s) ((((n) + (s) - 1) / (s)) * (s))
  22. #define div_round_up(x, d) ({ \
  23. __typeof__(x) __x = (x); \
  24. __typeof__(d) __d = (d); \
  25. (__x + __d - 1) / __d; \
  26. })
  27. #define div_round(x, d) ({ \
  28. __typeof__(x) __x = (x); \
  29. __typeof__(d) __d = (d); \
  30. (__x + (__d / 2)) / __d; \
  31. })
  32. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  33. #define compiler_barrier() __asm__ __volatile__("" : : : "memory")
  34. #define ALIGN(x) __attribute__((__aligned__(x)))
  35. #define ALIGN_MAX ALIGN(__BIGGEST_ALIGNMENT__)
  36. void msleep(unsigned int msecs);
  37. char * string_strip(char *str);
  38. char * string_split(char *str, int (*sep_match)(int c));
  39. static inline void * zalloc(size_t size)
  40. {
  41. return calloc(1, size);
  42. }
  43. static inline int strempty(const char *s)
  44. {
  45. return (s == NULL) || (s[0] == '\0');
  46. }
  47. uint_fast8_t tiny_hash(const char *str);
  48. pid_t subprocess_exec(const char *_command);
  49. #endif /* BACKEND_UTIL_H_ */