]> xenbits.xensource.com Git - libvirt.git/commit
Add a generic reference counted virObject type
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 11 Jul 2012 13:35:44 +0000 (14:35 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 7 Aug 2012 10:47:41 +0000 (11:47 +0100)
commit784a99f794ac7e9cf3577e036e5067c7cfc46e09
treed05fe11bc0e72e7899019c3a067c3677005e26fd
parentb0e478986f8b696cc347ed963a6181a56c80f4cd
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>
cfg.mk
src/Makefile.am
src/libvirt_private.syms
src/libvirt_probes.d
src/util/virobject.c [new file with mode: 0644]
src/util/virobject.h [new file with mode: 0644]