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

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

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