File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix.h
Revision 1.24: download - view: text, annotated - select for diffs - revision graph
Wed Aug 14 16:05:42 2024 UTC (2 months, 2 weeks ago) by misho
Branches: MAIN
CVS tags: elwix6_1, HEAD, ELWIX6_0
Version 6.0

Changelog:
 - New calloc() wrapper
 - New option --enable-mpool-mem-zero

/*************************************************************************
* (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
*  by Michael Pounov <misho@elwix.org>
*
* $Author: misho $
* $Id: elwix.h,v 1.24 2024/08/14 16:05:42 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2024
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef __ELWIX_H
#define __ELWIX_H


#include <assert.h>
#include <syslog.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/un.h>
#ifndef __linux__
#include <sys/limits.h>
#include <sys/endian.h>
#include <net/if_dl.h>
#else
#include <linux/if_packet.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>

#include <elwix/aqueue.h>
#include <elwix/atree.h>
#include <elwix/ampool.h>
#include <elwix/acrc.h>
#include <elwix/aarray.h>
#include <elwix/asarr.h>
#include <elwix/avar.h>
#include <elwix/astr.h>
#include <elwix/aregex.h>
#include <elwix/aav.h>
#include <elwix/anet.h>
#include <elwix/atime.h>
#include <elwix/apack.h>
#include <elwix/apio.h>
#include <elwix/ajson.h>
#include <elwix/aiov.h>
#include <elwix/aindex.h>


#ifndef STRSIZ
#define STRSIZ		256
#endif

#ifndef NBBY
#define	NBBY		8		/* number of bits in a byte */
#endif

#ifndef BYTE_ORDER
	#ifndef LITTLE_ENDIAN
		#define LITTLE_ENDIAN	1234
	#endif /* LITTLE_ENDIAN */
	#ifndef BIG_ENDIAN
		#define BIG_ENDIAN	4321
	#endif /* BIG_ENDIAN */
	#ifdef WORDS_BIGENDIAN
		#define BYTE_ORDER BIG_ENDIAN
	#else /* WORDS_BIGENDIAN */
		#define BYTE_ORDER LITTLE_ENDIAN
	#endif /* WORDS_BIGENDIAN */
#endif /* BYTE_ORDER */

#ifndef be16toh
#define be16toh		betoh16
#endif
#ifndef be32toh
#define be32toh		betoh32
#endif
#ifndef be64toh
#define be64toh		betoh64
#endif
#ifndef le16toh
#define le16toh		letoh16
#endif
#ifndef le32toh
#define le32toh		letoh32
#endif
#ifndef le64toh
#define le64toh		letoh64
#endif

/* Bit map related macros. */
#ifndef setbit
#define	setbit(a, i)		(((unsigned char *)(a))[(i) / NBBY] |= 1 << ((i) % NBBY))
#endif
#ifndef clrbit
#define	clrbit(a, i)		(((unsigned char *)(a))[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
#endif
#ifndef isset
#define	isset(a, i)		(((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY)))
#endif
#ifndef isclr
#define	isclr(a, i)		((((const unsigned char *)(a))[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
#endif

/* Macros for counting and rounding. */
#ifndef howmany
#define	howmany(x, y)		(((x) + ((y) - 1)) / (y))
#endif
#ifndef nitems
#define	nitems(x)		(sizeof((x)) / sizeof((x)[0]))
#endif
#ifndef rounddown
#define	rounddown(x, y)		(((x) / (y)) * (y))
#endif
#ifndef rounddown2
#define	rounddown2(x, y)	((x) & (~((y) - 1))) /* if y is power of two */
#endif
#ifndef roundup
#define	roundup(x, y)		((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */
#endif
#ifndef roundup2
#define	roundup2(x, y)		(((x) + ((y) - 1)) & (~((y) - 1))) /* if y is powers of two */
#endif
#ifndef powerof2
#define powerof2(x)		((((x) - 1) & (x)) == 0)
#endif

/* Macros for min/max. */
#ifndef MIN
#define	MIN(a, b)		(((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define	MAX(a, b)		(((a) > (b)) ? (a) : (b))
#endif

#define E_ALIGN(x, a)		(((x) + ((a) - 1)) & ~((a) - 1))


#define ELWIX_SYSM		0
#define ELWIX_MPOOL		1

#define VACUUM_LEFT		1
#define VACUUM_BETWEEN		2


extern int __isthreaded;


// elwix_SetProg() Set program memory pool name
void elwix_SetProg(const char *csProgName);
// elwix_GetProg() Get program memory pool name
const char *elwix_GetProg();

// elwix_GetErrno() Get error code of last operation
int elwix_GetErrno();
// elwix_GetError() Get error text of last operation
const char *elwix_GetError();


// elwix_mm_inuse() Check for memory management model
int elwix_mm_inuse();


