]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: storage: Turn virStorageSource into a virObject
authorPeter Krempa <pkrempa@redhat.com>
Fri, 15 Feb 2019 08:46:03 +0000 (09:46 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 18 Feb 2019 09:31:17 +0000 (10:31 +0100)
To allow tracking a single virStorageSource in multiple structures
without extra hassle allow refcounting by turining it into an object.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
src/util/virstoragefile.c
src/util/virstoragefile.h

index 7f52a5fdc7dbd013ec2ec734a5f5916e34481342..f3f05c82ecbc94d8d9bf91fd56c1e6991e63577a 100644 (file)
@@ -47,6 +47,8 @@
 
 VIR_LOG_INIT("util.storagefile");
 
+static virClassPtr virStorageSourceClass;
+
 VIR_ENUM_IMPL(virStorage, VIR_STORAGE_TYPE_LAST,
               "none",
               "file",
@@ -2558,30 +2560,49 @@ virStorageSourceClear(virStorageSourcePtr def)
 
     virStorageSourceInitiatorClear(&def->initiator);
 
-    memset(def, 0, sizeof(*def));
+    /* clear everything except the class header as the object APIs
+     * will break otherwise */
+    memset((char *) def + sizeof(def->parent), 0,
+           sizeof(*def) - sizeof(def->parent));
+}
+
+
+static void
+virStorageSourceDispose(void *obj)
+{
+    virStorageSourcePtr src = obj;
+
+    virStorageSourceClear(src);
 }
 
 
+static int
+virStorageSourceOnceInit(void)
+{
+    if (!VIR_CLASS_NEW(virStorageSource, virClassForObject()))
+        return -1;
+
+    return 0;
+}
+
+
+VIR_ONCE_GLOBAL_INIT(virStorageSource);
+
+
 virStorageSourcePtr
 virStorageSourceNew(void)
 {
-    virStorageSourcePtr ret = NULL;
-
-    if (VIR_ALLOC(ret) < 0)
+    if (virStorageSourceInitialize() < 0)
         return NULL;
 
-    return ret;
+    return virObjectNew(virStorageSourceClass);
 }
 
 
 void
 virStorageSourceFree(virStorageSourcePtr def)
 {
-    if (!def)
-        return;
-
-    virStorageSourceClear(def);
-    VIR_FREE(def);
+    virObjectUnref(def);
 }
 
 
index 48af06653ef35192707ed586d738fc3ab1460377..dc75d2d36f50ee4085b067cf4d2d6bcaa201b113 100644 (file)
@@ -242,6 +242,8 @@ typedef virStorageSource *virStorageSourcePtr;
  * IMPORTANT: When adding fields to this struct it's also necessary to add
  * appropriate code to the virStorageSourceCopy deep copy function */
 struct _virStorageSource {
+    virObject parent;
+
     unsigned int id; /* backing chain identifier, 0 is unset */
     int type; /* virStorageType */
     char *path;