[PATCH] fs: ramfs: allocate once instead of twice per ramfs chunk

Ahmad Fatoum a.fatoum at pengutronix.de
Wed May 15 08:24:10 PDT 2024


There's no reason to maintain two allocations per chunk, so just collect
them both into the same calloc call.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
I didn't test thoroughly what performance improvement this might bring,
but it looks like a sensible thing to do.
---
 fs/ramfs.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/fs/ramfs.c b/fs/ramfs.c
index 117e69b70c0c..3223beba7212 100644
--- a/fs/ramfs.c
+++ b/fs/ramfs.c
@@ -28,10 +28,10 @@
 #define CHUNK_SIZE	(4096 * 2)
 
 struct ramfs_chunk {
-	char *data;
 	unsigned long ofs;
 	int size;
 	struct list_head list;
+	char data[];
 };
 
 struct ramfs_inode {
@@ -98,19 +98,14 @@ static struct inode *ramfs_get_inode(struct super_block *sb, const struct inode
 
 static struct ramfs_chunk *ramfs_get_chunk(unsigned long size)
 {
-	struct ramfs_chunk *data = malloc(sizeof(struct ramfs_chunk));
-
-	if (!data)
-		return NULL;
+	struct ramfs_chunk *data;
 
 	if (size < MIN_SIZE)
 		size = MIN_SIZE;
 
-	data->data = calloc(size, 1);
-	if (!data->data) {
-		free(data);
+	data = calloc(struct_size(data, data, size), 1);
+	if (!data)
 		return NULL;
-	}
 
 	data->size = size;
 
@@ -119,7 +114,6 @@ static struct ramfs_chunk *ramfs_get_chunk(unsigned long size)
 
 static void ramfs_put_chunk(struct ramfs_chunk *data)
 {
-	free(data->data);
 	free(data);
 }
 
-- 
2.39.2




More information about the barebox mailing list