From 6feab75447468a90b3cbb43df0fce45595a691ac Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 7 Jul 2020 22:14:27 +0200 Subject: shared: add nm_steal_pointer()/nm_clear_free() to "nm-std-aux.h" There is the team of nm_auto*, nm_clear_pointer() and nm_steal_pointer(). These goes nicely together so that an autovariable owns a resource, to free it (clear) and to transfer ownership (steal). We have these also in glib, but we certainly also need it if we don't have glib because that very much goes together with nm_auto*. Add it. --- shared/nm-std-aux/nm-std-aux.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/shared/nm-std-aux/nm-std-aux.h b/shared/nm-std-aux/nm-std-aux.h index 026fafdcc7..2351198930 100644 --- a/shared/nm-std-aux/nm-std-aux.h +++ b/shared/nm-std-aux/nm-std-aux.h @@ -520,4 +520,22 @@ NM_AUTO_DEFINE_FCN_VOID0 (void *, _nm_auto_free_impl, free) _changed; \ }) +#define nm_clear_free(pp) nm_clear_pointer (pp, free) + +/*****************************************************************************/ + +static inline void * +_nm_steal_pointer (void *pp) +{ + void **ptr = (void **) pp; + void *ref; + + ref = *ptr; + *ptr = NULL; + return ref; +} + +#define nm_steal_pointer(pp) \ + ((typeof (*(pp))) _nm_steal_pointer (pp)) + #endif /* __NM_STD_AUX_H__ */ -- cgit v1.2.1