From 0a7171bc62f4ff630fde65b7080bfd7fa4d655e4 Mon Sep 17 00:00:00 2001 From: Ricard Rosson Date: Fri, 24 Jul 2026 07:48:17 +0100 Subject: [PATCH] arch: fall back to the kernel heap when there is no separate text heap When an architecture does not provide a separate text heap (CONFIG_ARCH_USE_TEXT_HEAP is not selected), redirect the text heap API to the normal kernel heap in include/nuttx/arch.h. On flat-memory architectures the kernel heap is executable, so code written against the portable API (up_textheap_memalign() and friends) now works on every such port without per-arch stubs that just wrap kmm_memalign(). Suggested by review on the previous revision of this change, which added exactly such a stub for rp23xx. up_textheap_data_address() and up_textheap_data_sync() identity fallbacks become available unconditionally as well. Gate the sim implementation on CONFIG_ARCH_USE_TEXT_HEAP: it was compiled unconditionally (harmless before, but it would now collide with the fallback macros), matching how every other implementation is already gated in its Make.defs/CMakeLists. Assisted-by: Claude (Anthropic Claude Code) Signed-off-by: Ricard Rosson --- arch/sim/src/sim/sim_sectionheap.c | 4 ++++ include/nuttx/arch.h | 25 +++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/arch/sim/src/sim/sim_sectionheap.c b/arch/sim/src/sim/sim_sectionheap.c index 8a9e0b3625be4..5e3db77fd1a7f 100644 --- a/arch/sim/src/sim/sim_sectionheap.c +++ b/arch/sim/src/sim/sim_sectionheap.c @@ -33,7 +33,9 @@ * Private Data ****************************************************************************/ +#ifdef CONFIG_ARCH_USE_TEXT_HEAP static struct mm_heap_s *g_textheap; +#endif static struct mm_heap_s *g_dataheap; /**************************************************************************** @@ -48,6 +50,7 @@ static struct mm_heap_s *g_dataheap; * ****************************************************************************/ +#ifdef CONFIG_ARCH_USE_TEXT_HEAP #ifdef CONFIG_ARCH_USE_SEPARATED_SECTION void *up_textheap_memalign(const char *sectname, size_t align, size_t size) #else @@ -92,6 +95,7 @@ bool up_textheap_heapmember(void *p) { return g_textheap != NULL && mm_heapmember(g_textheap, p); } +#endif /* CONFIG_ARCH_USE_TEXT_HEAP */ /**************************************************************************** * Name: up_dataheap_memalign diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index e1a598c222b2d..2d705f5fc197d 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -847,6 +847,11 @@ void up_extraheaps_init(void); * Description: * Allocate memory for text with the specified alignment and sectname. * + * If the architecture does not provide a separate text heap + * (CONFIG_ARCH_USE_TEXT_HEAP is not set), the text heap API falls back + * to the normal kernel heap: on flat-memory architectures the kernel + * heap is executable, so no separate allocator is needed. + * ****************************************************************************/ #if defined(CONFIG_ARCH_USE_TEXT_HEAP) @@ -856,6 +861,12 @@ FAR void *up_textheap_memalign(FAR const char *sectname, # else FAR void *up_textheap_memalign(size_t align, size_t size); # endif +#else +# if defined(CONFIG_ARCH_USE_SEPARATED_SECTION) +# define up_textheap_memalign(s,a,z) kmm_memalign(a,z) +# else +# define up_textheap_memalign(a,z) kmm_memalign(a,z) +# endif #endif /**************************************************************************** @@ -868,6 +879,8 @@ FAR void *up_textheap_memalign(size_t align, size_t size); #if defined(CONFIG_ARCH_USE_TEXT_HEAP) void up_textheap_free(FAR void *p); +#else +# define up_textheap_free(p) kmm_free(p) #endif /**************************************************************************** @@ -880,6 +893,8 @@ void up_textheap_free(FAR void *p); #if defined(CONFIG_ARCH_USE_TEXT_HEAP) bool up_textheap_heapmember(FAR void *p); +#else +# define up_textheap_heapmember(p) kmm_heapmember(p) #endif /**************************************************************************** @@ -900,13 +915,12 @@ bool up_textheap_heapmember(FAR void *p); * ****************************************************************************/ -#if defined(CONFIG_ARCH_USE_TEXT_HEAP) -#if defined(CONFIG_ARCH_HAVE_TEXT_HEAP_SEPARATE_DATA_ADDRESS) +#if defined(CONFIG_ARCH_USE_TEXT_HEAP) && \ + defined(CONFIG_ARCH_HAVE_TEXT_HEAP_SEPARATE_DATA_ADDRESS) FAR void *up_textheap_data_address(FAR void *p); #else #define up_textheap_data_address(p) ((FAR void *)p) #endif -#endif /**************************************************************************** * Name: up_textheap_data_sync @@ -918,13 +932,12 @@ FAR void *up_textheap_data_address(FAR void *p); * ****************************************************************************/ -#if defined(CONFIG_ARCH_USE_TEXT_HEAP) -#if defined(CONFIG_ARCH_HAVE_TEXT_HEAP_SEPARATE_DATA_ADDRESS) +#if defined(CONFIG_ARCH_USE_TEXT_HEAP) && \ + defined(CONFIG_ARCH_HAVE_TEXT_HEAP_SEPARATE_DATA_ADDRESS) void up_textheap_data_sync(void); #else #define up_textheap_data_sync() do {} while (0) #endif -#endif /**************************************************************************** * Name: up_dataheap_memalign