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

[libvirt] [PATCH 8/9] util: Introduce virObjectPoolableDef



Add a new virObjectPoolableHashElement child which will be used to provide
the basis for driver def/newDef elements.

Each virObjectPoolableDef has:

  1. A @recursive argument to denote which type of locks to use.
  2. A required @primaryKey to be used to uniquely identity the object
     by some string value.
  3. An optional @secondaryKey to be used as a secondary means of search
     for the object by some string value.
  4. A required @def and @defFreeFunc. The @def will be consumed by the
     object and when disposed the free function will be called.

The _virObjectPoolableDef has an additional @newDef element to store
the "next" boot configuration for consumers that support the functionality.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index fea0be7..4fad7c8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2258,6 +2258,7 @@ virNumaSetupMemoryPolicy;
 # util/virobject.h
 virClassForObject;
 virClassForObjectLockable;
+virClassForObjectPoolableDef;
 virClassForObjectPoolableHashElement;
 virClassIsDerivedFrom;
 virClassName;
@@ -2271,6 +2272,7 @@ virObjectLock;
 virObjectLockableNew;
 virObjectLockableRecursiveNew;
 virObjectNew;
+virObjectPoolableDefNew;
 virObjectPoolableHashElementGetPrimaryKey;
 virObjectPoolableHashElementGetSecondaryKey;
 virObjectPoolableHashElementNew;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 74299f8..d1fc3f0 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -62,9 +62,11 @@ struct _virClass {
 static virClassPtr virObjectClass;
 static virClassPtr virObjectLockableClass;
 static virClassPtr virObjectPoolableHashElementClass;
+static virClassPtr virObjectPoolableDefClass;
 
 static void virObjectLockableDispose(void *anyobj);
 static void virObjectPoolableHashElementDispose(void *anyobj);
+static void virObjectPoolableDefDispose(void *anyobj);
 
 static int
 virObjectOnceInit(void)
@@ -88,6 +90,13 @@ virObjectOnceInit(void)
                       virObjectPoolableHashElementDispose)))
         return -1;
 
+    if (!(virObjectPoolableDefClass =
+          virClassNew(virObjectPoolableHashElementClass,
+                      "virObjectPoolableDef",
+                      sizeof(virObjectPoolableDef),
+                      virObjectPoolableDefDispose)))
+        return -1;
+
     return 0;
 }
 
@@ -142,6 +151,23 @@ virClassForObjectPoolableHashElement(void)
 
 
 /**
+ * virClassForObjectPoolableDef:
+ *
+ * Returns the class instance for the virObjectPoolableDef type
+ */
+virClassPtr
+virClassForObjectPoolableDef(void)
+{
+    if (virObjectInitialize() < 0)
+        return NULL;
+
+    VIR_DEBUG("virObjectPoolableDefClass=%p",
+              virObjectPoolableDefClass);
+    return virObjectPoolableDefClass;
+}
+
+
+/**
  * virClassNew:
  * @parent: the parent class
  * @name: the class name
@@ -365,6 +391,62 @@ virObjectPoolableHashElementDispose(void *anyobj)
 
 
 /**
+ * virObjectPoolableDefNew:
+ * @klass: the klass to check
+ * @recursive: boolean to dictate which Lockable object to use
+ * @primaryKey: primary key (required)
+ * @secondaryKey: secondary key
+ * @def: XML definition (required)
+ * @defFreeFunc: Free function for @def and @newDef (required)
+ *
+ * Create a new poolable def object for storing "common" domain defs.
+ *
+ * Returns: New object on success, NULL on failure w/ error message set
+ */
+void *
+virObjectPoolableDefNew(virClassPtr klass,
+                        bool recursive,
+                        const char *primaryKey,
+                        const char *secondaryKey,
+                        void *def,
+                        virFreeCallback defFreeFunc)
+{
+    virObjectPoolableDefPtr obj;
+
+    if (!virClassIsDerivedFrom(klass, virClassForObjectPoolableDef())) {
+        virReportInvalidArg(klass,
+                            _("Class %s must derive from "
+                              "virObjectPoolableDef"),
+                            virClassName(klass));
+        return NULL;
+    }
+
+    if (!(obj = virObjectPoolableHashElementNew(klass, recursive,
+                                                primaryKey, secondaryKey)))
+        return NULL;
+
+    obj->def = def;
+    obj->defFreeFunc = defFreeFunc;
+
+    VIR_DEBUG("obj=%p, def=%p ff=%p", obj, obj->def, obj->defFreeFunc);
+
+    return obj;
+}
+
+
+static void
+virObjectPoolableDefDispose(void *anyobj)
+{
+    virObjectPoolableDefPtr obj = anyobj;
+
+    VIR_DEBUG("dispose obj=%p", obj);
+
+    (obj->defFreeFunc)(obj->def);
+    (obj->defFreeFunc)(obj->newDef);
+}
+
+
+/**
  * virObjectUnref:
  * @anyobj: any instance of virObjectPtr
  *
@@ -432,7 +514,8 @@ static virObjectLockablePtr
 virObjectGetLockableObj(void *anyobj)
 {
     if (virObjectIsClass(anyobj, virObjectLockableClass) ||
-        virObjectIsClass(anyobj, virObjectPoolableHashElementClass))
+        virObjectIsClass(anyobj, virObjectPoolableHashElementClass) ||
+        virObjectIsClass(anyobj, virObjectPoolableDefClass))
         return anyobj;
 
     VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass);
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 4706502..9bbbece 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -37,6 +37,9 @@ typedef virObjectLockable *virObjectLockablePtr;
 typedef struct _virObjectPoolableHashElement virObjectPoolableHashElement;
 typedef virObjectPoolableHashElement *virObjectPoolableHashElementPtr;
 
+typedef struct _virObjectPoolableDef virObjectPoolableDef;
+typedef virObjectPoolableDef *virObjectPoolableDefPtr;
+
 typedef void (*virObjectDisposeCallback)(void *obj);
 
 /* Most code should not play with the contents of this struct; however,
@@ -70,10 +73,22 @@ struct _virObjectPoolableHashElement {
     char *secondaryKey;
 };
 
+struct _virObjectPoolableDef {
+    virObjectPoolableHashElement parent;
+
+    /* 'def' is the current config definition.
+     * 'newDef' is the next boot configuration.
+     */
+    void *def;
+    void *newDef;
+    virFreeCallback defFreeFunc;
+};
+
 
 virClassPtr virClassForObject(void);
 virClassPtr virClassForObjectLockable(void);
 virClassPtr virClassForObjectPoolableHashElement(void);
+virClassPtr virClassForObjectPoolableDef(void);
 
 # ifndef VIR_PARENT_REQUIRED
 #  define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1)
@@ -131,6 +146,16 @@ virObjectPoolableHashElementNew(virClassPtr klass,
                                 const char *secondaryKey)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 
+void *
+virObjectPoolableDefNew(virClassPtr klass,
+                        bool recursive,
+                        const char *primaryKey,
+                        const char *secondaryKey,
+                        void *def,
+                        virFreeCallback defFreeFunc)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(5)
+    ATTRIBUTE_NONNULL(6);
+
 void
 virObjectLock(void *lockableobj)
     ATTRIBUTE_NONNULL(1);
-- 
2.9.4


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