| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="api.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="api.html" title='Python/C API Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="memoryExamples.html" /> |
| 13 | <link rel="prev" href="memoryOverview.html" /> |
| 14 | <link rel="parent" href="memory.html" /> |
| 15 | <link rel="next" href="memoryExamples.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>9.2 Memory Interface </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="9.1 Overview" |
| 25 | href="memoryOverview.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="9. Memory Management" |
| 28 | href="memory.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="9.3 Examples" |
| 31 | href="memoryExamples.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="memoryOverview.html">9.1 Overview</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="memory.html">9. Memory Management</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="memoryExamples.html">9.3 Examples</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0011200000000000000000"></A><A NAME="memoryInterface"></A> |
| 56 | <BR> |
| 57 | 9.2 Memory Interface |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | The following function sets, modeled after the ANSI C standard, |
| 62 | but specifying behavior when requesting zero bytes, |
| 63 | are available for allocating and releasing memory from the Python heap: |
| 64 | |
| 65 | <P> |
| 66 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void* <b><tt id='l2h-899' xml:id='l2h-899' class="cfunction">PyMem_Malloc</tt></b>(</nobr></td><td>size_t <var>n</var>)</td></tr></table></dt> |
| 67 | <dd> |
| 68 | Allocates <var>n</var> bytes and returns a pointer of type <tt class="ctype">void*</tt> |
| 69 | to the allocated memory, or <tt class="constant">NULL</tt> if the request fails. |
| 70 | Requesting zero bytes returns a distinct non-<tt class="constant">NULL</tt> pointer if |
| 71 | possible, as if <tt class="cfunction">PyMem_Malloc(1)</tt> had been called instead. |
| 72 | The memory will not have been initialized in any way. |
| 73 | </dd></dl> |
| 74 | |
| 75 | <P> |
| 76 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void* <b><tt id='l2h-900' xml:id='l2h-900' class="cfunction">PyMem_Realloc</tt></b>(</nobr></td><td>void *<var>p</var>, size_t <var>n</var>)</td></tr></table></dt> |
| 77 | <dd> |
| 78 | Resizes the memory block pointed to by <var>p</var> to <var>n</var> bytes. |
| 79 | The contents will be unchanged to the minimum of the old and the new |
| 80 | sizes. If <var>p</var> is <tt class="constant">NULL</tt>, the call is equivalent to |
| 81 | <tt class="cfunction">PyMem_Malloc(<var>n</var>)</tt>; else if <var>n</var> is equal to zero, the |
| 82 | memory block is resized but is not freed, and the returned pointer |
| 83 | is non-<tt class="constant">NULL</tt>. Unless <var>p</var> is <tt class="constant">NULL</tt>, it must have been |
| 84 | returned by a previous call to <tt class="cfunction">PyMem_Malloc()</tt> or |
| 85 | <tt class="cfunction">PyMem_Realloc()</tt>. |
| 86 | </dd></dl> |
| 87 | |
| 88 | <P> |
| 89 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-901' xml:id='l2h-901' class="cfunction">PyMem_Free</tt></b>(</nobr></td><td>void *<var>p</var>)</td></tr></table></dt> |
| 90 | <dd> |
| 91 | Frees the memory block pointed to by <var>p</var>, which must have been |
| 92 | returned by a previous call to <tt class="cfunction">PyMem_Malloc()</tt> or |
| 93 | <tt class="cfunction">PyMem_Realloc()</tt>. Otherwise, or if |
| 94 | <tt class="cfunction">PyMem_Free(p)</tt> has been called before, undefined |
| 95 | behavior occurs. If <var>p</var> is <tt class="constant">NULL</tt>, no operation is performed. |
| 96 | </dd></dl> |
| 97 | |
| 98 | <P> |
| 99 | The following type-oriented macros are provided for convenience. Note |
| 100 | that <var>TYPE</var> refers to any C type. |
| 101 | |
| 102 | <P> |
| 103 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>* <b><tt id='l2h-902' xml:id='l2h-902' class="cfunction">PyMem_New</tt></b>(</nobr></td><td>TYPE, size_t <var>n</var>)</td></tr></table></dt> |
| 104 | <dd> |
| 105 | Same as <tt class="cfunction">PyMem_Malloc()</tt>, but allocates <code>(<var>n</var> * |
| 106 | sizeof(<var>TYPE</var>))</code> bytes of memory. Returns a pointer cast to |
| 107 | <tt class="ctype"><var>TYPE</var>*</tt>. The memory will not have been initialized in |
| 108 | any way. |
| 109 | </dd></dl> |
| 110 | |
| 111 | <P> |
| 112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>* <b><tt id='l2h-903' xml:id='l2h-903' class="cfunction">PyMem_Resize</tt></b>(</nobr></td><td>void *<var>p</var>, TYPE, size_t <var>n</var>)</td></tr></table></dt> |
| 113 | <dd> |
| 114 | Same as <tt class="cfunction">PyMem_Realloc()</tt>, but the memory block is resized |
| 115 | to <code>(<var>n</var> * sizeof(<var>TYPE</var>))</code> bytes. Returns a pointer |
| 116 | cast to <tt class="ctype"><var>TYPE</var>*</tt>. |
| 117 | </dd></dl> |
| 118 | |
| 119 | <P> |
| 120 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-904' xml:id='l2h-904' class="cfunction">PyMem_Del</tt></b>(</nobr></td><td>void *<var>p</var>)</td></tr></table></dt> |
| 121 | <dd> |
| 122 | Same as <tt class="cfunction">PyMem_Free()</tt>. |
| 123 | </dd></dl> |
| 124 | |
| 125 | <P> |
| 126 | In addition, the following macro sets are provided for calling the |
| 127 | Python memory allocator directly, without involving the C API functions |
| 128 | listed above. However, note that their use does not preserve binary |
| 129 | compatibility across Python versions and is therefore deprecated in |
| 130 | extension modules. |
| 131 | |
| 132 | <P> |
| 133 | <tt class="cfunction">PyMem_MALLOC()</tt>, <tt class="cfunction">PyMem_REALLOC()</tt>, <tt class="cfunction">PyMem_FREE()</tt>. |
| 134 | |
| 135 | <P> |
| 136 | <tt class="cfunction">PyMem_NEW()</tt>, <tt class="cfunction">PyMem_RESIZE()</tt>, <tt class="cfunction">PyMem_DEL()</tt>. |
| 137 | |
| 138 | <P> |
| 139 | |
| 140 | <DIV CLASS="navigation"> |
| 141 | <div class='online-navigation'> |
| 142 | <p></p><hr /> |
| 143 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 144 | <tr> |
| 145 | <td class='online-navigation'><a rel="prev" title="9.1 Overview" |
| 146 | href="memoryOverview.html"><img src='../icons/previous.png' |
| 147 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 148 | <td class='online-navigation'><a rel="parent" title="9. Memory Management" |
| 149 | href="memory.html"><img src='../icons/up.png' |
| 150 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 151 | <td class='online-navigation'><a rel="next" title="9.3 Examples" |
| 152 | href="memoryExamples.html"><img src='../icons/next.png' |
| 153 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 154 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 155 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 156 | href="contents.html"><img src='../icons/contents.png' |
| 157 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 158 | <td class='online-navigation'><img src='../icons/blank.png' |
| 159 | border='0' height='32' alt='' width='32' /></td> |
| 160 | <td class='online-navigation'><a rel="index" title="Index" |
| 161 | href="genindex.html"><img src='../icons/index.png' |
| 162 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 163 | </tr></table> |
| 164 | <div class='online-navigation'> |
| 165 | <b class="navlabel">Previous:</b> |
| 166 | <a class="sectref" rel="prev" href="memoryOverview.html">9.1 Overview</A> |
| 167 | <b class="navlabel">Up:</b> |
| 168 | <a class="sectref" rel="parent" href="memory.html">9. Memory Management</A> |
| 169 | <b class="navlabel">Next:</b> |
| 170 | <a class="sectref" rel="next" href="memoryExamples.html">9.3 Examples</A> |
| 171 | </div> |
| 172 | </div> |
| 173 | <hr /> |
| 174 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 175 | </DIV> |
| 176 | <!--End of Navigation Panel--> |
| 177 | <ADDRESS> |
| 178 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 179 | </ADDRESS> |
| 180 | </BODY> |
| 181 | </HTML> |