utils: add set_softref() / clear_softref().

We often want a pointer which will turn to NULL if the pointed-to thing is
freed.  This is possible with tal objects, so create it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-10-08 11:42:24 +10:30
committed by neil saitug
parent 296868daf4
commit d5eca470dc
3 changed files with 171 additions and 0 deletions

View File

@@ -25,6 +25,20 @@ 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);
/* Macro to set memberptr in tal object outer to point to tal object obj,
* if it isn't NULL.
* The 0*sizeof() checks that *memberptr = obj is valid */
#define set_softref(outer, memberptr, obj) \
set_softref_((outer), sizeof(*(outer)) + 0*sizeof(*(memberptr) = obj), \
(void **)(memberptr), (obj))
/* Macro to clear a (set) softref ptr to NULL */
#define clear_softref(outer, memberptr) \
clear_softref_((outer), sizeof(*(outer)), (void **)(memberptr))
void set_softref_(const tal_t *outer, size_t outersize, void **ptr, tal_t *obj);
void clear_softref_(const tal_t *outer, size_t outersize, void **ptr);
/* Note: p is never a complex expression, otherwise this multi-evaluates! */
#define tal_arr_expand(p, s) \
do { \