Skip to content

Commit 223decd

Browse files
committed
fio: replace malloc+memset with calloc
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>
1 parent a93259c commit 223decd

20 files changed

+31
-65
lines changed

client.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ static struct fio_client *get_new_client(void)
369369
{
370370
struct fio_client *client;
371371

372-
client = malloc(sizeof(*client));
373-
memset(client, 0, sizeof(*client));
372+
client = calloc(1, sizeof(*client));
374373

375374
INIT_FLIST_HEAD(&client->list);
376375
INIT_FLIST_HEAD(&client->hash_list);
@@ -793,8 +792,7 @@ static int __fio_client_send_remote_ini(struct fio_client *client,
793792
dprint(FD_NET, "send remote ini %s to %s\n", filename, client->hostname);
794793

795794
p_size = sizeof(*pdu) + strlen(filename) + 1;
796-
pdu = malloc(p_size);
797-
memset(pdu, 0, p_size);
795+
pdu = calloc(1, p_size);
798796
pdu->name_len = strlen(filename);
799797
strcpy((char *) pdu->file, filename);
800798
pdu->client_type = cpu_to_le16((uint16_t) client->type);

engines/e4defrag.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ static int fio_e4defrag_init(struct thread_data *td)
7777
return 1;
7878
}
7979

80-
ed = malloc(sizeof(*ed));
80+
ed = calloc(1, sizeof(*ed));
8181
if (!ed) {
8282
td_verror(td, ENOMEM, "io_queue_init");
8383
return 1;
8484
}
85-
memset(ed, 0 ,sizeof(*ed));
8685

8786
if (td->o.directory)
8887
len = sprintf(donor_name, "%s/", td->o.directory);

engines/io_uring.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,10 @@ static void fio_ioring_probe(struct thread_data *td)
800800
/* default to off, as that's always safe */
801801
o->nonvectored = 0;
802802

803-
p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
803+
p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
804804
if (!p)
805805
return;
806806

807-
memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
808807
ret = syscall(__NR_io_uring_register, ld->ring_fd,
809808
IORING_REGISTER_PROBE, p, 256);
810809
if (ret < 0)

engines/libhdfs.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ static int fio_hdfsio_setup(struct thread_data *td)
315315
uint64_t file_size, total_file_size;
316316

317317
if (!td->io_ops_data) {
318-
hd = malloc(sizeof(*hd));
319-
memset(hd, 0, sizeof(*hd));
318+
hd = calloc(1, sizeof(*hd));
320319

321320
hd->curr_file_id = -1;
322321

engines/libiscsi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ static int fio_iscsi_setup_lun(struct iscsi_info *iscsi_info,
6868
struct scsi_readcapacity16 *rc16 = NULL;
6969
int ret = 0;
7070

71-
iscsi_lun = malloc(sizeof(struct iscsi_lun));
72-
memset(iscsi_lun, 0, sizeof(struct iscsi_lun));
71+
iscsi_lun = calloc(1, sizeof(struct iscsi_lun));
7372

7473
iscsi_lun->iscsi_info = iscsi_info;
7574

engines/net.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,9 +1370,7 @@ static int fio_netio_setup(struct thread_data *td)
13701370
}
13711371

13721372
if (!td->io_ops_data) {
1373-
nd = malloc(sizeof(*nd));
1374-
1375-
memset(nd, 0, sizeof(*nd));
1373+
nd = calloc(1, sizeof(*nd));
13761374
nd->listenfd = -1;
13771375
nd->pipes[0] = nd->pipes[1] = -1;
13781376
td->io_ops_data = nd;

engines/nfs.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ static int do_mount(struct thread_data *td, const char *url)
224224
return -1;
225225
}
226226

227-
options->events = malloc(event_size);
228-
memset(options->events, 0, event_size);
227+
options->events = calloc(1, event_size);
229228

230229
options->prev_requested_event_index = -1;
231230
options->queue_depth = td->o.iodepth;
@@ -278,8 +277,7 @@ static int fio_libnfs_open(struct thread_data *td, struct fio_file *f)
278277
options->nfs_url, ret, nfs_get_error(options->context));
279278
return ret;
280279
}
281-
nfs_data = malloc(sizeof(struct nfs_data));
282-
memset(nfs_data, 0, sizeof(struct nfs_data));
280+
nfs_data = calloc(1, sizeof(struct nfs_data));
283281
nfs_data->options = options;
284282

285283
if (td->o.td_ddir == TD_DDIR_WRITE)

engines/null.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ static struct null_data *null_init(struct thread_data *td)
112112
memset(nd, 0, sizeof(*nd));
113113

