mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] console: allocate only once instead of twice per log message
Date: Fri,  4 Aug 2023 10:11:47 +0200	[thread overview]
Message-ID: <20230804081147.559379-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230804081147.559379-1-a.fatoum@pengutronix.de>

We do two allocations for each log message, one for the log message
string itself and another for the struct log_entry referencing it.

We could just make the log message directly follow the log entry and
save the space for the pointer and the time to do a second allocation.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/console_common.c | 14 +++++++-------
 include/linux/printk.h  |  2 +-
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/common/console_common.c b/common/console_common.c
index 866a7cbf65dc..c78581299248 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -10,6 +10,7 @@
 #include <errno.h>
 #include <console.h>
 #include <init.h>
+#include <string.h>
 #include <environment.h>
 #include <globalvar.h>
 #include <magicvar.h>
@@ -19,6 +20,7 @@
 #include <malloc.h>
 #include <linux/pstore.h>
 #include <linux/math64.h>
+#include <linux/overflow.h>
 
 #ifndef CONFIG_CONSOLE_NONE
 
@@ -39,7 +41,6 @@ static int barebox_log_max_messages = 1000;
 
 static void log_del(struct log_entry *log)
 {
-	free(log->msg);
 	list_del(&log->list);
 	free(log);
 	barebox_logbuf_num_messages--;
@@ -89,15 +90,14 @@ static void pr_puts(int level, const char *str)
 			log_clean(barebox_log_max_messages - 1);
 
 		if (barebox_log_max_messages >= 0) {
-			log = malloc(sizeof(*log));
+			int msglen;
+
+			msglen = strlen(str);
+			log = malloc(struct_size(log, msg, msglen + 1));
 			if (!log)
 				goto nolog;
 
-			log->msg = strdup(str);
-			if (!log->msg) {
-				free(log);
-				goto nolog;
-			}
+			memcpy(log->msg, str, msglen + 1);
 
 			log->timestamp = get_time_ns();
 			log->level = level;
diff --git a/include/linux/printk.h b/include/linux/printk.h
index 76cdb15d5b4c..cd4c3cb68edb 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -148,9 +148,9 @@ int __pr_memory_display(int level, const void *addr, loff_t offs, unsigned nbyte
 
 struct log_entry {
 	struct list_head list;
-	char *msg;
 	uint64_t timestamp;
 	int level;
+	char msg[];
 };
 
 extern struct list_head barebox_logbuf;
-- 
2.39.2




  reply	other threads:[~2023-08-04  8:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-04  8:11 [PATCH 1/2] linux/printk.h: drop unused log_entry::dummy member Ahmad Fatoum
2023-08-04  8:11 ` Ahmad Fatoum [this message]
2023-08-04  8:31 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230804081147.559379-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox