diff --git a/libraries/AP_Scripting/lua_scripts.cpp b/libraries/AP_Scripting/lua_scripts.cpp index 85a4e54af2..9f0ca23b64 100644 --- a/libraries/AP_Scripting/lua_scripts.cpp +++ b/libraries/AP_Scripting/lua_scripts.cpp @@ -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) : _vm_steps(vm_steps), _debug_options(debug_options), - terminal(_terminal) { - _heap = hal.util->allocate_heap_memory(heap_size); + terminal(_terminal) +{ + _heap.create(heap_size, 4); } lua_scripts::~lua_scripts() { - free(_heap); + _heap.destroy(); } 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 print_error_count = 0; if (error_msg_buf) { - hal.util->heap_realloc(_heap, error_msg_buf, 0); + _heap.deallocate(error_msg_buf); 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 - error_msg_buf = (char *)hal.util->heap_realloc(_heap, nullptr, len+1); + error_msg_buf = (char *)_heap.allocate(len+1); if (!error_msg_buf) { // allocation failed 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 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) { // 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); @@ -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 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) { 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 script_info * script = load_script(L, filename); if (script == nullptr) { - hal.util->heap_realloc(_heap, filename, 0); + _heap.deallocate(filename); continue; } 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 luaL_unref(L, LUA_REGISTRYINDEX, script->lua_ref); } - hal.util->heap_realloc(_heap, script->name, 0); - hal.util->heap_realloc(_heap, script, 0); + _heap.deallocate(script->name); + _heap.deallocate(script); } void lua_scripts::reschedule_script(script_info *script) { @@ -410,11 +411,11 @@ void lua_scripts::reschedule_script(script_info *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)ud; (void)osize; /* not used */ - return hal.util->heap_realloc(_heap, ptr, nsize); + (void)ud; /* not used */ + return _heap.change_size(ptr, osize, nsize); } void lua_scripts::repl_cleanup (void) { @@ -433,7 +434,7 @@ void lua_scripts::repl_cleanup (void) { void lua_scripts::run(void) { bool succeeded_initial_load = false; - if (_heap == nullptr) { + if (!_heap.available()) { gcs().send_text(MAV_SEVERITY_INFO, "Lua: Unable to allocate a heap"); return; } @@ -582,7 +583,7 @@ void lua_scripts::run(void) { error_msg_buf_sem.take_blocking(); 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_sem.give(); diff --git a/libraries/AP_Scripting/lua_scripts.h b/libraries/AP_Scripting/lua_scripts.h index 5f4f466114..d996b1f36c 100644 --- a/libraries/AP_Scripting/lua_scripts.h +++ b/libraries/AP_Scripting/lua_scripts.h @@ -22,6 +22,7 @@ #include #include #include +#include #include "lua/src/lua.hpp" @@ -59,7 +60,7 @@ public: CLASS_NO_COPY(lua_scripts); // 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 void run(void); @@ -131,7 +132,7 @@ private: 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 void update_stats(const char *name, uint32_t run_time, int total_mem, int run_mem);