[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v3 03/16] util: Introduce virObjectLookupKeys



Add a new virObjectLockable child virObjectLookupKeys which can be used
by various driver/vir*object consumers as the means to describe the lookup
keys @uuid and/or @name.  Either or both keys may be defined/used and it's
left up to the object consumer to utilize the lookup keys as it sees fit.
Eventually, these will be the keys used to insert the object into the
hash table(s) for the driver/vir*object consumer.

Signed-off-by: John Ferlan <jferlan redhat com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virobject.c     | 78 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/util/virobject.h     | 17 +++++++++++
 3 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c1e9471..44d712d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2274,6 +2274,7 @@ virNumaSetupMemoryPolicy;
 # util/virobject.h
 virClassForObject;
 virClassForObjectLockable;
+virClassForObjectLookupKeys;
 virClassIsDerivedFrom;
 virClassName;
 virClassNew;
@@ -2284,6 +2285,7 @@ virObjectListFree;
 virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
+virObjectLookupKeysNew;
 virObjectNew;
 virObjectRef;
 virObjectUnlock;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 443718b..6908e8d 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -66,8 +66,10 @@ struct _virClass {
 
 static virClassPtr virObjectClass;
 static virClassPtr virObjectLockableClass;
+static virClassPtr virObjectLookupKeysClass;
 
 static void virObjectLockableDispose(void *anyobj);
+static void virObjectLookupKeysDispose(void *anyobj);
 
 static int
 virObjectOnceInit(void)
@@ -84,6 +86,12 @@ virObjectOnceInit(void)
                                                virObjectLockableDispose)))
         return -1;
 
+    if (!(virObjectLookupKeysClass = virClassNew(virObjectLockableClass,
+                                                 "virObjectLookupKeys",
+                                                 sizeof(virObjectLookupKeys),
+                                                 virObjectLookupKeysDispose)))
+        return -1;
+
     return 0;
 }
 
@@ -121,6 +129,21 @@ virClassForObjectLockable(void)
 
 
 /**
+ * virClassForObjectLookupKeys:
+ *
+ * Returns the class instance for the virObjectLookupKeys type
+ */
+virClassPtr
+virClassForObjectLookupKeys(void)
+{
+    if (virObjectInitialize() < 0)
+        return NULL;
+
+    return virObjectLookupKeysClass;
+}
+
+
+/**
  * virClassNew:
  * @parent: the parent class
  * @name: the class name
@@ -268,6 +291,58 @@ virObjectLockableDispose(void *anyobj)
 }
 
 
+void *
+virObjectLookupKeysNew(virClassPtr klass,
+                       const char *uuid,
+                       const char *name)
+{
+    virObjectLookupKeysPtr obj;
+
+    if (!virClassIsDerivedFrom(klass, virClassForObjectLookupKeys())) {
+        virReportInvalidArg(klass,
+                            _("Class %s must derive from virObjectLookupKeys"),
+                            virClassName(klass));
+        return NULL;
+    }
+
+    if (!uuid && !name) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("no key, either 'uuid' or 'name' must be defined"));
+        return NULL;
+    }
+
+    if (!(obj = virObjectLockableNew(klass)))
+        return NULL;
+
+    if (VIR_STRDUP(obj->uuid, uuid) < 0)
+        goto error;
+
+    if (VIR_STRDUP(obj->name, name) < 0)
+        goto error;
+
+    VIR_DEBUG("obj=%p, uuid=%s name=%s",
+              obj, NULLSTR(obj->uuid), NULLSTR(obj->name));
+
+    return obj;
+
+ error:
+    virObjectUnref(obj);
+    return NULL;
+}
+
+
+static void
+virObjectLookupKeysDispose(void *anyobj)
+{
+    virObjectLookupKeysPtr obj = anyobj;
+
+    VIR_DEBUG("dispose obj=%p", obj);
+
+    VIR_FREE(obj->uuid);
+    VIR_FREE(obj->name);
+}
+
+
 /**
  * virObjectUnref:
  * @anyobj: any instance of virObjectPtr
@@ -334,7 +409,8 @@ virObjectRef(void *anyobj)
 static virObjectLockablePtr
 virObjectGetLockableObj(void *anyobj)
 {
-    if (virObjectIsClass(anyobj, virObjectLockableClass))
+    if (virObjectIsClass(anyobj, virObjectLockableClass) ||
+        virObjectIsClass(anyobj, virObjectLookupKeysClass))
         return anyobj;
 
     VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass);
diff --git a/src/util/virobject.h b/src/util/virobject.h
index f4c292b..1cade9d 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -34,6 +34,9 @@ typedef virObject *virObjectPtr;
 typedef struct _virObjectLockable virObjectLockable;
 typedef virObjectLockable *virObjectLockablePtr;
 
+typedef struct _virObjectLookupKeys virObjectLookupKeys;
+typedef virObjectLookupKeys *virObjectLookupKeysPtr;
+
 typedef void (*virObjectDisposeCallback)(void *obj);
 
 /* Most code should not play with the contents of this struct; however,
@@ -59,9 +62,17 @@ struct _virObjectLockable {
     virMutex lock;
 };
 
+struct _virObjectLookupKeys {
+    virObjectLockable parent;
+
+    char *uuid;
+    char *name;
+};
+
 
 virClassPtr virClassForObject(void);
 virClassPtr virClassForObjectLockable(void);
+virClassPtr virClassForObjectLookupKeys(void);
 
 # ifndef VIR_PARENT_REQUIRED
 #  define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1)
@@ -108,6 +119,12 @@ void *
 virObjectLockableNew(virClassPtr klass)
     ATTRIBUTE_NONNULL(1);
 
+void *
+virObjectLookupKeysNew(virClassPtr klass,
+                       const char *uuid,
+                       const char *name)
+    ATTRIBUTE_NONNULL(1);
+
 void
 virObjectLock(void *lockableobj)
     ATTRIBUTE_NONNULL(1);
-- 
2.9.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]