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