[C-API Docs] Clarify Constraints on the Arena Allocator #101655
Labels
3.11
only security fixes
3.12
bugs and security fixes
3.13
bugs and security fixes
docs
Documentation in the Doc dir
topic-C-API
The "arena" allocator is used by obmalloc, as well as the code for allocating frames. It is exposed by the following public C-API:
PyObject_GetArenaAllocator()
PyObject_SetArenaAllocator()
(It is also exposed/used internally as
_PyObject_VirtualAlloc()
and_PyObject_VirtualFree()
. The implementation is exposed internally as_PyMem_ArenaAlloc()
and_PyMem_ArenaFree()
.)First of all, the docs for
PyObject_GetArenaAllocator()
are not clear at all about what the function should be used for. I expect it is only meant for use in combination withPyObject_SetArenaAllocator()
(e.g. to wrap the old one; to check what's set). That should be clarified.Second, the C-API docs make it clear that the two public functions may be called before the runtime is initialized. However, the following should be clarified, as there are no documented restrictions AFAICS:
PyObject_SetArenaAllocator()
be called after runtime init?PyMem_SetAllocator()
)PyMem_SetAllocator()
)PyObject_GetArenaAllocator()
be used directly?PyObject_SetArenaAllocator()
is allowed (i.e. only before init)PyObject_GetArenaAllocator()
be called with the GIL held after runtime init?PyObject_SetArenaAllocator()
is okay)PyObject_SetArenaAllocator()
be thread-safe (and reentrant)?If we do specify tighter restrictions than were already documented then we'll need to consider who might be using the public functions in unexpected ways. However, I'd be surprised if there were more than a few (if any) using them at all.
Regardless, ISTM it would be better to deprecate the two public functions and replace them with a new
PyConfig
field. That would align a lot better with the intention. If there is a need to see if the currently set allocator matches some expected one, then it would be better to have an explicit function for that (e.g.PyObject_IsArenaAllocator()
) rather than exposingPyObject_GetArenaAllocator()
for that purpose.The same goes for
PyMem_SetAllocator()
, where we'd have an additionalPyMem_SetAllocatorWrapper()
for the after-init case.The text was updated successfully, but these errors were encountered: