Skip to contentSkip to conversation

using of calloc instead of malloc+memset

PATCH In review Silvio Fricke 2 participants 14 messages Sep 29, 2015 – Sep 30, 2015 1 message not in the archive Btrfs file system

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

cover using of calloc instead of malloc+memset

Hi btrfs,

please review my patches about switching from malloc with memset to calloc. Thanks for that and eventual inclusion of my patches.

Silvio

Silvio Fricke (11):
btrfs-progs: use calloc instead of malloc+memset for btrfs-image.c
btrfs-progs: use calloc instead of malloc+memset for btrfs-list.c
btrfs-progs: use calloc instead of malloc+memset for chunk-recover.c
btrfs-progs: use calloc instead of malloc+memset for cmds-check.c
btrfs-progs: use calloc instead of malloc+memset for disk-io.c
btrfs-progs: use calloc instead of malloc+memset for extent_io.c
btrfs-progs: use calloc instead of malloc+memset for mkfs.c
btrfs-progs: use calloc instead of malloc+memset for qgroup.c
btrfs-progs: use calloc instead of malloc+memset for quick-test.c
btrfs-progs: use calloc instead of malloc+memset for volumes.c
btrfs-progs: check: fix memset range

 btrfs-image.c   |  3 +--
 btrfs-list.c    |  9 +++------
 chunk-recover.c |  6 ++----
 cmds-check.c    | 12 ++++--------
 disk-io.c       |  7 ++-----
 extent_io.c     |  3 +--
 mkfs.c          |  3 +--
 qgroup.c        |  9 +++------
 quick-test.c    |  3 +--
 volumes.c       |  3 +--
 10 files changed, 19 insertions(+), 39 deletions(-)
Signature · 1 line
2.5.3
#2 lore raw

PATCH 1/11 btrfs-progs: use calloc instead of malloc+memset for btrfs-image.c

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 btrfs-image.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
btrfs-image.c +1 -2
index 82eed05..20396ef 100644
@@ -1505,10 +1505,9 @@ static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size)
 {
 	struct extent_buffer *eb;
 
-	eb = malloc(sizeof(struct extent_buffer) + size);
+	eb = calloc(1, sizeof(struct extent_buffer) + size);
 	if (!eb)
 		return NULL;
-	memset(eb, 0, sizeof(struct extent_buffer) + size);
 
 	eb->start = bytenr;
 	eb->len = size;
Signature · 1 line
2.5.3
#3 lore raw

PATCH 2/11 btrfs-progs: use calloc instead of malloc+memset for btrfs-list.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 btrfs-list.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
btrfs-list.c +3 -6
index d54de61..7529e11 100644
@@ -226,13 +226,12 @@ struct btrfs_list_comparer_set *btrfs_list_alloc_comparer_set(void)
 
 	size = sizeof(struct btrfs_list_comparer_set) +
 	       BTRFS_LIST_NCOMPS_INCREASE * sizeof(struct btrfs_list_comparer);
-	set = malloc(size);
+	set = calloc(1, size);
 	if (!set) {
 		fprintf(stderr, "memory allocation failed\n");
 		exit(1);
 	}
 
-	memset(set, 0, size);
 	set->total = BTRFS_LIST_NCOMPS_INCREASE;
 
 	return set;
@@ -474,12 +473,11 @@ static int add_root(struct root_lookup *root_lookup,
 	if (!ret)
 		return 0;
 
-	ri = malloc(sizeof(*ri));
+	ri = calloc(1, sizeof(*ri));
 	if (!ri) {
 		printf("memory allocation failed\n");
 		exit(1);
 	}
-	memset(ri, 0, sizeof(*ri));
 	ri->root_id = root_id;
 
 	if (name && name_len > 0) {
@@ -1208,13 +1206,12 @@ struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
 
 	size = sizeof(struct btrfs_list_filter_set) +
 	       BTRFS_LIST_NFILTERS_INCREASE * sizeof(struct btrfs_list_filter);
-	set = malloc(size);
+	set = calloc(1, size);
 	if (!set) {
 		fprintf(stderr, "memory allocation failed\n");
 		exit(1);
 	}
 
-	memset(set, 0, size);
 	set->total = BTRFS_LIST_NFILTERS_INCREASE;
 
 	return set;
Signature · 1 line
2.5.3
#4 lore raw

PATCH 3/11 btrfs-progs: use calloc instead of malloc+memset for chunk-recover.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 chunk-recover.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
chunk-recover.c +2 -4
index 1fb04f7..d0ca920 100644
@@ -85,13 +85,12 @@ static struct extent_record *btrfs_new_extent_record(struct extent_buffer *eb)
 {
 	struct extent_record *rec;
 
-	rec = malloc(sizeof(*rec));
+	rec = calloc(1, sizeof(*rec));
 	if (!rec) {
 		fprintf(stderr, "Fail to allocate memory for extent record.\n");
 		exit(1);
 	}
 
-	memset(rec, 0, sizeof(*rec));
 	rec->cache.start = btrfs_header_bytenr(eb);
 	rec->cache.size = eb->len;
 	rec->generation = btrfs_header_generation(eb);
@@ -2228,10 +2227,9 @@ static int btrfs_recover_chunks(struct recover_control *rc)
 		nstripes = btrfs_get_device_extents(bg->objectid,
 						    &rc->devext.no_chunk_orphans,
 						    &devexts);
-		chunk = malloc(btrfs_chunk_record_size(nstripes));
+		chunk = calloc(1, btrfs_chunk_record_size(nstripes));
 		if (!chunk)
 			return -ENOMEM;
-		memset(chunk, 0, btrfs_chunk_record_size(nstripes));
 		INIT_LIST_HEAD(&chunk->dextents);
 		chunk->bg_rec = bg;
 		chunk->cache.start = bg->objectid;
Signature · 1 line
2.5.3
#5 lore raw

PATCH 4/11 btrfs-progs: use calloc instead of malloc+memset for cmds-check.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 cmds-check.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
cmds-check.c +3 -7
index 07df79f..2923d05 100644
@@ -4892,14 +4892,12 @@ struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
 	ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
 	num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
 
-	rec = malloc(btrfs_chunk_record_size(num_stripes));
+	rec = calloc(1, btrfs_chunk_record_size(num_stripes));
 	if (!rec) {
 		fprintf(stderr, "memory allocation failed\n");
 		exit(-1);
 	}
 
-	memset(rec, 0, btrfs_chunk_record_size(num_stripes));
-
 	INIT_LIST_HEAD(&rec->list);
 	INIT_LIST_HEAD(&rec->dextents);
 	rec->bg_rec = NULL;
@@ -4997,12 +4995,11 @@ btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
 	struct btrfs_block_group_item *ptr;
 	struct block_group_record *rec;
 
-	rec = malloc(sizeof(*rec));
+	rec = calloc(1, sizeof(*rec));
 	if (!rec) {
 		fprintf(stderr, "memory allocation failed\n");
 		exit(-1);
 	}
-	memset(rec, 0, sizeof(*rec));
 
 	rec->cache.start = key->objectid;
 	rec->cache.size = key->offset;
@@ -5046,12 +5043,11 @@ btrfs_new_device_extent_record(struct extent_buffer *leaf,
 	struct device_extent_record *rec;
 	struct btrfs_dev_extent *ptr;
 
-	rec = malloc(sizeof(*rec));
+	rec = calloc(1, sizeof(*rec));
 	if (!rec) {
 		fprintf(stderr, "memory allocation failed\n");
 		exit(-1);
 	}
-	memset(rec, 0, sizeof(*rec));
 
 	rec->cache.objectid = key->objectid;
 	rec->cache.start = key->offset;
Signature · 1 line
2.5.3
#6 lore raw

PATCH 5/11 btrfs-progs: use calloc instead of malloc+memset for disk-io.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 disk-io.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
disk-io.c +2 -5
index 2469a84..f7e6c41 100644
@@ -698,10 +698,9 @@ struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
 	u32 blocksize;
 	int ret = 0;
 
-	root = malloc(sizeof(*root));
+	root = calloc(1, sizeof(*root));
 	if (!root)
 		return ERR_PTR(-ENOMEM);
-	memset(root, 0, sizeof(*root));
 	if (location->offset == (u64)-1) {
 		ret = find_and_setup_root(tree_root, fs_info,
 					  location->objectid, root);
@@ -829,12 +828,10 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
 {
 	struct btrfs_fs_info *fs_info;
 
-	fs_info = malloc(sizeof(struct btrfs_fs_info));
+	fs_info = calloc(1, sizeof(struct btrfs_fs_info));
 	if (!fs_info)
 		return NULL;
 
-	memset(fs_info, 0, sizeof(struct btrfs_fs_info));
-
 	fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
 	fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
 	fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
Signature · 1 line
2.5.3
#7 lore raw

PATCH 6/11 btrfs-progs: use calloc instead of malloc+memset for extent_io.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 extent_io.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
extent_io.c +1 -2
index 07695ef..75496ce 100644
@@ -538,12 +538,11 @@ static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
 {
 	struct extent_buffer *eb;
 
-	eb = malloc(sizeof(struct extent_buffer) + blocksize);
+	eb = calloc(1, sizeof(struct extent_buffer) + blocksize);
 	if (!eb) {
 		BUG();
 		return NULL;
 	}
-	memset(eb, 0, sizeof(struct extent_buffer) + blocksize);
 
 	eb->start = bytenr;
 	eb->len = blocksize;
Signature · 1 line
2.5.3
#8 lore raw

PATCH 7/11 btrfs-progs: use calloc instead of malloc+memset for mkfs.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 mkfs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mkfs.c +1 -2
index a5802f7..ecd6fbf 100644
@@ -668,12 +668,11 @@ static int add_file_items(struct btrfs_trans_handle *trans,
 	 * do our IO in extent buffers so it can work
 	 * against any raid type
 	 */
-	eb = malloc(sizeof(*eb) + sectorsize);
+	eb = calloc(1, sizeof(*eb) + sectorsize);
 	if (!eb) {
 		ret = -ENOMEM;
 		goto end;
 	}
-	memset(eb, 0, sizeof(*eb) + sectorsize);
 
 again:
 
Signature · 1 line
2.5.3
#9 lore raw

PATCH 8/11 btrfs-progs: use calloc instead of malloc+memset for qgroup.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 qgroup.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
qgroup.c +3 -6
index ec9a3ac..99fddea 100644
@@ -436,13 +436,12 @@ struct btrfs_qgroup_comparer_set *btrfs_qgroup_alloc_comparer_set(void)
 	size = sizeof(struct btrfs_qgroup_comparer_set) +
 	       BTRFS_QGROUP_NCOMPS_INCREASE *
 	       sizeof(struct btrfs_qgroup_comparer);
-	set = malloc(size);
+	set = calloc(1, size);
 	if (!set) {
 		fprintf(stderr, "memory allocation failed\n");
 		exit(1);
 	}
 
-	memset(set, 0, size);
 	set->total = BTRFS_QGROUP_NCOMPS_INCREASE;
 
 	return set;
@@ -644,12 +643,11 @@ static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
 	if (!ret)
 		return 0;
 
-	bq = malloc(sizeof(*bq));
+	bq = calloc(1, sizeof(*bq));
 	if (!bq) {
 		printf("memory allocation failed\n");
 		exit(1);
 	}
-	memset(bq, 0, sizeof(*bq));
 	if (qgroupid) {
 		bq->qgroupid = qgroupid;
 		INIT_LIST_HEAD(&bq->qgroups);
@@ -813,12 +811,11 @@ struct btrfs_qgroup_filter_set *btrfs_qgroup_alloc_filter_set(void)
 	size = sizeof(struct btrfs_qgroup_filter_set) +
 	       BTRFS_QGROUP_NFILTERS_INCREASE *
 	       sizeof(struct btrfs_qgroup_filter);
-	set = malloc(size);
+	set = calloc(1, size);
 	if (!set) {
 		fprintf(stderr, "memory allocation failed\n");
 		exit(1);
 	}
-	memset(set, 0, size);
 	set->total = BTRFS_QGROUP_NFILTERS_INCREASE;
 
 	return set;
Signature · 1 line
2.5.3
#10 lore raw

PATCH 9/11 btrfs-progs: use calloc instead of malloc+memset for quick-test.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 quick-test.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
quick-test.c +1 -2
index 5dfb2fe..ffde85d 100644
@@ -46,8 +46,7 @@ int main(int ac, char **av) {
 	struct btrfs_root *root;
 	struct btrfs_trans_handle *trans;
 
-	buf = malloc(512);
-	memset(buf, 0, 512);
+	buf = calloc(1, 512);
 
 	radix_tree_init();
 
Signature · 1 line
2.5.3
#11 lore raw

PATCH 10/11 btrfs-progs: use calloc instead of malloc+memset for volumes.c

In reply to Silvio Fricke

This patch is generated from a coccinelle semantic patch:

	identifier t;
	expression e;
	statement s;
	@@
	-t = malloc(e);
	+t = calloc(1, e);
	(
	if (!t) s
	|
	if (t == NULL) s
	|
	)
	-memset(t, 0, e);
---
 volumes.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
volumes.c +1 -2
index ca50f1c..83ddd16 100644
@@ -1968,10 +1968,9 @@ static void split_eb_for_raid56(struct btrfs_fs_info *info,
 		if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
 			break;
 
-		eb = malloc(sizeof(struct extent_buffer) + stripe_len);
+		eb = calloc(1, sizeof(struct extent_buffer) + stripe_len);
 		if (!eb)
 			BUG();
-		memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
 
 		eb->start = raid_map[i];
 		eb->len = stripe_len;
Signature · 1 line
2.5.3
#12 lore raw

PATCH 11/11 btrfs-progs: check: fix memset range

In reply to Silvio Fricke
---
 cmds-check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmds-check.c +1 -1
index 2923d05..2c32ff3 100644
@@ -3021,7 +3021,7 @@ static struct root_backref *get_root_backref(struct root_record *rec,
 	}
 
 	backref = malloc(sizeof(*backref) + namelen + 1);
-	memset(backref, 0, sizeof(*backref));
+	memset(backref, 0, sizeof(*backref) + namelen + 1);
 	backref->ref_root = ref_root;
 	backref->dir = dir;
 	backref->index = index;
Signature · 1 line
2.5.3
#13 lore raw
In reply to Silvio Fricke
Quoted 11 lines · Silvio Fricke

Silvio Fricke (11):
btrfs-progs: use calloc instead of malloc+memset for btrfs-image.c
btrfs-progs: use calloc instead of malloc+memset for btrfs-list.c
btrfs-progs: use calloc instead of malloc+memset for chunk-recover.c
btrfs-progs: use calloc instead of malloc+memset for cmds-check.c
btrfs-progs: use calloc instead of malloc+memset for disk-io.c
btrfs-progs: use calloc instead of malloc+memset for extent_io.c
btrfs-progs: use calloc instead of malloc+memset for mkfs.c
btrfs-progs: use calloc instead of malloc+memset for qgroup.c
btrfs-progs: use calloc instead of malloc+memset for quick-test.c
btrfs-progs: use calloc instead of malloc+memset for volumes.c

Thanks.

I think that all of these can be squeezed into one, the change is logically same in all the files and easy to review even in a big patch. So I'll do that, the lat patch will be separate as it's a fix.

#14 lore raw
In reply to Silvio Fricke
Quoted 4 lines · Silvio Fricke

Silvio Fricke (11):
btrfs-progs: use calloc instead of malloc+memset for btrfs-image.c
btrfs-progs: use calloc instead of malloc+memset for btrfs-list.c
btrfs-progs: use calloc instead of malloc+memset for chunk-recover.c

Quoted 8 lines

btrfs-progs: use calloc instead of malloc+memset for cmds-check.c
btrfs-progs: use calloc instead of malloc+memset for disk-io.c
btrfs-progs: use calloc instead of malloc+memset for extent_io.c
btrfs-progs: use calloc instead of malloc+memset for mkfs.c
btrfs-progs: use calloc instead of malloc+memset for qgroup.c
btrfs-progs: use calloc instead of malloc+memset for quick-test.c
btrfs-progs: use calloc instead of malloc+memset for volumes.c
btrfs-progs: check: fix memset range

Actually this patch should go first (reordered in git now), then the semantic rule would apply to it as well and now the malloc/memset patter is gone.