Skip to contentSkip to conversation

scsi: Trivial cleanups around g_malloc()

PATCH Applied Markus Armbruster 4 participants 7 messages Dec 4, 2014 – Dec 5, 2014

lore
Share & tools
lore
#0 lore raw

cover scsi: Trivial cleanups around g_malloc()

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

 hw/scsi/lsi53c895a.c   | 2 +-
 hw/scsi/megasas.c      | 6 ++----
 hw/scsi/scsi-generic.c | 6 ++----
 hw/scsi/virtio-scsi.c  | 2 +-
 4 files changed, 6 insertions(+), 10 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/3 scsi: Drop superfluous conditionals around g_free()

---
 hw/scsi/scsi-generic.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
hw/scsi/scsi-generic.c +2 -4
index 6b9e4e1..e53470f 100644
@@ -298,8 +298,7 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd)
 #endif
 
     if (r->req.cmd.xfer == 0) {
-        if (r->buf != NULL)
-            g_free(r->buf);
+        g_free(r->buf);
         r->buflen = 0;
         r->buf = NULL;
         /* The request is used as the AIO opaque value, so add a ref.  */
@@ -314,8 +313,7 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd)
     }
 
     if (r->buflen != r->req.cmd.xfer) {
-        if (r->buf != NULL)
-            g_free(r->buf);
+        g_free(r->buf);
         r->buf = g_malloc(r->req.cmd.xfer);
         r->buflen = r->req.cmd.xfer;
     }
Signature · 1 line
1.9.3
#2 lore raw

PATCH 2/3 scsi: Fuse g_malloc(); memset() into g_malloc0()

In reply to Markus Armbruster

Coccinelle semantic patch:

@@
expression LHS, SZ;
@@
- LHS = g_malloc(SZ);
- memset(LHS, 0, SZ);
+ LHS = g_malloc0(SZ);

---
 hw/scsi/megasas.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
hw/scsi/megasas.c +2 -4
index 604252a..4852237 100644
@@ -1018,8 +1018,7 @@ static int megasas_pd_get_info_submit(SCSIDevice *sdev, int lun,
     size_t len, resid;
 
     if (!cmd->iov_buf) {
-        cmd->iov_buf = g_malloc(dcmd_size);
-        memset(cmd->iov_buf, 0, dcmd_size);
+        cmd->iov_buf = g_malloc0(dcmd_size);
         info = cmd->iov_buf;
         info->inquiry_data[0] = 0x7f; /* Force PQual 0x3, PType 0x1f */
         info->vpd_page83[0] = 0x7f;
@@ -1221,8 +1220,7 @@ static int megasas_ld_get_info_submit(SCSIDevice *sdev, int lun,
     uint64_t ld_size;
 
     if (!cmd->iov_buf) {
-        cmd->iov_buf = g_malloc(dcmd_size);
-        memset(cmd->iov_buf, 0x0, dcmd_size);
+        cmd->iov_buf = g_malloc0(dcmd_size);
         info = cmd->iov_buf;
         megasas_setup_inquiry(cdb, 0x83, sizeof(info->vpd_page83));
         req = scsi_req_new(sdev, cmd->index, lun, cdb, cmd);
Signature · 1 line
1.9.3
#3 lore raw

PATCH 3/3 scsi: Use g_new() & friends where that makes obvious sense

In reply to Markus Armbruster

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).

---
 hw/scsi/lsi53c895a.c  | 2 +-
 hw/scsi/virtio-scsi.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/scsi/lsi53c895a.c +1 -1
index d9b4c7e..ec92048 100644
@@ -781,7 +781,7 @@ static void lsi_do_command(LSIState *s)
     }
 
     assert(s->current == NULL);
-    s->current = g_malloc0(sizeof(lsi_request));
+    s->current = g_new0(lsi_request, 1);
     s->current->tag = s->select_tag;
     s->current->req = scsi_req_new(dev, s->current->tag, s->current_lun, buf,
                                    s->current);
hw/scsi/virtio-scsi.c +1 -1
index ef48550..b06dd39 100644
@@ -829,7 +829,7 @@ void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
         virtio_cleanup(vdev);
         return;
     }
-    s->cmd_vqs = g_malloc0(s->conf.num_queues * sizeof(VirtQueue *));
+    s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues);
     s->sense_size = VIRTIO_SCSI_SENSE_SIZE;
     s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
 
Signature · 1 line
1.9.3
Reviewed-by on the v1 series
#4 lore raw
In reply to Markus Armbruster
Quoted 4 lines · Markus Armbruster

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

Series:

Quoted 7 lines
 hw/scsi/lsi53c895a.c   | 2 +-
 hw/scsi/megasas.c      | 6 ++----
 hw/scsi/scsi-generic.c | 6 ++----
 hw/scsi/virtio-scsi.c  | 2 +-
 4 files changed, 6 insertions(+), 10 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on the v1 series
#5 lore raw
In reply to Markus Armbruster
Quoted 5 lines · Markus Armbruster

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

Quoted 10 lines
 hw/scsi/lsi53c895a.c   | 2 +-
 hw/scsi/megasas.c      | 6 ++----
 hw/scsi/scsi-generic.c | 6 ++----
 hw/scsi/virtio-scsi.c  | 2 +-
 4 files changed, 6 insertions(+), 10 deletions(-)
Signature · 1 line
1.9.3
#6 lore raw
In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster

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

 hw/scsi/lsi53c895a.c   | 2 +-
 hw/scsi/megasas.c      | 6 ++----
 hw/scsi/scsi-generic.c | 6 ++----
 hw/scsi/virtio-scsi.c  | 2 +-
 4 files changed, 6 insertions(+), 10 deletions(-)

Applied, thanks.

Paolo