--- libelwix/src/mem.c 2017/11/30 13:46:27 1.8 +++ libelwix/src/mem.c 2024/10/26 14:55:56 1.10.8.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mem.c,v 1.8 2017/11/30 13:46:27 misho Exp $ +* $Id: mem.c,v 1.10.8.1 2024/10/26 14:55:56 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 - 2017 +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 @@ -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; @@ -620,16 +644,17 @@ mpool_getquota(mpool_t * __restrict mp, u_long *currme * * @mp = Memory pool * @cb = Export statistics to callback - * return: none + * return: -1 error or >0 bytes in data buffer */ -void -mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb) +int +mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb, void *data, u_int datlen) { struct tagAlloc *m; register int i, act, inact; + int len = 0; if (!mp || !cb) - return; + return -1; for (i = act = inact = 0; i < MEM_BUCKETS; act = inact = 0, i++) { TAILQ_FOREACH(m, &mp->pool_active[i], alloc_node) @@ -637,8 +662,10 @@ mpool_statistics(mpool_t * __restrict mp, mpool_stat_c TAILQ_FOREACH(m, &mp->pool_inactive[i], alloc_node) inact++; - cb(1 << (i + MEM_MIN_BUCKET), act, inact); + len += cb(1 << (i + MEM_MIN_BUCKET), act, inact, data, datlen); } + + return len; } /* ----------------------------------------------------------- */ @@ -665,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); } /* @@ -706,22 +733,24 @@ mpool_xstrdup(const char *str) } /* - * mpool_xstatistics() - elwix memory pool statistics wrapper + * mpool_xstatistics() - elwix default memory pool statistics wrapper * * @cb = Export statistics to callback - * return: none + * @data = data buffer + * @datlen = data buffer length + * return: >0 data in string buffer */ -void -mpool_xstatistics(mpool_stat_cb cb) +int +mpool_xstatistics(mpool_stat_cb cb, void *data, u_int datlen) { - mpool_statistics(elwix_mpool, cb); + return mpool_statistics(elwix_mpool, cb, data, datlen); } -static void -xdump_show(u_int size, u_int act, u_int inact) +static int +xdump_show(u_int size, u_int act, u_int inact, void *data, u_int datlen) { if (!act && !inact) - return; /* skip empty bucket */ + return 0; /* skip empty bucket */ if (size < 1024) printf("\t\t* BUCKET %uB size, %u active, %u inactive\n", @@ -732,8 +761,35 @@ xdump_show(u_int size, u_int act, u_int inact) else printf("\t\t* BUCKET %uMB size, %u active, %u inactive\n", size / 1048576, act, inact); + + return 0; } +static int +xdump_show2(u_int size, u_int act, u_int inact, void *data, u_int datlen) +{ + char szStr[STRSIZ], *str = data; + int len = 0; + + if (!data || !datlen) + return 0; /* skip missing data buffer */ + if (!act && !inact) + return 0; /* skip empty bucket */ + + if (size < 1024) + len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uB size, %u active, %u inactive\n", + size, act, inact); + else if (size < 1048576) + len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uKB size, %u active, %u inactive\n", + size / 1024, act, inact); + else + len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uMB size, %u active, %u inactive\n", + size / 1048576, act, inact); + + strlcat(str, szStr, datlen); + return len; +} + /* * mpool_dump() - Dump elwix memory pool statistics * @@ -756,16 +812,46 @@ mpool_dump(mpool_t * __restrict mp, const char *fmt, . printf("\n%s(%d)\n", __func__, __LINE__); printf("------------------------------------------------------------\n"); - printf( " ELWIX memory pool ::\n" + printf( " ELWIX memory pool %p ::\n" "\t- quotas Current/Real/Max = %lu/%lu/%lu\n" "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n" "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n" - "\t- buckets :\n", + "\t- buckets :\n", mp, 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(p, xdump_show); + mpool_statistics(p, xdump_show, NULL, 0); printf("------------------------------------------------------------\n"); } +/* + * mpool_dump2() - Dump elwix memory pool statistics to string + * + * @mp = memory pool, if =NULL dump elwix default memory pool + * @str = string buffer + * @strlen = string buffer length + * return: >0 data in string buffer + */ +int +mpool_dump2(mpool_t * __restrict mp, char *str, int strlen) +{ + int len; + mpool_t *p = mp ? mp : elwix_mpool; + + len = snprintf(str, strlen, + "------------------------------------------------------------\n"); + len += snprintf(str + len, strlen - len, " ELWIX memory pool %p ::\n" + "\t- quotas Current/Real/Max = %lu/%lu/%lu\n" + "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n" + "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n" + "\t- buckets :\n", mp, + 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); + len += mpool_statistics(p, xdump_show2, str + len, strlen - len); + len += snprintf(str + len, strlen - len, + "------------------------------------------------------------\n"); + + return len; +}