123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- #ifndef _LSUP_CORE_H
- #define _LSUP_CORE_H
- #ifndef NOCOLOR
- #define LOG_USE_COLOR
- #endif
- #include <ctype.h>
- #include <dirent.h>
- #include <inttypes.h>
- #include <limits.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <uuid/uuid.h>
- #include "log.h"
- #include "xxhash.h"
- #define LSUP_VERSION "1.0a3"
- #ifdef DEBUG
- #include <signal.h>
- #define BREAKPOINT raise (SIGINT)
- #else
- #define BREAKPOINT
- #endif
- #define LIKELY(x) __builtin_expect(!!(x), true)
- #define UNLIKELY(x) __builtin_expect(!!(x), false)
- #define LSUP_NS "urn:lsup:"
- #define TMPDIR "/tmp"
- #define KLEN sizeof(LSUP_Key)
- #define DBL_KLEN sizeof(LSUP_DoubleKey)
- #define TRP_KLEN sizeof(LSUP_TripleKey)
- #define QUAD_KLEN sizeof(LSUP_QuadKey)
- # define UUIDSTR_SIZE 37
- #define NULL_TRP {NULL_KEY, NULL_KEY, NULL_KEY}
- typedef int LSUP_rc;
- #define LSUP_OK 0
- #define LSUP_NOACTION 88801
- #define LSUP_NORESULT 88802
- #define LSUP_END 88803
- #define LSUP_CONFLICT 88804
- #define LSUP_MIN_WARNING LSUP_NOACTION
- #define LSUP_MAX_WARNING LSUP_CONFLICT
- #define LSUP_ERROR -88899
- #define LSUP_PARSE_ERR -88898
- #define LSUP_VALUE_ERR -88897
- #define LSUP_TXN_ERR -88896
- #define LSUP_DB_ERR -88895
- #define LSUP_NOT_IMPL_ERR -88894
- #define LSUP_IO_ERR -88893
- #define LSUP_MEM_ERR -88892
- #define LSUP_CONFLICT_ERR -88891
- #define LSUP_ENV_ERR -88890
- #define LSUP_MIN_ERROR LSUP_ERROR
- #define LSUP_MAX_ERROR LSUP_ENV_ERR
- #ifndef LSUP_HASH_SEED
- #define LSUP_HASH_SEED 0
- #endif
- #define LSUP_HASH32(buf, size, seed) XXH32 (buf, size, seed)
- #define LSUP_HASH64(buf, size, seed) XXH3_64bits_withSeed (buf, size, seed)
- #define LSUP_HASH128(buf, size, seed) XXH3_128bits_withSeed (buf, size, seed)
- #define LSUP_HASH(buf, size, seed) LSUP_HASH64 (buf, size, seed)
- extern char *warning_msg[], *error_msg[];
- extern char *LSUP_root_path;
- typedef XXH32_hash_t LSUP_Hash32;
- typedef XXH64_hash_t LSUP_Hash64;
- typedef XXH128_hash_t LSUP_Hash128;
- typedef LSUP_Hash64 LSUP_Hash;
- typedef enum {
- LSUP_BOOL_UNION,
- LSUP_BOOL_SUBTRACTION,
- LSUP_BOOL_INTERSECTION,
- LSUP_BOOL_XOR,
- } LSUP_bool_op;
- typedef size_t LSUP_Key;
- typedef LSUP_Key LSUP_DoubleKey[2];
- typedef LSUP_Key LSUP_TripleKey[3];
- typedef LSUP_Key LSUP_QuadKey[4];
- typedef char uuid_str_t[UUIDSTR_SIZE];
- LSUP_rc
- mkdir_p (const char *path, mode_t mode);
- LSUP_rc
- rm_r (const char *path);
- const char *
- LSUP_strerror (LSUP_rc rc);
- inline int utf8_encode (const uint32_t utf, unsigned char *out)
- {
- if (utf <= 0x7F) {
-
- out[0] = (char) utf;
- out[1] = 0;
- return 1;
- }
- else if (utf <= 0x07FF) {
-
- out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
- out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
- out[2] = 0;
- return 2;
- }
- else if (utf <= 0xFFFF) {
-
- out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
- out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
- out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
- out[3] = 0;
- return 3;
- }
- else if (utf <= 0x10FFFF) {
-
- out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
- out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
- out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
- out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
- out[4] = 0;
- return 4;
- }
- else {
-
- out[0] = (char) 0xEF;
- out[1] = (char) 0xBF;
- out[2] = (char) 0xBD;
- out[3] = 0;
- return 0;
- }
- }
- #define LOG_RC(rc) do { \
- if ((rc) < 0) log_error (LSUP_strerror (rc)); \
- else if ((rc) > 0) log_debug (LSUP_strerror (rc)); \
- } while (0);
- #define CHECK(exp, marker) do { \
- LSUP_rc _rc = (exp); \
- LOG_RC(_rc); \
- if (UNLIKELY (_rc != LSUP_OK)) goto marker; \
- } while (0);
- #define PCHECK(exp, marker) do { \
- LSUP_rc _rc = (exp); \
- LOG_RC(_rc); \
- if (UNLIKELY (_rc < LSUP_OK)) goto marker; \
- } while (0);
- #define RCCK(exp) do { \
- LSUP_rc _rc = (exp); \
- LOG_RC(_rc); \
- if (UNLIKELY (_rc != LSUP_OK)) return _rc; \
- } while (0);
- #define PRCCK(exp) do { \
- LSUP_rc _rc = (exp); \
- LOG_RC(_rc); \
- if (UNLIKELY (_rc < LSUP_OK)) return _rc; \
- } while (0);
- #define RCNL(exp) do { \
- LSUP_rc _rc = (exp); \
- LOG_RC(_rc); \
- if (UNLIKELY (_rc != LSUP_OK)) return NULL; \
- } while (0);
- #define PRCNL(exp) do { \
- LSUP_rc _rc = (exp); \
- LOG_RC(_rc); \
- if (UNLIKELY (_rc < LSUP_OK)) return NULL; \
- } while (0);
- #define MALLOC_GUARD(var, rc) do { \
- (var) = malloc (sizeof *(var)); \
- if (UNLIKELY (var == NULL)) return (rc); \
- } while (0);
- #define CALLOC_GUARD(var, rc) do { \
- (var) = calloc (1, sizeof *(var)); \
- if (UNLIKELY (var == NULL)) return (rc); \
- } while (0);
- #endif
|