string: add strdup_const and kstrdup_const

Extend Linux compat by adding kstrdup_const(), backed by lib/string.c.
This leverages U-Boots .rodata section on ARM64 to avoid pointlessly
duplicating const strings.

This is used by the Linux CCF_FULL port and may be useful elsewhere
in U-Boot.

Signed-off-by: Casey Connolly <casey.connolly@linaro.org>
This commit is contained in:
Casey Connolly
2026-04-21 11:19:49 -06:00
committed by Tom Rini
parent f5e96fdffc
commit 139f5292e7
4 changed files with 65 additions and 0 deletions
+31
View File
@@ -379,6 +379,37 @@ char * strndup(const char *s, size_t n)
return new;
}
/**
* strdup_const - conditionally duplicate an existing const string
* @s: the string to duplicate
*
* Note: Strings allocated by kstrdup_const should be freed by kfree_const and
* must not be passed to krealloc().
*
* Return: source string if it is in .rodata section otherwise
* fallback to kstrdup.
*/
const char *strdup_const(const char *s)
{
if (is_kernel_rodata((unsigned long)s))
return s;
return strdup(s);
}
/**
* kfree_const - conditionally free memory
* @x: pointer to the memory
*
* Function calls kfree only if @x is not in .rodata section.
*/
void kfree_const(const void *x)
{
if (!is_kernel_rodata((unsigned long)x))
free((void *)x);
}
#endif
#ifndef __HAVE_ARCH_STRSPN