Annotation of libelwix/inc/elwix.h, revision 1.18.8.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.18.8.1! misho       6: * $Id: elwix.h,v 1.18 2019/12/30 18:11:16 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.18.8.1! misho      15: Copyright 2004 - 2021
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: 
                     50: #include <assert.h>
                     51: #include <syslog.h>
1.14      misho      52: #include <stdarg.h>
1.1       misho      53: #include <sys/types.h>
1.6       misho      54: #include <sys/param.h>
1.14      misho      55: #include <sys/socket.h>
                     56: #include <sys/un.h>
                     57: #ifndef __linux__
1.6       misho      58: #include <sys/limits.h>
1.8       misho      59: #include <sys/endian.h>
1.1       misho      60: #include <net/if_dl.h>
1.15      misho      61: #else
                     62: #include <linux/if_packet.h>
1.14      misho      63: #endif
1.1       misho      64: #include <netinet/in.h>
                     65: #include <arpa/inet.h>
                     66: 
1.14      misho      67: #include <elwix/aqueue.h>
1.1       misho      68: #include <elwix/atree.h>
                     69: #include <elwix/ampool.h>
                     70: #include <elwix/acrc.h>
                     71: #include <elwix/aarray.h>
                     72: #include <elwix/asarr.h>
                     73: #include <elwix/avar.h>
                     74: #include <elwix/astr.h>
                     75: #include <elwix/aregex.h>
1.2       misho      76: #include <elwix/aav.h>
1.1       misho      77: #include <elwix/anet.h>
1.2       misho      78: #include <elwix/atime.h>
1.7       misho      79: #include <elwix/apack.h>
1.9       misho      80: #include <elwix/apio.h>
1.16      misho      81: #include <elwix/ajson.h>
1.18.8.1! misho      82: #include <elwix/aiov.h>
1.1       misho      83: 
                     84: 
                     85: #ifndef STRSIZ
                     86: #define STRSIZ         256
                     87: #endif
                     88: 
1.6       misho      89: #ifndef NBBY
                     90: #define        NBBY            8               /* number of bits in a byte */
                     91: #endif
                     92: 
1.11      misho      93: #ifndef BYTE_ORDER
                     94:        #ifndef LITTLE_ENDIAN
                     95:                #define LITTLE_ENDIAN   1234
                     96:        #endif /* LITTLE_ENDIAN */
                     97:        #ifndef BIG_ENDIAN
                     98:                #define BIG_ENDIAN      4321
                     99:        #endif /* BIG_ENDIAN */
                    100:        #ifdef WORDS_BIGENDIAN
                    101:                #define BYTE_ORDER BIG_ENDIAN
                    102:        #else /* WORDS_BIGENDIAN */
                    103:                #define BYTE_ORDER LITTLE_ENDIAN
                    104:        #endif /* WORDS_BIGENDIAN */
                    105: #endif /* BYTE_ORDER */
                    106: 
1.1       misho     107: #ifndef be16toh
                    108: #define be16toh                betoh16
                    109: #endif
                    110: #ifndef be32toh
                    111: #define be32toh                betoh32
                    112: #endif
                    113: #ifndef be64toh
                    114: #define be64toh                betoh64
                    115: #endif
                    116: #ifndef le16toh
                    117: #define le16toh                letoh16
                    118: #endif
                    119: #ifndef le32toh
                    120: #define le32toh                letoh32
                    121: #endif
                    122: #ifndef le64toh
                    123: #define le64toh                letoh64
                    124: #endif
                    125: 
