Annotation of libelwix/src/elwix.c, revision 1.2.28.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.2.28.1! misho       6: * $Id: elwix.c,v 1.2 2013/05/30 09:07:33 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.2.28.1! misho      15: Copyright 2004 - 2014
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: #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
1.2       misho      74: void
1.1       misho      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
1.2       misho      81: const char *
1.1       misho      82: elwix_GetProg()
                     83: {
                     84:        return elwix_Prog;
                     85: }
                     86: 
                     87: // elwix_GetErrno() Get error code of last operation
1.2       misho      88: int
1.1       misho      89: elwix_GetErrno()
                     90: {
                     91:        return elwix_Errno;
                     92: }
                     93: 
                     94: // elwix_GetError() Get error text of last operation
1.2       misho      95: const char *
1.1       misho      96: elwix_GetError()
                     97: {
                     98:        return elwix_Error;
                     99: }
                    100: 
                    101: // elwix_SetErr() Set error to variables for internal use!!!
1.2       misho     102: void
1.1       misho     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
1.2       misho     115: int
1.1       misho     116: elwix_mm_inuse()
                    117: {
                    118:        return use_mm & ELWIX_MPOOL;
                    119: }
                    120: 
                    121: 
                    122: // init libelwix routine
                    123: __attribute__((constructor)) void
                    124: _elwix_init()
                    125: {
                    126:        elwixInit(ELWIX_MPOOL, 0);
                    127: }
                    128: 
                    129: // fini libelwix routine
                    130: __attribute__((destructor)) void
                    131: _elwix_fini()
                    132: {
                    133:        elwixFini();
                    134: }
                    135: 
                    136: /*
                    137:  * elwixInit() - Init libelwix library memory management
                    138:  *
                    139:  * @mm = memory management (ELWIX_SYSM or ELWIX_MPOOL)
                    140:  * @maxmem = memory limit
                    141:  * return: -1 error or !=-1 used memory management model
                    142:  */
                    143: int
                    144: elwixInit(int mm, u_long maxmem)
                    145: {
                    146:        switch (mm) {
                    147:                case ELWIX_MPOOL:       /* mpool */
                    148:                        elwix_mpool = mpool_init(maxmem);
                    149:                        if (elwix_mpool) {
                    150:                                e_malloc = mpool_xmalloc;
                    151:                                e_calloc = mpool_xcalloc;
                    152:                                e_realloc = mpool_xrealloc;
                    153:                                e_strdup = mpool_xstrdup;
                    154:                                e_free = mpool_xfree;
                    155:                                break;
                    156:                        } else {
                    157:                                mm = ELWIX_SYSM;
                    158:                                #undef USE_MPOOL
                    159:                        }
                    160:                case ELWIX_SYSM:        /* system */
                    161:                        e_malloc = malloc;
                    162:                        e_calloc = calloc;
                    163:                        e_realloc = realloc;
                    164:                        e_strdup = strdup;
                    165:                        e_free = free;
                    166:                        break;
                    167:                default:                /* not supported */
                    168:                        elwix_SetErr(EINVAL, "Not supported memory management");
                    169:                        return -1;
                    170:        }
                    171: 
                    172:        return (use_mm = mm);
                    173: }
                    174: 
                    175: /*
                    176:  * elwixFini() - Finish libaitio library memory management
                    177:  *
                    178:  * return: none
                    179:  */
                    180: void
                    181: elwixFini()
                    182: {
                    183:        switch (use_mm) {
                    184:                case ELWIX_MPOOL:
                    185:                        e_malloc = malloc;
                    186:                        e_calloc = calloc;
                    187:                        e_realloc = realloc;
                    188:                        e_strdup = strdup;
                    189:                        e_free = free;
                    190:                        use_mm = ELWIX_SYSM;
                    191: 
                    192:                        mpool_destroy(&elwix_mpool);
                    193:                        break;
                    194:        }
                    195: }

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