core.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // "NULL" key, a value that is never user-provided. Used to mark special
  31. // values (e.g. deleted records).
  32. #define NULL_KEY 0
  33. // Value of first key inserted in an empty term database.
  34. #define FIRST_KEY 1
  35. # define UUIDSTR_SIZE 37
  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. typedef size_t LSUP_Key;
  48. typedef LSUP_Key LSUP_DoubleKey[2];
  49. typedef LSUP_Key LSUP_TripleKey[3];
  50. typedef LSUP_Key LSUP_QuadKey[4];
  51. typedef char uuid_str_t[UUIDSTR_SIZE];
  52. // Don't use MIN and MAX macros: see
  53. // https://dustri.org/b/min-and-max-macro-considered-harmful.html
  54. inline int min(int x, int y) { return x < y ? x : y; }
  55. inline int max(int x, int y) { return x > y ? x : y; }
  56. #endif