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