Add new API's for object management:
virObjectPoolableDefGetDef
- Just return the obj->def from the object.
virObjectPoolableDefSetDef
- If the new @def is non-NULL, replace the previous obj->def with
the new @def argument calling the obj->defFreeFunc on the previous
obj->def. If the @def is NULL, then just clear the obj->def
leaving the caller to handle removal of the previous obj->def.
virObjectPoolableDefGetNewDef
- Just return the obj->newDef from the object.
virObjectPoolableDefSetNewDef
- If the new @newDef is non-NULL, replace the previous obj->newDef
with the new @newDef argument calling the obj->defFreeFunc on
the previous obj->newDef. If the @newDef is NULL, then just clear
the obj->newDef leaving the caller to handle removal of the
previous obj->newDef.
virObjectPoolableDefSwapNewDef
- Manage swapping the obj->newDef into obj->def by first calling
the obj->defFreeFunc on the current obj->def and then stealing
the obj->newDef into obj->def.
Signed-off-by: John Ferlan <jferlan(a)redhat.com>
---
src/libvirt_private.syms | 5 ++
src/util/virobject.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++
src/util/virobject.h | 17 ++++++
3 files changed, 165 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4fad7c8..f6e40c7 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2272,7 +2272,12 @@ virObjectLock;
virObjectLockableNew;
virObjectLockableRecursiveNew;
virObjectNew;
+virObjectPoolableDefGetDef;
+virObjectPoolableDefGetNewDef;
virObjectPoolableDefNew;
+virObjectPoolableDefSetDef;
+virObjectPoolableDefSetNewDef;
+virObjectPoolableDefStealNewDef;
virObjectPoolableHashElementGetPrimaryKey;
virObjectPoolableHashElementGetSecondaryKey;
virObjectPoolableHashElementNew;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index d1fc3f0..ef253b6 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -536,6 +536,18 @@ virObjectGetPoolableHashElementObj(void *anyobj)
}
+static virObjectPoolableDefPtr
+virObjectGetPoolableDefObj(void *anyobj)
+{
+ if (virObjectIsClass(anyobj, virObjectPoolableDefClass))
+ return anyobj;
+
+ VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectPoolableDefClass);
+
+ return NULL;
+}
+
+
/**
* virObjectLock:
* @anyobj: any instance of virObjectLockablePtr
@@ -725,3 +737,134 @@ virObjectPoolableHashElementGetSecondaryKey(void *anyobj)
return obj->secondaryKey;
}
+
+
+/**
+ * virObjectPoolableDefGetDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ *
+ * Returns: Pointer to the object @def or NULL on failure
+ */
+void *
+virObjectPoolableDefGetDef(void *anyobj)
+{
+ virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+ if (!obj)
+ return NULL;
+
+ return obj->def;
+}
+
+
+/**
+ * virObjectPoolableDefSetDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ * @def: Opaque object definition to replace in the object
+ *
+ * Set the @def of the object - this may be NULL if clearing the @def
+ * from the object in which case it is up to the caller to handle managing
+ * the previous obj->def. If @def is not NULL, this includes a call to the
+ * defFreeFunc prior to the set.
+ *
+ * Returns 0 on success, -1 on failure
+ */
+int
+virObjectPoolableDefSetDef(void *anyobj,
+ void *def)
+{
+ virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+ if (!obj)
+ return -1;
+
+ if (def) {
+ obj->defFreeFunc(obj->def);
+ obj->def = def;
+ } else {
+ obj->def = NULL;
+ }
+
+ return 0;
+}
+
+
+/**
+ * virObjectPoolableDefGetNewDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ *
+ * Returns: Pointer to the object 'newDef' or NULL on failure
+ */
+void *
+virObjectPoolableDefGetNewDef(void *anyobj)
+{
+ virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+ if (!obj)
+ return NULL;
+
+ return obj->newDef;
+}
+
+
+/**
+ * virObjectPoolableDefSetNewDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ * @newDef: Opaque 'newDef' to replace in the object, may be NULL
+ *
+ * Set the 'newDef' of the object - this may be NULL if clearing the newDef
+ * from the object in which case it is up to the caller to handle managing
+ * the previous newDef. If @newDef is not NULL, this includes a call to the
+ * defFreeFunc prior to the set.
+ *
+ * Returns 0 on success, -1 on failure
+ */
+int
+virObjectPoolableDefSetNewDef(void *anyobj,
+ void *newDef)
+{
+ virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+ if (!obj)
+ return -1;
+
+ if (newDef) {
+ obj->defFreeFunc(obj->newDef);
+ obj->newDef = newDef;
+ } else {
+ obj->newDef = NULL;
+ }
+
+ return 0;
+}
+
+
+/**
+ * virObjectPoolableDefSwapNewDef
+ * @anyobj: Pointer to a locked PoolableDef object
+ *
+ * Manage swapping the obj->newDef into obj->def in one pass.
+ * When called, the previous obj->def will be free'd and the
+ * existing defDef will take it's place.
+ *
+ * Returns 0 on success, -1 on failure
+ */
+int
+virObjectPoolableDefStealNewDef(void *anyobj)
+{
+ virObjectPoolableDefPtr obj = virObjectGetPoolableDefObj(anyobj);
+
+ if (!obj)
+ return -1;
+
+ if (!obj->newDef) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("Unable to steal NULL newDef"));
+ return -1;
+ }
+
+ obj->defFreeFunc(obj->def);
+ VIR_STEAL_PTR(obj->def, obj->newDef);
+
+ return 0;
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 9bbbece..d3aeb73 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -177,4 +177,21 @@ virObjectPoolableHashElementGetPrimaryKey(void *anyobj);
const char *
virObjectPoolableHashElementGetSecondaryKey(void *anyobj);
+void *
+virObjectPoolableDefGetDef(void *anyobj);
+
+int
+virObjectPoolableDefSetDef(void *anyobj,
+ void *def);
+
+void *
+virObjectPoolableDefGetNewDef(void *anyobj);
+
+int
+virObjectPoolableDefSetNewDef(void *anyobj,
+ void *newDef);
+
+int
+virObjectPoolableDefStealNewDef(void *anyobj);
+
#endif /* __VIR_OBJECT_H */
--
2.9.4