From a9ea7ca3dca81085ca2f998b11aba109cd1a4b0c Mon Sep 17 00:00:00 2001 From: bugobliterator Date: Mon, 16 Mar 2020 18:22:18 +1100 Subject: [PATCH] AP_HAL_ChibiOS: implement standard realloc method --- libraries/AP_HAL_ChibiOS/Util.cpp | 20 ++++++++++++++++++++ libraries/AP_HAL_ChibiOS/Util.h | 1 + 2 files changed, 21 insertions(+) diff --git a/libraries/AP_HAL_ChibiOS/Util.cpp b/libraries/AP_HAL_ChibiOS/Util.cpp index e839b41463..d49a96a964 100644 --- a/libraries/AP_HAL_ChibiOS/Util.cpp +++ b/libraries/AP_HAL_ChibiOS/Util.cpp @@ -86,6 +86,26 @@ void *Util::allocate_heap_memory(size_t size) return heap; } +/* + realloc implementation thanks to wolfssl, used by AP_Scripting + */ +void *Util::std_realloc(void *addr, size_t size) +{ + if (size == 0) { + free(addr); + return nullptr; + } + if (addr == nullptr) { + return malloc(size); + } + void *new_mem = malloc(size); + if (new_mem != nullptr) { + memcpy(new_mem, addr, chHeapGetSize(addr) > size ? size : chHeapGetSize(addr)); + free(addr); + } + return new_mem; +} + void *Util::heap_realloc(void *heap, void *ptr, size_t new_size) { if (heap == nullptr) { diff --git a/libraries/AP_HAL_ChibiOS/Util.h b/libraries/AP_HAL_ChibiOS/Util.h index 9222057d52..e284931e92 100644 --- a/libraries/AP_HAL_ChibiOS/Util.h +++ b/libraries/AP_HAL_ChibiOS/Util.h @@ -38,6 +38,7 @@ public: // 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 new_size) override; + virtual void *std_realloc(void *ptr, size_t new_size) override; #endif // ENABLE_HEAP /*