[libvirt] [RFC PATCH 1/2] Add virObject.

Hu Tao hutao at cn.fujitsu.com
Wed Mar 16 10:29:52 UTC 2011


virObject is the base struct that manages reference-counting
for all structs that need the ability of reference-counting.
---
 src/Makefile.am          |    1 +
 src/libvirt_private.syms |    5 ++++
 src/util/object.c        |   55 ++++++++++++++++++++++++++++++++++++++++++++++
 src/util/object.h        |   39 ++++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+), 0 deletions(-)
 create mode 100644 src/util/object.c
 create mode 100644 src/util/object.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 645119e..3ebabe9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -81,6 +81,7 @@ UTIL_SOURCES =							\
 		util/util.c util/util.h				\
 		util/xml.c util/xml.h				\
 		util/virtaudit.c util/virtaudit.h               \
+		util/object.c util/object.h			\
 		util/virterror.c util/virterror_internal.h
 
 EXTRA_DIST += util/threads-pthread.c util/threads-win32.c
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c0da78e..4f46eac 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -970,3 +970,8 @@ virXPathUInt;
 virXPathULong;
 virXPathULongHex;
 virXPathULongLong;
+
+# object.h
+virObjectInit;
+virObjectRef;
+virObjectUnref;
diff --git a/src/util/object.c b/src/util/object.c
new file mode 100644
index 0000000..31ea27b
--- /dev/null
+++ b/src/util/object.c
@@ -0,0 +1,55 @@
+/*
+ * object.c: base object that manages reference-counting
+ *
+ * Copyright (C) 2011 Hu Tao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Hu Tao <hutao at cn.fujitsu.com>
+ */
+
+#include <assert.h>
+
+#include "object.h"
+
+int virObjectInit(virObjectPtr obj, void (*free)(virObjectPtr obj))
+{
+    if (!free)
+        return -1;
+
+    obj->ref = 1;
+    obj->free = free;
+
+    return 0;
+}
+
+#define virAtomicInc(value) \
+    __sync_add_and_fetch(&value, 1)
+#define virAtomicDec(value) \
+    __sync_sub_and_fetch(&value, 1)
+
+void virObjectRef(virObjectPtr obj)
+{
+    assert(obj->ref > 0);
+    virAtomicInc(obj->ref);
+}
+
+void virObjectUnref(virObjectPtr obj)
+{
+    assert(obj->ref > 0);
+    if (virAtomicDec(obj->ref) == 0)
+        obj->free(obj);
+}
diff --git a/src/util/object.h b/src/util/object.h
new file mode 100644
index 0000000..f6eaea0
--- /dev/null
+++ b/src/util/object.h
@@ -0,0 +1,39 @@
+/*
+ * object.c: base object that manages reference-counting
+ *
+ * Copyright (C) 2011 Hu Tao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Hu Tao <hutao at cn.fujitsu.com>
+ */
+
+#ifndef __VIR_OBJECT_H
+#define __VIR_OBJECT_H
+
+typedef struct _virObject virObject;
+typedef virObject *virObjectPtr;
+
+struct _virObject {
+    int ref;
+    void (*free)(virObjectPtr obj);
+};
+
+int virObjectInit(virObjectPtr obj, void (*free)(virObjectPtr obj));
+void virObjectRef(virObjectPtr obj);
+void virObjectUnref(virObjectPtr obj);
+
+#endif /* __VIR_OBJECT_H */
-- 
1.7.3.1


-- 
Thanks,
Hu Tao




More information about the libvir-list mailing list