From 27bc02843b5998f8c143fd4053023188b9e5dbd2 Mon Sep 17 00:00:00 2001 From: natano Date: Tue, 19 Jul 2016 09:52:34 +0000 Subject: [PATCH] Replace malloc() + memset() with calloc(). ok mlarkin --- usr.sbin/vmd/virtio.c | 38 +++++++++++++------------------------- usr.sbin/vmd/vmm.c | 6 +++--- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/usr.sbin/vmd/virtio.c b/usr.sbin/vmd/virtio.c index ddf65052f3d..02eb053362a 100644 --- a/usr.sbin/vmd/virtio.c +++ b/usr.sbin/vmd/virtio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: virtio.c,v 1.15 2016/07/09 09:06:22 stefan Exp $ */ +/* $OpenBSD: virtio.c,v 1.16 2016/07/19 09:52:34 natano Exp $ */ /* * Copyright (c) 2015 Mike Larkin @@ -166,14 +166,12 @@ viornd_notifyq(void) q_gpa = viornd.vq[viornd.cfg.queue_notify].qa; q_gpa = q_gpa * VIRTIO_PAGE_SIZE; - buf = malloc(vr_sz); + buf = calloc(1, vr_sz); if (buf == NULL) { - log_warn("malloc error getting viornd ring"); + log_warn("calloc error getting viornd ring"); return (0); } - memset(buf, 0, vr_sz); - if (read_mem(q_gpa, buf, vr_sz)) { free(buf); return (0); @@ -382,14 +380,12 @@ vioblk_notifyq(struct vioblk_dev *dev) q_gpa = dev->vq[dev->cfg.queue_notify].qa; q_gpa = q_gpa * VIRTIO_PAGE_SIZE; - vr = malloc(vr_sz); + vr = calloc(1, vr_sz); if (vr == NULL) { - log_warn("malloc error getting vioblk ring"); + log_warn("calloc error getting vioblk ring"); return (0); } - memset(vr, 0, vr_sz); - if (read_mem(q_gpa, vr, vr_sz)) { log_warnx("error reading gpa 0x%llx", q_gpa); free(vr); @@ -804,14 +800,12 @@ vionet_enq_rx(struct vionet_dev *dev, char *pkt, ssize_t sz, int *spc) q_gpa = dev->vq[0].qa; q_gpa = q_gpa * VIRTIO_PAGE_SIZE; - vr = malloc(vr_sz); + vr = calloc(1, vr_sz); if (vr == NULL) { - log_warn("rx enq: malloc error getting vionet ring"); + log_warn("rx enq: calloc error getting vionet ring"); return (0); } - memset(vr, 0, vr_sz); - if (read_mem(q_gpa, vr, vr_sz)) { log_warnx("rx enq: error reading gpa 0x%llx", q_gpa); free(vr); @@ -1002,14 +996,12 @@ vionet_notifyq(struct vionet_dev *dev) q_gpa = dev->vq[dev->cfg.queue_notify].qa; q_gpa = q_gpa * VIRTIO_PAGE_SIZE; - vr = malloc(vr_sz); + vr = calloc(1, vr_sz); if (vr == NULL) { - log_warn("malloc error getting vionet ring"); + log_warn("calloc error getting vionet ring"); goto out; } - memset(vr, 0, vr_sz); - if (read_mem(q_gpa, vr, vr_sz)) { log_warnx("error reading gpa 0x%llx", q_gpa); goto out; @@ -1163,15 +1155,13 @@ virtio_init(struct vm_create_params *vcp, int *child_disks, int *child_taps) + sizeof(uint16_t) * (2 + VIORND_QUEUE_SIZE)); if (vcp->vcp_ndisks > 0) { - vioblk = malloc(sizeof(struct vioblk_dev) * vcp->vcp_ndisks); + vioblk = calloc(vcp->vcp_ndisks, sizeof(struct vioblk_dev)); if (vioblk == NULL) { - log_warn("%s: malloc failure allocating vioblks", + log_warn("%s: calloc failure allocating vioblks", __progname); return; } - memset(vioblk, 0, sizeof(struct vioblk_dev) * vcp->vcp_ndisks); - /* One virtio block device for each disk defined in vcp */ for (i = 0; i < vcp->vcp_ndisks; i++) { if ((sz = lseek(child_disks[i], 0, SEEK_END)) == -1) @@ -1206,15 +1196,13 @@ virtio_init(struct vm_create_params *vcp, int *child_disks, int *child_taps) } if (vcp->vcp_nnics > 0) { - vionet = malloc(sizeof(struct vionet_dev) * vcp->vcp_nnics); + vionet = calloc(vcp->vcp_nnics, sizeof(struct vionet_dev)); if (vionet == NULL) { - log_warn("%s: malloc failure allocating vionets", + log_warn("%s: calloc failure allocating vionets", __progname); return; } - memset(vionet, 0, sizeof(struct vionet_dev) * vcp->vcp_nnics); - nr_vionet = vcp->vcp_nnics; /* Virtio network */ for (i = 0; i < vcp->vcp_nnics; i++) { diff --git a/usr.sbin/vmd/vmm.c b/usr.sbin/vmd/vmm.c index 6ca56930e67..826b4cc21ef 100644 --- a/usr.sbin/vmd/vmm.c +++ b/usr.sbin/vmd/vmm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmm.c,v 1.32 2016/07/09 09:06:22 stefan Exp $ */ +/* $OpenBSD: vmm.c,v 1.33 2016/07/19 09:52:34 natano Exp $ */ /* * Copyright (c) 2015 Mike Larkin @@ -890,8 +890,8 @@ run_vm(int *child_disks, int *child_taps, struct vm_create_params *vcp, ret = 0; - tid = malloc(sizeof(pthread_t) * vcp->vcp_ncpus); - vrp = malloc(sizeof(struct vm_run_params *) * vcp->vcp_ncpus); + tid = calloc(vcp->vcp_ncpus, sizeof(pthread_t)); + vrp = calloc(vcp->vcp_ncpus, sizeof(struct vm_run_params *)); if (tid == NULL || vrp == NULL) { log_warn("%s: memory allocation error - exiting.", __progname); -- 2.20.1