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

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.7     ! misho       6: * $Id: ampool.h,v 1.6.12.1 2024/08/14 16:01:10 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.7     ! 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 __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.4       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: 
1.6       misho      93: typedef int (*mpool_stat_cb)(unsigned int, unsigned int, unsigned int, 
                     94:                void *, unsigned int);
1.1       misho      95: 
                     96: /* mpool functions */
                     97: 
                     98: /*
                     99:  * mpool_init() - Init memory pool
                    100:  *
                    101:  * @maxmem = If !=0 set maximum memory quota
                    102:  * return: =NULL error or !=NULL new allocated pool
                    103:  */
                    104: mpool_t *mpool_init(unsigned long maxmem);
                    105: /*
                    106:  * mpool_destroy() - Destroy memory pool
                    107:  *
                    108:  * @mp = Memory pool
                    109:  * return: none
                    110:  */
                    111: void mpool_destroy(mpool_t ** __restrict mp);
                    112: /*
                    113:  * mpool_purge() - Purge memory block cache and release resources
                    114:  *
                    115:  * @mp = Memory pool
                    116:  * @atmost = Free at most in buckets
                    117:  * return: -1 error or 0 ok
                    118:  */
                    119: int mpool_purge(mpool_t * __restrict mp, unsigned int atmost);
                    120: /*
                    121:  * mpool_malloc() - Memory allocation
                    122:  *
                    123:  * @mp = Memory pool
                    124:  * @size = Size
                    125:  * @memname = Optional memory block name
                    126:  * return: NULL error or !=NULL ok allocated memory
                    127:  */
                    128: void *mpool_malloc(mpool_t * __restrict mp, unsigned int size, const char *memname);
                    129: /*
1.7     ! misho     130:  * mpool_calloc() - Multiple memory block allocation
        !           131:  *
        !           132:  * @mp = Memory pool
        !           133:  * @nmemb = Number of memory blocks
        !           134:  * @size = Size
        !           135:  * @memname = Optional memory block name
        !           136:  * return: NULL error or !=NULL ok allocated memory
        !           137:  */
        !           138: void *mpool_calloc(mpool_t * __restrict mp, u_int nmemb, u_int size, const char *memname);
        !           139: /*
1.1       misho     140:  * mpool_free() Free allocated memory with mpool_alloc()
                    141:  *
                    142:  * @mp = Memory pool
                    143:  * @data = Allocated memory data
                    144:  * @purge = if !=0 force release memory block
                    145:  * return: <0 error or 0 ok released memory block
                    146:  */
                    147: int mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge);
                    148: /*
                    149:  * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
                    150:  *
                    151:  * @mp = Memory pool
                    152:  * @size = Allocated memory data size
                    153:  * @memname = Memory name
                    154:  * @purge = if !=0 force release memory block
                    155:  * return: <0 error or 0 ok released memory block
                    156:  */
                    157: int mpool_free2(mpool_t * __restrict mp, unsigned int size, const char *memname, int purge);
                    158: /*
                    159:  * mpool_realloc() Reallocate memory block with new size
                    160:  *
                    161:  * @mp = Memory pool
                    162:  * @data = Allocated memory data
                    163:  * @newsize = New size of memory block
                    164:  * @memname = Optional new memory block name
                    165:  * return: NULL error or !=NULL new reallocated memory block
                    166:  */
                    167: void *mpool_realloc(mpool_t * __restrict mp, void * __restrict data, 
                    168:                unsigned int newsize, const char *memname);
                    169: /*
                    170:  * mpool_strdup() - String duplicate
                    171:  *
                    172:  * @mp = Memory pool
                    173:  * @str = String
                    174:  * @memname = Memory name
                    175:  * return: NULL error or !=NULL new string
                    176:  */
                    177: char *mpool_strdup(mpool_t * __restrict mp, const char *str, const char *memname);
                    178: /*
                    179:  * mpool_getmembynam() Find allocated memory block by size and memory name
                    180:  *
                    181:  * @mp = Memory pool
                    182:  * @size = Memory size
                    183:  * @memname = Memory name
                    184:  * return: NULL error or not found and !=NULL allocated memory 
                    185:  */
1.2       misho     186: struct tagAlloc *mpool_getmembynam(mpool_t * __restrict mp, unsigned int size, const char *memname);
1.1       misho     187: /*
                    188:  * mpool_getsizebyaddr() - Get size of allocated memory block by address
                    189:  *
                    190:  * @data = allocated memory from mpool_malloc()
                    191:  * return: usable size of allocated memory block
                    192:  */
