--- libelwix/src/mem.c 2015/07/01 21:31:26 1.5.2.1 +++ libelwix/src/mem.c 2016/05/14 13:12:43 1.6.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.5.2.1 2015/07/01 21:31:26 misho Exp $ +* $Id: mem.c,v 1.6.2.2 2016/05/14 13:12:43 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2015 +Copyright 2004 - 2016 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -250,10 +250,6 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri int idx, oidx; void *p; u_int osize; -#ifdef MPOOL_USE_REALLOC - struct tagAlloc *m, *tmp; - u_int align; -#endif /* if !data execute mpool_malloc() */ if (!data) @@ -289,14 +285,13 @@ 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); + memcpy(p, data, MIN(osize, newsize)); mpool_free(mp, data, 0); } else { p = data; @@ -311,66 +306,6 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri } 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) { - /* 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); - elwix_SetErr(EFAULT, "Memory block not found"); - return NULL; - } - - /* try to reallocate memory block to new bucket */ - 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; - - mpool_unlock(mp); - return NULL; - } else - m->alloc_mem = (u_int*) p; - } - /* quota */ - mp->pool_quota.curr += (u_long) newsize - osize; - mp->pool_quota.real += (1 << (idx + MEM_MIN_BUCKET)) - (1 << (oidx + MEM_MIN_BUCKET)); - - 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; - - 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) - strlcpy(m->alloc_name, memname, sizeof m->alloc_name); - - mpool_unlock(mp); - return mem_data(m, void*); -#endif } /*