[libvirt] [PATCH 02/15] util: Add virabstracts file for keeping abstract classes

Martin Kletzander mkletzan at redhat.com
Thu Apr 16 14:46:37 UTC 2015


The first class in this file is going to be an abstract connection class
that holds a per-connection error inside.  virConnect will be the first
child class inheriting from this one.

This is a separate file because virerror.c is going to depend on it and
putting it into datatypes along with other connect classes would create
a dependency on datatypes from the util library.

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/Makefile.am          |   3 +-
 src/libvirt_private.syms |   5 +++
 src/util/virabstracts.c  | 100 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virabstracts.h  |  57 +++++++++++++++++++++++++++
 4 files changed, 164 insertions(+), 1 deletion(-)
 create mode 100644 src/util/virabstracts.c
 create mode 100644 src/util/virabstracts.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 3c9eac6..5763659 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in

-## Copyright (C) 2005-2014 Red Hat, Inc.
+## Copyright (C) 2005-2015 Red Hat, Inc.
 ##
 ## This library is free software; you can redistribute it and/or
 ## modify it under the terms of the GNU Lesser General Public
@@ -89,6 +89,7 @@ augeastest_DATA =
 # These files are not related to driver APIs. Simply generic
 # helper APIs for various purposes
 UTIL_SOURCES =							\
+		util/virabstracts.c util/virabstracts.h		\
 		util/viralloc.c util/viralloc.h			\
 		util/virarch.h util/virarch.c			\
 		util/viratomic.h util/viratomic.c		\
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 0335313..ef5877b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1035,6 +1035,11 @@ virSecurityManagerStackAddNested;
 virSecurityManagerVerify;


+# util/virabstracts.h
+virClassForAbstractConnect;
+virGetAbstractConnect;
+
+
 # util/viralloc.h
 virAlloc;
 virAllocN;
diff --git a/src/util/virabstracts.c b/src/util/virabstracts.c
new file mode 100644
index 0000000..f4c0829
--- /dev/null
+++ b/src/util/virabstracts.c
@@ -0,0 +1,100 @@
+/*
+ * virabstracts.c: abstract virClass definitions
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Martin Kletzander <mkletzan at redhat.com>
+ */
+
+#include <config.h>
+
+#include "virabstracts.h"
+#include "virerror.h"
+#include "virlog.h"
+#include "viralloc.h"
+#include "viruuid.h"
+#include "virstring.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util");
+
+virClassPtr virAbstractConnectClass;
+
+static void virAbstractConnectDispose(void *anyobj);
+
+
+static int
+virAbstractsOnceInit(void)
+{
+#define DECLARE_CLASS(basename, parent)                          \
+    if (!(basename ## Class = virClassNew(parent,                \
+                                          #basename,             \
+                                          sizeof(basename),      \
+                                          basename ## Dispose))) \
+        return -1;
+
+
+    DECLARE_CLASS(virAbstractConnect, virClassForObjectLockable());
+
+
+#undef DECLARE_CLASS
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virAbstracts)
+
+
+virClassPtr
+virClassForAbstractConnect(void)
+{
+    if (virAbstractsInitialize() < 0)
+        return NULL;
+
+    return virAbstractConnectClass;
+}
+
+
+/**
+ * virGetAbstractConnect:
+ *
+ * Allocates a new abstract connection object.
+ *
+ * Returns a pointer to the connection object, or NULL on error.
+ */
+void *
+virGetAbstractConnect(virClassPtr klass)
+{
+    virAbstractConnectPtr ret;
+
+    if (virAbstractsInitialize() < 0)
+        return NULL;
+
+    if (!(ret = virObjectLockableNew(klass)))
+        return NULL;
+
+    return ret;
+}
+
+
+static void
+virAbstractConnectDispose(void *anyobj)
+{
+    virAbstractConnectPtr obj = anyobj;
+
+    virResetError(&obj->err);
+}
diff --git a/src/util/virabstracts.h b/src/util/virabstracts.h
new file mode 100644
index 0000000..4d12862
--- /dev/null
+++ b/src/util/virabstracts.h
@@ -0,0 +1,57 @@
+/*
+ * virabstracts.h: abstract virClass definitions
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Martin Kletzander <mkletzan at redhat.com>
+ */
+
+#ifndef __VIR_ABSTRACTS_H_
+# define __VIR_ABSTRACTS_H_
+
+# include "internal.h"
+# include "virerror.h"
+# include "virobject.h"
+
+virClassPtr virClassForAbstractConnect(void);
+
+void *virGetAbstractConnect(virClassPtr klass);
+
+typedef struct _virAbstractConnect virAbstractConnect;
+typedef virAbstractConnect *virAbstractConnectPtr;
+
+/**
+ * _virAbstractConnect:
+ *
+ * Internal structure associated to a connection
+ */
+struct _virAbstractConnect {
+    virObjectLockable parent;
+
+    /*
+     * Object lock must be acquired before accessing/changing any of
+     * members following this point, or changing the ref count of any
+     * virDomain/virNetwork object associated with this connection.
+     */
+
+    /* Per-connection error. */
+    virError err;           /* the last error */
+    virErrorFunc handler;   /* associated handlet */
+    void *userData;         /* the user data */
+};
+
+#endif /* __VIR_ABSTRACTS_H__ */
-- 
2.3.5




More information about the libvir-list mailing list