1.2       misho     193: unsigned int mpool_getsizebyaddr(void * __restrict data);
1.1       misho     194: /*
                    195:  * mpool_chkaddr() - Check validity of given address
                    196:  *
                    197:  * @data = allocated memory from mpool_malloc()
                    198:  * return: -1 bad address, 1 corrupted address or 0 ok
                    199:  */
1.2       misho     200: int mpool_chkaddr(void * __restrict data);
1.1       misho     201: /*
                    202:  * mpool_setquota() - Change maximum memory quota
                    203:  *
                    204:  * @mp = Memory pool
                    205:  * @maxmem = New max quota size
                    206:  * return: old maximum memory quota size
                    207:  */
1.2       misho     208: unsigned long mpool_setquota(mpool_t * __restrict mp, unsigned long maxmem);
1.1       misho     209: /*
                    210:  * mpool_getquota() - Get memory quota
                    211:  *
                    212:  * @mp = Memory pool
1.4       misho     213:  * @currmem = Return current memory usage
                    214:  * @realmem = Return current real memory usage
1.1       misho     215:  * @maxmem = Return max quota size
                    216:  * return: none
                    217:  */
1.2       misho     218: void mpool_getquota(mpool_t * __restrict mp, unsigned long *currmem, 
1.4       misho     219:                unsigned long *realmem, unsigned long *maxmem);
1.1       misho     220: /*
                    221:  * mpool_statistics() - Dump statistics from memory pool buckets
                    222:  *
                    223:  * @mp = Memory pool
                    224:  * @cb = Export statistics to callback
1.6       misho     225:  * return: -1 error or >0 bytes in data buffer
1.1       misho     226:  */
1.6       misho     227: int mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb, 
                    228:                void *data, unsigned int datlen);
1.1       misho     229: 
                    230: 
                    231: /* Wrappers */
                    232: 
                    233: /*
                    234:  * mpool_xmalloc() - malloc wrapper
                    235:  *
                    236:  * @size = Size
                    237:  * return: NULL error or !=NULL ok allocated memory
                    238:  */
                    239: void *mpool_xmalloc(size_t size);
                    240: /*
                    241:  * mpool_xcalloc() - calloc wrapper
                    242:  *
                    243:  * @num = number of elements
                    244:  * @size = Size of element
                    245:  * return: NULL error or !=NULL ok allocated memory
                    246:  */
                    247: void *mpool_xcalloc(size_t num, size_t size);
                    248: /*
                    249:  * mpool_xrealloc() - realloc wrapper
                    250:  *
                    251:  * @data = Allocated memory data
                    252:  * @newsize = New size of memory block
                    253:  * return: NULL error or !=NULL new reallocated memory block
                    254:  */
                    255: void *mpool_xrealloc(void * __restrict data, size_t newsize);
                    256: /*
                    257:  * mpool_xfree() - free wrapper
                    258:  *
                    259:  * @data = Allocated memory data
                    260:  * return: none
                    261:  */
                    262: void mpool_xfree(void * __restrict data);
                    263: /*
                    264:  * mpool_xstrdup() - strdup wrapper
                    265:  *
                    266:  * @str = string
                    267:  * return: =NULL error or !=NULL new allocated string
                    268:  */
                    269: char *mpool_xstrdup(const char *str);
1.4       misho     270: /*
1.6       misho     271:  * mpool_xstatistics() - elwix default memory pool statistics wrapper
1.4       misho     272:  *
                    273:  * @cb = Export statistics to callback
1.6       misho     274:  * @data = data buffer
                    275:  * @datlen = data buffer length
                    276:  * return: -1 error or >0 bytes in data buffer
1.4       misho     277:  */
1.6       misho     278: int mpool_xstatistics(mpool_stat_cb cb, void *data, unsigned int datlen);
1.4       misho     279: /*
1.5       misho     280:  * mpool_dump() - Dump elwix memory pool statistics
1.4       misho     281:  *
1.5       misho     282:  * @mp = memory pool, if =NULL dump elwix default memory pool
                    283:  * @fmt = prefix info format string
1.4       misho     284:  * @... = argument(s)
                    285:  * return: none
                    286:  */
1.5       misho     287: void mpool_dump(mpool_t * __restrict mp, const char *fmt, ...);
1.6       misho     288: /*
                    289:  * mpool_dump2() - Dump elwix memory pool statistics to string
                    290:  *
                    291:  * @mp = memory pool, if =NULL dump elwix default memory pool
                    292:  * @str = string buffer
                    293:  * @strlen = string buffer length
                    294:  * return: >0 data in string buffer
                    295:  */
                    296: int mpool_dump2(mpool_t * __restrict mp, char *str, int strlen);
1.1       misho     297: 
                    298: 
                    299: #endif

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