Browse Source

common: add tal_gather_wally() function to reparent libwally objs.

This lets us reduce leaks, and ease their detection.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
travis-experimental
Rusty Russell 4 years ago
parent
commit
a316c517c7
  1. 2
      common/setup.c
  2. 10
      common/utils.c
  3. 6
      common/utils.h

2
common/setup.c

@ -5,8 +5,6 @@
#include <sodium.h> #include <sodium.h>
#include <wally_core.h> #include <wally_core.h>
static const tal_t *wally_tal_ctx;
static void *wally_tal(size_t size) static void *wally_tal(size_t size)
{ {
return tal_arr_label(wally_tal_ctx, u8, size, "wally_notleak"); return tal_arr_label(wally_tal_ctx, u8, size, "wally_notleak");

10
common/utils.c

@ -5,6 +5,7 @@
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <locale.h> #include <locale.h>
const tal_t *wally_tal_ctx;
secp256k1_context *secp256k1_ctx; secp256k1_context *secp256k1_ctx;
const tal_t *tmpctx; const tal_t *tmpctx;
@ -15,6 +16,15 @@ bool is_elements(const struct chainparams *chainparams)
return chainparams->is_elements; return chainparams->is_elements;
} }
/* Steal any wally allocations onto this context. */
void tal_gather_wally(const tal_t *ctx)
{
tal_t *p;
assert(tal_first(wally_tal_ctx));
while ((p = tal_first(wally_tal_ctx)) != NULL)
tal_steal(ctx, p);
}
#if DEVELOPER #if DEVELOPER
/* If you've got a softref, we assume no reallocs. */ /* If you've got a softref, we assume no reallocs. */
static void dont_move_softref(tal_t *ctx, enum tal_notify_type ntype, void *info) static void dont_move_softref(tal_t *ctx, enum tal_notify_type ntype, void *info)

6
common/utils.h

@ -84,6 +84,9 @@ void setup_tmpctx(void);
/* Free any children of tmpctx. */ /* Free any children of tmpctx. */
void clean_tmpctx(void); void clean_tmpctx(void);
/* Steal any wally allocations onto this context. */
void tal_gather_wally(const tal_t *ctx);
/* Define sha256_eq. */ /* Define sha256_eq. */
STRUCTEQ_DEF(sha256, 0, u); STRUCTEQ_DEF(sha256, 0, u);
@ -106,4 +109,7 @@ STRUCTEQ_DEF(ripemd160, 0, u);
#define IFDEV(dev, nondev) (nondev) #define IFDEV(dev, nondev) (nondev)
#endif #endif
/* Context which all wally allocations use (see common/setup.c) */
extern const tal_t *wally_tal_ctx;
#endif /* LIGHTNING_COMMON_UTILS_H */ #endif /* LIGHTNING_COMMON_UTILS_H */

Loading…
Cancel
Save