File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix.h
Revision 1.29: download - view: text, annotated - select for diffs - revision graph
Mon Nov 17 20:02:47 2025 UTC (4 weeks, 1 day ago) by misho
Branches: MAIN
CVS tags: elwix6_13, HEAD, ELWIX6_12
Version 6.12

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

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