core.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // TODO Cross-platform ramdisk path.
  31. #define TMPDIR /tmp
  32. #define KLEN sizeof(LSUP_Key)
  33. #define DBL_KLEN sizeof(LSUP_DoubleKey)
  34. #define TRP_KLEN sizeof(LSUP_TripleKey)
  35. #define QUAD_KLEN sizeof(LSUP_QuadKey)
  36. # define UUIDSTR_SIZE 37
  37. // Handy flags operations.
  38. #define SET_FLAG(n, f) ((n) |= (f))
  39. #define CLR_FLAG(n, f) ((n) &= ~(f))
  40. #define TGL_FLAG(n, f) ((n) ^= (f))
  41. #define CHK_FLAG(n, f) ((n) & (f))
  42. /* * * RETURN CODES * * */
  43. /**
  44. * 0 is success, positive integers (>88800) are warnings, and negative integers
  45. * (<-88800) are errors.
  46. */
  47. typedef enum {
  48. LSUP_OK = 0,
  49. LSUP_NOACTION = 88801,
  50. LSUP_NORESULT = 88802,
  51. LSUP_END = 88803,
  52. LSUP_ERROR = -88801,
  53. LSUP_PARSE_ERR = -88802,
  54. LSUP_VALUE_ERR = -88803,
  55. LSUP_TXN_ERR = -88804,
  56. LSUP_DB_ERR = -88805,
  57. LSUP_NOT_IMPL_ERR = -88806,
  58. } LSUP_rc;
  59. typedef enum {
  60. LSUP_BOOL_UNION,
  61. LSUP_BOOL_SUBTRACTION,
  62. LSUP_BOOL_INTERSECTION,
  63. LSUP_BOOL_XOR,
  64. } LSUP_bool_op;
  65. typedef size_t LSUP_Key;
  66. typedef LSUP_Key LSUP_DoubleKey[2];
  67. typedef LSUP_Key LSUP_TripleKey[3];
  68. typedef LSUP_Key LSUP_QuadKey[4];
  69. typedef char uuid_str_t[UUIDSTR_SIZE];
  70. // Yes, a textbook mistake; but writing min and max for all int types is crazy.
  71. #define min(x, y) (x) < (y) ? (x) : (y)
  72. #define max(x, y) (x) > (y) ? (x) : (y)
  73. /** @brief Make recursive directories.
  74. *
  75. * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  76. */
  77. LSUP_rc mkdir_p(const char *path, mode_t mode);
  78. // Error handling via goto.
  79. #define CHECK(exp, rc, marker) (rc) = (exp); if ((rc) != LSUP_OK) goto marker
  80. // Jump if rc is negative (skip warnings).
  81. #define PCHECK(exp, rc, marker) (rc) = (exp); if ((rc) < LSUP_OK) goto marker
  82. // Return rc if it is of LSUP_rc type and is negative (=error)
  83. #define RCCK(exp) LSUP_rc _rc = (exp); if (_rc < 0) return _rc
  84. // Return NULL if it is of LSUP_rc type and is negative (=error)
  85. #define RCNL(exp) if((exp) < 0) return NULL
  86. #endif