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

1.1.2.1   misho       1: #ifndef __AMPOOL_H
                      2: #define __AMPOOL_H
                      3: 
                      4: 
                      5: /* Memory pool */
                      6: 
                      7: #define MEM_BUCKETS    28      /* 32 bits - 4 bits = 28 items in bucket array */
                      8: 
                      9: struct tagAlloc {
1.1.2.2 ! misho      10:        char                    alloc_name[64];
1.1.2.1   misho      11: 
                     12:        unsigned int            *alloc_mem;
                     13: 
                     14:        TAILQ_ENTRY(tagAlloc)   alloc_node;
                     15: };
                     16: typedef TAILQ_HEAD(, tagAlloc) mpool_bucket_t;
                     17: 
                     18: typedef struct _tagMPool {
                     19:        pthread_mutex_t pool_mtx;
                     20: 
                     21:        struct {
                     22:                unsigned long alloc;
                     23:                unsigned long free;
                     24:                unsigned long cache;
                     25:        } pool_calls;
                     26:        struct {
                     27:                unsigned long alloc;
                     28:                unsigned long free;
                     29:                unsigned long cache;
                     30:        } pool_bytes;
                     31:        struct {
                     32:                unsigned long max;
                     33:                unsigned long curr;
                     34:        } pool_quota;
                     35: 
                     36:        /* pool buckets */
                     37:        mpool_bucket_t  pool_active[MEM_BUCKETS];
                     38:        mpool_bucket_t  pool_inactive[MEM_BUCKETS];
                     39: } mpool_t;
                     40: #define mpool_lock(x)  (assert((x)), pthread_mutex_lock(&(x)->pool_mtx))
                     41: #define mpool_unlock(x)        (assert((x)), pthread_mutex_unlock(&(x)->pool_mtx))
                     42: 
                     43: typedef void (*mpool_stat_cb)(unsigned int, unsigned int, unsigned int);
                     44: 
                     45: /* mpool functions */
                     46: 
                     47: /*
                     48:  * mpool_init() - Init memory pool
                     49:  *
                     50:  * @maxmem = If !=0 set maximum memory quota
                     51:  * return: =NULL error or !=NULL new allocated pool
                     52:  */
                     53: mpool_t *mpool_init(unsigned long maxmem);
                     54: /*
                     55:  * mpool_destroy() - Destroy memory pool
                     56:  *
                     57:  * @mp = Memory pool
                     58:  * return: none
                     59:  */
                     60: void mpool_destroy(mpool_t ** __restrict mp);
                     61: /*
                     62:  * mpool_purge() - Purge memory block cache and release resources
                     63:  *
                     64:  * @mp = Memory pool
                     65:  * @atmost = Free at most in buckets
                     66:  * return: -1 error or 0 ok
                     67:  */
                     68: int mpool_purge(mpool_t * __restrict mp, unsigned int atmost);
                     69: /*
                     70:  * mpool_malloc() - Memory allocation
                     71:  *
                     72:  * @mp = Memory pool
                     73:  * @size = Size
                     74:  * @memname = Optional memory block name
                     75:  * return: NULL error or !=NULL ok allocated memory
                     76:  */
                     77: void *mpool_malloc(mpool_t * __restrict mp, unsigned int size, const char *memname);
                     78: /*
                     79:  * mpool_free() Free allocated memory with mpool_alloc()
                     80:  *
                     81:  * @mp = Memory pool
                     82:  * @data = Allocated memory data
                     83:  * @purge = if !=0 force release memory block
                     84:  * return: <0 error or 0 ok released memory block
                     85:  */
                     86: int mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge);
                     87: /*
                     88:  * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
                     89:  *
                     90:  * @mp = Memory pool
                     91:  * @size = Allocated memory data size
                     92:  * @memname = Memory name
                     93:  * @purge = if !=0 force release memory block
                     94:  * return: <0 error or 0 ok released memory block
                     95:  */
                     96: int mpool_free2(mpool_t * __restrict mp, unsigned int size, const char *memname, int purge);
                     97: /*
                     98:  * mpool_realloc() Reallocate memory block with new size
                     99:  *
                    100:  * @mp = Memory pool
                    101:  * @data = Allocated memory data
                    102:  * @newsize = New size of memory block
                    103:  * @memname = Optional new memory block name
                    104:  * return: NULL error or !=NULL new reallocated memory block
                    105:  */
                    106: void *mpool_realloc(mpool_t * __restrict mp, void * __restrict data, 
                    107:                unsigned int newsize, const char *memname);
                    108: /*
                    109:  * mpool_getmembynam() Find allocated memory block by size and memory name
                    110:  *
                    111:  * @mp = Memory pool
                    112:  * @size = Memory size
                    113:  * @memname = Memory name
                    114:  * return: NULL error or not found and !=NULL allocated memory 
                    115:  */
                    116: inline struct tagAlloc *mpool_getmembynam(mpool_t * __restrict mp, unsigned int size, const char *memname);
                    117: /*
                    118:  * mpool_getsizebyaddr() - Get size of allocated memory block by address
                    119:  *
                    120:  * @data = allocated memory from mpool_malloc()
                    121:  * return: usable size of allocated memory block
                    122:  */
                    123: inline unsigned int mpool_getsizebyaddr(void * __restrict data);
                    124: /*
                    125:  * mpool_chkaddr() - Check validity of given address
                    126:  *
                    127:  * @data = allocated memory from mpool_malloc()
                    128:  * return: -1 bad address, 1 corrupted address or 0 ok
                    129:  */
                    130: inline int mpool_chkaddr(void * __restrict data);
                    131: /*
                    132:  * mpool_setquota() - Change maximum memory quota
                    133:  *
                    134:  * @mp = Memory pool
                    135:  * @maxmem = New max quota size
                    136:  * return: old maximum memory quota size
                    137:  */
                    138: inline unsigned long mpool_setquota(mpool_t * __restrict mp, unsigned long maxmem);
                    139: /*
                    140:  * mpool_getquota() - Get memory quota
                    141:  *
                    142:  * @mp = Memory pool
                    143:  * @currmem = Return current memory
                    144:  * @maxmem = Return max quota size
                    145:  * return: none
                    146:  */
                    147: inline void mpool_getquota(mpool_t * __restrict mp, unsigned long *currmem, 
                    148:                unsigned long *maxmem);
                    149: /*
                    150:  * mpool_statistics() - Dump statistics from memory pool buckets
                    151:  *
                    152:  * @mp = Memory pool
                    153:  * @cb = Export statistics to callback
                    154:  * return: none
                    155:  */
                    156: void mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb);
                    157: 
                    158: 
                    159: #endif

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