block: Use g_new() & friends more
v3 cover block: Use g_new() & friends more
PATCH 1+2 convert some allocations. While preparing them, I stumbled over dead error handling and some useless casts, which led to PATCH 3+4.
I posted a tree-wide version of PATCH 1 some time ago, and was told to split it up. This is the block part, redone from scratch. Other parts available on request.
v3:
* Manual style cleanups in PATCH 4 [Max, Jeff]
* PATCH 1-3 unchanged.
v2:
* Regenerated with spatch.
* Because of that, I decided not to carry Max's R-by forward.
Markus Armbruster (4):
block: Use g_new() & friends where that makes obvious sense
block: Use g_new() & friends to avoid multiplying sizes
qemu-io-cmds: g_renew() can't fail, bury dead error handling
block: Drop some superfluous casts from void *
block-migration.c | 6 +++--- block.c | 14 +++++++------- block/archipelago.c | 6 +++--- block/bochs.c | 2 +- block/gluster.c | 8 ++++---- block/iscsi.c | 2 +- block/nfs.c | 2 +- block/parallels.c | 2 +- block/qcow.c | 2 +- block/qcow2-cache.c | 2 +- block/qcow2-cluster.c | 2 +- block/qcow2-refcount.c | 8 ++++---- block/qcow2-snapshot.c | 8 ++++---- block/qed-check.c | 3 +-- block/raw-posix.c | 2 +- block/rbd.c | 6 +++--- block/sheepdog.c | 6 +++--- block/vdi.c | 2 +- block/vhdx-log.c | 2 +- block/vhdx.c | 4 ++-- block/vmdk.c | 7 +++---- block/vvfat.c | 10 +++++----- blockdev-nbd.c | 2 +- blockdev.c | 2 +- hw/block/nvme.c | 8 ++++---- hw/ide/ahci.c | 2 +- hw/ide/microdrive.c | 2 +- qemu-io-cmds.c | 21 ++++++--------------- qemu-io.c | 2 +- 29 files changed, 67 insertions(+), 78 deletions(-)
Signature · 1 line
1.9.3
PATCH v3 1/4 block: Use g_new() & friends where that makes obvious sense
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.
Patch created with Coccinelle, with two manual changes on top:
* Add const to bdrv_iterate_format() to keep the types straight
* Convert the allocation in bdrv_drop_intermediate(), which Coccinelle inexplicably misses
Coccinelle semantic patch:
@@
type T;
@@
-g_malloc(sizeof(T))
+g_new(T, 1)
@@
type T;
@@
-g_try_malloc(sizeof(T))
+g_try_new(T, 1)
@@
type T;
@@
-g_malloc0(sizeof(T))
+g_new0(T, 1)
@@
type T;
@@
-g_try_malloc0(sizeof(T))
+g_try_new0(T, 1)
@@
type T;
expression n;
@@
-g_malloc(sizeof(T) * (n))
+g_new(T, n)
@@
type T;
expression n;
@@
-g_try_malloc(sizeof(T) * (n))
+g_try_new(T, n)
@@
type T;
expression n;
@@
-g_malloc0(sizeof(T) * (n))
+g_new0(T, n)
@@
type T;
expression n;
@@
-g_try_malloc0(sizeof(T) * (n))
+g_try_new0(T, n)
@@
type T;
expression p, n;
@@
-g_realloc(p, sizeof(T) * (n))
+g_renew(T, p, n)
@@
type T;
expression p, n;
@@
-g_try_realloc(p, sizeof(T) * (n))
+g_try_renew(T, p, n)--- block-migration.c | 6 +++--- block.c | 14 +++++++------- block/archipelago.c | 6 +++--- block/gluster.c | 8 ++++---- block/iscsi.c | 2 +- block/nfs.c | 2 +- block/qcow.c | 2 +- block/qcow2-cluster.c | 2 +- block/qcow2-refcount.c | 8 ++++---- block/qcow2-snapshot.c | 8 ++++---- block/raw-posix.c | 2 +- block/rbd.c | 4 ++-- block/sheepdog.c | 4 ++-- block/vdi.c | 2 +- block/vhdx.c | 4 ++-- block/vmdk.c | 7 +++---- block/vvfat.c | 2 +- blockdev-nbd.c | 2 +- blockdev.c | 2 +- hw/ide/ahci.c | 2 +- qemu-io-cmds.c | 2 +- qemu-io.c | 2 +- 22 files changed, 46 insertions(+), 47 deletions(-)
block-migration.c +3 -3
@@ -283,7 +283,7 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds) nr_sectors = total_sectors - cur_sector; } - blk =g_malloc (sizeof (BlkMigBlock)); + blk =g_new (BlkMigBlock,1 ); blk->buf =g_malloc (BLOCK_SIZE ); blk->bmds = bmds; blk->sector = cur_sector;
@@ -354,7 +354,7 @@ static void init_blk_migration_it(void *opaque, BlockDriverState *bs)return ; } - bmds =g_malloc0 (sizeof (BlkMigDevState)); + bmds =g_new0 (BlkMigDevState,1 ); bmds->bs = bs; bmds->bulk_completed =0 ; bmds->total_sectors = sectors;
@@ -465,7 +465,7 @@ static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds, }else { nr_sectors =BDRV_SECTORS_PER_DIRTY_CHUNK ; } - blk =g_malloc (sizeof (BlkMigBlock)); + blk =g_new (BlkMigBlock,1 ); blk->buf =g_malloc (BLOCK_SIZE ); blk->bmds = bmds; blk->sector = sector;
block.c +7 -7
@@ -351,7 +351,7 @@ BlockDriverState *bdrv_new(const char *device_name, Error **errp)return NULL ; } - bs =g_malloc0 (sizeof (BlockDriverState)); + bs =g_new0 (BlockDriverState,1 );QLIST_INIT (&bs->dirty_bitmaps);pstrcpy (bs->device_name,sizeof (bs->device_name), device_name);if (device_name[0 ] !='\0' ) {
@@ -2628,7 +2628,7 @@ int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,* into our deletion queue, until we hit the 'base' */ while (intermediate) { - intermediate_state =g_malloc0 (sizeof (BlkIntermediateStates)); + intermediate_state =g_new0 (BlkIntermediateStates,1 ); intermediate_state->bs = intermediate;QSIMPLEQ_INSERT_TAIL (&states_to_delete, intermediate_state, entry);
@@ -3755,7 +3755,7 @@ void bdrv_iterate_format(void (*it)(void *opaque, const char *name), }if (!found) { - formats =g_realloc (formats, (count +1 ) *sizeof (char *)); + formats = g_renew(const char *, formats, count + 1); formats[count++] = drv->format_name;it (opaque, drv->format_name); }
@@ -5330,7 +5330,7 @@ BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity, errno = -bitmap_size;return NULL ; } - bitmap =g_malloc0 (sizeof (BdrvDirtyBitmap)); + bitmap =g_new0 (BdrvDirtyBitmap,1 ); bitmap->bitmap =hbitmap_alloc (bitmap_size,ffs (granularity) -1 );QLIST_INSERT_HEAD (&bs->dirty_bitmaps, bitmap, list);return bitmap;
@@ -5356,8 +5356,8 @@ BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)BlockDirtyInfoList **plist = &list; QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { -BlockDirtyInfo *info =g_malloc0 (sizeof (BlockDirtyInfo)); -BlockDirtyInfoList *entry =g_malloc0 (sizeof (BlockDirtyInfoList)); +BlockDirtyInfo *info =g_new0 (BlockDirtyInfo,1 ); +BlockDirtyInfoList *entry =g_new0 (BlockDirtyInfoList,1 ); info->count =bdrv_get_dirty_count (bs, bm); info->granularity = ((int64_t )BDRV_SECTOR_SIZE <<hbitmap_granularity (bm->bitmap));
@@ -5451,7 +5451,7 @@ void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)BdrvOpBlocker *blocker;assert ((int ) op >=0 && op <BLOCK_OP_TYPE_MAX ); - blocker =g_malloc0 (sizeof (BdrvOpBlocker)); + blocker =g_new0 (BdrvOpBlocker,1 ); blocker->reason = reason;QLIST_INSERT_HEAD (&bs->op_blockers[op], blocker, list); }
block/archipelago.c +3 -3
@@ -750,7 +750,7 @@ static int archipelago_submit_request(BDRVArchipelagoState *s,char *target;void *data =NULL ;struct xseg_request *req; -AIORequestData *reqdata =g_malloc (sizeof (AIORequestData)); +AIORequestData *reqdata =g_new (AIORequestData,1 ); targetlen =strlen (s->volname); req =xseg_get_request (s->xseg, s->srcport, s->vportno,X_ALLOC );
@@ -827,7 +827,7 @@ static int archipelago_aio_segmented_rw(BDRVArchipelagoState *s,int i, ret, segments_nr, last_segment_size;ArchipelagoSegmentedRequest *segreq; - segreq =g_malloc (sizeof (ArchipelagoSegmentedRequest)); + segreq =g_new (ArchipelagoSegmentedRequest,1 );if (op ==ARCHIP_OP_FLUSH ) { segments_nr =1 ;
@@ -960,7 +960,7 @@ static int64_t archipelago_volume_info(BDRVArchipelagoState *s)int ret, targetlen;struct xseg_request *req;struct xseg_reply_info *xinfo; -AIORequestData *reqdata =g_malloc (sizeof (AIORequestData)); +AIORequestData *reqdata =g_new (AIORequestData,1 );const char *volname = s->volname; targetlen =strlen (volname);
block/gluster.c +4 -4
@@ -291,7 +291,7 @@ static int qemu_gluster_open(BlockDriverState *bs, QDict *options,BDRVGlusterState *s = bs->opaque;int open_flags =0 ;int ret =0 ; -GlusterConf *gconf =g_malloc0 (sizeof (GlusterConf)); +GlusterConf *gconf =g_new0 (GlusterConf,1 );QemuOpts *opts;Error *local_err =NULL ;const char *filename;
@@ -351,12 +351,12 @@ static int qemu_gluster_reopen_prepare(BDRVReopenState *state,assert (state !=NULL );assert (state->bs !=NULL ); - state->opaque =g_malloc0 (sizeof (BDRVGlusterReopenState)); + state->opaque =g_new0 (BDRVGlusterReopenState,1 ); reop_s = state->opaque;qemu_gluster_parse_flags (state->flags, &open_flags); - gconf =g_malloc0 (sizeof (GlusterConf)); + gconf =g_new0 (GlusterConf,1 ); reop_s->glfs =qemu_gluster_init (gconf, state->bs->filename, errp);if (reop_s->glfs ==NULL ) {
@@ -486,7 +486,7 @@ static int qemu_gluster_create(const char *filename,int prealloc =0 ;int64_t total_size =0 ;char *tmp =NULL ; -GlusterConf *gconf =g_malloc0 (sizeof (GlusterConf)); +GlusterConf *gconf =g_new0 (GlusterConf,1 ); glfs =qemu_gluster_init (gconf, filename, errp);if (!glfs) {
block/iscsi.c +1 -1
@@ -1532,7 +1532,7 @@ static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)/* Read out options */ total_size =qemu_opt_get_size_del (opts,BLOCK_OPT_SIZE ,0 ) /BDRV_SECTOR_SIZE ; - bs->opaque =g_malloc0 (sizeof (struct IscsiLun )); + bs->opaque = g_new0(struct IscsiLun, 1); iscsilun = bs->opaque; bs_options =qdict_new ();
block/nfs.c +1 -1
@@ -409,7 +409,7 @@ static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp) {int ret =0 ;int64_t total_size =0 ; -NFSClient *client =g_malloc0 (sizeof (NFSClient)); +NFSClient *client =g_new0 (NFSClient,1 ); client->aio_context =qemu_get_aio_context ();
block/qcow.c +1 -1
@@ -182,7 +182,7 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags, } s->l1_table_offset = header.l1_table_offset; - s->l1_table =g_try_malloc (s->l1_size *sizeof (uint64_t )); + s->l1_table =g_try_new (uint64_t, s->l1_size);if (s->l1_table ==NULL ) {error_setg (errp,"Could not allocate memory for L1 table" ); ret = -ENOMEM ;
block/qcow2-cluster.c +1 -1
@@ -711,7 +711,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)trace_qcow2_cluster_link_l2 (qemu_coroutine_self (), m->nb_clusters);assert (m->nb_clusters >0 ); - old_cluster =g_try_malloc (m->nb_clusters *sizeof (uint64_t )); + old_cluster =g_try_new (uint64_t, m->nb_clusters);if (old_cluster ==NULL ) { ret = -ENOMEM ; goto err;
block/qcow2-refcount.c +4 -4
@@ -350,7 +350,7 @@ static int alloc_refcount_block(BlockDriverState *bs,uint64_t meta_offset = (blocks_used * refcount_block_clusters) * s->cluster_size;uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size; -uint64_t *new_table =g_try_malloc0 (table_size *sizeof (uint64_t )); +uint64_t *new_table =g_try_new0 (uint64_t, table_size);uint16_t *new_blocks =g_try_malloc0 (blocks_clusters * s->cluster_size);assert (table_size >0 && blocks_clusters >0 );
@@ -1524,7 +1524,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,return -EFBIG ; } - refcount_table =g_try_malloc0 (nb_clusters *sizeof (uint16_t )); + refcount_table =g_try_new0 (uint16_t, nb_clusters);if (nb_clusters && refcount_table ==NULL ) { res->check_errors++;return -ENOMEM ;
@@ -1605,8 +1605,8 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,/* increase refcount_table size if necessary */ int old_nb_clusters = nb_clusters; nb_clusters = (new_offset >> s->cluster_bits) +1 ; - refcount_table =g_realloc (refcount_table, - nb_clusters *sizeof (uint16_t )); + refcount_table =g_renew (uint16_t, refcount_table, + nb_clusters);memset (&refcount_table[old_nb_clusters],0 , (nb_clusters - old_nb_clusters) *sizeof (uint16_t )); }
block/qcow2-snapshot.c +4 -4
@@ -58,7 +58,7 @@ int qcow2_read_snapshots(BlockDriverState *bs) } offset = s->snapshots_offset; - s->snapshots =g_malloc0 (s->nb_snapshots *sizeof (QCowSnapshot)); + s->snapshots =g_new0 (QCowSnapshot, s->nb_snapshots);for (i =0 ; i < s->nb_snapshots; i++) {/* Read statically sized part of the snapshot header */
@@ -381,7 +381,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) sn->l1_table_offset = l1_table_offset; sn->l1_size = s->l1_size; - l1_table =g_try_malloc (s->l1_size *sizeof (uint64_t )); + l1_table =g_try_new (uint64_t, s->l1_size);if (s->l1_size && l1_table ==NULL ) { ret = -ENOMEM ; goto fail;
@@ -417,7 +417,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) }/* Append the new snapshot to the snapshot list */ - new_snapshot_list =g_malloc ((s->nb_snapshots +1 ) *sizeof (QCowSnapshot)); + new_snapshot_list =g_new (QCowSnapshot, s->nb_snapshots +1 );if (s->snapshots) {memcpy (new_snapshot_list, s->snapshots, s->nb_snapshots *sizeof (QCowSnapshot));
@@ -661,7 +661,7 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)return s->nb_snapshots; } - sn_tab =g_malloc0 (s->nb_snapshots *sizeof (QEMUSnapshotInfo)); + sn_tab =g_new0 (QEMUSnapshotInfo, s->nb_snapshots);for (i =0 ; i < s->nb_snapshots; i++) { sn_info = sn_tab + i; sn = s->snapshots + i;
block/raw-posix.c +1 -1
@@ -517,7 +517,7 @@ static int raw_reopen_prepare(BDRVReopenState *state, s = state->bs->opaque; - state->opaque =g_malloc0 (sizeof (BDRVRawReopenState)); + state->opaque =g_new0 (BDRVRawReopenState,1 ); raw_s = state->opaque;#ifdef CONFIG_LINUX_AIO
block/rbd.c +2 -2
@@ -652,7 +652,7 @@ static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs, off = sector_num *BDRV_SECTOR_SIZE ; size = nb_sectors *BDRV_SECTOR_SIZE ; - rcb =g_malloc (sizeof (RADOSCB )); + rcb =g_new (RADOSCB ,1 ); rcb->done =0 ; rcb->acb = acb; rcb->buf = buf;
@@ -873,7 +873,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs, goto done; } - sn_tab =g_malloc0 (snap_count *sizeof (QEMUSnapshotInfo)); + sn_tab =g_new0 (QEMUSnapshotInfo, snap_count);for (i =0 ; i < snap_count; i++) {const char *snap_name = snaps[i].name;
block/sheepdog.c +2 -2
@@ -1682,7 +1682,7 @@ static int sd_create(const char *filename, QemuOpts *opts,uint32_t snapid;bool prealloc = false; - s =g_malloc0 (sizeof (BDRVSheepdogState)); + s =g_new0 (BDRVSheepdogState,1 );memset (tag,0 ,sizeof (tag));if (strstr (filename,"://" )) {
@@ -2273,7 +2273,7 @@ static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)uint32_t snapid =0 ;int ret =0 ; - old_s =g_malloc (sizeof (BDRVSheepdogState)); + old_s =g_new (BDRVSheepdogState,1 );memcpy (old_s, s,sizeof (BDRVSheepdogState));
block/vdi.c +1 -1
@@ -292,7 +292,7 @@ static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,return -ENOTSUP ; } - bmap =g_try_malloc (s->header.blocks_in_image *sizeof (uint32_t )); + bmap =g_try_new (uint32_t, s->header.blocks_in_image);if (s->header.blocks_in_image && bmap ==NULL ) { res->check_errors++;return -ENOMEM ;
block/vhdx.c +2 -2
@@ -1381,7 +1381,7 @@ static int vhdx_create_new_headers(BlockDriverState *bs, uint64_t image_size,int ret =0 ;VHDXHeader *hdr =NULL ; - hdr =g_malloc0 (sizeof (VHDXHeader)); + hdr =g_new0 (VHDXHeader,1 ); hdr->signature =VHDX_HEADER_SIGNATURE ; hdr->sequence_number =g_random_int ();
@@ -1654,7 +1654,7 @@ static int vhdx_create_new_region_table(BlockDriverState *bs,/* Populate enough of the BDRVVHDXState to be able to use the * pre-existing BAT calculation, translation, and update functions */ - s =g_malloc0 (sizeof (BDRVVHDXState)); + s =g_new0 (BDRVVHDXState,1 ); s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK ) * (uint64_t ) sector_size / (uint64_t ) block_size;
block/vmdk.c +3 -4
@@ -233,7 +233,7 @@ static void vmdk_free_last_extent(BlockDriverState *bs)return ; } s->num_extents--; - s->extents =g_realloc (s->extents, s->num_extents *sizeof (VmdkExtent)); + s->extents =g_renew (VmdkExtent, s->extents, s->num_extents); }static uint32_t vmdk_read_cid (BlockDriverState *bs,int parent)
@@ -418,8 +418,7 @@ static int vmdk_add_extent(BlockDriverState *bs,return length; } - s->extents =g_realloc (s->extents, - (s->num_extents +1 ) *sizeof (VmdkExtent)); + s->extents =g_renew (VmdkExtent, s->extents, s->num_extents +1 ); extent = &s->extents[s->num_extents]; s->num_extents++;
@@ -497,7 +496,7 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent, } extent->l2_cache = -g_malloc (extent->l2_size *L2_CACHE_SIZE *sizeof (uint32_t )); +g_new (uint32_t, extent->l2_size *L2_CACHE_SIZE );return 0 ; fail_l1b:g_free (extent->l1_backup_table);
block/vvfat.c +1 -1
@@ -2950,7 +2950,7 @@ static int enable_write_target(BDRVVVFATState *s, Error **errp)bdrv_set_backing_hd (s->bs,bdrv_new ("" , &error_abort)); s->bs->backing_hd->drv = &vvfat_write_target; - s->bs->backing_hd->opaque =g_malloc (sizeof (void *)); + s->bs->backing_hd->opaque = g_new(void *, 1); *(void **)s->bs->backing_hd->opaque = s;return 0 ;
blockdev-nbd.c +1 -1
@@ -108,7 +108,7 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,nbd_export_set_name (exp, device); - n =g_malloc0 (sizeof (NBDCloseNotifier)); + n =g_new0 (NBDCloseNotifier,1 ); n->n.notify = nbd_close_notifier; n->exp = exp;bdrv_add_close_notifier (bs, &n->n);
blockdev.c +1 -1
@@ -1094,7 +1094,7 @@ SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,return NULL ; } - info =g_malloc0 (sizeof (SnapshotInfo)); + info =g_new0 (SnapshotInfo,1 ); info->id =g_strdup (sn.id_str); info->name =g_strdup (sn.name); info->date_nsec = sn.date_nsec;
hw/ide/ahci.c +1 -1
@@ -1203,7 +1203,7 @@ void ahci_init(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports) s->as = as; s->ports = ports; - s->dev =g_malloc0 (sizeof (AHCIDevice) * ports); + s->dev =g_new0 (AHCIDevice, ports);ahci_reg_init (s);/* XXX BAR size should be 1k, but that breaks, so bump it to 4k for now */ memory_region_init_io(&s->mem, OBJECT(qdev), &ahci_mem_ops, s,
qemu-io-cmds.c +1 -1
@@ -114,7 +114,7 @@ static char **breakline(char *input, int *count) {int c =0 ;char *p; -char **rval =g_malloc0 (sizeof (char *)); + char **rval = g_new0(char *, 1);char **tmp;while (rval && (p =qemu_strsep (&input," " )) !=NULL ) {
qemu-io.c +1 -1
@@ -356,7 +356,7 @@ static void command_loop(void)static void add_user_command (char *optarg) { - cmdline =g_realloc (cmdline, ++ncmdline *sizeof (char *)); + cmdline = g_renew(char *, cmdline, ++ncmdline); cmdline[ncmdline-1 ] = optarg; }
Signature · 1 line
1.9.3
PATCH v3 2/4 block: Use g_new() & friends to avoid multiplying sizes
In reply to Markus Armbrusterg_new(T, n) is safer than g_malloc(sizeof(*v) * n) 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.
Perhaps a conversion to g_malloc_n() would be neater in places, but that's merely four years old, and we can't use such newfangled stuff.
This commit only touches allocations with size arguments of the form sizeof(T), plus two that use 4 instead of sizeof(uint32_t). We can make the others safe by converting to g_malloc_n() when it becomes available to us in a couple of years.
--- block/bochs.c | 2 +- block/parallels.c | 2 +- block/qcow2-cache.c | 2 +- block/qed-check.c | 3 +-- block/rbd.c | 2 +- block/sheepdog.c | 2 +- hw/block/nvme.c | 8 ++++---- qemu-io-cmds.c | 10 +++++----- 8 files changed, 15 insertions(+), 16 deletions(-)
block/bochs.c +1 -1
@@ -131,7 +131,7 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,return -EFBIG ; } - s->catalog_bitmap =g_try_malloc (s->catalog_size *4 ); + s->catalog_bitmap =g_try_new (uint32_t, s->catalog_size);if (s->catalog_size && s->catalog_bitmap ==NULL ) {error_setg (errp,"Could not allocate memory for catalog" );return -ENOMEM ;
block/parallels.c +1 -1
@@ -121,7 +121,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, ret = -EFBIG ; goto fail; } - s->catalog_bitmap =g_try_malloc (s->catalog_size *4 ); + s->catalog_bitmap =g_try_new (uint32_t, s->catalog_size);if (s->catalog_size && s->catalog_bitmap ==NULL ) { ret = -ENOMEM ; goto fail;
block/qcow2-cache.c +1 -1
@@ -50,7 +50,7 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) c =g_malloc0 (sizeof (*c)); c->size = num_tables; - c->entries =g_malloc0 (sizeof (*c->entries) * num_tables); + c->entries =g_new0 (Qcow2CachedTable, num_tables);for (i =0 ; i < c->size; i++) { c->entries[i].table =qemu_try_blockalign (bs->file, s->cluster_size);
block/qed-check.c +1 -2
@@ -227,8 +227,7 @@ int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix) };int ret; - check.used_clusters =g_try_malloc0 (((check.nclusters +31 ) /32 ) * -sizeof (check.used_clusters[0 ])); + check.used_clusters =g_try_new0 (uint32_t, (check.nclusters +31 ) /32 );if (check.nclusters && check.used_clusters ==NULL ) {return -ENOMEM ; }
block/rbd.c +1 -1
@@ -862,7 +862,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs,int max_snaps =RBD_MAX_SNAPS ; do { - snaps = g_malloc(sizeof(*snaps) * max_snaps); + snaps = g_new(rbd_snap_info_t, max_snaps); snap_count = rbd_snap_list(s->image, snaps, &max_snaps); if (snap_count <= 0) { g_free(snaps);
block/sheepdog.c +1 -1
@@ -2357,7 +2357,7 @@ static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) goto out; } - sn_tab =g_malloc0 (nr *sizeof (*sn_tab)); + sn_tab =g_new0 (QEMUSnapshotInfo, nr);/* calculate a vdi id with hash function */ hval =fnv_64a_buf (s->name,strlen (s->name),FNV1A_64_INIT );
hw/block/nvme.c +4 -4
@@ -319,7 +319,7 @@ static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr, sq->size = size; sq->cqid = cqid; sq->head = sq->tail =0 ; - sq->io_req =g_malloc (sq->size *sizeof (*sq->io_req)); + sq->io_req =g_new (NvmeRequest, sq->size);QTAILQ_INIT (&sq->req_list);QTAILQ_INIT (&sq->out_req_list);
@@ -773,9 +773,9 @@ static int nvme_init(PCIDevice *pci_dev) n->reg_size =1 <<qemu_fls (0x1004 +2 * (n->num_queues +1 ) *4 ); n->ns_size = bs_size / (uint64_t )n->num_namespaces; - n->namespaces =g_malloc0 (sizeof (*n->namespaces)*n->num_namespaces); - n->sq =g_malloc0 (sizeof (*n->sq)*n->num_queues); - n->cq =g_malloc0 (sizeof (*n->cq)*n->num_queues); + n->namespaces =g_new0 (NvmeNamespace, n->num_namespaces); + n->sq = g_new0(NvmeSQueue *, n->num_queues); + n->cq = g_new0(NvmeCQueue *, n->num_queues);memory_region_init_io (&n->iomem,OBJECT (n), &nvme_mmio_ops, n,"nvme" , n->reg_size);
qemu-io-cmds.c +5 -5
@@ -29,7 +29,7 @@ static int compare_cmdname(const void *a, const void *b)void qemuio_add_command (const cmdinfo_t *ci) { - cmdtab =g_realloc (cmdtab, ++ncmds *sizeof (*cmdtab)); + cmdtab =g_renew (cmdinfo_t, cmdtab, ++ncmds); cmdtab[ncmds -1 ] = *ci;qsort (cmdtab, ncmds,sizeof (*cmdtab), compare_cmdname); }
@@ -122,7 +122,7 @@ static char **breakline(char *input, int *count)continue ; } c++; - tmp =g_realloc (rval,sizeof (*rval) * (c +1 )); + tmp = g_renew(char *, rval, (c + 1));if (!tmp) {g_free (rval); rval =NULL ;
@@ -1264,9 +1264,9 @@ static int multiwrite_f(BlockDriverState *bs, int argc, char **argv) } } - reqs =g_malloc0 (nr_reqs *sizeof (*reqs)); - buf =g_malloc0 (nr_reqs *sizeof (*buf)); - qiovs =g_malloc (nr_reqs *sizeof (*qiovs)); + reqs =g_new0 (BlockRequest, nr_reqs); + buf = g_new0(char *, nr_reqs); + qiovs =g_new (QEMUIOVector, nr_reqs);for (i =0 ; i < nr_reqs && optind < argc; i++) {int j;
Signature · 1 line
1.9.3
PATCH v3 3/4 qemu-io-cmds: g_renew() can't fail, bury dead error handling
In reply to Markus Armbruster--- qemu-io-cmds.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
qemu-io-cmds.c +1 -10
@@ -115,22 +115,13 @@ static char **breakline(char *input, int *count)int c =0 ;char *p; char **rval = g_new0(char *, 1); -char **tmp;while (rval && (p =qemu_strsep (&input," " )) !=NULL ) {if (!*p) {continue ; } c++; - tmp = g_renew(char *, rval, (c + 1)); -if (!tmp) { -g_free (rval); - rval =NULL ; - c =0 ; -break ; - }else { - rval = tmp; - } + rval = g_renew(char *, rval, (c + 1)); rval[c -1 ] = p; rval[c] =NULL ; }
Signature · 1 line
1.9.3
PATCH v3 4/4 block: Drop some superfluous casts from void *
In reply to Markus ArmbrusterThey clutter the code. Unfortunately, I can't figure out how to make Coccinelle drop all of them, so I have to settle for common special cases:
@@
type T;
T *pt;
void *pv;
@@
- pt = (T *)pv;
+ pt = pv;
@@
type T;
@@
- (T *)
(\(g_malloc\|g_malloc0\|g_realloc\|g_new\|g_new0\|g_renew\|
g_try_malloc\|g_try_malloc0\|g_try_realloc\|
g_try_new\|g_try_new0\|g_try_renew\)(...))Topped off with minor manual style cleanups.
--- block/vhdx-log.c | 2 +- block/vvfat.c | 8 ++++---- hw/ide/microdrive.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)
block/vhdx-log.c +1 -1
@@ -923,7 +923,7 @@ static int vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s, buffer =qemu_blockalign (bs, total_length);memcpy (buffer, &new_hdr,sizeof (new_hdr)); - new_desc = (VHDXLogDescriptor *) (buffer +sizeof (new_hdr)); + new_desc = buffer +sizeof (new_hdr); data_sector = buffer + (desc_sectors *VHDX_LOG_SECTOR_SIZE ); data_tmp = data;
block/vvfat.c +4 -4
@@ -732,7 +732,7 @@ static int read_directory(BDRVVVFATState* s, int mapping_index)if (first_cluster ==0 && (is_dotdot || is_dot))continue ; - buffer=(char *)g_malloc (length); + buffer =g_malloc (length);snprintf (buffer,length,"%s/%s" ,dirname,entry->d_name);if (stat (buffer,&st)<0 ) {
@@ -767,7 +767,7 @@ static int read_directory(BDRVVVFATState* s, int mapping_index)/* create mapping for this file */ if (!is_dot && !is_dotdot && (S_ISDIR (st.st_mode) || st.st_size)) { - s->current_mapping=(mapping_t *)array_get_next (&(s->mapping)); + s->current_mapping =array_get_next (&(s->mapping)); s->current_mapping->begin=0 ; s->current_mapping->end=st.st_size;/*
@@ -811,12 +811,12 @@ static int read_directory(BDRVVVFATState* s, int mapping_index) }/* reget the mapping, since s->mapping was possibly realloc()ed */ - mapping = (mapping_t *)array_get (&(s->mapping), mapping_index); + mapping =array_get (&(s->mapping), mapping_index); first_cluster += (s->directory.next - mapping->info.dir.first_dir_index) *0x20 / s->cluster_size; mapping->end = first_cluster; - direntry = (direntry_t *)array_get (&(s->directory), mapping->dir_index); + direntry =array_get (&(s->directory), mapping->dir_index);set_begin_of_direntry (direntry, mapping->begin);return 0 ;
hw/ide/microdrive.c +1 -1
@@ -567,7 +567,7 @@ PCMCIACardState *dscm1xxxx_init(DriveInfo *dinfo) } md->bus.ifs[0 ].drive_kind =IDE_CFATA ; md->bus.ifs[0 ].mdata_size =METADATA_SIZE ; - md->bus.ifs[0 ].mdata_storage = (uint8_t *)g_malloc0 (METADATA_SIZE ); + md->bus.ifs[0 ].mdata_storage =g_malloc0 (METADATA_SIZE );return PCMCIA_CARD (md); }
Signature · 1 line
1.9.3
Kevin Wolf
AppliedAm 19.08.2014 um 10:31 hat Markus Armbruster geschrieben:
Quoted 15 lines
PATCH 1+2 convert some allocations. While preparing them, I stumbled over dead error handling and some useless casts, which led to PATCH 3+4.
I posted a tree-wide version of PATCH 1 some time ago, and was told to split it up. This is the block part, redone from scratch. Other parts available on request.
v3:
* Manual style cleanups in PATCH 4 [Max, Jeff]
* PATCH 1-3 unchanged.
v2:
* Regenerated with spatch.
* Because of that, I decided not to carry Max's R-by forward.
Quoted 5 lines
Markus Armbruster (4):
block: Useg_new()& friends where that makes obvious sense
block: Useg_new()& friends to avoid multiplying sizes
qemu-io-cmds:g_renew()can't fail, bury dead error handling
block: Drop some superfluous casts from void *
Thanks, applied all to the block branch.
Kevin