aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2012-07-11 14:35:44 +0100
committerDaniel P. Berrange <berrange@redhat.com>2012-08-07 11:47:41 +0100
commit784a99f794ac7e9cf3577e036e5067c7cfc46e09 (patch)
treed05fe11bc0e72e7899019c3a067c3677005e26fd /src/Makefile.am
parentapparmor: QEMU bridge helper policy updates (diff)
downloadlibvirt-784a99f794ac7e9cf3577e036e5067c7cfc46e09.tar.gz
libvirt-784a99f794ac7e9cf3577e036e5067c7cfc46e09.tar.bz2
libvirt-784a99f794ac7e9cf3577e036e5067c7cfc46e09.zip
Add a generic reference counted virObject type
This introduces a fairly basic reference counted virObject type and an associated virClass type, that use atomic operations for ref counting. In a global initializer (recommended to be invoked using the virOnceInit API), a virClass type must be allocated for each object type. This requires a class name, a "dispose" callback which will be invoked to free memory associated with the object's fields, and the size in bytes of the object struct. eg, virClassPtr connclass = virClassNew("virConnect", sizeof(virConnect), virConnectDispose); The struct for the object, must include 'virObject' as its first member eg struct _virConnect { virObject object; virURIPtr uri; }; The 'dispose' callback is only responsible for freeing fields in the object, not the object itself. eg a suitable impl for the above struct would be void virConnectDispose(void *obj) { virConnectPtr conn = obj; virURIFree(conn->uri); } There is no need to reset fields to 'NULL' or '0' in the dispose callback, since the entire object will be memset to 0, and the klass pointer & magic integer fields will be poisoned with 0xDEADBEEF before being free()d When creating an instance of an object, one needs simply pass the virClassPtr eg virConnectPtr conn = virObjectNew(connclass); if (!conn) return NULL; conn->uri = virURIParse("foo:///bar") Object references can be manipulated with virObjectRef(conn) virObjectUnref(conn) The latter returns a true value, if the object has been freed (ie its ref count hit zero) Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'src/Makefile.am')
-rw-r--r--src/Makefile.am1
1 files changed, 1 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 49bcf5069..6ed4a41f3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -85,6 +85,7 @@ UTIL_SOURCES = \
util/virauthconfig.c util/virauthconfig.h \
util/virfile.c util/virfile.h \
util/virnodesuspend.c util/virnodesuspend.h \
+ util/virobject.c util/virobject.h \
util/virpidfile.c util/virpidfile.h \
util/virtypedparam.c util/virtypedparam.h \
util/xml.c util/xml.h \