AP_HAL_ChibiOS: implement standard realloc method

This commit is contained in:
bugobliterator 2020-03-16 18:22:18 +11:00 committed by Randy Mackay
parent 792fe2697a
commit a9ea7ca3dc
2 changed files with 21 additions and 0 deletions

View File

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

View File

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