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