From 5839e53bbc0fec56021d758aab7610df421ed8c8 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 19 Aug 2014 10:31:08 +0200 Subject: 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) Signed-off-by: Markus Armbruster Reviewed-by: Max Reitz Reviewed-by: Jeff Cody Signed-off-by: Kevin Wolf --- block/archipelago.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'block/archipelago.c') diff --git a/block/archipelago.c b/block/archipelago.c index 6629d03a1d..34f72dc5a5 100644 --- a/block/archipelago.c +++ b/block/archipelago.c @@ -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); -- cgit v1.2.1