Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C-API Docs] Clarify About When PyMem_SetAllocator() Can Be Called #96997

Closed
ericsnowcurrently opened this issue Sep 21, 2022 · 5 comments
Closed
Assignees
Labels
docs Documentation in the Doc dir

Comments

@ericsnowcurrently
Copy link
Member

ericsnowcurrently commented Sep 21, 2022

There are two uses for PyMem_SetAllocator():

  1. set the allocator for the runtime to use
  2. set an allocator hook that wraps the current allocator

The first must be done (only by embedders) "just after the pre-initialization, and before the Python initialization", while the second may be done at any point (e.g. by extension modules, like tracemalloc). See #96975 (comment).

The docs for PyMem_SetAllocator() should make the distinction more clear, as well as identifying the restriction on (1).

@ericsnowcurrently ericsnowcurrently added the docs Documentation in the Doc dir label Sep 21, 2022
@ericsnowcurrently
Copy link
Member Author

CC @vstinner

@pablogsal pablogsal self-assigned this Sep 28, 2022
@encukou
Copy link
Member

encukou commented Oct 31, 2022

Victor mentioned that some limitations for PyMem_SetAllocator are documented elsewhere:

PyMem_SetAllocator() can be called after Py_PreInitialize() and before Py_InitializeFromConfig() to install a custom memory allocator. It can be called before Py_PreInitialize() if PyPreConfig.allocator is set to PYMEM_ALLOCATOR_NOT_SET.

@pablogsal
Copy link
Member

I am preparing a PR to document this in the API docs, which I think is the correct place. This should also be a warning box, I think

pablogsal added a commit to pablogsal/cpython that referenced this issue Nov 1, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 2, 2022
…H-98977)

(cherry picked from commit c053284)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 2, 2022
…H-98977)

(cherry picked from commit c053284)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
miss-islington added a commit that referenced this issue Nov 2, 2022
(cherry picked from commit c053284)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
miss-islington added a commit that referenced this issue Nov 2, 2022
(cherry picked from commit c053284)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
@vstinner
Copy link
Member

vstinner commented Nov 3, 2022

Pablo's documentation LGTM.

@vstinner
Copy link
Member

vstinner commented Nov 3, 2022

Allocating memory is easy. The problem is more to know how to release allocated memory :-) Python/initconfig.c temporarily override the memory allocators to get a known memory allocator at exit. Example:

    /* Py_SetStandardStreamEncoding() can be called before Py_Initialize(),
       but Py_Initialize() can change the allocator. Use a known allocator
       to be able to release the memory later. */
    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    // ... code using PyMem_RawMalloc() and PyMem_RawFree() ...

    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

An alternative would be to add functions with a "context" parameter which can be used to get memory allocator functions. I considered that for Python/fileutils.c functions like Py_DecodeLocale() and _Py_EncodeLocaleRaw() which use PyMem_RawMalloc(). But I wasn't convinced that it's worth it, since _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc) is only used in a few places.

Code search, where I annotated public functions:

$ git grep '_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW'
Objects/obmalloc.c:        (void)_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL);

# PyImport_ExtendInittab()
Python/import.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/import.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

# Py_SetStandardStreamEncoding()
Python/initconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/initconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/initconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/initconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

# Py_SetPath(), Py_SetPythonHome() and other deprecated functions
Python/pathconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pathconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pathconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pathconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pathconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pathconfig.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

# _PyRuntimeState_Init(), _PyRuntimeState_ReInitThreads()
Python/pystate.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pystate.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pystate.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/pystate.c:        _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

# PySys_AddWarnOption(), PySys_AddXOption()
Python/sysmodule.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Python/sysmodule.c:    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

4 participants