Keep track of dynamic buffer allocations.

This commit is contained in:
Dianne Skoll
2025-12-31 17:28:25 -05:00
parent d4ed89f5ba
commit 0ee5efa4df
4 changed files with 24 additions and 0 deletions

View File

@@ -17,6 +17,15 @@
#include <string.h>
#include <stdlib.h>
static size_t NumMallocs = 0;
static size_t BytesMalloced = 0;
void DBufGetMallocStats(size_t *num_mallocs, size_t *bytes_malloced)
{
*num_mallocs = NumMallocs;
*bytes_malloced = BytesMalloced;
}
/**********************************************************************
%FUNCTION: DBufMakeRoom
%ARGUMENTS:
@@ -45,6 +54,9 @@ static int DBufMakeRoom(DynamicBuffer *dbuf, size_t n)
buf = malloc(size);
if (!buf) return E_NO_MEM;
NumMallocs++;
BytesMalloced += size;
/* Copy contents */
strcpy(buf, dbuf->buffer);

View File

@@ -29,6 +29,8 @@ int DBufPuts(DynamicBuffer *dbuf, char const *str);
void DBufFree(DynamicBuffer *dbuf);
int DBufGets(DynamicBuffer *dbuf, FILE *fp);
void DBufGetMallocStats(size_t *num_mallocs, size_t *bytes_malloced);
#define DBufValue(bufPtr) ((bufPtr)->buffer)
#define DBufLen(bufPtr) ((bufPtr)->len)

View File

@@ -62,12 +62,17 @@ exitfunc(void)
/* Kill any execution-time-limiter process */
unlimit_execution_time();
size_t num_mallocs, bytes_malloced;
if (DebugFlag & DB_UNUSED_VARS) {
DumpUnusedVars();
}
if (DebugFlag & DB_HASHSTATS) {
fflush(stdout);
fflush(ErrFp);
DBufGetMallocStats(&num_mallocs, &bytes_malloced);
fprintf(ErrFp, "DynBuf Mallocs: %lu mallocs; %lu bytes\n",
(unsigned long) num_mallocs, (unsigned long) bytes_malloced);
fprintf(ErrFp, "Variable hash table statistics:\n");
dump_var_hash_stats();