From 528b13ff5460e8172f375e6dd49fe06f40485664 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 17 Nov 2024 15:56:45 +1100 Subject: [PATCH] AP_HAL_ChibiOS: implement new scripting heap APIs --- libraries/AP_HAL_ChibiOS/Util.cpp | 60 +++++++++++++++---------------- libraries/AP_HAL_ChibiOS/Util.h | 10 +++--- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/libraries/AP_HAL_ChibiOS/Util.cpp b/libraries/AP_HAL_ChibiOS/Util.cpp index 5b6e705735..aeec5aed47 100644 --- a/libraries/AP_HAL_ChibiOS/Util.cpp +++ b/libraries/AP_HAL_ChibiOS/Util.cpp @@ -97,8 +97,10 @@ void Util::free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) #if ENABLE_HEAP - -void *Util::allocate_heap_memory(size_t size) +/* + heap functions used by lua scripting + */ +void *Util::heap_create(uint32_t size) { memory_heap_t *heap = (memory_heap_t *)malloc(size + sizeof(memory_heap_t)); if (heap == nullptr) { @@ -108,10 +110,33 @@ void *Util::allocate_heap_memory(size_t size) return heap; } +void Util::heap_destroy(void *ptr) +{ + free(ptr); +} + +void *Util::heap_allocate(void *heap, uint32_t size) +{ + if (heap == nullptr) { + return nullptr; + } + if (size == 0) { + return nullptr; + } + return chHeapAlloc((memory_heap_t *)heap, size); +} + +void Util::heap_free(void *ptr) +{ + return chHeapFree(ptr); +} + + /* - realloc implementation thanks to wolfssl, used by AP_Scripting + realloc implementation thanks to wolfssl, used by ExpandingString + and ExpandingArray */ -void *Util::std_realloc(void *addr, size_t size) +void *Util::std_realloc(void *addr, uint32_t size) { if (size == 0) { free(addr); @@ -128,33 +153,6 @@ void *Util::std_realloc(void *addr, size_t size) return new_mem; } -void *Util::heap_realloc(void *heap, void *ptr, size_t old_size, size_t new_size) -{ - if (heap == nullptr) { - return nullptr; - } - if (new_size == 0) { - if (ptr != nullptr) { - chHeapFree(ptr); - } - return nullptr; - } - if (ptr == nullptr) { - return chHeapAlloc((memory_heap_t *)heap, new_size); - } - void *new_mem = chHeapAlloc((memory_heap_t *)heap, new_size); - if (new_mem != nullptr) { - const size_t old_size2 = chHeapGetSize(ptr); -#if defined(HAL_DEBUG_BUILD) && !defined(IOMCU_FW) - if (new_size != 0 && old_size2 != old_size) { - INTERNAL_ERROR(AP_InternalError::error_t::invalid_arg_or_result); - } -#endif - memcpy(new_mem, ptr, old_size2 > new_size ? new_size : old_size2); - chHeapFree(ptr); - } - return new_mem; -} #endif // ENABLE_HEAP #endif // CH_CFG_USE_HEAP diff --git a/libraries/AP_HAL_ChibiOS/Util.h b/libraries/AP_HAL_ChibiOS/Util.h index f11dbf7239..9fae8f9490 100644 --- a/libraries/AP_HAL_ChibiOS/Util.h +++ b/libraries/AP_HAL_ChibiOS/Util.h @@ -49,10 +49,12 @@ public: void free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) override; #if ENABLE_HEAP - // heap functions, note that a heap once alloc'd cannot be dealloc'd - virtual void *allocate_heap_memory(size_t size) override; - virtual void *heap_realloc(void *heap, void *ptr, size_t old_size, size_t new_size) override; - virtual void *std_realloc(void *ptr, size_t new_size) override; + // heap functions for scripting support + void *heap_create(uint32_t size) override; + void heap_destroy(void *p) override; + void *heap_allocate(void *heap, uint32_t size) override; + void heap_free(void *ptr) override; + void *std_realloc(void *ptr, uint32_t new_size) override; #endif // ENABLE_HEAP /*