]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/services/memalloc/common/v2_0/doc/memalloc.sgml
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / services / memalloc / common / v2_0 / doc / memalloc.sgml
1 <!-- {{{ Banner                         -->
2
3 <!-- =============================================================== -->
4 <!--                                                                 -->
5 <!--     memalloc.sgml                                                  -->
6 <!--                                                                 -->
7 <!--     eCos memory allocation support                                          -->
8 <!--                                                                 -->
9 <!-- =============================================================== -->
10 <!-- ####COPYRIGHTBEGIN####                                          -->
11 <!--                                                                 -->
12 <!-- =============================================================== -->
13 <!-- Copyright (C) 2005 Andrew Lunn                                  -->
14 <!-- This material may be distributed only subject to the terms      -->
15 <!-- and conditions set forth in the Open Publication License, v1.0  -->
16 <!-- or later (the latest version is presently available at          -->
17 <!-- http://www.opencontent.org/openpub/)                            -->
18 <!-- Distribution of the work or derivative of the work in any       -->
19 <!-- standard (paper) book form is prohibited unless prior           -->
20 <!-- permission obtained from the copyright holder                   -->
21 <!-- =============================================================== -->
22 <!--                                                                 -->      
23 <!-- ####COPYRIGHTEND####                                            -->
24 <!-- =============================================================== -->
25 <!-- #####DESCRIPTIONBEGIN####                                       -->
26 <!-- This explains the memory allocation functions which were        -->
27 <!-- origionally part of the KAPI, but there moved a long long time  -->
28 <!-- ago. I used the old documentation as a basis, so credit should  -->
29 <!-- realy go to somebody else                                       -->
30 <!-- ####DESCRIPTIONEND####                                          -->
31 <!-- =============================================================== -->
32
33 <!-- }}} -->
34
35 <PART id="services-memalloc-common">
36 <TITLE>Memory Allocation</TITLE>
37 <CHAPTER id="memalloc">
38 <TITLE>eCos Memory Pools</TITLE>
39 <SECT1 id="ecos-memory-pools">
40 <TITLE>eCos Memory pools</TITLE>
41
42 <PARA>
43   There are three sorts of memory pools. A variable size memory pool
44   is for allocating blocks of any size. A fixed size memory pool, has
45   the block size specified when the pool is created and only provides
46   blocks of that size. Both of these pools must be explicitly created.
47   The third type is the traditional heap which can be accessed using
48   malloc and friends.</PARA>
49
50   <refentry id="variable-pools">
51   
52     <refmeta>
53     <refentrytitle>Variable Size Allocation Pools</refentrytitle>
54     </refmeta>
55
56       <refnamediv>
57       <refname>cyg_mempool_var_create</refname>
58       <refname>cyg_mempool_var_delete</refname>
59       <refname>cyg_mempool_var_alloc</refname>
60       <refname>cyg_mempool_var_timed_alloc</refname>
61       <refname>cyg_mempool_var_try_alloc</refname>
62       <refname>cyg_mempool_var_free</refname>
63       <refname>cyg_mempool_var_waiting</refname>
64       <refname>cyg_mempool_var_get_info</refname>
65       <refpurpose>Variable Size Allocation Pools</refpurpose>
66     </refnamediv>
67
68     <refsynopsisdiv>
69       <funcsynopsis>
70         <funcsynopsisinfo>
71 #include &lt;cyg/kernel/kapi.h&gt;
72         </funcsynopsisinfo>
73         <funcprototype>
74           <funcdef>void <function>cyg_mempool_var_create</function></funcdef>
75           <paramdef>void* <parameter>base</parameter></paramdef>
76           <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
77           <paramdef>cyg_handle_t* <parameter>varpool</parameter></paramdef>
78           <paramdef>cyg_mempool_var* <parameter>var</parameter></paramdef>
79         </funcprototype>
80         <funcprototype>
81           <funcdef>void <function>cyg_mempool_var_delete</function></funcdef>
82           <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
83         </funcprototype>
84         <funcprototype>
85           <funcdef>void* <function>cyg_mempool_var_alloc</function></funcdef>
86           <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
87           <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
88         </funcprototype>
89         <funcprototype>
90           <funcdef>void* <function>cyg_mempool_var_timed_alloc</function></funcdef>
91           <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
92           <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
93           <paramdef>cyg_tick_count_t <parameter>abstime</parameter></paramdef>
94         </funcprototype>
95         <funcprototype>
96           <funcdef>void* <function>cyg_mempool_var_try_alloc</function></funcdef>
97           <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
98           <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
99         </funcprototype>
100         <funcprototype>
101           <funcdef>void <function>cyg_mempool_var_free</function></funcdef>
102           <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
103           <paramdef>void* <parameter>p</parameter></paramdef>
104         </funcprototype>
105         <funcprototype>
106           <funcdef>cyg_bool_t <function>cyg_mempool_var_waiting</function></funcdef>
107           <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
108         </funcprototype>
109         <funcprototype>
110           <funcdef>void <function>cyg_mempool_var_get_info</function></funcdef>
111           <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
112           <paramdef>cyg_mempool_info* <parameter>info</parameter></paramdef>
113         </funcprototype>
114       </funcsynopsis>
115     </refsynopsisdiv>
116
117     <refsect1 id="memalloc-variable-description"><title>Description</title>
118
119       <para>
120       The variable size memory pool functions are used for allocating
121       blocks of any size. Before memory can be allocated the pool must
122       first be created by calling
123       <function>cyg_mempool_var_create()</function>. The parameter
124       <parameter>base</parameter> is a point to the bottom of the
125       memory area to be used by the pool and
126       <parameter>size</parameter> is the size of the memory area in
127       bytes. It also takes a pointer to a
128       <structname>cyg_mempool_var</structname> data structure which is
129       typically statically allocated, and may be part of a larger data
130       structure. It should be noted that some memory is take from the
131       pool for book keeping purposes. If a memory pool is no longer
132       required and there are not threads waiting to allocate memory
133       from it, it can be destroyed with
134       <function>cyg_mempool_var_delete()</function>.  
135       </para>
136
137       <para>
138       Memory can be allocated from the pool using a number of
139       functions. They all take the
140       paramters<parameter>varpool</parameter>which indicates which
141       pool should be used and the <parameter>size</parameter>which
142       indicates who big a memory area should be
143       allocated. <function>cyg_mempool_var_alloc()</function> will
144       block until the memory becomes
145       available. <function>cyg_mempool_tryalloc()</function>will try
146       not block if no memory is available and will return
147       <literal>NULL</literal>. Otherwise a pointer to the allocated
148       memory will be returned.
149       <function>cyg_mempool_var_timed_alloc()</function> will block if
150       memory is not available and wait for memory to become available
151       until the time<parameter>abstime</parameter> is reached. It will
152       then return <literal>NULL</literal>.
153       </para>
154
155       <para>
156       Allocated memory can be freed using the function <function>cyg_mempool_var_free()</function>.
157       </para>
158       
159       <para>
160       Lastly it is possible to query information about the pool using
161       the function
162       <function>cyg_mempool_var_get_info()</function>. This takes a
163       pointer to the structure <structname>cyg_mempool_info</structname> which is:
164       <programlisting>
165 typedef struct {
166     cyg_int32 totalmem;
167     cyg_int32 freemem;
168     void      *base;
169     cyg_int32 size;
170     cyg_int32 blocksize;
171     cyg_int32 maxfree;
172 } cyg_mempool_info;
173       </programlisting>
174      </para>
175    </refsect1> 
176   </refentry>
177   <refentry id="fixed-pools">
178   
179     <refmeta>
180     <refentrytitle>Fixed Size Allocation Pools</refentrytitle>
181     </refmeta>
182
183       <refnamediv>
184       <refname>cyg_mempool_fix_create</refname>
185       <refname>cyg_mempool_fix_delete</refname>
186       <refname>cyg_mempool_fix_alloc</refname>
187       <refname>cyg_mempool_fix_timed_alloc</refname>
188       <refname>cyg_mempool_fix_try_alloc</refname>
189       <refname>cyg_mempool_fix_free</refname>
190       <refname>cyg_mempool_fix_waiting</refname>
191       <refname>cyg_mempool_fix_get_info</refname>
192       <refpurpose>Fixed Size Allocation Pools</refpurpose>
193     </refnamediv>
194
195     <refsynopsisdiv>
196       <funcsynopsis>
197         <funcsynopsisinfo>
198 #include &lt;cyg/kernel/kapi.h&gt;
199         </funcsynopsisinfo>
200         <funcprototype>
201           <funcdef>void <function>cyg_mempool_fix_create</function></funcdef>
202           <paramdef>void* <parameter>base</parameter></paramdef>
203           <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
204           <paramdef>cyg_uint32 <parameter>blocksize</parameter></paramdef>
205           <paramdef>cyg_handle_t* <parameter>fixpool</parameter></paramdef>
206           <paramdef>cyg_mempool_fix* <parameter>fix</parameter></paramdef>
207         </funcprototype>
208         <funcprototype>
209           <funcdef>void <function>cyg_mempool_fix_delete</function></funcdef>
210           <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
211         </funcprototype>
212         <funcprototype>
213           <funcdef>void* <function>cyg_mempool_fix_alloc</function></funcdef>
214           <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
215         </funcprototype>
216         <funcprototype>
217           <funcdef>void* <function>cyg_mempool_fix_timed_alloc</function></funcdef>
218           <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
219           <paramdef>cyg_tick_count_t <parameter>abstime</parameter></paramdef>
220         </funcprototype>
221         <funcprototype>
222           <funcdef>void* <function>cyg_mempool_fix_try_alloc</function></funcdef>
223           <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
224         </funcprototype>
225         <funcprototype>
226           <funcdef>void <function>cyg_mempool_fix_free</function></funcdef>
227           <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
228           <paramdef>void* <parameter>p</parameter></paramdef>
229         </funcprototype>
230         <funcprototype>
231           <funcdef>cyg_bool_t <function>cyg_mempool_fix_waiting</function></funcdef>
232           <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
233         </funcprototype>
234         <funcprototype>
235           <funcdef>void <function>cyg_mempool_fix_get_info</function></funcdef>
236           <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
237           <paramdef>cyg_mempool_info* <parameter>info</parameter></paramdef>
238         </funcprototype>
239       </funcsynopsis>
240     </refsynopsisdiv>
241
242     <refsect1 id="memalloc-fixed-description"><title>Description</title>
243
244       <para>
245       The fixed size memory pool functions are used for allocating
246       blocks of the same size. The allocation and free functions are
247       more efficient than the variable size pools, but are naturally
248       limited to being only able to allocate blocks of a sized
249       size. Before memory can be allocated the pool must first be
250       created by calling
251       <function>cyg_mempool_fix_create()</function>.  
252
253       The parameter <parameter>base</parameter> is a point to the
254       bottom of the memory area to be used by the pool and
255       <parameter>size</parameter> is the size of the memory area in
256       bytes. <parameter>blocksize</parameter> indicates the size of
257       each allocation in bytes. The function also takes a pointer to a
258       <structname>cyg_mempool_fix</structname> data structure which is
259       typically statically allocated, and may be part of a larger data
260       structure. It should be noted that some memory is take from the
261       pool for book keeping purposes. If a memory pool is no longer
262       required and there are not threads waiting to allocate memory
263       from it, it can be destroyed with
264       <function>cyg_mempool_fix_delete()</function>.  
265       </para>
266
267       <para>
268       Memory can be allocated from the pool using a number of
269       functions. They all take the
270       paramter<parameter>fixpool</parameter>which indicates which pool
271       should be used. <function>cyg_mempool_fix_alloc()</function>
272       will block until the memory becomes
273       available. <function>cyg_mempool_tryalloc()</function>will try
274       not block if no memory is available and will return
275       <literal>NULL</literal>. Otherwise a pointer to the allocated
276       memory will be returned.
277       <function>cyg_mempool_fix_timed_alloc()</function> will block if
278       memory is not available and wait for memory to become available
279       until the time<parameter>abstime</parameter> is reached. It will
280       then return <literal>NULL</literal>.  </para>
281
282       <para>
283       Allocated memory can be freed using the function <function>cyg_mempool_fix_free()</function>.
284       </para>
285       
286       <para>
287       Lastly it is possible to query information about the pool using
288       the function
289       <function>cyg_mempool_fix_get_info()</function>. This takes a
290       pointer to the structure <structname>cyg_mempool_info</structname> which is:
291       <programlisting>
292 typedef struct {
293     cyg_int32 totalmem;
294     cyg_int32 freemem;
295     void      *base;
296     cyg_int32 size;
297     cyg_int32 blocksize;
298     cyg_int32 maxfree;
299 } cyg_mempool_info;
300       </programlisting>
301      </para>
302    </refsect1> 
303  </refentry>
304
305  <refentry id="malloc-pools">
306  
307     <refmeta>
308     <refentrytitle>stdlib malloc Pools</refentrytitle>
309     </refmeta>
310
311       <refnamediv>
312       <refname>malloc</refname>
313       <refname>calloc</refname>
314       <refname>free</refname>
315       <refname>realloc</refname>
316       <refname>mallinfo</refname>
317       <refpurpose>stdlib malloc pool</refpurpose>
318       </refnamediv>
319
320     <refsynopsisdiv>
321       <funcsynopsis>
322         <funcsynopsisinfo>
323 #include &lt;stdlib.h&gt;
324         </funcsynopsisinfo>
325         <funcprototype>
326           <funcdef>void *<function>malloc</function></funcdef>
327           <paramdef>size_t <parameter>size</parameter></paramdef>
328         </funcprototype>
329         <funcprototype>
330           <funcdef>void *<function>calloc</function></funcdef>
331           <paramdef>size_t <parameter>nmemb</parameter></paramdef>
332           <paramdef>size_t <parameter>size</parameter></paramdef>
333         </funcprototype>
334         <funcprototype>
335           <funcdef>void <function>free</function></funcdef>
336           <paramdef>void *<parameter>ptr</parameter></paramdef>
337         </funcprototype>
338         <funcprototype>
339           <funcdef>void *<function>realloc</function></funcdef>
340           <paramdef>void *<parameter>ptr</parameter></paramdef>
341           <paramdef>size_t <parameter>size</parameter></paramdef>
342         </funcprototype>
343         <funcprototype>
344           <funcdef>struct mallinfo <function>mallinfo</function></funcdef>
345           <void>
346         </funcprototype>
347        </funcsynopsis>
348     </refsynopsisdiv>
349
350     <refsect1 id="memalloc-stdlib-description"><title>Description</title>
351
352       <para>
353       eCos provides the standard library functions used for allocating
354       memory from the heap. <function>malloc()</function> allocates a
355       block of memory of
356       <parameter>size</parameter> bytes. <function>calloc()</function>
357       performs the same, but also sets the memory to zero. The
358       function <function>free()</function> returns a block to the
359       pool. <function>realloc</function> resizes a block of
360       memory. Lastly, <function>mallinfo()</function> returns
361       information about the heap, as described by the structure
362       <structname>mallinfo</structname>:
363
364       <programlisting>
365 struct mallinfo {
366     int arena;    /* total size of memory arena */
367     int ordblks;  /* number of ordinary memory blocks */
368     int smblks;   /* number of small memory blocks */
369     int hblks;    /* number of mmapped regions */
370     int hblkhd;   /* total space in mmapped regions */
371     int usmblks;  /* space used by small memory blocks */
372     int fsmblks;  /* space available for small memory blocks */
373     int uordblks; /* space used by ordinary memory blocks */
374     int fordblks; /* space free for ordinary blocks */
375     int keepcost; /* top-most, releasable (via malloc_trim) space */
376     int maxfree;  /* (NON-STANDARD EXTENSION) size of largest free block */
377 };
378       </programlisting>
379      </para>
380    </refsect1> 
381  </refentry>
382 </SECT1>
383 </CHAPTER> 
384 </PART>