[python] Fixed the Python stack length computation.

On the device, the stack is stored in reverse order, but it might not be
the case on other platforms, which changes stack length computation.

Change-Id: I0218224a77465b9672f137771c00f734fdfaea64
This commit is contained in:
Léa Saviot
2017-11-21 17:47:23 +01:00
parent 52bdf4a41f
commit 6ccd0c1c55

View File

@@ -133,7 +133,17 @@ void gc_collect(void) {
setjmp(regs);
void **regs_ptr = (void**)(void*)&regs;
gc_collect_root(regs_ptr, ((uintptr_t)python_stack_top - (uintptr_t)&regs) / sizeof(uintptr_t));
/* On the device, the stack is stored in reverse order, but it might not be
* the case on a computer. We thus have to take the absolute value of the
* addresses difference. */
size_t stackLength;
if ((uintptr_t)python_stack_top > (uintptr_t)(&regs)) {
stackLength = (((uintptr_t)python_stack_top - (uintptr_t)(&regs)) / sizeof(uintptr_t));
} else {
stackLength = (((uintptr_t)(&regs) - (uintptr_t)python_stack_top) / sizeof(uintptr_t));
}
gc_collect_root(regs_ptr, stackLength);
gc_collect_end();
}