HAL_ChibiOS: added realloc implementation

needed for AP_Scripting
This commit is contained in:
Andrew Tridgell 2018-10-04 11:25:39 +10:00
parent 68576eff32
commit bcc1bd9752
2 changed files with 42 additions and 0 deletions

View File

@ -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

View File

@ -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);