File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / Attic / ampool.h
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Sun Jul 22 20:39:45 2012 UTC (11 years, 11 months ago) by misho
Branches: MAIN
CVS tags: io5_0, io4_1, io4_0, io3_9, io3_8, io3_7, io3_6, io3_5, io3_4, io3_3, IO4_1, IO4_0, IO3_9, IO3_8, IO3_7, IO3_6, IO3_5, IO3_4, IO3_3, IO3_2, HEAD
version 3.2

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

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