core.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #include "xxhash.h"
  17. // Logging and debugging.
  18. #ifdef DEBUG
  19. // GDB breakpoints.
  20. #include <signal.h>
  21. #define BREAKPOINT raise (SIGINT)
  22. #else
  23. #define BREAKPOINT
  24. #endif
  25. #define LIKELY(x) __builtin_expect(!!(x), true)
  26. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  27. #define LSUP_NS "urn:lsup:" /// Default LS namespace.
  28. // TODO Cross-platform ramdisk path.
  29. #define TMPDIR "/tmp"
  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. /** @brief "NULL" triple, a value that is never user-provided.
  36. */
  37. #define NULL_TRP {NULL_KEY, NULL_KEY, NULL_KEY}
  38. /* * * RETURN CODES * * */
  39. /**
  40. * 0 is success, positive integers (>88800) are warnings, and negative integers
  41. * (>-88900) are errors.
  42. */
  43. typedef int LSUP_rc;
  44. /** @brief Generic success return code.
  45. */
  46. #define LSUP_OK 0
  47. /** @brief No action taken.
  48. *
  49. * An attempt to create or update a resource was made, but the resource already
  50. * existed substantially in the same form, so no action took place. The caller
  51. * is expected to find the resource as though the action actually took place.
  52. * If this is returned from the iteration of multiple updates, it means that
  53. * none of the iterations produced a change in state.
  54. */
  55. #define LSUP_NOACTION 88801
  56. /** @brief No result yielded.
  57. *
  58. * A read operation returned no results. If an iterator is expected to be
  59. * created, it may be created empty.
  60. */
  61. #define LSUP_NORESULT 88802
  62. /** @brief Loop end.
  63. *
  64. * End of a loop was reached. This can be used in a while() or for()
  65. * loop as a terminating condition.
  66. */
  67. #define LSUP_END 88803
  68. /** @brief Conflict warning.
  69. * An attempt to create or update a resource was made, but the resource existed
  70. * with a different form or value. The caller should find the value of the
  71. * existing resource to be different than the one that was attempted to store.
  72. * If this is returned from the iteration of multiple updates, it means that
  73. * other resources in the loop may have changed state and the operation as a
  74. * whole completed successfully.
  75. */
  76. #define LSUP_CONFLICT 88804
  77. /*
  78. * NOTE When adding new warning codes, use a value larger than the last one
  79. * in the list. Also change LSUP_MAX_WARNING.
  80. */
  81. #define LSUP_MIN_WARNING LSUP_NOACTION
  82. #define LSUP_MAX_WARNING LSUP_CONFLICT
  83. /** @brief Generic error return code.
  84. */
  85. #define LSUP_ERROR -88899
  86. /** @brief TODO
  87. */
  88. #define LSUP_PARSE_ERR -88898
  89. /** @brief TODO
  90. */
  91. #define LSUP_VALUE_ERR -88897
  92. /** @brief TODO
  93. */
  94. #define LSUP_TXN_ERR -88896
  95. /** @brief TODO
  96. */
  97. #define LSUP_DB_ERR -88895
  98. /** @brief TODO
  99. */
  100. #define LSUP_NOT_IMPL_ERR -88894
  101. /** @brief TODO
  102. */
  103. #define LSUP_IO_ERR -88893
  104. /** @brief TODO
  105. */
  106. #define LSUP_MEM_ERR -88892
  107. /** @brief Conflict error.
  108. *
  109. * A critical resource conflict happened and no resources were updated. If this
  110. * is returned from the iteration of multiple updates, it means that the
  111. * operation has been interrupted and any state change within the loop prior to
  112. * the error has been rolled back.
  113. */
  114. #define LSUP_CONFLICT_ERR -88891
  115. /** @brief TODO
  116. */
  117. #define LSUP_ENV_ERR -88890
  118. /*
  119. * NOTE When adding new error codes, use a value larger than the last one
  120. * in the list. Also change LSUP_MAX_ERROR.
  121. */
  122. #define LSUP_MIN_ERROR LSUP_ERROR
  123. #define LSUP_MAX_ERROR LSUP_ENV_ERR
  124. // Hashing stuff.
  125. #ifndef LSUP_HASH_SEED
  126. /** @brief Seed used for all hashing. Compile-time configurable.
  127. */
  128. #define LSUP_HASH_SEED 0
  129. #endif
  130. #define LSUP_HASH32(buf, size, seed) XXH32 (buf, size, seed)
  131. #define LSUP_HASH64(buf, size, seed) XXH3_64bits_withSeed (buf, size, seed)
  132. #define LSUP_HASH128(buf, size, seed) XXH3_128bits_withSeed (buf, size, seed)
  133. // TODO Add 32-bit switch.
  134. #define LSUP_HASH(buf, size, seed) LSUP_HASH64 (buf, size, seed)
  135. extern char *warning_msg[], *error_msg[];
  136. extern char *LSUP_root_path;
  137. typedef XXH32_hash_t LSUP_Hash32;
  138. typedef XXH64_hash_t LSUP_Hash64;
  139. typedef XXH128_hash_t LSUP_Hash128;
  140. // TODO Add 32-bit switch.
  141. typedef LSUP_Hash64 LSUP_Hash;
  142. typedef enum {
  143. LSUP_BOOL_UNION,
  144. LSUP_BOOL_SUBTRACTION,
  145. LSUP_BOOL_INTERSECTION,
  146. LSUP_BOOL_XOR,
  147. } LSUP_bool_op;
  148. typedef size_t LSUP_Key;
  149. typedef LSUP_Key LSUP_DoubleKey[2];
  150. typedef LSUP_Key LSUP_TripleKey[3];
  151. typedef LSUP_Key LSUP_QuadKey[4];
  152. typedef char uuid_str_t[UUIDSTR_SIZE];
  153. /** @brief Make recursive directories.
  154. *
  155. * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  156. */
  157. LSUP_rc mkdir_p(const char *path, mode_t mode);
  158. /** @brief Remove a directory recursively, as in Unix "rm -r".
  159. *
  160. * @param path[in] Path of directory to remove.
  161. */
  162. LSUP_rc
  163. rm_r (const char *path);
  164. /** @brief Return an error message for a return code.
  165. */
  166. const char *
  167. LSUP_strerror (LSUP_rc rc);
  168. /** @brief Encode a code point using UTF-8.
  169. *
  170. * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
  171. *
  172. * @author Ondřej Hruška <ondra@ondrovo.com>
  173. *
  174. * @license MIT
  175. *
  176. * @param out - output buffer (min 5 characters), will be 0-terminated
  177. * @param utf - code point 0-0x10FFFF
  178. * @return number of bytes on success, 0 on failure (also produces U+FFFD,
  179. * which uses 3 bytes)
  180. */
  181. inline int utf8_encode(const uint32_t utf, unsigned char *out)
  182. {
  183. if (utf <= 0x7F) {
  184. // Plain ASCII
  185. out[0] = (char) utf;
  186. out[1] = 0;
  187. return 1;
  188. }
  189. else if (utf <= 0x07FF) {
  190. // 2-byte unicode
  191. out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
  192. out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
  193. out[2] = 0;
  194. return 2;
  195. }
  196. else if (utf <= 0xFFFF) {
  197. // 3-byte unicode
  198. out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
  199. out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
  200. out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
  201. out[3] = 0;
  202. return 3;
  203. }
  204. else if (utf <= 0x10FFFF) {
  205. // 4-byte unicode
  206. out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
  207. out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
  208. out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
  209. out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
  210. out[4] = 0;
  211. return 4;
  212. }
  213. else {
  214. // error - use replacement character
  215. out[0] = (char) 0xEF;
  216. out[1] = (char) 0xBF;
  217. out[2] = (char) 0xBD;
  218. out[3] = 0;
  219. return 0;
  220. }
  221. }
  222. /** @brief Log an error or warning for return codes that are not LSUP_OK.
  223. *
  224. * Note that, if used outside of the other macros below, care must be taken
  225. * to pass it an actual return code rather than an expression, otherwise the
  226. * expression will be evaluated multiple times.
  227. */
  228. #define LOG_RC(rc) do { \
  229. if ((rc) < 0) log_error (LSUP_strerror (rc)); \
  230. else if ((rc) > 0) log_warn (LSUP_strerror (rc)); \
  231. } while (0);
  232. /// Error handling via goto.
  233. #define CHECK(exp, marker) do { \
  234. LSUP_rc _rc = (exp); \
  235. LOG_RC(_rc); \
  236. if (UNLIKELY (_rc != LSUP_OK)) goto marker; \
  237. } while (0);
  238. /// Jump if rc is negative (skip warnings).
  239. #define PCHECK(exp, marker) do { \
  240. LSUP_rc _rc = (exp); \
  241. LOG_RC(_rc); \
  242. if (UNLIKELY (_rc < LSUP_OK)) goto marker; \
  243. } while (0);
  244. /// Return rc if it is of LSUP_rc type and is negative (=error)
  245. #define RCCK(exp) do { \
  246. LSUP_rc _rc = (exp); \
  247. LOG_RC(_rc); \
  248. if (UNLIKELY (_rc < LSUP_OK)) return _rc; \
  249. } while (0);
  250. /// Return NULL if it is of LSUP_rc type and is negative (=error)
  251. #define RCNL(exp) do { \
  252. LSUP_rc _rc = (exp); \
  253. LOG_RC(_rc); \
  254. if (UNLIKELY (_rc < LSUP_OK)) return NULL; \
  255. } while (0);
  256. /// Allocate one pointer with malloc and return rc if it fails.
  257. #define MALLOC_GUARD(var, rc) do { \
  258. (var) = malloc (sizeof *(var)); \
  259. if (UNLIKELY (var == NULL)) return (rc); \
  260. } while (0);
  261. /// Allocate one pointer with calloc and return rc if it fails.
  262. #define CALLOC_GUARD(var, rc) do { \
  263. (var) = calloc (1, sizeof *(var)); \
  264. if (UNLIKELY (var == NULL)) return (rc); \
  265. } while (0);
  266. /*
  267. #define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), LSUP_MEM_ERR) \
  268. #define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), LSUP_MEM_ERR) \
  269. #define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
  270. #define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
  271. */
  272. #endif /* _LSUP_CORE_H */