ext4: easy malloc/calloc/realloc/free substitution
authorMichał Majewicz <mmajewicz@users.noreply.github.com>
Fri, 19 Aug 2016 08:41:47 +0000 (10:41 +0200)
committerMichał Majewicz <mmajewicz@users.noreply.github.com>
Fri, 19 Aug 2016 09:49:59 +0000 (11:49 +0200)
include/ext4_types.h
src/ext4.c
src/ext4_bcache.c
src/ext4_dir_idx.c
src/ext4_extent.c
src/ext4_journal.c
src/ext4_mkfs.c

index 4f934f22b80608fc223261c9ae8721675fb13ad3..23ea9008ac6e5d8476c0b8b8343da8e5fd88570c 100644 (file)
@@ -1004,6 +1004,24 @@ struct jbd_sb {
 }
 #endif
 
+
+#ifdef USE_OTHER_MALLOC
+
+#define ext4_malloc  pool_malloc
+#define ext4_calloc  pool_calloc
+#define ext4_realloc pool_realloc
+#define ext4_free    pool_free
+
+#else
+
+#define ext4_malloc  malloc
+#define ext4_calloc  calloc
+#define ext4_realloc realloc
+#define ext4_free    free
+
+#endif
+
+
 #endif /* EXT4_TYPES_H_ */
 
 /**
index 7f10e11686d65232ad79813f50e5fcf1d4e4cd9a..94744bbb4b24be97c80f5a09635703b7d4a31320 100644 (file)
@@ -395,12 +395,12 @@ int ext4_mount(const char *dev_name, const char *mount_point,
        if (!bc) {
                /*Automatic block cache alloc.*/
                mp->cache_dynamic = 1;
-               bc = malloc(sizeof(struct ext4_bcache));
+               bc = ext4_malloc(sizeof(struct ext4_bcache));
 
                r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
                                             bsize);
                if (r != EOK) {
-                       free(bc);
+                       ext4_free(bc);
                        ext4_block_fini(bd);
                        return r;
                }
@@ -416,7 +416,7 @@ int ext4_mount(const char *dev_name, const char *mount_point,
                ext4_block_fini(bd);
                if (mp->cache_dynamic) {
                        ext4_bcache_fini_dynamic(bc);
-                       free(bc);
+                       ext4_free(bc);
                }
                return r;
        }
@@ -451,7 +451,7 @@ int ext4_umount(const char *mount_point)
        ext4_bcache_cleanup(mp->fs.bdev->bc);
        if (mp->cache_dynamic) {
                ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
-               free(mp->fs.bdev->bc);
+               ext4_free(mp->fs.bdev->bc);
        }
        r = ext4_block_fini(mp->fs.bdev);
 Finish:
@@ -549,7 +549,7 @@ static int __ext4_recover(const char *mount_point)
        int r = ENOTSUP;
        EXT4_MP_LOCK(mp);
        if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
