Annotation of libelwix/inc/elwix.h, revision 1.33.2.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.33.2.1! misho       6: * $Id: elwix.h,v 1.33 2026/07/29 01:10:40 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.33.2.1! misho      65: #include <stdint.h>
        !            66: #include <limits.h>
        !            67: #ifdef __linux__
        !            68:        #include <endian.h>
        !            69:        #include <linux/if_packet.h>
1.15      misho      70: #else
1.33.2.1! misho      71:        #include <sys/endian.h>
        !            72:        #include <net/if_dl.h>
1.14      misho      73: #endif
1.1       misho      74: #include <netinet/in.h>
                     75: #include <arpa/inet.h>
                     76: 
1.14      misho      77: #include <elwix/aqueue.h>
1.1       misho      78: #include <elwix/atree.h>
                     79: #include <elwix/ampool.h>
                     80: #include <elwix/acrc.h>
                     81: #include <elwix/aarray.h>
                     82: #include <elwix/asarr.h>
                     83: #include <elwix/avar.h>
                     84: #include <elwix/astr.h>
                     85: #include <elwix/aregex.h>
1.2       misho      86: #include <elwix/aav.h>
1.1       misho      87: #include <elwix/anet.h>
1.2       misho      88: #include <elwix/atime.h>
1.7       misho      89: #include <elwix/apack.h>
1.9       misho      90: #include <elwix/apio.h>
1.16      misho      91: #include <elwix/ajson.h>
1.19      misho      92: #include <elwix/aiov.h>
1.20      misho      93: #include <elwix/aindex.h>
1.27      misho      94: #include <elwix/aring.h>
1.29      misho      95: #include <elwix/auuid.h>
1.1       misho      96: 
                     97: 
                     98: #ifndef STRSIZ
                     99: #define STRSIZ         256
                    100: #endif
                    101: 
1.6       misho     102: #ifndef NBBY
                    103: #define        NBBY            8               /* number of bits in a byte */
                    104: #endif
                    105: 
1.11      misho     106: #ifndef BYTE_ORDER
                    107:        #ifndef LITTLE_ENDIAN
                    108:                #define LITTLE_ENDIAN   1234
                    109:        #endif /* LITTLE_ENDIAN */
                    110:        #ifndef BIG_ENDIAN
                    111:                #define BIG_ENDIAN      4321
                    112:        #endif /* BIG_ENDIAN */
                    113:        #ifdef WORDS_BIGENDIAN
                    114:                #define BYTE_ORDER BIG_ENDIAN
                    115:        #else /* WORDS_BIGENDIAN */
                    116:                #define BYTE_ORDER LITTLE_ENDIAN
                    117:        #endif /* WORDS_BIGENDIAN */
                    118: #endif /* BYTE_ORDER */
                    119: 
1.1       misho     120: #ifndef be16toh
                    121: #define be16toh                betoh16
                    122: #endif
                    123: #ifndef be32toh
                    124: #define be32toh                betoh32
                    125: #endif
                    126: #ifndef be64toh
                    127: #define be64toh                betoh64
                    128: #endif
                    129: #ifndef le16toh
                    130: #define le16toh                letoh16
                    131: #endif
                    132: #ifndef le32toh
                    133: #define le32toh                letoh32
                    134: #endif
                    135: #ifndef le64toh
                    136: #define le64toh                letoh64
                    137: #endif
                    138: 
1.6       misho     139: /* Bit map related macros. */
                    140: #ifndef setbit
                    141: #define        setbit(a, i)            (((unsigned char *)(a))[(i) / NBBY] |= 1 << ((i) % NBBY))
                    142: #endif
                    143: #ifndef clrbit
                    144: #define        clrbit(a, i)            (((unsigned char *)(a))[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
                    145: #endif
                    146: #ifndef isset
                    147: #define        isset(a, i)             (((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY)))
                    148: #endif
                    149: #ifndef isclr
                    150: #define        isclr(a, i)             ((((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
                    151: #endif
                    152: 
                    153: /* Macros for counting and rounding. */
                    154: #ifndef howmany
                    155: #define        howmany(x, y)           (((x) + ((y) - 1)) / (y))
                    156: #endif
                    157: #ifndef nitems
                    158: #define        nitems(x)               (sizeof((x)) / sizeof((x)[0]))
                    159: #endif
                    160: #ifndef rounddown
                    161: #define        rounddown(x, y)         (((x) / (y)) * (y))
                    162: #endif
                    163: #ifndef rounddown2
                    164: #define        rounddown2(x, y)        ((x) & (~((y) - 1))) /* if y is power of two */
                    165: #endif
                    166: #ifndef roundup
                    167: #define        roundup(x, y)           ((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */
                    168: #endif
                    169: #ifndef roundup2
                    170: #define        roundup2(x, y)          (((x) + ((y) - 1)) & (~((y) - 1))) /* if y is powers of two */
                    171: #endif
                    172: #ifndef powerof2
                    173: #define powerof2(x)            ((((x) - 1) & (x)) == 0)
                    174: #endif
                    175: 
