Skip to content

Commit

Permalink
Merge pull request #107 from dag-erling/des/xmalloc
Browse files Browse the repository at this point in the history
Improve xmalloc
  • Loading branch information
dag-erling authored Jul 30, 2024
2 parents 9c098ea + 9f40449 commit 369cbec
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/xmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

typedef struct hashTableItemRec {
void *ptr;
int bytes;
size_t bytes;
const char *file;
int line;
const char *func;
Expand Down Expand Up @@ -90,7 +90,7 @@ hash_void_ptr(void *ptr)
}

static void
hash_table_add(hashTable *tbl, void *ptr, int bytes,
hash_table_add(hashTable *tbl, void *ptr, size_t bytes,
const char *file, int line, const char *func)
{
int i;
Expand Down Expand Up @@ -125,6 +125,9 @@ hash_table_add(hashTable *tbl, void *ptr, int bytes,
}

static void
#if defined(__GNUC__) && __GNUC__ >= 10
__attribute__((access(none, 2)))
#endif
hash_table_del(hashTable *tbl, void *ptr)
{
int i;
Expand Down Expand Up @@ -199,9 +202,9 @@ xmalloc_configure(int fail_after)
int
xmalloc_dump_leaks(void)
{
int i;
int num_leaks = 0;
int leaked_bytes = 0;
unsigned int i;
unsigned int num_leaks = 0;
size_t leaked_bytes = 0;
hashTableItem *item;

xmalloc_init();
Expand All @@ -211,7 +214,7 @@ xmalloc_dump_leaks(void)
item = xmalloc_table->table[i];
while (item != NULL)
{
printf("%s:%d: %s: %d bytes at %p not freed\n",
printf("%s:%d: %s: %zu bytes at %p not freed\n",
item->file, item->line, item->func, item->bytes, item->ptr);
num_leaks++;
leaked_bytes += item->bytes;
Expand All @@ -221,7 +224,7 @@ xmalloc_dump_leaks(void)
if (num_leaks == 0)
printf("No memory leaks.\n");
else
printf("%d unfreed memory chuncks, total %d unfreed bytes.\n",
printf("%u unfreed memory chuncks, total %zu unfreed bytes.\n",
num_leaks, leaked_bytes);
printf("Peak memory consumption %d bytes (%.1f kB, %.1f MB) in %d blocks ",
xmalloc_peak, (double)xmalloc_peak / 1024,
Expand Down Expand Up @@ -338,7 +341,7 @@ xrealloc_impl(void *ptr, size_t new_size, const char *file, int line,
xmalloc_fail_after--;

new_ptr = realloc(ptr, new_size);
if (new_ptr != NULL)
if (new_ptr != NULL && new_ptr != ptr)
{
hash_table_del(xmalloc_table, ptr);
hash_table_add(xmalloc_table, new_ptr, (int)new_size, file, line, func);
Expand Down

0 comments on commit 369cbec

Please sign in to comment.