core.h 7.1 KB

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