8000 Revert "gh-121263: Macro-ify most stackref functions for MSVC (GH-121… · mdboom/cpython@0853d0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0853d0e

Browse files
committed
Revert "pythongh-121263: Macro-ify most stackref functions for MSVC (pythonGH-121270)"
This reverts commit 722229e.
1 parent d66b061 commit 0853d0e

File tree

1 file changed

+58
-30
lines changed

1 file changed

+58
-30
lines changed

Include/internal/pycore_stackref.h

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,67 +85,81 @@ typedef union _PyStackRef {
8585
# define PyStackRef_None ((_PyStackRef){.bits = ((uintptr_t)&_Py_NoneStruct) })
8686
#endif
8787

88-
// Note: the following are all macros because MSVC (Windows) has trouble inlining them.
8988

90-
#define PyStackRef_Is(a, b) ((a).bits == (b).bits)
91-
92-
#define PyStackRef_IsDeferred(ref) (((ref).bits & Py_TAG_BITS) == Py_TAG_DEFERRED)
89+
static inline int
90+
PyStackRef_Is(_PyStackRef a, _PyStackRef b) {
91+
return a.bits == b.bits;
92+
}
9393

94+
static inline int
95+
PyStackRef_IsDeferred(_PyStackRef ref)
96+
{
97+
return ((ref.bits & Py_TAG_BITS) == Py_TAG_DEFERRED);
98+
}
9499

95-
#ifdef Py_GIL_DISABLED
96100
// Gets a PyObject * from a _PyStackRef
97101
static inline PyObject *
98102
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
99103
{
104+
#ifdef Py_GIL_DISABLED
100105
PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
101106
return cleared;
102-
}
103107
#else
104-
# define PyStackRef_AsPyObjectBorrow(stackref) ((PyObject *)(stackref).bits)
108+
return ((PyObject *)(stackref).bits);
105109
#endif
110+
}
106111

107112
// Converts a PyStackRef back to a PyObject *, stealing the
108113
// PyStackRef.
109-
#ifdef Py_GIL_DISABLED
110114
static inline PyObject *
111115
PyStackRef_AsPyObjectSteal(_PyStackRef stackref)
112116
{
117+
#ifdef Py_GIL_DISABLED
113118
if (!PyStackRef_IsNull(stackref) && PyStackRef_IsDeferred(stackref)) {
114119
return Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref));
115120
}
116121
return PyStackRef_AsPyObjectBorrow(stackref);
117-
}
118122
#else
119-
# define PyStackRef_AsPyObjectSteal(stackref) PyStackRef_AsPyObjectBorrow(stackref)
123+
return PyStackRef_AsPyObjectBorrow(stackref);
120124
#endif
125+
}
121126

122127
// Converts a PyStackRef back to a PyObject *, converting the
123128
// stackref to a new reference.
124-
#define PyStackRef_AsPyObjectNew(stackref) Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref))
129+
static inline PyObject *
130+
PyStackRef_AsPyObjectNew(_PyStackRef stackref)
131+
{
132+
return Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref));
133+
}
125134

126-
#define PyStackRef_TYPE(stackref) Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref))
135+
static inline PyTypeObject *
136+
PyStackRef_TYPE(_PyStackRef stackref)
137+
{
138+
return Py_TYPE(PyStackRef_AsPyObjectBorrow(stackref));
139+
}
127140

128141
// Converts a PyObject * to a PyStackRef, stealing the reference
129-
#ifdef Py_GIL_DISABLED
130142
static inline _PyStackRef
131143
_PyStackRef_FromPyObjectSteal(PyObject *obj)
132144
{
145+
#ifdef Py_GIL_DISABLED
133146
// Make sure we don't take an already tagged value.
134147
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
135148
int tag = (obj == NULL || _Py_IsImmortal(obj)) ? (Py_TAG_DEFERRED) : Py_TAG_PTR;
136149
return ((_PyStackRef){.bits = ((uintptr_t)(obj)) | tag});
137-
}
138-
# define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
139150
#else
140-
# define PyStackRef_FromPyObjectSteal(obj) ((_PyStackRef){.bits = ((uintptr_t)(obj))})
151+
return ((_PyStackRef){.bits = ((uintptr_t)(obj))});
141152
#endif
153+
}
154+
155+
#define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
142156

143157

