diff -r adb6b029b102 Include/pymem.h --- a/Include/pymem.h Wed Mar 09 15:02:31 2016 +0100 +++ b/Include/pymem.h Thu Mar 10 15:28:38 2016 +0100 @@ -16,6 +16,19 @@ PyAPI_FUNC(void *) PyMem_RawMalloc(size_ PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize); PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size); PyAPI_FUNC(void) PyMem_RawFree(void *ptr); + +/* Track an allocated memory block in the tracemalloc module. + Return 0 on success, return -1 on error (failed to allocate memory to store + the trace). + + The GIL must be held to call this function. */ +PyAPI_FUNC(int) _PyTraceMalloc_Track(void *ptr, size_t size); + +/* Untrack an allocated memory block in the tracemalloc module. + Do nothing if the block was not tracked. + + The GIL doesn't need to be held to call this function. */ +PyAPI_FUNC(void) _PyTraceMalloc_Untrack(void *ptr); #endif diff -r adb6b029b102 Modules/_tracemalloc.c --- a/Modules/_tracemalloc.c Wed Mar 09 15:02:31 2016 +0100 +++ b/Modules/_tracemalloc.c Thu Mar 10 15:28:38 2016 +0100 @@ -1395,6 +1395,24 @@ parse_sys_xoptions(PyObject *value) } int +_PyTraceMalloc_Track(void *ptr, size_t size) +{ + int res; + TABLES_LOCK(); + res = tracemalloc_add_trace(ptr, size); + TABLES_UNLOCK(); + return res; +} + +void +_PyTraceMalloc_Untrack(void *ptr) +{ + TABLES_LOCK(); + tracemalloc_remove_trace(ptr); + TABLES_UNLOCK(); +} + +int _PyTraceMalloc_Init(void) { char *p;