nref: clear pointer if refcount reached 0

This commit is contained in:
anna 2021-07-16 14:12:18 +02:00
parent 7169a434bf
commit a1ac5d8c41
Signed by: fef
GPG key ID: EC22E476DC2D3D84

View file

@ -40,12 +40,15 @@ int _neo_nput(struct _neo_nref *ref);
* Decrement the reference counter of a structure embedding `NREF_FIELD`.
*
* If the counter reaches zero, the destroy callback passed to `nref_init`
* is invoked.
* is invoked and the pointer is set to `nil` to prevent any further usage.
*
* @param ptr: The `struct *` to decrement the reference counter of
* @returns The new reference count
*/
#define nput(ptr) (_neo_nput( &(ptr)->__neo_nref ))
#define nput(ptr) ({ \
if (_neo_nput(&(ptr)->__neo_nref) == 0) \
ptr = nil; \
})
/*
* This file is part of libneo.