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

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

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