Annotation of libelwix/inc/elwix/ampool.h, revision 1.3.20.2

1.1       misho       1: /*************************************************************************
                      2: * (C) 2012 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.3.20.2! misho       6: * $Id: ampool.h,v 1.3.20.1 2015/06/25 00:36:47 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.3.20.1  misho      15: Copyright 2004 - 2015
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 __AMPOOL_H
                     47: #define __AMPOOL_H
                     48: 
                     49: 
                     50: #include <pthread.h>
                     51: #include <sys/queue.h>
                     52: 
                     53: 
                     54: /* Memory pool */
                     55: 
                     56: #define MEM_BUCKETS    28      /* 32 bits - 4 bits = 28 items in bucket array */
                     57: 
                     58: struct tagAlloc {
                     59:        char                    alloc_name[64];
                     60: 
                     61:        unsigned int            *alloc_mem;
                     62: 
                     63:        TAILQ_ENTRY(tagAlloc)   alloc_node;
                     64: };
                     65: typedef TAILQ_HEAD(, tagAlloc) mpool_bucket_t;
                     66: 
                     67: typedef struct _tagMPool {
                     68:        pthread_mutex_t pool_mtx;
                     69: 
                     70:        struct {
                     71:                unsigned long alloc;
                     72:                unsigned long free;
                     73:                unsigned long cache;
                     74:        } pool_calls;
                     75:        struct {
                     76:                unsigned long alloc;
                     77:                unsigned long free;
                     78:                unsigned long cache;
                     79:        } pool_bytes;
                     80:        struct {
                     81:                unsigned long max;
                     82:                unsigned long curr;
                     83:        } pool_quota;
                     84: 
                     85:        /* pool buckets */
                     86:        mpool_bucket_t  pool_active[MEM_BUCKETS];
                     87:        mpool_bucket_t  pool_inactive[MEM_BUCKETS];
                     88: } mpool_t;
                     89: #define mpool_lock(x)  (assert((x)), pthread_mutex_lock(&(x)->pool_mtx))
                     90: #define mpool_unlock(x)        (assert((x)), pthread_mutex_unlock(&(x)->pool_mtx))
                     91: 
                     92: typedef void (*mpool_stat_cb)(unsigned int, unsigned int, unsigned int);
                     93: 
                     94: /* mpool functions */
                     95: 
                     96: /*
                     97:  * mpool_init() - Init memory pool
                     98:  *
                     99:  * @maxmem = If !=0 set maximum memory quota
                    100:  * return: =NULL error or !=NULL new allocated pool
                    101:  */
                    102: mpool_t *mpool_init(unsigned long maxmem);
                    103: /*
                    104:  * mpool_destroy() - Destroy memory pool
                    105:  *
                    106:  * @mp = Memory pool
                    107:  * return: none
                    108:  */
                    109: void mpool_destroy(mpool_t ** __restrict mp);
                    110: /*
                    111:  * mpool_purge() - Purge memory block cache and release resources
                    112:  *
                    113:  * @mp = Memory pool
                    114:  * @atmost = Free at most in buckets
                    115:  * return: -1 error or 0 ok
                    116:  */
                    117: int mpool_purge(mpool_t * __restrict mp, unsigned int atmost);
                    118: /*
                    119:  * mpool_malloc() - Memory allocation
                    120:  *
                    121:  * @mp = Memory pool
                    122:  * @size = Size
                    123:  * @memname = Optional memory block name
                    124:  * return: NULL error or !=NULL ok allocated memory
                    125:  */
                    126: void *mpool_malloc(mpool_t * __restrict mp, unsigned int size, const char *memname);
                    127: /*
                    128:  * mpool_free() Free allocated memory with mpool_alloc()
                    129:  *
                    130:  * @mp = Memory pool
                    131:  * @data = Allocated memory data
                    132:  * @purge = if !=0 force release memory block
                    133:  * return: <0 error or 0 ok released memory block
                    134:  */
                    135: int mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge);
                    136: /*
                    137:  * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
                    138:  *
                    139:  * @mp = Memory pool
                    140:  * @size = Allocated memory data size
                    141:  * @memname = Memory name
                    142:  * @purge = if !=0 force release memory block
                    143:  * return: <0 error or 0 ok released memory block
                    144:  */
                    145: int mpool_free2(mpool_t * __restrict mp, unsigned int size, const char *memname, int purge);
                    146: /*
                    147:  * mpool_realloc() Reallocate memory block with new size
                    148:  *
                    149:  * @mp = Memory pool
                    150:  * @data = Allocated memory data
                    151:  * @newsize = New size of memory block
                    152:  * @memname = Optional new memory block name
                    153:  * return: NULL error or !=NULL new reallocated memory block
                    154:  */
                    155: void *mpool_realloc(mpool_t * __restrict mp, void * __restrict data, 
                    156:                unsigned int newsize, const char *memname);
                    157: /*
                    158:  * mpool_strdup() - String duplicate
                    159:  *
                    160:  * @mp = Memory pool
                    161:  * @str = String
                    162:  * @memname = Memory name
                    163:  * return: NULL error or !=NULL new string
                    164:  */
                    165: char *mpool_strdup(mpool_t * __restrict mp, const char *str, const char *memname);
                    166: /*
                    167:  * mpool_getmembynam() Find allocated memory block by size and memory name
                    168:  *
                    169:  * @mp = Memory pool
                    170:  * @size = Memory size
                    171:  * @memname = Memory name
                    172:  * return: NULL error or not found and !=NULL allocated memory 
                    173:  */
