Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

pymem.h 8.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  1. /* The PyMem_ family: low-level memory allocation interfaces.
  2. See objimpl.h for the PyObject_ memory family.
  3. */
  4. #ifndef Py_PYMEM_H
  5. #define Py_PYMEM_H
  6. #include "pyport.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #ifndef Py_LIMITED_API
  11. PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
  12. PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
  13. PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
  14. PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
  15. /* Configure the Python memory allocators. Pass NULL to use default
  16. allocators. */
  17. PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt);
  18. /* Try to get the allocators name set by _PyMem_SetupAllocators(). */
  19. PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void);
  20. /* Track an allocated memory block in the tracemalloc module.
  21. Return 0 on success, return -1 on error (failed to allocate memory to store
  22. the trace).
  23. Return -2 if tracemalloc is disabled.
  24. If memory block is already tracked, update the existing trace. */
  25. PyAPI_FUNC(int) PyTraceMalloc_Track(
  26. unsigned int domain,
  27. uintptr_t ptr,
  28. size_t size);
  29. /* Untrack an allocated memory block in the tracemalloc module.
  30. Do nothing if the block was not tracked.
  31. Return -2 if tracemalloc is disabled, otherwise return 0. */
  32. PyAPI_FUNC(int) PyTraceMalloc_Untrack(
  33. unsigned int domain,
  34. uintptr_t ptr);
  35. /* Get the traceback where a memory block was allocated.
  36. Return a tuple of (filename: str, lineno: int) tuples.
  37. Return None if the tracemalloc module is disabled or if the memory block
  38. is not tracked by tracemalloc.
  39. Raise an exception and return NULL on error. */
  40. PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
  41. unsigned int domain,
  42. uintptr_t ptr);
  43. PyAPI_FUNC(int) _PyMem_IsFreed(void *ptr, size_t size);
  44. #endif /* !defined(Py_LIMITED_API) */
  45. /* BEWARE:
  46. Each interface exports both functions and macros. Extension modules should
  47. use the functions, to ensure binary compatibility across Python versions.
  48. Because the Python implementation is free to change internal details, and
  49. the macros may (or may not) expose details for speed, if you do use the
  50. macros you must recompile your extensions with each Python release.
  51. Never mix calls to PyMem_ with calls to the platform malloc/realloc/
  52. calloc/free. For example, on Windows different DLLs may end up using
  53. different heaps, and if you use PyMem_Malloc you'll get the memory from the
  54. heap used by the Python DLL; it could be a disaster if you free()'ed that
  55. directly in your own extension. Using PyMem_Free instead ensures Python
  56. can return the memory to the proper heap. As another example, in
  57. PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
  58. memory functions in special debugging wrappers that add additional
  59. debugging info to dynamic memory blocks. The system routines have no idea
  60. what to do with that stuff, and the Python wrappers have no idea what to do
  61. with raw blocks obtained directly by the system routines then.
  62. The GIL must be held when using these APIs.
  63. */
  64. /*
  65. * Raw memory interface
  66. * ====================
  67. */
  68. /* Functions
  69. Functions supplying platform-independent semantics for malloc/realloc/
  70. free. These functions make sure that allocating 0 bytes returns a distinct
  71. non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
  72. may be returned), even if the platform malloc and realloc don't.
  73. Returned pointers must be checked for NULL explicitly. No action is
  74. performed on failure (no exception is set, no warning is printed, etc).
  75. */
  76. PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
  77. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  78. PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
  79. #endif
  80. PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
  81. PyAPI_FUNC(void) PyMem_Free(void *ptr);
  82. #ifndef Py_LIMITED_API
  83. /* strdup() using PyMem_RawMalloc() */
  84. PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
  85. /* strdup() using PyMem_Malloc() */
  86. PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
  87. /* wcsdup() using PyMem_RawMalloc() */
  88. PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);
  89. #endif
  90. /* Macros. */
  91. /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
  92. for malloc(0), which would be treated as an error. Some platforms
  93. would return a pointer with no memory behind it, which would break
  94. pymalloc. To solve these problems, allocate an extra byte. */
  95. /* Returns NULL to indicate error if a negative size or size larger than
  96. Py_ssize_t can represent is supplied. Helps prevents security holes. */
  97. #define PyMem_MALLOC(n) PyMem_Malloc(n)
  98. #define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
  99. #define PyMem_FREE(p) PyMem_Free(p)
  100. /*
  101. * Type-oriented memory interface
  102. * ==============================
  103. *
  104. * Allocate memory for n objects of the given type. Returns a new pointer
  105. * or NULL if the request was too large or memory allocation failed. Use
  106. * these macros rather than doing the multiplication yourself so that proper
  107. * overflow checking is always done.
  108. */
  109. #define PyMem_New(type, n) \
  110. ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  111. ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
  112. #define PyMem_NEW(type, n) \
  113. ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  114. ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
  115. /*
  116. * The value of (p) is always clobbered by this macro regardless of success.
  117. * The caller MUST check if (p) is NULL afterwards and deal with the memory
  118. * error if so. This means the original value of (p) MUST be saved for the
  119. * caller's memory error handler to not lose track of it.
  120. */
  121. #define PyMem_Resize(p, type, n) \
  122. ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  123. (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
  124. #define PyMem_RESIZE(p, type, n) \
  125. ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
  126. (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
  127. /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
  128. * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
  129. */
  130. #define PyMem_Del PyMem_Free
  131. #define PyMem_DEL PyMem_FREE
  132. #ifndef Py_LIMITED_API
  133. typedef enum {
  134. /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
  135. PYMEM_DOMAIN_RAW,
  136. /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
  137. PYMEM_DOMAIN_MEM,
  138. /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
  139. PYMEM_DOMAIN_OBJ
  140. } PyMemAllocatorDomain;
  141. typedef struct {
  142. /* user context passed as the first argument to the 4 functions */
  143. void *ctx;
  144. /* allocate a memory block */
  145. void* (*malloc) (void *ctx, size_t size);
  146. /* allocate a memory block initialized by zeros */
  147. void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
  148. /* allocate or resize a memory block */
  149. void* (*realloc) (void *ctx, void *ptr, size_t new_size);
  150. /* release a memory block */
  151. void (*free) (void *ctx, void *ptr);
  152. } PyMemAllocatorEx;
  153. /* Get the memory block allocator of the specified domain. */
  154. PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
  155. PyMemAllocatorEx *allocator);
  156. /* Set the memory block allocator of the specified domain.
  157. The new allocator must return a distinct non-NULL pointer when requesting
  158. zero bytes.
  159. For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
  160. is not held when the allocator is called.
  161. If the new allocator is not a hook (don't call the previous allocator), the
  162. PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
  163. on top on the new allocator. */
  164. PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
  165. PyMemAllocatorEx *allocator);
  166. /* Setup hooks to detect bugs in the following Python memory allocator
  167. functions:
  168. - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
  169. - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
  170. - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
  171. Newly allocated memory is filled with the byte 0xCB, freed memory is filled
  172. with the byte 0xDB. Additional checks:
  173. - detect API violations, ex: PyObject_Free() called on a buffer allocated
  174. by PyMem_Malloc()
  175. - detect write before the start of the buffer (buffer underflow)
  176. - detect write after the end of the buffer (buffer overflow)
  177. The function does nothing if Python is not compiled is debug mode. */
  178. PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
  179. #endif
  180. #ifdef Py_BUILD_CORE
  181. /* Set the memory allocator of the specified domain to the default.
  182. Save the old allocator into *old_alloc if it's non-NULL.
  183. Return on success, or return -1 if the domain is unknown. */
  184. PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
  185. PyMemAllocatorDomain domain,
  186. PyMemAllocatorEx *old_alloc);
  187. #endif
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif /* !Py_PYMEM_H */
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...