| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 12:51:29 -04:00
										 |  |  | #include "pycore_dict.h"        // _PyDict_UniqueId()
 | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | #include "pycore_lock.h"        // PyMutex_LockFlags()
 | 
					
						
							|  |  |  | #include "pycore_pystate.h"     // _PyThreadState_GET()
 | 
					
						
							|  |  |  | #include "pycore_object.h"      // _Py_IncRefTotal
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | #include "pycore_uniqueid.h"
 | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | // This contains code for allocating unique ids for per-thread reference
 | 
					
						
							|  |  |  | // counting and re-using those ids when an object is deallocated.
 | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | // Currently, per-thread reference counting is only used for heap types.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // See Include/internal/pycore_uniqueid.h for more details.
 | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define POOL_MIN_SIZE 8
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define LOCK_POOL(pool) PyMutex_LockFlags(&pool->mutex, _Py_LOCK_DONT_DETACH)
 | 
					
						
							|  |  |  | #define UNLOCK_POOL(pool) PyMutex_Unlock(&pool->mutex)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | resize_interp_type_id_pool(struct _Py_unique_id_pool *pool) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     if ((size_t)pool->size > PY_SSIZE_T_MAX / (2 * sizeof(*pool->table))) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t new_size = pool->size * 2; | 
					
						
							|  |  |  |     if (new_size < POOL_MIN_SIZE) { | 
					
						
							|  |  |  |         new_size = POOL_MIN_SIZE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     _Py_unique_id_entry *table = PyMem_Realloc(pool->table, | 
					
						
							|  |  |  |                                                new_size * sizeof(*pool->table)); | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     if (table == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t start = pool->size; | 
					
						
							|  |  |  |     for (Py_ssize_t i = start; i < new_size - 1; i++) { | 
					
						
							|  |  |  |         table[i].next = &table[i + 1]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     table[new_size - 1].next = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pool->table = table; | 
					
						
							|  |  |  |     pool->freelist = &table[start]; | 
					
						
							|  |  |  |     _Py_atomic_store_ssize(&pool->size, new_size); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | resize_local_refcounts(_PyThreadStateImpl *tstate) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     if (tstate->refcounts.is_finalized) { | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     struct _Py_unique_id_pool *pool = &tstate->base.interp->unique_ids; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     Py_ssize_t size = _Py_atomic_load_ssize(&pool->size); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     Py_ssize_t *refcnts = PyMem_Realloc(tstate->refcounts.values, | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |                                         size * sizeof(Py_ssize_t)); | 
					
						
							|  |  |  |     if (refcnts == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     Py_ssize_t old_size = tstate->refcounts.size; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     if (old_size < size) { | 
					
						
							|  |  |  |        memset(refcnts + old_size, 0, (size - old_size) * sizeof(Py_ssize_t)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     tstate->refcounts.values = refcnts; | 
					
						
							|  |  |  |     tstate->refcounts.size = size; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | Py_ssize_t | 
					
						
							|  |  |  | _PyObject_AssignUniqueId(PyObject *obj) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     struct _Py_unique_id_pool *pool = &interp->unique_ids; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     LOCK_POOL(pool); | 
					
						
							|  |  |  |     if (pool->freelist == NULL) { | 
					
						
							|  |  |  |         if (resize_interp_type_id_pool(pool) < 0) { | 
					
						
							|  |  |  |             UNLOCK_POOL(pool); | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |             return _Py_INVALID_UNIQUE_ID; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     _Py_unique_id_entry *entry = pool->freelist; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     pool->freelist = entry->next; | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     entry->obj = obj; | 
					
						
							|  |  |  |     _PyObject_SetDeferredRefcount(obj); | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |     // The unique id is one plus the index of the entry in the table.
 | 
					
						
							|  |  |  |     Py_ssize_t unique_id = (entry - pool->table) + 1; | 
					
						
							|  |  |  |     assert(unique_id > 0); | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     UNLOCK_POOL(pool); | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     return unique_id; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 12:51:29 -04:00
										 |  |  | void | 
					
						
							|  |  |  | _PyObject_ReleaseUniqueId(Py_ssize_t unique_id) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     struct _Py_unique_id_pool *pool = &interp->unique_ids; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     LOCK_POOL(pool); | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |     assert(unique_id > 0 && unique_id <= pool->size); | 
					
						
							|  |  |  |     Py_ssize_t idx = unique_id - 1; | 
					
						
							|  |  |  |     _Py_unique_id_entry *entry = &pool->table[idx]; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     entry->next = pool->freelist; | 
					
						
							|  |  |  |     pool->freelist = entry; | 
					
						
							|  |  |  |     UNLOCK_POOL(pool); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  | static Py_ssize_t | 
					
						
							|  |  |  | clear_unique_id(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |     Py_ssize_t id = _Py_INVALID_UNIQUE_ID; | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |     if (PyType_Check(obj)) { | 
					
						
							|  |  |  |         if (PyType_HasFeature((PyTypeObject *)obj, Py_TPFLAGS_HEAPTYPE)) { | 
					
						
							|  |  |  |             PyHeapTypeObject *ht = (PyHeapTypeObject *)obj; | 
					
						
							|  |  |  |             id = ht->unique_id; | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |             ht->unique_id = _Py_INVALID_UNIQUE_ID; | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (PyCode_Check(obj)) { | 
					
						
							|  |  |  |         PyCodeObject *co = (PyCodeObject *)obj; | 
					
						
							|  |  |  |         id = co->_co_unique_id; | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |         co->_co_unique_id = _Py_INVALID_UNIQUE_ID; | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-21 12:51:29 -04:00
										 |  |  |     else if (PyDict_Check(obj)) { | 
					
						
							|  |  |  |         PyDictObject *mp = (PyDictObject *)obj; | 
					
						
							|  |  |  |         id = _PyDict_UniqueId(mp); | 
					
						
							|  |  |  |         mp->_ma_watcher_tag &= ~(UINT64_MAX << DICT_UNIQUE_ID_SHIFT); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |     return id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _PyObject_DisablePerThreadRefcounting(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t id = clear_unique_id(obj); | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |     if (id != _Py_INVALID_UNIQUE_ID) { | 
					
						
							| 
									
										
										
										
											2024-10-21 12:51:29 -04:00
										 |  |  |         _PyObject_ReleaseUniqueId(id); | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  | _PyObject_ThreadIncrefSlow(PyObject *obj, size_t idx) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |     if (((Py_ssize_t)idx) < 0 || resize_local_refcounts(tstate) < 0) { | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |         // just incref the object directly.
 | 
					
						
							|  |  |  |         Py_INCREF(obj); | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |     assert(idx < (size_t)tstate->refcounts.size); | 
					
						
							|  |  |  |     tstate->refcounts.values[idx]++; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							|  |  |  |     _Py_IncRefTotal((PyThreadState *)tstate); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     _Py_INCREF_STAT_INC(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | _PyObject_MergePerThreadRefcounts(_PyThreadStateImpl *tstate) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     if (tstate->refcounts.values == NULL) { | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     struct _Py_unique_id_pool *pool = &tstate->base.interp->unique_ids; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     LOCK_POOL(pool); | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     for (Py_ssize_t i = 0, n = tstate->refcounts.size; i < n; i++) { | 
					
						
							|  |  |  |         Py_ssize_t refcnt = tstate->refcounts.values[i]; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         if (refcnt != 0) { | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |             PyObject *obj = pool->table[i].obj; | 
					
						
							|  |  |  |             _Py_atomic_add_ssize(&obj->ob_ref_shared, | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |                                  refcnt << _Py_REF_SHARED_SHIFT); | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |             tstate->refcounts.values[i] = 0; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     UNLOCK_POOL(pool); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | _PyObject_FinalizePerThreadRefcounts(_PyThreadStateImpl *tstate) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     _PyObject_MergePerThreadRefcounts(tstate); | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     PyMem_Free(tstate->refcounts.values); | 
					
						
							|  |  |  |     tstate->refcounts.values = NULL; | 
					
						
							|  |  |  |     tstate->refcounts.size = 0; | 
					
						
							|  |  |  |     tstate->refcounts.is_finalized = 1; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  | _PyObject_FinalizeUniqueIdPool(PyInterpreterState *interp) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |     struct _Py_unique_id_pool *pool = &interp->unique_ids; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // First, set the free-list to NULL values
 | 
					
						
							|  |  |  |     while (pool->freelist) { | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |         _Py_unique_id_entry *next = pool->freelist->next; | 
					
						
							|  |  |  |         pool->freelist->obj = NULL; | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         pool->freelist = next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |     // Now everything non-NULL is a object. Clear their unique ids as the
 | 
					
						
							|  |  |  |     // object outlives the interpreter.
 | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     for (Py_ssize_t i = 0; i < pool->size; i++) { | 
					
						
							| 
									
										
										
										
											2024-10-01 13:05:42 -04:00
										 |  |  |         PyObject *obj = pool->table[i].obj; | 
					
						
							|  |  |  |         pool->table[i].obj = NULL; | 
					
						
							| 
									
										
										
										
											2024-10-15 15:06:41 -04:00
										 |  |  |         if (obj != NULL) { | 
					
						
							|  |  |  |             Py_ssize_t id = clear_unique_id(obj); | 
					
						
							|  |  |  |             (void)id; | 
					
						
							| 
									
										
										
										
											2025-01-17 10:42:27 -05:00
										 |  |  |             assert(id == i + 1); | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyMem_Free(pool->table); | 
					
						
							|  |  |  |     pool->table = NULL; | 
					
						
							|  |  |  |     pool->freelist = NULL; | 
					
						
							|  |  |  |     pool->size = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif   /* Py_GIL_DISABLED */
 |