Browse Source

HAL_ChibiOS: added realloc implementation

needed for AP_Scripting
mission-4.1.18
Andrew Tridgell 6 years ago
parent
commit
bcc1bd9752
  1. 41
      libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c
  2. 1
      libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h

41
libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c

@ -214,4 +214,45 @@ size_t mem_available(void) @@ -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

1
libraries/AP_HAL_ChibiOS/hwdef/common/stdio.h

@ -42,6 +42,7 @@ int vsscanf (const char *buf, const char *s, va_list ap); @@ -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);

Loading…
Cancel
Save