AP_HAL_ChibiOS: implement new scripting heap APIs

This commit is contained in:
Andrew Tridgell 2024-11-17 15:56:45 +11:00
parent 9f75ad1be8
commit c999340786
2 changed files with 35 additions and 35 deletions

View File

@ -97,8 +97,10 @@ void Util::free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type)
#if ENABLE_HEAP
void *Util::allocate_heap_memory(size_t size)
/*
heap functions used by lua scripting
*/
void *Util::heap_create(uint32_t size)
{
memory_heap_t *heap = (memory_heap_t *)malloc(size + sizeof(memory_heap_t));
if (heap == nullptr) {
@ -108,10 +110,33 @@ void *Util::allocate_heap_memory(size_t size)
return heap;
}
void Util::heap_destroy(void *ptr)
{
free(ptr);
}
void *Util::heap_allocate(void *heap, uint32_t size)
{
if (heap == nullptr) {
return nullptr;
}
if (size == 0) {
return nullptr;
}
return chHeapAlloc((memory_heap_t *)heap, size);
}
void Util::heap_free(void *ptr)
{
return chHeapFree(ptr);
}
/*
realloc implementation thanks to wolfssl, used by AP_Scripting
realloc implementation thanks to wolfssl, used by ExpandingString
and ExpandingArray
*/
void *Util::std_realloc(void *addr, size_t size)
void *Util::std_realloc(void *addr, uint32_t size)
{
if (size == 0) {
free(addr);
@ -128,33 +153,6 @@ void *Util::std_realloc(void *addr, size_t size)
return new_mem;
}
void *Util::heap_realloc(void *heap, void *ptr, size_t old_size, size_t new_size)
{
if (heap == nullptr) {
return nullptr;
}
if (new_size == 0) {
if (ptr != nullptr) {
chHeapFree(ptr);
}
return nullptr;
}
if (ptr == nullptr) {
return chHeapAlloc((memory_heap_t *)heap, new_size);
}
void *new_mem = chHeapAlloc((memory_heap_t *)heap, new_size);
if (new_mem != nullptr) {
const size_t old_size2 = chHeapGetSize(ptr);
#if defined(HAL_DEBUG_BUILD) && !defined(IOMCU_FW)
if (new_size != 0 && old_size2 != old_size) {
INTERNAL_ERROR(AP_InternalError::error_t::invalid_arg_or_result);
}
#endif
memcpy(new_mem, ptr, old_size2 > new_size ? new_size : old_size2);
chHeapFree(ptr);
}
return new_mem;
}
#endif // ENABLE_HEAP
#endif // CH_CFG_USE_HEAP

View File

@ -49,10 +49,12 @@ public:
void free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) override;
#if ENABLE_HEAP
// 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 old_size, size_t new_size) override;
virtual void *std_realloc(void *ptr, size_t new_size) override;
// heap functions for scripting support
void *heap_create(uint32_t size) override;
void heap_destroy(void *p) override;
void *heap_allocate(void *heap, uint32_t size) override;
void heap_free(void *ptr) override;
void *std_realloc(void *ptr, uint32_t new_size) override;
#endif // ENABLE_HEAP
/*