[libvirt] [PATCH 04/11] util: use glib memory allocation functions

Daniel P. Berrangé posted 11 patches 6 years, 4 months ago
Only 10 patches received!
There is a newer version of this series
[libvirt] [PATCH 04/11] util: use glib memory allocation functions
Posted by Daniel P. Berrangé 6 years, 4 months ago
Convert the VIR_ALLOC family of APIs with use of the g_malloc family of
APIs. Use of VIR_ALLOC related functions should be incrementally phased
out over time, allowing return value checks to be dropped. Use of
VIR_FREE should be replaced with auto-cleanup whenever possible.

We previously used the 'calloc-posix' gnulib module because mingw does
not set errno to ENOMEM on failure.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 bootstrap.conf      |  1 -
 src/util/viralloc.c | 29 ++++++-----------------------
 2 files changed, 6 insertions(+), 24 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 4f944a9c23..549d18c6d4 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -28,7 +28,6 @@ byteswap
 c-ctype
 c-strcase
 c-strcasestr
-calloc-posix
 canonicalize-lgpl
 chown
 clock-time
diff --git a/src/util/viralloc.c b/src/util/viralloc.c
index 10a8d0fb73..b8ca850764 100644
--- a/src/util/viralloc.c
+++ b/src/util/viralloc.c
@@ -45,10 +45,7 @@ VIR_LOG_INIT("util.alloc");
 int virAlloc(void *ptrptr,
              size_t size)
 {
-    *(void **)ptrptr = calloc(1, size);
-    if (*(void **)ptrptr == NULL)
-        abort();
-
+    *(void **)ptrptr = g_malloc0(size);
     return 0;
 }
 
@@ -69,10 +66,7 @@ int virAllocN(void *ptrptr,
               size_t size,
               size_t count)
 {
-    *(void**)ptrptr = calloc(count, size);
-    if (*(void**)ptrptr == NULL)
-        abort();
-
+    *(void**)ptrptr = g_malloc0_n(count, size);
     return 0;
 }
 
@@ -94,16 +88,7 @@ int virReallocN(void *ptrptr,
                 size_t size,
                 size_t count)
 {
-    void *tmp;
-
-    if (xalloc_oversized(count, size))
-        abort();
-
-    tmp = realloc(*(void**)ptrptr, size * count);
-    if (!tmp && ((size * count) != 0))
-        abort();
-
-    *(void**)ptrptr = tmp;
+    *(void **)ptrptr = g_realloc_n(*(void**)ptrptr, size, count);
     return 0;
 }
 
@@ -343,9 +328,7 @@ int virAllocVar(void *ptrptr,
         abort();
 
     alloc_size = struct_size + (element_size * count);
-    *(void **)ptrptr = calloc(1, alloc_size);
-    if (*(void **)ptrptr == NULL)
-        abort();
+    *(void **)ptrptr = g_malloc0(alloc_size);
     return 0;
 }
 
@@ -362,7 +345,7 @@ void virFree(void *ptrptr)
 {
     int save_errno = errno;
 
-    free(*(void**)ptrptr);
+    g_free(*(void**)ptrptr);
     *(void**)ptrptr = NULL;
     errno = save_errno;
 }
@@ -395,7 +378,7 @@ void virDispose(void *ptrptr,
     if (*(void**)ptrptr && count > 0)
         memset(*(void **)ptrptr, 0, count * element_size);
 
-    free(*(void**)ptrptr);
+    g_free(*(void**)ptrptr);
     *(void**)ptrptr = NULL;
 
     if (countptr)
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 04/11] util: use glib memory allocation functions
Posted by Pavel Hrdina 6 years, 4 months ago
On Fri, Sep 27, 2019 at 06:17:26PM +0100, Daniel P. Berrangé wrote:
> Convert the VIR_ALLOC family of APIs with use of the g_malloc family of
> APIs. Use of VIR_ALLOC related functions should be incrementally phased
> out over time, allowing return value checks to be dropped. Use of
> VIR_FREE should be replaced with auto-cleanup whenever possible.
> 
> We previously used the 'calloc-posix' gnulib module because mingw does
> not set errno to ENOMEM on failure.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  bootstrap.conf      |  1 -
>  src/util/viralloc.c | 29 ++++++-----------------------
>  2 files changed, 6 insertions(+), 24 deletions(-)

\o/ Another gnulib module removed.  On Friday I was trying to figure out
how to incorporate gnulib into meson build system and it's not easy at
all and still I did not verify that it actually works so we might need
to get rid of gnulib before we can do that conversion.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 04/11] util: use glib memory allocation functions
Posted by Daniel P. Berrangé 6 years, 4 months ago
On Mon, Sep 30, 2019 at 12:02:17PM +0200, Pavel Hrdina wrote:
> On Fri, Sep 27, 2019 at 06:17:26PM +0100, Daniel P. Berrangé wrote:
> > Convert the VIR_ALLOC family of APIs with use of the g_malloc family of
> > APIs. Use of VIR_ALLOC related functions should be incrementally phased
> > out over time, allowing return value checks to be dropped. Use of
> > VIR_FREE should be replaced with auto-cleanup whenever possible.
> > 
> > We previously used the 'calloc-posix' gnulib module because mingw does
> > not set errno to ENOMEM on failure.
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> >  bootstrap.conf      |  1 -
> >  src/util/viralloc.c | 29 ++++++-----------------------
> >  2 files changed, 6 insertions(+), 24 deletions(-)
> 
> \o/ Another gnulib module removed.  On Friday I was trying to figure out
> how to incorporate gnulib into meson build system and it's not easy at
> all and still I did not verify that it actually works so we might need
> to get rid of gnulib before we can do that conversion.

