qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc()
@ 2014-12-04 14:00 Markus Armbruster
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 1/3] util: Drop superfluous conditionals around g_free() Markus Armbruster
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Markus Armbruster @ 2014-12-04 14:00 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-trivial

Markus Armbruster (3):
  util: Drop superfluous conditionals around g_free()
  Fuse g_malloc(); memset() into g_new0()
  util: Use g_new() & friends where that makes obvious sense

 util/hbitmap.c |  4 ++--
 util/iov.c     |  4 ++--
 util/uri.c     | 51 +++++++++++++++++++++++----------------------------
 3 files changed, 27 insertions(+), 32 deletions(-)

-- 
1.9.3

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 1/3] util: Drop superfluous conditionals around g_free()
  2014-12-04 14:00 [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Markus Armbruster
@ 2014-12-04 14:00 ` Markus Armbruster
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 2/3] Fuse g_malloc(); memset() into g_new0() Markus Armbruster
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Markus Armbruster @ 2014-12-04 14:00 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-trivial

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 util/uri.c | 48 ++++++++++++++++++++++--------------------------
 1 file changed, 22 insertions(+), 26 deletions(-)

diff --git a/util/uri.c b/util/uri.c
index bbf2832..01dc09e 100644
--- a/util/uri.c
+++ b/util/uri.c
@@ -225,7 +225,7 @@ rfc3986_parse_scheme(URI *uri, const char **str) {
     while (ISA_ALPHA(cur) || ISA_DIGIT(cur) ||
            (*cur == '+') || (*cur == '-') || (*cur == '.')) cur++;
     if (uri != NULL) {
-	if (uri->scheme != NULL) g_free(uri->scheme);
+        g_free(uri->scheme);
 	uri->scheme = g_strndup(*str, cur - *str);
     }
     *str = cur;
@@ -262,8 +262,7 @@ rfc3986_parse_fragment(URI *uri, const char **str)
            ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))
         NEXT(cur);
     if (uri != NULL) {
-        if (uri->fragment != NULL)
-            g_free(uri->fragment);
+        g_free(uri->fragment);
 	if (uri->cleanup & 2)
 	    uri->fragment = g_strndup(*str, cur - *str);
 	else
@@ -298,8 +297,7 @@ rfc3986_parse_query(URI *uri, const char **str)
            ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))
         NEXT(cur);
     if (uri != NULL) {
-	if (uri->query != NULL)
-	    g_free (uri->query);
+        g_free(uri->query);
 	uri->query = g_strndup (*str, cur - *str);
     }
     *str = cur;
@@ -360,7 +358,7 @@ rfc3986_parse_user_info(URI *uri, const char **str)
 	NEXT(cur);
     if (*cur == '@') {
 	if (uri != NULL) {
-	    if (uri->user != NULL) g_free(uri->user);
+            g_free(uri->user);
 	    if (uri->cleanup & 2)
 		uri->user = g_strndup(*str, cur - *str);
 	    else
@@ -473,9 +471,9 @@ not_ipv4:
         NEXT(cur);
 found:
     if (uri != NULL) {
-	if (uri->authority != NULL) g_free(uri->authority);
+        g_free(uri->authority);
 	uri->authority = NULL;
-	if (uri->server != NULL) g_free(uri->server);
+        g_free(uri->server);
 	if (cur != host) {
 	    if (uri->cleanup & 2)
 		uri->server = g_strndup(host, cur - host);
@@ -585,7 +583,7 @@ rfc3986_parse_path_ab_empty(URI *uri, const char **str)
 	if (ret != 0) return(ret);
     }
     if (uri != NULL) {
-	if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (*str != cur) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -631,7 +629,7 @@ rfc3986_parse_path_absolute(URI *uri, const char **str)
 	}
     }
     if (uri != NULL) {
-	if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (cur != *str) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -673,7 +671,7 @@ rfc3986_parse_path_rootless(URI *uri, const char **str)
 	if (ret != 0) return(ret);
     }
     if (uri != NULL) {
-	if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (cur != *str) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -715,7 +713,7 @@ rfc3986_parse_path_no_scheme(URI *uri, const char **str)
 	if (ret != 0) return(ret);
     }
     if (uri != NULL) {
-	if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (cur != *str) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -769,7 +767,7 @@ rfc3986_parse_hier_part(URI *uri, const char **str)
     } else {
 	/* path-empty is effectively empty */
 	if (uri != NULL) {
-	    if (uri->path != NULL) g_free(uri->path);
+            g_free(uri->path);
 	    uri->path = NULL;
 	}
     }
@@ -812,7 +810,7 @@ rfc3986_parse_relative_ref(URI *uri, const char *str) {
     } else {
 	/* path-empty is effectively empty */
 	if (uri != NULL) {
-	    if (uri->path != NULL) g_free(uri->path);
+            g_free(uri->path);
 	    uri->path = NULL;
 	}
     }
@@ -1285,21 +1283,21 @@ static void
 uri_clean(URI *uri) {
     if (uri == NULL) return;
 
-    if (uri->scheme != NULL) g_free(uri->scheme);
+    g_free(uri->scheme);
     uri->scheme = NULL;
-    if (uri->server != NULL) g_free(uri->server);
+    g_free(uri->server);
     uri->server = NULL;
-    if (uri->user != NULL) g_free(uri->user);
+    g_free(uri->user);
     uri->user = NULL;
-    if (uri->path != NULL) g_free(uri->path);
+    g_free(uri->path);
     uri->path = NULL;
-    if (uri->fragment != NULL) g_free(uri->fragment);
+    g_free(uri->fragment);
     uri->fragment = NULL;
-    if (uri->opaque != NULL) g_free(uri->opaque);
+    g_free(uri->opaque);
     uri->opaque = NULL;
-    if (uri->authority != NULL) g_free(uri->authority);
+    g_free(uri->authority);
     uri->authority = NULL;
-    if (uri->query != NULL) g_free(uri->query);
+    g_free(uri->query);
     uri->query = NULL;
 }
 
@@ -1711,10 +1709,8 @@ uri_resolve(const char *uri, const char *base) {
 	/*
 	 * the base fragment must be ignored
 	 */
-	if (bas->fragment != NULL) {
-	    g_free(bas->fragment);
-	    bas->fragment = NULL;
-	}
+        g_free(bas->fragment);
+        bas->fragment = NULL;
 	val = uri_to_string(bas);
 	goto done;
     }
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 2/3] Fuse g_malloc(); memset() into g_new0()
  2014-12-04 14:00 [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Markus Armbruster
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 1/3] util: Drop superfluous conditionals around g_free() Markus Armbruster
@ 2014-12-04 14:00 ` Markus Armbruster
  2014-12-04 14:04   ` Markus Armbruster
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 3/3] util: Use g_new() & friends where that makes obvious sense Markus Armbruster
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Markus Armbruster @ 2014-12-04 14:00 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-trivial

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 util/uri.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/util/uri.c b/util/uri.c
index 01dc09e..918d235 100644
--- a/util/uri.c
+++ b/util/uri.c
@@ -1004,8 +1004,7 @@ URI *
 uri_new(void) {
     URI *ret;
 
-    ret = (URI *) g_malloc(sizeof(URI));
-    memset(ret, 0, sizeof(URI));
+    ret = g_new0(URI, 1);
     return(ret);
 }
 
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [Qemu-devel] [PATCH 3/3] util: Use g_new() & friends where that makes obvious sense
  2014-12-04 14:00 [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Markus Armbruster
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 1/3] util: Drop superfluous conditionals around g_free() Markus Armbruster
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 2/3] Fuse g_malloc(); memset() into g_new0() Markus Armbruster
@ 2014-12-04 14:00 ` Markus Armbruster
  2014-12-04 22:51 ` [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Eric Blake
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Markus Armbruster @ 2014-12-04 14:00 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-trivial

g_new(T, n) is neater than g_malloc(sizeof(T) * n).  It's also safer,
for two reasons.  One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.

This commit only touches allocations with size arguments of the form
sizeof(T).

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 util/hbitmap.c | 4 ++--
 util/iov.c     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/util/hbitmap.c b/util/hbitmap.c
index b3060e6..ab13971 100644
--- a/util/hbitmap.c
+++ b/util/hbitmap.c
@@ -373,7 +373,7 @@ void hbitmap_free(HBitmap *hb)
 
 HBitmap *hbitmap_alloc(uint64_t size, int granularity)
 {
-    HBitmap *hb = g_malloc0(sizeof (struct HBitmap));
+    HBitmap *hb = g_new0(struct HBitmap, 1);
     unsigned i;
 
     assert(granularity >= 0 && granularity < 64);
@@ -384,7 +384,7 @@ HBitmap *hbitmap_alloc(uint64_t size, int granularity)
     hb->granularity = granularity;
     for (i = HBITMAP_LEVELS; i-- > 0; ) {
         size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
-        hb->levels[i] = g_malloc0(size * sizeof(unsigned long));
+        hb->levels[i] = g_new0(unsigned long, size);
     }
 
     /* We necessarily have free bits in level 0 due to the definition
diff --git a/util/iov.c b/util/iov.c
index 24566c8..2fb18e6 100644
--- a/util/iov.c
+++ b/util/iov.c
@@ -253,7 +253,7 @@ unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
 
 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
 {
-    qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
+    qiov->iov = g_new(struct iovec, alloc_hint);
     qiov->niov = 0;
     qiov->nalloc = alloc_hint;
     qiov->size = 0;
@@ -277,7 +277,7 @@ void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
 
     if (qiov->niov == qiov->nalloc) {
         qiov->nalloc = 2 * qiov->nalloc + 1;
-        qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
+        qiov->iov = g_renew(struct iovec, qiov->iov, qiov->nalloc);
     }
     qiov->iov[qiov->niov].iov_base = base;
     qiov->iov[qiov->niov].iov_len = len;
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] [PATCH 2/3] Fuse g_malloc(); memset() into g_new0()
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 2/3] Fuse g_malloc(); memset() into g_new0() Markus Armbruster
@ 2014-12-04 14:04   ` Markus Armbruster
  0 siblings, 0 replies; 8+ messages in thread
From: Markus Armbruster @ 2014-12-04 14:04 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-trivial

Oops, subject lacks "util: " prefix.  Perhaps it can be fixed up on
commit.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc()
  2014-12-04 14:00 [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Markus Armbruster
                   ` (2 preceding siblings ...)
  2014-12-04 14:00 ` [Qemu-devel] [PATCH 3/3] util: Use g_new() & friends where that makes obvious sense Markus Armbruster
@ 2014-12-04 22:51 ` Eric Blake
  2014-12-05  6:54 ` Fam Zheng
  2014-12-10  8:34 ` [Qemu-devel] [Qemu-trivial] " Michael Tokarev
  5 siblings, 0 replies; 8+ messages in thread
From: Eric Blake @ 2014-12-04 22:51 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel; +Cc: qemu-trivial

[-- Attachment #1: Type: text/plain, Size: 481 bytes --]

On 12/04/2014 07:00 AM, Markus Armbruster wrote:
> Markus Armbruster (3):
>   util: Drop superfluous conditionals around g_free()
>   Fuse g_malloc(); memset() into g_new0()
>   util: Use g_new() & friends where that makes obvious sense

Series:
Reviewed-by: Eric Blake <eblake@redhat.com>

Up to the maintainer if they want to touch up your 2/3 subject line.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc()
  2014-12-04 14:00 [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Markus Armbruster
                   ` (3 preceding siblings ...)
  2014-12-04 22:51 ` [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Eric Blake
@ 2014-12-05  6:54 ` Fam Zheng
  2014-12-10  8:34 ` [Qemu-devel] [Qemu-trivial] " Michael Tokarev
  5 siblings, 0 replies; 8+ messages in thread
From: Fam Zheng @ 2014-12-05  6:54 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: qemu-trivial, qemu-devel

On Thu, 12/04 15:00, Markus Armbruster wrote:
> Markus Armbruster (3):
>   util: Drop superfluous conditionals around g_free()
>   Fuse g_malloc(); memset() into g_new0()
>   util: Use g_new() & friends where that makes obvious sense
> 
>  util/hbitmap.c |  4 ++--
>  util/iov.c     |  4 ++--
>  util/uri.c     | 51 +++++++++++++++++++++++----------------------------
>  3 files changed, 27 insertions(+), 32 deletions(-)
> 
> -- 
> 1.9.3
> 
> 
Reviewed-by: Fam Zheng <famz@redhat.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Qemu-devel] [Qemu-trivial] [PATCH 0/3] util: Trivial cleanups around g_malloc()
  2014-12-04 14:00 [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Markus Armbruster
                   ` (4 preceding siblings ...)
  2014-12-05  6:54 ` Fam Zheng
@ 2014-12-10  8:34 ` Michael Tokarev
  5 siblings, 0 replies; 8+ messages in thread
From: Michael Tokarev @ 2014-12-10  8:34 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel; +Cc: qemu-trivial

04.12.2014 17:00, Markus Armbruster wrote:
> Markus Armbruster (3):
>   util: Drop superfluous conditionals around g_free()
>   Fuse g_malloc(); memset() into g_new0()
>   util: Use g_new() & friends where that makes obvious sense

Applied to trivial (fixing 2/3 subject prefix), thank you!

/mjt

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-12-10  8:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-04 14:00 [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Markus Armbruster
2014-12-04 14:00 ` [Qemu-devel] [PATCH 1/3] util: Drop superfluous conditionals around g_free() Markus Armbruster
2014-12-04 14:00 ` [Qemu-devel] [PATCH 2/3] Fuse g_malloc(); memset() into g_new0() Markus Armbruster
2014-12-04 14:04   ` Markus Armbruster
2014-12-04 14:00 ` [Qemu-devel] [PATCH 3/3] util: Use g_new() & friends where that makes obvious sense Markus Armbruster
2014-12-04 22:51 ` [Qemu-devel] [PATCH 0/3] util: Trivial cleanups around g_malloc() Eric Blake
2014-12-05  6:54 ` Fam Zheng
2014-12-10  8:34 ` [Qemu-devel] [Qemu-trivial] " Michael Tokarev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).