File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / elwix.c
Revision 1.11: download - view: text, annotated - select for diffs - revision graph
Mon Jan 22 15:24:28 2024 UTC (3 months, 3 weeks ago) by misho
Branches: MAIN
CVS tags: elwix5_9, elwix5_12, elwix5_11, elwix5_10, HEAD, ELWIX5_9, ELWIX5_8, ELWIX5_11, ELWIX5_10
Version 5.8

    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.c,v 1.11 2024/01/22 15:24:28 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 - 2023
   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: #include "global.h"
   47: 
   48: 
   49: int elwix_Debug;
   50: int elwix_Verbose;
   51: 
   52: 
   53: /* Memory management */
   54: 
   55: void *(*e_malloc)(size_t) = malloc;
   56: void *(*e_calloc)(size_t, size_t) = calloc;
   57: void *(*e_realloc)(void*, size_t) = realloc;
   58: char *(*e_strdup)(const char*) = strdup;
   59: void (*e_free)(void*) = free;
   60: 
   61: 
   62: #pragma GCC visibility push(hidden)
   63: 
   64: int use_mm;
   65: const char elwix_Prog[STRSIZ];
   66: 
   67: int elwix_Errno;
   68: char elwix_Error[STRSIZ];
   69: 
   70: #pragma GCC visibility pop
   71: 
   72: 
   73: // elwix_SetProg() Set program memory pool name
   74: void
   75: elwix_SetProg(const char *csProgName)
   76: {
   77: 	strlcpy((char*) elwix_Prog, csProgName, sizeof elwix_Prog);
   78: }
   79: 
   80: // elwix_GetProg() Get program memory pool name
   81: const char *
   82: elwix_GetProg()
   83: {
   84: 	return elwix_Prog;
   85: }
   86: 
   87: // elwix_GetErrno() Get error code of last operation
   88: int
   89: elwix_GetErrno()
   90: {
   91: 	return elwix_Errno;
   92: }
   93: 
   94: // elwix_GetError() Get error text of last operation
   95: const char *
   96: elwix_GetError()
   97: {
   98: 	return elwix_Error;
   99: }
  100: 
  101: // elwix_SetErr() Set error to variables for internal use!!!
  102: void
  103: elwix_SetErr(int eno, char *estr, ...)
  104: {
  105: 	va_list lst;
  106: 
  107: 	elwix_Errno = eno;
  108: 	memset(elwix_Error, 0, sizeof elwix_Error);
  109: 	va_start(lst, estr);
  110: 	vsnprintf(elwix_Error, sizeof elwix_Error, estr, lst);
  111: 	va_end(lst);
  112: }
  113: 
  114: // elwix_mm_inuse() Check for memory management model
  115: int
  116: elwix_mm_inuse()
  117: {
  118: 	return use_mm & ELWIX_MPOOL;
  119: }
  120: 
  121: 
  122: #pragma GCC visibility push(hidden)
  123: // init libelwix routine
  124: __attribute__((constructor)) static void
  125: _elwix_init()
  126: {
  127: 	elwixInit(MEMMGR, 0);
  128: }
  129: 
  130: // fini libelwix routine
  131: __attribute__((destructor)) static void
  132: _elwix_fini()
  133: {
  134: 	elwixFini();
  135: }
  136: 
  137: /*
  138:  * elwixInit() - Init libelwix library memory management
  139:  *
  140:  * @mm = memory management (ELWIX_SYSM or ELWIX_MPOOL)
  141:  * @maxmem = memory limit
  142:  * return: -1 error or !=-1 used memory management model
  143:  */
  144: int
  145: elwixInit(int mm, u_long maxmem)
  146: {
  147: 	switch (mm) {
  148: 		case ELWIX_MPOOL:	/* mpool */
  149: 			elwix_mpool = mpool_init(maxmem);
  150: 			if (elwix_mpool) {
  151: 				e_malloc = mpool_xmalloc;
  152: 				e_calloc = mpool_xcalloc;
  153: 				e_realloc = mpool_xrealloc;
  154: 				e_strdup = mpool_xstrdup;
  155: 				e_free = mpool_xfree;
  156: 				break;
  157: 			} else {
  158: 				mm = ELWIX_SYSM;
  159: 				#undef USE_MPOOL
  160: 			}
  161: 		case ELWIX_SYSM:	/* system */
  162: 			e_malloc = malloc;
  163: 			e_calloc = calloc;
  164: 			e_realloc = realloc;
  165: 			e_strdup = strdup;
  166: 			e_free = free;
  167: 			break;
  168: 		default:		/* not supported */
  169: 			elwix_SetErr(EINVAL, "Not supported memory management");
  170: 			return -1;
  171: 	}
  172: 
  173: 	return (use_mm = mm);
  174: }
  175: 
  176: /*
  177:  * elwixFini() - Finish libelwix library memory management
  178:  *
  179:  * return: none
  180:  */
  181: void
  182: elwixFini()
  183: {
  184: 	switch (use_mm) {
  185: 		case ELWIX_MPOOL:
  186: 			e_malloc = malloc;
  187: 			e_calloc = calloc;
  188: 			e_realloc = realloc;
  189: 			e_strdup = strdup;
  190: 			e_free = free;
  191: 			use_mm = ELWIX_SYSM;
  192: 
  193: 			mpool_destroy(&elwix_mpool);
  194: 			break;
  195: 	}
  196: }
  197: #pragma GCC visibility pop
  198: 
  199: /*
  200:  * elwix_byteOrder() - Detect platform byte order
  201:  *
  202:  * return: 1 = little endian or 0 big endian
  203:  */
  204: int
  205: elwix_byteOrder()
  206: {
  207: 	int x = 1;
  208: 
  209: 	return *(char*) &x;
  210: }
  211: 
  212: #ifndef HAVE_STRLCPY
  213: /*
  214:  * Copy src to string dst of size siz.  At most siz-1 characters
  215:  * will be copied.  Always NUL terminates (unless siz == 0).
  216:  * Returns strlen(src); if retval >= siz, truncation occurred.
  217:  */
  218: size_t
  219: strlcpy(char *dst, const char *src, size_t siz)
  220: {
  221: 	char *d = dst;
  222: 	const char *s = src;
  223: 	register size_t n = siz;
  224: 
  225: 	/* Copy as many bytes as will fit */
  226: 	if (n)
  227: 		while (--n)
  228: 			if ((*d++ = *s++) == '\0')
  229: 				break;
  230: 
  231: 	/* Not enough room in dst, add NUL and traverse rest of src */
  232: 	if (!n) {
  233: 		if (siz)
  234: 			*d = '\0';	/* NUL-terminate dst */
  235: 		while (*s++);
  236: 	}
  237: 
  238: 	return (s - src - 1);	/* count does not include NUL */
  239: }
  240: #endif
  241: 
  242: #ifndef HAVE_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
  251: strlcat(char *dst, const char *src, size_t siz)
  252: {
  253: 	char *d = dst;
  254: 	const char *s = src;
  255: 	register size_t n = siz;
  256: 	size_t dlen;
  257: 
  258: 	/* Find the end of dst and adjust bytes left but don't go past end */
  259: 	while (n-- && *d != '\0')
  260: 		d++;
  261: 
  262: 	dlen = d - dst;
  263: 	n = siz - dlen;
  264: 	if (!n)
  265: 		return (dlen + strlen(s));
  266: 
  267: 	while (*s != '\0') {
  268: 		if (n != 1) {
  269: 			*d++ = *s;
  270: 			n--;
  271: 		}
  272: 		s++;
  273: 	}
  274: 	*d = '\0';
  275: 
  276: 	return (dlen + (s - src));	/* count does not include NUL */
  277: }
  278: #endif

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