Skip to contentSkip to conversation

hw/vfio/platform: replace g_malloc0_n by g_malloc0

PATCH Superseded Eric Auger 3 participants 4 messages Jun 11, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

PATCH 1/1 hw/vfio/platform: replace g_malloc0_n by g_malloc0

g_malloc0_n() is introduced since glib-2.24 while QEMU currently requires glib-2.22. This may cause a link error on some distributions.

---
 hw/vfio/platform.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/vfio/platform.c +1 -2
index 35266a8..c0acc8b 100644
@@ -346,8 +346,7 @@ static int vfio_populate_device(VFIODevice *vbasedev)
         return ret;
     }
 
-    vdev->regions = g_malloc0_n(vbasedev->num_regions,
-                                sizeof(VFIORegion *));
+    vdev->regions = g_malloc0(vbasedev->num_regions * sizeof(VFIORegion *));
 
     for (i = 0; i < vbasedev->num_regions; i++) {
         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
Signature · 1 line
1.8.3.2
#1 lore raw
Quoted 13 lines · Eric Auger

g_malloc0_n() is introduced since glib-2.24 while QEMU currently requires glib-2.22. This may cause a link error on some distributions.

---
 hw/vfio/platform.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/vfio/platform.c +0 -0
index 35266a8..c0acc8b 100644

hw/vfio/platform.c static int vfio_populate_device(VFIODevice *vbasedev) · line 350

Quoted 7 lines
hw/vfio/platform.c +1 -2
@@ -346,8 +346,7 @@ static int vfio_populate_device(VFIODevice *vbasedev)
         return ret;
     }
 
-    vdev->regions = g_malloc0_n(vbasedev->num_regions,
-                                sizeof(VFIORegion *));
+    vdev->regions = g_malloc0(vbasedev->num_regions * sizeof(VFIORegion *));
 

Why not use g_new0() which returned pointer is cast to the given type?

Regards,
-Gonglei

Quoted 3 lines
hw/vfio/platform.c +0 -0
     for (i = 0; i < vbasedev->num_regions; i++) {
         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
author
#2 lore raw

Hi Gonglei,

Quoted 17 lines · Gonglei
Quoted 16 lines · Eric Auger

g_malloc0_n() is introduced since glib-2.24 while QEMU currently requires glib-2.22. This may cause a link error on some distributions.

---
 hw/vfio/platform.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/vfio/platform.c +0 -0
index 35266a8..c0acc8b 100644
@@ -346,8 +346,7 @@ static int vfio_populate_device(VFIODevice *vbasedev)
         return ret;
     }
 
Quoted 6 lines

hw/vfio/platform.c static int vfio_populate_device(VFIODevice *vbasedev) · line 350

Quoted 4 lines
hw/vfio/platform.c +1 -2
-    vdev->regions = g_malloc0_n(vbasedev->num_regions,
-                                sizeof(VFIORegion *));
+    vdev->regions = g_malloc0(vbasedev->num_regions * sizeof(VFIORegion *));
 

Why not use g_new0() which returned pointer is cast to the given type?

Looks relevant indeed.

g_malloc0 is used in similar situations in rest of the VFIO code so I posted a new version and let Alex choose among both.

Thanks

Eric

Quoted 8 lines

Regards,
-Gonglei

Quoted 3 lines
hw/vfio/platform.c +0 -0
     for (i = 0; i < vbasedev->num_regions; i++) {
         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
#3 lore raw
In reply to Eric Auger
Quoted 13 lines · Eric Auger

g_malloc0_n() is introduced since glib-2.24 while QEMU currently requires glib-2.22. This may cause a link error on some distributions.

---
 hw/vfio/platform.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/vfio/platform.c +0 -0
index 35266a8..c0acc8b 100644

hw/vfio/platform.c static int vfio_populate_device(VFIODevice *vbasedev) · line 352

Quoted 9 lines
hw/vfio/platform.c +1 -2
@@ -346,8 +346,7 @@ static int vfio_populate_device(VFIODevice *vbasedev)
         return ret;
     }
 
-    vdev->regions = g_malloc0_n(vbasedev->num_regions,
-                                sizeof(VFIORegion *));
+    vdev->regions = g_malloc0(vbasedev->num_regions * sizeof(VFIORegion *));
 
     for (i = 0; i < vbasedev->num_regions; i++) {
         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };

Please use g_new0(VFIORegion, vbasedev->num_regions) for extra compile-time checking and integer overflow protection. See commits 58889fe net: Use g_new() & friends where that makes obvious sense 3c55fe2 scsi: Use g_new() & friends where that makes obvious sense ab3ad07 x86: Use g_new() & friends where that makes obvious sense 3ba235a block: Use g_new0() for a bit of extra type checking e1cf558 util: Use g_new() & friends where that makes obvious sense 02c4f26 block: Use g_new() & friends to avoid multiplying sizes 5839e53 block: Use g_new() & friends where that makes obvious sense