/*
 * elwixInit() - Init libelwix library memory management
 *
 * @mm = memory management (IO_SYSM or IO_MPOOL)
 * @maxmem = memory limit
 * return: -1 error or !=-1 used memory management model
 */
int elwixInit(int mm, unsigned long maxmem);
/*
 * elwixFini() - Finish libelwix library memory management
 *
 * return: none
 */
void elwixFini();

/*
 * elwix_byteOrder() - Detect platform byte order
 *
 * return: 1 = little endian or 0 big endian
 */
int elwix_byteOrder();

#ifndef strlcpy
/*
 * Copy src to string dst of size siz.  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz == 0).
 * Returns strlen(src); if retval >= siz, truncation occurred.
 */
size_t strlcpy(char *dst, const char *src, size_t siz);
#endif
#ifndef strlcat
/*
 * Appends src to string dst of size siz (unlike strncat, siz is the
 * full size of dst, not space left).  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
 * If retval >= siz, truncation occurred.
 */
size_t strlcat(char *dst, const char *src, size_t siz);
#endif

/* memory management hooks */
extern void *(*e_malloc)(size_t);
extern void *(*e_calloc)(size_t, size_t);
extern void *(*e_realloc)(void*, size_t);
extern char *(*e_strdup)(const char*);
extern void (*e_free)(void*);


/* Debug helper macros */

/* Verbose macros */
extern int elwix_Verbose;
#define e_Verbose		elwix_Verbose
#define e_initVerbose(x)	(elwix_Verbose = (x))
#define e_incVerbose		(elwix_Verbose++)
#define e_decVerbose		(elwix_Verbose--)

#define EVERBS(x)		if ((x) <= elwix_Verbose)
#define EVERBOSE(x, fmt, ...)	do { assert((fmt)); \
					if ((x) <= elwix_Verbose) { \
						char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
						snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
						syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
								(x), __func__, __LINE__, __str); \
					} \
				} while (0)
#define EVERBOSE2(x, fmt, ...)	do { assert((fmt)); \
					if ((x) <= elwix_Verbose) { \
						char __str[0x10000] = { [0 ... 0xffff] = 0 }; \
						snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
						syslog(LOG_INFO, "Verbose(%d):%s(%d): %s\n", \
								(x), __func__, __LINE__, __str); \
					} \
				} while (0)

/* Debug macros */
extern int elwix_Debug;
#define e_Debug			elwix_Debug

#define ELWIX_DEBUG_OFF		0x0
#define ELWIX_DEBUG_TRACE	0x1
#define ELWIX_DEBUG_LOG		0x2
#define ELWIX_DEBUG_MEM		0x4
#define ELWIX_DEBUG_MEMORY	ELWIX_DEBUG_MEM
#define ELWIX_DEBUG_IO		0x8
#define ELWIX_DEBUG_IPC		0x10
#define ELWIX_DEBUG_LOCK	0x20
#define ELWIX_DEBUG_SYS		0x40
#define ELWIX_DEBUG_NET		0x80
#define ELWIX_DEBUG_ANY		0xFFFFFFFF

#define EDBG(x)			(elwix_Debug & (x))
#define EDBGS(x)		if (EDBG(x))
#define ETRACE()		if (elwix_Debug & ELWIX_DEBUG_TRACE) \
					   syslog(LOG_DEBUG, "I'm in %s(%d)\n", __func__, __LINE__)
#define EDEBUG(x, fmt, ...)	do { assert((fmt)); \
					if ((x) & elwix_Debug) { \
						char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
						snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
						syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \
								(x), __func__, __LINE__, __str); \
					} \
				} while (0)

/* Logger macro */
#define ELOGGER(x, fmt, ...)	do { assert((fmt)); \
					char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
					snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
					syslog((x), "Logger:%s(%d): %s\n", \
								__func__, __LINE__, __str); \
				} while (0)

#define EWARNING(x, fmt, ...)	do { assert((fmt)); \
					char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
					snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
					syslog(LOG_WARNING, "Warning:%s(%d): #%d - %s\n", \
							 __func__, __LINE__, (x), __str); \
				} while (0)
/* Error state macros */
#define EERROR(x, fmt, ...)	do { assert((fmt)); \
					char __str[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; \
					snprintf(__str, sizeof __str, (fmt), ##__VA_ARGS__); \
					syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \
							 __func__, __LINE__, (x), __str); \
				} while (0)
#define ESYSERR(x)		do { \
					if (x > 0 || errno) { \
						int _ern = errno; \
						syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \
								__func__, __LINE__, x > 0 ? x : errno, \
								strerror(x > 0 ? x : errno)); \
						errno = _ern; \
					} \
				} while (0)
#define ELIBERR(ait)		do { \
					if (ait##_GetErrno()) \
						syslog(LOG_ERR, "Error(lib):%s(%d): #%d - %s\n", \
								__func__, __LINE__, ait##_GetErrno(), \
								ait##_GetError()); \
				} while (0)


#endif

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