-               struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
+               struct jbd_fs *jbd_fs = ext4_calloc(1, sizeof(struct jbd_fs));
                if (!jbd_fs) {
                         r = ENOMEM;
                         goto Finish;
@@ -558,13 +558,13 @@ static int __ext4_recover(const char *mount_point)
 
                r = jbd_get_fs(&mp->fs, jbd_fs);
                if (r != EOK) {
-                       free(jbd_fs);
+                       ext4_free(jbd_fs);
                        goto Finish;
                }
 
                r = jbd_recover(jbd_fs);
                jbd_put_fs(jbd_fs);
-               free(jbd_fs);
+               ext4_free(jbd_fs);
        }
        if (r == EOK && !mp->fs.read_only) {
                uint32_t bgid;
@@ -2611,7 +2611,7 @@ int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
 
        r = ext4_xattr_list(&inode_ref, NULL, &list_len);
        if (r == EOK && list_len) {
-               xattr_list = malloc(list_len);
+               xattr_list = ext4_malloc(list_len);
                if (!xattr_list) {
                        ext4_fs_put_inode_ref(&inode_ref);
                        r = ENOMEM;
@@ -2658,7 +2658,7 @@ int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
 Finish:
        EXT4_MP_UNLOCK(mp);
        if (xattr_list)
-               free(xattr_list);
+               ext4_free(xattr_list);
 
        return r;
 
index e1e3b0ccbc13cb0c5ed0d790d27b5c1a5a87cadb..8b9682531e0074ce109d33aa89508c5c19fd00d3 100644 (file)
@@ -35,6 +35,7 @@
  */
 
 #include "ext4_config.h"
+#include "ext4_types.h"
 #include "ext4_bcache.h"
 #include "ext4_blockdev.h"
 #include "ext4_debug.h"
@@ -120,13 +121,13 @@ ext4_buf_alloc(struct ext4_bcache *bc, uint64_t lba)
 {
        void *data;
        struct ext4_buf *buf;
-       data = malloc(bc->itemsize);
+       data = ext4_malloc(bc->itemsize);
        if (!data)
                return NULL;
 
-       buf = calloc(1, sizeof(struct ext4_buf));
+       buf = ext4_calloc(1, sizeof(struct ext4_buf));
        if (!buf) {
-               free(data);
+               ext4_free(data);
                return NULL;
        }
 
@@ -138,8 +139,8 @@ ext4_buf_alloc(struct ext4_bcache *bc, uint64_t lba)
 
 static void ext4_buf_free(struct ext4_buf *buf)
 {
-       free(buf->data);
-       free(buf);
+       ext4_free(buf->data);
+       ext4_free(buf);
 }
 
 static struct ext4_buf *
index 7e105af0cf2a558ec22799a561fd93dbc208a582..0a89b922867072e3b3c11abc6aa69736817459da 100644 (file)
@@ -939,7 +939,7 @@ static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
        uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb);
 
        /* Allocate buffer for directory entries */
-       uint8_t *entry_buffer = malloc(block_size);
+       uint8_t *entry_buffer = ext4_malloc(block_size);
        if (entry_buffer == NULL)
                return ENOMEM;
 
@@ -949,9 +949,9 @@ static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
        /* Allocate sort entry */
        struct ext4_dx_sort_entry *sort;
 
-       sort = malloc(max_ecnt * sizeof(struct ext4_dx_sort_entry));
+       sort = ext4_malloc(max_ecnt * sizeof(struct ext4_dx_sort_entry));
        if (sort == NULL) {
-               free(entry_buffer);
+               ext4_free(entry_buffer);
                return ENOMEM;
        }
 
@@ -972,8 +972,8 @@ static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
                        rc = ext4_dir_dx_hash_string(&hinfo_tmp, len,
                                                     (char *)de->name);
                        if (rc != EOK) {
-                               free(sort);
-                               free(entry_buffer);
+                               ext4_free(sort);
+                               ext4_free(entry_buffer);
                                return rc;
                        }
 
@@ -1008,8 +1008,8 @@ static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
        uint32_t new_iblock;
        rc = ext4_fs_append_inode_dblk(inode_ref, &new_fblock, &new_iblock);
        if (rc != EOK) {
-               free(sort);
-               free(entry_buffer);
+               ext4_free(sort);
+               ext4_free(entry_buffer);
                return rc;
        }
 
@@ -1018,8 +1018,8 @@ static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
        rc = ext4_trans_block_get_noread(inode_ref->fs->bdev, &new_data_block_tmp,
                                   new_fblock);
        if (rc != EOK) {
-               free(sort);
-               free(entry_buffer);
+               ext4_free(sort);
+               ext4_free(entry_buffer);
                return rc;
        }
 
@@ -1097,8 +1097,8 @@ static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref,
        ext4_trans_set_block_dirty(old_data_block->buf);
        ext4_trans_set_block_dirty(new_data_block_tmp.buf);
 
-       free(sort);
-       free(entry_buffer);
+       ext4_free(sort);
+       ext4_free(entry_buffer);
 
        ext4_dir_dx_insert_entry(inode_ref, index_block, new_hash + continued,
                                new_iblock);
index 67871cfdb2554e349c2c65bfd61f34ecd25dfe63..28db8533b4d14ce5331985143af71f4cd449dde4 100644 (file)
@@ -529,14 +529,14 @@ static int ext4_find_extent(struct ext4_inode_ref *inode_ref, ext4_lblk_t block,
        if (path) {
                ext4_ext_drop_refs(inode_ref, path, 0);
                if (depth > path[0].maxdepth) {
-                       free(path);
+                       ext4_free(path);
                        *orig_path = path = NULL;
                }
        }
        if (!path) {
                int32_t path_depth = depth + 1;
                /* account possible depth increase */
-               path = calloc(1, sizeof(struct ext4_extent_path) *
+               path = ext4_calloc(1, sizeof(struct ext4_extent_path) *
                                     (path_depth + 1));
                if (!path)
                        return ENOMEM;
@@ -592,7 +592,7 @@ static int ext4_find_extent(struct ext4_inode_ref *inode_ref, ext4_lblk_t block,
 
 err:
        ext4_ext_drop_refs(inode_ref, path, 0);
-       free(path);
+       ext4_free(path);
        if (orig_path)
                *orig_path = NULL;
        return ret;
@@ -1130,7 +1130,7 @@ again:
                i = depth - (level - 1);
                /* We split from leaf to the i-th node */
                if (level > 0) {
-                       npath = calloc(1, sizeof(struct ext4_extent_path) *
+                       npath = ext4_calloc(1, sizeof(struct ext4_extent_path) *
                                              (level));
                        if (!npath) {
                                ret = ENOMEM;
@@ -1168,7 +1168,7 @@ out:
                }
        }
        if (npath)
-               free(npath);
+               ext4_free(npath);
 
        return ret;
 }
@@ -1498,7 +1498,7 @@ int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
 
 out:
        ext4_ext_drop_refs(inode_ref, path, 0);
-       free(path);
+       ext4_free(path);
        path = NULL;
        return ret;
 }
@@ -1792,7 +1792,7 @@ out:
 out2:
        if (path) {
                ext4_ext_drop_refs(inode_ref, path, 0);
-               free(path);
+               ext4_free(path);
        }
 
        return err;
index 9f3829020046f382a87f97f2fe7618f94c036418..76cb0a9ef9a876b9fdf75c652ef3555d353410b1 100644 (file)
@@ -147,8 +147,8 @@ RB_GENERATE_INTERNAL(jbd_block, jbd_block_rec, block_rec_node,
 RB_GENERATE_INTERNAL(jbd_revoke_tree, jbd_revoke_rec, revoke_node,
                     jbd_revoke_rec_cmp, static inline)
 
-#define jbd_alloc_revoke_entry() calloc(1, sizeof(struct revoke_entry))
-#define jbd_free_revoke_entry(addr) free(addr)
+#define jbd_alloc_revoke_entry() ext4_calloc(1, sizeof(struct revoke_entry))
+#define jbd_free_revoke_entry(addr) ext4_free(addr)
 
 static int jbd_has_csum(struct jbd_sb *jbd_sb)
 {
@@ -1310,7 +1310,7 @@ static void jbd_journal_flush_trans(struct jbd_trans *trans)
        struct jbd_buf *jbd_buf, *tmp;
        struct jbd_journal *journal = trans->journal;
        struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
-       void *tmp_data = malloc(journal->block_size);
+       void *tmp_data = ext4_malloc(journal->block_size);
        ext4_assert(tmp_data);
 
        TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
@@ -1340,7 +1340,7 @@ static void jbd_journal_flush_trans(struct jbd_trans *trans)
                        ext4_block_set(fs->bdev, &block);
        }
 
-       free(tmp_data);
+       ext4_free(tmp_data);
 }
 
 static void
@@ -1496,7 +1496,7 @@ jbd_trans_insert_block_rec(struct jbd_trans *trans,
                jbd_trans_change_ownership(block_rec, trans);
                return block_rec;
        }
-       block_rec = calloc(1, sizeof(struct jbd_block_rec));
+       block_rec = ext4_calloc(1, sizeof(struct jbd_block_rec));
        if (!block_rec)
                return NULL;
 
@@ -1587,7 +1587,7 @@ jbd_trans_remove_block_rec(struct jbd_journal *journal,
                RB_REMOVE(jbd_block,
                                &journal->block_rec_root,
                                block_rec);
-               free(block_rec);
+               ext4_free(block_rec);
        }
 }
 
@@ -1609,13 +1609,13 @@ int jbd_trans_set_block_dirty(struct jbd_trans *trans,
                if (jbd_buf && jbd_buf->trans == trans)
                        return EOK;
        }
-       jbd_buf = calloc(1, sizeof(struct jbd_buf));
+       jbd_buf = ext4_calloc(1, sizeof(struct jbd_buf));
        if (!jbd_buf)
                return ENOMEM;
 
        if ((block_rec = jbd_trans_insert_block_rec(trans,
                                        block->lb_id)) == NULL) {
-               free(jbd_buf);
+               ext4_free(jbd_buf);
                return ENOMEM;
        }
 
@@ -1643,7 +1643,7 @@ int jbd_trans_set_block_dirty(struct jbd_trans *trans,
        if (rec) {
                RB_REMOVE(jbd_revoke_tree, &trans->revoke_root,
                          rec);
-               free(rec);
+               ext4_free(rec);
        }
 
        return EOK;
@@ -1665,7 +1665,7 @@ int jbd_trans_revoke_block(struct jbd_trans *trans,
        if (rec)
                return EOK;
 
-       rec = calloc(1, sizeof(struct jbd_revoke_rec));
+       rec = ext4_calloc(1, sizeof(struct jbd_revoke_rec));
        if (!rec)
                return ENOMEM;
 
@@ -1736,19 +1736,19 @@ void jbd_journal_free_trans(struct jbd_journal *journal,
                                abort,
                                false);
                TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
-               free(jbd_buf);
+               ext4_free(jbd_buf);
        }
        RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root,
                          tmp2) {
                RB_REMOVE(jbd_revoke_tree, &trans->revoke_root, rec);
-               free(rec);
+               ext4_free(rec);
        }
        LIST_FOREACH_SAFE(block_rec, &trans->tbrec_list, tbrec_node,
                          tmp3) {
                jbd_trans_remove_block_rec(journal, block_rec, trans);
        }
 
-       free(trans);
+       ext4_free(trans);
 }
 
 /**@brief  Write commit block for a transaction
@@ -1854,7 +1854,7 @@ static int jbd_journal_prepare(struct jbd_journal *journal,
 
                ext4_block_set(fs->bdev, &jbd_buf->block);
                TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
-               free(jbd_buf);
+               ext4_free(jbd_buf);
        }
 
        TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, tmp) {
@@ -1887,7 +1887,7 @@ static int jbd_journal_prepare(struct jbd_journal *journal,
 
                        ext4_block_set(fs->bdev, &jbd_buf->block);
                        TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
-                       free(jbd_buf);
+                       ext4_free(jbd_buf);
                        continue;
                }
                checksum = jbd_block_csum(journal->jbd_fs,
@@ -2123,7 +2123,7 @@ static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
                buf->end_write_arg = NULL;
        }
 
-       free(jbd_buf);
+       ext4_free(jbd_buf);
 
        trans->written_cnt++;
        if (trans->written_cnt == trans->data_cnt) {
@@ -2257,7 +2257,7 @@ struct jbd_trans *
 jbd_journal_new_trans(struct jbd_journal *journal)
 {
        struct jbd_trans *trans = NULL;
-       trans = calloc(1, sizeof(struct jbd_trans));
+       trans = ext4_calloc(1, sizeof(struct jbd_trans));
        if (!trans)
                return NULL;
 
index d6cb1ead94dbdfd19b2a0d1ceb108f31caec7d90..7af33018b1ab27ec397f7d44aaa215f8f713c6d5 100644 (file)
@@ -176,11 +176,11 @@ static int create_fs_aux_info(struct fs_aux_info *aux_info,
                aux_info->len_blocks -= last_group_size;
        }
 
-       aux_info->sb = calloc(1, EXT4_SUPERBLOCK_SIZE);
+       aux_info->sb = ext4_calloc(1, EXT4_SUPERBLOCK_SIZE);
        if (!aux_info->sb)
                return ENOMEM;
 
-       aux_info->bg_desc_blk = calloc(1, info->block_size);
+       aux_info->bg_desc_blk = ext4_calloc(1, info->block_size);
        if (!aux_info->bg_desc_blk)
                return ENOMEM;
 
@@ -213,9 +213,9 @@ static int create_fs_aux_info(struct fs_aux_info *aux_info,
 static void release_fs_aux_info(struct fs_aux_info *aux_info)
 {
        if (aux_info->sb)
-               free(aux_info->sb);
+               ext4_free(aux_info->sb);
        if (aux_info->bg_desc_blk)
-               free(aux_info->bg_desc_blk);
+               ext4_free(aux_info->bg_desc_blk);
 }
 
 
@@ -467,7 +467,7 @@ int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
        if (r != EOK)
                return r;
 
-       sb = malloc(EXT4_SUPERBLOCK_SIZE);
+       sb = ext4_malloc(EXT4_SUPERBLOCK_SIZE);
        if (!sb)
                goto Finish;
 
@@ -480,7 +480,7 @@ int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
 
 Finish:
        if (sb)
-               free(sb);
+               ext4_free(sb);
        ext4_block_fini(bd);
        return r;
 }