Skip to contentSkip to conversation

9p: use g_new(T, n) instead of g_malloc(sizeof(T) * n)

PATCH In review Greg Kurz 2 participants 5 messages Nov 29, 2018

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH 1/2 9p: use g_new(T, n) instead of g_malloc(sizeof(T) * n)

Because it is a recommended coding practice (see HACKING).

---
 hw/9pfs/9p.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +2 -2
index bdf7919abfc5..55821343e594 100644
@@ -1331,8 +1331,8 @@ static void coroutine_fn v9fs_walk(void *opaque)
     trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
 
     if (nwnames && nwnames <= P9_MAXWELEM) {
-        wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
-        qids   = g_malloc0(sizeof(qids[0]) * nwnames);
+        wnames = g_new0(V9fsString, nwnames);
+        qids   = g_new0(V9fsQID, nwnames);
         for (i = 0; i < nwnames; i++) {
             err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
             if (err < 0) {
author
#1 lore raw

PATCH 2/2 xen/9pfs: use g_new(T, n) instead of g_malloc(sizeof(T) * n)

Because it is a recommended coding practice (see HACKING).

---
 hw/9pfs/xen-9p-backend.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/9pfs/xen-9p-backend.c +3 -3
index 3f54a21c7623..9015fe7773d0 100644
@@ -178,7 +178,7 @@ static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU *pdu,
 
     g_free(ring->sg);
 
-    ring->sg = g_malloc0(sizeof(*ring->sg) * 2);
+    ring->sg = g_new0(struct iovec, 2);
     xen_9pfs_out_sg(ring, ring->sg, &num, pdu->idx);
     *piov = ring->sg;
     *pniov = num;
@@ -196,7 +196,7 @@ static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU *pdu,
 
     g_free(ring->sg);
 
-    ring->sg = g_malloc0(sizeof(*ring->sg) * 2);
+    ring->sg = g_new0(struct iovec, 2);
     xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, size);
 
     buf_size = iov_size(ring->sg, num);
@@ -368,7 +368,7 @@ static int xen_9pfs_connect(struct XenDevice *xendev)
         return -1;
     }
 
-    xen_9pdev->rings = g_malloc0(xen_9pdev->num_rings * sizeof(Xen9pfsRing));
+    xen_9pdev->rings = g_new0(Xen9pfsRing, xen_9pdev->num_rings);
     for (i = 0; i < xen_9pdev->num_rings; i++) {
         char *str;
         int ring_order;
author
#4 lore raw
Quoted 10 lines · On Thu, 29 Nov 2018 16:26:48 +0000 Anthony PERARD <[email protected]>…
Quoted 3 lines · Greg Kurz

Because it is a recommended coding practice (see HACKING).

It would have been nice if there were a cover letter for these two patches, as we would had automatic build test and coding style test ;-).

Yeah... I had it ready and I simply forgot to pass it to stg mail :-\

I've pushed them to my 9p-next branch, which automatically triggers travis, so I'll have the build test covered at least :)

Thanks for the review !

Cheers,

--
Greg