@@ -330,12 +330,12 @@ static inline bool
330330_PyThreadState_HasStackSpace (PyThreadState * tstate , int size )
331331{
332332 assert (
333- (tstate -> datastack_top == NULL && tstate -> datastack_limit == NULL )
333+ (tstate -> stack_top == NULL && tstate -> stack_limit == NULL )
334334 ||
335- (tstate -> datastack_top != NULL && tstate -> datastack_limit != NULL )
335+ (tstate -> stack_top != NULL && tstate -> stack_limit != NULL )
336336 );
337- return tstate -> datastack_top != NULL &&
338- size < tstate -> datastack_limit - tstate -> datastack_top ;
337+ return tstate -> stack_top != NULL &&
338+ size < tstate -> stack_limit - tstate -> stack_top ;
339339}
340340
341341extern _PyInterpreterFrame *
@@ -352,9 +352,9 @@ _PyFrame_PushUnchecked(PyThreadState *tstate, _PyStackRef func, int null_locals_
352352 CALL_STAT_INC (frames_pushed );
353353 PyFunctionObject * func_obj = (PyFunctionObject * )PyStackRef_AsPyObjectBorrow (func );
354354 PyCodeObject * code = (PyCodeObject * )func_obj -> func_code ;
355- _PyInterpreterFrame * new_frame = (_PyInterpreterFrame * )tstate -> datastack_top ;
356- tstate -> datastack_top += code -> co_framesize ;
357- assert (tstate -> datastack_top < tstate -> datastack_limit );
355+ _PyInterpreterFrame * new_frame = (_PyInterpreterFrame * )tstate -> stack_top ;
356+ tstate -> stack_top += code -> co_framesize ;
357+ assert (tstate -> stack_top < tstate -> stack_limit );
358358 _PyFrame_Initialize (tstate , new_frame , func , NULL , code , null_locals_from ,
359359 previous );
360360 return new_frame ;
@@ -366,9 +366,9 @@ static inline _PyInterpreterFrame *
366366_PyFrame_PushTrampolineUnchecked (PyThreadState * tstate , PyCodeObject * code , int stackdepth , _PyInterpreterFrame * previous )
367367{
368368 CALL_STAT_INC (frames_pushed );
369- _PyInterpreterFrame * frame = (_PyInterpreterFrame * )tstate -> datastack_top ;
370- tstate -> datastack_top += code -> co_framesize ;
371- assert (tstate -> datastack_top < tstate -> datastack_limit );
369+ _PyInterpreterFrame * frame = (_PyInterpreterFrame * )tstate -> stack_top ;
370+ tstate -> stack_top += code -> co_framesize ;
371+ assert (tstate -> stack_top < tstate -> stack_limit );
372372 frame -> previous = previous ;
373373 frame -> f_funcobj = PyStackRef_None ;
374374 frame -> f_executable = PyStackRef_FromPyObjectNew (code );
@@ -404,6 +404,54 @@ PyAPI_FUNC(_PyInterpreterFrame *)
404404_PyEvalFramePushAndInit_Ex (PyThreadState * tstate , _PyStackRef func ,
405405 PyObject * locals , Py_ssize_t nargs , PyObject * callargs , PyObject * kwargs , _PyInterpreterFrame * previous );
406406
407+ static inline bool
408+ ptr_in_chunk (const char * ptr , const _PyStackChunk * chunk )
409+ {
410+ assert (chunk != NULL );
411+ const char * start = (char * )& chunk -> data [0 ];
412+ const intptr_t offset = ptr - start ;
413+ const intptr_t usable_size = (intptr_t )(chunk -> size - _PY_STACK_CHUNK_OVERHEADS );
414+ return offset >= 0 && offset < usable_size && start + offset == ptr ;
415+ }
416+
417+ static inline uintptr_t
418+ get_offset_in_chunk (const char * ptr , const _PyStackChunk * chunk )
419+ {
420+ assert (chunk != NULL );
421+ assert (chunk -> data != NULL );
422+ assert (ptr_in_chunk (ptr , chunk ));
423+
424+ return ptr - (char * )chunk ;
425+ }
426+
427+ static inline uintptr_t
428+ get_offset_in_chunk_list (char * base , _PyStackChunk * stack_chunk_list )
429+ {
430+ assert (stack_chunk_list != NULL );
431+ assert (base != NULL );
432+ _PyStackChunk * chunk = stack_chunk_list ;
433+ do {
434+ if (ptr_in_chunk (base , chunk )) {
435+ return get_offset_in_chunk (base , chunk );
436+ }
437+ chunk = chunk -> previous ;
438+ } while (chunk );
439+ assert (false); // did not find correct chunk
440+ Py_UNREACHABLE ();
441+ }
442+
443+ static inline void *
444+ _Py_ensure_frame_in_current_stack_chunk (PyThreadState * tstate , char * frame )
445+ {
446+ assert (tstate != NULL );
447+ assert (frame != NULL );
448+ if (ptr_in_chunk (frame , tstate -> stack_chunk_list )) {
449+ return frame ;
450+ }
451+ uintptr_t offset = get_offset_in_chunk_list (frame , tstate -> stack_chunk_list -> previous );
452+ return ((char * )tstate -> stack_chunk_list ) + offset ;
453+ }
454+
407455#ifdef __cplusplus
408456}
409457#endif
0 commit comments