123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #ifndef _LSUP_CORE_H
- #define _LSUP_CORE_H
- #include <ctype.h>
- #include <errno.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>
- #ifdef DEBUG
- #define DEBUG_TEST 1
- #else
- #define DEBUG_TEST 0
- #endif
- #define STR "%s\n"
- #define TRACE(fmt, ...) \
- do {\
- if (DEBUG_TEST) \
- fprintf(stderr, "%s:%d:%s(): " fmt "\n", \
- __FILE__, __LINE__, __func__, __VA_ARGS__); \
- } while (0)
- #define LIKELY(x) __builtin_expect(!!(x), true)
- #define UNLIKELY(x) __builtin_expect(!!(x), false)
- // TODO Handle memory errors better.
- #define CRITICAL(exp) if (UNLIKELY(((exp) == NULL))) { abort(); }
- #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
- // Handy flags operations.
- #define SET_FLAG(n, f) ((n) |= (f))
- #define CLR_FLAG(n, f) ((n) &= ~(f))
- #define TGL_FLAG(n, f) ((n) ^= (f))
- #define CHK_FLAG(n, f) ((n) & (f))
- /* * * RETURN CODES * * */
- /**
- * 0 is success, positive integers (>88800) are warnings, and negative integers
- * (<-88800) are errors.
- */
- typedef enum {
- LSUP_OK = 0,
- LSUP_NOACTION = 88801,
- LSUP_NORESULT = 88802,
- LSUP_END = 88803,
- LSUP_ERROR = -88801,
- LSUP_PARSE_ERR = -88802,
- LSUP_VALUE_ERR = -88803,
- LSUP_TXN_ERR = -88804,
- LSUP_DB_ERR = -88805,
- } LSUP_rc;
- 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];
- // Don't use MIN and MAX macros: see
- // https://dustri.org/b/min-and-max-macro-considered-harmful.html
- inline int min(int x, int y) { return x < y ? x : y; }
- inline int max(int x, int y) { return x > y ? x : y; }
- /** @brief Make recursive directories.
- *
- * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
- */
- LSUP_rc mkdir_p(const char *path, mode_t mode);
- // Error handling via goto.
- #define CHECK(exp, rc, marker) (rc) = (exp); if ((rc) != LSUP_OK) goto marker
- // Jump if rc is negative (skip warnings).
- #define PCHECK(exp, rc, marker) (rc) = (exp); if ((rc) < LSUP_OK) goto marker
- #endif
|