#ifndef _LSUP_CORE_H #define _LSUP_CORE_H #include #include #include #include #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() // TODO Cross-platform ramdisk path. #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 // 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_NOT_IMPL_ERR = -88806, LSUP_IO_ERR = -88807, LSUP_MEM_ERR = -88808, } LSUP_rc; 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]; // Yes, a textbook mistake; but writing min and max for all int types is crazy. #define min(x, y) (x) < (y) ? (x) : (y) #define max(x, y) (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); /** @brief Remove a directory recursively, as in Unix "rm -r". * * @param path[in] Path of directory to remove. */ LSUP_rc rm_r (const char *path); // 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 // Return rc if it is of LSUP_rc type and is negative (=error) #define RCCK(exp) LSUP_rc _rc = (exp); if (_rc < 0) return _rc // Return NULL if it is of LSUP_rc type and is negative (=error) #define RCNL(exp) if((exp) < 0) return NULL #endif