core.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #ifdef DEBUG
  10. #define DEBUG_TEST 1
  11. #else
  12. #define DEBUG_TEST 0
  13. #endif
  14. #define STR "%s\n"
  15. #define TRACE(fmt, ...) \
  16. do {\
  17. if (DEBUG_TEST) \
  18. fprintf(stderr, "%s:%d:%s(): " fmt "\n", \
  19. __FILE__, __LINE__, __func__, __VA_ARGS__); \
  20. } while (0)
  21. #define LIKELY(x) __builtin_expect(!!(x), true)
  22. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  23. // TODO Handle memory errors better.
  24. #define CRITICAL(exp) if (UNLIKELY(((exp) == NULL))) { abort(); }
  25. // NOTE This may change in the future, e.g. if a different key size is to
  26. // be forced.
  27. typedef size_t LSUP_Key;
  28. typedef LSUP_Key LSUP_DoubleKey[2];
  29. typedef LSUP_Key LSUP_TripleKey[3];
  30. typedef LSUP_Key LSUP_QuadKey[4];
  31. // "NULL" key, a value that is never user-provided. Used to mark special
  32. // values (e.g. deleted records).
  33. #define NULL_KEY 0
  34. // Value of first key inserted in an empty term database.
  35. #define FIRST_KEY 1
  36. // "NULL" triple, a value that is never user-provided. Used to fill deleted
  37. // triples in a keyset.
  38. extern LSUP_TripleKey NULL_TRP;
  39. // Don't use MIN and MAX macros: see
  40. // https://dustri.org/b/min-and-max-macro-considered-harmful.html
  41. inline int min(int x, int y) { return x < y ? x : y; }
  42. inline int max(int x, int y) { return x > y ? x : y; }
  43. #endif