core.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. typedef enum {
  125. LSUP_BOOL_UNION,
  126. LSUP_BOOL_SUBTRACTION,
  127. LSUP_BOOL_INTERSECTION,
  128. LSUP_BOOL_XOR,
  129. } LSUP_bool_op;
  130. typedef size_t LSUP_Key;
  131. typedef LSUP_Key LSUP_DoubleKey[2];
  132. typedef LSUP_Key LSUP_TripleKey[3];
  133. typedef LSUP_Key LSUP_QuadKey[4];
  134. typedef char uuid_str_t[UUIDSTR_SIZE];
  135. /** @brief Make recursive directories.
  136. *
  137. * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  138. */
  139. LSUP_rc mkdir_p(const char *path, mode_t mode);
  140. /** @brief Remove a directory recursively, as in Unix "rm -r".
  141. *
  142. * @param path[in] Path of directory to remove.
  143. */
  144. LSUP_rc
  145. rm_r (const char *path);
  146. /** @brief Return an error message for a return code.
  147. */
  148. const char *
  149. LSUP_strerror (LSUP_rc rc);
  150. /**
  151. * Encode a code point using UTF-8
  152. *
  153. * @author Ondřej Hruška <ondra@ondrovo.com>
  154. * @license MIT
  155. * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
  156. *
  157. * @param out - output buffer (min 5 characters), will be 0-terminated
  158. * @param utf - code point 0-0x10FFFF
  159. * @return number of bytes on success, 0 on failure (also produces U+FFFD,
  160. * which uses 3 bytes)
  161. */
  162. inline int utf8_encode(const uint32_t utf, unsigned char *out)
  163. {
  164. if (utf <= 0x7F) {
  165. // Plain ASCII
  166. out[0] = (char) utf;
  167. out[1] = 0;
  168. return 1;
  169. }
  170. else if (utf <= 0x07FF) {
  171. // 2-byte unicode
  172. out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
  173. out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
  174. out[2] = 0;
  175. return 2;
  176. }
  177. else if (utf <= 0xFFFF) {
  178. // 3-byte unicode
  179. out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
  180. out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
  181. out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
  182. out[3] = 0;
  183. return 3;
  184. }
  185. else if (utf <= 0x10FFFF) {
  186. // 4-byte unicode
  187. out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
  188. out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
  189. out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
  190. out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
  191. out[4] = 0;
  192. return 4;
  193. }
  194. else {
  195. // error - use replacement character
  196. out[0] = (char) 0xEF;
  197. out[1] = (char) 0xBF;
  198. out[2] = (char) 0xBD;
  199. out[3] = 0;
  200. return 0;
  201. }
  202. }
  203. // Error handling via goto.
  204. #define CHECK(exp, rc, marker) (rc) = (exp); if ((rc) != LSUP_OK) goto marker
  205. // Jump if rc is negative (skip warnings).
  206. #define PCHECK(exp, rc, marker) (rc) = (exp); if ((rc) < LSUP_OK) goto marker
  207. // Return rc if it is of LSUP_rc type and is negative (=error)
  208. #define RCCK(exp) LSUP_rc _rc = (exp); if (UNLIKELY (_rc < 0)) return _rc
  209. // Return NULL if it is of LSUP_rc type and is negative (=error)
  210. #define RCNL(exp) if (UNLIKELY ((exp) < 0)) return NULL
  211. #define MALLOC_GUARD(var, rc) do { \
  212. (var) = malloc (sizeof *(var)); \
  213. if (UNLIKELY (var == NULL)) return (rc); \
  214. } while (0);
  215. #define CALLOC_GUARD(var, rc) do { \
  216. (var) = calloc (1, sizeof *(var)); \
  217. if (UNLIKELY (var == NULL)) return (rc); \
  218. } while (0);
  219. /*
  220. #define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), LSUP_MEM_ERR) \
  221. #define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), LSUP_MEM_ERR) \
  222. #define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
  223. #define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
  224. */
  225. #endif