AP_Scripting: use MultiHeap class

This commit is contained in:
Andrew Tridgell 2022-12-22 09:36:11 +11:00
parent 22a16f9672
commit b45be75a98
2 changed files with 19 additions and 17 deletions

View File

@ -34,12 +34,13 @@ uint32_t lua_scripts::last_print_ms;
lua_scripts::lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_options, struct AP_Scripting::terminal_s &_terminal) lua_scripts::lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_options, struct AP_Scripting::terminal_s &_terminal)
: _vm_steps(vm_steps), : _vm_steps(vm_steps),
_debug_options(debug_options), _debug_options(debug_options),
terminal(_terminal) { terminal(_terminal)
_heap = hal.util->allocate_heap_memory(heap_size); {
_heap.create(heap_size, 4);
} }
lua_scripts::~lua_scripts() { lua_scripts::~lua_scripts() {
free(_heap); _heap.destroy();
} }
void lua_scripts::hook(lua_State *L, lua_Debug *ar) { void lua_scripts::hook(lua_State *L, lua_Debug *ar) {
@ -69,7 +70,7 @@ void lua_scripts::set_and_print_new_error_message(MAV_SEVERITY severity, const c
// reset buffer and print count // reset buffer and print count
print_error_count = 0; print_error_count = 0;
if (error_msg_buf) { if (error_msg_buf) {
hal.util->heap_realloc(_heap, error_msg_buf, 0); _heap.deallocate(error_msg_buf);
error_msg_buf = nullptr; error_msg_buf = nullptr;
} }
@ -92,7 +93,7 @@ void lua_scripts::set_and_print_new_error_message(MAV_SEVERITY severity, const c
} }
// allocate buffer on scripting heap // allocate buffer on scripting heap
error_msg_buf = (char *)hal.util->heap_realloc(_heap, nullptr, len+1); error_msg_buf = (char *)_heap.allocate(len+1);
if (!error_msg_buf) { if (!error_msg_buf) {
// allocation failed // allocation failed
va_end(arg_list); va_end(arg_list);
@ -170,7 +171,7 @@ lua_scripts::script_info *lua_scripts::load_script(lua_State *L, char *filename)
const int loadMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0); const int loadMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
const uint32_t loadStart = AP_HAL::micros(); const uint32_t loadStart = AP_HAL::micros();
script_info *new_script = (script_info *)hal.util->heap_realloc(_heap, nullptr, sizeof(script_info)); script_info *new_script = (script_info *)_heap.allocate(sizeof(script_info));
if (new_script == nullptr) { if (new_script == nullptr) {
// No memory, shouldn't happen, we even attempted to do a GC // No memory, shouldn't happen, we even attempted to do a GC
set_and_print_new_error_message(MAV_SEVERITY_CRITICAL, "Insufficent memory loading %s", filename); set_and_print_new_error_message(MAV_SEVERITY_CRITICAL, "Insufficent memory loading %s", filename);
@ -242,7 +243,7 @@ void lua_scripts::load_all_scripts_in_dir(lua_State *L, const char *dirname) {
// FIXME: because chunk name fetching is not working we are allocating and storing an extra string we shouldn't need to // FIXME: because chunk name fetching is not working we are allocating and storing an extra string we shouldn't need to
size_t size = strlen(dirname) + strlen(de->d_name) + 2; size_t size = strlen(dirname) + strlen(de->d_name) + 2;
char * filename = (char *) hal.util->heap_realloc(_heap, nullptr, size); char * filename = (char *) _heap.allocate(size);
if (filename == nullptr) { if (filename == nullptr) {
continue; continue;
} }
@ -251,7 +252,7 @@ void lua_scripts::load_all_scripts_in_dir(lua_State *L, const char *dirname) {
// we have something that looks like a lua file, attempt to load it // we have something that looks like a lua file, attempt to load it
script_info * script = load_script(L, filename); script_info * script = load_script(L, filename);
if (script == nullptr) { if (script == nullptr) {
hal.util->heap_realloc(_heap, filename, 0); _heap.deallocate(filename);
continue; continue;
} }
reschedule_script(script); reschedule_script(script);
@ -371,8 +372,8 @@ void lua_scripts::remove_script(lua_State *L, script_info *script) {
// state could be null if we are force killing all scripts // state could be null if we are force killing all scripts
luaL_unref(L, LUA_REGISTRYINDEX, script->lua_ref); luaL_unref(L, LUA_REGISTRYINDEX, script->lua_ref);
} }
hal.util->heap_realloc(_heap, script->name, 0); _heap.deallocate(script->name);
hal.util->heap_realloc(_heap, script, 0); _heap.deallocate(script);
} }
void lua_scripts::reschedule_script(script_info *script) { void lua_scripts::reschedule_script(script_info *script) {
@ -410,11 +411,11 @@ void lua_scripts::reschedule_script(script_info *script) {
previous->next = script; previous->next = script;
} }
void *lua_scripts::_heap; MultiHeap lua_scripts::_heap;
void *lua_scripts::alloc(void *ud, void *ptr, size_t osize, size_t nsize) { void *lua_scripts::alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize; /* not used */ (void)ud; /* not used */
return hal.util->heap_realloc(_heap, ptr, nsize); return _heap.change_size(ptr, osize, nsize);
} }
void lua_scripts::repl_cleanup (void) { void lua_scripts::repl_cleanup (void) {
@ -433,7 +434,7 @@ void lua_scripts::repl_cleanup (void) {
void lua_scripts::run(void) { void lua_scripts::run(void) {
bool succeeded_initial_load = false; bool succeeded_initial_load = false;
if (_heap == nullptr) { if (!_heap.available()) {
gcs().send_text(MAV_SEVERITY_INFO, "Lua: Unable to allocate a heap"); gcs().send_text(MAV_SEVERITY_INFO, "Lua: Unable to allocate a heap");
return; return;
} }
@ -582,7 +583,7 @@ void lua_scripts::run(void) {
error_msg_buf_sem.take_blocking(); error_msg_buf_sem.take_blocking();
if (error_msg_buf != nullptr) { if (error_msg_buf != nullptr) {
hal.util->heap_realloc(_heap, error_msg_buf, 0); _heap.deallocate(error_msg_buf);
error_msg_buf = nullptr; error_msg_buf = nullptr;
} }
error_msg_buf_sem.give(); error_msg_buf_sem.give();

View File

@ -22,6 +22,7 @@
#include <AP_Scripting/AP_Scripting.h> #include <AP_Scripting/AP_Scripting.h>
#include <GCS_MAVLink/GCS_MAVLink.h> #include <GCS_MAVLink/GCS_MAVLink.h>
#include <AP_HAL/Semaphores.h> #include <AP_HAL/Semaphores.h>
#include <AP_Common/MultiHeap.h>
#include "lua/src/lua.hpp" #include "lua/src/lua.hpp"
@ -59,7 +60,7 @@ public:
CLASS_NO_COPY(lua_scripts); CLASS_NO_COPY(lua_scripts);
// return true if initialisation failed // return true if initialisation failed
bool heap_allocated() const { return _heap != nullptr; } bool heap_allocated() const { return _heap.available(); }
// run scripts, does not return unless an error occured // run scripts, does not return unless an error occured
void run(void); void run(void);
@ -131,7 +132,7 @@ private:
static void *alloc(void *ud, void *ptr, size_t osize, size_t nsize); static void *alloc(void *ud, void *ptr, size_t osize, size_t nsize);
static void *_heap; static MultiHeap _heap;
// helper for print and log of runtime stats // helper for print and log of runtime stats
void update_stats(const char *name, uint32_t run_time, int total_mem, int run_mem); void update_stats(const char *name, uint32_t run_time, int total_mem, int run_mem);