1.6       misho     126: /* Bit map related macros. */
                    127: #ifndef setbit
                    128: #define        setbit(a, i)            (((unsigned char *)(a))[(i) / NBBY] |= 1 << ((i) % NBBY))
                    129: #endif
                    130: #ifndef clrbit
                    131: #define        clrbit(a, i)            (((unsigned char *)(a))[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
                    132: #endif
                    133: #ifndef isset
                    134: #define        isset(a, i)             (((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY)))
                    135: #endif
                    136: #ifndef isclr
                    137: #define        isclr(a, i)             ((((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
                    138: #endif
                    139: 
                    140: /* Macros for counting and rounding. */
                    141: #ifndef howmany
                    142: #define        howmany(x, y)           (((x) + ((y) - 1)) / (y))
                    143: #endif
                    144: #ifndef nitems
                    145: #define        nitems(x)               (sizeof((x)) / sizeof((x)[0]))
                    146: #endif
                    147: #ifndef rounddown
                    148: #define        rounddown(x, y)         (((x) / (y)) * (y))
                    149: #endif
                    150: #ifndef rounddown2
                    151: #define        rounddown2(x, y)        ((x) & (~((y) - 1))) /* if y is power of two */
                    152: #endif
                    153: #ifndef roundup
                    154: #define        roundup(x, y)           ((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */
                    155: #endif
                    156: #ifndef roundup2
                    157: #define        roundup2(x, y)          (((x) + ((y) - 1)) & (~((y) - 1))) /* if y is powers of two */
                    158: #endif
                    159: #ifndef powerof2
                    160: #define powerof2(x)            ((((x) - 1) & (x)) == 0)
                    161: #endif
                    162: 
                    163: /* Macros for min/max. */
                    164: #ifndef MIN
                    165: #define        MIN(a, b)               (((a) < (b)) ? (a) : (b))
                    166: #endif
                    167: #ifndef MAX
                    168: #define        MAX(a, b)               (((a) > (b)) ? (a) : (b))
                    169: #endif
                    170: 
                    171: #define E_ALIGN(x, a)          (((x) + ((a) - 1)) & ~((a) - 1))
1.1       misho     172: 
                    173: 
1.6       misho     174: #define ELWIX_SYSM             0
                    175: #define ELWIX_MPOOL            1
1.1       misho     176: 
1.6       misho     177: #define VACUUM_LEFT            1
                    178: #define VACUUM_BETWEEN         2
1.1       misho     179: 
                    180: 
1.9       misho     181: extern int __isthreaded;
                    182: 
                    183: 
1.1       misho     184: // elwix_SetProg() Set program memory pool name
1.3       misho     185: void elwix_SetProg(const char *csProgName);
1.1       misho     186: // elwix_GetProg() Get program memory pool name
1.3       misho     187: const char *elwix_GetProg();
1.1       misho     188: 
                    189: // elwix_GetErrno() Get error code of last operation
1.3       misho     190: int elwix_GetErrno();
1.1       misho     191: // elwix_GetError() Get error text of last operation
1.3       misho     192: const char *elwix_GetError();
1.1       misho     193: 
                    194: 
                    195: // elwix_mm_inuse() Check for memory management model
1.3       misho     196: int elwix_mm_inuse();
1.1       misho     197: 
                    198: 
                    199: /*
                    200:  * elwixInit() - Init libelwix library memory management
                    201:  *
                    202:  * @mm = memory management (IO_SYSM or IO_MPOOL)
                    203:  * @maxmem = memory limit
                    204:  * return: -1 error or !=-1 used memory management model
                    205:  */
1.3       misho     206: int elwixInit(int mm, unsigned long maxmem);
1.1       misho     207: /*
                    208:  * elwixFini() - Finish libelwix library memory management
                    209:  *
                    210:  * return: none
                    211:  */
1.3       misho     212: void elwixFini();
1.1       misho     213: 
1.11      misho     214: /*
                    215:  * elwix_byteOrder() - Detect platform byte order
                    216:  *
                    217:  * return: 1 = little endian or 0 big endian
                    218:  */
                    219: int elwix_byteOrder();
1.14      misho     220: 
                    221: #ifndef strlcpy
                    222: /*
                    223:  * Copy src to string dst of size siz.  At most siz-1 characters
                    224:  * will be copied.  Always NUL terminates (unless siz == 0).
                    225:  * Returns strlen(src); if retval >= siz, truncation occurred.
                    226:  */
                    227: size_t strlcpy(char *dst, const char *src, size_t siz);
                    228: #endif
                    229: #ifndef strlcat
                    230: /*
                    231:  * Appends src to string dst of size siz (unlike strncat, siz is the
                    232:  * full size of dst, not space left).  At most siz-1 characters
                    233:  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
                    234:  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
                    235:  * If retval >= siz, truncation occurred.
                    236:  */
                    237: size_t strlcat(char *dst, const char *src, size_t siz);
                    238: #endif
