gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1094 lines
98KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Memory Management" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/memory.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Overview: Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manag..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Overview: Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manag..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Memory Management &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Object Implementation Support" href="objimpl.html" />
  33. <link rel="prev" title="Python Initialization Configuration" href="init_config.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/c-api/memory.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Memory Management</a><ul>
  86. <li><a class="reference internal" href="#overview">Overview</a></li>
  87. <li><a class="reference internal" href="#allocator-domains">Allocator Domains</a></li>
  88. <li><a class="reference internal" href="#raw-memory-interface">Raw Memory Interface</a></li>
  89. <li><a class="reference internal" href="#memory-interface">Memory Interface</a></li>
  90. <li><a class="reference internal" href="#object-allocators">Object allocators</a></li>
  91. <li><a class="reference internal" href="#default-memory-allocators">Default Memory Allocators</a></li>
  92. <li><a class="reference internal" href="#customize-memory-allocators">Customize Memory Allocators</a></li>
  93. <li><a class="reference internal" href="#debug-hooks-on-the-python-memory-allocators">Debug hooks on the Python memory allocators</a></li>
  94. <li><a class="reference internal" href="#the-pymalloc-allocator">The pymalloc allocator</a><ul>
  95. <li><a class="reference internal" href="#customize-pymalloc-arena-allocator">Customize pymalloc Arena Allocator</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#tracemalloc-c-api">tracemalloc C API</a></li>
  99. <li><a class="reference internal" href="#examples">Examples</a></li>
  100. </ul>
  101. </li>
  102. </ul>
  103. </div>
  104. <div>
  105. <h4>Previous topic</h4>
  106. <p class="topless"><a href="init_config.html"
  107. title="previous chapter">Python Initialization Configuration</a></p>
  108. </div>
  109. <div>
  110. <h4>Next topic</h4>
  111. <p class="topless"><a href="objimpl.html"
  112. title="next chapter">Object Implementation Support</a></p>
  113. </div>
  114. <div role="note" aria-label="source link">
  115. <h3>This Page</h3>
  116. <ul class="this-page-menu">
  117. <li><a href="../bugs.html">Report a Bug</a></li>
  118. <li>
  119. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/memory.rst"
  120. rel="nofollow">Show Source
  121. </a>
  122. </li>
  123. </ul>
  124. </div>
  125. </nav>
  126. </div>
  127. </div>
  128. <div class="related" role="navigation" aria-label="related navigation">
  129. <h3>Navigation</h3>
  130. <ul>
  131. <li class="right" style="margin-right: 10px">
  132. <a href="../genindex.html" title="General Index"
  133. accesskey="I">index</a></li>
  134. <li class="right" >
  135. <a href="../py-modindex.html" title="Python Module Index"
  136. >modules</a> |</li>
  137. <li class="right" >
  138. <a href="objimpl.html" title="Object Implementation Support"
  139. accesskey="N">next</a> |</li>
  140. <li class="right" >
  141. <a href="init_config.html" title="Python Initialization Configuration"
  142. accesskey="P">previous</a> |</li>
  143. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  144. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  145. <li class="switchers">
  146. <div class="language_switcher_placeholder"></div>
  147. <div class="version_switcher_placeholder"></div>
  148. </li>
  149. <li>
  150. </li>
  151. <li id="cpython-language-and-version">
  152. <a href="../index.html">3.12.3 Documentation</a> &#187;
  153. </li>
  154. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python/C API Reference Manual</a> &#187;</li>
  155. <li class="nav-item nav-item-this"><a href="">Memory Management</a></li>
  156. <li class="right">
  157. <div class="inline-search" role="search">
  158. <form class="inline-search" action="../search.html" method="get">
  159. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  160. <input type="submit" value="Go" />
  161. </form>
  162. </div>
  163. |
  164. </li>
  165. <li class="right">
  166. <label class="theme-selector-label">
  167. Theme
  168. <select class="theme-selector" oninput="activateTheme(this.value)">
  169. <option value="auto" selected>Auto</option>
  170. <option value="light">Light</option>
  171. <option value="dark">Dark</option>
  172. </select>
  173. </label> |</li>
  174. </ul>
  175. </div>
  176. <div class="document">
  177. <div class="documentwrapper">
  178. <div class="bodywrapper">
  179. <div class="body" role="main">
  180. <section id="memory-management">
  181. <span id="memory"></span><h1>Memory Management<a class="headerlink" href="#memory-management" title="Link to this heading">¶</a></h1>
  182. <section id="overview">
  183. <span id="memoryoverview"></span><h2>Overview<a class="headerlink" href="#overview" title="Link to this heading">¶</a></h2>
  184. <p>Memory management in Python involves a private heap containing all Python
  185. objects and data structures. The management of this private heap is ensured
  186. internally by the <em>Python memory manager</em>. The Python memory manager has
  187. different components which deal with various dynamic storage management aspects,
  188. like sharing, segmentation, preallocation or caching.</p>
  189. <p>At the lowest level, a raw memory allocator ensures that there is enough room in
  190. the private heap for storing all Python-related data by interacting with the
  191. memory manager of the operating system. On top of the raw memory allocator,
  192. several object-specific allocators operate on the same heap and implement
  193. distinct memory management policies adapted to the peculiarities of every object
  194. type. For example, integer objects are managed differently within the heap than
  195. strings, tuples or dictionaries because integers imply different storage
  196. requirements and speed/space tradeoffs. The Python memory manager thus delegates
  197. some of the work to the object-specific allocators, but ensures that the latter
  198. operate within the bounds of the private heap.</p>
  199. <p>It is important to understand that the management of the Python heap is
  200. performed by the interpreter itself and that the user has no control over it,
  201. even if they regularly manipulate object pointers to memory blocks inside that
  202. heap. The allocation of heap space for Python objects and other internal
  203. buffers is performed on demand by the Python memory manager through the Python/C
  204. API functions listed in this document.</p>
  205. <p id="index-0">To avoid memory corruption, extension writers should never try to operate on
  206. Python objects with the functions exported by the C library: <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code>,
  207. <code class="xref c c-func docutils literal notranslate"><span class="pre">calloc()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">realloc()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code>. This will result in mixed
  208. calls between the C allocator and the Python memory manager with fatal
  209. consequences, because they implement different algorithms and operate on
  210. different heaps. However, one may safely allocate and release memory blocks
  211. with the C library allocator for individual purposes, as shown in the following
  212. example:</p>
  213. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">res</span><span class="p">;</span>
  214. <span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">BUFSIZ</span><span class="p">);</span><span class="w"> </span><span class="cm">/* for I/O */</span>
  215. <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">buf</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
  216. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">PyErr_NoMemory</span><span class="p">();</span>
  217. <span class="p">...</span><span class="n">Do</span><span class="w"> </span><span class="n">some</span><span class="w"> </span><span class="n">I</span><span class="o">/</span><span class="n">O</span><span class="w"> </span><span class="n">operation</span><span class="w"> </span><span class="n">involving</span><span class="w"> </span><span class="n">buf</span><span class="p">...</span>
  218. <span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyBytes_FromString</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
  219. <span class="n">free</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span><span class="w"> </span><span class="cm">/* malloc&#39;ed */</span>
  220. <span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
  221. </pre></div>
  222. </div>
  223. <p>In this example, the memory request for the I/O buffer is handled by the C
  224. library allocator. The Python memory manager is involved only in the allocation
  225. of the bytes object returned as a result.</p>
  226. <p>In most situations, however, it is recommended to allocate memory from the
  227. Python heap specifically because the latter is under control of the Python
  228. memory manager. For example, this is required when the interpreter is extended
  229. with new object types written in C. Another reason for using the Python heap is
  230. the desire to <em>inform</em> the Python memory manager about the memory needs of the
  231. extension module. Even when the requested memory is used exclusively for
  232. internal, highly specific purposes, delegating all memory requests to the Python
  233. memory manager causes the interpreter to have a more accurate image of its
  234. memory footprint as a whole. Consequently, under certain circumstances, the
  235. Python memory manager may or may not trigger appropriate actions, like garbage
  236. collection, memory compaction or other preventive procedures. Note that by using
  237. the C library allocator as shown in the previous example, the allocated memory
  238. for the I/O buffer escapes completely the Python memory manager.</p>
  239. <div class="admonition seealso">
  240. <p class="admonition-title">See also</p>
  241. <p>The <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code></a> environment variable can be used to configure
  242. the memory allocators used by Python.</p>
  243. <p>The <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONMALLOCSTATS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONMALLOCSTATS</span></code></a> environment variable can be used to print
  244. statistics of the <a class="reference internal" href="#pymalloc"><span class="std std-ref">pymalloc memory allocator</span></a> every time a
  245. new pymalloc object arena is created, and on shutdown.</p>
  246. </div>
  247. </section>
  248. <section id="allocator-domains">
  249. <h2>Allocator Domains<a class="headerlink" href="#allocator-domains" title="Link to this heading">¶</a></h2>
  250. <p id="id1">All allocating functions belong to one of three different “domains” (see also
  251. <a class="reference internal" href="#c.PyMemAllocatorDomain" title="PyMemAllocatorDomain"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemAllocatorDomain</span></code></a>). These domains represent different allocation
  252. strategies and are optimized for different purposes. The specific details on
  253. how every domain allocates memory or what internal functions each domain calls
  254. is considered an implementation detail, but for debugging purposes a simplified
  255. table can be found at <a class="reference internal" href="#default-memory-allocators"><span class="std std-ref">here</span></a>. There is no hard
  256. requirement to use the memory returned by the allocation functions belonging to
  257. a given domain for only the purposes hinted by that domain (although this is the
  258. recommended practice). For example, one could use the memory returned by
  259. <a class="reference internal" href="#c.PyMem_RawMalloc" title="PyMem_RawMalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code></a> for allocating Python objects or the memory returned
  260. by <a class="reference internal" href="#c.PyObject_Malloc" title="PyObject_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Malloc()</span></code></a> for allocating memory for buffers.</p>
  261. <p>The three allocation domains are:</p>
  262. <ul class="simple">
  263. <li><p>Raw domain: intended for allocating memory for general-purpose memory
  264. buffers where the allocation <em>must</em> go to the system allocator or where the
  265. allocator can operate without the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a>. The memory is requested directly
  266. to the system.</p></li>
  267. <li><p>“Mem” domain: intended for allocating memory for Python buffers and
  268. general-purpose memory buffers where the allocation must be performed with
  269. the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> held. The memory is taken from the Python private heap.</p></li>
  270. <li><p>Object domain: intended for allocating memory belonging to Python objects. The
  271. memory is taken from the Python private heap.</p></li>
  272. </ul>
  273. <p>When freeing memory previously allocated by the allocating functions belonging to a
  274. given domain,the matching specific deallocating functions must be used. For example,
  275. <a class="reference internal" href="#c.PyMem_Free" title="PyMem_Free"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Free()</span></code></a> must be used to free memory allocated using <a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>.</p>
  276. </section>
  277. <section id="raw-memory-interface">
  278. <h2>Raw Memory Interface<a class="headerlink" href="#raw-memory-interface" title="Link to this heading">¶</a></h2>
  279. <p>The following function sets are wrappers to the system allocator. These
  280. functions are thread-safe, the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">GIL</span></a> does not
  281. need to be held.</p>
  282. <p>The <a class="reference internal" href="#default-memory-allocators"><span class="std std-ref">default raw memory allocator</span></a> uses
  283. the following functions: <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">calloc()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">realloc()</span></code>
  284. and <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code>; call <code class="docutils literal notranslate"><span class="pre">malloc(1)</span></code> (or <code class="docutils literal notranslate"><span class="pre">calloc(1,</span> <span class="pre">1)</span></code>) when requesting
  285. zero bytes.</p>
  286. <div class="versionadded">
  287. <p><span class="versionmodified added">New in version 3.4.</span></p>
  288. </div>
  289. <dl class="c function">
  290. <dt class="sig sig-object c" id="c.PyMem_RawMalloc">
  291. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_RawMalloc</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_RawMalloc" title="Link to this definition">¶</a><br /></dt>
  292. <dd><p>Allocates <em>n</em> bytes and returns a pointer of type <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> to the
  293. allocated memory, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the request fails.</p>
  294. <p>Requesting zero bytes returns a distinct non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer if possible, as
  295. if <code class="docutils literal notranslate"><span class="pre">PyMem_RawMalloc(1)</span></code> had been called instead. The memory will not have
  296. been initialized in any way.</p>
  297. </dd></dl>
  298. <dl class="c function">
  299. <dt class="sig sig-object c" id="c.PyMem_RawCalloc">
  300. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_RawCalloc</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nelem</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">elsize</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_RawCalloc" title="Link to this definition">¶</a><br /></dt>
  301. <dd><p>Allocates <em>nelem</em> elements each whose size in bytes is <em>elsize</em> and returns
  302. a pointer of type <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> to the allocated memory, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the
  303. request fails. The memory is initialized to zeros.</p>
  304. <p>Requesting zero elements or elements of size zero bytes returns a distinct
  305. non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer if possible, as if <code class="docutils literal notranslate"><span class="pre">PyMem_RawCalloc(1,</span> <span class="pre">1)</span></code> had been
  306. called instead.</p>
  307. <div class="versionadded">
  308. <p><span class="versionmodified added">New in version 3.5.</span></p>
  309. </div>
  310. </dd></dl>
  311. <dl class="c function">
  312. <dt class="sig sig-object c" id="c.PyMem_RawRealloc">
  313. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_RawRealloc</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_RawRealloc" title="Link to this definition">¶</a><br /></dt>
  314. <dd><p>Resizes the memory block pointed to by <em>p</em> to <em>n</em> bytes. The contents will
  315. be unchanged to the minimum of the old and the new sizes.</p>
  316. <p>If <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the call is equivalent to <code class="docutils literal notranslate"><span class="pre">PyMem_RawMalloc(n)</span></code>; else if
  317. <em>n</em> is equal to zero, the memory block is resized but is not freed, and the
  318. returned pointer is non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  319. <p>Unless <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, it must have been returned by a previous call to
  320. <a class="reference internal" href="#c.PyMem_RawMalloc" title="PyMem_RawMalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code></a>, <a class="reference internal" href="#c.PyMem_RawRealloc" title="PyMem_RawRealloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawRealloc()</span></code></a> or
  321. <a class="reference internal" href="#c.PyMem_RawCalloc" title="PyMem_RawCalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawCalloc()</span></code></a>.</p>
  322. <p>If the request fails, <a class="reference internal" href="#c.PyMem_RawRealloc" title="PyMem_RawRealloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawRealloc()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and <em>p</em>
  323. remains a valid pointer to the previous memory area.</p>
  324. </dd></dl>
  325. <dl class="c function">
  326. <dt class="sig sig-object c" id="c.PyMem_RawFree">
  327. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_RawFree</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_RawFree" title="Link to this definition">¶</a><br /></dt>
  328. <dd><p>Frees the memory block pointed to by <em>p</em>, which must have been returned by a
  329. previous call to <a class="reference internal" href="#c.PyMem_RawMalloc" title="PyMem_RawMalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code></a>, <a class="reference internal" href="#c.PyMem_RawRealloc" title="PyMem_RawRealloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawRealloc()</span></code></a> or
  330. <a class="reference internal" href="#c.PyMem_RawCalloc" title="PyMem_RawCalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawCalloc()</span></code></a>. Otherwise, or if <code class="docutils literal notranslate"><span class="pre">PyMem_RawFree(p)</span></code> has been
  331. called before, undefined behavior occurs.</p>
  332. <p>If <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, no operation is performed.</p>
  333. </dd></dl>
  334. </section>
  335. <section id="memory-interface">
  336. <span id="memoryinterface"></span><h2>Memory Interface<a class="headerlink" href="#memory-interface" title="Link to this heading">¶</a></h2>
  337. <p>The following function sets, modeled after the ANSI C standard, but specifying
  338. behavior when requesting zero bytes, are available for allocating and releasing
  339. memory from the Python heap.</p>
  340. <p>The <a class="reference internal" href="#default-memory-allocators"><span class="std std-ref">default memory allocator</span></a> uses the
  341. <a class="reference internal" href="#pymalloc"><span class="std std-ref">pymalloc memory allocator</span></a>.</p>
  342. <div class="admonition warning">
  343. <p class="admonition-title">Warning</p>
  344. <p>The <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">GIL</span></a> must be held when using these
  345. functions.</p>
  346. </div>
  347. <div class="versionchanged">
  348. <p><span class="versionmodified changed">Changed in version 3.6: </span>The default allocator is now pymalloc instead of system <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code>.</p>
  349. </div>
  350. <dl class="c function">
  351. <dt class="sig sig-object c" id="c.PyMem_Malloc">
  352. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_Malloc</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_Malloc" title="Link to this definition">¶</a><br /></dt>
  353. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Allocates <em>n</em> bytes and returns a pointer of type <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> to the
  354. allocated memory, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the request fails.</p>
  355. <p>Requesting zero bytes returns a distinct non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer if possible, as
  356. if <code class="docutils literal notranslate"><span class="pre">PyMem_Malloc(1)</span></code> had been called instead. The memory will not have
  357. been initialized in any way.</p>
  358. </dd></dl>
  359. <dl class="c function">
  360. <dt class="sig sig-object c" id="c.PyMem_Calloc">
  361. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_Calloc</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nelem</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">elsize</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_Calloc" title="Link to this definition">¶</a><br /></dt>
  362. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Allocates <em>nelem</em> elements each whose size in bytes is <em>elsize</em> and returns
  363. a pointer of type <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> to the allocated memory, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the
  364. request fails. The memory is initialized to zeros.</p>
  365. <p>Requesting zero elements or elements of size zero bytes returns a distinct
  366. non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer if possible, as if <code class="docutils literal notranslate"><span class="pre">PyMem_Calloc(1,</span> <span class="pre">1)</span></code> had been called
  367. instead.</p>
  368. <div class="versionadded">
  369. <p><span class="versionmodified added">New in version 3.5.</span></p>
  370. </div>
  371. </dd></dl>
  372. <dl class="c function">
  373. <dt class="sig sig-object c" id="c.PyMem_Realloc">
  374. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_Realloc</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_Realloc" title="Link to this definition">¶</a><br /></dt>
  375. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Resizes the memory block pointed to by <em>p</em> to <em>n</em> bytes. The contents will be
  376. unchanged to the minimum of the old and the new sizes.</p>
  377. <p>If <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the call is equivalent to <code class="docutils literal notranslate"><span class="pre">PyMem_Malloc(n)</span></code>; else if <em>n</em>
  378. is equal to zero, the memory block is resized but is not freed, and the
  379. returned pointer is non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  380. <p>Unless <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, it must have been returned by a previous call to
  381. <a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>, <a class="reference internal" href="#c.PyMem_Realloc" title="PyMem_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Realloc()</span></code></a> or <a class="reference internal" href="#c.PyMem_Calloc" title="PyMem_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Calloc()</span></code></a>.</p>
  382. <p>If the request fails, <a class="reference internal" href="#c.PyMem_Realloc" title="PyMem_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Realloc()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and <em>p</em> remains
  383. a valid pointer to the previous memory area.</p>
  384. </dd></dl>
  385. <dl class="c function">
  386. <dt class="sig sig-object c" id="c.PyMem_Free">
  387. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_Free</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_Free" title="Link to this definition">¶</a><br /></dt>
  388. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Frees the memory block pointed to by <em>p</em>, which must have been returned by a
  389. previous call to <a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>, <a class="reference internal" href="#c.PyMem_Realloc" title="PyMem_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Realloc()</span></code></a> or
  390. <a class="reference internal" href="#c.PyMem_Calloc" title="PyMem_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Calloc()</span></code></a>. Otherwise, or if <code class="docutils literal notranslate"><span class="pre">PyMem_Free(p)</span></code> has been called
  391. before, undefined behavior occurs.</p>
  392. <p>If <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, no operation is performed.</p>
  393. </dd></dl>
  394. <p>The following type-oriented macros are provided for convenience. Note that
  395. <em>TYPE</em> refers to any C type.</p>
  396. <dl class="c macro">
  397. <dt class="sig sig-object c" id="c.PyMem_New">
  398. <span class="sig-name descname"><span class="n"><span class="pre">PyMem_New</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">TYPE</span></span>, <span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_New" title="Link to this definition">¶</a><br /></dt>
  399. <dd><p>Same as <a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>, but allocates <code class="docutils literal notranslate"><span class="pre">(n</span> <span class="pre">*</span> <span class="pre">sizeof(TYPE))</span></code> bytes of
  400. memory. Returns a pointer cast to <code class="docutils literal notranslate"><span class="pre">TYPE*</span></code>. The memory will not have
  401. been initialized in any way.</p>
  402. </dd></dl>
  403. <dl class="c macro">
  404. <dt class="sig sig-object c" id="c.PyMem_Resize">
  405. <span class="sig-name descname"><span class="n"><span class="pre">PyMem_Resize</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">p</span></span>, <span class="n"><span class="pre">TYPE</span></span>, <span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_Resize" title="Link to this definition">¶</a><br /></dt>
  406. <dd><p>Same as <a class="reference internal" href="#c.PyMem_Realloc" title="PyMem_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Realloc()</span></code></a>, but the memory block is resized to <code class="docutils literal notranslate"><span class="pre">(n</span> <span class="pre">*</span>
  407. <span class="pre">sizeof(TYPE))</span></code> bytes. Returns a pointer cast to <code class="docutils literal notranslate"><span class="pre">TYPE*</span></code>. On return,
  408. <em>p</em> will be a pointer to the new memory area, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> in the event of
  409. failure.</p>
  410. <p>This is a C preprocessor macro; <em>p</em> is always reassigned. Save the original
  411. value of <em>p</em> to avoid losing memory when handling errors.</p>
  412. </dd></dl>
  413. <dl class="c function">
  414. <dt class="sig sig-object c" id="c.PyMem_Del">
  415. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_Del</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_Del" title="Link to this definition">¶</a><br /></dt>
  416. <dd><p>Same as <a class="reference internal" href="#c.PyMem_Free" title="PyMem_Free"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Free()</span></code></a>.</p>
  417. </dd></dl>
  418. <p>In addition, the following macro sets are provided for calling the Python memory
  419. allocator directly, without involving the C API functions listed above. However,
  420. note that their use does not preserve binary compatibility across Python
  421. versions and is therefore deprecated in extension modules.</p>
  422. <ul class="simple">
  423. <li><p><code class="docutils literal notranslate"><span class="pre">PyMem_MALLOC(size)</span></code></p></li>
  424. <li><p><code class="docutils literal notranslate"><span class="pre">PyMem_NEW(type,</span> <span class="pre">size)</span></code></p></li>
  425. <li><p><code class="docutils literal notranslate"><span class="pre">PyMem_REALLOC(ptr,</span> <span class="pre">size)</span></code></p></li>
  426. <li><p><code class="docutils literal notranslate"><span class="pre">PyMem_RESIZE(ptr,</span> <span class="pre">type,</span> <span class="pre">size)</span></code></p></li>
  427. <li><p><code class="docutils literal notranslate"><span class="pre">PyMem_FREE(ptr)</span></code></p></li>
  428. <li><p><code class="docutils literal notranslate"><span class="pre">PyMem_DEL(ptr)</span></code></p></li>
  429. </ul>
  430. </section>
  431. <section id="object-allocators">
  432. <h2>Object allocators<a class="headerlink" href="#object-allocators" title="Link to this heading">¶</a></h2>
  433. <p>The following function sets, modeled after the ANSI C standard, but specifying
  434. behavior when requesting zero bytes, are available for allocating and releasing
  435. memory from the Python heap.</p>
  436. <div class="admonition note">
  437. <p class="admonition-title">Note</p>
  438. <p>There is no guarantee that the memory returned by these allocators can be
  439. successfully cast to a Python object when intercepting the allocating
  440. functions in this domain by the methods described in
  441. the <a class="reference internal" href="#customize-memory-allocators"><span class="std std-ref">Customize Memory Allocators</span></a> section.</p>
  442. </div>
  443. <p>The <a class="reference internal" href="#default-memory-allocators"><span class="std std-ref">default object allocator</span></a> uses the
  444. <a class="reference internal" href="#pymalloc"><span class="std std-ref">pymalloc memory allocator</span></a>.</p>
  445. <div class="admonition warning">
  446. <p class="admonition-title">Warning</p>
  447. <p>The <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">GIL</span></a> must be held when using these
  448. functions.</p>
  449. </div>
  450. <dl class="c function">
  451. <dt class="sig sig-object c" id="c.PyObject_Malloc">
  452. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Malloc</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Malloc" title="Link to this definition">¶</a><br /></dt>
  453. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Allocates <em>n</em> bytes and returns a pointer of type <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> to the
  454. allocated memory, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the request fails.</p>
  455. <p>Requesting zero bytes returns a distinct non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer if possible, as
  456. if <code class="docutils literal notranslate"><span class="pre">PyObject_Malloc(1)</span></code> had been called instead. The memory will not have
  457. been initialized in any way.</p>
  458. </dd></dl>
  459. <dl class="c function">
  460. <dt class="sig sig-object c" id="c.PyObject_Calloc">
  461. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Calloc</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nelem</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">elsize</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Calloc" title="Link to this definition">¶</a><br /></dt>
  462. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Allocates <em>nelem</em> elements each whose size in bytes is <em>elsize</em> and returns
  463. a pointer of type <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> to the allocated memory, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if the
  464. request fails. The memory is initialized to zeros.</p>
  465. <p>Requesting zero elements or elements of size zero bytes returns a distinct
  466. non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer if possible, as if <code class="docutils literal notranslate"><span class="pre">PyObject_Calloc(1,</span> <span class="pre">1)</span></code> had been called
  467. instead.</p>
  468. <div class="versionadded">
  469. <p><span class="versionmodified added">New in version 3.5.</span></p>
  470. </div>
  471. </dd></dl>
  472. <dl class="c function">
  473. <dt class="sig sig-object c" id="c.PyObject_Realloc">
  474. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Realloc</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Realloc" title="Link to this definition">¶</a><br /></dt>
  475. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Resizes the memory block pointed to by <em>p</em> to <em>n</em> bytes. The contents will be
  476. unchanged to the minimum of the old and the new sizes.</p>
  477. <p>If <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the call is equivalent to <code class="docutils literal notranslate"><span class="pre">PyObject_Malloc(n)</span></code>; else if <em>n</em>
  478. is equal to zero, the memory block is resized but is not freed, and the
  479. returned pointer is non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  480. <p>Unless <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, it must have been returned by a previous call to
  481. <a class="reference internal" href="#c.PyObject_Malloc" title="PyObject_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Malloc()</span></code></a>, <a class="reference internal" href="#c.PyObject_Realloc" title="PyObject_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Realloc()</span></code></a> or <a class="reference internal" href="#c.PyObject_Calloc" title="PyObject_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Calloc()</span></code></a>.</p>
  482. <p>If the request fails, <a class="reference internal" href="#c.PyObject_Realloc" title="PyObject_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Realloc()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and <em>p</em> remains
  483. a valid pointer to the previous memory area.</p>
  484. </dd></dl>
  485. <dl class="c function">
  486. <dt class="sig sig-object c" id="c.PyObject_Free">
  487. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Free</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Free" title="Link to this definition">¶</a><br /></dt>
  488. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Frees the memory block pointed to by <em>p</em>, which must have been returned by a
  489. previous call to <a class="reference internal" href="#c.PyObject_Malloc" title="PyObject_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Malloc()</span></code></a>, <a class="reference internal" href="#c.PyObject_Realloc" title="PyObject_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Realloc()</span></code></a> or
  490. <a class="reference internal" href="#c.PyObject_Calloc" title="PyObject_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Calloc()</span></code></a>. Otherwise, or if <code class="docutils literal notranslate"><span class="pre">PyObject_Free(p)</span></code> has been called
  491. before, undefined behavior occurs.</p>
  492. <p>If <em>p</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, no operation is performed.</p>
  493. </dd></dl>
  494. </section>
  495. <section id="default-memory-allocators">
  496. <span id="id2"></span><h2>Default Memory Allocators<a class="headerlink" href="#default-memory-allocators" title="Link to this heading">¶</a></h2>
  497. <p>Default memory allocators:</p>
  498. <table class="docutils align-default">
  499. <thead>
  500. <tr class="row-odd"><th class="head"><p>Configuration</p></th>
  501. <th class="head"><p>Name</p></th>
  502. <th class="head"><p>PyMem_RawMalloc</p></th>
  503. <th class="head"><p>PyMem_Malloc</p></th>
  504. <th class="head"><p>PyObject_Malloc</p></th>
  505. </tr>
  506. </thead>
  507. <tbody>
  508. <tr class="row-even"><td><p>Release build</p></td>
  509. <td><p><code class="docutils literal notranslate"><span class="pre">&quot;pymalloc&quot;</span></code></p></td>
  510. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code></p></td>
  511. <td><p><code class="docutils literal notranslate"><span class="pre">pymalloc</span></code></p></td>
  512. <td><p><code class="docutils literal notranslate"><span class="pre">pymalloc</span></code></p></td>
  513. </tr>
  514. <tr class="row-odd"><td><p>Debug build</p></td>
  515. <td><p><code class="docutils literal notranslate"><span class="pre">&quot;pymalloc_debug&quot;</span></code></p></td>
  516. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code> + debug</p></td>
  517. <td><p><code class="docutils literal notranslate"><span class="pre">pymalloc</span></code> + debug</p></td>
  518. <td><p><code class="docutils literal notranslate"><span class="pre">pymalloc</span></code> + debug</p></td>
  519. </tr>
  520. <tr class="row-even"><td><p>Release build, without pymalloc</p></td>
  521. <td><p><code class="docutils literal notranslate"><span class="pre">&quot;malloc&quot;</span></code></p></td>
  522. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code></p></td>
  523. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code></p></td>
  524. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code></p></td>
  525. </tr>
  526. <tr class="row-odd"><td><p>Debug build, without pymalloc</p></td>
  527. <td><p><code class="docutils literal notranslate"><span class="pre">&quot;malloc_debug&quot;</span></code></p></td>
  528. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code> + debug</p></td>
  529. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code> + debug</p></td>
  530. <td><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code> + debug</p></td>
  531. </tr>
  532. </tbody>
  533. </table>
  534. <p>Legend:</p>
  535. <ul class="simple">
  536. <li><p>Name: value for <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code></a> environment variable.</p></li>
  537. <li><p><code class="docutils literal notranslate"><span class="pre">malloc</span></code>: system allocators from the standard C library, C functions:
  538. <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">calloc()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">realloc()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code>.</p></li>
  539. <li><p><code class="docutils literal notranslate"><span class="pre">pymalloc</span></code>: <a class="reference internal" href="#pymalloc"><span class="std std-ref">pymalloc memory allocator</span></a>.</p></li>
  540. <li><p>“+ debug”: with <a class="reference internal" href="#pymem-debug-hooks"><span class="std std-ref">debug hooks on the Python memory allocators</span></a>.</p></li>
  541. <li><p>“Debug build”: <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">Python build in debug mode</span></a>.</p></li>
  542. </ul>
  543. </section>
  544. <section id="customize-memory-allocators">
  545. <span id="id3"></span><h2>Customize Memory Allocators<a class="headerlink" href="#customize-memory-allocators" title="Link to this heading">¶</a></h2>
  546. <div class="versionadded">
  547. <p><span class="versionmodified added">New in version 3.4.</span></p>
  548. </div>
  549. <dl class="c type">
  550. <dt class="sig sig-object c" id="c.PyMemAllocatorEx">
  551. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMemAllocatorEx</span></span></span><a class="headerlink" href="#c.PyMemAllocatorEx" title="Link to this definition">¶</a><br /></dt>
  552. <dd><p>Structure used to describe a memory block allocator. The structure has
  553. the following fields:</p>
  554. <table class="docutils align-default">
  555. <thead>
  556. <tr class="row-odd"><th class="head"><p>Field</p></th>
  557. <th class="head"><p>Meaning</p></th>
  558. </tr>
  559. </thead>
  560. <tbody>
  561. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">*ctx</span></code></p></td>
  562. <td><p>user context passed as first argument</p></td>
  563. </tr>
  564. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">void*</span> <span class="pre">malloc(void</span> <span class="pre">*ctx,</span> <span class="pre">size_t</span> <span class="pre">size)</span></code></p></td>
  565. <td><p>allocate a memory block</p></td>
  566. </tr>
  567. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">void*</span> <span class="pre">calloc(void</span> <span class="pre">*ctx,</span> <span class="pre">size_t</span> <span class="pre">nelem,</span> <span class="pre">size_t</span> <span class="pre">elsize)</span></code></p></td>
  568. <td><p>allocate a memory block initialized
  569. with zeros</p></td>
  570. </tr>
  571. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">void*</span> <span class="pre">realloc(void</span> <span class="pre">*ctx,</span> <span class="pre">void</span> <span class="pre">*ptr,</span> <span class="pre">size_t</span> <span class="pre">new_size)</span></code></p></td>
  572. <td><p>allocate or resize a memory block</p></td>
  573. </tr>
  574. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">free(void</span> <span class="pre">*ctx,</span> <span class="pre">void</span> <span class="pre">*ptr)</span></code></p></td>
  575. <td><p>free a memory block</p></td>
  576. </tr>
  577. </tbody>
  578. </table>
  579. <div class="versionchanged">
  580. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemAllocator</span></code> structure was renamed to
  581. <a class="reference internal" href="#c.PyMemAllocatorEx" title="PyMemAllocatorEx"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemAllocatorEx</span></code></a> and a new <code class="docutils literal notranslate"><span class="pre">calloc</span></code> field was added.</p>
  582. </div>
  583. </dd></dl>
  584. <dl class="c type">
  585. <dt class="sig sig-object c" id="c.PyMemAllocatorDomain">
  586. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMemAllocatorDomain</span></span></span><a class="headerlink" href="#c.PyMemAllocatorDomain" title="Link to this definition">¶</a><br /></dt>
  587. <dd><p>Enum used to identify an allocator domain. Domains:</p>
  588. <dl class="c macro">
  589. <dt class="sig sig-object c" id="c.PYMEM_DOMAIN_RAW">
  590. <span class="sig-name descname"><span class="n"><span class="pre">PYMEM_DOMAIN_RAW</span></span></span><a class="headerlink" href="#c.PYMEM_DOMAIN_RAW" title="Link to this definition">¶</a><br /></dt>
  591. <dd><p>Functions:</p>
  592. <ul class="simple">
  593. <li><p><a class="reference internal" href="#c.PyMem_RawMalloc" title="PyMem_RawMalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code></a></p></li>
  594. <li><p><a class="reference internal" href="#c.PyMem_RawRealloc" title="PyMem_RawRealloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawRealloc()</span></code></a></p></li>
  595. <li><p><a class="reference internal" href="#c.PyMem_RawCalloc" title="PyMem_RawCalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawCalloc()</span></code></a></p></li>
  596. <li><p><a class="reference internal" href="#c.PyMem_RawFree" title="PyMem_RawFree"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawFree()</span></code></a></p></li>
  597. </ul>
  598. </dd></dl>
  599. <dl class="c macro">
  600. <dt class="sig sig-object c" id="c.PYMEM_DOMAIN_MEM">
  601. <span class="sig-name descname"><span class="n"><span class="pre">PYMEM_DOMAIN_MEM</span></span></span><a class="headerlink" href="#c.PYMEM_DOMAIN_MEM" title="Link to this definition">¶</a><br /></dt>
  602. <dd><p>Functions:</p>
  603. <ul class="simple">
  604. <li><p><a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>,</p></li>
  605. <li><p><a class="reference internal" href="#c.PyMem_Realloc" title="PyMem_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Realloc()</span></code></a></p></li>
  606. <li><p><a class="reference internal" href="#c.PyMem_Calloc" title="PyMem_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Calloc()</span></code></a></p></li>
  607. <li><p><a class="reference internal" href="#c.PyMem_Free" title="PyMem_Free"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Free()</span></code></a></p></li>
  608. </ul>
  609. </dd></dl>
  610. <dl class="c macro">
  611. <dt class="sig sig-object c" id="c.PYMEM_DOMAIN_OBJ">
  612. <span class="sig-name descname"><span class="n"><span class="pre">PYMEM_DOMAIN_OBJ</span></span></span><a class="headerlink" href="#c.PYMEM_DOMAIN_OBJ" title="Link to this definition">¶</a><br /></dt>
  613. <dd><p>Functions:</p>
  614. <ul class="simple">
  615. <li><p><a class="reference internal" href="#c.PyObject_Malloc" title="PyObject_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Malloc()</span></code></a></p></li>
  616. <li><p><a class="reference internal" href="#c.PyObject_Realloc" title="PyObject_Realloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Realloc()</span></code></a></p></li>
  617. <li><p><a class="reference internal" href="#c.PyObject_Calloc" title="PyObject_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Calloc()</span></code></a></p></li>
  618. <li><p><a class="reference internal" href="#c.PyObject_Free" title="PyObject_Free"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Free()</span></code></a></p></li>
  619. </ul>
  620. </dd></dl>
  621. </dd></dl>
  622. <dl class="c function">
  623. <dt class="sig sig-object c" id="c.PyMem_GetAllocator">
  624. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_GetAllocator</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyMemAllocatorDomain" title="PyMemAllocatorDomain"><span class="n"><span class="pre">PyMemAllocatorDomain</span></span></a><span class="w"> </span><span class="n"><span class="pre">domain</span></span>, <a class="reference internal" href="#c.PyMemAllocatorEx" title="PyMemAllocatorEx"><span class="n"><span class="pre">PyMemAllocatorEx</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">allocator</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_GetAllocator" title="Link to this definition">¶</a><br /></dt>
  625. <dd><p>Get the memory block allocator of the specified domain.</p>
  626. </dd></dl>
  627. <dl class="c function">
  628. <dt class="sig sig-object c" id="c.PyMem_SetAllocator">
  629. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_SetAllocator</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyMemAllocatorDomain" title="PyMemAllocatorDomain"><span class="n"><span class="pre">PyMemAllocatorDomain</span></span></a><span class="w"> </span><span class="n"><span class="pre">domain</span></span>, <a class="reference internal" href="#c.PyMemAllocatorEx" title="PyMemAllocatorEx"><span class="n"><span class="pre">PyMemAllocatorEx</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">allocator</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_SetAllocator" title="Link to this definition">¶</a><br /></dt>
  630. <dd><p>Set the memory block allocator of the specified domain.</p>
  631. <p>The new allocator must return a distinct non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer when requesting
  632. zero bytes.</p>
  633. <p>For the <a class="reference internal" href="#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_RAW</span></code></a> domain, the allocator must be
  634. thread-safe: the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">GIL</span></a> is not held when the
  635. allocator is called.</p>
  636. <p>For the remaining domains, the allocator must also be thread-safe:
  637. the allocator may be called in different interpreters that do not
  638. share a <code class="docutils literal notranslate"><span class="pre">GIL</span></code>.</p>
  639. <p>If the new allocator is not a hook (does not call the previous allocator),
  640. the <a class="reference internal" href="#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> function must be called to reinstall the
  641. debug hooks on top on the new allocator.</p>
  642. <p>See also <a class="reference internal" href="init_config.html#c.PyPreConfig.allocator" title="PyPreConfig.allocator"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyPreConfig.allocator</span></code></a> and <a class="reference internal" href="init_config.html#c-preinit"><span class="std std-ref">Preinitialize Python
  643. with PyPreConfig</span></a>.</p>
  644. <div class="admonition warning">
  645. <p class="admonition-title">Warning</p>
  646. <p><a class="reference internal" href="#c.PyMem_SetAllocator" title="PyMem_SetAllocator"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetAllocator()</span></code></a> does have the following contract:</p>
  647. <ul class="simple">
  648. <li><p>It can be called after <a class="reference internal" href="init_config.html#c.Py_PreInitialize" title="Py_PreInitialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitialize()</span></code></a> and before
  649. <a class="reference internal" href="init_config.html#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a> to install a custom memory
  650. allocator. There are no restrictions over the installed allocator
  651. other than the ones imposed by the domain (for instance, the Raw
  652. Domain allows the allocator to be called without the GIL held). See
  653. <a class="reference internal" href="#id1"><span class="std std-ref">the section on allocator domains</span></a> for more
  654. information.</p></li>
  655. <li><p>If called after Python has finish initializing (after
  656. <a class="reference internal" href="init_config.html#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a> has been called) the allocator
  657. <strong>must</strong> wrap the existing allocator. Substituting the current
  658. allocator for some other arbitrary one is <strong>not supported</strong>.</p></li>
  659. </ul>
  660. </div>
  661. <div class="versionchanged">
  662. <p><span class="versionmodified changed">Changed in version 3.12: </span>All allocators must be thread-safe.</p>
  663. </div>
  664. </dd></dl>
  665. <dl class="c function">
  666. <dt class="sig sig-object c" id="c.PyMem_SetupDebugHooks">
  667. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyMem_SetupDebugHooks</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyMem_SetupDebugHooks" title="Link to this definition">¶</a><br /></dt>
  668. <dd><p>Setup <a class="reference internal" href="#pymem-debug-hooks"><span class="std std-ref">debug hooks in the Python memory allocators</span></a>
  669. to detect memory errors.</p>
  670. </dd></dl>
  671. </section>
  672. <section id="debug-hooks-on-the-python-memory-allocators">
  673. <span id="pymem-debug-hooks"></span><h2>Debug hooks on the Python memory allocators<a class="headerlink" href="#debug-hooks-on-the-python-memory-allocators" title="Link to this heading">¶</a></h2>
  674. <p>When <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">Python is built in debug mode</span></a>, the
  675. <a class="reference internal" href="#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> function is called at the <a class="reference internal" href="init_config.html#c-preinit"><span class="std std-ref">Python
  676. preinitialization</span></a> to setup debug hooks on Python memory allocators
  677. to detect memory errors.</p>
  678. <p>The <span class="target" id="index-4"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code></a> environment variable can be used to install debug
  679. hooks on a Python compiled in release mode (ex: <code class="docutils literal notranslate"><span class="pre">PYTHONMALLOC=debug</span></code>).</p>
  680. <p>The <a class="reference internal" href="#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> function can be used to set debug hooks
  681. after calling <a class="reference internal" href="#c.PyMem_SetAllocator" title="PyMem_SetAllocator"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetAllocator()</span></code></a>.</p>
  682. <p>These debug hooks fill dynamically allocated memory blocks with special,
  683. recognizable bit patterns. Newly allocated memory is filled with the byte
  684. <code class="docutils literal notranslate"><span class="pre">0xCD</span></code> (<code class="docutils literal notranslate"><span class="pre">PYMEM_CLEANBYTE</span></code>), freed memory is filled with the byte <code class="docutils literal notranslate"><span class="pre">0xDD</span></code>
  685. (<code class="docutils literal notranslate"><span class="pre">PYMEM_DEADBYTE</span></code>). Memory blocks are surrounded by “forbidden bytes”
  686. filled with the byte <code class="docutils literal notranslate"><span class="pre">0xFD</span></code> (<code class="docutils literal notranslate"><span class="pre">PYMEM_FORBIDDENBYTE</span></code>). Strings of these bytes
  687. are unlikely to be valid addresses, floats, or ASCII strings.</p>
  688. <p>Runtime checks:</p>
  689. <ul class="simple">
  690. <li><p>Detect API violations. For example, detect if <a class="reference internal" href="#c.PyObject_Free" title="PyObject_Free"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Free()</span></code></a> is
  691. called on a memory block allocated by <a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>.</p></li>
  692. <li><p>Detect write before the start of the buffer (buffer underflow).</p></li>
  693. <li><p>Detect write after the end of the buffer (buffer overflow).</p></li>
  694. <li><p>Check that the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">GIL</span></a> is held when
  695. allocator functions of <a class="reference internal" href="#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a> (ex:
  696. <a class="reference internal" href="#c.PyObject_Malloc" title="PyObject_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Malloc()</span></code></a>) and <a class="reference internal" href="#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a> (ex:
  697. <a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>) domains are called.</p></li>
  698. </ul>
  699. <p>On error, the debug hooks use the <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module to get the
  700. traceback where a memory block was allocated. The traceback is only displayed
  701. if <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> is tracing Python memory allocations and the memory block
  702. was traced.</p>
  703. <p>Let <em>S</em> = <code class="docutils literal notranslate"><span class="pre">sizeof(size_t)</span></code>. <code class="docutils literal notranslate"><span class="pre">2*S</span></code> bytes are added at each end of each block
  704. of <em>N</em> bytes requested. The memory layout is like so, where p represents the
  705. address returned by a malloc-like or realloc-like function (<code class="docutils literal notranslate"><span class="pre">p[i:j]</span></code> means
  706. the slice of bytes from <code class="docutils literal notranslate"><span class="pre">*(p+i)</span></code> inclusive up to <code class="docutils literal notranslate"><span class="pre">*(p+j)</span></code> exclusive; note
  707. that the treatment of negative indices differs from a Python slice):</p>
  708. <dl>
  709. <dt><code class="docutils literal notranslate"><span class="pre">p[-2*S:-S]</span></code></dt><dd><p>Number of bytes originally asked for. This is a size_t, big-endian (easier
  710. to read in a memory dump).</p>
  711. </dd>
  712. <dt><code class="docutils literal notranslate"><span class="pre">p[-S]</span></code></dt><dd><p>API identifier (ASCII character):</p>
  713. <ul class="simple">
  714. <li><p><code class="docutils literal notranslate"><span class="pre">'r'</span></code> for <a class="reference internal" href="#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_RAW</span></code></a>.</p></li>
  715. <li><p><code class="docutils literal notranslate"><span class="pre">'m'</span></code> for <a class="reference internal" href="#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a>.</p></li>
  716. <li><p><code class="docutils literal notranslate"><span class="pre">'o'</span></code> for <a class="reference internal" href="#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a>.</p></li>
  717. </ul>
  718. </dd>
  719. <dt><code class="docutils literal notranslate"><span class="pre">p[-S+1:0]</span></code></dt><dd><p>Copies of PYMEM_FORBIDDENBYTE. Used to catch under- writes and reads.</p>
  720. </dd>
  721. <dt><code class="docutils literal notranslate"><span class="pre">p[0:N]</span></code></dt><dd><p>The requested memory, filled with copies of PYMEM_CLEANBYTE, used to catch
  722. reference to uninitialized memory. When a realloc-like function is called
  723. requesting a larger memory block, the new excess bytes are also filled with
  724. PYMEM_CLEANBYTE. When a free-like function is called, these are
  725. overwritten with PYMEM_DEADBYTE, to catch reference to freed memory. When
  726. a realloc- like function is called requesting a smaller memory block, the
  727. excess old bytes are also filled with PYMEM_DEADBYTE.</p>
  728. </dd>
  729. <dt><code class="docutils literal notranslate"><span class="pre">p[N:N+S]</span></code></dt><dd><p>Copies of PYMEM_FORBIDDENBYTE. Used to catch over- writes and reads.</p>
  730. </dd>
  731. <dt><code class="docutils literal notranslate"><span class="pre">p[N+S:N+2*S]</span></code></dt><dd><p>Only used if the <code class="docutils literal notranslate"><span class="pre">PYMEM_DEBUG_SERIALNO</span></code> macro is defined (not defined by
  732. default).</p>
  733. <p>A serial number, incremented by 1 on each call to a malloc-like or
  734. realloc-like function. Big-endian <code class="xref c c-type docutils literal notranslate"><span class="pre">size_t</span></code>. If “bad memory” is detected
  735. later, the serial number gives an excellent way to set a breakpoint on the
  736. next run, to capture the instant at which this block was passed out. The
  737. static function bumpserialno() in obmalloc.c is the only place the serial
  738. number is incremented, and exists so you can set such a breakpoint easily.</p>
  739. </dd>
  740. </dl>
  741. <p>A realloc-like or free-like function first checks that the PYMEM_FORBIDDENBYTE
  742. bytes at each end are intact. If they’ve been altered, diagnostic output is
  743. written to stderr, and the program is aborted via Py_FatalError(). The other
  744. main failure mode is provoking a memory error when a program reads up one of
  745. the special bit patterns and tries to use it as an address. If you get in a
  746. debugger then and look at the object, you’re likely to see that it’s entirely
  747. filled with PYMEM_DEADBYTE (meaning freed memory is getting used) or
  748. PYMEM_CLEANBYTE (meaning uninitialized memory is getting used).</p>
  749. <div class="versionchanged">
  750. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <a class="reference internal" href="#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> function now also works on Python
  751. compiled in release mode. On error, the debug hooks now use
  752. <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> to get the traceback where a memory block was allocated.
  753. The debug hooks now also check if the GIL is held when functions of
  754. <a class="reference internal" href="#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a> and <a class="reference internal" href="#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a> domains are
  755. called.</p>
  756. </div>
  757. <div class="versionchanged">
  758. <p><span class="versionmodified changed">Changed in version 3.8: </span>Byte patterns <code class="docutils literal notranslate"><span class="pre">0xCB</span></code> (<code class="docutils literal notranslate"><span class="pre">PYMEM_CLEANBYTE</span></code>), <code class="docutils literal notranslate"><span class="pre">0xDB</span></code> (<code class="docutils literal notranslate"><span class="pre">PYMEM_DEADBYTE</span></code>)
  759. and <code class="docutils literal notranslate"><span class="pre">0xFB</span></code> (<code class="docutils literal notranslate"><span class="pre">PYMEM_FORBIDDENBYTE</span></code>) have been replaced with <code class="docutils literal notranslate"><span class="pre">0xCD</span></code>,
  760. <code class="docutils literal notranslate"><span class="pre">0xDD</span></code> and <code class="docutils literal notranslate"><span class="pre">0xFD</span></code> to use the same values than Windows CRT debug
  761. <code class="docutils literal notranslate"><span class="pre">malloc()</span></code> and <code class="docutils literal notranslate"><span class="pre">free()</span></code>.</p>
  762. </div>
  763. </section>
  764. <section id="the-pymalloc-allocator">
  765. <span id="pymalloc"></span><h2>The pymalloc allocator<a class="headerlink" href="#the-pymalloc-allocator" title="Link to this heading">¶</a></h2>
  766. <p>Python has a <em>pymalloc</em> allocator optimized for small objects (smaller or equal
  767. to 512 bytes) with a short lifetime. It uses memory mappings called “arenas”
  768. with a fixed size of either 256 KiB on 32-bit platforms or 1 MiB on 64-bit
  769. platforms. It falls back to <a class="reference internal" href="#c.PyMem_RawMalloc" title="PyMem_RawMalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code></a> and
  770. <a class="reference internal" href="#c.PyMem_RawRealloc" title="PyMem_RawRealloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawRealloc()</span></code></a> for allocations larger than 512 bytes.</p>
  771. <p><em>pymalloc</em> is the <a class="reference internal" href="#default-memory-allocators"><span class="std std-ref">default allocator</span></a> of the
  772. <a class="reference internal" href="#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a> (ex: <a class="reference internal" href="#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Malloc()</span></code></a>) and
  773. <a class="reference internal" href="#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a> (ex: <a class="reference internal" href="#c.PyObject_Malloc" title="PyObject_Malloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Malloc()</span></code></a>) domains.</p>
  774. <p>The arena allocator uses the following functions:</p>
  775. <ul class="simple">
  776. <li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">VirtualAlloc()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">VirtualFree()</span></code> on Windows,</p></li>
  777. <li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">mmap()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">munmap()</span></code> if available,</p></li>
  778. <li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code> otherwise.</p></li>
  779. </ul>
  780. <p>This allocator is disabled if Python is configured with the
  781. <a class="reference internal" href="../using/configure.html#cmdoption-without-pymalloc"><code class="xref std std-option docutils literal notranslate"><span class="pre">--without-pymalloc</span></code></a> option. It can also be disabled at runtime using
  782. the <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code></a> environment variable (ex: <code class="docutils literal notranslate"><span class="pre">PYTHONMALLOC=malloc</span></code>).</p>
  783. <section id="customize-pymalloc-arena-allocator">
  784. <h3>Customize pymalloc Arena Allocator<a class="headerlink" href="#customize-pymalloc-arena-allocator" title="Link to this heading">¶</a></h3>
  785. <div class="versionadded">
  786. <p><span class="versionmodified added">New in version 3.4.</span></p>
  787. </div>
  788. <dl class="c type">
  789. <dt class="sig sig-object c" id="c.PyObjectArenaAllocator">
  790. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObjectArenaAllocator</span></span></span><a class="headerlink" href="#c.PyObjectArenaAllocator" title="Link to this definition">¶</a><br /></dt>
  791. <dd><p>Structure used to describe an arena allocator. The structure has
  792. three fields:</p>
  793. <table class="docutils align-default">
  794. <thead>
  795. <tr class="row-odd"><th class="head"><p>Field</p></th>
  796. <th class="head"><p>Meaning</p></th>
  797. </tr>
  798. </thead>
  799. <tbody>
  800. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">*ctx</span></code></p></td>
  801. <td><p>user context passed as first argument</p></td>
  802. </tr>
  803. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">void*</span> <span class="pre">alloc(void</span> <span class="pre">*ctx,</span> <span class="pre">size_t</span> <span class="pre">size)</span></code></p></td>
  804. <td><p>allocate an arena of size bytes</p></td>
  805. </tr>
  806. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">free(void</span> <span class="pre">*ctx,</span> <span class="pre">void</span> <span class="pre">*ptr,</span> <span class="pre">size_t</span> <span class="pre">size)</span></code></p></td>
  807. <td><p>free an arena</p></td>
  808. </tr>
  809. </tbody>
  810. </table>
  811. </dd></dl>
  812. <dl class="c function">
  813. <dt class="sig sig-object c" id="c.PyObject_GetArenaAllocator">
  814. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GetArenaAllocator</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObjectArenaAllocator" title="PyObjectArenaAllocator"><span class="n"><span class="pre">PyObjectArenaAllocator</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">allocator</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GetArenaAllocator" title="Link to this definition">¶</a><br /></dt>
  815. <dd><p>Get the arena allocator.</p>
  816. </dd></dl>
  817. <dl class="c function">
  818. <dt class="sig sig-object c" id="c.PyObject_SetArenaAllocator">
  819. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_SetArenaAllocator</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyObjectArenaAllocator" title="PyObjectArenaAllocator"><span class="n"><span class="pre">PyObjectArenaAllocator</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">allocator</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_SetArenaAllocator" title="Link to this definition">¶</a><br /></dt>
  820. <dd><p>Set the arena allocator.</p>
  821. </dd></dl>
  822. </section>
  823. </section>
  824. <section id="tracemalloc-c-api">
  825. <h2>tracemalloc C API<a class="headerlink" href="#tracemalloc-c-api" title="Link to this heading">¶</a></h2>
  826. <div class="versionadded">
  827. <p><span class="versionmodified added">New in version 3.7.</span></p>
  828. </div>
  829. <dl class="c function">
  830. <dt class="sig sig-object c" id="c.PyTraceMalloc_Track">
  831. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTraceMalloc_Track</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">domain</span></span>, <span class="n"><span class="pre">uintptr_t</span></span><span class="w"> </span><span class="n"><span class="pre">ptr</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">size</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyTraceMalloc_Track" title="Link to this definition">¶</a><br /></dt>
  832. <dd><p>Track an allocated memory block in the <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module.</p>
  833. <p>Return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success, return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error (failed to allocate memory to
  834. store the trace). Return <code class="docutils literal notranslate"><span class="pre">-2</span></code> if tracemalloc is disabled.</p>
  835. <p>If memory block is already tracked, update the existing trace.</p>
  836. </dd></dl>
  837. <dl class="c function">
  838. <dt class="sig sig-object c" id="c.PyTraceMalloc_Untrack">
  839. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTraceMalloc_Untrack</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">domain</span></span>, <span class="n"><span class="pre">uintptr_t</span></span><span class="w"> </span><span class="n"><span class="pre">ptr</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyTraceMalloc_Untrack" title="Link to this definition">¶</a><br /></dt>
  840. <dd><p>Untrack an allocated memory block in the <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module.
  841. Do nothing if the block was not tracked.</p>
  842. <p>Return <code class="docutils literal notranslate"><span class="pre">-2</span></code> if tracemalloc is disabled, otherwise return <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
  843. </dd></dl>
  844. </section>
  845. <section id="examples">
  846. <span id="memoryexamples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  847. <p>Here is the example from section <a class="reference internal" href="#memoryoverview"><span class="std std-ref">Overview</span></a>, rewritten so that the
  848. I/O buffer is allocated from the Python heap by using the first function set:</p>
  849. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">res</span><span class="p">;</span>
  850. <span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">PyMem_Malloc</span><span class="p">(</span><span class="n">BUFSIZ</span><span class="p">);</span><span class="w"> </span><span class="cm">/* for I/O */</span>
  851. <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">buf</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
  852. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">PyErr_NoMemory</span><span class="p">();</span>
  853. <span class="cm">/* ...Do some I/O operation involving buf... */</span>
  854. <span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyBytes_FromString</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
  855. <span class="n">PyMem_Free</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span><span class="w"> </span><span class="cm">/* allocated with PyMem_Malloc */</span>
  856. <span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
  857. </pre></div>
  858. </div>
  859. <p>The same code using the type-oriented function set:</p>
  860. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">res</span><span class="p">;</span>
  861. <span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyMem_New</span><span class="p">(</span><span class="kt">char</span><span class="p">,</span><span class="w"> </span><span class="n">BUFSIZ</span><span class="p">);</span><span class="w"> </span><span class="cm">/* for I/O */</span>
  862. <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">buf</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
  863. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">PyErr_NoMemory</span><span class="p">();</span>
  864. <span class="cm">/* ...Do some I/O operation involving buf... */</span>
  865. <span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyBytes_FromString</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
  866. <span class="n">PyMem_Del</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span><span class="w"> </span><span class="cm">/* allocated with PyMem_New */</span>
  867. <span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
  868. </pre></div>
  869. </div>
  870. <p>Note that in the two examples above, the buffer is always manipulated via
  871. functions belonging to the same set. Indeed, it is required to use the same
  872. memory API family for a given memory block, so that the risk of mixing different
  873. allocators is reduced to a minimum. The following code sequence contains two
  874. errors, one of which is labeled as <em>fatal</em> because it mixes two different
  875. allocators operating on different heaps.</p>
  876. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buf1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyMem_New</span><span class="p">(</span><span class="kt">char</span><span class="p">,</span><span class="w"> </span><span class="n">BUFSIZ</span><span class="p">);</span>
  877. <span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buf2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="n">BUFSIZ</span><span class="p">);</span>
  878. <span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buf3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">PyMem_Malloc</span><span class="p">(</span><span class="n">BUFSIZ</span><span class="p">);</span>
  879. <span class="p">...</span>
  880. <span class="n">PyMem_Del</span><span class="p">(</span><span class="n">buf3</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Wrong -- should be PyMem_Free() */</span>
  881. <span class="n">free</span><span class="p">(</span><span class="n">buf2</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Right -- allocated via malloc() */</span>
  882. <span class="n">free</span><span class="p">(</span><span class="n">buf1</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Fatal -- should be PyMem_Del() */</span>
  883. </pre></div>
  884. </div>
  885. <p>In addition to the functions aimed at handling raw memory blocks from the Python
  886. heap, objects in Python are allocated and released with <a class="reference internal" href="allocation.html#c.PyObject_New" title="PyObject_New"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_New</span></code></a>,
  887. <a class="reference internal" href="allocation.html#c.PyObject_NewVar" title="PyObject_NewVar"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_NewVar</span></code></a> and <a class="reference internal" href="allocation.html#c.PyObject_Del" title="PyObject_Del"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Del()</span></code></a>.</p>
  888. <p>These will be explained in the next chapter on defining and implementing new
  889. object types in C.</p>
  890. </section>
  891. </section>
  892. <div class="clearer"></div>
  893. </div>
  894. </div>
  895. </div>
  896. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  897. <div class="sphinxsidebarwrapper">
  898. <div>
  899. <h3><a href="../contents.html">Table of Contents</a></h3>
  900. <ul>
  901. <li><a class="reference internal" href="#">Memory Management</a><ul>
  902. <li><a class="reference internal" href="#overview">Overview</a></li>
  903. <li><a class="reference internal" href="#allocator-domains">Allocator Domains</a></li>
  904. <li><a class="reference internal" href="#raw-memory-interface">Raw Memory Interface</a></li>
  905. <li><a class="reference internal" href="#memory-interface">Memory Interface</a></li>
  906. <li><a class="reference internal" href="#object-allocators">Object allocators</a></li>
  907. <li><a class="reference internal" href="#default-memory-allocators">Default Memory Allocators</a></li>
  908. <li><a class="reference internal" href="#customize-memory-allocators">Customize Memory Allocators</a></li>
  909. <li><a class="reference internal" href="#debug-hooks-on-the-python-memory-allocators">Debug hooks on the Python memory allocators</a></li>
  910. <li><a class="reference internal" href="#the-pymalloc-allocator">The pymalloc allocator</a><ul>
  911. <li><a class="reference internal" href="#customize-pymalloc-arena-allocator">Customize pymalloc Arena Allocator</a></li>
  912. </ul>
  913. </li>
  914. <li><a class="reference internal" href="#tracemalloc-c-api">tracemalloc C API</a></li>
  915. <li><a class="reference internal" href="#examples">Examples</a></li>
  916. </ul>
  917. </li>
  918. </ul>
  919. </div>
  920. <div>
  921. <h4>Previous topic</h4>
  922. <p class="topless"><a href="init_config.html"
  923. title="previous chapter">Python Initialization Configuration</a></p>
  924. </div>
  925. <div>
  926. <h4>Next topic</h4>
  927. <p class="topless"><a href="objimpl.html"
  928. title="next chapter">Object Implementation Support</a></p>
  929. </div>
  930. <div role="note" aria-label="source link">
  931. <h3>This Page</h3>
  932. <ul class="this-page-menu">
  933. <li><a href="../bugs.html">Report a Bug</a></li>
  934. <li>
  935. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/memory.rst"
  936. rel="nofollow">Show Source
  937. </a>
  938. </li>
  939. </ul>
  940. </div>
  941. </div>
  942. <div id="sidebarbutton" title="Collapse sidebar">
  943. <span>«</span>
  944. </div>
  945. </div>
  946. <div class="clearer"></div>
  947. </div>
  948. <div class="related" role="navigation" aria-label="related navigation">
  949. <h3>Navigation</h3>
  950. <ul>
  951. <li class="right" style="margin-right: 10px">
  952. <a href="../genindex.html" title="General Index"
  953. >index</a></li>
  954. <li class="right" >
  955. <a href="../py-modindex.html" title="Python Module Index"
  956. >modules</a> |</li>
  957. <li class="right" >
  958. <a href="objimpl.html" title="Object Implementation Support"
  959. >next</a> |</li>
  960. <li class="right" >
  961. <a href="init_config.html" title="Python Initialization Configuration"
  962. >previous</a> |</li>
  963. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  964. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  965. <li class="switchers">
  966. <div class="language_switcher_placeholder"></div>
  967. <div class="version_switcher_placeholder"></div>
  968. </li>
  969. <li>
  970. </li>
  971. <li id="cpython-language-and-version">
  972. <a href="../index.html">3.12.3 Documentation</a> &#187;
  973. </li>
  974. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  975. <li class="nav-item nav-item-this"><a href="">Memory Management</a></li>
  976. <li class="right">
  977. <div class="inline-search" role="search">
  978. <form class="inline-search" action="../search.html" method="get">
  979. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  980. <input type="submit" value="Go" />
  981. </form>
  982. </div>
  983. |
  984. </li>
  985. <li class="right">
  986. <label class="theme-selector-label">
  987. Theme
  988. <select class="theme-selector" oninput="activateTheme(this.value)">
  989. <option value="auto" selected>Auto</option>
  990. <option value="light">Light</option>
  991. <option value="dark">Dark</option>
  992. </select>
  993. </label> |</li>
  994. </ul>
  995. </div>
  996. <div class="footer">
  997. &copy;
  998. <a href="../copyright.html">
  999. Copyright
  1000. </a>
  1001. 2001-2024, Python Software Foundation.
  1002. <br />
  1003. This page is licensed under the Python Software Foundation License Version 2.
  1004. <br />
  1005. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1006. <br />
  1007. See <a href="/license.html">History and License</a> for more information.<br />
  1008. <br />
  1009. The Python Software Foundation is a non-profit corporation.
  1010. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1011. <br />
  1012. <br />
  1013. Last updated on Apr 09, 2024 (13:47 UTC).
  1014. <a href="/bugs.html">Found a bug</a>?
  1015. <br />
  1016. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1017. </div>
  1018. </body>
  1019. </html>
上海开阖软件有限公司 沪ICP备12045867号-1