--- libelwix/src/mem.c 2022/10/24 00:10:22 1.9 +++ libelwix/src/mem.c 2024/10/27 10:08:02 1.10.8.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.9 2022/10/24 00:10:22 misho Exp $ +* $Id: mem.c,v 1.10.8.2 2024/10/27 10:08:02 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 - 2022 +Copyright 2004 - 2024 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -91,23 +91,23 @@ mpool_init(u_long maxmem) void mpool_destroy(mpool_t ** __restrict mp) { - struct tagAlloc *m; + struct tagAlloc *m, *n; register int i; - if (!mp && !*mp) + if (!mp || !*mp) return; mpool_lock(*mp); for (i = 0; i < MEM_BUCKETS; i++) { - while ((m = TAILQ_FIRST(&(*mp)->pool_active[i]))) { - TAILQ_REMOVE(&(*mp)->pool_active[i], m, alloc_node); + for (m = TAILQ_FIRST(&(*mp)->pool_active[i]); m; m = n) { + n = TAILQ_NEXT(m, alloc_node); if (m->alloc_mem) free(m->alloc_mem); free(m); } - while ((m = TAILQ_FIRST(&(*mp)->pool_inactive[i]))) { - TAILQ_REMOVE(&(*mp)->pool_inactive[i], m, alloc_node); + for (m = TAILQ_FIRST(&(*mp)->pool_inactive[i]); m; m = n) { + n = TAILQ_NEXT(m, alloc_node); if (m->alloc_mem) free(m->alloc_mem); free(m); @@ -219,7 +219,9 @@ mpool_malloc(mpool_t * __restrict mp, u_int size, cons } else { /* quota */ mp->pool_quota.curr += size; mp->pool_quota.real += 1 << (idx + MEM_MIN_BUCKET); +#ifdef MPOOL_MEM_ZERO memset(m->alloc_mem, 0, align + 12); +#endif } } @@ -236,6 +238,29 @@ mpool_malloc(mpool_t * __restrict mp, u_int size, cons } /* + * mpool_calloc() - Multiple memory block allocation + * + * @mp = Memory pool + * @nmemb = Number of memory blocks + * @size = Size + * @memname = Optional memory block name + * return: NULL error or !=NULL ok allocated memory + */ +void * +mpool_calloc(mpool_t * __restrict mp, u_int nmemb, u_int size, const char *memname) +{ + void *m; + u_int total = nmemb * size; + + m = mpool_malloc(mp, total, memname); +#ifndef MPOOL_MEM_ZERO + if (m) + memset(m, 0, total); +#endif + return m; +} + +/* * mpool_realloc() Reallocate memory block with new size * * @mp = Memory pool @@ -293,7 +318,6 @@ mpool_realloc(mpool_t * __restrict mp, void * __restri memcpy(p, data, MIN(osize, newsize)); mpool_free(mp, data, 0); - data = p; } else { p = data; @@ -668,7 +692,7 @@ mpool_xmalloc(size_t size) void * mpool_xcalloc(size_t num, size_t size) { - return mpool_malloc(elwix_mpool, num * size, elwix_Prog); + return mpool_calloc(elwix_mpool, num, size, elwix_Prog); } /*