#ifndef _LSUP_CORE_H #define _LSUP_CORE_H #include #include #include #include #include #include #include #include #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_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; } // Error handling via goto. #define CHECK(exp, marker) rc = (exp); if (rc != LSUP_OK) goto marker // Jump if rc is negative (skip warnings). #define PCHECK(exp, marker) rc = (exp); if (rc < LSUP_OK) goto marker // Check against a list of allowed return codes. // If none match, jump to marker. #define MCHECK(exp, allowed, marker) \ rc = (exp); \ bool jump = true; \ for(int i = 0; i < sizeof(allowed) / sizeof(int); i++) { \ if (rc == allowed[i]) { \ jump = false; \ break; \ } \ } \ if (jump) goto marker; \ #endif