<p dir="ltr"><br>
On 13 Jun 2013 09:09, "Victor Stinner" <<a href="mailto:victor.stinner@gmail.com">victor.stinner@gmail.com</a>> wrote:<br>
><br>
> Hi,<br>
><br>
> I would like to improve memory allocators of Python. My two use cases<br>
> are replacing memory allocators with custom allocators in embedded<br>
> system and hooking allocators to track usage of memory.<br>
><br>
> I wrote a patch for this, I'm going to commit it if nobody complains:<br>
> <a href="http://bugs.python.org/issue3329">http://bugs.python.org/issue3329</a><br>
><br>
> Using this patch, detecting memory corruptions (buffer underflow and<br>
> overflow) can be done without recompilation. We may add an environment<br>
> variable to enable Python debug functions at runtime, example:<br>
> PYDEBUGMALLOC=1. There is just a restriction: the environment variable<br>
> would not be ignored with -E command line option, because command line<br>
> options are parsed after the first memory allocation. What do you<br>
> think?</p>
<p dir="ltr">The rest of it sounds fine, but please don't add the runtime switching support to our existing main function. Interpreter startup is a mess already. If you were interested in helping directly with PEP 432, though, that would be good - I haven't been able to spend much time on it lately.</p>

<p dir="ltr">Cheers,<br>
Nick.</p>
<p dir="ltr">><br>
> *****<br>
><br>
> The patch adds the following functions:<br>
><br>
> void PyMem_GetAllocators(<br>
>     void **ctx_p,<br>
>     void* (**malloc_p) (void *ctx, size_t size),<br>
>     void* (**realloc_p) (void *ctx, void *ptr, size_t size),<br>
>     void (**free_p) (void *ctx, void *ptr));<br>
><br>
> void PyMem_SetAllocators(<br>
>     void *ctx,<br>
>     void* (*malloc) (void *ctx, size_t size),<br>
>     void* (*realloc) (void *ctx, void *ptr, size_t size),<br>
>     void (*free) (void *ctx, void *ptr));<br>
><br>
> It adds 4 similar functions (get/set) for PyObject_Malloc() and<br>
> allocators of pymalloc arenas.<br>
><br>
> *****<br>
><br>
> For the "track usage of memory" use case, see the following project<br>
> which hooks memory allocators using PyMem_SetAllocators() and<br>
> PyObject_SetAllocators() to get allocated bytes per filename and line<br>
> number.<br>
> <a href="https://pypi.python.org/pypi/pytracemalloc">https://pypi.python.org/pypi/pytracemalloc</a><br>
><br>
> *****<br>
><br>
> Another issue proposes to use VirtualAlloc() and VirtualFree() for<br>
> pymalloc arenas, see:<br>
> <a href="http://bugs.python.org/issue13483">http://bugs.python.org/issue13483</a><br>
><br>
> I don't know if it would be interesting, but it would now possible to<br>
> choose the memory allocator (malloc, mmap, HeapAlloc, VirtualAlloc,<br>
> ...) at runtime, with an environment variable for example.<br>
><br>
> Victor<br>
> _______________________________________________<br>
> Python-Dev mailing list<br>
> <a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-dev">http://mail.python.org/mailman/listinfo/python-dev</a><br>
> Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com">http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com</a><br>
</p>