• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* Try to get the allocators name set by _PyMem_SetupAllocators(). */
15 PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void);
16 
17 PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
18 
19 /* strdup() using PyMem_RawMalloc() */
20 PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
21 
22 /* strdup() using PyMem_Malloc() */
23 PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
24 
25 /* wcsdup() using PyMem_RawMalloc() */
26 PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);
27 
28 
29 typedef 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 
40 typedef 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 
53 typedef 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. */
71 PyAPI_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. */
85 PyAPI_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. */
104 PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
105 
106 #ifdef __cplusplus
107 }
108 #endif
109