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