@@ -312,11 +312,16 @@ _Py_COMP_DIAG_POP
312312/* Remote profilers access interpreter and thread states without synchronizing
313313 with their lifetime. Until the runtime is finalized, keep deleted states in
314314 per-type free lists, so that a stale access only reaches a state of the same
315- type. Each state is preceded by the free-list link and the block to free. */
315+ type. */
316+ typedef struct state_header {
317+ struct state_header * next ; // Next entry in the free list
318+ void * mem ; // Block to free at finalization
319+ } state_header ;
320+
316321static struct {
317322 PyMutex mutex ;
318- void * tstates ;
319- void * interpreters ;
323+ state_header * tstates ;
324+ state_header * interpreters ;
320325} state_freelists ;
321326
322327#define LOCKS_INIT (runtime ) \
@@ -402,15 +407,15 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
402407#endif
403408 gilstate_clear ();
404409
405- void * * lists [] = {& state_freelists .tstates , & state_freelists .interpreters };
410+ state_header * * lists [] = {& state_freelists .tstates , & state_freelists .interpreters };
406411 for (size_t i = 0 ; i < Py_ARRAY_LENGTH (lists ); i ++ ) {
407412 PyMutex_LockFlags (& state_freelists .mutex , _Py_LOCK_DONT_DETACH );
408- void * * entry = * lists [i ];
413+ state_header * entry = * lists [i ];
409414 * lists [i ] = NULL ;
410415 PyMutex_Unlock (& state_freelists .mutex );
411416 while (entry != NULL ) {
412- void * * next = entry [ 0 ] ;
413- PyMem_RawFree (entry [ 1 ] );
417+ state_header * next = entry -> next ;
418+ PyMem_RawFree (entry -> mem );
414419 entry = next ;
415420 }
416421 }
@@ -470,37 +475,37 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
470475}
471476
472477static void *
473- alloc_state (void * * freelist , size_t size , size_t alignment )
478+ alloc_state (state_header * * freelist , size_t size , size_t alignment )
474479{
475480 PyMutex_LockFlags (& state_freelists .mutex , _Py_LOCK_DONT_DETACH );
476- void * * entry = * freelist ;
481+ state_header * entry = * freelist ;
477482 if (entry != NULL ) {
478- * freelist = * entry ;
483+ * freelist = entry -> next ;
479484 }
480485 PyMutex_Unlock (& state_freelists .mutex );
481486 if (entry != NULL ) {
482- return entry + 2 ;
487+ return entry + 1 ;
483488 }
484489
485- size_t allocsize = size + 2 * sizeof (void * ) + alignment - 1 ;
490+ size_t allocsize = size + sizeof (state_header ) + alignment - 1 ;
486491 void * mem = PyMem_RawCalloc (1 , allocsize );
487492 if (mem == NULL ) {
488493 return NULL ;
489494 }
490- void * ptr = _Py_ALIGN_UP ((char * )mem + 2 * sizeof (void * ), alignment );
491- ((void * * )ptr )[ -1 ] = mem ;
495+ void * ptr = _Py_ALIGN_UP ((char * )mem + sizeof (state_header ), alignment );
496+ ((state_header * )ptr - 1 ) -> mem = mem ;
492497 assert (_Py_IS_ALIGNED (ptr , alignment ));
493498 return ptr ;
494499}
495500
496501static void
497- recycle_state (void * * freelist , void * state , size_t size )
502+ recycle_state (state_header * * freelist , void * state , size_t size )
498503{
499504 memset (state , 0 , size );
500- void * * entry = (void * * )state - 2 ;
505+ state_header * entry = (state_header * )state - 1 ;
501506
502507 PyMutex_LockFlags (& state_freelists .mutex , _Py_LOCK_DONT_DETACH );
503- entry [ 0 ] = * freelist ;
508+ entry -> next = * freelist ;
504509 // Keep the list consistent for a child forked by another thread.
505510 _Py_atomic_store_ptr_release (freelist , entry );
506511 PyMutex_Unlock (& state_freelists .mutex );
0 commit comments