memleak: allow for scanning non-talloc regions.

For some daemons we'll be handing it non-talloc memory to scan for pointers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-08-24 14:50:06 +09:30 committed by Christian Decker
parent 8733015836
commit a2828ed40b
2 changed files with 13 additions and 10 deletions

View File

@ -118,25 +118,27 @@ struct htable *memleak_enter_allocations(const tal_t *ctx,
return memtable;
}
static void scan_for_pointers(struct htable *memtable, const tal_t *p)
static void scan_for_pointers(struct htable *memtable,
const tal_t *p, size_t bytelen)
{
size_t i, n;
/* Search for (aligned) pointers. */
n = tal_bytelen(p) / sizeof(void *);
n = bytelen / sizeof(void *);
for (i = 0; i < n; i++) {
void *ptr;
memcpy(&ptr, (char *)p + i * sizeof(void *), sizeof(ptr));
if (pointer_referenced(memtable, ptr))
scan_for_pointers(memtable, ptr);
scan_for_pointers(memtable, ptr, tal_bytelen(ptr));
}
}
void memleak_scan_region(struct htable *memtable, const void *ptr)
void memleak_scan_region(struct htable *memtable,
const void *ptr, size_t bytelen)
{
pointer_referenced(memtable, ptr);
scan_for_pointers(memtable, ptr);
scan_for_pointers(memtable, ptr, bytelen);
}
static void remove_with_children(struct htable *memtable, const tal_t *p)
@ -153,8 +155,8 @@ void memleak_remove_referenced(struct htable *memtable, const void *root)
size_t i;
/* Now delete the ones which are referenced. */
memleak_scan_region(memtable, root);
memleak_scan_region(memtable, notleaks);
memleak_scan_region(memtable, root, tal_bytelen(root));
memleak_scan_region(memtable, notleaks, tal_bytelen(notleaks));
/* Those who asked tal children to be removed, do so. */
for (i = 0; i < tal_count(notleaks); i++)
@ -175,7 +177,7 @@ void memleak_remove_htable(struct htable *memtable, const struct htable *ht)
const void *p;
for (p = htable_first(ht, &i); p; p = htable_next(ht, &i))
memleak_scan_region(memtable, p);
memleak_scan_region(memtable, p, tal_bytelen(p));
}
/* FIXME: If uintmap used tal, this wouldn't be necessary! */
@ -185,7 +187,7 @@ void memleak_remove_intmap_(struct htable *memtable, const struct intmap *m)
intmap_index_t i;
for (p = intmap_first_(m, &i); p; p = intmap_after_(m, &i))
memleak_scan_region(memtable, p);
memleak_scan_region(memtable, p, tal_bytelen(p));
}
static bool ptr_match(const void *candidate, void *ptr)

View File

@ -47,7 +47,8 @@ struct intmap;
void memleak_remove_intmap_(struct htable *memtable, const struct intmap *m);
/* Mark this pointer as being referenced, and search within for more. */
void memleak_scan_region(struct htable *memtable, const void *p);
void memleak_scan_region(struct htable *memtable,
const void *p, size_t bytelen);
/* Get (and remove) a leak from memtable, or NULL */
const void *memleak_get(struct htable *memtable, const uintptr_t **backtrace);