This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 861220f6491bd9518f72600992376666ac52d8b7 Author: dongjiuzhu1 <[email protected]> AuthorDate: Fri Oct 13 22:16:57 2023 +0800 arch/sim: Simplify the implementation of textheap by reuse the heap manager Signed-off-by: Xiang Xiao <[email protected]> --- arch/sim/src/sim/sim_textheap.c | 90 +++++------------------------------------ 1 file changed, 9 insertions(+), 81 deletions(-) diff --git a/arch/sim/src/sim/sim_textheap.c b/arch/sim/src/sim/sim_textheap.c index ef0a29ac01..bc967cdfc8 100644 --- a/arch/sim/src/sim/sim_textheap.c +++ b/arch/sim/src/sim/sim_textheap.c @@ -23,8 +23,7 @@ ****************************************************************************/ #include <nuttx/arch.h> -#include <nuttx/queue.h> -#include <nuttx/kmalloc.h> +#include <nuttx/mm/mm.h> #include "sim_internal.h" @@ -32,16 +31,7 @@ * Private Data ****************************************************************************/ -/* Record memory allocated for text sections by sq_queue_t */ - -struct textheap_s -{ - sq_entry_t entry; - void *p; - size_t size; -}; - -static sq_queue_t g_textheap_list; +static struct mm_heap_s *g_textheap; /**************************************************************************** * Public Functions @@ -57,40 +47,13 @@ static sq_queue_t g_textheap_list; void *up_textheap_memalign(size_t align, size_t size) { - struct textheap_s *node; - void *p; - irqstate_t flags; - - /* host_allocheap (mmap) returns memory aligned to the page size, which - * is always a multiple of the alignment (4/8) for text section. So, we - * don't need to do anything here. - */ - - p = host_allocheap(size); - - flags = up_irq_save(); - - if (p) + if (g_textheap == NULL) { - node = kmm_malloc(sizeof(*node)); - - if (node) - { - node->p = p; - node->size = size; - - sq_addlast(&node->entry, &g_textheap_list); - } - else - { - host_freeheap(p); - p = NULL; - } + g_textheap = mm_initialize("textheap", host_allocheap(SIM_HEAP_SIZE), + SIM_HEAP_SIZE); } - up_irq_restore(flags); - - return p; + return mm_memalign(g_textheap, align, size); } /**************************************************************************** @@ -103,26 +66,10 @@ void *up_textheap_memalign(size_t align, size_t size) void up_textheap_free(void *p) { - sq_entry_t *node; - struct textheap_s *entry; - irqstate_t flags = up_irq_save(); - - for (node = sq_peek(&g_textheap_list); - node != NULL; - node = sq_next(node)) + if (g_textheap != NULL) { - entry = (struct textheap_s *)node; - - if (entry->p == p) - { - sq_rem(node, &g_textheap_list); - host_freeheap(p); - kmm_free(entry); - break; - } + mm_free(g_textheap, p); } - - up_irq_restore(flags); } /**************************************************************************** @@ -135,24 +82,5 @@ void up_textheap_free(void *p) bool up_textheap_heapmember(void *p) { - bool ret = false; - sq_entry_t *node; - struct textheap_s *entry; - irqstate_t flags = up_irq_save(); - - for (node = sq_peek(&g_textheap_list); - node != NULL; - node = sq_next(node)) - { - entry = (struct textheap_s *)node; - - if (entry->p == p) - { - ret = true; - break; - } - } - - up_irq_restore(flags); - return ret; + return g_textheap != NULL && mm_heapmember(g_textheap, p); }
