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:
parent
d17b136f7d
commit
f89bb0210f
|
|
@ -86,9 +86,8 @@ reg:
|
||||||
return f;
|
return f;
|
||||||
|
|
||||||
noexist:
|
noexist:
|
||||||
f = malloc(sizeof(*f));
|
f = calloc(1, sizeof(*f));
|
||||||
if (f) {
|
if (f) {
|
||||||
memset(f, 0, sizeof(*f));
|
|
||||||
strncpy(f->name, str, 15);
|
strncpy(f->name, str, 15);
|
||||||
f->parse_genlopt = parse_nofopt;
|
f->parse_genlopt = parse_nofopt;
|
||||||
f->print_genlopt = print_nofopt;
|
f->print_genlopt = print_nofopt;
|
||||||
|
|
|
||||||
|
|
@ -54,15 +54,12 @@ struct db_names *db_names_alloc(void)
|
||||||
{
|
{
|
||||||
struct db_names *db;
|
struct db_names *db;
|
||||||
|
|
||||||
db = malloc(sizeof(*db));
|
db = calloc(1, sizeof(*db));
|
||||||
if (!db)
|
if (!db)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
memset(db, 0, sizeof(*db));
|
|
||||||
|
|
||||||
db->size = MAX_ENTRIES;
|
db->size = MAX_ENTRIES;
|
||||||
db->hash = malloc(sizeof(struct db_entry *) * db->size);
|
db->hash = calloc(db->size, sizeof(struct db_entry *));
|
||||||
memset(db->hash, 0, sizeof(struct db_entry *) * db->size);
|
|
||||||
|
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -182,10 +182,8 @@ static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
|
||||||
static struct table_hdr th;
|
static struct table_hdr th;
|
||||||
int ofs = 0;
|
int ofs = 0;
|
||||||
|
|
||||||
for (i = 0; i < HDR_LINES; i++) {
|
for (i = 0; i < HDR_LINES; i++)
|
||||||
th.hdr[i] = malloc(HDR_LINE_LENGTH);
|
th.hdr[i] = calloc(1, HDR_LINE_LENGTH);
|
||||||
memset(th.hdr[i], 0, HDR_LINE_LENGTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < fps->num; i++) {
|
for (i = 0; i < fps->num; i++) {
|
||||||
char *cname, *fname = fps->params[i].lf->name;
|
char *cname, *fname = fps->params[i].lf->name;
|
||||||
|
|
|
||||||
|
|
@ -173,15 +173,13 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file)
|
||||||
struct lnstat_file *lf;
|
struct lnstat_file *lf;
|
||||||
|
|
||||||
/* allocate */
|
/* allocate */
|
||||||
lf = malloc(sizeof(*lf));
|
lf = calloc(1, sizeof(*lf));
|
||||||
if (!lf) {
|
if (!lf) {
|
||||||
fprintf(stderr, "out of memory\n");
|
fprintf(stderr, "out of memory\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize */
|
/* initialize */
|
||||||
memset(lf, 0, sizeof(*lf));
|
|
||||||
|
|
||||||
/* de->d_name is guaranteed to be <= NAME_MAX */
|
/* de->d_name is guaranteed to be <= NAME_MAX */
|
||||||
strcpy(lf->basename, file);
|
strcpy(lf->basename, file);
|
||||||
strcpy(lf->path, path);
|
strcpy(lf->path, path);
|
||||||
|
|
|
||||||
|
|
@ -106,8 +106,8 @@ static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
|
||||||
if (args == NULL)
|
if (args == NULL)
|
||||||
return PARSE_ERR(args, "canid: missing arguments");
|
return PARSE_ERR(args, "canid: missing arguments");
|
||||||
|
|
||||||
rules.rules_raw = malloc(sizeof(struct can_filter) * rules.rules_capacity);
|
rules.rules_raw = calloc(rules.rules_capacity,
|
||||||
memset(rules.rules_raw, 0, sizeof(struct can_filter) * rules.rules_capacity);
|
sizeof(struct can_filter));
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (!bstrcmp(args, "sff")) {
|
if (!bstrcmp(args, "sff")) {
|
||||||
|
|
|
||||||
|
|
@ -126,9 +126,8 @@ noexist:
|
||||||
goto restart_s;
|
goto restart_s;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
a = malloc(sizeof(*a));
|
a = calloc(1, sizeof(*a));
|
||||||
if (a) {
|
if (a) {
|
||||||
memset(a, 0, sizeof(*a));
|
|
||||||
strncpy(a->id, "noact", 15);
|
strncpy(a->id, "noact", 15);
|
||||||
a->parse_aopt = parse_noaopt;
|
a->parse_aopt = parse_noaopt;
|
||||||
a->print_aopt = print_noaopt;
|
a->print_aopt = print_noaopt;
|
||||||
|
|
|
||||||
13
tc/m_ipt.c
13
tc/m_ipt.c
|
|
@ -164,16 +164,11 @@ get_target_name(const char *name)
|
||||||
return NULL;
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
new_name = malloc(strlen(name) + 1);
|
new_name = calloc(1, strlen(name) + 1);
|
||||||
lname = malloc(strlen(name) + 1);
|
lname = calloc(1, strlen(name) + 1);
|
||||||
if (new_name)
|
if (!new_name)
|
||||||
memset(new_name, '\0', strlen(name) + 1);
|
|
||||||
else
|
|
||||||
exit_error(PARAMETER_PROBLEM, "get_target_name");
|
exit_error(PARAMETER_PROBLEM, "get_target_name");
|
||||||
|
if (!lname)
|
||||||
if (lname)
|
|
||||||
memset(lname, '\0', strlen(name) + 1);
|
|
||||||
else
|
|
||||||
exit_error(PARAMETER_PROBLEM, "get_target_name");
|
exit_error(PARAMETER_PROBLEM, "get_target_name");
|
||||||
|
|
||||||
strcpy(new_name, name);
|
strcpy(new_name, name);
|
||||||
|
|
|
||||||
|
|
@ -107,9 +107,8 @@ reg:
|
||||||
return p;
|
return p;
|
||||||
|
|
||||||
noexist:
|
noexist:
|
||||||
p = malloc(sizeof(*p));
|
p = calloc(1, sizeof(*p));
|
||||||
if (p) {
|
if (p) {
|
||||||
memset(p, 0, sizeof(*p));
|
|
||||||
strncpy(p->id, str, sizeof(p->id) - 1);
|
strncpy(p->id, str, sizeof(p->id) - 1);
|
||||||
p->parse_peopt = pedit_parse_nopopt;
|
p->parse_peopt = pedit_parse_nopopt;
|
||||||
goto reg;
|
goto reg;
|
||||||
|
|
|
||||||
9
tc/tc.c
9
tc/tc.c
|
|
@ -133,11 +133,9 @@ reg:
|
||||||
return q;
|
return q;
|
||||||
|
|
||||||
noexist:
|
noexist:
|
||||||
q = malloc(sizeof(*q));
|
q = calloc(1, sizeof(*q));
|
||||||
if (q) {
|
if (q) {
|
||||||
|
q->id = strdup(str);
|
||||||
memset(q, 0, sizeof(*q));
|
|
||||||
q->id = strcpy(malloc(strlen(str)+1), str);
|
|
||||||
q->parse_qopt = parse_noqopt;
|
q->parse_qopt = parse_noqopt;
|
||||||
q->print_qopt = print_noqopt;
|
q->print_qopt = print_noqopt;
|
||||||
goto reg;
|
goto reg;
|
||||||
|
|
@ -177,9 +175,8 @@ reg:
|
||||||
filter_list = q;
|
filter_list = q;
|
||||||
return q;
|
return q;
|
||||||
noexist:
|
noexist:
|
||||||
q = malloc(sizeof(*q));
|
q = calloc(1, sizeof(*q));
|
||||||
if (q) {
|
if (q) {
|
||||||
memset(q, 0, sizeof(*q));
|
|
||||||
strncpy(q->id, str, 15);
|
strncpy(q->id, str, 15);
|
||||||
q->parse_fopt = parse_nofopt;
|
q->parse_fopt = parse_nofopt;
|
||||||
q->print_fopt = print_nofopt;
|
q->print_fopt = print_nofopt;
|
||||||
|
|
|
||||||
|
|
@ -112,12 +112,10 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
tmp_len = sizeof("4096,") + BPF_MAXINSNS * op_len;
|
tmp_len = sizeof("4096,") + BPF_MAXINSNS * op_len;
|
||||||
tmp_string = malloc(tmp_len);
|
tmp_string = calloc(1, tmp_len);
|
||||||
if (tmp_string == NULL)
|
if (tmp_string == NULL)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
memset(tmp_string, 0, tmp_len);
|
|
||||||
|
|
||||||
fp = fopen(arg, "r");
|
fp = fopen(arg, "r");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
perror("Cannot fopen");
|
perror("Cannot fopen");
|
||||||
|
|
|
||||||
|
|
@ -163,9 +163,8 @@ __u32 filter_classid;
|
||||||
static void graph_node_add(__u32 parent_id, __u32 id, void *data,
|
static void graph_node_add(__u32 parent_id, __u32 id, void *data,
|
||||||
int len)
|
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->id = id;
|
||||||
node->parent_id = parent_id;
|
node->parent_id = parent_id;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,9 +71,8 @@ reg:
|
||||||
|
|
||||||
return eu;
|
return eu;
|
||||||
noexist:
|
noexist:
|
||||||
eu = malloc(sizeof(*eu));
|
eu = calloc(1, sizeof(*eu));
|
||||||
if (eu) {
|
if (eu) {
|
||||||
memset(eu, 0, sizeof(*eu));
|
|
||||||
strncpy(eu->id, name, sizeof(eu->id) - 1);
|
strncpy(eu->id, name, sizeof(eu->id) - 1);
|
||||||
eu->parse_eopt = parse_noeopt;
|
eu->parse_eopt = parse_noeopt;
|
||||||
goto reg;
|
goto reg;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue