File tree Expand file tree Collapse file tree 2 files changed +9
-12
lines changed Expand file tree Collapse file tree 2 files changed +9
-12
lines changed Original file line number Diff line number Diff line change @@ -769,12 +769,6 @@ struct _is {
769769 * and should be placed at the beginning. */
770770 struct _ceval_state ceval ;
771771
772- /* This structure is carefully allocated so that it's correctly aligned
773- * to avoid undefined behaviors during LOAD and STORE. The '_malloced'
774- * field stores the allocated pointer address that will later be freed.
775- */
776- void * _malloced ;
777-
778772 PyInterpreterState * next ;
779773
780774 int64_t id ;
Original file line number Diff line number Diff line change @@ -457,16 +457,19 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
457457static PyInterpreterState *
458458alloc_interpreter (void )
459459{
460+ // Aligned allocation for PyInterpreterState.
461+ // the first word of the memory block is used to store
462+ // the original pointer to be used later to free the memory.
460463 size_t alignment = _Alignof(PyInterpreterState );
461- size_t allocsize = sizeof (PyInterpreterState ) + alignment - 1 ;
464+ size_t allocsize = sizeof (PyInterpreterState ) + sizeof ( void * ) + alignment - 1 ;
462465 void * mem = PyMem_RawCalloc (1 , allocsize );
463466 if (mem == NULL ) {
464467 return NULL ;
465468 }
466- PyInterpreterState * interp = _Py_ALIGN_UP (mem , alignment );
467- assert ( _Py_IS_ALIGNED ( interp , alignment )) ;
468- interp -> _malloced = mem ;
469- return interp ;
469+ void * ptr = _Py_ALIGN_UP (( char * ) mem + sizeof ( void * ) , alignment );
470+ (( void * * ) ptr )[ -1 ] = mem ;
471+ assert ( _Py_IS_ALIGNED ( ptr , alignment )) ;
472+ return ptr ;
470473}
471474
472475static void
@@ -481,7 +484,7 @@ free_interpreter(PyInterpreterState *interp)
481484 interp -> obmalloc = NULL ;
482485 }
483486 assert (_Py_IS_ALIGNED (interp , _Alignof(PyInterpreterState )));
484- PyMem_RawFree (interp -> _malloced );
487+ PyMem_RawFree ((( void * * ) interp )[ -1 ] );
485488 }
486489}
487490
You can’t perform that action at this time.
0 commit comments