core.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // Handy flags operations.
  32. #define SET_FLAG(n, f) ((n) |= (f))
  33. #define CLR_FLAG(n, f) ((n) &= ~(f))
  34. #define TGL_FLAG(n, f) ((n) ^= (f))
  35. #define CHK_FLAG(n, f) ((n) & (f))
  36. /* * * RETURN CODES * * */
  37. /**
  38. * 0 is success, positive integers (>88800) are warnings, and negative integers
  39. * (<-88800) are errors.
  40. */
  41. #define LSUP_OK 0
  42. #define LSUP_NOACTION 88801
  43. #define LSUP_NORESULT 88802
  44. #define LSUP_END 88803
  45. #define LSUP_ERROR (-88801)
  46. #define LSUP_PARSE_ERR (-88802)
  47. #define LSUP_VALUE_ERR (-88803)
  48. typedef size_t LSUP_Key;
  49. typedef LSUP_Key LSUP_DoubleKey[2];
  50. typedef LSUP_Key LSUP_TripleKey[3];
  51. typedef LSUP_Key LSUP_QuadKey[4];
  52. typedef char uuid_str_t[UUIDSTR_SIZE];
  53. // Don't use MIN and MAX macros: see
  54. // https://dustri.org/b/min-and-max-macro-considered-harmful.html
  55. inline int min(int x, int y) { return x < y ? x : y; }
  56. inline int max(int x, int y) { return x > y ? x : y; }
  57. #endif