mirror of
https://github.com/ArduPilot/ardupilot.git
synced 2026-02-08 20:09:13 +08:00
AP_Scripting: Use dedicated heap
This commit is contained in:
committed by
WickedShell
parent
7b259fb09f
commit
c191789651
@@ -39,9 +39,9 @@ extern const AP_HAL::HAL& hal;
|
||||
bool lua_scripts::overtime;
|
||||
jmp_buf lua_scripts::panic_jmp;
|
||||
|
||||
lua_scripts::lua_scripts(const AP_Int32 &vm_steps)
|
||||
lua_scripts::lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size)
|
||||
: _vm_steps(vm_steps) {
|
||||
scripts = nullptr;
|
||||
_heap = hal.util->allocate_heap_memory(heap_size);
|
||||
}
|
||||
|
||||
void lua_scripts::hook(lua_State *L, lua_Debug *ar) {
|
||||
@@ -80,7 +80,7 @@ lua_scripts::script_info *lua_scripts::load_script(lua_State *L, char *filename)
|
||||
}
|
||||
}
|
||||
|
||||
script_info *new_script = (script_info *)malloc(sizeof(script_info));
|
||||
script_info *new_script = (script_info *)hal.util->heap_realloc(_heap, nullptr, sizeof(script_info));
|
||||
if (new_script == nullptr) {
|
||||
// No memory, shouldn't happen, we even attempted to do a GC
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "Lua: Insufficent memory loading %s", filename);
|
||||
@@ -132,7 +132,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 *)malloc(size);
|
||||
char * filename = (char *) hal.util->heap_realloc(_heap, nullptr, size);
|
||||
if (filename == nullptr) {
|
||||
continue;
|
||||
}
|
||||
@@ -141,7 +141,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) {
|
||||
free(filename);
|
||||
hal.util->heap_realloc(_heap, filename, 0);
|
||||
continue;
|
||||
}
|
||||
reschedule_script(script);
|
||||
@@ -254,8 +254,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);
|
||||
}
|
||||
free(script->name);
|
||||
free(script);
|
||||
hal.util->heap_realloc(_heap, script->name, 0);
|
||||
hal.util->heap_realloc(_heap, script, 0);
|
||||
}
|
||||
|
||||
void lua_scripts::reschedule_script(script_info *script) {
|
||||
@@ -293,7 +293,19 @@ void lua_scripts::reschedule_script(script_info *script) {
|
||||
previous->next = script;
|
||||
}
|
||||
|
||||
void *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 lua_scripts::run(void) {
|
||||
if (_heap == nullptr) {
|
||||
gcs().send_text(MAV_SEVERITY_INFO, "Lua: Unable to allocate a heap");
|
||||
return;
|
||||
}
|
||||
|
||||
// panic should be hooked first
|
||||
if (setjmp(panic_jmp)) {
|
||||
if (lua_state != nullptr) {
|
||||
@@ -307,7 +319,7 @@ void lua_scripts::run(void) {
|
||||
overtime = false;
|
||||
}
|
||||
|
||||
lua_state = luaL_newstate();
|
||||
lua_state = lua_newstate(alloc, NULL);
|
||||
lua_State *L = lua_state;
|
||||
if (L == nullptr) {
|
||||
gcs().send_text(MAV_SEVERITY_CRITICAL, "Lua: Couldn't allocate a lua state");
|
||||
@@ -361,14 +373,14 @@ void lua_scripts::run(void) {
|
||||
|
||||
gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: Running %s", scripts->name);
|
||||
|
||||
const uint32_t startMem = hal.util->available_memory();
|
||||
const uint32_t startMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
|
||||
const uint32_t loadEnd = AP_HAL::micros();
|
||||
|
||||
run_next_script(L);
|
||||
|
||||
const uint32_t runEnd = AP_HAL::micros();
|
||||
const uint32_t endMem = hal.util->available_memory();
|
||||
gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: Time: %d Mem: %d", runEnd - loadEnd, startMem - endMem);
|
||||
const uint32_t endMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
|
||||
gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: Time: %d Mem: %d", runEnd - loadEnd, endMem - startMem);
|
||||
|
||||
} else {
|
||||
gcs().send_text(MAV_SEVERITY_DEBUG, "Lua: No scripts to run");
|
||||
|
||||
Reference in New Issue
Block a user