core.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef _LSUP_CORE_H
  2. #define _LSUP_CORE_H
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <inttypes.h>
  6. #include <limits.h>
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <uuid/uuid.h>
  14. #ifdef DEBUG
  15. #define DEBUG_TEST 1
  16. #else
  17. #define DEBUG_TEST 0
  18. #endif
  19. #define STR "%s\n"
  20. #define TRACE(fmt, ...) \
  21. do {\
  22. if (DEBUG_TEST) \
  23. fprintf(stderr, "%s:%d:%s(): " fmt "\n", \
  24. __FILE__, __LINE__, __func__, __VA_ARGS__); \
  25. } while (0)
  26. #define LIKELY(x) __builtin_expect(!!(x), true)
  27. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  28. // TODO Handle memory errors better.
  29. #define CRITICAL(exp) if (UNLIKELY(((exp) == NULL))) { abort(); }
  30. #define KLEN sizeof(LSUP_Key)
  31. #define DBL_KLEN sizeof(LSUP_DoubleKey)
  32. #define TRP_KLEN sizeof(LSUP_TripleKey)
  33. #define QUAD_KLEN sizeof(LSUP_QuadKey)
  34. # define UUIDSTR_SIZE 37
  35. // Handy flags operations.
  36. #define SET_FLAG(n, f) ((n) |= (f))
  37. #define CLR_FLAG(n, f) ((n) &= ~(f))
  38. #define TGL_FLAG(n, f) ((n) ^= (f))
  39. #define CHK_FLAG(n, f) ((n) & (f))
  40. /* * * RETURN CODES * * */
  41. /**
  42. * 0 is success, positive integers (>88800) are warnings, and negative integers
  43. * (<-88800) are errors.
  44. */
  45. typedef enum {
  46. LSUP_OK = 0,
  47. LSUP_NOACTION = 88801,
  48. LSUP_NORESULT = 88802,
  49. LSUP_END = 88803,
  50. LSUP_ERROR = -88801,
  51. LSUP_PARSE_ERR = -88802,
  52. LSUP_VALUE_ERR = -88803,
  53. LSUP_TXN_ERR = -88804,
  54. LSUP_DB_ERR = -88805,
  55. LSUP_NOT_IMPL_ERR = -88806,
  56. } LSUP_rc;
  57. typedef enum {
  58. LSUP_BOOL_UNION,
  59. LSUP_BOOL_SUBTRACTION,
  60. LSUP_BOOL_INTERSECTION,
  61. LSUP_BOOL_XOR,
  62. } LSUP_bool_op;
  63. typedef size_t LSUP_Key;
  64. typedef LSUP_Key LSUP_DoubleKey[2];
  65. typedef LSUP_Key LSUP_TripleKey[3];
  66. typedef LSUP_Key LSUP_QuadKey[4];
  67. typedef char uuid_str_t[UUIDSTR_SIZE];
  68. // Don't use MIN and MAX macros: see
  69. // https://dustri.org/b/min-and-max-macro-considered-harmful.html
  70. inline int min(int x, int y) { return x < y ? x : y; }
  71. inline int max(int x, int y) { return x > y ? x : y; }
  72. /** @brief Make recursive directories.
  73. *
  74. * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  75. */
  76. LSUP_rc mkdir_p(const char *path, mode_t mode);
  77. // Error handling via goto.
  78. #define CHECK(exp, rc, marker) (rc) = (exp); if ((rc) != LSUP_OK) goto marker
  79. // Jump if rc is negative (skip warnings).
  80. #define PCHECK(exp, rc, marker) (rc) = (exp); if ((rc) < LSUP_OK) goto marker
  81. #endif