utils.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #define PK_REGION(name) 1
  8. #define PK_SLICE_LOOP(i, start, stop, step) \
  9. for(int i = start; step > 0 ? i < stop : i > stop; i += step)
  10. // global constants
  11. #define PK_HEX_TABLE "0123456789abcdef"
  12. #ifdef _MSC_VER
  13. #define c11__unreachedable() __assume(0)
  14. #else
  15. #define c11__unreachedable() __builtin_unreachable()
  16. #endif
  17. #define c11__abort(...) \
  18. do { \
  19. fprintf(stderr, __VA_ARGS__); \
  20. putchar('\n'); \
  21. abort(); \
  22. } while(0)
  23. #define c11__min(a, b) ((a) < (b) ? (a) : (b))
  24. #define c11__max(a, b) ((a) > (b) ? (a) : (b))
  25. #define c11__count_array(a) (sizeof(a) / sizeof(a[0]))
  26. // NARGS
  27. #define PK_NARGS_SEQ(_1, _2, _3, _4, N, ...) N
  28. #define PK_NARGS(...) PK_NARGS_SEQ(__VA_ARGS__, 4, 3, 2, 1, 0)
  29. #define PK_NPTRS(...) PK_NARGS_SEQ(__VA_ARGS__, int****, int***, int**, int*, int)
  30. // ref counting
  31. typedef struct RefCounted {
  32. int count;
  33. void (*dtor)(void*);
  34. } RefCounted;
  35. #define PK_INCREF(obj) (obj)->rc.count++
  36. #define PK_DECREF(obj) \
  37. do { \
  38. if(--(obj)->rc.count == 0) { \
  39. (obj)->rc.dtor(obj); \
  40. free(obj); \
  41. } \
  42. } while(0)
  43. #ifdef __cplusplus
  44. }
  45. #endif