Victor Stinner | 9820c07 | 2019-04-15 17:00:19 +0200 | [diff] [blame^] | 1 | #ifndef Py_CPYTHON_PYMEM_H |
| 2 | # error "this header file must not be included directly" |
| 3 | #endif |
| 4 | |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
| 9 | PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size); |
| 10 | PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize); |
| 11 | PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size); |
| 12 | PyAPI_FUNC(void) PyMem_RawFree(void *ptr); |
| 13 | |
| 14 | /* Configure the Python memory allocators. Pass NULL to use default |
| 15 | allocators. */ |
| 16 | PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt); |
| 17 | |
| 18 | /* Try to get the allocators name set by _PyMem_SetupAllocators(). */ |
| 19 | PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void); |
| 20 | |
| 21 | PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize); |
| 22 | |
| 23 | /* strdup() using PyMem_RawMalloc() */ |
| 24 | PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str); |
| 25 | |
| 26 | /* strdup() using PyMem_Malloc() */ |
| 27 | PyAPI_FUNC(char *) _PyMem_Strdup(const char *str); |
| 28 | |
| 29 | /* wcsdup() using PyMem_RawMalloc() */ |
| 30 | PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str); |
| 31 | |
| 32 | |
| 33 | typedef enum { |
| 34 | /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */ |
| 35 | PYMEM_DOMAIN_RAW, |
| 36 | |
| 37 | /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */ |
| 38 | PYMEM_DOMAIN_MEM, |
| 39 | |
| 40 | /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */ |
| 41 | PYMEM_DOMAIN_OBJ |
| 42 | } PyMemAllocatorDomain; |
| 43 | |
| 44 | typedef struct { |
| 45 | /* user context passed as the first argument to the 4 functions */ |
| 46 | void *ctx; |
| 47 | |
| 48 | /* allocate a memory block */ |
| 49 | void* (*malloc) (void *ctx, size_t size); |
| 50 | |
| 51 | /* allocate a memory block initialized by zeros */ |
| 52 | void* (*calloc) (void *ctx, size_t nelem, size_t elsize); |
| 53 | |
| 54 | /* allocate or resize a memory block */ |
| 55 | void* (*realloc) (void *ctx, void *ptr, size_t new_size); |
| 56 | |
| 57 | /* release a memory block */ |
| 58 | void (*free) (void *ctx, void *ptr); |
| 59 | } PyMemAllocatorEx; |
| 60 | |
| 61 | /* Get the memory block allocator of the specified domain. */ |
| 62 | PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain, |
| 63 | PyMemAllocatorEx *allocator); |
| 64 | |
| 65 | /* Set the memory block allocator of the specified domain. |
| 66 | |
| 67 | The new allocator must return a distinct non-NULL pointer when requesting |
| 68 | zero bytes. |
| 69 | |
| 70 | For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL |
| 71 | is not held when the allocator is called. |
| 72 | |
| 73 | If the new allocator is not a hook (don't call the previous allocator), the |
| 74 | PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks |
| 75 | on top on the new allocator. */ |
| 76 | PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain, |
| 77 | PyMemAllocatorEx *allocator); |
| 78 | |
| 79 | /* Setup hooks to detect bugs in the following Python memory allocator |
| 80 | functions: |
| 81 | |
| 82 | - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree() |
| 83 | - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free() |
| 84 | - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() |
| 85 | |
| 86 | Newly allocated memory is filled with the byte 0xCB, freed memory is filled |
| 87 | with the byte 0xDB. Additional checks: |
| 88 | |
| 89 | - detect API violations, ex: PyObject_Free() called on a buffer allocated |
| 90 | by PyMem_Malloc() |
| 91 | - detect write before the start of the buffer (buffer underflow) |
| 92 | - detect write after the end of the buffer (buffer overflow) |
| 93 | |
| 94 | The function does nothing if Python is not compiled is debug mode. */ |
| 95 | PyAPI_FUNC(void) PyMem_SetupDebugHooks(void); |
| 96 | |
| 97 | #ifdef __cplusplus |
| 98 | } |
| 99 | #endif |