From a2828ed40b76c15ad2448c8ca503dbf36003e3b9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 24 Aug 2018 14:50:06 +0930 Subject: [PATCH] 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 --- common/memleak.c | 20 +++++++++++--------- common/memleak.h | 3 ++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/common/memleak.c b/common/memleak.c index f1cba6a29..3d12f8d64 100644 --- a/common/memleak.c +++ b/common/memleak.c @@ -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) diff --git a/common/memleak.h b/common/memleak.h index cd0e12c4a..df2cced7e 100644 --- a/common/memleak.h +++ b/common/memleak.h @@ -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);