File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix.h
Revision 1.14: download - view: text, annotated - select for diffs - revision graph
Wed May 18 12:47:42 2016 UTC (8 years, 1 month ago) by misho
Branches: MAIN
CVS tags: elwix4_4, elwix4_3, elwix4_2, elwix4_1, HEAD, ELWIX4_3, ELWIX4_2, ELWIX4_1, ELWIX4_0
version 4.0

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

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