[RFC PATCH 7/8] util: Introduce virObjectPoolableHashElement

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

 



Add a new virObjectLockable child which will be used to describe
elements that would be inserted into the virObjectPoolableHashTable.

Each virObjectPoolableHashElement will have a primaryKey and a secondaryKey
which can be used to insert the same object into both of the hash tables
that are part of the virObjectPoolableHashTable.  This allows for lookup
of the object by more than one means.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/libvirt_private.syms |  2 ++
 src/util/virobject.c     | 77 ++++++++++++++++++++++++++++++++++++++++++++++--
 src/util/virobject.h     | 17 +++++++++++
 3 files changed, 94 insertions(+), 2 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 9b3345a..74d4bf8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2223,6 +2223,7 @@ virNumaSetupMemoryPolicy;
 # util/virobject.h
 virClassForObject;
 virClassForObjectLockable;
+virClassForObjectPoolableHashElement;
 virClassForObjectPoolableHashTable;
 virClassIsDerivedFrom;
 virClassName;
@@ -2235,6 +2236,7 @@ virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
 virObjectNew;
+virObjectPoolableHashElementNew;
 virObjectPoolableHashTableGetPrimary;
 virObjectPoolableHashTableGetSecondary;
 virObjectPoolableHashTableNew;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 625bf90..c9e631c 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -62,10 +62,11 @@ struct _virClass {
 static virClassPtr virObjectClass;
 static virClassPtr virObjectLockableClass;
 static virClassPtr virObjectPoolableHashTableClass;
+static virClassPtr virObjectPoolableHashElementClass;
 
 static void virObjectLockableDispose(void *anyobj);
 static void virObjectPoolableHashTableDispose(void *anyobj);
-
+static void virObjectPoolableHashElementDispose(void *anyobj);
 
 static int
 virObjectOnceInit(void)
@@ -89,6 +90,13 @@ virObjectOnceInit(void)
                       virObjectPoolableHashTableDispose)))
         return -1;
 
+    if (!(virObjectPoolableHashElementClass =
+          virClassNew(virObjectLockableClass,
+                      "virObjectPoolableHashElement",
+                      sizeof(virObjectPoolableHashElement),
+                      virObjectPoolableHashElementDispose)))
+        return -1;
+
     return 0;
 }
 
@@ -145,6 +153,23 @@ virClassForObjectPoolableHashTable(void)
 
 
 /**
+ * virClassForObjectPoolableHashElement:
+ *
+ * Returns the class instance for the virObjectPoolableHashElement type
+ */
+virClassPtr
+virClassForObjectPoolableHashElement(void)
+{
+    if (virObjectInitialize() < 0)
+        return NULL;
+
+    VIR_DEBUG("virObjectPoolableHashElementClass=%p",
+              virObjectPoolableHashElementClass);
+    return virObjectPoolableHashElementClass;
+}
+
+
+/**
  * virClassNew:
  * @parent: the parent class
  * @name: the class name
@@ -349,6 +374,53 @@ virObjectPoolableHashTableDispose(void *anyobj)
 }
 
 
+void *
+virObjectPoolableHashElementNew(virClassPtr klass,
+                                const char *primaryKey,
+                                const char *secondaryKey)
+{
+    virObjectPoolableHashElementPtr obj;
+
+    if (!virClassIsDerivedFrom(klass, virClassForObjectPoolableHashElement())) {
+        virReportInvalidArg(klass,
+                            _("Class %s must derive from "
+                              "virObjectPoolableHashElement"),
+                            virClassName(klass));
+        return NULL;
+    }
+
+    if (!(obj = virObjectLockableNew(klass)))
+        return NULL;
+
+    if (VIR_STRDUP(obj->primaryKey, primaryKey) < 0)
+        goto error;
+
+    if (secondaryKey && VIR_STRDUP(obj->secondaryKey, secondaryKey) < 0)
+        goto error;
+
+    VIR_DEBUG("obj=%p, primary=%s secondary=%s",
+              obj, obj->primaryKey, NULLSTR(obj->secondaryKey));
+
+    return obj;
+
+ error:
+    virObjectUnref(obj);
+    return NULL;
+
+}
+
+static void
+virObjectPoolableHashElementDispose(void *anyobj)
+{
+    virObjectPoolableHashElementPtr obj = anyobj;
+
+    VIR_DEBUG("dispose obj=%p", obj);
+
+    VIR_FREE(obj->primaryKey);
+    VIR_FREE(obj->secondaryKey);
+}
+
+
 /**
  * virObjectUnref:
  * @anyobj: any instance of virObjectPtr
@@ -417,7 +489,8 @@ static virObjectLockablePtr
 virObjectGetLockableObj(void *anyobj)
 {
     if (virObjectIsClass(anyobj, virObjectLockableClass) ||
-        virObjectIsClass(anyobj, virObjectPoolableHashTableClass))
+        virObjectIsClass(anyobj, virObjectPoolableHashTableClass) ||
+        virObjectIsClass(anyobj, virObjectPoolableHashElementClass))
         return anyobj;
 
     VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass);
diff --git a/src/util/virobject.h b/src/util/virobject.h
index ac21190..36fd842 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -38,6 +38,9 @@ typedef virObjectLockable *virObjectLockablePtr;
 typedef struct _virObjectPoolableHashTable virObjectPoolableHashTable;
 typedef virObjectPoolableHashTable *virObjectPoolableHashTablePtr;
 
+typedef struct _virObjectPoolableHashElement virObjectPoolableHashElement;
+typedef virObjectPoolableHashElement *virObjectPoolableHashElementPtr;
+
 typedef void (*virObjectDisposeCallback)(void *obj);
 
 /* Most code should not play with the contents of this struct; however,
@@ -96,9 +99,17 @@ struct _virObjectPoolableHashTable {
 };
 
 
+struct _virObjectPoolableHashElement {
+    virObjectLockable parent;
+
+    char *primaryKey;
+    char *secondaryKey;
+};
+
 virClassPtr virClassForObject(void);
 virClassPtr virClassForObjectLockable(void);
 virClassPtr virClassForObjectPoolableHashTable(void);
+virClassPtr virClassForObjectPoolableHashElement(void);
 
 # ifndef VIR_PARENT_REQUIRED
 #  define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1)
@@ -152,6 +163,12 @@ virObjectPoolableHashTableNew(virClassPtr klass,
                               bool primaryOnly)
     ATTRIBUTE_NONNULL(1);
 
+void *
+virObjectPoolableHashElementNew(virClassPtr klass,
+                                const char *primaryKey,
+                                const char *secondaryKey)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 void
 virObjectLock(void *lockableobj)
     ATTRIBUTE_NONNULL(1);
-- 
2.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux