File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / ampool.h
Revision 1.8: download - view: text, annotated - select for diffs - revision graph
Thu Aug 21 15:43:00 2025 UTC (3 weeks, 4 days ago) by misho
Branches: MAIN
CVS tags: elwix6_9, elwix6_8, HEAD, ELWIX6_8, ELWIX6_7
Version 6.7

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

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