[libvirt] [PATCH 2/5] util: storage: Turn virStorageSource into a virObject

Peter Krempa posted 5 patches 6 years, 11 months ago
[libvirt] [PATCH 2/5] util: storage: Turn virStorageSource into a virObject
Posted by Peter Krempa 6 years, 11 months ago
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>
---
 src/util/virstoragefile.c | 40 +++++++++++++++++++++++++++++----------
 src/util/virstoragefile.h |  2 ++
 2 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 7f52a5fdc7..56c6510c5e 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -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,48 @@ virStorageSourceClear(virStorageSourcePtr def)

     virStorageSourceInitiatorClear(&def->initiator);

-    memset(def, 0, sizeof(*def));
+    /* clear everything except the class header */
+    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);
 }


diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index 48af06653e..dc75d2d36f 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -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;
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 2/5] util: storage: Turn virStorageSource into a virObject
Posted by Erik Skultety 6 years, 11 months ago
On Fri, Feb 15, 2019 at 01:42:10PM +0100, Peter Krempa wrote:
> 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>
> ---
>  src/util/virstoragefile.c | 40 +++++++++++++++++++++++++++++----------
>  src/util/virstoragefile.h |  2 ++
>  2 files changed, 32 insertions(+), 10 deletions(-)
>
> diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
> index 7f52a5fdc7..56c6510c5e 100644
> --- a/src/util/virstoragefile.c
> +++ b/src/util/virstoragefile.c
> @@ -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,48 @@ virStorageSourceClear(virStorageSourcePtr def)
>
>      virStorageSourceInitiatorClear(&def->initiator);
>
> -    memset(def, 0, sizeof(*def));
> +    /* clear everything except the class header */
> +    memset((char *) def + sizeof(def->parent), 0,
> +           sizeof(*def) - sizeof(def->parent));

I've seen the reason behind this change, but I think a more thorough
explanation would be appreciated - in this case enhancing the commentary is
more useful than the commit message.

Reviewed-by: Erik Skultety <eskultet@redhat.com>

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list