Annotation of libelwix/inc/elwix.h, revision 1.7.2.2

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.7.2.2 ! misho       6: * $Id: elwix.h,v 1.7.2.1 2013/07/07 21:05:58 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: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
                     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>
                     52: #include <sys/types.h>
1.6       misho      53: #include <sys/param.h>
                     54: #include <sys/limits.h>
1.1       misho      55: #include <sys/socket.h>
1.7.2.1   misho      56: #include <sys/endian.h>
1.1       misho      57: #include <sys/un.h>
                     58: #include <net/if_dl.h>
                     59: #include <netinet/in.h>
                     60: #include <arpa/inet.h>
                     61: 
                     62: #include <elwix/atree.h>
                     63: #include <elwix/ampool.h>
                     64: #include <elwix/acrc.h>
                     65: #include <elwix/aarray.h>
                     66: #include <elwix/asarr.h>
                     67: #include <elwix/avar.h>
                     68: #include <elwix/astr.h>
                     69: #include <elwix/aregex.h>
1.2       misho      70: #include <elwix/aav.h>
1.1       misho      71: #include <elwix/anet.h>
1.2       misho      72: #include <elwix/atime.h>
1.7       misho      73: #include <elwix/apack.h>
1.1       misho      74: 
                     75: 
                     76: #ifndef STRSIZ
                     77: #define STRSIZ         256
                     78: #endif
                     79: 
1.6       misho      80: #ifndef NBBY
                     81: #define        NBBY            8               /* number of bits in a byte */
                     82: #endif
                     83: 
1.1       misho      84: #ifndef be16toh
                     85: #define be16toh                betoh16
                     86: #endif
                     87: #ifndef be32toh
                     88: #define be32toh                betoh32
                     89: #endif
                     90: #ifndef be64toh
                     91: #define be64toh                betoh64
                     92: #endif
                     93: #ifndef le16toh
                     94: #define le16toh                letoh16
                     95: #endif
                     96: #ifndef le32toh
                     97: #define le32toh                letoh32
                     98: #endif
                     99: #ifndef le64toh
                    100: #define le64toh                letoh64
                    101: #endif
                    102: 
1.6       misho     103: /* Bit map related macros. */
                    104: #ifndef setbit
                    105: #define        setbit(a, i)            (((unsigned char *)(a))[(i) / NBBY] |= 1 << ((i) % NBBY))
                    106: #endif
                    107: #ifndef clrbit
                    108: #define        clrbit(a, i)            (((unsigned char *)(a))[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
                    109: #endif
                    110: #ifndef isset
                    111: #define        isset(a, i)             (((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY)))
                    112: #endif
                    113: #ifndef isclr
                    114: #define        isclr(a, i)             ((((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
                    115: #endif
                    116: 
                    117: /* Macros for counting and rounding. */
                    118: #ifndef howmany
                    119: #define        howmany(x, y)           (((x) + ((y) - 1)) / (y))
                    120: #endif
                    121: #ifndef nitems
                    122: #define        nitems(x)               (sizeof((x)) / sizeof((x)[0]))
                    123: #endif
                    124: #ifndef rounddown
                    125: #define        rounddown(x, y)         (((x) / (y)) * (y))
                    126: #endif
                    127: #ifndef rounddown2
                    128: #define        rounddown2(x, y)        ((x) & (~((y) - 1))) /* if y is power of two */
                    129: #endif
                    130: #ifndef roundup
                    131: #define        roundup(x, y)           ((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */
                    132: #endif
                    133: #ifndef roundup2
                    134: #define        roundup2(x, y)          (((x) + ((y) - 1)) & (~((y) - 1))) /* if y is powers of two */
                    135: #endif
                    136: #ifndef powerof2
                    137: #define powerof2(x)            ((((x) - 1) & (x)) == 0)
                    138: #endif
                    139: 
                    140: /* Macros for min/max. */
                    141: #ifndef MIN
                    142: #define        MIN(a, b)               (((a) < (b)) ? (a) : (b))
                    143: #endif
                    144: #ifndef MAX
                    145: #define        MAX(a, b)               (((a) > (b)) ? (a) : (b))
                    146: #endif
                    147: 
                    148: #define E_ALIGN(x, a)          (((x) + ((a) - 1)) & ~((a) - 1))
1.1       misho     149: 
                    150: 
1.6       misho     151: #define ELWIX_SYSM             0
                    152: #define ELWIX_MPOOL            1
1.1       misho     153: 
1.6       misho     154: #define VACUUM_LEFT            1
                    155: #define VACUUM_BETWEEN         2
1.1       misho     156: 
                    157: 
                    158: // elwix_SetProg() Set program memory pool name
1.3       misho     159: void elwix_SetProg(const char *csProgName);
1.1       misho     160: // elwix_GetProg() Get program memory pool name
1.3       misho     161: const char *elwix_GetProg();
1.1       misho     162: 
                    163: // elwix_GetErrno() Get error code of last operation
