core.h 7.7 KB

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