1.33      misho     176: /* timespec helpers */
                    177: /* Operations on timevals. */
                    178: #ifndef timerclear
                    179: #define        timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
                    180: #endif
                    181: 
                    182: #ifndef timerisset
                    183: #define        timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
                    184: #endif
                    185: 
                    186: #ifndef timercmp
                    187: #define        timercmp(tvp, uvp, cmp)                                         \
                    188:        (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
                    189:            ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
                    190:            ((tvp)->tv_sec cmp (uvp)->tv_sec))
                    191: #endif
                    192: 
                    193: #ifndef timeradd
                    194: #define        timeradd(tvp, uvp, vvp)                                         \
                    195:        do {                                                            \
                    196:                (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
                    197:                (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
                    198:                if ((vvp)->tv_usec >= 1000000) {                        \
                    199:                        (vvp)->tv_sec++;                                \
                    200:                        (vvp)->tv_usec -= 1000000;                      \
                    201:                }                                                       \
                    202:        } while (0)
                    203: #endif
                    204: 
                    205: #ifndef timersub
                    206: #define        timersub(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 < 0) {                               \
                    211:                        (vvp)->tv_sec--;                                \
                    212:                        (vvp)->tv_usec += 1000000;                      \
                    213:                }                                                       \
                    214:        } while (0)
                    215: #endif
                    216: 
                    217: /* Operations on timespecs. */
                    218: #ifndef timespecclear
                    219: #define        timespecclear(tsp)              ((tsp)->tv_sec = (tsp)->tv_nsec = 0)
                    220: #endif
                    221: 
                    222: #ifndef timespecisset
                    223: #define        timespecisset(tsp)              ((tsp)->tv_sec || (tsp)->tv_nsec)
                    224: #endif
                    225: 
                    226: #ifndef timespeccmp
                    227: #define        timespeccmp(tsp, usp, cmp)                                      \
                    228:        (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
                    229:            ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
                    230:            ((tsp)->tv_sec cmp (usp)->tv_sec))
                    231: #endif
                    232: 
                    233: #ifndef timespecadd
                    234: #define        timespecadd(tsp, usp, vsp)                                      \
                    235:        do {                                                            \
                    236:                (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
                    237:                (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
                    238:                if ((vsp)->tv_nsec >= 1000000000L) {                    \
                    239:                        (vsp)->tv_sec++;                                \
                    240:                        (vsp)->tv_nsec -= 1000000000L;                  \
                    241:                }                                                       \
                    242:        } while (0)
                    243: #endif
                    244: 
                    245: #ifndef timespecsub
                    246: #define        timespecsub(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 < 0) {                               \
                    251:                        (vsp)->tv_sec--;                                \
                    252:                        (vsp)->tv_nsec += 1000000000L;                  \
                    253:                }                                                       \
                    254:        } while (0)
                    255: #endif
                    256: 
1.6       misho     257: /* Macros for min/max. */
                    258: #ifndef MIN
                    259: #define        MIN(a, b)               (((a) < (b)) ? (a) : (b))
                    260: #endif
                    261: #ifndef MAX
                    262: #define        MAX(a, b)               (((a) > (b)) ? (a) : (b))
                    263: #endif
                    264: 
                    265: #define E_ALIGN(x, a)          (((x) + ((a) - 1)) & ~((a) - 1))
1.1       misho     266: 
                    267: 
1.6       misho     268: #define ELWIX_SYSM             0
                    269: #define ELWIX_MPOOL            1
1.1       misho     270: 
1.6       misho     271: #define VACUUM_LEFT            1
                    272: #define VACUUM_BETWEEN         2
1.1       misho     273: 
                    274: 
1.28      misho     275: typedef struct {
                    276:        E_ATOMIC_ALIGN int      value;
                    277: } e_atomic_int;
