utils: add a global tmpctx.

I did a brief audit of tmpctx uses, and we do leak them in various
corner cases.  Fortunely, all our daemons are based on some kind of
I/O loop, so it's fairly easy to clean a global tmpctx at that point.

This makes things a bit neater, and slightly more efficient, but also
clearer: I avoided creating a tmpctx in a few places because I didn't
want to add another allocation.  With that penalty removed, I can use
it more freely and hopefully write clearer code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-03-15 15:00:37 +10:30
parent 41ef42ee94
commit ef2a063169
10 changed files with 38 additions and 40 deletions

View File

@@ -16,12 +16,16 @@ 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_tmpctx_((ctx), __FILE__, __LINE__)
/* FIXME: Remove in favor of global */
#define tal_tmpctx(ctx) tal((ctx), char)
/* Return non-NULL if any tmpctx still allocated. */
const char *tmpctx_any(void);
/* Global temporary convenience context: freed in io loop core. */
extern const tal_t *tmpctx;
/* Initial creation of tmpctx. */
void setup_tmpctx(void);
/* Free any children of tmpctx. */
void clean_tmpctx(void);
#endif /* LIGHTNING_COMMON_UTILS_H */