1.2       misho     174: struct tagAlloc *mpool_getmembynam(mpool_t * __restrict mp, unsigned int size, const char *memname);
1.1       misho     175: /*
                    176:  * mpool_getsizebyaddr() - Get size of allocated memory block by address
                    177:  *
                    178:  * @data = allocated memory from mpool_malloc()
                    179:  * return: usable size of allocated memory block
                    180:  */
1.2       misho     181: unsigned int mpool_getsizebyaddr(void * __restrict data);
1.1       misho     182: /*
                    183:  * mpool_chkaddr() - Check validity of given address
                    184:  *
                    185:  * @data = allocated memory from mpool_malloc()
                    186:  * return: -1 bad address, 1 corrupted address or 0 ok
                    187:  */
1.2       misho     188: int mpool_chkaddr(void * __restrict data);
1.1       misho     189: /*
                    190:  * mpool_setquota() - Change maximum memory quota
                    191:  *
                    192:  * @mp = Memory pool
                    193:  * @maxmem = New max quota size
                    194:  * return: old maximum memory quota size
                    195:  */
1.2       misho     196: unsigned long mpool_setquota(mpool_t * __restrict mp, unsigned long maxmem);
1.1       misho     197: /*
                    198:  * mpool_getquota() - Get memory quota
                    199:  *
                    200:  * @mp = Memory pool
                    201:  * @currmem = Return current memory
                    202:  * @maxmem = Return max quota size
                    203:  * return: none
                    204:  */
1.2       misho     205: void mpool_getquota(mpool_t * __restrict mp, unsigned long *currmem, 
1.1       misho     206:                unsigned long *maxmem);
                    207: /*
                    208:  * mpool_statistics() - Dump statistics from memory pool buckets
                    209:  *
                    210:  * @mp = Memory pool
                    211:  * @cb = Export statistics to callback
                    212:  * return: none
                    213:  */
                    214: void mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb);
                    215: 
                    216: 
                    217: /* Wrappers */
                    218: 
                    219: /*
                    220:  * mpool_xmalloc() - malloc wrapper
                    221:  *
                    222:  * @size = Size
                    223:  * return: NULL error or !=NULL ok allocated memory
                    224:  */
                    225: void *mpool_xmalloc(size_t size);
                    226: /*
                    227:  * mpool_xcalloc() - calloc wrapper
                    228:  *
                    229:  * @num = number of elements
                    230:  * @size = Size of element
                    231:  * return: NULL error or !=NULL ok allocated memory
                    232:  */
                    233: void *mpool_xcalloc(size_t num, size_t size);
                    234: /*
                    235:  * mpool_xrealloc() - realloc wrapper
                    236:  *
                    237:  * @data = Allocated memory data
                    238:  * @newsize = New size of memory block
                    239:  * return: NULL error or !=NULL new reallocated memory block
                    240:  */
                    241: void *mpool_xrealloc(void * __restrict data, size_t newsize);
                    242: /*
                    243:  * mpool_xfree() - free wrapper
                    244:  *
                    245:  * @data = Allocated memory data
                    246:  * return: none
                    247:  */
                    248: void mpool_xfree(void * __restrict data);
                    249: /*
                    250:  * mpool_xstrdup() - strdup wrapper
                    251:  *
                    252:  * @str = string
                    253:  * return: =NULL error or !=NULL new allocated string
                    254:  */
                    255: char *mpool_xstrdup(const char *str);
1.3.20.1  misho     256: /*
                    257:  * mpool_xstatistics() - elwix memory pool statistics wrapper
                    258:  *
                    259:  * @cb = Export statistics to callback
                    260:  * return: none
                    261:  */
                    262: void mpool_xstatistics(mpool_stat_cb cb);
1.3.20.2! misho     263: /*
        !           264:  * mpool_xdump() - Dump elwix memory pool statistics
        !           265:  *
        !           266:  * return: none
        !           267:  */
        !           268: void mpool_xdump();
1.1       misho     269: 
                    270: 
                    271: #endif

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