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.
The recursive parameter controls which type of lock is used.
Signed-off-by: John Ferlan <jferlan(a)redhat.com>
---
src/libvirt_private.syms | 2 ++
src/util/virobject.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++-
src/util/virobject.h | 18 +++++++++++
3 files changed, 101 insertions(+), 1 deletion(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 9693dc4..eca1980 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;
@@ -2270,6 +2271,7 @@ virObjectLock;
virObjectLockableNew;
virObjectLockableRecursiveNew;
virObjectNew;
+virObjectPoolableHashElementNew;
virObjectRef;
virObjectUnlock;
virObjectUnref;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index b1fabd7..25dbb8f 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -61,8 +61,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)
@@ -79,6 +81,13 @@ virObjectOnceInit(void)
virObjectLockableDispose)))
return -1;
+ if (!(virObjectPoolableHashElementClass =
+ virClassNew(virObjectLockableClass,
+ "virObjectPoolableHashElement",
+ sizeof(virObjectPoolableHashElement),
+ virObjectPoolableHashElementDispose)))
+ return -1;
+
return 0;
}
@@ -116,6 +125,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
@@ -285,6 +311,59 @@ virObjectLockableDispose(void *anyobj)
}
+void *
+virObjectPoolableHashElementNew(virClassPtr klass,
+ bool recursive,
+ 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 (!recursive) {
+ if (!(obj = virObjectLockableNew(klass)))
+ return NULL;
+ } else {
+ if (!(obj = virObjectLockableRecursiveNew(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
@@ -352,7 +431,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 7df9b47..0377cd5 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,
@@ -60,9 +63,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)
@@ -113,6 +124,13 @@ void *
virObjectLockableRecursiveNew(virClassPtr klass)
ATTRIBUTE_NONNULL(1);
+void *
+virObjectPoolableHashElementNew(virClassPtr klass,
+ bool recursive,
+ const char *primaryKey,
+ const char *secondaryKey)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
+
void
virObjectLock(void *lockableobj)
ATTRIBUTE_NONNULL(1);
--
2.9.4