btrfs-progs: use calloc instead of malloc+memset
authorSilvio Fricke <silvio.fricke@gmail.com>
Tue, 29 Sep 2015 17:10:36 +0000 (19:10 +0200)
committerDavid Sterba <dsterba@suse.com>
Wed, 21 Oct 2015 12:29:26 +0000 (14:29 +0200)
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);

Signed-off-by: Silvio Fricke <silvio.fricke@gmail.com>
[squashed patches into one]
Signed-off-by: David Sterba <dsterba@suse.com>
btrfs-image.c
btrfs-list.c
chunk-recover.c
cmds-check.c
disk-io.c
extent_io.c
mkfs.c
qgroup.c
quick-test.c
volumes.c

index 82eed05..20396ef 100644 (file)
@@ -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;
index d54de61..7529e11 100644 (file)
@@ -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;
index 1fb04f7..d0ca920 100644 (file)
@@ -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;
index 0b14b61..fe51aae 100644 (file)
@@ -3020,8 +3020,7 @@ static struct root_backref *get_root_backref(struct root_record *rec,
                return backref;
        }
 
-       backref = malloc(sizeof(*backref) + namelen + 1);
-       memset(backref, 0, sizeof(*backref) + namelen + 1);
+       backref = calloc(1, sizeof(*backref) + namelen + 1);
        backref->ref_root = ref_root;
        backref->dir = dir;
        backref->index = index;
@@ -4892,14 +4891,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 +4994,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 +5042,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;
index 7e62310..139bc80 100644 (file)
--- a/disk-io.c
+++ b/disk-io.c
@@ -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));
index 07695ef..75496ce 100644 (file)
@@ -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;
diff --git a/mkfs.c b/mkfs.c
index a5802f7..ecd6fbf 100644 (file)
--- a/mkfs.c
+++ b/mkfs.c
@@ -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:
 
index ec9a3ac..99fddea 100644 (file)
--- a/qgroup.c
+++ b/qgroup.c
@@ -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;
index 5dfb2fe..ffde85d 100644 (file)
@@ -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();
 
index ca50f1c..83ddd16 100644 (file)
--- a/volumes.c
+++ b/volumes.c
@@ -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;