1.11      misho     239: 
1.1       misho     240: /* memory management hooks */
                    241: extern void *(*e_malloc)(size_t);
                    242: extern void *(*e_calloc)(size_t, size_t);
                    243: extern void *(*e_realloc)(void*, size_t);
                    244: extern char *(*e_strdup)(const char*);
                    245: extern void (*e_free)(void*);
                    246: 
                    247: 
                    248: /* Debug helper macros */
                    249: 
                    250: /* Verbose macros */
                    251: extern int elwix_Verbose;
                    252: #define e_initVerbose(x)       (elwix_Verbose = (x))
                    253: #define e_incVerbose           (elwix_Verbose++)
                    254: #define e_decVerbose           (elwix_Verbose--)
                    255: 
1.2       misho     256: #define EVERBS(x)              if ((x) <= elwix_Verbose)
                    257: #define EVERBOSE(x, fmt, ...)  do { assert((fmt)); \
                    258:                                        if ((x) <= elwix_Verbose) { \
1.8       misho     259:                                                char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
1.18      misho     260:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
                    261:                                                syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
                    262:                                                                (x), __func__, __LINE__, __str); \
                    263:                                        } \
                    264:                                } while (0)
                    265: #define EVERBOSE2(x, fmt, ...) do { assert((fmt)); \
                    266:                                        if ((x) <= elwix_Verbose) { \
                    267:                                                char __str[0x10000] = { [0 ... 0xffff] = 0 }; \
1.8       misho     268:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.16      misho     269:                                                syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
1.8       misho     270:                                                                (x), __func__, __LINE__, __str); \
1.2       misho     271:                                        } \
                    272:                                } while (0)
1.1       misho     273: 
                    274: /* Debug macros */
                    275: extern int elwix_Debug;
1.16      misho     276: #define e_Debug                        elwix_Debug
                    277: 
1.1       misho     278: #define ELWIX_DEBUG_OFF                0x0
                    279: #define ELWIX_DEBUG_TRACE      0x1
                    280: #define ELWIX_DEBUG_LOG                0x2
                    281: #define ELWIX_DEBUG_ANY                0xFFFFFFFF
                    282: 
1.5       misho     283: #define ETRACE()               if (elwix_Debug & ELWIX_DEBUG_TRACE) \
1.4       misho     284:                                           syslog(LOG_DEBUG, "I'm in %s(%d)\n", __func__, __LINE__)
1.1       misho     285: #define EDEBUG(x, fmt, ...)    do { assert((fmt)); \
                    286:                                        if ((x) & elwix_Debug) { \
1.8       misho     287:                                                char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
                    288:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     289:                                                syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \
1.8       misho     290:                                                                (x), __func__, __LINE__, __str); \
1.1       misho     291:                                        } \
                    292:                                } while (0)
                    293: 
                    294: /* Logger macro */
                    295: #define ELOGGER(x, fmt, ...)   do { assert((fmt)); \
1.8       misho     296:                                        char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
                    297:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     298:                                        syslog((x), "Logger:%s(%d): %s\n", \
1.8       misho     299:                                                                __func__, __LINE__, __str); \
1.1       misho     300:                                } while (0)
                    301: 
1.12      misho     302: #define EWARNING(x, fmt, ...)  do { assert((fmt)); \
                    303:                                        char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
                    304:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
                    305:                                        syslog(LOG_WARNING, "Warning:%s(%d): #%d - %s\n", \
                    306:                                                         __func__, __LINE__, (x), __str); \
                    307:                                } while (0)
1.1       misho     308: /* Error state macros */
                    309: #define EERROR(x, fmt, ...)    do { assert((fmt)); \
1.8       misho     310:                                        char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
                    311:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     312:                                        syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \
1.8       misho     313:                                                         __func__, __LINE__, (x), __str); \
1.1       misho     314:                                } while (0)
                    315: #define ESYSERR(x)             do { \
1.17      misho     316:                                        if (x > 0 || errno) { \
                    317:                                                int _ern = errno; \
1.1       misho     318:                                                syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \
                    319:                                                                __func__, __LINE__, x > 0 ? x : errno, \
                    320:                                                                strerror(x > 0 ? x : errno)); \
1.17      misho     321:                                                errno = _ern; \
                    322:                                        } \
1.1       misho     323:                                } while (0)
                    324: #define ELIBERR(ait)           do { \
                    325:                                        if (ait##_GetErrno()) \
                    326:                                                syslog(LOG_ERR, "Error(lib):%s(%d): #%d - %s\n", \
                    327:                                                                __func__, __LINE__, ait##_GetErrno(), \
                    328:                                                                ait##_GetError()); \
                    329:                                } while (0)
                    330: 
                    331: 
                    332: #endif

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