--- libaitsess/src/Attic/mem.c 2012/02/27 21:00:43 1.1.2.2 +++ libaitsess/src/Attic/mem.c 2012/02/27 23:57:12 1.1.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.1.2.2 2012/02/27 21:00:43 misho Exp $ +* $Id: mem.c,v 1.1.2.3 2012/02/27 23:57:12 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -55,6 +55,7 @@ mpool_t * mpool_init(void) { mpool_t *mp; + register int i; mp = malloc(sizeof(mpool_t)); if (!mp) { @@ -64,6 +65,14 @@ mpool_init(void) memset(mp, 0, sizeof(mpool_t)); pthread_mutex_init(&mp->pool_mtx, NULL); + + mpool_lock(mp); + for (i = 0; i < MEM_BUCKETS; i++) { + TAILQ_INIT(&mp->pool_active[i]); + TAILQ_INIT(&mp->pool_inactive[i]); + } + mpool_unlock(mp); + return mp; } @@ -131,6 +140,9 @@ pullInactive(mpool_t * __restrict mp, int idx) /* must be locked pool before use this function */ if ((m = TAILQ_FIRST(&mp->pool_inactive[idx]))) { TAILQ_REMOVE(&mp->pool_inactive[idx], m, alloc_node); + /* statistics */ + mp->pool_calls.cache--; + mp->pool_bytes.cache -= mem_size(m); /* clear name */ *m->alloc_name = 0; @@ -216,6 +228,52 @@ mpool_malloc(mpool_t * __restrict mp, u_int size, cons } /* + * mpool_purge() - Purge memory block cache and release resources + * + * @mp = Memory pool + * @atmost = Free at most in buckets + * return: -1 error or 0 ok + */ +int +mpool_purge(mpool_t * __restrict mp, int atmost) +{ + register int i, cx = 0; + struct tagAlloc *m, *tmp; + + if (!mp) { + sess_SetErr(EINVAL, "Pool not specified"); + return -1; + } + + mpool_lock(mp); + + for (i = 0; i < MEM_BUCKETS; i++) { + TAILQ_FOREACH_SAFE(m, &mp->pool_inactive[i], alloc_node, tmp) { + /* count barrier for purge */ + if (cx++ < atmost) + continue; + + TAILQ_REMOVE(&mp->pool_inactive[i], m, alloc_node); + /* statistics */ + mp->pool_calls.cache--; + mp->pool_bytes.cache -= mem_size(m); + + mp->pool_calls.free++; + mp->pool_bytes.free += mem_size(m); + /* quota */ + mp->pool_quota.curr -= mem_size(m); + + if (m->alloc_mem) + free(m->alloc_mem); + free(m); + } + } + + mpool_unlock(mp); + return 0; +} + +/* * mpool_free() Free allocated memory with mpool_alloc() * * @mp = Memory pool @@ -244,6 +302,9 @@ mpool_free(mpool_t * __restrict mp, void * __restrict TAILQ_FOREACH(m, &mp->pool_active[idx], alloc_node) if (mem_data(m, void*) == data) { TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node); + /* statistics */ + mp->pool_calls.alloc--; + mp->pool_bytes.alloc -= mem_size(m); if (!purge) { TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node); @@ -251,12 +312,15 @@ mpool_free(mpool_t * __restrict mp, void * __restrict mp->pool_calls.cache++; mp->pool_bytes.cache += mem_size(m); } else { - if (m->alloc_mem) - free(m->alloc_mem); - free(m); /* statistics */ mp->pool_calls.free++; mp->pool_bytes.free += mem_size(m); + /* quota */ + mp->pool_quota.curr -= mem_size(m); + + if (m->alloc_mem) + free(m->alloc_mem); + free(m); } break; } @@ -290,6 +354,9 @@ mpool_free2(mpool_t * __restrict mp, u_int size, const TAILQ_FOREACH(m, &mp->pool_active[idx], alloc_node) if (!strcmp(m->alloc_name, memname)) { TAILQ_REMOVE(&mp->pool_active[idx], m, alloc_node); + /* statistics */ + mp->pool_calls.alloc--; + mp->pool_bytes.alloc -= mem_size(m); if (!purge) { TAILQ_INSERT_HEAD(&mp->pool_inactive[idx], m, alloc_node); @@ -297,12 +364,15 @@ mpool_free2(mpool_t * __restrict mp, u_int size, const mp->pool_calls.cache++; mp->pool_bytes.cache += mem_size(m); } else { - if (m->alloc_mem) - free(m->alloc_mem); - free(m); /* statistics */ mp->pool_calls.free++; mp->pool_bytes.free += mem_size(m); + /* quota */ + mp->pool_quota.curr -= mem_size(m); + + if (m->alloc_mem) + free(m->alloc_mem); + free(m); } break; } @@ -334,4 +404,38 @@ mpool_getmembynam(mpool_t * __restrict mp, u_int size, break; return mem_data(m, void*); +} + +/* + * mpool_getsizebyaddr() - Get size of allocated memory block by address + * + * @addr = allocated memory from mpool_malloc() + * return: usable size of allocated memory block + */ +inline u_int +mpool_getsizebyaddr(void * __restrict data) +{ + if (mpool_chkaddr(data)) + return 0; + + return (((u_int*) data)[-2] * sizeof(u_int)); +} + +/* + * mpool_chkaddr() - Check validity of given address + * + * @data = allocated memory from mpool_malloc() + * return: -1 bad address, 1 corrupted address or 0 ok + */ +inline int +mpool_chkaddr(void * __restrict data) +{ + /* check address range */ + if (MEM_BADADDR(data)) + return -1; + /* check sentinel */ + if (MEM_CORRUPT(data)) + return 1; + /* data address is ok! */ + return 0; }