Replace g_malloc0 occurences by g_new0 where that makes obvious sense. This looks to be the best practice bringing extra compile-time checking and integer overflow protection.
Signed-off-by: Eric Auger <eric.au...@linaro.org> --- hw/vfio/common.c | 8 ++++---- hw/vfio/pci.c | 24 ++++++++++++------------ hw/vfio/platform.c | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index b1045da..7f09700 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -376,7 +376,7 @@ static void vfio_listener_region_add(MemoryListener *listener, * they must make sure there are no existing mappings or * loop through existing mappings to map them into VFIO. */ - giommu = g_malloc0(sizeof(*giommu)); + giommu = g_new0(VFIOGuestIOMMU, 1); giommu->iommu = section->mr; giommu->container = container; giommu->n.notify = vfio_iommu_map_notify; @@ -614,7 +614,7 @@ static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as) } /* No suitable VFIOAddressSpace, create a new one */ - space = g_malloc0(sizeof(*space)); + space = g_new0(VFIOAddressSpace, 1); space->as = as; QLIST_INIT(&space->containers); @@ -662,7 +662,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) goto close_fd_exit; } - container = g_malloc0(sizeof(*container)); + container = g_new0(VFIOContainer, 1); container->space = space; container->fd = fd; if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) || @@ -804,7 +804,7 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as) } } - group = g_malloc0(sizeof(*group)); + group = g_new0(VFIOGroup, 1); snprintf(path, sizeof(path), "/dev/vfio/%d", groupid); group->fd = qemu_open(path, O_RDWR); diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index e0e339a..7e2e1ea 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -752,7 +752,7 @@ static void vfio_enable_msix(VFIOPCIDevice *vdev) { vfio_disable_interrupts(vdev); - vdev->msi_vectors = g_malloc0(vdev->msix->entries * sizeof(VFIOMSIVector)); + vdev->msi_vectors = g_new0(VFIOMSIVector, vdev->msix->entries); vdev->interrupt = VFIO_INT_MSIX; @@ -788,7 +788,7 @@ static void vfio_enable_msi(VFIOPCIDevice *vdev) vdev->nr_vectors = msi_nr_vectors_allocated(&vdev->pdev); retry: - vdev->msi_vectors = g_malloc0(vdev->nr_vectors * sizeof(VFIOMSIVector)); + vdev->msi_vectors = g_new0(VFIOMSIVector, vdev->nr_vectors); for (i = 0; i < vdev->nr_vectors; i++) { VFIOMSIVector *vector = &vdev->msi_vectors[i]; @@ -1424,7 +1424,7 @@ static void vfio_vga_probe_ati_3c3_quirk(VFIOPCIDevice *vdev) return; } - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; memory_region_init_io(&quirk->mem, OBJECT(vdev), &vfio_ati_3c3_quirk, quirk, @@ -1458,7 +1458,7 @@ static void vfio_probe_ati_bar4_window_quirk(VFIOPCIDevice *vdev, int nr) return; } - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; quirk->data.address_size = 4; quirk->data.data_offset = 4; @@ -1614,7 +1614,7 @@ static void vfio_probe_rtl8168_bar2_window_quirk(VFIOPCIDevice *vdev, int nr) return; } - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; quirk->data.bar = nr; @@ -1641,7 +1641,7 @@ static void vfio_probe_ati_bar2_4000_quirk(VFIOPCIDevice *vdev, int nr) return; } - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; quirk->data.flags = quirk->data.read_flags = quirk->data.write_flags = 1; quirk->data.address_match = 0x4000; @@ -1772,7 +1772,7 @@ static void vfio_vga_probe_nvidia_3d0_quirk(VFIOPCIDevice *vdev) return; } - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; quirk->data.base_offset = 0x10; quirk->data.address_offset = 4; @@ -1862,7 +1862,7 @@ static void vfio_probe_nvidia_bar5_window_quirk(VFIOPCIDevice *vdev, int nr) return; } - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; quirk->data.read_flags = quirk->data.write_flags = NV_BAR5_VALID; quirk->data.address_offset = 0x8; @@ -1934,7 +1934,7 @@ static void vfio_probe_nvidia_bar0_88000_quirk(VFIOPCIDevice *vdev, int nr) return; } - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; quirk->data.flags = quirk->data.read_flags = quirk->data.write_flags = 1; quirk->data.address_match = 0x88000; @@ -1971,7 +1971,7 @@ static void vfio_probe_nvidia_bar0_1800_quirk(VFIOPCIDevice *vdev, int nr) (unsigned int)(vfio_region_read(&vdev->bars[0].region, 0, 4) >> 20) & 0xff); - quirk = g_malloc0(sizeof(*quirk)); + quirk = g_new0(VFIOQuirk, 1); quirk->vdev = vdev; quirk->data.flags = quirk->data.read_flags = quirk->data.write_flags = 1; quirk->data.address_match = 0x1800; @@ -2245,7 +2245,7 @@ static int vfio_early_setup_msix(VFIOPCIDevice *vdev) table = le32_to_cpu(table); pba = le32_to_cpu(pba); - vdev->msix = g_malloc0(sizeof(*(vdev->msix))); + vdev->msix = g_new0(VFIOMSIXInfo, 1); vdev->msix->table_bar = table & PCI_MSIX_FLAGS_BIRMASK; vdev->msix->table_offset = table & ~PCI_MSIX_FLAGS_BIRMASK; vdev->msix->pba_bar = pba & PCI_MSIX_FLAGS_BIRMASK; @@ -2792,7 +2792,7 @@ static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single) vfio_pci_pre_reset(vdev); vdev->vbasedev.needs_reset = false; - info = g_malloc0(sizeof(*info)); + info = g_new0(struct vfio_pci_hot_reset_info, 1); info->argsz = sizeof(*info); ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO, info); diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c index 9382bb7..c19a0ee 100644 --- a/hw/vfio/platform.c +++ b/hw/vfio/platform.c @@ -46,7 +46,7 @@ static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev, SysBusDevice *sbdev = SYS_BUS_DEVICE(vdev); VFIOINTp *intp; - intp = g_malloc0(sizeof(*intp)); + intp = g_new0(VFIOINTp, 1); intp->vdev = vdev; intp->pin = info.index; intp->flags = info.flags; @@ -352,7 +352,7 @@ static int vfio_populate_device(VFIODevice *vbasedev) struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) }; VFIORegion *ptr; - vdev->regions[i] = g_malloc0(sizeof(VFIORegion)); + vdev->regions[i] = g_new0(VFIORegion, 1); ptr = vdev->regions[i]; reg_info.index = i; ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, ®_info); -- 1.8.3.2