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

[libvirt] [PATCH v2 5/8] util: Introduce virObjectPoolableHashElement



Add a new virObjectLockable child which will be used to more generically
describe driver objects. Eventually these objects will be placed into a
more generic hash table object which will take care of object mgmt functions.

Each virObjectPoolableHashElement will have a primaryKey (required) and a
secondaryKey (optional) which can be used to insert the same object into
two hash tables for faster lookups.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 429b095..a400d8f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2258,6 +2258,7 @@ virNumaSetupMemoryPolicy;
 # util/virobject.h
 virClassForObject;
 virClassForObjectLockable;
+virClassForObjectPoolableHashElement;
 virClassIsDerivedFrom;
 virClassName;
 virClassNew;
@@ -2269,6 +2270,7 @@ virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
 virObjectNew;
+virObjectPoolableHashElementNew;
 virObjectRef;
 virObjectUnlock;
 virObjectUnref;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index e0465b1..302b7ac 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -62,8 +62,10 @@ struct _virClass {
 
 static virClassPtr virObjectClass;
 static virClassPtr virObjectLockableClass;
+static virClassPtr virObjectPoolableHashElementClass;
 
 static void virObjectLockableDispose(void *anyobj);
+static void virObjectPoolableHashElementDispose(void *anyobj);
 
 static int
 virObjectOnceInit(void)
@@ -80,6 +82,13 @@ virObjectOnceInit(void)
                                                virObjectLockableDispose)))
         return -1;
 
+    if (!(virObjectPoolableHashElementClass =
+          virClassNew(virObjectLockableClass,
+                      "virObjectPoolableHashElement",
+                      sizeof(virObjectPoolableHashElement),
+                      virObjectPoolableHashElementDispose)))
+        return -1;
+
     return 0;
 }
 
@@ -117,6 +126,23 @@ virClassForObjectLockable(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
@@ -260,6 +286,53 @@ virObjectLockableDispose(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
@@ -326,7 +399,8 @@ virObjectRef(void *anyobj)
 static virObjectLockablePtr
 virObjectGetLockableObj(void *anyobj)
 {
-    if (virObjectIsClass(anyobj, virObjectLockableClass))
+    if (virObjectIsClass(anyobj, virObjectLockableClass) ||
+        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 f4c292b..e29dae7 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 _virObjectPoolableHashElement virObjectPoolableHashElement;
+typedef virObjectPoolableHashElement *virObjectPoolableHashElementPtr;
+
 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 _virObjectPoolableHashElement {
+    virObjectLockable parent;
+
+    char *primaryKey;
+    char *secondaryKey;
+};
+
 
 virClassPtr virClassForObject(void);
 virClassPtr virClassForObjectLockable(void);
+virClassPtr virClassForObjectPoolableHashElement(void);
 
 # ifndef VIR_PARENT_REQUIRED
 #  define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1)
@@ -108,6 +119,12 @@ void *
 virObjectLockableNew(virClassPtr klass)
     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.4


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