[syslinux:elflink] xfs: Avoid useless malloc()'s and free()'s

syslinux-bot for Paulo Alcantara pcacjr at zytor.com
Thu Jan 24 09:45:06 PST 2013


Commit-ID:  b90e1ce83631b0baa4d1e097d418e33d7316298a
Gitweb:     http://www.syslinux.org/commit/b90e1ce83631b0baa4d1e097d418e33d7316298a
Author:     Paulo Alcantara <pcacjr at zytor.com>
AuthorDate: Mon, 21 Jan 2013 17:38:10 -0200
Committer:  Paulo Alcantara <pcacjr at zytor.com>
CommitDate: Mon, 21 Jan 2013 17:38:10 -0200

xfs: Avoid useless malloc()'s and free()'s

This patch speeds up the process of retrieving and comparing entry names
while looking up inodes.

Signed-off-by: Paulo Alcantara <pcacjr at zytor.com>

---
 core/fs/xfs/xfs_dir2.c    | 57 ++++++++++++++---------------------------------
 core/fs/xfs/xfs_dir2.h    |  2 +-
 core/fs/xfs/xfs_readdir.c | 27 ++++++----------------
 3 files changed, 25 insertions(+), 61 deletions(-)

diff --git a/core/fs/xfs/xfs_dir2.c b/core/fs/xfs/xfs_dir2.c
index 97a3eb0..4c63ec9 100644
--- a/core/fs/xfs/xfs_dir2.c
+++ b/core/fs/xfs/xfs_dir2.c
@@ -28,22 +28,16 @@
 
 #include "xfs_dir2.h"
 
-char *xfs_dir2_get_entry_name(uint8_t *start, uint8_t *end)
+int xfs_dir2_entry_name_cmp(uint8_t *start, uint8_t *end, const char *name)
 {
-    char *s;
-    char *p;
+    if (!name || (strlen(name) != end - start))
+	return -1;
 
-    s = malloc(end - start + 1);
-    if (!s)
-	malloc_error("string");
-
-    p = s;
     while (start < end)
-	*p++ = *start++;
-
-    *p = '\0';
+	if (*start++ != *name++)
+	    return -1;
 
-    return s;
+    return 0;
 }
 
 uint32_t xfs_dir2_da_hashname(const uint8_t *name, int namelen)
