--- libelwix/src/mem.c 2015/06/25 17:52:41 1.4.18.5 +++ libelwix/src/mem.c 2015/07/01 21:31:26 1.5.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.4.18.5 2015/06/25 17:52:41 misho Exp $ +* $Id: mem.c,v 1.5.2.1 2015/07/01 21:31:26 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -247,10 +247,13 @@ mpool_malloc(mpool_t * __restrict mp, u_int size, cons void * mpool_realloc(mpool_t * __restrict mp, void * __restrict data, u_int newsize, const char *memname) { - struct tagAlloc *m, *tmp; int idx, oidx; void *p; - u_int align, osize; + u_int osize; +#ifdef MPOOL_USE_REALLOC + struct tagAlloc *m, *tmp; + u_int align; +#endif /* if !data execute mpool_malloc() */ if (!data) @@ -264,21 +267,20 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri if (MEM_BADADDR(data) || MEM_CORRUPT(data)) { elwix_SetErr(EFAULT, "Corrupted memory address"); return NULL; - } else { - osize = ((u_int*)data)[-2] * sizeof(u_int); - oidx = BucketIndex(osize); } /* prepare new size */ if (newsize > MEM_ALLOC_MAX) { elwix_SetErr(ENOMEM, "Memory size is too large"); return NULL; - } else { - newsize = (newsize + 3) & ~3; /* must align to 4 because needed room for sentinels */ - idx = BucketIndex(newsize); } mpool_lock(mp); + osize = ((u_int*)data)[-2] * sizeof(u_int); + oidx = BucketIndex(osize); + newsize = (newsize + 3) & ~3; /* must align to 4 because needed room for sentinels */ + idx = BucketIndex(newsize); + /* quota */ if (mp->pool_quota.max && (mp->pool_quota.curr + ((u_long) newsize - osize)) > mp->pool_quota.max) { @@ -287,6 +289,29 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri return NULL; } +#ifndef MPOOL_USE_REALLOC + if (oidx != idx) { + mpool_unlock(mp); + p = mpool_malloc(mp, newsize, memname); + if (!p) + return NULL; + + memcpy(p, data, osize); + mpool_free(mp, data, 0); + } else { + p = data; + + ((u_int*) p)[-2] = newsize / sizeof(u_int); + ((u_int*) p)[newsize / sizeof(u_int)] = MEM_MAGIC_STOP; + + mp->pool_bytes.alloc += (u_long) newsize - osize; + mp->pool_quota.curr += (u_long) newsize - osize; + + mpool_unlock(mp); + } + + return p; +#else /* find old memory block */ TAILQ_FOREACH_SAFE(m, &mp->pool_active[oidx], alloc_node, tmp) { if (mem_data(m, void*) == data && mem_size(m) == osize) { @@ -345,6 +370,7 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri mpool_unlock(mp); return mem_data(m, void*); +#endif } /* @@ -773,16 +799,18 @@ xdump_show(u_int size, u_int act, u_int inact) } /* - * mpool_xdump() - Dump elwix memory pool statistics + * mpool_dump() - Dump elwix memory pool statistics * - * @fmt = format string + * @mp = memory pool, if =NULL dump elwix default memory pool + * @fmt = prefix info format string * @... = argument(s) * return: none */ void -mpool_xdump(const char *fmt, ...) +mpool_dump(mpool_t * __restrict mp, const char *fmt, ...) { va_list lst; + mpool_t *p = mp ? mp : elwix_mpool; if (fmt) { va_start(lst, fmt); @@ -797,12 +825,11 @@ mpool_xdump(const char *fmt, ...) "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n" "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n" "\t- buckets :\n", - elwix_mpool->pool_quota.curr, elwix_mpool->pool_quota.real, elwix_mpool->pool_quota.max, - elwix_mpool->pool_calls.alloc, elwix_mpool->pool_calls.free, - elwix_mpool->pool_calls.cache, elwix_mpool->pool_bytes.alloc, - elwix_mpool->pool_bytes.free, elwix_mpool->pool_bytes.cache); + p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max, + p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache, + p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache); - mpool_statistics(elwix_mpool, xdump_show); + mpool_statistics(p, xdump_show); printf("------------------------------------------------------------\n"); }