--- libelwix/src/mem.c 2022/10/24 00:10:22 1.9 +++ libelwix/src/mem.c 2024/08/14 16:05:42 1.10 @@ -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 2024/08/14 16:05:42 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 @@ -94,7 +94,7 @@ mpool_destroy(mpool_t ** __restrict mp) struct tagAlloc *m; register int i; - if (!mp && !*mp) + if (!mp || !*mp) return; mpool_lock(*mp); @@ -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 @@ -668,7 +693,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); } /*