@@ -86,15 +86,17 @@ _PyObject_AssignUniqueId(PyObject *obj)
86
86
if (pool -> freelist == NULL ) {
87
87
if (resize_interp_type_id_pool (pool ) < 0 ) {
88
88
UNLOCK_POOL (pool );
89
- return -1 ;
89
+ return _Py_INVALID_UNIQUE_ID ;
90
90
}
91
91
}
92
92
93
93
_Py_unique_id_entry * entry = pool -> freelist ;
94
94
pool -> freelist = entry -> next ;
95
95
entry -> obj = obj ;
96
96
_PyObject_SetDeferredRefcount (obj );
97
- Py_ssize_t unique_id = (entry - pool -> table );
97
+ // The unique id is one plus the index of the entry in the table.
98
+ Py_ssize_t unique_id = (entry - pool -> table ) + 1 ;
99
+ assert (unique_id > 0 );
98
100
UNLOCK_POOL (pool );
99
101
return unique_id ;
100
102
}
@@ -106,8 +108,9 @@ _PyObject_ReleaseUniqueId(Py_ssize_t unique_id)
106
108
struct _Py_unique_id_pool * pool = & interp -> unique_ids ;
107
109
108
110
LOCK_POOL (pool );
109
- assert (unique_id >= 0 && unique_id < pool -> size );
110
- _Py_unique_id_entry * entry = & pool -> table [unique_id ];
111
+ assert (unique_id > 0 && unique_id <= pool -> size );
112
+ Py_ssize_t idx = unique_id - 1 ;
113
+ _Py_unique_id_entry * entry = & pool -> table [idx ];
111
114
entry -> next = pool -> freelist ;
112
115
pool -> freelist = entry ;
113
116
UNLOCK_POOL (pool );
@@ -116,18 +119,18 @@ _PyObject_ReleaseUniqueId(Py_ssize_t unique_id)
116
119
static Py_ssize_t
117
120
clear_unique_id (PyObject * obj )
118
121
{
119
- Py_ssize_t id = -1 ;
122
+ Py_ssize_t id = _Py_INVALID_UNIQUE_ID ;
120
123
if (PyType_Check (obj )) {
121
124
if (PyType_HasFeature ((PyTypeObject * )obj , Py_TPFLAGS_HEAPTYPE )) {
122
125
PyHeapTypeObject * ht = (PyHeapTypeObject * )obj ;
123
126
id = ht -> unique_id ;
124
- ht -> unique_id = -1 ;
127
+ ht -> unique_id = _Py_INVALID_UNIQUE_ID ;
125
128
}
126
129
}
127
130
else if (PyCode_Check (obj )) {
128
131
PyCodeObject * co = (PyCodeObject * )obj ;
129
132
id = co -> _co_unique_id ;
130
- co -> _co_unique_id = -1 ;
133
+ co -> _co_unique_id = _Py_INVALID_UNIQUE_ID ;
131
134
}
132
135
else if (PyDict_Check (obj )) {
133
136
PyDictObject * mp = (PyDictObject * )obj ;
@@ -141,23 +144,23 @@ void
141
144
_PyObject_DisablePerThreadRefcounting (PyObject * obj )
142
145
{
143
146
Py_ssize_t id = clear_unique_id (obj );
144
- if (id >= 0 ) {
147
+ if (id != _Py_INVALID_UNIQUE_ID ) {
145
148
_PyObject_ReleaseUniqueId (id );
146
149
}
147
150
}
148
151
149
152
void
150
- _PyObject_ThreadIncrefSlow (PyObject * obj , Py_ssize_t unique_id )
153
+ _PyObject_ThreadIncrefSlow (PyObject * obj , size_t idx )
151
154
{
152
155
_PyThreadStateImpl * tstate = (_PyThreadStateImpl * )_PyThreadState_GET ();
153
- if (unique_id < 0 || resize_local_refcounts (tstate ) < 0 ) {
156
+ if ((( Py_ssize_t ) idx ) < 0 || resize_local_refcounts (tstate ) < 0 ) {
154
157
// just incref the object directly.
155
158
Py_INCREF (obj );
156
159
return ;
157
160
}
158
161
159
- assert (unique_id < tstate -> refcounts .size );
160
- tstate -> refcounts .values [unique_id ]++ ;
162
+ assert (idx < ( size_t ) tstate -> refcounts .size );
163
+ tstate -> refcounts .values [idx ]++ ;
161
164
#ifdef Py_REF_DEBUG
162
165
_Py_IncRefTotal ((PyThreadState * )tstate );
163
166
#endif
@@ -217,7 +220,7 @@ _PyObject_FinalizeUniqueIdPool(PyInterpreterState *interp)
217
220
if (obj != NULL ) {
218
221
Py_ssize_t id = clear_unique_id (obj );
219
222
(void )id ;
220
- assert (id == i );
223
+ assert (id == i + 1 );
221
224
}
222
225
}
223
226
PyMem_Free (pool -> table );
0 commit comments