util: alloc: Introduce VIR_AUTOUNREF macro

Add helper for utilizing __attribute__(cleanup())) for unref-ing
instances of sublasses of virObject.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Peter Krempa 2019-02-15 12:35:12 +01:00
parent 53a0fa7366
commit 0d13790695
4 changed files with 27 additions and 0 deletions

View File

@ -2511,6 +2511,7 @@ virClassForObjectRWLockable;
virClassIsDerivedFrom;
virClassName;
virClassNew;
virObjectAutoUnref;
virObjectFreeCallback;
virObjectFreeHashData;
virObjectIsClass;

View File

@ -637,4 +637,14 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
# define VIR_AUTOPTR(type) \
__attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *
/**
* VIR_AUTOUNREF:
* @type: type of an virObject subclass to be unref'd automatically
*
* Declares a variable of @type which will be automatically unref'd when
* control goes out of the scope.
*/
# define VIR_AUTOUNREF(type) \
__attribute__((cleanup(virObjectAutoUnref))) type
#endif /* LIBVIRT_VIRALLOC_H */

View File

@ -362,6 +362,19 @@ virObjectUnref(void *anyobj)
}
/**
* virObjectAutoUnref:
*
* Helper used by VIR_AUTOUNREF
*/
void
virObjectAutoUnref(void *objptr)
{
virObjectPtr *obj = objptr;
virObjectUnref(*obj);
}
/**
* virObjectRef:
* @anyobj: any instance of virObjectPtr

View File

@ -102,6 +102,9 @@ virObjectNew(virClassPtr klass)
bool
virObjectUnref(void *obj);
void
virObjectAutoUnref(void *objptr);
void *
virObjectRef(void *obj);