utils.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #define PK_REGION(name) 1
  6. #define PK_SLICE_LOOP(i, start, stop, step) \
  7. for(int i = start; step > 0 ? i < stop : i > stop; i += step)
  8. // global constants
  9. #define PK_HEX_TABLE "0123456789abcdef"
  10. #ifdef _MSC_VER
  11. #define c11__unreachable() __assume(0)
  12. #else
  13. #define c11__unreachable() __builtin_unreachable()
  14. #endif
  15. #define c11__abort(...) \
  16. do { \
  17. fprintf(stderr, __VA_ARGS__); \
  18. putchar('\n'); \
  19. abort(); \
  20. } while(0)
  21. #define c11__rtassert(cond) \
  22. if(!(cond)) c11__abort("assertion failed: %s", #cond)
  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. // ref counting
  27. typedef struct RefCounted {
  28. int count;
  29. void (*dtor)(void*);
  30. } RefCounted;
  31. #define PK_INCREF(obj) (obj)->rc.count++
  32. #define PK_DECREF(obj) \
  33. do { \
  34. if(--(obj)->rc.count == 0) { \
  35. (obj)->rc.dtor(obj); \
  36. PK_FREE(obj); \
  37. } \
  38. } while(0)