144158
// Converts a PyObject * to a PyStackRef, with a new reference
145-
#ifdef Py_GIL_DISABLED
146159
static inline _PyStackRef
147160
PyStackRef_FromPyObjectNew(PyObject *obj)
148161
{
162+
#ifdef Py_GIL_DISABLED
149163
// Make sure we don't take an already tagged value.
150164
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
151165
assert(obj != NULL);
@@ -156,27 +170,30 @@ PyStackRef_FromPyObjectNew(PyObject *obj)
156170
else {
157171
return (_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) | Py_TAG_PTR };
158172
}
159-
}
160-
# define PyStackRef_FromPyObjectNew(obj) PyStackRef_FromPyObjectNew(_PyObject_CAST(obj))
161173
#else
162-
# define PyStackRef_FromPyObjectNew(obj) ((_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) })
174+
return ((_PyStackRef){ .bits = (uintptr_t)(Py_NewRef(obj)) });
163175
#endif
176+
}
177+
178+
#define PyStackRef_FromPyObjectNew(obj) PyStackRef_FromPyObjectNew(_PyObject_CAST(obj))
164179

165-
#ifdef Py_GIL_DISABLED
166180
// Same as PyStackRef_FromPyObjectNew but only for immortal objects.
167181
static inline _PyStackRef
168182
PyStackRef_FromPyObjectImmortal(PyObject *obj)
169183
{
184+
#ifdef Py_GIL_DISABLED
170185
// Make sure we don't take an already tagged value.
171186
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
172187
assert(obj != NULL);
173188
assert(_Py_IsImmortal(obj));
174189
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_DEFERRED };
175-
}
176-
# define PyStackRef_FromPyObjectImmortal(obj) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))
177190
#else
178-
# define PyStackRef_FromPyObjectImmortal(obj) ((_PyStackRef){ .bits = (uintptr_t)(obj) })
191+
assert(_Py_IsImmortal(obj));
192+
return ((_PyStackRef){ .bits = (uintptr_t)(obj) });
179193
#endif
194+
}
195+
196+
#define PyStackRef_FromPyObjectImmortal(obj) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))
180197

181198

182199
#define PyStackRef_CLEAR(op) \
@@ -189,20 +206,20 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
189206
} \
190207
} while (0)
191208

192-
#ifdef Py_GIL_DISABLED
193209
static inline void
194210
PyStackRef_CLOSE(_PyStackRef stackref)
195211
{
212+
#ifdef Py_GIL_DISABLED
196213
if (PyStackRef_IsDeferred(stackref)) {
197214
// No assert for being immortal or deferred here.
198215
// The GC unsets deferred objects right before clearing.
199216
return;
200217
}
201218
Py_DECREF(PyStackRef_AsPyObjectBorrow(stackref));
202-
}
203219
#else
204-
# define PyStackRef_CLOSE(stackref) Py_DECREF(PyStackRef_AsPyObjectBorrow(stackref));
220+
Py_DECREF(PyStackRef_AsPyObjectBorrow(stackref));
205221
#endif
222+
}
206223

207224
#define PyStackRef_XCLOSE(stackref) \
208225
do { \
@@ -213,21 +230,32 @@ PyStackRef_CLOSE(_PyStackRef stackref)
213230
} while (0);
214231

215232

216-
#ifdef Py_GIL_DISABLED
217233
static inline _PyStackRef
218234
PyStackRef_DUP(_PyStackRef stackref)
219235
{
236+
#ifdef Py_GIL_DISABLED
220237
if (PyStackRef_IsDeferred(stackref)) {
221238
assert(PyStackRef_IsNull(stackref) ||
222239
_Py_IsImmortal(PyStackRef_AsPyObjectBorrow(stackref)));
223240
return stackref;
224241
}
225242
Py_INCREF(PyStackRef_AsPyObjectBorrow(stackref));
226243
return stackref;
227-
}
228244
#else
229-
# define PyStackRef_DUP(stackref) PyStackRef_FromPyObjectSteal(Py_NewRef(PyStackRef_AsPyObjectBorrow(stackref)));
245+
Py_INCREF(PyStackRef_AsPyObjectBorrow(stackref));
246+
return stackref;
230247
#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+
231259

232260
static inline void
233261
_PyObjectStack_FromStackRefStack(PyObject **dst, const _PyStackRef *src, size_t length)

0 commit comments

Comments
 (0)
0