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

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