--- libelwix/inc/elwix.h 2017/11/30 13:46:27 1.16 +++ libelwix/inc/elwix.h 2026/07/29 01:09:33 1.32.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: elwix.h,v 1.16 2017/11/30 13:46:27 misho Exp $ +* $Id: elwix.h,v 1.32.4.2 2026/07/29 01:09:33 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2017 +Copyright 2004 - 2026 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -47,13 +47,21 @@ SUCH DAMAGE. #define __ELWIX_H +#include #include #include #include +#include #include #include #include #include +#ifndef __cplusplus + #include +#else + #include + #define _Atomic(X) std::atomic +#endif #ifndef __linux__ #include #include @@ -79,6 +87,10 @@ SUCH DAMAGE. #include #include #include +#include +#include +#include +#include #ifndef STRSIZ @@ -159,6 +171,87 @@ SUCH DAMAGE. #define powerof2(x) ((((x) - 1) & (x)) == 0) #endif +/* timespec helpers */ +/* Operations on timevals. */ +#ifndef timerclear +#define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) +#endif + +#ifndef timerisset +#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) +#endif + +#ifndef timercmp +#define timercmp(tvp, uvp, cmp) \ + (((tvp)->tv_sec == (uvp)->tv_sec) ? \ + ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ + ((tvp)->tv_sec cmp (uvp)->tv_sec)) +#endif + +#ifndef timeradd +#define timeradd(tvp, uvp, vvp) \ + do { \ + (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ + (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ + if ((vvp)->tv_usec >= 1000000) { \ + (vvp)->tv_sec++; \ + (vvp)->tv_usec -= 1000000; \ + } \ + } while (0) +#endif + +#ifndef timersub +#define timersub(tvp, uvp, vvp) \ + do { \ + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ + if ((vvp)->tv_usec < 0) { \ + (vvp)->tv_sec--; \ + (vvp)->tv_usec += 1000000; \ + } \ + } while (0) +#endif + +/* Operations on timespecs. */ +#ifndef timespecclear +#define timespecclear(tsp) ((tsp)->tv_sec = (tsp)->tv_nsec = 0) +#endif + +#ifndef timespecisset +#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec) +#endif + +#ifndef timespeccmp +#define timespeccmp(tsp, usp, cmp) \ + (((tsp)->tv_sec == (usp)->tv_sec) ? \ + ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \ + ((tsp)->tv_sec cmp (usp)->tv_sec)) +#endif + +#ifndef timespecadd +#define timespecadd(tsp, usp, vsp) \ + do { \ + (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \ + (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \ + if ((vsp)->tv_nsec >= 1000000000L) { \ + (vsp)->tv_sec++; \ + (vsp)->tv_nsec -= 1000000000L; \ + } \ + } while (0) +#endif + +#ifndef timespecsub +#define timespecsub(tsp, usp, vsp) \ + do { \ + (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ + (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ + if ((vsp)->tv_nsec < 0) { \ + (vsp)->tv_sec--; \ + (vsp)->tv_nsec += 1000000000L; \ + } \ + } while (0) +#endif + /* Macros for min/max. */ #ifndef MIN #define MIN(a, b) (((a) < (b)) ? (a) : (b)) @@ -177,9 +270,16 @@ SUCH DAMAGE. #define VACUUM_BETWEEN 2 -extern int __isthreaded; +typedef struct { + E_ATOMIC_ALIGN int value; +} e_atomic_int; +#ifdef __cplusplus +extern "C" { +#endif +extern int __isthreaded; + // elwix_SetProg() Set program memory pool name void elwix_SetProg(const char *csProgName); // elwix_GetProg() Get program memory pool name @@ -248,6 +348,7 @@ extern void (*e_free)(void*); /* Verbose macros */ extern int elwix_Verbose; +#define e_Verbose elwix_Verbose #define e_initVerbose(x) (elwix_Verbose = (x)) #define e_incVerbose (elwix_Verbose++) #define e_decVerbose (elwix_Verbose--) @@ -255,12 +356,20 @@ extern int elwix_Verbose; #define EVERBS(x) if ((x) <= elwix_Verbose) #define EVERBOSE(x, fmt, ...) do { assert((fmt)); \ if ((x) <= elwix_Verbose) { \ - char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \ + char __str[BUFSIZ] = { 0 }; \ snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \ syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \ (x), __func__, __LINE__, __str); \ } \ } while (0) +#define EVERBOSE2(x, fmt, ...) do { assert((fmt)); \ + if ((x) <= elwix_Verbose) { \ + char __str[0x10000] = { 0 }; \ + snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \ + syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \ + (x), __func__, __LINE__, __str); \ + } \ + } while (0) /* Debug macros */ extern int elwix_Debug; @@ -269,13 +378,26 @@ extern int elwix_Debug; #define ELWIX_DEBUG_OFF 0x0 #define ELWIX_DEBUG_TRACE 0x1 #define ELWIX_DEBUG_LOG 0x2 +#define ELWIX_DEBUG_MEM 0x4 +#define ELWIX_DEBUG_MEMORY ELWIX_DEBUG_MEM +#define ELWIX_DEBUG_IO 0x8 +#define ELWIX_DEBUG_IPC 0x10 +#define ELWIX_DEBUG_LOCK 0x20 +#define ELWIX_DEBUG_SYS 0x40 +#define ELWIX_DEBUG_NET 0x80 +#define ELWIX_DEBUG_1 0x100 +#define ELWIX_DEBUG_2 0x200 +#define ELWIX_DEBUG_3 0x400 +#define ELWIX_DEBUG_4 0x800 #define ELWIX_DEBUG_ANY 0xFFFFFFFF +#define EDBG(x) (elwix_Debug & (x)) +#define EDBGS(x) if (EDBG(x)) #define ETRACE() if (elwix_Debug & ELWIX_DEBUG_TRACE) \ syslog(LOG_DEBUG, "I'm in %s(%d)\n", __func__, __LINE__) #define EDEBUG(x, fmt, ...) do { assert((fmt)); \ if ((x) & elwix_Debug) { \ - char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \ + char __str[BUFSIZ] = { 0 }; \ snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \ syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \ (x), __func__, __LINE__, __str); \ @@ -284,30 +406,33 @@ extern int elwix_Debug; /* Logger macro */ #define ELOGGER(x, fmt, ...) do { assert((fmt)); \ - char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \ + char __str[BUFSIZ] = { 0 }; \ snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \ - syslog((x), "Logger:%s(%d): %s\n", \ + syslog((x), "Log:%s(%d): %s\n", \ __func__, __LINE__, __str); \ } while (0) #define EWARNING(x, fmt, ...) do { assert((fmt)); \ - char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \ + char __str[BUFSIZ] = { 0 }; \ snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \ syslog(LOG_WARNING, "Warning:%s(%d): #%d - %s\n", \ __func__, __LINE__, (x), __str); \ } while (0) /* Error state macros */ #define EERROR(x, fmt, ...) do { assert((fmt)); \ - char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \ + char __str[BUFSIZ] = { 0 }; \ snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \ syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \ __func__, __LINE__, (x), __str); \ } while (0) #define ESYSERR(x) do { \ - if (x > 0 || errno) \ + if (x > 0 || errno) { \ + int _ern = errno; \ syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \ __func__, __LINE__, x > 0 ? x : errno, \ strerror(x > 0 ? x : errno)); \ + errno = _ern; \ + } \ } while (0) #define ELIBERR(ait) do { \ if (ait##_GetErrno()) \ @@ -316,5 +441,9 @@ extern int elwix_Debug; ait##_GetError()); \ } while (0) + +#ifdef __cplusplus +} +#endif #endif