Browse Source

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>
travis-debug
Rusty Russell 5 years ago
committed by neil saitug
parent
commit
d5eca470dc
  1. 85
      common/test/run-softref.c
  2. 72
      common/utils.c
  3. 14
      common/utils.h

85
common/test/run-softref.c

@ -0,0 +1,85 @@
#include <assert.h>
#include <ccan/short_types/short_types.h>
#include <stdio.h>
#include <wire/wire.h>
/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_asset_is_main */
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); }
/* Generated stub for amount_asset_to_sat */
struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED)
{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); }
/* Generated stub for amount_sat_add */
bool amount_sat_add(struct amount_sat *val UNNEEDED,
struct amount_sat a UNNEEDED,
struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_add called!\n"); abort(); }
/* Generated stub for amount_sat_eq */
bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
/* Generated stub for amount_sat_sub */
bool amount_sat_sub(struct amount_sat *val UNNEEDED,
struct amount_sat a UNNEEDED,
struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); }
/* Generated stub for fromwire_fail */
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
struct objtype {
char *c_softref, *c_softref2;
};
int main(void)
{
const void *ctx = tal(NULL, char);
struct objtype *o;
char *c;
setup_locale();
/* Simple test: freeing obj NULLs softref */
o = tal(ctx, struct objtype);
c = tal(ctx, char);
set_softref(o, &o->c_softref, c);
assert(o->c_softref == c);
tal_free(c);
assert(o->c_softref == NULL);\
/* Duplicate ptrs work */
o = tal(ctx, struct objtype);
c = tal(ctx, char);
set_softref(o, &o->c_softref, c);
assert(o->c_softref == c);
set_softref(o, &o->c_softref2, c);
assert(o->c_softref2 == c);
tal_free(c);
assert(o->c_softref == NULL);
assert(o->c_softref2 == NULL);
/* Cleans up properly if o is freed first. */
c = tal(ctx, char);
set_softref(o, &o->c_softref, c);
tal_free(o);
tal_free(c);
/* Setting to NULL works. */
o = tal(ctx, struct objtype);
c = tal(ctx, char);
set_softref(o, &o->c_softref, c);
assert(o->c_softref == c);
clear_softref(o, &o->c_softref);
assert(o->c_softref == NULL);
/* Now it's not a softref, won't clear! */
o->c_softref = c;
tal_free(c);
assert(o->c_softref == c);
tal_free(o);
tal_free(ctx);
}

72
common/utils.c

@ -15,6 +15,78 @@ bool is_elements(const struct chainparams *chainparams)
return chainparams->is_elements; return chainparams->is_elements;
} }
#if DEVELOPER
/* 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)
{
abort();
}
#endif
static void softref_nullify(tal_t *obj, void **ptr)
{
*ptr = NULL;
#if DEVELOPER
tal_del_notifier(obj, dont_move_softref);
#endif
}
static void softref_cleanup(const tal_t *outer, void **ptr)
{
if (*ptr) {
tal_del_destructor2(*ptr, softref_nullify, ptr);
}
#if DEVELOPER
tal_del_notifier(outer, dont_move_softref);
#endif
}
void set_softref_(const tal_t *outer, size_t outersize, void **ptr, tal_t *obj)
{
/* pointer is inside outer, right? */
assert((char *)ptr >= (char *)outer);
assert((char *)ptr < (char *)outer + outersize);
/* This is harmless if there was no prior, otherwise constrains the
* leak: we don't have enough information in softref_nullify to
* clear softref_cleanup */
tal_del_destructor2(outer, softref_cleanup, ptr);
if (obj) {
tal_add_destructor2(outer, softref_cleanup, ptr);
tal_add_destructor2(obj, softref_nullify, ptr);
#if DEVELOPER
tal_add_notifier(obj, TAL_NOTIFY_MOVE, dont_move_softref);
#endif
}
#if DEVELOPER
tal_add_notifier(outer, TAL_NOTIFY_MOVE, dont_move_softref);
#endif
*ptr = obj;
}
void clear_softref_(const tal_t *outer, size_t outersize, void **ptr)
{
assert((char *)ptr >= (char *)outer);
assert((char *)ptr < (char *)outer + outersize);
if (*ptr) {
tal_del_destructor2(outer, softref_cleanup, ptr);
tal_del_destructor2(*ptr, softref_nullify, ptr);
#if DEVELOPER
tal_del_notifier(*ptr, dont_move_softref);
#endif
}
#if DEVELOPER
tal_del_notifier(outer, dont_move_softref);
#endif
*ptr = NULL;
}
char *tal_hexstr(const tal_t *ctx, const void *data, size_t len) char *tal_hexstr(const tal_t *ctx, const void *data, size_t len)
{ {
char *str = tal_arr(ctx, char, hex_str_size(len)); char *str = tal_arr(ctx, char, hex_str_size(len));

14
common/utils.h

@ -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. */ /* 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); 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! */ /* Note: p is never a complex expression, otherwise this multi-evaluates! */
#define tal_arr_expand(p, s) \ #define tal_arr_expand(p, s) \
do { \ do { \

Loading…
Cancel
Save