Annotation of libaitio/inc/ampool.h, revision 1.1.2.4

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

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