1.3       misho     164: int elwix_GetErrno();
1.1       misho     165: // elwix_GetError() Get error text of last operation
1.3       misho     166: const char *elwix_GetError();
1.1       misho     167: 
                    168: 
                    169: // elwix_mm_inuse() Check for memory management model
1.3       misho     170: int elwix_mm_inuse();
1.1       misho     171: 
                    172: 
                    173: /*
                    174:  * elwixInit() - Init libelwix library memory management
                    175:  *
                    176:  * @mm = memory management (IO_SYSM or IO_MPOOL)
                    177:  * @maxmem = memory limit
                    178:  * return: -1 error or !=-1 used memory management model
                    179:  */
1.3       misho     180: int elwixInit(int mm, unsigned long maxmem);
1.1       misho     181: /*
                    182:  * elwixFini() - Finish libelwix library memory management
                    183:  *
                    184:  * return: none
                    185:  */
1.3       misho     186: void elwixFini();
1.1       misho     187: 
                    188: /* memory management hooks */
                    189: extern void *(*e_malloc)(size_t);
                    190: extern void *(*e_calloc)(size_t, size_t);
                    191: extern void *(*e_realloc)(void*, size_t);
                    192: extern char *(*e_strdup)(const char*);
                    193: extern void (*e_free)(void*);
                    194: 
                    195: 
                    196: /* Debug helper macros */
                    197: 
                    198: /* Verbose macros */
                    199: extern int elwix_Verbose;
                    200: #define e_initVerbose(x)       (elwix_Verbose = (x))
                    201: #define e_incVerbose           (elwix_Verbose++)
                    202: #define e_decVerbose           (elwix_Verbose--)
                    203: 
1.2       misho     204: #define EVERBS(x)              if ((x) <= elwix_Verbose)
                    205: #define EVERBOSE(x, fmt, ...)  do { assert((fmt)); \
                    206:                                        if ((x) <= elwix_Verbose) { \
1.7.2.2 ! misho     207:                                                char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
        !           208:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.2       misho     209:                                                syslog(LOG_DEBUG, "Verbose(%d):%s(%d): %s\n", \
1.7.2.2 ! misho     210:                                                                (x), __func__, __LINE__, __str); \
1.2       misho     211:                                        } \
                    212:                                } while (0)
1.1       misho     213: 
                    214: /* Debug macros */
                    215: extern int elwix_Debug;
                    216: #define ELWIX_DEBUG_OFF                0x0
                    217: #define ELWIX_DEBUG_TRACE      0x1
                    218: #define ELWIX_DEBUG_LOG                0x2
                    219: #define ELWIX_DEBUG_ANY                0xFFFFFFFF
                    220: 
1.5       misho     221: #define ETRACE()               if (elwix_Debug & ELWIX_DEBUG_TRACE) \
1.4       misho     222:                                           syslog(LOG_DEBUG, "I'm in %s(%d)\n", __func__, __LINE__)
1.1       misho     223: #define EDEBUG(x, fmt, ...)    do { assert((fmt)); \
                    224:                                        if ((x) & elwix_Debug) { \
1.7.2.2 ! misho     225:                                                char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
        !           226:                                                snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     227:                                                syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \
1.7.2.2 ! misho     228:                                                                (x), __func__, __LINE__, __str); \
1.1       misho     229:                                        } \
                    230:                                } while (0)
                    231: 
                    232: /* Logger macro */
                    233: #define ELOGGER(x, fmt, ...)   do { assert((fmt)); \
1.7.2.2 ! misho     234:                                        char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
        !           235:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     236:                                        syslog((x), "Logger:%s(%d): %s\n", \
1.7.2.2 ! misho     237:                                                                __func__, __LINE__, __str); \
1.1       misho     238:                                } while (0)
                    239: 
                    240: /* Error state macros */
                    241: #define EERROR(x, fmt, ...)    do { assert((fmt)); \
1.7.2.2 ! misho     242:                                        char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
        !           243:                                        snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
1.1       misho     244:                                        syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \
1.7.2.2 ! misho     245:                                                         __func__, __LINE__, (x), __str); \
1.1       misho     246:                                } while (0)
                    247: #define ESYSERR(x)             do { \
                    248:                                        if (x > 0 || errno) \
                    249:                                                syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \
                    250:                                                                __func__, __LINE__, x > 0 ? x : errno, \
                    251:                                                                strerror(x > 0 ? x : errno)); \
                    252:                                } while (0)
                    253: #define ELIBERR(ait)           do { \
                    254:                                        if (ait##_GetErrno()) \
                    255:                                                syslog(LOG_ERR, "Error(lib):%s(%d): #%d - %s\n", \
                    256:                                                                __func__, __LINE__, ait##_GetErrno(), \
                    257:                                                                ait##_GetError()); \
                    258:                                } while (0)
                    259: 
                    260: 
                    261: #endif

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