1.9       misho     278: 
1.26      misho     279: #ifdef __cplusplus
                    280: extern "C" {
                    281: #endif
1.9       misho     282: 
1.28      misho     283: extern int __isthreaded;
                    284: 
1.1       misho     285: // elwix_SetProg() Set program memory pool name
1.3       misho     286: void elwix_SetProg(const char *csProgName);
1.1       misho     287: // elwix_GetProg() Get program memory pool name
1.3       misho     288: const char *elwix_GetProg();
1.1       misho     289: 
                    290: // elwix_GetErrno() Get error code of last operation
1.3       misho     291: int elwix_GetErrno();
1.1       misho     292: // elwix_GetError() Get error text of last operation
1.3       misho     293: const char *elwix_GetError();
1.1       misho     294: 
                    295: 
                    296: // elwix_mm_inuse() Check for memory management model
1.3       misho     297: int elwix_mm_inuse();
1.1       misho     298: 
                    299: 
                    300: /*
                    301:  * elwixInit() - Init libelwix library memory management
                    302:  *
                    303:  * @mm = memory management (IO_SYSM or IO_MPOOL)
                    304:  * @maxmem = memory limit
                    305:  * return: -1 error or !=-1 used memory management model
                    306:  */
1.3       misho     307: int elwixInit(int mm, unsigned long maxmem);
1.1       misho     308: /*
                    309:  * elwixFini() - Finish libelwix library memory management
                    310:  *
                    311:  * return: none
                    312:  */
1.3       misho     313: void elwixFini();
1.1       misho     314: 
1.11      misho     315: /*
                    316:  * elwix_byteOrder() - Detect platform byte order
                    317:  *
                    318:  * return: 1 = little endian or 0 big endian
                    319:  */
                    320: int elwix_byteOrder();
1.14      misho     321: 
                    322: #ifndef strlcpy
                    323: /*
                    324:  * Copy src to string dst of size siz.  At most siz-1 characters
                    325:  * will be copied.  Always NUL terminates (unless siz == 0).
                    326:  * Returns strlen(src); if retval >= siz, truncation occurred.
                    327:  */
                    328: size_t strlcpy(char *dst, const char *src, size_t siz);
                    329: #endif
                    330: #ifndef strlcat
                    331: /*
                    332:  * Appends src to string dst of size siz (unlike strncat, siz is the
                    333:  * full size of dst, not space left).  At most siz-1 characters
                    334:  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
                    335:  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
                    336:  * If retval >= siz, truncation occurred.
                    337:  */
                    338: size_t strlcat(char *dst, const char *src, size_t siz);
                    339: #endif
1.11      misho     340: 
1.1       misho     341: /* memory management hooks */
                    342: extern void *(*e_malloc)(size_t);
                    343: extern void *(*e_calloc)(size_t, size_t);
                    344: extern void *(*e_realloc)(void*, size_t);
                    345: extern char *(*e_strdup)(const char*);
                    346: extern void (*e_free)(void*);
                    347: 
                    348: 
                    349: /* Debug helper macros */
                    350: 
                    351: /* Verbose macros */
                    352: extern int elwix_Verbose;
1.21      misho     353: #define e_Verbose              elwix_Verbose
1.1       misho     354: #define e_initVerbose(x)       (elwix_Verbose = (x))
                    355: #define e_incVerbose           (elwix_Verbose++)
                    356: #define e_decVerbose           (elwix_Verbose--)
                    357: 
1.2       misho     358: #define EVERBS(x)              if ((x) <= elwix_Verbose)
                    359: #define EVERBOSE(x, fmt, ...)  do { assert((fmt)); \
                    360:                                        if ((x) <= elwix_Verbose) { \
1.26      misho     361:                                                char __str[BUFSIZ] = { 0 }; \
1.18      misho     362:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
                    363:                                                syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
                    364:                                                                (x), __func__, __LINE__, __str); \
                    365:                                        } \
                    366:                                } while (0)
                    367: #define EVERBOSE2(x, fmt, ...) do { assert((fmt)); \
                    368:                                        if ((x) <= elwix_Verbose) { \
1.26      misho     369:                                                char __str[0x10000] = { 0 }; \
1.8       misho     370:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.16      misho     371:                                                syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
1.8       misho     372:                                                                (x), __func__, __LINE__, __str); \
1.2       misho     373:                                        } \
                    374:                                } while (0)
1.1       misho     375: 
                    376: /* Debug macros */
                    377: extern int elwix_Debug;
1.16      misho     378: #define e_Debug                        elwix_Debug
                    379: 
1.1       misho     380: #define ELWIX_DEBUG_OFF                0x0
                    381: #define ELWIX_DEBUG_TRACE      0x1
                    382: #define ELWIX_DEBUG_LOG                0x2
1.22      misho     383: #define ELWIX_DEBUG_MEM                0x4
                    384: #define ELWIX_DEBUG_MEMORY     ELWIX_DEBUG_MEM
                    385: #define ELWIX_DEBUG_IO         0x8
                    386: #define ELWIX_DEBUG_IPC                0x10
                    387: #define ELWIX_DEBUG_LOCK       0x20
                    388: #define ELWIX_DEBUG_SYS                0x40
                    389: #define ELWIX_DEBUG_NET                0x80
1.31      misho     390: #define ELWIX_DEBUG_1          0x100
                    391: #define ELWIX_DEBUG_2          0x200
                    392: #define ELWIX_DEBUG_3          0x400
                    393: #define ELWIX_DEBUG_4          0x800
1.1       misho     394: #define ELWIX_DEBUG_ANY                0xFFFFFFFF
                    395: 
1.24      misho     396: #define EDBG(x)                        (elwix_Debug & (x))
                    397: #define EDBGS(x)               if (EDBG(x))
1.5       misho     398: #define ETRACE()               if (elwix_Debug & ELWIX_DEBUG_TRACE) \
1.4       misho     399:                                           syslog(LOG_DEBUG, "I'm in %s(%d)\n", __func__, __LINE__)
1.1       misho     400: #define EDEBUG(x, fmt, ...)    do { assert((fmt)); \
                    401:                                        if ((x) & elwix_Debug) { \
1.26      misho     402:                                                char __str[BUFSIZ] = { 0 }; \
1.8       misho     403:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     404:                                                syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \
1.8       misho     405:                                                                (x), __func__, __LINE__, __str); \
1.1       misho     406:                                        } \
                    407:                                } while (0)
                    408: 
                    409: /* Logger macro */
                    410: #define ELOGGER(x, fmt, ...)   do { assert((fmt)); \
1.26      misho     411:                                        char __str[BUFSIZ] = { 0 }; \
1.8       misho     412:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.26      misho     413:                                        syslog((x), "Log:%s(%d): %s\n", \
1.8       misho     414:                                                                __func__, __LINE__, __str); \
1.1       misho     415:                                } while (0)
                    416: 
1.12      misho     417: #define EWARNING(x, fmt, ...)  do { assert((fmt)); \
1.26      misho     418:                                        char __str[BUFSIZ] = { 0 }; \
1.12      misho     419:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
                    420:                                        syslog(LOG_WARNING, "Warning:%s(%d): #%d - %s\n", \
                    421:                                                         __func__, __LINE__, (x), __str); \
                    422:                                } while (0)
1.1       misho     423: /* Error state macros */
                    424: #define EERROR(x, fmt, ...)    do { assert((fmt)); \
1.26      misho     425:                                        char __str[BUFSIZ] = { 0 }; \
1.8       misho     426:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     427:                                        syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \
1.8       misho     428:                                                         __func__, __LINE__, (x), __str); \
1.1       misho     429:                                } while (0)
                    430: #define ESYSERR(x)             do { \
1.17      misho     431:                                        if (x > 0 || errno) { \
                    432:                                                int _ern = errno; \
1.1       misho     433:                                                syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \
                    434:                                                                __func__, __LINE__, x > 0 ? x : errno, \
                    435:                                                                strerror(x > 0 ? x : errno)); \
1.17      misho     436:                                                errno = _ern; \
                    437:                                        } \
1.1       misho     438:                                } while (0)
                    439: #define ELIBERR(ait)           do { \
                    440:                                        if (ait##_GetErrno()) \
                    441:                                                syslog(LOG_ERR, "Error(lib):%s(%d): #%d - %s\n", \
                    442:                                                                __func__, __LINE__, ait##_GetErrno(), \
                    443:                                                                ait##_GetError()); \
                    444:                                } while (0)
                    445: 
                    446: 
1.26      misho     447: #ifdef __cplusplus
                    448: }
                    449: #endif
                    450: 
1.1       misho     451: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>