util: Trivial cleanups around g_malloc()
PATCH 3/3 util: Use g_new() & friends where that makes obvious sense
In reply to Markus Armbrusterg_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).
--- util/hbitmap.c | 4 ++-- util/iov.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
util/hbitmap.c +2 -2
@@ -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
util/iov.c +2 -2
@@ -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;
Signature · 1 line
1.9.3
cover util: Trivial cleanups around g_malloc()
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(-)
Signature · 1 line
1.9.3
PATCH 1/3 util: Drop superfluous conditionals around g_free()
--- util/uri.c | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-)
util/uri.c +22 -26
@@ -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; }
Signature · 1 line
1.9.3
PATCH 2/3 Fuse g_malloc(); memset() into g_new0()
In reply to Markus Armbruster--- util/uri.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
util/uri.c +1 -2
@@ -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); }
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH 2/3] Fuse g_malloc(); memset() into g_new0()
Oops, subject lacks "util: " prefix. Perhaps it can be fixed up on commit.
Quoted 4 lines · Markus Armbruster
Markus Armbruster (3):
util: Drop superfluous conditionals aroundg_free()Fuseg_malloc();memset()intog_new0()
util: Useg_new()& friends where that makes obvious sense
Series:
Up to the maintainer if they want to touch up your 2/3 subject line.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 5 lines · Markus Armbruster
Markus Armbruster (3):
util: Drop superfluous conditionals aroundg_free()Fuseg_malloc();memset()intog_new0()
util: Useg_new()& friends where that makes obvious sense
Quoted 9 lines
util/hbitmap.c | 4 ++-- util/iov.c | 4 ++-- util/uri.c | 51 +++++++++++++++++++++++---------------------------- 3 files changed, 27 insertions(+), 32 deletions(-)Signature · 1 line
1.9.3
Michael Tokarev
AppliedQuoted 4 lines · Markus Armbruster
Markus Armbruster (3):
util: Drop superfluous conditionals aroundg_free()Fuseg_malloc();memset()intog_new0()
util: Useg_new()& friends where that makes obvious sense
Applied to trivial (fixing 2/3 subject prefix), thank you!
/mjt