114114
if (td->o.iodepth != 1) {
115-
nd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
116-
memset(nd->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
115+
nd->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
117116
td->io_ops->flags |= FIO_ASYNCIO_SETS_ISSUE_TIME;
118117
} else
119118
td->io_ops->flags |= FIO_SYNCIO;

engines/posixaio.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,8 @@ static void fio_posixaio_cleanup(struct thread_data *td)
198198
static int fio_posixaio_init(struct thread_data *td)
199199
{
200200
struct posixaio_data *pd;
201-
pd = malloc(sizeof(*pd));
202-
203-
memset(pd, 0, sizeof(*pd));
204-
pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
205-
memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
201+
pd = calloc(1, sizeof(*pd));
202+
pd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
206203

207204
td->io_ops_data = pd;
208205
return 0;

engines/rdma.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,23 +1296,18 @@ static int fio_rdmaio_init(struct thread_data *td)
12961296

12971297
if ((rd->rdma_protocol == FIO_RDMA_MEM_WRITE) ||
12981298
(rd->rdma_protocol == FIO_RDMA_MEM_READ)) {
1299-
rd->rmt_us =
1300-
malloc(FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1301-
memset(rd->rmt_us, 0,
1302-
FIO_RDMA_MAX_IO_DEPTH * sizeof(struct remote_u));
1299+
rd->rmt_us = calloc(FIO_RDMA_MAX_IO_DEPTH,
1300+
sizeof(struct remote_u));
13031301
rd->rmt_nr = 0;
13041302
}
13051303

1306-
rd->io_us_queued = malloc(td->o.iodepth * sizeof(struct io_u *));
1307-
memset(rd->io_us_queued, 0, td->o.iodepth * sizeof(struct io_u *));
1304+
rd->io_us_queued = calloc(td->o.iodepth, sizeof(struct io_u *));
13081305
rd->io_u_queued_nr = 0;
13091306

1310-
rd->io_us_flight = malloc(td->o.iodepth * sizeof(struct io_u *));
1311-
memset(rd->io_us_flight, 0, td->o.iodepth * sizeof(struct io_u *));
1307+
rd->io_us_flight = calloc(td->o.iodepth, sizeof(struct io_u *));
13121308
rd->io_u_flight_nr = 0;
13131309

1314-
rd->io_us_completed = malloc(td->o.iodepth * sizeof(struct io_u *));
1315-
memset(rd->io_us_completed, 0, td->o.iodepth * sizeof(struct io_u *));
1310+
rd->io_us_completed = calloc(td->o.iodepth, sizeof(struct io_u *));
13161311
rd->io_u_completed_nr = 0;
13171312

13181313
if (td_read(td)) { /* READ as the server */
@@ -1339,8 +1334,7 @@ static int fio_rdmaio_post_init(struct thread_data *td)
13391334
for (i = 0; i < td->io_u_freelist.nr; i++) {
13401335
struct io_u *io_u = td->io_u_freelist.io_us[i];
13411336

1342-
io_u->engine_data = malloc(sizeof(struct rdma_io_u_data));
1343-
memset(io_u->engine_data, 0, sizeof(struct rdma_io_u_data));
1337+
io_u->engine_data = calloc(1, sizeof(struct rdma_io_u_data));
13441338
((struct rdma_io_u_data *)io_u->engine_data)->wr_id = i;
13451339

13461340
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)
13861380
}
13871381

13881382
if (!td->io_ops_data) {
1389-
rd = malloc(sizeof(*rd));
1390-
1391-
memset(rd, 0, sizeof(*rd));
1383+
rd = calloc(1, sizeof(*rd));
13921384
init_rand_seed(&rd->rand_state, (unsigned int) GOLDEN_RATIO_64, 0);
13931385
td->io_ops_data = rd;
13941386
}

engines/solarisaio.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ static int fio_solarisaio_init(struct thread_data *td)
187187
{
188188
unsigned int max_depth;
189189
struct solarisaio_data *sd;
190-
sd = malloc(sizeof(*sd));
191-
memset(sd, 0, sizeof(*sd));
190+
sd = calloc(1, sizeof(*sd));
192191

193192
max_depth = td->o.iodepth;
194193
if (max_depth > MAXASYNCHIO) {
@@ -197,8 +196,7 @@ static int fio_solarisaio_init(struct thread_data *td)
197196
max_depth);
198197
}
199198

200-
sd->aio_events = malloc(max_depth * sizeof(struct io_u *));
201-
memset(sd->aio_events, 0, max_depth * sizeof(struct io_u *));
199+
sd->aio_events = calloc(max_depth, sizeof(struct io_u *));
202200
sd->max_depth = max_depth;
203201

204202
#ifdef USE_SIGNAL_COMPLETIONS

engines/sync.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,7 @@ static int fio_vsyncio_init(struct thread_data *td)
402402
{
403403
struct syncio_data *sd;
404404

405-
sd = malloc(sizeof(*sd));
406-
memset(sd, 0, sizeof(*sd));
405+
sd = calloc(1, sizeof(*sd));
407406
sd->last_offset = -1ULL;
408407
sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
409408
sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));

eta.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,7 @@ bool calc_thread_status(struct jobs_eta *je, int force)
409409
if (!ddir_rw_sum(disp_io_bytes))
410410
fill_start_time(&disp_prev_time);
411411

412-
eta_secs = malloc(thread_number * sizeof(uint64_t));
413-
memset(eta_secs, 0, thread_number * sizeof(uint64_t));
412+
eta_secs = calloc(thread_number, sizeof(uint64_t));
414413

415414
je->elapsed_sec = (mtime_since_genesis() + 999) / 1000;
416415

@@ -692,10 +691,9 @@ struct jobs_eta *get_jobs_eta(bool force, size_t *size)
692691
return NULL;
693692

694693
*size = sizeof(*je) + THREAD_RUNSTR_SZ + 8;
695-
je = malloc(*size);
694+
je = calloc(1, *size);
696695
if (!je)
697696
return NULL;
698-
memset(je, 0, *size);
699697

700698
if (!calc_thread_status(je, force)) {
701699
free(je);

filesetup.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,12 @@ static bool pre_read_file(struct thread_data *td, struct fio_file *f)
303303
if (bs > left)
304304
bs = left;
305305

306-
b = malloc(bs);
306+
b = calloc(1, bs);
307307
if (!b) {
308308
td_verror(td, errno, "malloc");
309309
ret = false;
310310
goto error;
311311
}
312-
memset(b, 0, bs);
313312

314313
if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
315314
td_verror(td, errno, "lseek");

gfio.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,7 @@ static struct gui_entry *alloc_new_gui_entry(struct gui *ui)
730730
{
731731
struct gui_entry *ge;
732732

733-
ge = malloc(sizeof(*ge));
734-
memset(ge, 0, sizeof(*ge));
733+
ge = calloc(1, sizeof(*ge));
735734
ge->state = GE_STATE_NEW;
736735
ge->ui = ui;
737736
return ge;

graph.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,7 @@ static void graph_label_add_value(struct graph_label *i, void *value,
713713
struct graph *g = i->parent;
714714
struct graph_value *x;
715715

716-
x = malloc(sizeof(*x));
717-
memset(x, 0, sizeof(*x));
716+
x = calloc(1, sizeof(*x));
718717
INIT_FLIST_HEAD(&x->alias);
719718
INIT_FLIST_HEAD(&x->list);
720719
flist_add_tail(&x->list, &i->value_list);

init.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,8 +1946,7 @@ static int __parse_jobs_ini(struct thread_data *td,
19461946
* it's really 256 + small bit, 280 should suffice
19471947
*/
19481948
if (!nested) {
1949-
name = malloc(280);
1950-
memset(name, 0, 280);
1949+
name = calloc(1, 280);
19511950
}
19521951

19531952
opts = NULL;

t/io_uring.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,10 @@ static void io_uring_probe(int fd)
487487
struct io_uring_probe *p;
488488
int ret;
489489

490-
p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
490+
p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
491491
if (!p)
492492
return;
493493

494-
memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
495494
ret = syscall(__NR_io_uring_register, fd, IORING_REGISTER_PROBE, p, 256);
496495
if (ret < 0)
497496
goto out;

t/lfsr-test.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ int main(int argc, char *argv[])
7878
/* Create verification table */
7979
if (verify) {
8080
v_size = numbers * sizeof(uint8_t);
81-
v = malloc(v_size);
82-
memset(v, 0, v_size);
81+
v = calloc(1, v_size);
8382
printf("\nVerification table is %lf KiB\n", (double)(v_size) / 1024);
8483
}
8584
v_start = v;

verify.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,8 +1595,7 @@ struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
15951595
*sz = sizeof(*rep);
15961596
*sz += nr * sizeof(struct thread_io_list);
15971597
*sz += depth * sizeof(struct file_comp);
1598-
rep = malloc(*sz);
1599-
memset(rep, 0, *sz);
1598+
rep = calloc(1, *sz);
16001599

16011600
rep->threads = cpu_to_le64((uint64_t) nr);
16021601

0 commit comments

Comments
 (0)