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.
This commit is contained in:
Thomas Haller 2020-07-07 22:14:27 +02:00
parent 1d6e208c12
commit 6feab75447
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -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__ */