Skip to content

Commit 6f59c31

Browse files
committed
gh-162539: Define the ctypes pin helpers in _ctypes.c
Move the pin and unpin helpers from static inline functions in ctypes.h to extern functions in _ctypes.c, as suggested in review.
1 parent d60658f commit 6f59c31

2 files changed

Lines changed: 49 additions & 43 deletions

File tree

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,6 +3621,52 @@ _PyCData_set(ctypes_state *st,
36213621
return NULL;
36223622
}
36233623

3624+
/* gh-162539: pinning the memory block of a ctypes object; see ctypes.h. */
3625+
3626+
static CDataObject *
3627+
cdata_memory_owner(CDataObject *obj)
3628+
{
3629+
CDataObject *base = obj->b_base;
3630+
while (base != NULL
3631+
&& obj->b_ptr >= base->b_ptr
3632+
&& obj->b_ptr + obj->b_size <= base->b_ptr + base->b_size)
3633+
{
3634+
obj = base;
3635+
base = obj->b_base;
3636+
}
3637+
return obj;
3638+
}
3639+
3640+
void
3641+
_PyCData_Pin(CDataObject *obj)
3642+
{
3643+
CDataObject *owner = cdata_memory_owner(obj);
3644+
#ifdef Py_GIL_DISABLED
3645+
_Py_atomic_add_int(&owner->b_pins, 1);
3646+
#else
3647+
owner->b_pins++;
3648+
#endif
3649+
}
3650+
3651+
void
3652+
_PyCData_Unpin(CDataObject *obj)
3653+
{
3654+
CDataObject *owner = cdata_memory_owner(obj);
3655+
#ifdef Py_GIL_DISABLED
3656+
int old = _Py_atomic_add_int(&owner->b_pins, -1);
3657+
#else
3658+
int old = owner->b_pins--;
3659+
#endif
3660+
assert(old > 0);
3661+
(void)old;
3662+
}
3663+
3664+
int
3665+
_PyCData_IsPinned(CDataObject *obj)
3666+
{
3667+
return FT_ATOMIC_LOAD_INT(obj->b_pins) > 0;
3668+
}
3669+
36243670
/*
36253671
* Set a slice in object 'dst', which has the type 'type',
36263672
* to the value 'value'.

‎Modules/_ctypes/ctypes.h‎

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -269,49 +269,9 @@ extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palig
269269
Every _PyCData_Pin() must be balanced by exactly one _PyCData_Unpin() on
270270
the same object, on every path including errors. */
271271

272-
static inline CDataObject *
273-
_PyCData_MemoryOwner(CDataObject *obj)
274-
{
275-
CDataObject *base = obj->b_base;
276-
while (base != NULL
277-
&& obj->b_ptr >= base->b_ptr
278-
&& obj->b_ptr + obj->b_size <= base->b_ptr + base->b_size)
279-
{
280-
obj = base;
281-
base = obj->b_base;
282-
}
283-
return obj;
284-
}
285-
286-
static inline void
287-
_PyCData_Pin(CDataObject *obj)
288-
{
289-
CDataObject *owner = _PyCData_MemoryOwner(obj);
290-
#ifdef Py_GIL_DISABLED
291-
_Py_atomic_add_int(&owner->b_pins, 1);
292-
#else
293-
owner->b_pins++;
294-
#endif
295-
}
296-
297-
static inline void
298-
_PyCData_Unpin(CDataObject *obj)
299-
{
300-
CDataObject *owner = _PyCData_MemoryOwner(obj);
301-
#ifdef Py_GIL_DISABLED
302-
int old = _Py_atomic_add_int(&owner->b_pins, -1);
303-
#else
304-
int old = owner->b_pins--;
305-
#endif
306-
assert(old > 0);
307-
(void)old;
308-
}
309-
310-
static inline int
311-
_PyCData_IsPinned(CDataObject *obj)
312-
{
313-
return FT_ATOMIC_LOAD_INT(obj->b_pins) > 0;
314-
}
272+
extern void _PyCData_Pin(CDataObject *obj);
273+
extern void _PyCData_Unpin(CDataObject *obj);
274+
extern int _PyCData_IsPinned(CDataObject *obj);
315275

316276
#define PyCSimpleTypeObject_CheckExact(st, v) Py_IS_TYPE((v), (st)->PyCSimpleType_Type)
317277
#define PyCSimpleTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCSimpleType_Type)

0 commit comments

Comments
 (0)