diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-06-14 22:37:46 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-06-14 22:37:46 (GMT) |
commit | 4d7056258b07df7cbb1b9b44e7a1a9bad04f7454 (patch) | |
tree | f5b3d2b16be7b8d16dd234ac59c722d0ca24e0b4 /Include/objimpl.h | |
parent | 8c18da20f979cf428414a038bfaee46283e12fa2 (diff) | |
download | cpython-4d7056258b07df7cbb1b9b44e7a1a9bad04f7454.zip cpython-4d7056258b07df7cbb1b9b44e7a1a9bad04f7454.tar.gz cpython-4d7056258b07df7cbb1b9b44e7a1a9bad04f7454.tar.bz2 |
Issue #3329: Add new APIs to customize memory allocators
* Add a new PyMemAllocators structure
* New functions:
- PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree(): GIL-free memory
allocator functions
- PyMem_GetRawAllocators(), PyMem_SetRawAllocators()
- PyMem_GetAllocators(), PyMem_SetAllocators()
- PyMem_SetupDebugHooks()
- _PyObject_GetArenaAllocators(), _PyObject_SetArenaAllocators()
* Add unit test for PyMem_Malloc(0) and PyObject_Malloc(0)
* Add unit test for new get/set allocators functions
* PyObject_Malloc() now falls back on PyMem_Malloc() instead of malloc() if
size is bigger than SMALL_REQUEST_THRESHOLD, and PyObject_Realloc() falls
back on PyMem_Realloc() instead of realloc()
* PyMem_Malloc() and PyMem_Realloc() now always call malloc() and realloc(),
instead of calling PyObject_Malloc() and PyObject_Realloc() in debug mode
Diffstat (limited to 'Include/objimpl.h')
-rw-r--r-- | Include/objimpl.h | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/Include/objimpl.h b/Include/objimpl.h index 8f36360..740c46f 100644 --- a/Include/objimpl.h +++ b/Include/objimpl.h @@ -94,9 +94,9 @@ PyObject_{New, NewVar, Del}. the object gets initialized via PyObject_{Init, InitVar} after obtaining the raw memory. */ -PyAPI_FUNC(void *) PyObject_Malloc(size_t); -PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t); -PyAPI_FUNC(void) PyObject_Free(void *); +PyAPI_FUNC(void *) PyObject_Malloc(size_t size); +PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size); +PyAPI_FUNC(void) PyObject_Free(void *ptr); /* This function returns the number of allocated memory blocks, regardless of size */ PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); @@ -106,41 +106,46 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); #ifndef Py_LIMITED_API PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out); #endif /* #ifndef Py_LIMITED_API */ -#ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */ -PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes); -PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes); -PyAPI_FUNC(void) _PyObject_DebugFree(void *p); -PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p); -PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p); -PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes); -PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes); -PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p); -PyAPI_FUNC(void) _PyObject_DebugCheckAddressApi(char api, const void *p); -PyAPI_FUNC(void *) _PyMem_DebugMalloc(size_t nbytes); -PyAPI_FUNC(void *) _PyMem_DebugRealloc(void *p, size_t nbytes); -PyAPI_FUNC(void) _PyMem_DebugFree(void *p); -#define PyObject_MALLOC _PyObject_DebugMalloc -#define PyObject_Malloc _PyObject_DebugMalloc -#define PyObject_REALLOC _PyObject_DebugRealloc -#define PyObject_Realloc _PyObject_DebugRealloc -#define PyObject_FREE _PyObject_DebugFree -#define PyObject_Free _PyObject_DebugFree - -#else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */ +#endif + +/* Macros */ #define PyObject_MALLOC PyObject_Malloc #define PyObject_REALLOC PyObject_Realloc #define PyObject_FREE PyObject_Free -#endif - -#else /* ! WITH_PYMALLOC */ -#define PyObject_MALLOC PyMem_MALLOC -#define PyObject_REALLOC PyMem_REALLOC -#define PyObject_FREE PyMem_FREE - -#endif /* WITH_PYMALLOC */ - #define PyObject_Del PyObject_Free -#define PyObject_DEL PyObject_FREE +#define PyObject_DEL PyObject_Free + +/* Get internal functions of PyObject_Malloc(), PyObject_Realloc() and + PyObject_Free(). *ctx_p is an arbitrary user value. */ +PyAPI_FUNC(void) PyObject_GetAllocators(PyMemAllocators *allocators); + +/* Set internal functions of PyObject_Malloc(), PyObject_Realloc() and PyObject_Free(). + ctx is an arbitrary user value. + + malloc(ctx, 0) and realloc(ctx, ptr, 0) must not return NULL: it would be + treated as an error. + + PyMem_SetupDebugHooks() should be called to reinstall debug hooks if new + functions do no call original functions anymore. */ +PyAPI_FUNC(void) PyObject_SetAllocators(PyMemAllocators *allocators); + +/* Get internal functions allocating and deallocating arenas for + PyObject_Malloc(), PyObject_Realloc() and PyObject_Free(). + *ctx_p is an arbitrary user value. */ +PyAPI_FUNC(void) _PyObject_GetArenaAllocators( + void **ctx_p, + void* (**malloc_p) (void *ctx, size_t size), + void (**free_p) (void *ctx, void *ptr, size_t size) + ); + +/* Get internal functions allocating and deallocating arenas for + PyObject_Malloc(), PyObject_Realloc() and PyObject_Free(). + ctx is an arbitrary user value. */ +PyAPI_FUNC(void) _PyObject_SetArenaAllocators( + void *ctx, + void* (*malloc) (void *ctx, size_t size), + void (*free) (void *ctx, void *ptr, size_t size) + ); /* * Generic object allocator interface |