core.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef _LSUP_CORE_H
  2. #define _LSUP_CORE_H
  3. #include <ctype.h>
  4. #include <dirent.h>
  5. #include <errno.h>
  6. #include <inttypes.h>
  7. #include <limits.h>
  8. #include <stdbool.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <uuid/uuid.h>
  15. #include "log.h"
  16. // Logging and debugging.
  17. #ifdef DEBUG
  18. // GDB breakpoints.
  19. #include <signal.h>
  20. #define BREAKPOINT raise (SIGINT)
  21. #else
  22. #define BREAKPOINT
  23. #endif
  24. #define LIKELY(x) __builtin_expect(!!(x), true)
  25. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  26. // TODO Cross-platform ramdisk path.
  27. #define TMPDIR "/tmp"
  28. #define KLEN sizeof(LSUP_Key)
  29. #define DBL_KLEN sizeof(LSUP_DoubleKey)
  30. #define TRP_KLEN sizeof(LSUP_TripleKey)
  31. #define QUAD_KLEN sizeof(LSUP_QuadKey)
  32. # define UUIDSTR_SIZE 37
  33. /* * * RETURN CODES * * */
  34. /**
  35. * 0 is success, positive integers (>88800) are warnings, and negative integers
  36. * (>-88900) are errors.
  37. */
  38. typedef int LSUP_rc;
  39. #define LSUP_OK 0
  40. #define LSUP_NOACTION 88801
  41. #define LSUP_NORESULT 88802
  42. #define LSUP_END 88803
  43. // NOTE When adding new warning codes, use a value larger than the last one
  44. // in the list. Also change LSUP_MAX_WARN.
  45. #define LSUP_MIN_WARNING LSUP_NOACTION
  46. #define LSUP_MAX_WARNING LSUP_END
  47. #define LSUP_ERROR -88899
  48. #define LSUP_PARSE_ERR -88898
  49. #define LSUP_VALUE_ERR -88897
  50. #define LSUP_TXN_ERR -88896
  51. #define LSUP_DB_ERR -88895
  52. #define LSUP_NOT_IMPL_ERR -88894
  53. #define LSUP_IO_ERR -88893
  54. #define LSUP_MEM_ERR -88892
  55. // NOTE When adding new error codes, use a value larger than the last one
  56. // in the list. Also change LSUP_MAX_ERR.
  57. #define LSUP_MIN_ERROR LSUP_ERROR
  58. #define LSUP_MAX_ERROR LSUP_MEM_ERR
  59. extern char *warning_msg[], *error_msg[];
  60. typedef enum {
  61. LSUP_BOOL_UNION,
  62. LSUP_BOOL_SUBTRACTION,
  63. LSUP_BOOL_INTERSECTION,
  64. LSUP_BOOL_XOR,
  65. } LSUP_bool_op;
  66. typedef size_t LSUP_Key;
  67. typedef LSUP_Key LSUP_DoubleKey[2];
  68. typedef LSUP_Key LSUP_TripleKey[3];
  69. typedef LSUP_Key LSUP_QuadKey[4];
  70. typedef char uuid_str_t[UUIDSTR_SIZE];
  71. /*
  72. // Yes, a textbook mistake; but writing min and max for all int types is crazy.
  73. #define min(x, y) (x) < (y) ? (x) : (y)
  74. #define max(x, y) (x) > (y) ? (x) : (y)
  75. */
  76. /** @brief Make recursive directories.
  77. *
  78. * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  79. */
  80. LSUP_rc mkdir_p(const char *path, mode_t mode);
  81. /** @brief Remove a directory recursively, as in Unix "rm -r".
  82. *
  83. * @param path[in] Path of directory to remove.
  84. */
  85. LSUP_rc
  86. rm_r (const char *path);
  87. /** @brief Return an error message for a return code.
  88. */
  89. const char *
  90. LSUP_strerror (LSUP_rc rc);
  91. /**
  92. * Encode a code point using UTF-8
  93. *
  94. * @author Ondřej Hruška <ondra@ondrovo.com>
  95. * @license MIT
  96. * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
  97. *
  98. * @param out - output buffer (min 5 characters), will be 0-terminated
  99. * @param utf - code point 0-0x10FFFF
  100. * @return number of bytes on success, 0 on failure (also produces U+FFFD,
  101. * which uses 3 bytes)
  102. */
  103. inline int utf8_encode(const uint32_t utf, unsigned char *out)
  104. {
  105. if (utf <= 0x7F) {
  106. // Plain ASCII
  107. out[0] = (char) utf;
  108. out[1] = 0;
  109. return 1;
  110. }
  111. else if (utf <= 0x07FF) {
  112. // 2-byte unicode
  113. out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
  114. out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
  115. out[2] = 0;
  116. return 2;
  117. }
  118. else if (utf <= 0xFFFF) {
  119. // 3-byte unicode
  120. out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
  121. out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
  122. out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
  123. out[3] = 0;
  124. return 3;
  125. }
  126. else if (utf <= 0x10FFFF) {
  127. // 4-byte unicode
  128. out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
  129. out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
  130. out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
  131. out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
  132. out[4] = 0;
  133. return 4;
  134. }
  135. else {
  136. // error - use replacement character
  137. out[0] = (char) 0xEF;
  138. out[1] = (char) 0xBF;
  139. out[2] = (char) 0xBD;
  140. out[3] = 0;
  141. return 0;
  142. }
  143. }
  144. // Error handling via goto.
  145. #define CHECK(exp, rc, marker) (rc) = (exp); if ((rc) != LSUP_OK) goto marker
  146. // Jump if rc is negative (skip warnings).
  147. #define PCHECK(exp, rc, marker) (rc) = (exp); if ((rc) < LSUP_OK) goto marker
  148. // Return rc if it is of LSUP_rc type and is negative (=error)
  149. #define RCCK(exp) LSUP_rc _rc = (exp); if (UNLIKELY (_rc < 0)) return _rc
  150. // Return NULL if it is of LSUP_rc type and is negative (=error)
  151. #define RCNL(exp) if (UNLIKELY ((exp) < 0)) return NULL
  152. #define MALLOC_GUARD(var, rc) do { \
  153. (var) = malloc (sizeof *(var)); \
  154. if (UNLIKELY (var == NULL)) return (rc); \
  155. } while (0);
  156. #define CALLOC_GUARD(var, rc) do { \
  157. (var) = calloc (1, sizeof *(var)); \
  158. if (UNLIKELY (var == NULL)) return (rc); \
  159. } while (0);
  160. /*
  161. #define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), LSUP_MEM_ERR) \
  162. #define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), LSUP_MEM_ERR) \
  163. #define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
  164. #define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
  165. */
  166. #endif