mirror of https://github.com/ArduPilot/ardupilot
AP_Common: added last_failed for leveraging lua GC
we want the lua garbage collector to be used to re-use memory where possible. This implements a suggestion from Thomas to avoid heap expansion unless the last allocation failed
This commit is contained in:
parent
1cd05618f0
commit
8ebfa28183
|
@ -103,16 +103,24 @@ void *MultiHeap::allocate(uint32_t size)
|
||||||
}
|
}
|
||||||
void *newptr = hal.util->heap_allocate(heaps[i].hp, size);
|
void *newptr = hal.util->heap_allocate(heaps[i].hp, size);
|
||||||
if (newptr != nullptr) {
|
if (newptr != nullptr) {
|
||||||
|
last_failed = false;
|
||||||
return newptr;
|
return newptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!allow_expansion) {
|
if (!allow_expansion || !last_failed) {
|
||||||
|
/*
|
||||||
|
we only allow expansion when the last allocation
|
||||||
|
failed. This gives the lua engine a chance to use garbage
|
||||||
|
collection to recover memory
|
||||||
|
*/
|
||||||
|
last_failed = true;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hal.util->get_soft_armed()) {
|
if (!hal.util->get_soft_armed()) {
|
||||||
// only expand the available heaps when armed. When disarmed
|
// only expand the available heaps when armed. When disarmed
|
||||||
// user should fix their SCR_HEAP_SIZE parameter
|
// user should fix their SCR_HEAP_SIZE parameter
|
||||||
|
last_failed = true;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +133,7 @@ void *MultiHeap::allocate(uint32_t size)
|
||||||
const uint32_t heap_overhead = 128; // conservative value, varies with HAL
|
const uint32_t heap_overhead = 128; // conservative value, varies with HAL
|
||||||
const uint32_t min_size = size + heap_overhead;
|
const uint32_t min_size = size + heap_overhead;
|
||||||
if (available < reserve_size+min_size) {
|
if (available < reserve_size+min_size) {
|
||||||
|
last_failed = true;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,20 +141,24 @@ void *MultiHeap::allocate(uint32_t size)
|
||||||
const uint32_t round_to = 30*1024U;
|
const uint32_t round_to = 30*1024U;
|
||||||
const uint32_t alloc_size = MIN(available - reserve_size, MAX(size+heap_overhead, round_to));
|
const uint32_t alloc_size = MIN(available - reserve_size, MAX(size+heap_overhead, round_to));
|
||||||
if (alloc_size < min_size) {
|
if (alloc_size < min_size) {
|
||||||
|
last_failed = true;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
for (uint8_t i=0; i<num_heaps; i++) {
|
for (uint8_t i=0; i<num_heaps; i++) {
|
||||||
if (heaps[i].hp == nullptr) {
|
if (heaps[i].hp == nullptr) {
|
||||||
heaps[i].hp = hal.util->heap_create(alloc_size);
|
heaps[i].hp = hal.util->heap_create(alloc_size);
|
||||||
if (heaps[i].hp == nullptr) {
|
if (heaps[i].hp == nullptr) {
|
||||||
|
last_failed = true;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
sum_size += alloc_size;
|
sum_size += alloc_size;
|
||||||
expanded_to = sum_size;
|
expanded_to = sum_size;
|
||||||
void *p = hal.util->heap_allocate(heaps[i].hp, size);
|
void *p = hal.util->heap_allocate(heaps[i].hp, size);
|
||||||
|
last_failed = p == nullptr;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
last_failed = true;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,6 +170,7 @@ void MultiHeap::deallocate(void *ptr)
|
||||||
if (!available() || ptr == nullptr) {
|
if (!available() || ptr == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
last_failed = false;
|
||||||
hal.util->heap_free(ptr);
|
hal.util->heap_free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,4 +43,9 @@ private:
|
||||||
uint32_t reserve_size;
|
uint32_t reserve_size;
|
||||||
uint32_t sum_size;
|
uint32_t sum_size;
|
||||||
uint32_t expanded_to;
|
uint32_t expanded_to;
|
||||||
|
|
||||||
|
// we only do heap expansion if the last allocation failed this
|
||||||
|
// encourages the lua scripting engine to garbage collect to
|
||||||
|
// re-use memory when possible
|
||||||
|
bool last_failed;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue