@@ -85,81 +85,67 @@ typedef union _PyStackRef {
85
85
# define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) })
86
86
#endif
87
87
88
+ // Note: the following are all macros because MSVC (Windows) has trouble inlining them.
88
89
89
- static inline int
90
- PyStackRef_Is (_PyStackRef a , _PyStackRef b ) {
91
- return a .bits == b .bits ;
92
- }
90
+ #define PyStackRef_Is (a , b ) ((a).bits == (b).bits)
91
+
92
+ #define PyStackRef_IsDeferred (ref ) (((ref).bits & Py_TAG_BITS) == Py_TAG_DEFERRED)
93
93
94
- static inline int
95
- PyStackRef_IsDeferred (_PyStackRef ref )
96
- {
97
- return ((ref .bits & Py_TAG_BITS ) == Py_TAG_DEFERRED );
98
- }
99
94
95
+ #ifdef Py_GIL_DISABLED
100
96
// Gets a PyObject * from a _PyStackRef
101
97
static inline PyObject *
102
98
PyStackRef_AsPyObjectBorrow (_PyStackRef stackref )
103
99
{
104
- #ifdef Py_GIL_DISABLED
105
100
PyObject * cleared = ((PyObject * )((stackref ).bits & (~Py_TAG_BITS )));
106
101
return cleared ;
102
+ }
107
103
#else
108
- return ((PyObject * )(stackref ).bits );
104
+ # define PyStackRef_AsPyObjectBorrow ( stackref ) ((PyObject *)(stackref).bits)
109
105
#endif
110
- }
111
106
112
107
// Converts a PyStackRef back to a PyObject *, stealing the
113
108
// PyStackRef.
109
+ #ifdef Py_GIL_DISABLED
114
110
static inline PyObject *
115
111
PyStackRef_AsPyObjectSteal (_PyStackRef stackref )
116
112
{
117
- #ifdef Py_GIL_DISABLED
118
113
if (!PyStackRef_IsNull (stackref ) && PyStackRef_IsDeferred (stackref )) {
119
114
return Py_NewRef (PyStackRef_AsPyObjectBorrow (stackref ));
120
115
}
121
116
return PyStackRef_AsPyObjectBorrow (stackref );
117
+ }
122
118
#else
123
- return PyStackRef_AsPyObjectBorrow (stackref );
119
+ # define PyStackRef_AsPyObjectSteal ( stackref ) PyStackRef_AsPyObjectBorrow(stackref)
124
120
#endif
125
- }
126
121
127
122
// Converts a PyStackRef back to a PyObject *, converting the
128
123
// stackref to a new reference.
129
- static inline PyObject *
130
- PyStackRef_AsPyObjectNew (_PyStackRef stackref )
131
- {
132
- return Py_NewRef (PyStackRef_AsPyObjectBorrow (stackref ));
133
- }
124
+ #define PyStackRef_AsPyObjectNew (stackref ) Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref))
134
125
135
- static inline PyTypeObject *
136
- PyStackRef_TYPE (_PyStackRef stackref )
137
- {
138
- return Py_TYPE (PyStackRef_AsPyObjectBorrow (stackref ));
139
- }
126
+ #define PyStackRef_TYPE (stackref ) Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref))
140
127
141
128
// Converts a PyObject * to a PyStackRef, stealing the reference
129
+ #ifdef Py_GIL_DISABLED
142
130
static inline _PyStackRef
143
131
_PyStackRef_FromPyObjectSteal (PyObject * obj )
144
132
{
145
- #ifdef Py_GIL_DISABLED
146
133
// Make sure we don't take an already tagged value.
147
134
assert (((uintptr_t )obj & Py_TAG_BITS ) == 0 );
148
135
int tag = (obj == NULL || _Py_IsImmortal (obj )) ? (Py_TAG_DEFERRED ) : Py_TAG_PTR ;
149
136
return ((_PyStackRef ){.bits = ((uintptr_t )(obj )) | tag });
137
+ }
138
+ # define PyStackRef_FromPyObjectSteal (obj ) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
150
139
#else
151
- return ((_PyStackRef ){.bits = ((uintptr_t )(obj ))});
140
+ # define PyStackRef_FromPyObjectSteal ( obj ) ((_PyStackRef){.bits = ((uintptr_t)(obj))})
152
141
#endif
153
- }
154
-
155
- #define PyStackRef_FromPyObjectSteal (obj ) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
156
142
157
143
158
144
// Converts a PyObject * to a PyStackRef, with a new reference
145
+ #ifdef Py_GIL_DISABLED
159
146
static inline _PyStackRef
160
147
PyStackRef_FromPyObjectNew (PyObject * obj )
161
148
{
162
- #ifdef Py_GIL_DISABLED
163
149
// Make sure we don't take an already tagged value.
164
150
assert (((uintptr_t )obj & Py_TAG_BITS ) == 0 );
165
151
assert (obj != NULL );
@@ -170,30 +156,27 @@ PyStackRef_FromPyObjectNew(PyObject *obj)
170
156
else {
171
157
return (_PyStackRef ){ .bits = (uintptr_t )(Py_NewRef (obj )) | Py_TAG_PTR };
172
158
}
159
+ }
160
+ # define PyStackRef_FromPyObjectNew (obj ) PyStackRef_FromPyObjectNew(_PyObject_CAST(obj))
173
161
#else
174
- return ((_PyStackRef ){ .bits = (uintptr_t )(Py_NewRef (obj )) });
162
+ # define PyStackRef_FromPyObjectNew ( obj ) ((_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) })
175
163
#endif
176
- }
177
-
178
- #define PyStackRef_FromPyObjectNew (obj ) PyStackRef_FromPyObjectNew(_PyObject_CAST(obj))
179
164
165
+ #ifdef Py_GIL_DISABLED
180
166
// Same as PyStackRef_FromPyObjectNew but only for immortal objects.
181
167
static inline _PyStackRef
182
168
PyStackRef_FromPyObjectImmortal (PyObject * obj )
183
169
{
184
- #ifdef Py_GIL_DISABLED
185
170
// Make sure we don't take an already tagged value.
186
171
assert (((uintptr_t )obj & Py_TAG_BITS ) == 0 );
187
172
assert (obj != NULL );
188
173
assert (_Py_IsImmortal (obj ));
189
174
return (_PyStackRef ){ .bits = (uintptr_t )obj | Py_TAG_DEFERRED };
175
+ }
176
+ # define PyStackRef_FromPyObjectImmortal (obj ) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))
190
177
#else
191
- assert (_Py_IsImmortal (obj ));
192
- return ((_PyStackRef ){ .bits = (uintptr_t )(obj ) });
178
+ # define PyStackRef_FromPyObjectImmortal (obj ) ((_PyStackRef){ .bits = (uintptr_t)(obj) })
193
179
#endif
194
- }
195
-
196
- #define PyStackRef_FromPyObjectImmortal (obj ) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))
197
180
198
181
199
182
#define PyStackRef_CLEAR (op ) \
@@ -206,20 +189,20 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
206
189
} \
207
190
} while (0)
208
191
192
+ #ifdef Py_GIL_DISABLED
209
193
static inline void
210
194
PyStackRef_CLOSE (_PyStackRef stackref )
211
195
{
212
- #ifdef Py_GIL_DISABLED
213
196
if (PyStackRef_IsDeferred (stackref )) {
214
197
// No assert for being immortal or deferred here.
215
198
// The GC unsets deferred objects right before clearing.
216
199
return ;
217
200
}
218
201
Py_DECREF (PyStackRef_AsPyObjectBorrow (stackref ));
202
+ }
219
203
#else
220
- Py_DECREF (PyStackRef_AsPyObjectBorrow (stackref ));
204
+ # define PyStackRef_CLOSE ( stackref ) Py_DECREF(PyStackRef_AsPyObjectBorrow(stackref));
221
205
#endif
222
- }
223
206
224
207
#define PyStackRef_XCLOSE (stackref ) \
225
208
do { \
@@ -230,32 +213,21 @@ PyStackRef_CLOSE(_PyStackRef stackref)
230
213
} while (0);
231
214
232
215
216
+ #ifdef Py_GIL_DISABLED
233
217
static inline _PyStackRef
234
218
PyStackRef_DUP (_PyStackRef stackref )
235
219
{
236
- #ifdef Py_GIL_DISABLED
237
220
if (PyStackRef_IsDeferred (stackref )) {
238
221
assert (PyStackRef_IsNull (stackref ) ||
239
222
_Py_IsImmortal (PyStackRef_AsPyObjectBorrow (stackref )));
240
223
return stackref ;
241
224
}
242
225
Py_INCREF (PyStackRef_AsPyObjectBorrow (stackref ));
243
226
return stackref ;
227
+ }
244
228
#else
245
- Py_INCREF (PyStackRef_AsPyObjectBorrow (stackref ));
246
- return stackref ;
229
+ # define PyStackRef_DUP (stackref ) PyStackRef_FromPyObjectSteal(Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref)));
247
230
#endif
248
- }
249
-
250
- static inline _PyStackRef
251
- PyStackRef_XDUP (_PyStackRef stackref )
252
- {
253
- if (!PyStackRef_IsNull (stackref )) {
254
- return PyStackRef_DUP (stackref );
255
- }
256
- return stackref ;
257
- }
258
-
259
231
260
232
static inline void
261
233
_PyObjectStack_FromStackRefStack (PyObject * * dst , const _PyStackRef * src , size_t length )
0 commit comments