@@ -83,6 +83,15 @@ collection, memory compaction or other preventive procedures. Note that by using
83
83
the C library allocator as shown in the previous example, the allocated memory
84
84
for the I/O buffer escapes completely the Python memory manager.
85
85
86
+ .. seealso ::
87
+
88
+ The :envvar: `PYTHONMALLOC ` environment variable can be used to configure
89
+ the memory allocators used by Python.
90
+
91
+ The :envvar: `PYTHONMALLOCSTATS ` environment variable can be used to print
92
+ statistics of the :ref: `pymalloc memory allocator <pymalloc >` every time a
93
+ new pymalloc object arena is created, and on shutdown.
94
+
86
95
87
96
Raw Memory Interface
88
97
====================
@@ -100,41 +109,51 @@ The default raw memory block allocator uses the following functions:
100
109
.. c :function :: void * PyMem_RawMalloc (size_t n)
101
110
102
111
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
103
- allocated memory, or *NULL* if the request fails. Requesting zero bytes
104
- returns a distinct non-*NULL* pointer if possible, as if
105
- ``PyMem_RawMalloc(1)`` had been called instead. The memory will not have
112
+ allocated memory, or *NULL* if the request fails.
113
+
114
+ Requesting zero bytes returns a distinct non-*NULL* pointer if possible, as
115
+ if ``PyMem_RawMalloc(1)`` had been called instead. The memory will not have
106
116
been initialized in any way.
107
117
108
118
109
119
.. c:function:: void* PyMem_RawCalloc(size_t nelem, size_t elsize)
110
120
111
121
Allocates *nelem* elements each whose size in bytes is *elsize* and returns
112
122
a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
113
- request fails. The memory is initialized to zeros. Requesting zero elements
114
- or elements of size zero bytes returns a distinct non-*NULL* pointer if
115
- possible, as if ``PyMem_RawCalloc(1, 1)`` had been called instead.
123
+ request fails. The memory is initialized to zeros.
124
+
125
+ Requesting zero elements or elements of size zero bytes returns a distinct
126
+ non-*NULL* pointer if possible, as if ``PyMem_RawCalloc(1, 1)`` had been
127
+ called instead.
116
128
117
129
.. versionadded:: 3.5
118
130
119
131
120
132
.. c:function:: void* PyMem_RawRealloc(void *p, size_t n)
121
133
122
134
Resizes the memory block pointed to by *p * to *n * bytes. The contents will
123
- be unchanged to the minimum of the old and the new sizes. If *p * is *NULL *,
124
- the call is equivalent to ``PyMem_RawMalloc(n) ``; else if *n * is equal to
125
- zero, the memory block is resized but is not freed, and the returned pointer
126
- is non-*NULL *. Unless *p * is *NULL *, it must have been returned by a
127
- previous call to :c:func: `PyMem_RawMalloc ` or :c:func: `PyMem_RawRealloc `. If
128
- the request fails, :c:func: `PyMem_RawRealloc ` returns *NULL * and *p * remains
129
- a valid pointer to the previous memory area.
135
+ be unchanged to the minimum of the old and the new sizes.
136
+
137
+ If *p * is *NULL *, the call is equivalent to ``PyMem_RawMalloc(n) ``; else if
138
+ *n * is equal to zero, the memory block is resized but is not freed, and the
139
+ returned pointer is non-*NULL *.
140
+
141
+ Unless *p * is *NULL *, it must have been returned by a previous call to
142
+ :c:func: `PyMem_RawMalloc `, :c:func: `PyMem_RawRealloc ` or
143
+ :c:func: `PyMem_RawCalloc `.
144
+
145
+ If the request fails, :c:func: `PyMem_RawRealloc ` returns *NULL * and *p *
146
+ remains a valid pointer to the previous memory area.
130
147
131
148
132
149
.. c :function :: void PyMem_RawFree (void *p)
133
150
134
151
Frees the memory block pointed to by *p *, which must have been returned by a
135
- previous call to :c:func: `PyMem_RawMalloc ` or :c:func: `PyMem_RawRealloc `.
136
- Otherwise, or if ``PyMem_Free(p) `` has been called before, undefined
137
- behavior occurs. If *p * is *NULL *, no operation is performed.
152
+ previous call to :c:func: `PyMem_RawMalloc `, :c:func: `PyMem_RawRealloc ` or
153
+ :c:func: `PyMem_RawCalloc `. Otherwise, or if ``PyMem_Free(p) `` has been
154
+ called before, undefined behavior occurs.
155
+
156
+ If *p * is *NULL *, no operation is performed.
138
157
139
158
140
159
.. _memoryinterface :
@@ -158,40 +177,50 @@ The default memory block allocator uses the following functions:
158
177
.. c:function:: void* PyMem_Malloc(size_t n)
159
178
160
179
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
161
- allocated memory, or *NULL* if the request fails. Requesting zero bytes returns
162
- a distinct non-*NULL* pointer if possible, as if ``PyMem_Malloc(1)`` had
163
- been called instead. The memory will not have been initialized in any way.
180
+ allocated memory, or *NULL* if the request fails.
181
+
182
+ Requesting zero bytes returns a distinct non-*NULL* pointer if possible, as
183
+ if ``PyMem_Malloc(1)`` had been called instead. The memory will not have
184
+ been initialized in any way.
164
185
165
186
166
187
.. c:function:: void* PyMem_Calloc(size_t nelem, size_t elsize)
167
188
168
189
Allocates *nelem* elements each whose size in bytes is *elsize* and returns
169
190
a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
170
- request fails. The memory is initialized to zeros. Requesting zero elements
171
- or elements of size zero bytes returns a distinct non-*NULL* pointer if
172
- possible, as if ``PyMem_Calloc(1, 1)`` had been called instead.
191
+ request fails. The memory is initialized to zeros.
192
+
193
+ Requesting zero elements or elements of size zero bytes returns a distinct
194
+ non-*NULL* pointer if possible, as if ``PyMem_Calloc(1, 1)`` had been called
195
+ instead.
173
196
174
197
.. versionadded:: 3.5
175
198
176
199
177
200
.. c:function:: void* PyMem_Realloc(void *p, size_t n)
178
201
179
202
Resizes the memory block pointed to by *p * to *n * bytes. The contents will be
180
- unchanged to the minimum of the old and the new sizes. If *p * is *NULL *, the
181
- call is equivalent to ``PyMem_Malloc(n) ``; else if *n * is equal to zero,
182
- the memory block is resized but is not freed, and the returned pointer is
183
- non-*NULL *. Unless *p * is *NULL *, it must have been returned by a previous call
184
- to :c:func: `PyMem_Malloc ` or :c:func: `PyMem_Realloc `. If the request fails,
185
- :c:func: `PyMem_Realloc ` returns *NULL * and *p * remains a valid pointer to the
186
- previous memory area.
203
+ unchanged to the minimum of the old and the new sizes.
204
+
205
+ If *p * is *NULL *, the call is equivalent to ``PyMem_Malloc(n) ``; else if *n *
206
+ is equal to zero, the memory block is resized but is not freed, and the
207
+ returned pointer is non-*NULL *.
208
+
209
+ Unless *p * is *NULL *, it must have been returned by a previous call to
210
+ :c:func: `PyMem_Malloc `, :c:func: `PyMem_Realloc ` or :c:func: `PyMem_Calloc `.
211
+
212
+ If the request fails, :c:func: `PyMem_Realloc ` returns *NULL * and *p * remains
213
+ a valid pointer to the previous memory area.
187
214
188
215
189
216
.. c :function :: void PyMem_Free (void *p)
190
217
191
218
Frees the memory block pointed to by *p *, which must have been returned by a
192
- previous call to :c:func: `PyMem_Malloc ` or :c:func: `PyMem_Realloc `. Otherwise, or
193
- if ``PyMem_Free(p) `` has been called before, undefined behavior occurs. If
194
- *p * is *NULL *, no operation is performed.
219
+ previous call to :c:func: `PyMem_Malloc `, :c:func: `PyMem_Realloc ` or
220
+ :c:func: `PyMem_Calloc `. Otherwise, or if ``PyMem_Free(p) `` has been called
221
+ before, undefined behavior occurs.
222
+
223
+ If *p * is *NULL *, no operation is performed.
195
224
196
225
The following type-oriented macros are provided for convenience. Note that
197
226
*TYPE * refers to any C type.
@@ -209,8 +238,10 @@ The following type-oriented macros are provided for convenience. Note that
209
238
Same as :c:func: `PyMem_Realloc `, but the memory block is resized to ``(n *
210
239
sizeof(TYPE)) `` bytes. Returns a pointer cast to :c:type: `TYPE\* `. On return,
211
240
*p * will be a pointer to the new memory area, or *NULL * in the event of
212
- failure. This is a C preprocessor macro; p is always reassigned. Save
213
- the original value of p to avoid losing memory when handling errors.
241
+ failure.
242
+
243
+ This is a C preprocessor macro; *p * is always reassigned. Save the original
244
+ value of *p * to avoid losing memory when handling errors.
214
245
215
246
216
247
.. c :function :: void PyMem_Del (void *p)
@@ -222,9 +253,12 @@ allocator directly, without involving the C API functions listed above. However,
222
253
note that their use does not preserve binary compatibility across Python
223
254
versions and is therefore deprecated in extension modules.
224
255
225
- :c:func: `PyMem_MALLOC `, :c:func: `PyMem_REALLOC `, :c:func: `PyMem_FREE `.
226
-
227
- :c:func: `PyMem_NEW `, :c:func: `PyMem_RESIZE `, :c:func: `PyMem_DEL `.
256
+ * ``PyMem_MALLOC(size) ``
257
+ * ``PyMem_NEW(type, size) ``
258
+ * ``PyMem_REALLOC(ptr, size) ``
259
+ * ``PyMem_RESIZE(ptr, type, size) ``
260
+ * ``PyMem_FREE(ptr) ``
261
+ * ``PyMem_DEL(ptr) ``
228
262
229
263
230
264
Customize Memory Allocators
@@ -262,11 +296,13 @@ Customize Memory Allocators
262
296
Enum used to identify an allocator domain. Domains:
263
297
264
298
* :c:data: `PYMEM_DOMAIN_RAW `: functions :c:func: `PyMem_RawMalloc `,
265
- :c:func: `PyMem_RawRealloc ` and :c:func: `PyMem_RawFree `
299
+ :c:func: `PyMem_RawRealloc `, :c:func: `PyMem_RawCalloc ` and
300
+ :c:func: `PyMem_RawFree `
266
301
* :c:data: `PYMEM_DOMAIN_MEM `: functions :c:func: `PyMem_Malloc `,
267
- :c:func: `PyMem_Realloc ` and :c:func: `PyMem_Free `
302
+ :c:func: `PyMem_Realloc `, :c:func: ` PyMem_Calloc ` and :c:func: `PyMem_Free `
268
303
* :c:data: `PYMEM_DOMAIN_OBJ `: functions :c:func: `PyObject_Malloc `,
269
- :c:func: `PyObject_Realloc ` and :c:func: `PyObject_Free `
304
+ :c:func: `PyObject_Realloc `, :c:func: `PyObject_Calloc ` and
305
+ :c:func: `PyObject_Free `
270
306
271
307
272
308
.. c :function :: void PyMem_GetAllocator (PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
@@ -296,38 +332,63 @@ Customize Memory Allocators
296
332
functions:
297
333
298
334
- :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc`,
299
- :c:func:`PyMem_RawFree`
300
- - :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc`, :c:func:`PyMem_Free`
335
+ :c:func:`PyMem_RawCalloc`, :c:func:`PyMem_RawFree`
336
+ - :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc`, :c:func:`PyMem_Calloc`,
337
+ :c:func:`PyMem_Free`
301
338
- :c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc`,
302
- :c:func:`PyObject_Free`
339
+ :c:func:`PyObject_Calloc`, :c:func:` PyObject_Free`
303
340
304
341
Newly allocated memory is filled with the byte ``0xCB``, freed memory is
305
342
filled with the byte ``0xDB``. Additional checks:
306
343
307
- - detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
344
+ - Detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
308
345
allocated by :c:func:`PyMem_Malloc`
309
- - detect write before the start of the buffer (buffer underflow)
310
- - detect write after the end of the buffer (buffer overflow)
346
+ - Detect write before the start of the buffer (buffer underflow)
347
+ - Detect write after the end of the buffer (buffer overflow)
348
+ - Check that the :term:`GIL <global interpreter lock>` is held when
349
+ allocator functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex:
350
+ :c:func: `PyObject_Malloc `) and :c:data:`PYMEM_DOMAIN_MEM` (ex:
351
+ :c:func: `PyMem_Malloc `) domains are called
352
+
353
+ On error, the debug hooks use the :mod:`tracemalloc` module to get the
354
+ traceback where a memory block was allocated. The traceback is only
355
+ displayed if :mod:`tracemalloc` is tracing Python memory allocations and the
356
+ memory block was traced.
357
+
358
+ These hooks are installed by default if Python is compiled in debug
359
+ mode. The :envvar:`PYTHONMALLOC` environment variable can be used to install
360
+ debug hooks on a Python compiled in release mode.
311
361
312
- The function does nothing if Python is not compiled is debug mode.
362
+ .. versionchanged:: 3.6
363
+ This function now also works on Python compiled in release mode.
364
+ On error, the debug hooks now use :mod:`tracemalloc` to get the traceback
365
+ where a memory block was allocated. The debug hooks now also check
366
+ if the GIL is held when functions of :c:data:`PYMEM_DOMAIN_OBJ` and
367
+ :c:data:`PYMEM_DOMAIN_MEM` domains are called.
313
368
314
369
315
- Customize PyObject Arena Allocator
316
- ==================================
370
+ .. _pymalloc:
317
371
318
- Python has a *pymalloc* allocator for allocations smaller than 512 bytes. This
319
- allocator is optimized for small objects with a short lifetime. It uses memory
320
- mappings called "arenas" with a fixed size of 256 KB. It falls back to
321
- :c:func:`PyMem_RawMalloc` and :c:func:`PyMem_RawRealloc` for allocations larger
322
- than 512 bytes. *pymalloc* is the default allocator used by
323
- :c:func:`PyObject_Malloc`.
372
+ The pymalloc allocator
373
+ ======================
324
374
325
- The default arena allocator uses the following functions:
375
+ Python has a *pymalloc* allocator optimized for small objects (smaller or equal
376
+ to 512 bytes) with a short lifetime. It uses memory mappings called "arenas"
377
+ with a fixed size of 256 KB. It falls back to :c:func:`PyMem_RawMalloc` and
378
+ :c:func:`PyMem_RawRealloc` for allocations larger than 512 bytes.
379
+
380
+ *pymalloc* is the default allocator of the :c:data:`PYMEM_DOMAIN_OBJ` domain
381
+ (ex: :c:func: `PyObject_Malloc `).
382
+
383
+ The arena allocator uses the following functions:
326
384
327
385
* :c:func:`VirtualAlloc` and :c:func:`VirtualFree` on Windows,
328
386
* :c:func:`mmap` and :c:func:`munmap` if available,
329
387
* :c:func:`malloc` and :c:func:`free` otherwise.
330
388
389
+ Customize pymalloc Arena Allocator
390
+ ----------------------------------
391
+
331
392
.. versionadded:: 3.4
332
393
333
394
.. c:type:: PyObjectArenaAllocator
0 commit comments