fio: replace malloc+memset with calloc
authorVincent Fu <vincent.fu@samsung.com>
Sat, 15 Apr 2023 00:52:47 +0000 (00:52 +0000)
committerVincent Fu <vincent.fu@samsung.com>
Thu, 20 Apr 2023 13:43:30 +0000 (09:43 -0400)
Clean up the code base by replacing malloc+memset with calloc. This
patch was generated from the Coccinelle script below.

The script below is inspired by similar scripts used elsewhere:
https://lore.kernel.org/linux-btrfs/cover.1443546000.git.silvio.fricke@gmail.com/
https://github.com/coccinelle/coccinellery/blob/master/simple_kzalloc/simple_kzalloc1.cocci

@@
expression x,y;
statement s;
type T;
@@

-x = malloc(y * sizeof(T));
+x = calloc(y, sizeof(T));
(
if (!x) s
|
if (x == NULL) s
|
)
-memset(x, 0, y * sizeof(T));

@@
expression x,y,z;
statement s;
@@

-x = malloc(y * sizeof(z));
+x = calloc(y, sizeof(z));
(
if (!x) s
|
if (x == NULL) s
|
)
-memset(x, 0, y * sizeof(z));

@@
expression e,x;
statement s;
@@

-x = malloc(e);
+x = calloc(1, e);
(
if (!x) s
|
if (x == NULL) s
|
)
-memset(x, 0, e);

Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
20 files changed:
client.c
engines/e4defrag.c
engines/io_uring.c
engines/libhdfs.c
engines/libiscsi.c
engines/net.c
engines/nfs.c
engines/null.c
engines/posixaio.c
engines/rdma.c
engines/solarisaio.c
engines/sync.c
eta.c
filesetup.c
gfio.c
graph.c
init.c
t/io_uring.c
t/lfsr-test.c
verify.c

index 51496c770248a2d67466a97ad7529d8d4ec68e82..7cd2ba66e1548cb9c84cbc43958c478d752386d8 100644 (file)
--- a/client.c
+++ b/client.c
@@ -369,8 +369,7 @@ static struct fio_client *get_new_client(void)
 {
        struct fio_client *client;
 
-       client = malloc(sizeof(*client));
-       memset(client, 0, sizeof(*client));
+       client = calloc(1, sizeof(*client));
 
        INIT_FLIST_HEAD(&client->list);
        INIT_FLIST_HEAD(&client->hash_list);
@@ -793,8 +792,7 @@ static int __fio_client_send_remote_ini(struct fio_client *client,
        dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
 
        p_size = sizeof(*pdu) + strlen(filename) + 1;
-       pdu = malloc(p_size);
-       memset(pdu, 0, p_size);
+       pdu = calloc(1, p_size);
        pdu->name_len = strlen(filename);
        strcpy((char *) pdu->file, filename);
        pdu->client_type = cpu_to_le16((uint16_t) client->type);
index 0a0004d0477fc795dd415d417a6c935f4cfb3131..37cc2ada817dad98faa6968c9d3c8b2e8d0a8af9 100644 (file)
@@ -77,12 +77,11 @@ static int fio_e4defrag_init(struct thread_data *td)
                return 1;
        }
 
-       ed = malloc(sizeof(*ed));
+       ed = calloc(1, sizeof(*ed));
        if (!ed) {
                td_verror(td, ENOMEM, "io_queue_init");
                return 1;
        }
-       memset(ed, 0 ,sizeof(*ed));
 
        if (td->o.directory)
                len = sprintf(donor_name, "%s/", td->o.directory);
index 7f743c2a88ade6e964edda2a6f6f306db6299f39..f5ffe9f448309de8fdd533313fd14e1de981ffe6 100644 (file)
@@ -800,11 +800,10 @@ static void fio_ioring_probe(struct thread_data *td)
        /* default to off, as that's always safe */
        o->nonvectored = 0;
 
-       p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
+       p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
        if (!p)
                return;
 
-       memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
        ret = syscall(__NR_io_uring_register, ld->ring_fd,
                        IORING_REGISTER_PROBE, p, 256);
        if (ret < 0)
index f20e45cac1c466dcd659071dd5fb5ea9936727c0..d0a268408435418f261616a734f335b03aa91286 100644 (file)
@@ -315,8 +315,7 @@ static int fio_hdfsio_setup(struct thread_data *td)
        uint64_t file_size, total_file_size;
 
        if (!td->io_ops_data) {
-               hd = malloc(sizeof(*hd));
-               memset(hd, 0, sizeof(*hd));
+               hd = calloc(1, sizeof(*hd));
                
                hd->curr_file_id = -1;
 
index c97b5709ae779eda9af451c0418d352ad365e772..37c9b55a91c932f259bbc1698aae507b19ee0419 100644 (file)
@@ -68,8 +68,7 @@ static int fio_iscsi_setup_lun(struct iscsi_info *iscsi_info,
        struct scsi_readcapacity16      *rc16       = NULL;
        int                              ret        = 0;
 
-       iscsi_lun = malloc(sizeof(struct iscsi_lun));
-       memset(iscsi_lun, 0, sizeof(struct iscsi_lun));
+       iscsi_lun = calloc(1, sizeof(struct iscsi_lun));
 
        iscsi_lun->iscsi_info = iscsi_info;
 
index c6cec5845aac48a3a8643b88c89b5f27fcdb8995..fec53d74175eedb5d4dd3be6e7a68ece50bb629f 100644 (file)
@@ -1370,9 +1370,7 @@ static int fio_netio_setup(struct thread_data *td)
        }
 
        if (!td->io_ops_data) {
-               nd = malloc(sizeof(*nd));
-
-               memset(nd, 0, sizeof(*nd));
+               nd = calloc(1, sizeof(*nd));
                nd->listenfd = -1;
                nd->pipes[0] = nd->pipes[1] = -1;
                td->io_ops_data = nd;
index 336e670bf801caacd8c9e5560c2321ce09ba8617..970962a3f9d4d762e7d56c57e1846d64d9893144 100644 (file)
@@ -224,8 +224,7 @@ static int do_mount(struct thread_data *td, const char *url)
                return -1;
        }
 
-       options->events = malloc(event_size);
-       memset(options->events, 0, event_size);
+       options->events = calloc(1, event_size);
 
        options->prev_requested_event_index = -1;
        options->queue_depth = td->o.iodepth;
@@ -278,8 +277,7 @@ static int fio_libnfs_open(struct thread_data *td, struct fio_file *f)
                        options->nfs_url, ret, nfs_get_error(options->context));
                return ret;
        }
-       nfs_data = malloc(sizeof(struct nfs_data));
-       memset(nfs_data, 0, sizeof(struct nfs_data));
+       nfs_data = calloc(1, sizeof(struct nfs_data));
        nfs_data->options = options;
 
        if (td->o.td_ddir == TD_DDIR_WRITE)
index c17697bff59642012faed0270d0efa25a9114877..7236ec94886a47136e450a81c0d366d84f46ec2e 100644 (file)
@@ -112,8 +112,7 @@ static struct null_data *null_init(struct thread_data *td)
        memset(nd, 0, sizeof(*nd));
 
        if (td->o.iodepth != 1) {
-               nd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
-               memset(nd->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
+               nd->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
                td->io_ops->flags |= FIO_ASYNCIO_SETS_ISSUE_TIME;
        } else
                td->io_ops->flags |= FIO_SYNCIO;
index 948f8a2483210ac368c53595bd3a401e6f9bd831..0f4eea68a9ee6019e256498c0279be611dfd3473 100644 (file)
@@ -198,11 +198,8 @@ static void fio_posixaio_cleanup(struct thread_data *td)
 static int fio_posixaio_init(struct thread_data *td)
 {
        struct posixaio_data *pd;
-       pd = malloc(sizeof(*pd));
-
-       memset(pd, 0, sizeof(*pd));
-       pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
-       memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
+       pd = calloc(1, sizeof(*pd));
+       pd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
 
        td->io_ops_data = pd;
        return 0;
index fcb41068896de230ad4cd7f2b83064045eb0f122..ee2844d323544be2b012245e08faf1e1d96342d4 100644 (file)
@@ -1296,23 +1296,18 @@ static int fio_rdmaio_init(struct thread_data *td)
 
        if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
            (rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
-               rd->rmt_us =
-                       malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
-               memset(rd->rmt_us, 0,
-                       FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
+               rd->rmt_us = calloc(FIO_RDMA_MAX_IO_DEPTH,
+                                   sizeof(struct remote_u));
                rd->rmt_nr = 0;
        }
 
-       rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
-       memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
+       rd->io_us_queued = calloc(td->o.iodepth, sizeof(struct io_u *));
        rd->io_u_queued_nr = 0;
 
-       rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
-       memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
+       rd->io_us_flight = calloc(td->o.iodepth, sizeof(struct io_u *));
        rd->io_u_flight_nr = 0;
 
-       rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
-       memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
+       rd->io_us_completed = calloc(td->o.iodepth, sizeof(struct io_u *));
        rd->io_u_completed_nr = 0;
 
        if (td_read(td)) {      /* READ as the server */
@@ -1339,8 +1334,7 @@ static int fio_rdmaio_post_init(struct thread_data *td)
        for (i = 0; i < td->io_u_freelist.nr; i++) {
                struct io_u *io_u = td->io_u_freelist.io_us[i];
 
-               io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
-               memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
+               io_u->engine_data = calloc(1, sizeof(struct rdma_io_u_data));
                ((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
 
                io_u->mr = ibv_reg_mr(rd->pd, io_u->buf, max_bs,
@@ -1386,9 +1380,7 @@ static int fio_rdmaio_setup(struct thread_data *td)
        }
 
        if (!td->io_ops_data) {
-               rd = malloc(sizeof(*rd));
-
-               memset(rd, 0, sizeof(*rd));
+               rd = calloc(1, sizeof(*rd));
                init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_64, 0);
                td->io_ops_data = rd;
        }
index 366b72f46de714f410b1a0fd17fedde45e73257c..b2b47fede675c8cef09e4b921297d3e4a6bed27f 100644 (file)
@@ -187,8 +187,7 @@ static int fio_solarisaio_init(struct thread_data *td)
 {
        unsigned int max_depth;
        struct solarisaio_data *sd;
-       sd = malloc(sizeof(*sd));
-       memset(sd, 0, sizeof(*sd));
+       sd = calloc(1, sizeof(*sd));
 
        max_depth = td->o.iodepth;
        if (max_depth > MAXASYNCHIO) {
@@ -197,8 +196,7 @@ static int fio_solarisaio_init(struct thread_data *td)
                                                        max_depth);
        }
 
-       sd->aio_events = malloc(max_depth * sizeof(struct io_u *));
-       memset(sd->aio_events, 0, max_depth * sizeof(struct io_u *));
+       sd->aio_events = calloc(max_depth, sizeof(struct io_u *));
        sd->max_depth = max_depth;
 
 #ifdef USE_SIGNAL_COMPLETIONS
index 339ba9997010ac4290ac9a5699ba26f7d4926710..d19991222e80ba46dd3decdb343704781439cc87 100644 (file)
@@ -402,8 +402,7 @@ static int fio_vsyncio_init(struct thread_data *td)
 {
        struct syncio_data *sd;
 
-       sd = malloc(sizeof(*sd));
-       memset(sd, 0, sizeof(*sd));
+       sd = calloc(1, sizeof(*sd));
        sd->last_offset = -1ULL;
        sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
        sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
diff --git a/eta.c b/eta.c
index ce1c6f2dcd86431fc7561fdac4d684e2d9b0751b..af4027e0e2997e9ae220fe2fe94da05ff1995121 100644 (file)
--- a/eta.c
+++ b/eta.c
@@ -409,8 +409,7 @@ bool calc_thread_status(struct jobs_eta *je, int force)
        if (!ddir_rw_sum(disp_io_bytes))
                fill_start_time(&disp_prev_time);
 
-       eta_secs = malloc(thread_number * sizeof(uint64_t));
-       memset(eta_secs, 0, thread_number * sizeof(uint64_t));
+       eta_secs = calloc(thread_number, sizeof(uint64_t));
 
        je->elapsed_sec = (mtime_since_genesis() + 999) / 1000;
 
@@ -692,10 +691,9 @@ struct jobs_eta *get_jobs_eta(bool force, size_t *size)
                return NULL;
 
        *size = sizeof(*je) + THREAD_RUNSTR_SZ + 8;
-       je = malloc(*size);
+       je = calloc(1, *size);
        if (!je)
                return NULL;
-       memset(je, 0, *size);
 
        if (!calc_thread_status(je, force)) {
                free(je);
index 8e5059412ed5e82c396d0f87a87523a2e61f1c4c..14c7610849218194c475366d8fe2f641084fa6ba 100644 (file)
@@ -303,13 +303,12 @@ static bool pre_read_file(struct thread_data *td, struct fio_file *f)
        if (bs > left)
                bs = left;
 
-       b = malloc(bs);
+       b = calloc(1, bs);
        if (!b) {
                td_verror(td, errno, "malloc");
                ret = false;
                goto error;
        }
-       memset(b, 0, bs);
 
        if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
                td_verror(td, errno, "lseek");
diff --git a/gfio.c b/gfio.c
index 22c5314d3d1d5f93141dd7bedd8b7e902a848cba..10c9b0947bbde51623f0f58b5a0ef5d43b30e4d6 100644 (file)
--- a/gfio.c
+++ b/gfio.c
@@ -730,8 +730,7 @@ static struct gui_entry *alloc_new_gui_entry(struct gui *ui)
 {
        struct gui_entry *ge;
 
-       ge = malloc(sizeof(*ge));
-       memset(ge, 0, sizeof(*ge));
+       ge = calloc(1, sizeof(*ge));
        ge->state = GE_STATE_NEW;
        ge->ui = ui;
        return ge;
diff --git a/graph.c b/graph.c
index c49cdae14fb00e04154a8cf6669f0194bfd0570c..3d2b6c96dd0ba12f3a4b7619d1ab2548d9ccbf5c 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -713,8 +713,7 @@ static void graph_label_add_value(struct graph_label *i, void *value,
        struct graph *g = i->parent;
        struct graph_value *x;
 
-       x = malloc(sizeof(*x));
-       memset(x, 0, sizeof(*x));
+       x = calloc(1, sizeof(*x));
        INIT_FLIST_HEAD(&x->alias);
        INIT_FLIST_HEAD(&x->list);
        flist_add_tail(&x->list, &i->value_list);
diff --git a/init.c b/init.c
index 48121f1496c2d7d6e1b2e57b9f16bf5f5c0768b3..437406ecaea5aeb31ab368cd680d1fa0989726bf 100644 (file)
--- a/init.c
+++ b/init.c
@@ -1946,8 +1946,7 @@ static int __parse_jobs_ini(struct thread_data *td,
         * it's really 256 + small bit, 280 should suffice
         */
        if (!nested) {
-               name = malloc(280);
-               memset(name, 0, 280);
+               name = calloc(1, 280);
        }
 
        opts = NULL;
index f9f4b84005b91d4655989901c8a8778b60429897..6b0efef85cb745fecc71cefff3f52d7eada4477a 100644 (file)
@@ -487,11 +487,10 @@ static void io_uring_probe(int fd)
        struct io_uring_probe *p;
        int ret;
 
-       p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
+       p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
        if (!p)
                return;
 
-       memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
        ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
        if (ret < 0)
                goto out;
index 4b255e19bb07445bfe9564807676123b06cd75af..632de38313401a468bddd0bf27476f8455297ef8 100644 (file)
@@ -78,8 +78,7 @@ int main(int argc, char *argv[])
        /* Create verification table */
        if (verify) {
                v_size = numbers * sizeof(uint8_t);
-               v = malloc(v_size);
-               memset(v, 0, v_size);
+               v = calloc(1, v_size);
                printf("\nVerification table is %lf KiB\n", (double)(v_size) / 1024);
        }
        v_start = v;
index e7e4c69ca66ee6b76ee1ad087ebdc2e42ba5c181..2848b686123223fec6b76fd209dd2d7fd6b54970 100644 (file)
--- a/verify.c
+++ b/verify.c
@@ -1595,8 +1595,7 @@ struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
        *sz = sizeof(*rep);
        *sz += nr * sizeof(struct thread_io_list);
        *sz += depth * sizeof(struct file_comp);
-       rep = malloc(*sz);
-       memset(rep, 0, *sz);
+       rep = calloc(1, *sz);
 
        rep->threads = cpu_to_le64((uint64_t) nr);