Fix t_malloc_snapshot(): stash the difference between nalloc and nfree,

not the numbers themselves.

In t_malloc_printstats(), show information about mapped allocations.

In t_malloc_leaked(), check for leaked mapped allocations.  Note that
they would show up anyway since the metadata are allocated from the
bucket allocator, but this makes them more visible.
This commit is contained in:
Dag-Erling Smørgrav 2015-11-02 19:45:12 +00:00 committed by des
parent 7b48030ada
commit 5584b68908
2 changed files with 14 additions and 11 deletions

View file

@ -203,6 +203,7 @@ t_run_test(struct t_test *t, int n)
snaplen = t_malloc_snapshot(snap2, sizeof snap2); snaplen = t_malloc_snapshot(snap2, sizeof snap2);
if (snaplen > sizeof snap2) if (snaplen > sizeof snap2)
snaplen = sizeof snap2; snaplen = sizeof snap2;
t_compare_mem(snap1, snap2, snaplen);
if (memcmp(snap1, snap2, snaplen) != 0) if (memcmp(snap1, snap2, snaplen) != 0)
t_verbose("WARNING: allocator state changed\n"); t_verbose("WARNING: allocator state changed\n");
} }

View file

@ -472,19 +472,16 @@ free(void *p)
size_t size_t
t_malloc_snapshot(void *buf, size_t len) t_malloc_snapshot(void *buf, size_t len)
{ {
unsigned long snapshot[BUCKET_MAX_SHIFT * 2]; unsigned long snapshot[BUCKET_MAX_SHIFT];
unsigned int i; unsigned int i;
if (buf == NULL) if (buf == NULL)
return (sizeof snapshot); return (sizeof snapshot);
snapshot[0] = nmapalloc; snapshot[0] = nmapalloc - nmapfree;
snapshot[1] = nmapfree;
for (i = 2; i < BUCKET_MIN_SHIFT; ++i) for (i = 2; i < BUCKET_MIN_SHIFT; ++i)
snapshot[i * 2 - 2] = snapshot[i * 2 - 1] = 0; snapshot[i - 1] = 0;
for (i = BUCKET_MIN_SHIFT; i <= BUCKET_MAX_SHIFT; ++i) { for (i = BUCKET_MIN_SHIFT; i <= BUCKET_MAX_SHIFT; ++i)
snapshot[i * 2 - 2] = buckets[i].nalloc; snapshot[i - 1] = buckets[i].nalloc - buckets[i].nfree;
snapshot[i * 2 - 1] = buckets[i].nfree;
}
if (len > sizeof snapshot) if (len > sizeof snapshot)
len = sizeof snapshot; len = sizeof snapshot;
memcpy(buf, snapshot, len); memcpy(buf, snapshot, len);
@ -500,14 +497,17 @@ t_malloc_printstats(FILE *f)
struct bucket *b; struct bucket *b;
unsigned int shift; unsigned int shift;
fprintf(f, "%6s %9s %9s %9s\n", "bucket", "alloc", "free", "leak"); fprintf(f, "%6s %9s %9s %9s\n", "bucket", "alloc", "free", "leaked");
for (shift = 0; shift <= BUCKET_MAX_SHIFT; ++shift) { for (shift = BUCKET_MIN_SHIFT; shift <= BUCKET_MAX_SHIFT; ++shift) {
b = &buckets[shift]; b = &buckets[shift];
if (b->nalloc > 0) if (b->nalloc > 0)
fprintf(f, " 1^%-3u %9lu %9lu %9lu\n", fprintf(f, " 1^%-3u %9lu %9lu %9lu\n",
shift, b->nalloc, b->nfree, shift, b->nalloc, b->nfree,
b->nalloc - b->nfree); b->nalloc - b->nfree);
} }
if (nmapalloc > 0)
fprintf(f, "%6s %9lu %9lu %9lu\n", "mapped",
nmapalloc, nmapfree, nmapalloc - nmapfree);
} }
/* /*
@ -520,10 +520,12 @@ t_malloc_leaked(char **desc, void *arg CRYB_UNUSED)
unsigned int shift; unsigned int shift;
unsigned long nleaked; unsigned long nleaked;
for (nleaked = shift = 0; shift <= BUCKET_MAX_SHIFT; ++shift) { nleaked = 0;
for (shift = BUCKET_MIN_SHIFT; shift <= BUCKET_MAX_SHIFT; ++shift) {
b = &buckets[shift]; b = &buckets[shift];
nleaked += b->nalloc - b->nfree; nleaked += b->nalloc - b->nfree;
} }
nleaked += nmapalloc - nmapfree;
if (nleaked > 0) if (nleaked > 0)
asprintf(desc, "%lu allocation(s) leaked", nleaked); asprintf(desc, "%lu allocation(s) leaked", nleaked);
else else