> Be aware about external code which allocate memory itself (i.e. expat).
Well, I know that it will hard to cover 100% of the stdlib. I just want to replace the most obvious calls.
Some libraries can be configured to use a custom memory allocators:
- zlib: zalloc and zfree, http://www.zlib.net/manual.html#Usage
- bz2
- lzma: LzmaEnc_Create parameter
- OpenSSL: CRYPTO_set_mem_functions
- etc.
We should probably uses these functions to reuse Python allocators (PyMem_Malloc()), or maybe only if PyMem_SetAllocators() has been called? (if Python does not use system allocators, aka malloc)
See also #18178 for libffi, it may be related.
The _decimal module configures libmpdec to use PyMem_Malloc:
#define _Py_DEC_MINALLOC 4
mpd_mallocfunc = PyMem_Malloc;
mpd_reallocfunc = PyMem_Realloc;
mpd_callocfunc = mpd_callocfunc_em;
mpd_free = PyMem_Free;
mpd_setminalloc(_Py_DEC_MINALLOC); |