core.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef _LSUP_CORE_H
  2. #define _LSUP_CORE_H
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <uuid/uuid.h>
  10. #ifdef DEBUG
  11. #define DEBUG_TEST 1
  12. #else
  13. #define DEBUG_TEST 0
  14. #endif
  15. #define STR "%s\n"
  16. #define TRACE(fmt, ...) \
  17. do {\
  18. if (DEBUG_TEST) \
  19. fprintf(stderr, "%s:%d:%s(): " fmt "\n", \
  20. __FILE__, __LINE__, __func__, __VA_ARGS__); \
  21. } while (0)
  22. #define LIKELY(x) __builtin_expect(!!(x), true)
  23. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  24. // TODO Handle memory errors better.
  25. #define CRITICAL(exp) if (UNLIKELY(((exp) == NULL))) { abort(); }
  26. #define KLEN sizeof(LSUP_Key)
  27. #define DBL_KLEN sizeof(LSUP_DoubleKey)
  28. #define TRP_KLEN sizeof(LSUP_TripleKey)
  29. #define QUAD_KLEN sizeof(LSUP_QuadKey)
  30. # define UUIDSTR_SIZE 37
  31. /* * * RETURN CODES * * */
  32. /**
  33. * 0 is success, positive integers (>88800) are warnings, and negative integers
  34. * (<-88800) are errors.
  35. */
  36. #define LSUP_OK 0
  37. #define LSUP_NOACTION 88801
  38. #define LSUP_NORESULT 88802
  39. #define LSUP_END 88803
  40. #define LSUP_ERROR (-88801)
  41. #define LSUP_PARSE_ERR (-88802)
  42. typedef size_t LSUP_Key;
  43. typedef LSUP_Key LSUP_DoubleKey[2];
  44. typedef LSUP_Key LSUP_TripleKey[3];
  45. typedef LSUP_Key LSUP_QuadKey[4];
  46. typedef char uuid_str_t[UUIDSTR_SIZE];
  47. // Don't use MIN and MAX macros: see
  48. // https://dustri.org/b/min-and-max-macro-considered-harmful.html
  49. inline int min(int x, int y) { return x < y ? x : y; }
  50. inline int max(int x, int y) { return x > y ? x : y; }
  51. #endif