From db7c608e2dde5275e68f8cb540541237806cd9cc Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 6 Jul 2023 17:06:50 +0930 Subject: [PATCH] common/utils: remove now-unused softref facility. Signed-off-by: Rusty Russell --- common/test/run-softref.c | 161 -------------------------------------- common/utils.c | 72 ----------------- common/utils.h | 14 ---- 3 files changed, 247 deletions(-) delete mode 100644 common/test/run-softref.c diff --git a/common/test/run-softref.c b/common/test/run-softref.c deleted file mode 100644 index e4294219b..000000000 --- a/common/test/run-softref.c +++ /dev/null @@ -1,161 +0,0 @@ -#include "config.h" -#include -#include -#include -#include -#include -#include - -/* 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 */ -struct amount_sat amount_sat(u64 satoshis UNNEEDED) -{ fprintf(stderr, "amount_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_div */ -struct amount_sat amount_sat_div(struct amount_sat sat UNNEEDED, u64 div UNNEEDED) -{ fprintf(stderr, "amount_sat_div 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_greater_eq */ -bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) -{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); } -/* Generated stub for amount_sat_mul */ -bool amount_sat_mul(struct amount_sat *res UNNEEDED, struct amount_sat sat UNNEEDED, u64 mul UNNEEDED) -{ fprintf(stderr, "amount_sat_mul 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 amount_sat_to_asset */ -struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED) -{ fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); } -/* Generated stub for amount_tx_fee */ -struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) -{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); } -/* Generated stub for fromwire */ -const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) -{ fprintf(stderr, "fromwire called!\n"); abort(); } -/* Generated stub for fromwire_bool */ -bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_bool called!\n"); abort(); } -/* Generated stub for fromwire_fail */ -void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_fail called!\n"); abort(); } -/* Generated stub for fromwire_secp256k1_ecdsa_signature */ -void fromwire_secp256k1_ecdsa_signature(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, - secp256k1_ecdsa_signature *signature UNNEEDED) -{ fprintf(stderr, "fromwire_secp256k1_ecdsa_signature called!\n"); abort(); } -/* Generated stub for fromwire_sha256 */ -void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED) -{ fprintf(stderr, "fromwire_sha256 called!\n"); abort(); } -/* Generated stub for fromwire_tal_arrn */ -u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED, - const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED) -{ fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); } -/* Generated stub for fromwire_u32 */ -u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); } -/* Generated stub for fromwire_u64 */ -u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); } -/* Generated stub for fromwire_u8 */ -u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) -{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); } -/* Generated stub for fromwire_u8_array */ -void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED) -{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); } -/* Generated stub for towire */ -void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) -{ fprintf(stderr, "towire called!\n"); abort(); } -/* Generated stub for towire_bool */ -void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED) -{ fprintf(stderr, "towire_bool called!\n"); abort(); } -/* Generated stub for towire_secp256k1_ecdsa_signature */ -void towire_secp256k1_ecdsa_signature(u8 **pptr UNNEEDED, - const secp256k1_ecdsa_signature *signature UNNEEDED) -{ fprintf(stderr, "towire_secp256k1_ecdsa_signature called!\n"); abort(); } -/* Generated stub for towire_sha256 */ -void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED) -{ fprintf(stderr, "towire_sha256 called!\n"); abort(); } -/* Generated stub for towire_u32 */ -void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) -{ fprintf(stderr, "towire_u32 called!\n"); abort(); } -/* Generated stub for towire_u64 */ -void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED) -{ fprintf(stderr, "towire_u64 called!\n"); abort(); } -/* Generated stub for towire_u8 */ -void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) -{ fprintf(stderr, "towire_u8 called!\n"); abort(); } -/* Generated stub for towire_u8_array */ -void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED) -{ fprintf(stderr, "towire_u8_array called!\n"); abort(); } -/* AUTOGENERATED MOCKS END */ - -struct objtype { - char *c_softref, *c_softref2; -}; - -int main(int argc, char *argv[]) -{ - const void *ctx = tal(NULL, char); - struct objtype *o; - char *c; - - common_setup(argv[0]); - - /* 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); - common_shutdown(); -} diff --git a/common/utils.c b/common/utils.c index c3c5b0fb9..c264b6924 100644 --- a/common/utils.c +++ b/common/utils.c @@ -57,78 +57,6 @@ void tal_wally_end_onto_(const tal_t *parent, tal_wally_end(tal_steal(parent, from_wally)); } -#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 *str = tal_arr(ctx, char, hex_str_size(len)); diff --git a/common/utils.h b/common/utils.h index abf3ca684..8f5cc4cb3 100644 --- a/common/utils.h +++ b/common/utils.h @@ -54,20 +54,6 @@ 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 { \