tal_tmpctx: keep information around so we can find leaks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-09-28 13:10:57 +09:30
parent 7200002773
commit e587ec3bd3
2 changed files with 40 additions and 2 deletions

View File

@ -1,5 +1,7 @@
#include "utils.h"
#include <ccan/list/list.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
secp256k1_context *secp256k1_ctx;
@ -22,3 +24,35 @@ u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len)
return NULL;
return data;
}
struct tmpctx {
struct list_node list;
const char *file;
unsigned int line;
};
static struct list_head tmpctxs = LIST_HEAD_INIT(tmpctxs);
static void destroy_tmpctx(struct tmpctx *t)
{
list_del_from(&tmpctxs, &t->list);
}
tal_t *tal_tmpctx_(const tal_t *ctx, const char *file, unsigned int line)
{
struct tmpctx *t = tal(ctx, struct tmpctx);
t->file = file;
t->line = line;
list_add_tail(&tmpctxs, &t->list);
tal_add_destructor(t, destroy_tmpctx);
return t;
}
const char *tmpctx_any(void)
{
struct tmpctx *t = list_top(&tmpctxs, struct tmpctx, list);
if (t)
return tal_fmt(t, "%s:%u", t->file, t->line);
return NULL;
}

View File

@ -16,8 +16,12 @@ char *tal_hex(const tal_t *ctx, const tal_t *data);
/* Allocate and fill a buffer with the data of this hex string. */
u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len);
/* Get a temporary context for this function scope (tal_free at end) */
tal_t *tal_tmpctx_(const tal_t *ctx, const char *file, unsigned int line);
#define tal_tmpctx(ctx) \
tal_alloc_((ctx), 0, false, false, \
__FILE__ ":" stringify(__LINE__) ":tal_tmpctx")
tal_tmpctx_((ctx), __FILE__, __LINE__)
/* Return non-NULL if any tmpctx still allocated. */
const char *tmpctx_any(void);
#endif /* LIGHTNING_COMMON_UTILS_H */