I think it is possible roughly as follows

  - Create a minimal configure.ac that only runs gnulib, nothing else
  - In meson, we have to run 'configure' using 'run_command'
  - Define 'custom_target' that invokes 'make'
  - Declare a dependancy for the result of the custom_target and
    add that as a pre-req for every other target

The main problem when I was exploring this is that 'run_command' does
not show any output from configure. So you don't see the progress
messages from configure. This is not nice, but also not the end of the
world since this configure script will be much smaller than what we have
today, and getting ever smaller as we purge more gnulib.

> Reviewed-by: Pavel Hrdina <phrdina@redhat.com>

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 04/11] util: use glib memory allocation functions
Posted by Pavel Hrdina 6 years, 4 months ago
On Mon, Sep 30, 2019 at 11:08:09AM +0100, Daniel P. Berrangé wrote:
> On Mon, Sep 30, 2019 at 12:02:17PM +0200, Pavel Hrdina wrote:
> > On Fri, Sep 27, 2019 at 06:17:26PM +0100, Daniel P. Berrangé wrote:
> > > Convert the VIR_ALLOC family of APIs with use of the g_malloc family of
> > > APIs. Use of VIR_ALLOC related functions should be incrementally phased
> > > out over time, allowing return value checks to be dropped. Use of
> > > VIR_FREE should be replaced with auto-cleanup whenever possible.
> > > 
> > > We previously used the 'calloc-posix' gnulib module because mingw does
> > > not set errno to ENOMEM on failure.
> > > 
> > > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > > ---
> > >  bootstrap.conf      |  1 -
> > >  src/util/viralloc.c | 29 ++++++-----------------------
> > >  2 files changed, 6 insertions(+), 24 deletions(-)
> > 
> > \o/ Another gnulib module removed.  On Friday I was trying to figure out
> > how to incorporate gnulib into meson build system and it's not easy at
> > all and still I did not verify that it actually works so we might need
> > to get rid of gnulib before we can do that conversion.
> 
> I think it is possible roughly as follows
> 
>   - Create a minimal configure.ac that only runs gnulib, nothing else
>   - In meson, we have to run 'configure' using 'run_command'
>   - Define 'custom_target' that invokes 'make'
>   - Declare a dependancy for the result of the custom_target and
>     add that as a pre-req for every other target
> 
> The main problem when I was exploring this is that 'run_command' does
> not show any output from configure. So you don't see the progress
> messages from configure. This is not nice, but also not the end of the
> world since this configure script will be much smaller than what we have
> today, and getting ever smaller as we purge more gnulib.

I have it in a similar way except that I don't use 'run_command' but
only 'custom_target' as I don't want to pollute source dire.  It
actually compiles and the static library libgnu.a is generated.  I just
did not compile any executable that would actually use gnulib and tested
it that it works.  But I'll definitely try to finish it and post some
RFC patches to list so we can discuss that solution and figure out what
to do.

Pavel
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 04/11] util: use glib memory allocation functions
Posted by Daniel P. Berrangé 6 years, 4 months ago
On Mon, Sep 30, 2019 at 12:18:11PM +0200, Pavel Hrdina wrote:
> On Mon, Sep 30, 2019 at 11:08:09AM +0100, Daniel P. Berrangé wrote:
> > On Mon, Sep 30, 2019 at 12:02:17PM +0200, Pavel Hrdina wrote:
> > > On Fri, Sep 27, 2019 at 06:17:26PM +0100, Daniel P. Berrangé wrote:
> > > > Convert the VIR_ALLOC family of APIs with use of the g_malloc family of
> > > > APIs. Use of VIR_ALLOC related functions should be incrementally phased
> > > > out over time, allowing return value checks to be dropped. Use of
> > > > VIR_FREE should be replaced with auto-cleanup whenever possible.
> > > > 
> > > > We previously used the 'calloc-posix' gnulib module because mingw does
> > > > not set errno to ENOMEM on failure.
> > > > 
> > > > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > > > ---
> > > >  bootstrap.conf      |  1 -
> > > >  src/util/viralloc.c | 29 ++++++-----------------------
> > > >  2 files changed, 6 insertions(+), 24 deletions(-)
> > > 
> > > \o/ Another gnulib module removed.  On Friday I was trying to figure out
> > > how to incorporate gnulib into meson build system and it's not easy at
> > > all and still I did not verify that it actually works so we might need
> > > to get rid of gnulib before we can do that conversion.
> > 
> > I think it is possible roughly as follows
> > 
> >   - Create a minimal configure.ac that only runs gnulib, nothing else
> >   - In meson, we have to run 'configure' using 'run_command'
> >   - Define 'custom_target' that invokes 'make'
> >   - Declare a dependancy for the result of the custom_target and
> >     add that as a pre-req for every other target
> > 
> > The main problem when I was exploring this is that 'run_command' does
> > not show any output from configure. So you don't see the progress
> > messages from configure. This is not nice, but also not the end of the
> > world since this configure script will be much smaller than what we have
> > today, and getting ever smaller as we purge more gnulib.
> 
> I have it in a similar way except that I don't use 'run_command' but
> only 'custom_target' as I don't want to pollute source dire.  It
> actually compiles and the static library libgnu.a is generated.  I just
> did not compile any executable that would actually use gnulib and tested
> it that it works.  But I'll definitely try to finish it and post some
> RFC patches to list so we can discuss that solution and figure out what
> to do.

Looks like wiring up a custom target as a dependancy is not quite as
simple as I thought. There's a recentish enhancement to support it
though:

  https://github.com/mesonbuild/meson/pull/5103

Seems to need a fairly new meson though (>= 0.51 IIUC)

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

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