diff --git a/python/port/port.cpp b/python/port/port.cpp index af3ea103c..6f01f26ca 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -140,11 +141,30 @@ void gc_collect(void) { size_t stackLength; void ** scanStart; if ((uintptr_t)python_stack_top > (uintptr_t)®s) { - stackLength = ((uintptr_t)python_stack_top - (uintptr_t)®s) / sizeof(uintptr_t); + + /* To compute the stack length: + * regs + * <-----------> + * STACK -> ...| | | | | |--|--|--|--| | | | | | | + * ^®s ^python_stack_top + * */ + + stackLength = ceil((float)((uintptr_t)python_stack_top - (uintptr_t)®s) / (float)sizeof(uintptr_t)); scanStart = regs_ptr; + } else { - stackLength = ((uintptr_t)(®s) - (uintptr_t)python_stack_top) / sizeof(uintptr_t); + + /* When computing the stack length, take into account regs' size. + * regs + * <-----------> + * STACK -> | | | | | | | | | | | |--|--|--|--| | | |... + * ^python_stack_top ^®s + * */ + + size_t sizeOfRegs = ceil(((float)sizeof(regs))/(float)sizeof(uintptr_t)); + stackLength = (size_t)ceil(((float)((uintptr_t)(®s) - (uintptr_t)python_stack_top)) / (float)sizeof(uintptr_t)) + sizeOfRegs; scanStart = (void **)python_stack_top; + } gc_collect_root(scanStart, stackLength);