Skip to content

Commit

Permalink
fio: replace malloc+memset with calloc
Browse files Browse the repository at this point in the history
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/[email protected]/
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 <[email protected]>
  • Loading branch information
vincentkfu committed Apr 20, 2023
1 parent a93259c commit 223decd
Show file tree
Hide file tree
Showing 20 changed files with 31 additions and 65 deletions.
6 changes: 2 additions & 4 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions engines/e4defrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions engines/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions engines/libhdfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions engines/libiscsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 1 addition & 3 deletions engines/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions engines/nfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions engines/null.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions engines/posixaio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 7 additions & 15 deletions engines/rdma.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions engines/solarisaio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions engines/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *));
Expand Down
6 changes: 2 additions & 4 deletions eta.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions filesetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions gfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions t/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions t/lfsr-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 223decd

Please sign in to comment.