| 1 |
#ifndef Py_CPYTHON_PYMEM_H |
| 2 |
# error "this header file must not be included directly" |
| 3 |
#endif |
| 4 |
|
| 5 |
typedef enum { |
| 6 |
/* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */ |
| 7 |
PYMEM_DOMAIN_RAW, |
| 8 |
|
| 9 |
/* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */ |
| 10 |
PYMEM_DOMAIN_MEM, |
| 11 |
|
| 12 |
/* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */ |
| 13 |
PYMEM_DOMAIN_OBJ |
| 14 |
} PyMemAllocatorDomain; |
| 15 |
|
| 16 |
typedef enum { |
| 17 |
PYMEM_ALLOCATOR_NOT_SET = 0, |
| 18 |
PYMEM_ALLOCATOR_DEFAULT = 1, |
| 19 |
PYMEM_ALLOCATOR_DEBUG = 2, |
| 20 |
PYMEM_ALLOCATOR_MALLOC = 3, |
| 21 |
PYMEM_ALLOCATOR_MALLOC_DEBUG = 4, |
| 22 |
#ifdef WITH_PYMALLOC |
| 23 |
PYMEM_ALLOCATOR_PYMALLOC = 5, |
| 24 |
PYMEM_ALLOCATOR_PYMALLOC_DEBUG = 6, |
| 25 |
#endif |
| 26 |
#ifdef WITH_MIMALLOC |
| 27 |
PYMEM_ALLOCATOR_MIMALLOC = 7, |
| 28 |
PYMEM_ALLOCATOR_MIMALLOC_DEBUG = 8, |
| 29 |
#endif |
| 30 |
} PyMemAllocatorName; |
| 31 |
|
| 32 |
|
| 33 |
typedef struct { |
| 34 |
/* user context passed as the first argument to the 4 functions */ |
| 35 |
void *ctx; |
| 36 |
|
| 37 |
/* allocate a memory block */ |
| 38 |
void* (*malloc) (void *ctx, size_t size); |
| 39 |
|
| 40 |
/* allocate a memory block initialized by zeros */ |
| 41 |
void* (*calloc) (void *ctx, size_t nelem, size_t elsize); |
| 42 |
|
| 43 |
/* allocate or resize a memory block */ |
| 44 |
void* (*realloc) (void *ctx, void *ptr, size_t new_size); |
| 45 |
|
| 46 |
/* release a memory block */ |
| 47 |
void (*free) (void *ctx, void *ptr); |
| 48 |
} PyMemAllocatorEx; |
| 49 |
|
| 50 |
/* Get the memory block allocator of the specified domain. */ |
| 51 |
PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain, |
| 52 |
PyMemAllocatorEx *allocator); |
| 53 |
|
| 54 |
/* Set the memory block allocator of the specified domain. |
| 55 |
|
| 56 |
The new allocator must return a distinct non-NULL pointer when requesting |
| 57 |
zero bytes. |
| 58 |
|
| 59 |
For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL |
| 60 |
is not held when the allocator is called. |
| 61 |
|
| 62 |
If the new allocator is not a hook (don't call the previous allocator), the |
| 63 |
PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks |
| 64 |
on top on the new allocator. */ |
| 65 |
PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain, |
| 66 |
PyMemAllocatorEx *allocator); |
| 67 |
|
| 68 |
/* Setup hooks to detect bugs in the following Python memory allocator |
| 69 |
functions: |
| 70 |
|
| 71 |
- PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree() |
| 72 |
- PyMem_Malloc(), PyMem_Realloc(), PyMem_Free() |
| 73 |
- PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() |
| 74 |
|
| 75 |
Newly allocated memory is filled with the byte 0xCB, freed memory is filled |
| 76 |
with the byte 0xDB. Additional checks: |
| 77 |
|
| 78 |
- detect API violations, ex: PyObject_Free() called on a buffer allocated |
| 79 |
by PyMem_Malloc() |
| 80 |
- detect write before the start of the buffer (buffer underflow) |
| 81 |
- detect write after the end of the buffer (buffer overflow) |
| 82 |
|
| 83 |
The function does nothing if Python is not compiled is debug mode. */ |
| 84 |
PyAPI_FUNC(void) PyMem_SetupDebugHooks(void); |