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

source code of include/python3.10/cpython/pymem.h