Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / api / memoryOverview.html
CommitLineData
86530b38
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="next" href="memoryInterface.html" />
13<link rel="prev" href="memory.html" />
14<link rel="parent" href="memory.html" />
15<link rel="next" href="memoryInterface.html" />
16<meta name='aesop' content='information' />
17<title>9.1 Overview </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. Memory Management"
25 href="memory.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.2 Memory Interface"
31 href="memoryInterface.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="memory.html">9. Memory Management</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="memoryInterface.html">9.2 Memory Interface</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0011100000000000000000"></A><A NAME="memoryOverview"></A>
56<BR>
579.1 Overview
58</H1>
59
60<P>
61Memory management in Python involves a private heap containing all
62Python objects and data structures. The management of this private
63heap is ensured internally by the <em>Python memory manager</em>. The
64Python memory manager has different components which deal with various
65dynamic storage management aspects, like sharing, segmentation,
66preallocation or caching.
67
68<P>
69At the lowest level, a raw memory allocator ensures that there is
70enough room in the private heap for storing all Python-related data
71by interacting with the memory manager of the operating system. On top
72of the raw memory allocator, several object-specific allocators
73operate on the same heap and implement distinct memory management
74policies adapted to the peculiarities of every object type. For
75example, integer objects are managed differently within the heap than
76strings, tuples or dictionaries because integers imply different
77storage requirements and speed/space tradeoffs. The Python memory
78manager thus delegates some of the work to the object-specific
79allocators, but ensures that the latter operate within the bounds of
80the private heap.
81
82<P>
83It is important to understand that the management of the Python heap
84is performed by the interpreter itself and that the user has no
85control over it, even if she regularly manipulates object pointers to
86memory blocks inside that heap. The allocation of heap space for
87Python objects and other internal buffers is performed on demand by
88the Python memory manager through the Python/C API functions listed in
89this document.
90
91<P>
92To avoid memory corruption, extension writers should never try to
93operate on Python objects with the functions exported by the C
94library: <tt class="cfunction">malloc()</tt><a id='l2h-895' xml:id='l2h-895'></a>,
95<tt class="cfunction">calloc()</tt><a id='l2h-896' xml:id='l2h-896'></a>,
96<tt class="cfunction">realloc()</tt><a id='l2h-897' xml:id='l2h-897'></a> and
97<tt class="cfunction">free()</tt><a id='l2h-898' xml:id='l2h-898'></a>. This will result in
98mixed calls between the C allocator and the Python memory manager
99with fatal consequences, because they implement different algorithms
100and operate on different heaps. However, one may safely allocate and
101release memory blocks with the C library allocator for individual
102purposes, as shown in the following example:
103
104<P>
105<div class="verbatim"><pre>
106 PyObject *res;
107 char *buf = (char *) malloc(BUFSIZ); /* for I/O */
108
109 if (buf == NULL)
110 return PyErr_NoMemory();
111 ...Do some I/O operation involving buf...
112 res = PyString_FromString(buf);
113 free(buf); /* malloc'ed */
114 return res;
115</pre></div>
116
117<P>
118In this example, the memory request for the I/O buffer is handled by
119the C library allocator. The Python memory manager is involved only
120in the allocation of the string object returned as a result.
121
122<P>
123In most situations, however, it is recommended to allocate memory from
124the Python heap specifically because the latter is under control of
125the Python memory manager. For example, this is required when the
126interpreter is extended with new object types written in C. Another
127reason for using the Python heap is the desire to <em>inform</em> the
128Python memory manager about the memory needs of the extension module.
129Even when the requested memory is used exclusively for internal,
130highly-specific purposes, delegating all memory requests to the Python
131memory manager causes the interpreter to have a more accurate image of
132its memory footprint as a whole. Consequently, under certain
133circumstances, the Python memory manager may or may not trigger
134appropriate actions, like garbage collection, memory compaction or
135other preventive procedures. Note that by using the C library
136allocator as shown in the previous example, the allocated memory for
137the I/O buffer escapes completely the Python memory manager.
138
139<P>
140
141<DIV CLASS="navigation">
142<div class='online-navigation'>
143<p></p><hr />
144<table align="center" width="100%" cellpadding="0" cellspacing="2">
145<tr>
146<td class='online-navigation'><a rel="prev" title="9. Memory Management"
147 href="memory.html"><img src='../icons/previous.png'
148 border='0' height='32' alt='Previous Page' width='32' /></A></td>
149<td class='online-navigation'><a rel="parent" title="9. Memory Management"
150 href="memory.html"><img src='../icons/up.png'
151 border='0' height='32' alt='Up One Level' width='32' /></A></td>
152<td class='online-navigation'><a rel="next" title="9.2 Memory Interface"
153 href="memoryInterface.html"><img src='../icons/next.png'
154 border='0' height='32' alt='Next Page' width='32' /></A></td>
155<td align="center" width="100%">Python/C API Reference Manual</td>
156<td class='online-navigation'><a rel="contents" title="Table of Contents"
157 href="contents.html"><img src='../icons/contents.png'
158 border='0' height='32' alt='Contents' width='32' /></A></td>
159<td class='online-navigation'><img src='../icons/blank.png'
160 border='0' height='32' alt='' width='32' /></td>
161<td class='online-navigation'><a rel="index" title="Index"
162 href="genindex.html"><img src='../icons/index.png'
163 border='0' height='32' alt='Index' width='32' /></A></td>
164</tr></table>
165<div class='online-navigation'>
166<b class="navlabel">Previous:</b>
167<a class="sectref" rel="prev" href="memory.html">9. Memory Management</A>
168<b class="navlabel">Up:</b>
169<a class="sectref" rel="parent" href="memory.html">9. Memory Management</A>
170<b class="navlabel">Next:</b>
171<a class="sectref" rel="next" href="memoryInterface.html">9.2 Memory Interface</A>
172</div>
173</div>
174<hr />
175<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
176</DIV>
177<!--End of Navigation Panel-->
178<ADDRESS>
179See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
180</ADDRESS>
181</BODY>
182</HTML>