Replace malloc && memset by calloc

This only replaces occurrences where the newly allocated memory is
cleared completely afterwards, as in other cases it is a theoretical
performance hit although code would be cleaner this way.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: David Ahern <dsa@cumulusnetworks.com>
This commit is contained in:
Phil Sutter 2016-07-18 16:48:43 +02:00 committed by Stephen Hemminger
parent d17b136f7d
commit f89bb0210f
12 changed files with 20 additions and 42 deletions

View File

@ -86,9 +86,8 @@ reg:
return f;
noexist:
f = malloc(sizeof(*f));
f = calloc(1, sizeof(*f));
if (f) {
memset(f, 0, sizeof(*f));
strncpy(f->name, str, 15);
f->parse_genlopt = parse_nofopt;
f->print_genlopt = print_nofopt;

View File

@ -54,15 +54,12 @@ struct db_names *db_names_alloc(void)
{
struct db_names *db;
db = malloc(sizeof(*db));
db = calloc(1, sizeof(*db));
if (!db)
return NULL;
memset(db, 0, sizeof(*db));
db->size = MAX_ENTRIES;
db->hash = malloc(sizeof(struct db_entry *) * db->size);
memset(db->hash, 0, sizeof(struct db_entry *) * db->size);
db->hash = calloc(db->size, sizeof(struct db_entry *));
return db;
}

View File

@ -182,10 +182,8 @@ static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
static struct table_hdr th;
int ofs = 0;
for (i = 0; i < HDR_LINES; i++) {
th.hdr[i] = malloc(HDR_LINE_LENGTH);
memset(th.hdr[i], 0, HDR_LINE_LENGTH);
}
for (i = 0; i < HDR_LINES; i++)
th.hdr[i] = calloc(1, HDR_LINE_LENGTH);
for (i = 0; i < fps->num; i++) {
char *cname, *fname = fps->params[i].lf->name;

View File

@ -173,15 +173,13 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file)
struct lnstat_file *lf;
/* allocate */
lf = malloc(sizeof(*lf));
lf = calloc(1, sizeof(*lf));
if (!lf) {
fprintf(stderr, "out of memory\n");
return NULL;
}
/* initialize */
memset(lf, 0, sizeof(*lf));
/* de->d_name is guaranteed to be <= NAME_MAX */
strcpy(lf->basename, file);
strcpy(lf->path, path);

View File

@ -106,8 +106,8 @@ static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
if (args == NULL)
return PARSE_ERR(args, "canid: missing arguments");
rules.rules_raw = malloc(sizeof(struct can_filter) * rules.rules_capacity);
memset(rules.rules_raw, 0, sizeof(struct can_filter) * rules.rules_capacity);
rules.rules_raw = calloc(rules.rules_capacity,
sizeof(struct can_filter));
do {
if (!bstrcmp(args, "sff")) {

View File

@ -126,9 +126,8 @@ noexist:
goto restart_s;
}
#endif
a = malloc(sizeof(*a));
a = calloc(1, sizeof(*a));
if (a) {
memset(a, 0, sizeof(*a));
strncpy(a->id, "noact", 15);
a->parse_aopt = parse_noaopt;
a->print_aopt = print_noaopt;

View File

@ -164,16 +164,11 @@ get_target_name(const char *name)
return NULL;
#endif
new_name = malloc(strlen(name) + 1);
lname = malloc(strlen(name) + 1);
if (new_name)
memset(new_name, '\0', strlen(name) + 1);
else
new_name = calloc(1, strlen(name) + 1);
lname = calloc(1, strlen(name) + 1);
if (!new_name)
exit_error(PARAMETER_PROBLEM, "get_target_name");
if (lname)
memset(lname, '\0', strlen(name) + 1);
else
if (!lname)
exit_error(PARAMETER_PROBLEM, "get_target_name");
strcpy(new_name, name);

View File

@ -107,9 +107,8 @@ reg:
return p;
noexist:
p = malloc(sizeof(*p));
p = calloc(1, sizeof(*p));
if (p) {
memset(p, 0, sizeof(*p));
strncpy(p->id, str, sizeof(p->id) - 1);
p->parse_peopt = pedit_parse_nopopt;
goto reg;

View File

@ -133,11 +133,9 @@ reg:
return q;
noexist:
q = malloc(sizeof(*q));
q = calloc(1, sizeof(*q));
if (q) {
memset(q, 0, sizeof(*q));
q->id = strcpy(malloc(strlen(str)+1), str);
q->id = strdup(str);
q->parse_qopt = parse_noqopt;
q->print_qopt = print_noqopt;
goto reg;
@ -177,9 +175,8 @@ reg:
filter_list = q;
return q;
noexist:
q = malloc(sizeof(*q));
q = calloc(1, sizeof(*q));
if (q) {
memset(q, 0, sizeof(*q));
strncpy(q->id, str, 15);
q->parse_fopt = parse_nofopt;
q->print_fopt = print_nofopt;

View File

@ -112,12 +112,10 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
FILE *fp;
tmp_len = sizeof("4096,") + BPF_MAXINSNS * op_len;
tmp_string = malloc(tmp_len);
tmp_string = calloc(1, tmp_len);
if (tmp_string == NULL)
return -ENOMEM;
memset(tmp_string, 0, tmp_len);
fp = fopen(arg, "r");
if (fp == NULL) {
perror("Cannot fopen");

View File

@ -163,9 +163,8 @@ __u32 filter_classid;
static void graph_node_add(__u32 parent_id, __u32 id, void *data,
int len)
{
struct graph_node *node = malloc(sizeof(struct graph_node));
struct graph_node *node = calloc(1, sizeof(struct graph_node));
memset(node, 0, sizeof(*node));
node->id = id;
node->parent_id = parent_id;

View File

@ -71,9 +71,8 @@ reg:
return eu;
noexist:
eu = malloc(sizeof(*eu));
eu = calloc(1, sizeof(*eu));
if (eu) {
memset(eu, 0, sizeof(*eu));
strncpy(eu->id, name, sizeof(eu->id) - 1);
eu->parse_eopt = parse_noeopt;
goto reg;