mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] fs: ramfs: allocate once instead of twice per ramfs chunk
@ 2024-05-15 15:24 Ahmad Fatoum
  2024-05-16  5:56 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2024-05-15 15:24 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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@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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] fs: ramfs: allocate once instead of twice per ramfs chunk
  2024-05-15 15:24 [PATCH] fs: ramfs: allocate once instead of twice per ramfs chunk Ahmad Fatoum
@ 2024-05-16  5:56 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2024-05-16  5:56 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Wed, 15 May 2024 17:24:10 +0200, Ahmad Fatoum wrote:
> There's no reason to maintain two allocations per chunk, so just collect
> them both into the same calloc call.
> 
> No functional change.
> 
> 

Applied, thanks!

[1/1] fs: ramfs: allocate once instead of twice per ramfs chunk
      https://git.pengutronix.de/cgit/barebox/commit/?id=6e502a0ca8d9 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-16  5:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-15 15:24 [PATCH] fs: ramfs: allocate once instead of twice per ramfs chunk Ahmad Fatoum
2024-05-16  5:56 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox