Commit | Line | Data |
---|---|---|
920dae64 AT |
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="prev" href="memoryInterface.html" /> | |
13 | <link rel="parent" href="memory.html" /> | |
14 | <link rel="next" href="newTypes.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>9.3 Examples </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="9.2 Memory Interface" | |
24 | href="memoryInterface.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="9. Memory Management" | |
27 | href="memory.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="10. Object Implementation Support" | |
30 | href="newTypes.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python/C API Reference Manual</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><img src='../icons/blank.png' | |
37 | border='0' height='32' alt='' width='32' /></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="memoryInterface.html">9.2 Memory Interface</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="memory.html">9. Memory Management</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="newTypes.html">10. Object Implementation Support</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H1><A NAME="SECTION0011300000000000000000"></A><A NAME="memoryExamples"></A> | |
55 | <BR> | |
56 | 9.3 Examples | |
57 | </H1> | |
58 | ||
59 | <P> | |
60 | Here is the example from section <A href="memoryOverview.html#memoryOverview">9.1</A>, rewritten so | |
61 | that the I/O buffer is allocated from the Python heap by using the | |
62 | first function set: | |
63 | ||
64 | <P> | |
65 | <div class="verbatim"><pre> | |
66 | PyObject *res; | |
67 | char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */ | |
68 | ||
69 | if (buf == NULL) | |
70 | return PyErr_NoMemory(); | |
71 | /* ...Do some I/O operation involving buf... */ | |
72 | res = PyString_FromString(buf); | |
73 | PyMem_Free(buf); /* allocated with PyMem_Malloc */ | |
74 | return res; | |
75 | </pre></div> | |
76 | ||
77 | <P> | |
78 | The same code using the type-oriented function set: | |
79 | ||
80 | <P> | |
81 | <div class="verbatim"><pre> | |
82 | PyObject *res; | |
83 | char *buf = PyMem_New(char, BUFSIZ); /* for I/O */ | |
84 | ||
85 | if (buf == NULL) | |
86 | return PyErr_NoMemory(); | |
87 | /* ...Do some I/O operation involving buf... */ | |
88 | res = PyString_FromString(buf); | |
89 | PyMem_Del(buf); /* allocated with PyMem_New */ | |
90 | return res; | |
91 | </pre></div> | |
92 | ||
93 | <P> | |
94 | Note that in the two examples above, the buffer is always | |
95 | manipulated via functions belonging to the same set. Indeed, it | |
96 | is required to use the same memory API family for a given | |
97 | memory block, so that the risk of mixing different allocators is | |
98 | reduced to a minimum. The following code sequence contains two errors, | |
99 | one of which is labeled as <em>fatal</em> because it mixes two different | |
100 | allocators operating on different heaps. | |
101 | ||
102 | <P> | |
103 | <div class="verbatim"><pre> | |
104 | char *buf1 = PyMem_New(char, BUFSIZ); | |
105 | char *buf2 = (char *) malloc(BUFSIZ); | |
106 | char *buf3 = (char *) PyMem_Malloc(BUFSIZ); | |
107 | ... | |
108 | PyMem_Del(buf3); /* Wrong -- should be PyMem_Free() */ | |
109 | free(buf2); /* Right -- allocated via malloc() */ | |
110 | free(buf1); /* Fatal -- should be PyMem_Del() */ | |
111 | </pre></div> | |
112 | ||
113 | <P> | |
114 | In addition to the functions aimed at handling raw memory blocks from | |
115 | the Python heap, objects in Python are allocated and released with | |
116 | <tt class="cfunction">PyObject_New()</tt>, <tt class="cfunction">PyObject_NewVar()</tt> and | |
117 | <tt class="cfunction">PyObject_Del()</tt>, or with their corresponding macros | |
118 | <tt class="cfunction">PyObject_NEW()</tt>, <tt class="cfunction">PyObject_NEW_VAR()</tt> and | |
119 | <tt class="cfunction">PyObject_DEL()</tt>. | |
120 | ||
121 | <P> | |
122 | These will be explained in the next chapter on defining and | |
123 | implementing new object types in C. | |
124 | ||
125 | <DIV CLASS="navigation"> | |
126 | <div class='online-navigation'> | |
127 | <p></p><hr /> | |
128 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
129 | <tr> | |
130 | <td class='online-navigation'><a rel="prev" title="9.2 Memory Interface" | |
131 | href="memoryInterface.html"><img src='../icons/previous.png' | |
132 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
133 | <td class='online-navigation'><a rel="parent" title="9. Memory Management" | |
134 | href="memory.html"><img src='../icons/up.png' | |
135 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
136 | <td class='online-navigation'><a rel="next" title="10. Object Implementation Support" | |
137 | href="newTypes.html"><img src='../icons/next.png' | |
138 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
139 | <td align="center" width="100%">Python/C API Reference Manual</td> | |
140 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
141 | href="contents.html"><img src='../icons/contents.png' | |
142 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
143 | <td class='online-navigation'><img src='../icons/blank.png' | |
144 | border='0' height='32' alt='' width='32' /></td> | |
145 | <td class='online-navigation'><a rel="index" title="Index" | |
146 | href="genindex.html"><img src='../icons/index.png' | |
147 | border='0' height='32' alt='Index' width='32' /></A></td> | |
148 | </tr></table> | |
149 | <div class='online-navigation'> | |
150 | <b class="navlabel">Previous:</b> | |
151 | <a class="sectref" rel="prev" href="memoryInterface.html">9.2 Memory Interface</A> | |
152 | <b class="navlabel">Up:</b> | |
153 | <a class="sectref" rel="parent" href="memory.html">9. Memory Management</A> | |
154 | <b class="navlabel">Next:</b> | |
155 | <a class="sectref" rel="next" href="newTypes.html">10. Object Implementation Support</A> | |
156 | </div> | |
157 | </div> | |
158 | <hr /> | |
159 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
160 | </DIV> | |
161 | <!--End of Navigation Panel--> | |
162 | <ADDRESS> | |
163 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
164 | </ADDRESS> | |
165 | </BODY> | |
166 | </HTML> |