Browse Source

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>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
a2828ed40b
  1. 20
      common/memleak.c
  2. 3
      common/memleak.h

20
common/memleak.c

@ -118,25 +118,27 @@ struct htable *memleak_enter_allocations(const tal_t *ctx,
return memtable; 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; size_t i, n;
/* Search for (aligned) pointers. */ /* Search for (aligned) pointers. */
n = tal_bytelen(p) / sizeof(void *); n = bytelen / sizeof(void *);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
void *ptr; void *ptr;
memcpy(&ptr, (char *)p + i * sizeof(void *), sizeof(ptr)); memcpy(&ptr, (char *)p + i * sizeof(void *), sizeof(ptr));
if (pointer_referenced(memtable, 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); 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) 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; size_t i;
/* Now delete the ones which are referenced. */ /* Now delete the ones which are referenced. */
memleak_scan_region(memtable, root); memleak_scan_region(memtable, root, tal_bytelen(root));
memleak_scan_region(memtable, notleaks); memleak_scan_region(memtable, notleaks, tal_bytelen(notleaks));
/* Those who asked tal children to be removed, do so. */ /* Those who asked tal children to be removed, do so. */
for (i = 0; i < tal_count(notleaks); i++) 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; const void *p;
for (p = htable_first(ht, &i); p; p = htable_next(ht, &i)) 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! */ /* 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; intmap_index_t i;
for (p = intmap_first_(m, &i); p; p = intmap_after_(m, &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) static bool ptr_match(const void *candidate, void *ptr)

3
common/memleak.h

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

Loading…
Cancel
Save