Skip to content

Commit 64cfd86

Browse files
committed
Use GetCurrentThreadStackLimits instead of probing with alloca
1 parent 82173ed commit 64cfd86

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

Python/ceval.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,25 +342,17 @@ _Py_EnterRecursiveCallUnchecked(PyThreadState *tstate)
342342
void
343343
_Py_UpdateRecursionLimits(PyThreadState *tstate)
344344
{
345+
#ifdef WIN32
346+
ULONG_PTR low, high;
347+
GetCurrentThreadStackLimits(&low, &high);
348+
tstate->c_stack_top = (uintptr_t)high;
349+
ULONG guarantee = 0;
350+
SetThreadStackGuarantee(&guarantee);
351+
tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + PYOS_STACK_MARGIN_BYTES;
352+
tstate->c_stack_soft_limit = tstate->c_stack_hard_limit + PYOS_STACK_MARGIN_BYTES;
353+
#else
345354
char here;
346355
uintptr_t here_addr = (uintptr_t)&here;
347-
#ifdef USE_STACKCHECK
348-
int to_probe = PYOS_STACK_MARGIN_BYTES * 2;
349-
if (tstate->c_stack_top == 0) {
350-
assert(tstate->c_stack_soft_limit == UINTPTR_MAX);
351-
tstate->c_stack_top = _Py_SIZE_ROUND_UP(here_addr, 4096);
352-
tstate->c_stack_soft_limit = tstate->c_stack_top;
353-
to_probe = PYOS_STACK_MARGIN_BYTES * 4;
354-
}
355-
int depth;
356-
uintptr_t implicit_hard_limit = tstate->c_stack_soft_limit - PYOS_STACK_MARGIN_BYTES;
357-
_Py_StackProbe(implicit_hard_limit, to_probe, &depth);
358-
tstate->c_stack_soft_limit = implicit_hard_limit - depth + PYOS_STACK_MARGIN_BYTES * 2;
359-
if (depth != to_probe) {
360-
tstate->c_stack_hard_limit = tstate->c_stack_soft_limit - PYOS_STACK_MARGIN_BYTES;
361-
}
362-
#else
363-
assert(tstate->c_stack_top == 0);
364356
tstate->c_stack_top = _Py_SIZE_ROUND_UP(here_addr, 4096);
365357
tstate->c_stack_soft_limit = tstate->c_stack_top - Py_C_STACK_SIZE;
366358
tstate->c_stack_hard_limit = tstate->c_stack_top - (Py_C_STACK_SIZE + PYOS_STACK_MARGIN_BYTES);

0 commit comments

Comments
 (0)