From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: h.assmann@pengutronix.de, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 3/4] bootsource: have bootsource_get_alias_name return const char *
Date: Fri, 15 Nov 2024 12:57:35 +0100 [thread overview]
Message-ID: <20241115115736.3945523-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20241115115736.3945523-1-a.fatoum@pengutronix.de>
The only user of bootsource_get_alias_name() calls it and frees it
shortly after and users in board code will likely do the same.
Instead of allocating every time, let's just return a static array and
expect users to call strdup() if they want to keep the bootsource valid
for longer.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/bootsource.c | 23 +++++++++++------------
include/bootsource.h | 2 +-
2 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/common/bootsource.c b/common/bootsource.c
index 6808c9c51d88..3c8f53f3a890 100644
--- a/common/bootsource.c
+++ b/common/bootsource.c
@@ -72,27 +72,24 @@ const char *bootsource_get_alias_stem(enum bootsource src)
/**
* bootsource_get_alias_name() - Get the name of the bootsource alias
*
- * This function will return newly allocated string containing name of
+ * This function will return a pointer to a static string containing name of
* the alias that is expected to point to DTB node corresponding to
* detected bootsource
*
- * NOTE: Caller is expected to free() the string allocated by this
- * function
*/
-char *bootsource_get_alias_name(void)
+const char *bootsource_get_alias_name(void)
{
+ static char buf[sizeof("i2c-eeprom-2147483647")];
const char *stem;
+ int ret;
/*
* If alias name was overridden via
* bootsource_set_alias_name() return that value without
* asking any questions.
- *
- * Note that we have to strdup() the result to make it
- * free-able.
*/
if (bootsource_alias_name)
- return strdup(bootsource_alias_name);
+ return bootsource_alias_name;
stem = bootsource_get_alias_stem(bootsource);
if (!stem)
@@ -105,13 +102,17 @@ char *bootsource_get_alias_name(void)
if (bootsource_instance == BOOTSOURCE_INSTANCE_UNKNOWN)
return NULL;
- return basprintf("%s%d", stem, bootsource_instance);
+ ret = snprintf(buf, sizeof(buf), "%s%d", stem, bootsource_instance);
+ if (ret < 0 || ret >= sizeof(buf))
+ return NULL;
+
+ return buf;
}
struct device_node *bootsource_of_node_get(struct device_node *root)
{
struct device_node *np;
- char *alias_name;
+ const char *alias_name;
alias_name = bootsource_get_alias_name();
if (!alias_name)
@@ -119,8 +120,6 @@ struct device_node *bootsource_of_node_get(struct device_node *root)
np = of_find_node_by_alias(root, alias_name);
- free(alias_name);
-
return np;
}
diff --git a/include/bootsource.h b/include/bootsource.h
index 33ad26946059..9fb77a0bb0a9 100644
--- a/include/bootsource.h
+++ b/include/bootsource.h
@@ -29,7 +29,7 @@ enum bootsource bootsource_get(void);
enum bootsource bootsource_get_device(void);
int bootsource_get_instance(void);
void bootsource_set_alias_name(const char *name);
-char *bootsource_get_alias_name(void);
+const char *bootsource_get_alias_name(void);
const char *bootsource_to_string(enum bootsource src);
const char *bootsource_get_alias_stem(enum bootsource bs);
int bootsource_of_alias_xlate(enum bootsource bs, int instance);
--
2.39.5
next prev parent reply other threads:[~2024-11-15 11:58 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-15 11:57 [PATCH 1/4] of: add helpers to get alias from device node path + property name Ahmad Fatoum
2024-11-15 11:57 ` [PATCH 2/4] environment: implement of_env_get_alias_by_path helper Ahmad Fatoum
2024-11-15 11:57 ` Ahmad Fatoum [this message]
2024-11-15 11:57 ` [PATCH 4/4] ARM: i.MX8MP: tqma8mpxl: fix bbu registration for other boards using SoM Ahmad Fatoum
2024-11-15 12:08 ` [PATCH 1/4] of: add helpers to get alias from device node path + property name Ahmad Fatoum
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=20241115115736.3945523-3-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=h.assmann@pengutronix.de \
/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