--- libaitsess/src/Attic/mem.c 2012/02/28 10:34:43 1.1.2.7 +++ libaitsess/src/Attic/mem.c 2012/02/28 13:00:24 1.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.1.2.7 2012/02/28 10:34:43 misho Exp $ +* $Id: mem.c,v 1.2 2012/02/28 13:00:24 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -149,8 +149,6 @@ pullInactive(mpool_t * __restrict mp, int idx) /* clear name */ *m->alloc_name = 0; - /* clear flags */ - m->alloc_flags ^= m->alloc_flags; } return m; @@ -168,7 +166,8 @@ void * mpool_malloc(mpool_t * __restrict mp, u_int size, const char *memname) { struct tagAlloc *m; - int idx, align; + int idx; + u_int align; if (!mp) { sess_SetErr(EINVAL, "Pool not specified"); @@ -243,8 +242,9 @@ void * mpool_realloc(mpool_t * __restrict mp, void * __restrict data, u_int newsize, const char *memname) { struct tagAlloc *m, *tmp; - int idx, align, oidx, osize; + int idx, oidx; void *p; + u_int align, osize; /* if !data execute mpool_malloc() */ if (!data) @@ -275,20 +275,25 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri /* quota */ if (mp->pool_quota.max && - (mp->pool_quota.curr + (newsize - osize)) > mp->pool_quota.max) { + (mp->pool_quota.curr + ((u_long) newsize - osize)) > mp->pool_quota.max) { sess_SetErr(ENOMEM, "Max.allocate memory quota has been reached"); mpool_unlock(mp); return NULL; } /* find old memory block */ - TAILQ_FOREACH_SAFE(m, &mp->pool_active[oidx], alloc_node, tmp) + TAILQ_FOREACH_SAFE(m, &mp->pool_active[oidx], alloc_node, tmp) { if (mem_data(m, void*) == data && mem_size(m) == osize) { - TAILQ_REMOVE(&mp->pool_active[oidx], m, alloc_node); - /* statistics */ - mp->pool_calls.alloc--; + /* case in different buckets */ + if (oidx != idx) { + TAILQ_REMOVE(&mp->pool_active[oidx], m, alloc_node); + /* statistics */ + mp->pool_calls.alloc--; + } mp->pool_bytes.alloc -= osize; + break; } + } /* memory block not found! */ if (!m) { mpool_unlock(mp); @@ -297,28 +302,35 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri } /* try to reallocate memory block to new bucket */ - align = 1 << (idx + MEM_MIN_BUCKET); - p = realloc(m->alloc_mem, align + 12); - if (!p) { - LOGERR; + if (oidx != idx) { + align = 1 << (idx + MEM_MIN_BUCKET); + p = realloc(m->alloc_mem, align + 12); + if (!p) { + LOGERR; - /* restore to old bucket pulled memory block for reallocation */ - TAILQ_INSERT_HEAD(&mp->pool_active[oidx], m, alloc_node); - /* statistics */ - mp->pool_calls.alloc++; - mp->pool_bytes.alloc += osize; + /* restore to old bucket pulled memory block for reallocation */ + TAILQ_INSERT_HEAD(&mp->pool_active[oidx], m, alloc_node); + /* statistics */ + mp->pool_calls.alloc++; + mp->pool_bytes.alloc += osize; - mpool_unlock(mp); - return NULL; - } else /* quota */ - mp->pool_quota.curr += (newsize - osize); + mpool_unlock(mp); + return NULL; + } else + m->alloc_mem = (u_int*) p; + } + /* quota */ + mp->pool_quota.curr += (u_long) newsize - osize; m->alloc_mem[0] = newsize / sizeof(u_int); m->alloc_mem[1] = MEM_MAGIC_START; m->alloc_mem[2 + newsize / sizeof(u_int)] = MEM_MAGIC_STOP; - TAILQ_INSERT_HEAD(&mp->pool_active[idx], m, alloc_node); - /* statistics */ - mp->pool_calls.alloc++; + + if (oidx != idx) { + TAILQ_INSERT_HEAD(&mp->pool_active[idx], m, alloc_node); + /* statistics */ + mp->pool_calls.alloc++; + } mp->pool_bytes.alloc += newsize; if (memname)