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)
: _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();