Annotation of libelwix/inc/elwix.h, revision 1.32.4.1
1.1 misho 1: /*************************************************************************
2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.32.4.1! misho 6: * $Id: elwix.h,v 1.32 2026/05/11 15:53:20 misho Exp $
1.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.30 misho 15: Copyright 2004 - 2026
1.1 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #ifndef __ELWIX_H
47: #define __ELWIX_H
48:
49:
1.25 misho 50: #include <stdio.h>
1.1 misho 51: #include <assert.h>
52: #include <syslog.h>
1.14 misho 53: #include <stdarg.h>
1.27 misho 54: #include <stdalign.h>
1.1 misho 55: #include <sys/types.h>
1.6 misho 56: #include <sys/param.h>
1.14 misho 57: #include <sys/socket.h>
58: #include <sys/un.h>
1.32 misho 59: #ifndef __cplusplus
60: #include <stdatomic.h>
61: #else
62: #include <atomic>
63: #define _Atomic(X) std::atomic<X>
64: #endif
1.14 misho 65: #ifndef __linux__
1.6 misho 66: #include <sys/limits.h>
1.8 misho 67: #include <sys/endian.h>
1.1 misho 68: #include <net/if_dl.h>
1.15 misho 69: #else
70: #include <linux/if_packet.h>
1.14 misho 71: #endif
1.1 misho 72: #include <netinet/in.h>
73: #include <arpa/inet.h>
74:
1.14 misho 75: #include <elwix/aqueue.h>
1.1 misho 76: #include <elwix/atree.h>
77: #include <elwix/ampool.h>
78: #include <elwix/acrc.h>
79: #include <elwix/aarray.h>
80: #include <elwix/asarr.h>
81: #include <elwix/avar.h>
82: #include <elwix/astr.h>
83: #include <elwix/aregex.h>
1.2 misho 84: #include <elwix/aav.h>
1.1 misho 85: #include <elwix/anet.h>
1.2 misho 86: #include <elwix/atime.h>
1.7 misho 87: #include <elwix/apack.h>
1.9 misho 88: #include <elwix/apio.h>
1.16 misho 89: #include <elwix/ajson.h>
1.19 misho 90: #include <elwix/aiov.h>
1.20 misho 91: #include <elwix/aindex.h>
1.27 misho 92: #include <elwix/aring.h>
1.29 misho 93: #include <elwix/auuid.h>
1.1 misho 94:
95:
96: #ifndef STRSIZ
97: #define STRSIZ 256
98: #endif
99:
1.6 misho 100: #ifndef NBBY
101: #define NBBY 8 /* number of bits in a byte */
102: #endif
103:
1.11 misho 104: #ifndef BYTE_ORDER
105: #ifndef LITTLE_ENDIAN
106: #define LITTLE_ENDIAN 1234
107: #endif /* LITTLE_ENDIAN */
108: #ifndef BIG_ENDIAN
109: #define BIG_ENDIAN 4321
110: #endif /* BIG_ENDIAN */
111: #ifdef WORDS_BIGENDIAN
112: #define BYTE_ORDER BIG_ENDIAN
113: #else /* WORDS_BIGENDIAN */
114: #define BYTE_ORDER LITTLE_ENDIAN
115: #endif /* WORDS_BIGENDIAN */
116: #endif /* BYTE_ORDER */
117:
1.1 misho 118: #ifndef be16toh
119: #define be16toh betoh16
120: #endif
121: #ifndef be32toh
122: #define be32toh betoh32
123: #endif
124: #ifndef be64toh
125: #define be64toh betoh64
126: #endif
127: #ifndef le16toh
128: #define le16toh letoh16
129: #endif
130: #ifndef le32toh
131: #define le32toh letoh32
132: #endif
133: #ifndef le64toh
134: #define le64toh letoh64
135: #endif
136:
1.6 misho 137: /* Bit map related macros. */
138: #ifndef setbit
139: #define setbit(a, i) (((unsigned char *)(a))[(i) / NBBY] |= 1 << ((i) % NBBY))
140: #endif
141: #ifndef clrbit
142: #define clrbit(a, i) (((unsigned char *)(a))[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
143: #endif
144: #ifndef isset
145: #define isset(a, i) (((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY)))
146: #endif
147: #ifndef isclr
148: #define isclr(a, i) ((((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
149: #endif
150:
151: /* Macros for counting and rounding. */
152: #ifndef howmany
153: #define howmany(x, y) (((x) + ((y) - 1)) / (y))
154: #endif
155: #ifndef nitems
156: #define nitems(x) (sizeof((x)) / sizeof((x)[0]))
157: #endif
158: #ifndef rounddown
159: #define rounddown(x, y) (((x) / (y)) * (y))
160: #endif
161: #ifndef rounddown2
162: #define rounddown2(x, y) ((x) & (~((y) - 1))) /* if y is power of two */
163: #endif
164: #ifndef roundup
165: #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */
166: #endif
167: #ifndef roundup2
168: #define roundup2(x, y) (((x) + ((y) - 1)) & (~((y) - 1))) /* if y is powers of two */
169: #endif
170: #ifndef powerof2
171: #define powerof2(x) ((((x) - 1) & (x)) == 0)
172: #endif
173:
1.32.4.1! misho 174: /* timespec helpers */
! 175: #ifndef TIMEVAL_TO_TIMESPEC
! 176: #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
! 177: (ts)->tv_sec = (tv)->tv_sec; \
! 178: (ts)->tv_nsec = (tv)->tv_usec * 1000; \
! 179: }
! 180: #endif
! 181:
! 182: #ifndef TIMESPEC_TO_TIMEVAL
! 183: #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
! 184: (tv)->tv_sec = (ts)->tv_sec; \
! 185: (tv)->tv_usec = (ts)->tv_nsec / 1000; \
! 186: }
! 187: #endif
! 188:
! 189: /* Operations on timevals. */
! 190: #ifndef timerclear
! 191: #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
! 192: #endif
! 193:
! 194: #ifndef timerisset
! 195: #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
! 196: #endif
! 197:
! 198: #ifndef timercmp
! 199: #define timercmp(tvp, uvp, cmp) \
! 200: (((tvp)->tv_sec == (uvp)->tv_sec) ? \
! 201: ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
! 202: ((tvp)->tv_sec cmp (uvp)->tv_sec))
! 203: #endif
! 204:
! 205: #ifndef timeradd
! 206: #define timeradd(tvp, uvp, vvp) \
! 207: do { \
! 208: (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
! 209: (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
! 210: if ((vvp)->tv_usec >= 1000000) { \
! 211: (vvp)->tv_sec++; \
! 212: (vvp)->tv_usec -= 1000000; \
! 213: } \
! 214: } while (0)
! 215: #endif
! 216:
! 217: #ifndef timersub
! 218: #define timersub(tvp, uvp, vvp) \
! 219: do { \
! 220: (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
! 221: (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
! 222: if ((vvp)->tv_usec < 0) { \
! 223: (vvp)->tv_sec--; \
! 224: (vvp)->tv_usec += 1000000; \
! 225: } \
! 226: } while (0)
! 227: #endif
! 228:
! 229: /* Operations on timespecs. */
! 230: #ifndef timespecclear
! 231: #define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
! 232: #endif
! 233:
! 234: #ifndef timespecisset
! 235: #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
! 236: #endif
! 237:
! 238: #ifndef timespeccmp
! 239: #define timespeccmp(tsp, usp, cmp) \
! 240: (((tsp)->tv_sec == (usp)->tv_sec) ? \
! 241: ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
! 242: ((tsp)->tv_sec cmp (usp)->tv_sec))
! 243: #endif
! 244:
! 245: #ifndef timespecadd
! 246: #define timespecadd(tsp, usp, vsp) \
! 247: do { \
! 248: (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
! 249: (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
! 250: if ((vsp)->tv_nsec >= 1000000000L) { \
! 251: (vsp)->tv_sec++; \
! 252: (vsp)->tv_nsec -= 1000000000L; \
! 253: } \
! 254: } while (0)
! 255: #endif
! 256:
! 257: #ifndef timespecsub
! 258: #define timespecsub(tsp, usp, vsp) \
! 259: do { \
! 260: (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
! 261: (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
! 262: if ((vsp)->tv_nsec < 0) { \
! 263: (vsp)->tv_sec--; \
! 264: (vsp)->tv_nsec += 1000000000L; \
! 265: } \
! 266: } while (0)
! 267: #endif
! 268:
1.6 misho 269: /* Macros for min/max. */
270: #ifndef MIN
271: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
272: #endif
273: #ifndef MAX
274: #define MAX(a, b) (((a) > (b)) ? (a) : (b))
275: #endif
276:
277: #define E_ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
1.1 misho 278:
279:
1.6 misho 280: #define ELWIX_SYSM 0
281: #define ELWIX_MPOOL 1
1.1 misho 282:
1.6 misho 283: #define VACUUM_LEFT 1
284: #define VACUUM_BETWEEN 2
1.1 misho 285:
286:
1.28 misho 287: typedef struct {
288: E_ATOMIC_ALIGN int value;
289: } e_atomic_int;
1.9 misho 290:
1.26 misho 291: #ifdef __cplusplus
292: extern "C" {
293: #endif
1.9 misho 294:
1.28 misho 295: extern int __isthreaded;
296:
1.1 misho 297: // elwix_SetProg() Set program memory pool name
1.3 misho 298: void elwix_SetProg(const char *csProgName);
1.1 misho 299: // elwix_GetProg() Get program memory pool name
1.3 misho 300: const char *elwix_GetProg();
1.1 misho 301:
302: // elwix_GetErrno() Get error code of last operation
1.3 misho 303: int elwix_GetErrno();
1.1 misho 304: // elwix_GetError() Get error text of last operation
1.3 misho 305: const char *elwix_GetError();
1.1 misho 306:
307:
308: // elwix_mm_inuse() Check for memory management model
1.3 misho 309: int elwix_mm_inuse();
1.1 misho 310:
311:
312: /*
313: * elwixInit() - Init libelwix library memory management
314: *
315: * @mm = memory management (IO_SYSM or IO_MPOOL)
316: * @maxmem = memory limit
317: * return: -1 error or !=-1 used memory management model
318: */
1.3 misho 319: int elwixInit(int mm, unsigned long maxmem);
1.1 misho 320: /*
321: * elwixFini() - Finish libelwix library memory management
322: *
323: * return: none
324: */
1.3 misho 325: void elwixFini();
1.1 misho 326:
1.11 misho 327: /*
328: * elwix_byteOrder() - Detect platform byte order
329: *
330: * return: 1 = little endian or 0 big endian
331: */
332: int elwix_byteOrder();
1.14 misho 333:
334: #ifndef strlcpy
335: /*
336: * Copy src to string dst of size siz. At most siz-1 characters
337: * will be copied. Always NUL terminates (unless siz == 0).
338: * Returns strlen(src); if retval >= siz, truncation occurred.
339: */
340: size_t strlcpy(char *dst, const char *src, size_t siz);
341: #endif
342: #ifndef strlcat
343: /*
344: * Appends src to string dst of size siz (unlike strncat, siz is the
345: * full size of dst, not space left). At most siz-1 characters
346: * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
347: * Returns strlen(src) + MIN(siz, strlen(initial dst)).
348: * If retval >= siz, truncation occurred.
349: */
350: size_t strlcat(char *dst, const char *src, size_t siz);
351: #endif
1.11 misho 352:
1.1 misho 353: /* memory management hooks */
354: extern void *(*e_malloc)(size_t);
355: extern void *(*e_calloc)(size_t, size_t);
356: extern void *(*e_realloc)(void*, size_t);
357: extern char *(*e_strdup)(const char*);
358: extern void (*e_free)(void*);
359:
360:
361: /* Debug helper macros */
362:
363: /* Verbose macros */
364: extern int elwix_Verbose;
1.21 misho 365: #define e_Verbose elwix_Verbose
1.1 misho 366: #define e_initVerbose(x) (elwix_Verbose = (x))
367: #define e_incVerbose (elwix_Verbose++)
368: #define e_decVerbose (elwix_Verbose--)
369:
1.2 misho 370: #define EVERBS(x) if ((x) <= elwix_Verbose)
371: #define EVERBOSE(x, fmt, ...) do { assert((fmt)); \
372: if ((x) <= elwix_Verbose) { \
1.26 misho 373: char __str[BUFSIZ] = { 0 }; \
1.18 misho 374: snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
375: syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
376: (x), __func__, __LINE__, __str); \
377: } \
378: } while (0)
379: #define EVERBOSE2(x, fmt, ...) do { assert((fmt)); \
380: if ((x) <= elwix_Verbose) { \
1.26 misho 381: char __str[0x10000] = { 0 }; \
1.8 misho 382: snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.16 misho 383: syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
1.8 misho 384: (x), __func__, __LINE__, __str); \
1.2 misho 385: } \
386: } while (0)
1.1 misho 387:
388: /* Debug macros */
389: extern int elwix_Debug;
1.16 misho 390: #define e_Debug elwix_Debug
391:
1.1 misho 392: #define ELWIX_DEBUG_OFF 0x0
393: #define ELWIX_DEBUG_TRACE 0x1
394: #define ELWIX_DEBUG_LOG 0x2
1.22 misho 395: #define ELWIX_DEBUG_MEM 0x4
396: #define ELWIX_DEBUG_MEMORY ELWIX_DEBUG_MEM
397: #define ELWIX_DEBUG_IO 0x8
398: #define ELWIX_DEBUG_IPC 0x10
399: #define ELWIX_DEBUG_LOCK 0x20
400: #define ELWIX_DEBUG_SYS 0x40
401: #define ELWIX_DEBUG_NET 0x80
1.31 misho 402: #define ELWIX_DEBUG_1 0x100
403: #define ELWIX_DEBUG_2 0x200
404: #define ELWIX_DEBUG_3 0x400
405: #define ELWIX_DEBUG_4 0x800
1.1 misho 406: #define ELWIX_DEBUG_ANY 0xFFFFFFFF
407:
1.24 misho 408: #define EDBG(x) (elwix_Debug & (x))
409: #define EDBGS(x) if (EDBG(x))
1.5 misho 410: #define ETRACE() if (elwix_Debug & ELWIX_DEBUG_TRACE) \
1.4 misho 411: syslog(LOG_DEBUG, "I'm in %s(%d)\n", __func__, __LINE__)
1.1 misho 412: #define EDEBUG(x, fmt, ...) do { assert((fmt)); \
413: if ((x) & elwix_Debug) { \
1.26 misho 414: char __str[BUFSIZ] = { 0 }; \
1.8 misho 415: snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1 misho 416: syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \
1.8 misho 417: (x), __func__, __LINE__, __str); \
1.1 misho 418: } \
419: } while (0)
420:
421: /* Logger macro */
422: #define ELOGGER(x, fmt, ...) do { assert((fmt)); \
1.26 misho 423: char __str[BUFSIZ] = { 0 }; \
1.8 misho 424: snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.26 misho 425: syslog((x), "Log:%s(%d): %s\n", \
1.8 misho 426: __func__, __LINE__, __str); \
1.1 misho 427: } while (0)
428:
1.12 misho 429: #define EWARNING(x, fmt, ...) do { assert((fmt)); \
1.26 misho 430: char __str[BUFSIZ] = { 0 }; \
1.12 misho 431: snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
432: syslog(LOG_WARNING, "Warning:%s(%d): #%d - %s\n", \
433: __func__, __LINE__, (x), __str); \
434: } while (0)
1.1 misho 435: /* Error state macros */
436: #define EERROR(x, fmt, ...) do { assert((fmt)); \
1.26 misho 437: char __str[BUFSIZ] = { 0 }; \
1.8 misho 438: snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1 misho 439: syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \
1.8 misho 440: __func__, __LINE__, (x), __str); \
1.1 misho 441: } while (0)
442: #define ESYSERR(x) do { \
1.17 misho 443: if (x > 0 || errno) { \
444: int _ern = errno; \
1.1 misho 445: syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \
446: __func__, __LINE__, x > 0 ? x : errno, \
447: strerror(x > 0 ? x : errno)); \
1.17 misho 448: errno = _ern; \
449: } \
1.1 misho 450: } while (0)
451: #define ELIBERR(ait) do { \
452: if (ait##_GetErrno()) \
453: syslog(LOG_ERR, "Error(lib):%s(%d): #%d - %s\n", \
454: __func__, __LINE__, ait##_GetErrno(), \
455: ait##_GetError()); \
456: } while (0)
457:
458:
1.26 misho 459: #ifdef __cplusplus
460: }
461: #endif
462:
1.1 misho 463: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>