From bcc1bd9752e3ef04a23d5b8a48b6ca1cf917133b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 4 Oct 2018 11:25:39 +1000 Subject: [PATCH] HAL_ChibiOS: added realloc implementation needed for AP_Scripting --- .../AP_HAL_ChibiOS/hwdef/common/malloc.c | 41 +++++++++++++++++++ libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h | 1 + 2 files changed, 42 insertions(+) diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c index ce5c1fc33f..6703f278c9 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c @@ -214,4 +214,45 @@ size_t mem_available(void) return totalp; } +/* + realloc implementation thanks to wolfssl, used by AP_Scripting + */ +void *realloc(void *addr, size_t size) +{ + union heap_header *hp; + uint32_t prev_size, new_size; + + void *ptr; + + if(addr == NULL) { + return chHeapAlloc(NULL, size); + } + + /* previous allocated segment is preceded by an heap_header */ + hp = addr - sizeof(union heap_header); + prev_size = hp->used.size; /* size is always multiple of 8 */ + + /* check new size memory alignment */ + if (size % 8 == 0) { + new_size = size; + } else { + new_size = ((int) (size / 8)) * 8 + 8; + } + + if(prev_size >= new_size) { + return addr; + } + + ptr = chHeapAlloc(NULL, size); + if (ptr == NULL) { + return NULL; + } + + memcpy(ptr, addr, prev_size); + + chHeapFree(addr); + + return ptr; +} + #endif // CH_CFG_USE_HEAP diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h index b57111f8f2..6435a22f91 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h @@ -42,6 +42,7 @@ int vsscanf (const char *buf, const char *s, va_list ap); void *malloc(size_t size); void *calloc(size_t nmemb, size_t size); void free(void *ptr); +void *realloc(void *ptr, size_t size); extern int (*vprintf_console_hook)(const char *fmt, va_list arg);