@@ -115,19 +109,12 @@ struct inode *xfs_dir2_local_find_entry(const char *dname, struct inode *parent,
     while (count--) {
 	uint8_t *start_name = &sf_entry->name[0];
 	uint8_t *end_name = start_name + sf_entry->namelen;
-	char *name;
-
-	name = xfs_dir2_get_entry_name(start_name, end_name);
 
-	xfs_debug("entry name: %s", name);
-
-	if (!strncmp(name, dname, strlen(dname))) {
-	    free(name);
+	if (!xfs_dir2_entry_name_cmp(start_name, end_name, dname)) {
+	    xfs_debug("Found entry %s", dname);
 	    goto found;
 	}
 
-	free(name);
-
 	sf_entry = (xfs_dir2_sf_entry_t *)((uint8_t *)sf_entry +
 					   offsetof(struct xfs_dir2_sf_entry,
 						    name[0]) +
@@ -216,7 +203,6 @@ struct inode *xfs_dir2_block_find_entry(const char *dname, struct inode *parent,
     while (p < endp) {
         uint8_t *start_name;
         uint8_t *end_name;
-        char *name;
 
         dup = (xfs_dir2_data_unused_t *)p;
         if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
@@ -228,14 +214,12 @@ struct inode *xfs_dir2_block_find_entry(const char *dname, struct inode *parent,
 
         start_name = &dep->name[0];
         end_name = start_name + dep->namelen;
-        name = xfs_dir2_get_entry_name(start_name, end_name);
 
-        if (!strncmp(name, dname, strlen(dname))) {
-            free(name);
+	if (!xfs_dir2_entry_name_cmp(start_name, end_name, dname)) {
+	    xfs_debug("Found entry %s", dname);
             goto found;
         }
 
-        free(name);
 	p += xfs_dir2_data_entsize(dep->namelen);
     }
 
@@ -305,7 +289,6 @@ struct inode *xfs_dir2_leaf_find_entry(const char *dname, struct inode *parent,
     xfs_dir2_data_hdr_t *data_hdr;
     uint8_t *start_name;
     uint8_t *end_name;
-    char *name;
     xfs_intino_t ino;
     xfs_dinode_t *ncore;
     uint8_t *buf = NULL;
@@ -325,7 +308,7 @@ struct inode *xfs_dir2_leaf_find_entry(const char *dname, struct inode *parent,
     }
 
     if (!leaf->hdr.count)
-        goto out;
+	goto out;
 
     hashwant = xfs_dir2_da_hashname((uint8_t *)dname, strlen(dname));
 
@@ -382,14 +365,11 @@ struct inode *xfs_dir2_leaf_find_entry(const char *dname, struct inode *parent,
 
         start_name = &dep->name[0];
         end_name = start_name + dep->namelen;
-        name = xfs_dir2_get_entry_name(start_name, end_name);
 
-        if (!strncmp(name, dname, strlen(dname))) {
-            free(name);
+	if (!xfs_dir2_entry_name_cmp(start_name, end_name, dname)) {
+	    xfs_debug("Found entry %s", dname);
             goto found;
         }
-
-        free(name);
     }
 
 out1:
@@ -565,7 +545,6 @@ struct inode *xfs_dir2_node_find_entry(const char *dname, struct inode *parent,
     struct inode *ip;
     uint8_t *start_name;
     uint8_t *end_name;
-    char *name;
     int low;
     int high;
     int mid = 0;
@@ -706,13 +685,11 @@ struct inode *xfs_dir2_node_find_entry(const char *dname, struct inode *parent,
 
         start_name = &dep->name[0];
         end_name = start_name + dep->namelen;
-        name = xfs_dir2_get_entry_name(start_name, end_name);
-        if (!strncmp(name, dname, strlen(dname))) {
-            free(name);
-            goto found;
-        }
 
-        free(name);
+	if (!xfs_dir2_entry_name_cmp(start_name, end_name, dname)) {
+	    xfs_debug("Found entry %s", dname);
+	    goto found;
+        }
     }
 
 out1:
diff --git a/core/fs/xfs/xfs_dir2.h b/core/fs/xfs/xfs_dir2.h
index e1b9622..8e4e985 100644
--- a/core/fs/xfs/xfs_dir2.h
+++ b/core/fs/xfs/xfs_dir2.h
@@ -23,7 +23,7 @@
 
 #include "xfs.h"
 
-char *xfs_dir2_get_entry_name(uint8_t *start, uint8_t *end);
+int xfs_dir2_entry_name_cmp(uint8_t *start, uint8_t *end, const char *name);
 void *xfs_dir2_get_dirblks(struct fs_info *fs, block_t startblock,
 			   xfs_filblks_t c);
 uint32_t xfs_dir2_da_hashname(const uint8_t *name, int namelen);
diff --git a/core/fs/xfs/xfs_readdir.c b/core/fs/xfs/xfs_readdir.c
index e3e6285..bbfc026 100644
--- a/core/fs/xfs/xfs_readdir.c
+++ b/core/fs/xfs/xfs_readdir.c
@@ -55,7 +55,8 @@ static int fill_dirent(struct fs_info *fs, struct dirent *dirent,
     else if (be16_to_cpu(core->di_mode) & S_IFLNK)
         dirent->d_type = DT_LNK;
 
-    memcpy(dirent->d_name, name, namelen + 1);
+    memcpy(dirent->d_name, name, namelen);
+    dirent->d_name[namelen] = '\0';
 
     return 0;
 }
@@ -69,7 +70,6 @@ int xfs_readdir_dir2_local(struct file *file, struct dirent *dirent,
     uint32_t offset = file->offset;
     uint8_t *start_name;
     uint8_t *end_name;
-    char *name;
     xfs_ino_t ino;
     struct fs_info *fs = file->fs;
     int retval = 0;
@@ -100,21 +100,17 @@ int xfs_readdir_dir2_local(struct file *file, struct dirent *dirent,
     start_name = &sf_entry->name[0];
     end_name = start_name + sf_entry->namelen;
 
-    name = xfs_dir2_get_entry_name(start_name, end_name);
-
     ino = xfs_dir2_sf_get_inumber(sf, (xfs_dir2_inou_t *)(
 				      (uint8_t *)sf_entry +
 				      offsetof(struct xfs_dir2_sf_entry,
 					       name[0]) +
 				      sf_entry->namelen));
 
-    retval = fill_dirent(fs, dirent, file->offset, ino, (char *)name,
+    retval = fill_dirent(fs, dirent, file->offset, ino, (char *)start_name,
 			 end_name - start_name);
     if (retval)
 	xfs_error("Failed to fill in dirent structure");
 
-    free(name);
-
     return retval;
 }
 
@@ -133,7 +129,6 @@ int xfs_readdir_dir2_block(struct file *file, struct dirent *dirent,
     xfs_dir2_data_entry_t *dep;
     uint8_t *start_name;
     uint8_t *end_name;
-    char *name;
     xfs_ino_t ino;
     int retval = 0;
 
@@ -181,17 +176,15 @@ int xfs_readdir_dir2_block(struct file *file, struct dirent *dirent,
 
     start_name = &dep->name[0];
     end_name = start_name + dep->namelen;
-    name = xfs_dir2_get_entry_name(start_name, end_name);
 
     ino = be64_to_cpu(dep->inumber);
 
-    retval = fill_dirent(fs, dirent, file->offset, ino, name,
+    retval = fill_dirent(fs, dirent, file->offset, ino, (char *)start_name,
 			 end_name - start_name);
     if (retval)
 	xfs_error("Failed to fill in dirent structure");
 
     free(dirblk_buf);
-    free(name);
 
     return retval;
 }
@@ -210,7 +203,6 @@ int xfs_readdir_dir2_leaf(struct file *file, struct dirent *dirent,
     xfs_dir2_data_hdr_t *data_hdr;
     uint8_t *start_name;
     uint8_t *end_name;
-    char *name;
     xfs_intino_t ino;
     uint8_t *buf = NULL;
     int retval = 0;
@@ -260,16 +252,14 @@ int xfs_readdir_dir2_leaf(struct file *file, struct dirent *dirent,
 
     start_name = &dep->name[0];
     end_name = start_name + dep->namelen;
-    name = xfs_dir2_get_entry_name(start_name, end_name);
 
     ino = be64_to_cpu(dep->inumber);
 
-    retval = fill_dirent(fs, dirent, file->offset, ino, name,
+    retval = fill_dirent(fs, dirent, file->offset, ino, (char *)start_name,
 			 end_name - start_name);
     if (retval)
 	xfs_error("Failed to fill in dirent structure");
 
-    free(name);
     free(buf);
     free(leaf);
 
@@ -301,7 +291,6 @@ int xfs_readdir_dir2_node(struct file *file, struct dirent *dirent,
     xfs_dir2_data_entry_t *dep;
     uint8_t *start_name;
     uint8_t *end_name;
-    char *name;
     uint32_t db;
     uint8_t *buf = NULL;
     int retval = 0;
@@ -384,14 +373,12 @@ try_next_btree:
 
     start_name = &dep->name[0];
     end_name = start_name + dep->namelen;
-    name = xfs_dir2_get_entry_name(start_name, end_name);
 
-    retval = fill_dirent(fs, dirent, 0, be64_to_cpu(dep->inumber), name,
-			 end_name - start_name);
+    retval = fill_dirent(fs, dirent, 0, be64_to_cpu(dep->inumber),
+			 (char *)start_name, end_name - start_name);
     if (retval)
 	xfs_error("Failed to fill in dirent structure");
 
-    free(name);
     free(buf);
     free(leaf);
     free(node);


More information about the Syslinux-commits mailing list