File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix.h
Revision 1.30: download - view: text, annotated - select for diffs - revision graph
Tue Feb 10 17:50:00 2026 UTC (9 days, 22 hours ago) by misho
Branches: MAIN
CVS tags: elwix6_18, elwix6_17, elwix6_16, elwix6_15, elwix6_14, HEAD, ELWIX6_17, ELWIX6_16, ELWIX6_15, ELWIX6_14, ELWIX6_13
Version 6.13

Changelog:
 - Implements linear ring buffer

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

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