Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting: backport memory handling changes to 4.6 #28680

Merged
merged 21 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Tools/AP_Periph/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def build(bld):
'AP_RobotisServo',
'AP_FETtecOneWire',
'GCS_MAVLink',
'AP_Relay'
'AP_Relay',
'AP_MultiHeap',
]

bld.ap_stlib(
Expand Down
1 change: 1 addition & 0 deletions Tools/ardupilotwaf/ardupilotwaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
'AP_Beacon',
'AP_Arming',
'AP_RCMapper',
'AP_MultiHeap',
]

def get_legacy_defines(sketch_name, bld):
Expand Down
148 changes: 0 additions & 148 deletions libraries/AP_Common/MultiHeap.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions libraries/AP_Common/MultiHeap.h

This file was deleted.

2 changes: 2 additions & 0 deletions libraries/AP_HAL/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#else
#include <time.h>
#endif
#include <AP_InternalError/AP_InternalError.h>
#include <AP_Math/AP_Math.h>

/* Helper class implements AP_HAL::Print so we can use utility/vprintf */
class BufferPrinter : public AP_HAL::BetterStream {
Expand Down
13 changes: 6 additions & 7 deletions libraries/AP_HAL/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,15 @@ class AP_HAL::Util {
virtual void free_type(void *ptr, size_t size, Memory_Type mem_type) { return free(ptr); }

#if ENABLE_HEAP
// heap functions, note that a heap once alloc'd cannot be dealloc'd
virtual void *allocate_heap_memory(size_t size) = 0;
virtual void *heap_realloc(void *heap, void *ptr, size_t old_size, size_t new_size) = 0;
/*
heap functions used by non-scripting
*/
#if USE_LIBC_REALLOC
virtual void *std_realloc(void *ptr, size_t new_size) { return realloc(ptr, new_size); }
virtual void *std_realloc(void *ptr, uint32_t new_size) { return realloc(ptr, new_size); }
#else
virtual void *std_realloc(void *ptr, size_t new_size) = 0;
virtual void *std_realloc(void *ptr, uint32_t new_size) = 0;
#endif // USE_LIBC_REALLOC
#endif // ENABLE_HEAP

#endif

/**
how much free memory do we have in bytes. If unknown return 4096
Expand Down
43 changes: 3 additions & 40 deletions libraries/AP_HAL_ChibiOS/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,11 @@ 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)
{
memory_heap_t *heap = (memory_heap_t *)malloc(size + sizeof(memory_heap_t));
if (heap == nullptr) {
return nullptr;
}
chHeapObjectInit(heap, heap + 1U, size);
return heap;
}

/*
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);
Expand All @@ -128,33 +118,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
Expand Down
9 changes: 1 addition & 8 deletions libraries/AP_HAL_ChibiOS/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ class ChibiOS::Util : public AP_HAL::Util {
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;
void *std_realloc(void *ptr, uint32_t new_size) override;
#endif // ENABLE_HEAP

/*
Expand Down Expand Up @@ -142,10 +139,6 @@ class ChibiOS::Util : public AP_HAL::Util {
FlashBootloader flash_bootloader() override;
#endif

#if ENABLE_HEAP
static memory_heap_t scripting_heap;
#endif // ENABLE_HEAP

// stm32F4 and F7 have 20 total RTC backup registers. We use the first one for boot type
// flags, so 19 available for persistent data
static_assert(sizeof(persistent_data) <= 19*4, "watchdog persistent data too large");
Expand Down
30 changes: 2 additions & 28 deletions libraries/AP_HAL_ESP32/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,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)
{
void *buf = calloc(1, size);
if (buf == nullptr) {
return nullptr;
}

multi_heap_handle_t *heap = (multi_heap_handle_t *)calloc(1, sizeof(multi_heap_handle_t));
if (heap != nullptr) {
auto hp = multi_heap_register(buf, size);
memcpy(heap, &hp, sizeof(multi_heap_handle_t));
}

return heap;
}

void *Util::heap_realloc(void *heap, void *ptr, size_t old_size, size_t new_size)
{
if (heap == nullptr) {
return nullptr;
}

return multi_heap_realloc(*(multi_heap_handle_t *)heap, ptr, new_size);
}

/*
realloc implementation thanks to wolfssl, used by AP_Scripting
realloc implementation thanks to wolfssl, used by ExpandingString
*/
void *Util::std_realloc(void *addr, size_t size)
void *Util::std_realloc(void *addr, uint32_t size)
{
if (size == 0) {
free(addr);
Expand Down
8 changes: 1 addition & 7 deletions libraries/AP_HAL_ESP32/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class ESP32::Util : public AP_HAL::Util

#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;
virtual void *std_realloc(void *ptr, uint32_t new_size) override;
#endif // ENABLE_HEAP

/*
Expand Down Expand Up @@ -85,10 +83,6 @@ class ESP32::Util : public AP_HAL::Util
FlashBootloader flash_bootloader() override;
#endif

#if ENABLE_HEAP
// static memory_heap_t scripting_heap;
#endif // ENABLE_HEAP

// stm32F4 and F7 have 20 total RTC backup registers. We use the first one for boot type
// flags, so 19 available for persistent data
static_assert(sizeof(persistent_data) <= 19*4, "watchdog persistent data too large");
Expand Down
Loading
Loading