mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-05 07:28:29 -04:00
AP_HAL_ChibiOS: implement new scripting heap APIs
This commit is contained in:
parent
9f714dc23e
commit
528b13ff54
@ -97,8 +97,10 @@ void Util::free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type)
|
|||||||
|
|
||||||
|
|
||||||
#if ENABLE_HEAP
|
#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));
|
memory_heap_t *heap = (memory_heap_t *)malloc(size + sizeof(memory_heap_t));
|
||||||
if (heap == nullptr) {
|
if (heap == nullptr) {
|
||||||
@ -108,10 +110,33 @@ void *Util::allocate_heap_memory(size_t size)
|
|||||||
return heap;
|
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) {
|
if (size == 0) {
|
||||||
free(addr);
|
free(addr);
|
||||||
@ -128,33 +153,6 @@ void *Util::std_realloc(void *addr, size_t size)
|
|||||||
return new_mem;
|
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 // ENABLE_HEAP
|
||||||
|
|
||||||
#endif // CH_CFG_USE_HEAP
|
#endif // CH_CFG_USE_HEAP
|
||||||
|
@ -49,10 +49,12 @@ public:
|
|||||||
void free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) override;
|
void free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) override;
|
||||||
|
|
||||||
#if ENABLE_HEAP
|
#if ENABLE_HEAP
|
||||||
// heap functions, note that a heap once alloc'd cannot be dealloc'd
|
// heap functions for scripting support
|
||||||
virtual void *allocate_heap_memory(size_t size) override;
|
void *heap_create(uint32_t size) override;
|
||||||
virtual void *heap_realloc(void *heap, void *ptr, size_t old_size, size_t new_size) override;
|
void heap_destroy(void *p) override;
|
||||||
virtual void *std_realloc(void *ptr, size_t new_size) 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
|
#endif // ENABLE_HEAP
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user