[RFC PATCH 6/8] util: Add the hash tables to virObjectPoolableHashTable

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

 



Add a pair of hash tables to the object - a primary and a secondary one.
If the consumer requested only a primary, then only it will be created.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6861155..9b3345a 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2235,6 +2235,8 @@ virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
 virObjectNew;
+virObjectPoolableHashTableGetPrimary;
+virObjectPoolableHashTableGetSecondary;
 virObjectPoolableHashTableNew;
 virObjectRef;
 virObjectUnlock;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index d284dde..625bf90 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -295,7 +295,9 @@ virObjectLockableDispose(void *anyobj)
 
 void *
 virObjectPoolableHashTableNew(virClassPtr klass,
-                              virObjectPoolableHashTableObjType objtype)
+                              virObjectPoolableHashTableObjType objtype,
+                              int tableElemsStart,
+                              bool primaryOnly)
 {
     virObjectPoolableHashTablePtr obj;
 
@@ -311,20 +313,39 @@ virObjectPoolableHashTableNew(virClassPtr klass,
         return NULL;
 
     obj->objtype = objtype;
+    obj->tableElemsStart = tableElemsStart;
 
-    VIR_DEBUG("poolable new obj=%p, type=%d",
-              obj, objtype);
+    if (!(obj->objsPrimary = virHashCreate(tableElemsStart,
+                                           virObjectFreeHashData)))
+        goto error;
+
+    if (!primaryOnly &&
+        !(obj->objsSecondary = virHashCreate(tableElemsStart,
+                                             virObjectFreeHashData)))
+        goto error;
+
+
+    VIR_DEBUG("obj=%p, type=%d, elems=%d objsPrimary=%p objsSecondary=%p",
+              obj, objtype, tableElemsStart,
+              obj->objsPrimary, obj->objsSecondary);
 
     return obj;
 
+ error:
+    virObjectUnref(obj);
+    return NULL;
 }
 
+
 static void
 virObjectPoolableHashTableDispose(void *anyobj)
 {
     virObjectPoolableHashTablePtr obj = anyobj;
 
     VIR_DEBUG("poolable dispose obj=%p", obj);
+
+    virHashFree(obj->objsPrimary);
+    virHashFree(obj->objsSecondary);
 }
 
 
@@ -402,7 +423,6 @@ virObjectGetLockableObj(void *anyobj)
     VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass);
 
     return NULL;
-
 }
 
 
@@ -557,3 +577,53 @@ virObjectListFreeCount(void *list,
 
     VIR_FREE(list);
 }
+
+
+static virObjectPoolableHashTablePtr
+virObjectGetPoolableHashTableObj(void *anyobj)
+{
+    if (virObjectIsClass(anyobj, virObjectPoolableHashTableClass))
+        return anyobj;
+
+    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectPoolableHashTableClass);
+
+    return NULL;
+}
+
+
+/**
+ * virObjectPoolableHashTableGetPrimary
+ * @anyobj: Pointer to a PoolableHashTable object
+ *
+ * Returns: Pointer to the Primary Hash Table or NULL on failure
+ */
+virHashTablePtr
+virObjectPoolableHashTableGetPrimary(void *anyobj)
+{
+    virObjectPoolableHashTablePtr obj =
+        virObjectGetPoolableHashTableObj(anyobj);
+
+    if (!obj)
+        return NULL;
+
+    return obj->objsPrimary;
+}
+
+
+/**
+ * virObjectPoolableHashTableGetSecondary
+ * @anyobj: Pointer to a PoolableHashTable object
+ *
+ * Returns: Pointer to the Secondary Hash Table or NULL on failure
+ */
+virHashTablePtr
+virObjectPoolableHashTableGetSecondary(void *anyobj)
+{
+    virObjectPoolableHashTablePtr obj =
+        virObjectGetPoolableHashTableObj(anyobj);
+
+    if (!obj)
+        return NULL;
+
+    return obj->objsSecondary;
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 30ce6a1..ac21190 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -81,6 +81,18 @@ typedef enum {
 struct _virObjectPoolableHashTable {
     virObjectLockable parent;
     virObjectPoolableHashTableObjType objtype;
+
+    int tableElemsStart;
+
+    /* primary key -> object mapping for O(1),
+     * lockless lookup-by-primary */
+    virHashTable *objsPrimary;
+
+    /* uuid string -> virPoolObj  mapping
+     * for O(1), lockless lookup-by-uuid */
+    /* secondary key -> object mapping for O(1),
+     * lockless lookup-by-secondary */
+    virHashTable *objsSecondary;
 };
 
 
@@ -135,7 +147,9 @@ virObjectLockableNew(virClassPtr klass)
 
 void *
 virObjectPoolableHashTableNew(virClassPtr klass,
-                              virObjectPoolableHashTableObjType objtype)
+                              virObjectPoolableHashTableObjType objtype,
+                              int tableElemsStart,
+                              bool primaryOnly)
     ATTRIBUTE_NONNULL(1);
 
 void
@@ -153,4 +167,10 @@ void
 virObjectListFreeCount(void *list,
                        size_t count);
 
+virHashTablePtr
+virObjectPoolableHashTableGetPrimary(void *anyobj);
+
+virHashTablePtr
+virObjectPoolableHashTableGetSecondary(void *anyobj);
+
 #endif /* __VIR_OBJECT_H */
-- 
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