You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
16 KiB

  1. .. highlightlang:: c
  2. .. _memory:
  3. *****************
  4. Memory Management
  5. *****************
  6. .. sectionauthor:: Vladimir Marangozov <Vladimir.Marangozov@inrialpes.fr>
  7. .. _memoryoverview:
  8. Overview
  9. ========
  10. Memory management in Python involves a private heap containing all Python
  11. objects and data structures. The management of this private heap is ensured
  12. internally by the *Python memory manager*. The Python memory manager has
  13. different components which deal with various dynamic storage management aspects,
  14. like sharing, segmentation, preallocation or caching.
  15. At the lowest level, a raw memory allocator ensures that there is enough room in
  16. the private heap for storing all Python-related data by interacting with the
  17. memory manager of the operating system. On top of the raw memory allocator,
  18. several object-specific allocators operate on the same heap and implement
  19. distinct memory management policies adapted to the peculiarities of every object
  20. type. For example, integer objects are managed differently within the heap than
  21. strings, tuples or dictionaries because integers imply different storage
  22. requirements and speed/space tradeoffs. The Python memory manager thus delegates
  23. some of the work to the object-specific allocators, but ensures that the latter
  24. operate within the bounds of the private heap.
  25. It is important to understand that the management of the Python heap is
  26. performed by the interpreter itself and that the user has no control over it,
  27. even if she regularly manipulates object pointers to memory blocks inside that
  28. heap. The allocation of heap space for Python objects and other internal
  29. buffers is performed on demand by the Python memory manager through the Python/C
  30. API functions listed in this document.
  31. .. index::
  32. single: malloc()
  33. single: calloc()
  34. single: realloc()
  35. single: free()
  36. To avoid memory corruption, extension writers should never try to operate on
  37. Python objects with the functions exported by the C library: :c:func:`malloc`,
  38. :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`. This will result in mixed
  39. calls between the C allocator and the Python memory manager with fatal
  40. consequences, because they implement different algorithms and operate on
  41. different heaps. However, one may safely allocate and release memory blocks
  42. with the C library allocator for individual purposes, as shown in the following
  43. example::
  44. PyObject *res;
  45. char *buf = (char *) malloc(BUFSIZ); /* for I/O */
  46. if (buf == NULL)
  47. return PyErr_NoMemory();
  48. ...Do some I/O operation involving buf...
  49. res = PyBytes_FromString(buf);
  50. free(buf); /* malloc'ed */
  51. return res;
  52. In this example, the memory request for the I/O buffer is handled by the C
  53. library allocator. The Python memory manager is involved only in the allocation
  54. of the string object returned as a result.
  55. In most situations, however, it is recommended to allocate memory from the
  56. Python heap specifically because the latter is under control of the Python
  57. memory manager. For example, this is required when the interpreter is extended
  58. with new object types written in C. Another reason for using the Python heap is
  59. the desire to *inform* the Python memory manager about the memory needs of the
  60. extension module. Even when the requested memory is used exclusively for
  61. internal, highly-specific purposes, delegating all memory requests to the Python
  62. memory manager causes the interpreter to have a more accurate image of its
  63. memory footprint as a whole. Consequently, under certain circumstances, the
  64. Python memory manager may or may not trigger appropriate actions, like garbage
  65. collection, memory compaction or other preventive procedures. Note that by using
  66. the C library allocator as shown in the previous example, the allocated memory
  67. for the I/O buffer escapes completely the Python memory manager.
  68. Raw Memory Interface
  69. ====================
  70. The following function sets are wrappers to the system allocator. These
  71. functions are thread-safe, the :term:`GIL <global interpreter lock>` does not
  72. need to be held.
  73. The default raw memory block allocator uses the following functions:
  74. :c:func:`malloc`, :c:func:`realloc` and :c:func:`free`; call ``malloc(1)`` when
  75. requesting zero bytes.
  76. .. versionadded:: 3.4
  77. .. c:function:: void* PyMem_RawMalloc(size_t n)
  78. Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
  79. allocated memory, or *NULL* if the request fails. Requesting zero bytes
  80. returns a distinct non-*NULL* pointer if possible, as if
  81. ``PyMem_RawMalloc(1)`` had been called instead. The memory will not have
  82. been initialized in any way.
  83. .. c:function:: void* PyMem_RawRealloc(void *p, size_t n)
  84. Resizes the memory block pointed to by *p* to *n* bytes. The contents will
  85. be unchanged to the minimum of the old and the new sizes. If *p* is *NULL*,
  86. the call is equivalent to ``PyMem_RawMalloc(n)``; else if *n* is equal to
  87. zero, the memory block is resized but is not freed, and the returned pointer
  88. is non-*NULL*. Unless *p* is *NULL*, it must have been returned by a
  89. previous call to :c:func:`PyMem_RawMalloc` or :c:func:`PyMem_RawRealloc`. If
  90. the request fails, :c:func:`PyMem_RawRealloc` returns *NULL* and *p* remains
  91. a valid pointer to the previous memory area.
  92. .. c:function:: void PyMem_RawFree(void *p)
  93. Frees the memory block pointed to by *p*, which must have been returned by a
  94. previous call to :c:func:`PyMem_RawMalloc` or :c:func:`PyMem_RawRealloc`.
  95. Otherwise, or if ``PyMem_Free(p)`` has been called before, undefined
  96. behavior occurs. If *p* is *NULL*, no operation is performed.
  97. .. _memoryinterface:
  98. Memory Interface
  99. ================
  100. The following function sets, modeled after the ANSI C standard, but specifying
  101. behavior when requesting zero bytes, are available for allocating and releasing
  102. memory from the Python heap.
  103. The default memory block allocator uses the following functions:
  104. :c:func:`malloc`, :c:func:`realloc` and :c:func:`free`; call ``malloc(1)`` when
  105. requesting zero bytes.
  106. .. warning::
  107. The :term:`GIL <global interpreter lock>` must be held when using these
  108. functions.
  109. .. c:function:: void* PyMem_Malloc(size_t n)
  110. Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
  111. allocated memory, or *NULL* if the request fails. Requesting zero bytes returns
  112. a distinct non-*NULL* pointer if possible, as if ``PyMem_Malloc(1)`` had
  113. been called instead. The memory will not have been initialized in any way.
  114. .. c:function:: void* PyMem_Realloc(void *p, size_t n)
  115. Resizes the memory block pointed to by *p* to *n* bytes. The contents will be
  116. unchanged to the minimum of the old and the new sizes. If *p* is *NULL*, the
  117. call is equivalent to ``PyMem_Malloc(n)``; else if *n* is equal to zero,
  118. the memory block is resized but is not freed, and the returned pointer is
  119. non-*NULL*. Unless *p* is *NULL*, it must have been returned by a previous call
  120. to :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. If the request fails,
  121. :c:func:`PyMem_Realloc` returns *NULL* and *p* remains a valid pointer to the
  122. previous memory area.
  123. .. c:function:: void PyMem_Free(void *p)
  124. Frees the memory block pointed to by *p*, which must have been returned by a
  125. previous call to :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. Otherwise, or
  126. if ``PyMem_Free(p)`` has been called before, undefined behavior occurs. If
  127. *p* is *NULL*, no operation is performed.
  128. The following type-oriented macros are provided for convenience. Note that
  129. *TYPE* refers to any C type.
  130. .. c:function:: TYPE* PyMem_New(TYPE, size_t n)
  131. Same as :c:func:`PyMem_Malloc`, but allocates ``(n * sizeof(TYPE))`` bytes of
  132. memory. Returns a pointer cast to :c:type:`TYPE\*`. The memory will not have
  133. been initialized in any way.
  134. .. c:function:: TYPE* PyMem_Resize(void *p, TYPE, size_t n)
  135. Same as :c:func:`PyMem_Realloc`, but the memory block is resized to ``(n *
  136. sizeof(TYPE))`` bytes. Returns a pointer cast to :c:type:`TYPE\*`. On return,
  137. *p* will be a pointer to the new memory area, or *NULL* in the event of
  138. failure. This is a C preprocessor macro; p is always reassigned. Save
  139. the original value of p to avoid losing memory when handling errors.
  140. .. c:function:: void PyMem_Del(void *p)
  141. Same as :c:func:`PyMem_Free`.
  142. In addition, the following macro sets are provided for calling the Python memory
  143. allocator directly, without involving the C API functions listed above. However,
  144. note that their use does not preserve binary compatibility across Python
  145. versions and is therefore deprecated in extension modules.
  146. :c:func:`PyMem_MALLOC`, :c:func:`PyMem_REALLOC`, :c:func:`PyMem_FREE`.
  147. :c:func:`PyMem_NEW`, :c:func:`PyMem_RESIZE`, :c:func:`PyMem_DEL`.
  148. Customize Memory Allocators
  149. ===========================
  150. .. versionadded:: 3.4
  151. .. c:type:: PyMemAllocator
  152. Structure used to describe a memory block allocator. The structure has
  153. four fields:
  154. +----------------------------------------------------------+---------------------------------------+
  155. | Field | Meaning |
  156. +==========================================================+=======================================+
  157. | ``void *ctx`` | user context passed as first argument |
  158. +----------------------------------------------------------+---------------------------------------+
  159. | ``void* malloc(void *ctx, size_t size)`` | allocate a memory block |
  160. +----------------------------------------------------------+---------------------------------------+
  161. | ``void* realloc(void *ctx, void *ptr, size_t new_size)`` | allocate or resize a memory block |
  162. +----------------------------------------------------------+---------------------------------------+
  163. | ``void free(void *ctx, void *ptr)`` | free a memory block |
  164. +----------------------------------------------------------+---------------------------------------+
  165. .. c:type:: PyMemAllocatorDomain
  166. Enum used to identify an allocator domain. Domains:
  167. * :c:data:`PYMEM_DOMAIN_RAW`: functions :c:func:`PyMem_RawMalloc`,
  168. :c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree`
  169. * :c:data:`PYMEM_DOMAIN_MEM`: functions :c:func:`PyMem_Malloc`,
  170. :c:func:`PyMem_Realloc` and :c:func:`PyMem_Free`
  171. * :c:data:`PYMEM_DOMAIN_OBJ`: functions :c:func:`PyObject_Malloc`,
  172. :c:func:`PyObject_Realloc` and :c:func:`PyObject_Free`
  173. .. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
  174. Get the memory block allocator of the specified domain.
  175. .. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
  176. Set the memory block allocator of the specified domain.
  177. The new allocator must return a distinct non-NULL pointer when requesting
  178. zero bytes.
  179. For the :c:data:`PYMEM_DOMAIN_RAW` domain, the allocator must be
  180. thread-safe: the :term:`GIL <global interpreter lock>` is not held when the
  181. allocator is called.
  182. If the new allocator is not a hook (does not call the previous allocator),
  183. the :c:func:`PyMem_SetupDebugHooks` function must be called to reinstall the
  184. debug hooks on top on the new allocator.
  185. .. c:function:: void PyMem_SetupDebugHooks(void)
  186. Setup hooks to detect bugs in the following Python memory allocator
  187. functions:
  188. - :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc`,
  189. :c:func:`PyMem_RawFree`
  190. - :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc`, :c:func:`PyMem_Free`
  191. - :c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc`,
  192. :c:func:`PyObject_Free`
  193. Newly allocated memory is filled with the byte ``0xCB``, freed memory is
  194. filled with the byte ``0xDB``. Additionnal checks:
  195. - detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
  196. allocated by :c:func:`PyMem_Malloc`
  197. - detect write before the start of the buffer (buffer underflow)
  198. - detect write after the end of the buffer (buffer overflow)
  199. The function does nothing if Python is not compiled is debug mode.
  200. Customize PyObject Arena Allocator
  201. ==================================
  202. Python has a *pymalloc* allocator for allocations smaller than 512 bytes. This
  203. allocator is optimized for small objects with a short lifetime. It uses memory
  204. mappings called "arenas" with a fixed size of 256 KB. It falls back to
  205. :c:func:`PyMem_Malloc` and :c:func:`PyMem_Realloc` for allocations larger than
  206. 512 bytes. *pymalloc* is the default allocator used by
  207. :c:func:`PyObject_Malloc`.
  208. The default arena allocator uses the following functions:
  209. * :c:func:`VirtualAlloc` and :c:func:`VirtualFree` on Windows,
  210. * :c:func:`mmap` and :c:func:`munmap` if available,
  211. * :c:func:`malloc` and :c:func:`free` otherwise.
  212. .. versionadded:: 3.4
  213. .. c:type:: PyObjectArenaAllocator
  214. Structure used to describe an arena allocator. The structure has
  215. three fields:
  216. +--------------------------------------------------+---------------------------------------+
  217. | Field | Meaning |
  218. +==================================================+=======================================+
  219. | ``void *ctx`` | user context passed as first argument |
  220. +--------------------------------------------------+---------------------------------------+
  221. | ``void* alloc(void *ctx, size_t size)`` | allocate an arena of size bytes |
  222. +--------------------------------------------------+---------------------------------------+
  223. | ``void free(void *ctx, size_t size, void *ptr)`` | free an arena |
  224. +--------------------------------------------------+---------------------------------------+
  225. .. c:function:: PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)
  226. Get the arena allocator.
  227. .. c:function:: PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator)
  228. Set the arena allocator.
  229. .. _memoryexamples:
  230. Examples
  231. ========
  232. Here is the example from section :ref:`memoryoverview`, rewritten so that the
  233. I/O buffer is allocated from the Python heap by using the first function set::
  234. PyObject *res;
  235. char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */
  236. if (buf == NULL)
  237. return PyErr_NoMemory();
  238. /* ...Do some I/O operation involving buf... */
  239. res = PyBytes_FromString(buf);
  240. PyMem_Free(buf); /* allocated with PyMem_Malloc */
  241. return res;
  242. The same code using the type-oriented function set::
  243. PyObject *res;
  244. char *buf = PyMem_New(char, BUFSIZ); /* for I/O */
  245. if (buf == NULL)
  246. return PyErr_NoMemory();
  247. /* ...Do some I/O operation involving buf... */
  248. res = PyBytes_FromString(buf);
  249. PyMem_Del(buf); /* allocated with PyMem_New */
  250. return res;
  251. Note that in the two examples above, the buffer is always manipulated via
  252. functions belonging to the same set. Indeed, it is required to use the same
  253. memory API family for a given memory block, so that the risk of mixing different
  254. allocators is reduced to a minimum. The following code sequence contains two
  255. errors, one of which is labeled as *fatal* because it mixes two different
  256. allocators operating on different heaps. ::
  257. char *buf1 = PyMem_New(char, BUFSIZ);
  258. char *buf2 = (char *) malloc(BUFSIZ);
  259. char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
  260. ...
  261. PyMem_Del(buf3); /* Wrong -- should be PyMem_Free() */
  262. free(buf2); /* Right -- allocated via malloc() */
  263. free(buf1); /* Fatal -- should be PyMem_Del() */
  264. In addition to the functions aimed at handling raw memory blocks from the Python
  265. heap, objects in Python are allocated and released with :c:func:`PyObject_New`,
  266. :c:func:`PyObject_NewVar` and :c:func:`PyObject_Del`.
  267. These will be explained in the next chapter on defining and implementing new
  268. object types in C.