summaryrefslogtreecommitdiff
path: root/ecos/packages/services/memalloc/common/current/doc/memalloc.sgml
blob: ef6fbeaf3e987128bd8f9ffd73d775cb4b6a93d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<!-- {{{ Banner                         -->

<!-- =============================================================== -->
<!--                                                                 -->
<!--     memalloc.sgml                                                  -->
<!--                                                                 -->
<!--     eCos memory allocation support                                          -->
<!--                                                                 -->
<!-- =============================================================== -->
<!-- ####COPYRIGHTBEGIN####                                          -->
<!--                                                                 -->
<!-- =============================================================== -->
<!-- Copyright (C) 2005 Andrew Lunn                                  -->
<!-- This material may be distributed only subject to the terms      -->
<!-- and conditions set forth in the Open Publication License, v1.0  -->
<!-- or later (the latest version is presently available at          -->
<!-- http://www.opencontent.org/openpub/)                            -->
<!-- Distribution of the work or derivative of the work in any       -->
<!-- standard (paper) book form is prohibited unless prior           -->
<!-- permission obtained from the copyright holder                   -->
<!-- =============================================================== -->
<!--                                                                 -->      
<!-- ####COPYRIGHTEND####                                            -->
<!-- =============================================================== -->
<!-- #####DESCRIPTIONBEGIN####                                       -->
<!-- This explains the memory allocation functions which were        -->
<!-- origionally part of the KAPI, but there moved a long long time  -->
<!-- ago. I used the old documentation as a basis, so credit should  -->
<!-- realy go to somebody else                                       -->
<!-- ####DESCRIPTIONEND####                                          -->
<!-- =============================================================== -->

<!-- }}} -->

<PART id="services-memalloc-common">
<TITLE>Memory Allocation</TITLE>
<CHAPTER id="memalloc">
<TITLE>eCos Memory Pools</TITLE>
<SECT1 id="ecos-memory-pools">
<TITLE>eCos Memory pools</TITLE>

<PARA>
  There are three sorts of memory pools. A variable size memory pool
  is for allocating blocks of any size. A fixed size memory pool, has
  the block size specified when the pool is created and only provides
  blocks of that size. Both of these pools must be explicitly created.
  The third type is the traditional heap which can be accessed using
  malloc and friends.</PARA>

  <refentry id="variable-pools">
  
    <refmeta>
    <refentrytitle>Variable Size Allocation Pools</refentrytitle>
    </refmeta>

      <refnamediv>
      <refname>cyg_mempool_var_create</refname>
      <refname>cyg_mempool_var_delete</refname>
      <refname>cyg_mempool_var_alloc</refname>
      <refname>cyg_mempool_var_timed_alloc</refname>
      <refname>cyg_mempool_var_try_alloc</refname>
      <refname>cyg_mempool_var_free</refname>
      <refname>cyg_mempool_var_waiting</refname>
      <refname>cyg_mempool_var_get_info</refname>
      <refpurpose>Variable Size Allocation Pools</refpurpose>
    </refnamediv>

    <refsynopsisdiv>
      <funcsynopsis>
        <funcsynopsisinfo>
#include &lt;cyg/kernel/kapi.h&gt;
        </funcsynopsisinfo>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_var_create</function></funcdef>
          <paramdef>void* <parameter>base</parameter></paramdef>
          <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
          <paramdef>cyg_handle_t* <parameter>varpool</parameter></paramdef>
          <paramdef>cyg_mempool_var* <parameter>var</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_var_delete</function></funcdef>
          <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void* <function>cyg_mempool_var_alloc</function></funcdef>
          <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
          <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void* <function>cyg_mempool_var_timed_alloc</function></funcdef>
          <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
          <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
          <paramdef>cyg_tick_count_t <parameter>abstime</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void* <function>cyg_mempool_var_try_alloc</function></funcdef>
          <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
          <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_var_free</function></funcdef>
          <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
          <paramdef>void* <parameter>p</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>cyg_bool_t <function>cyg_mempool_var_waiting</function></funcdef>
          <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_var_get_info</function></funcdef>
          <paramdef>cyg_handle_t <parameter>varpool</parameter></paramdef>
          <paramdef>cyg_mempool_info* <parameter>info</parameter></paramdef>
        </funcprototype>
      </funcsynopsis>
    </refsynopsisdiv>

    <refsect1 id="memalloc-variable-description"><title>Description</title>

      <para>
      The variable size memory pool functions are used for allocating
      blocks of any size. Before memory can be allocated the pool must
      first be created by calling
      <function>cyg_mempool_var_create()</function>. The parameter
      <parameter>base</parameter> is a point to the bottom of the
      memory area to be used by the pool and
      <parameter>size</parameter> is the size of the memory area in
      bytes. It also takes a pointer to a
      <structname>cyg_mempool_var</structname> data structure which is
      typically statically allocated, and may be part of a larger data
      structure. It should be noted that some memory is take from the
      pool for book keeping purposes. If a memory pool is no longer
      required and there are not threads waiting to allocate memory
      from it, it can be destroyed with
      <function>cyg_mempool_var_delete()</function>.  
      </para>

      <para>
      Memory can be allocated from the pool using a number of
      functions. They all take the
      paramters<parameter>varpool</parameter>which indicates which
      pool should be used and the <parameter>size</parameter>which
      indicates who big a memory area should be
      allocated. <function>cyg_mempool_var_alloc()</function> will
      block until the memory becomes
      available. <function>cyg_mempool_tryalloc()</function>will try
      not block if no memory is available and will return
      <literal>NULL</literal>. Otherwise a pointer to the allocated
      memory will be returned.
      <function>cyg_mempool_var_timed_alloc()</function> will block if
      memory is not available and wait for memory to become available
      until the time<parameter>abstime</parameter> is reached. It will
      then return <literal>NULL</literal>.
      </para>

      <para>
      Allocated memory can be freed using the function <function>cyg_mempool_var_free()</function>.
      </para>
      
      <para>
      Lastly it is possible to query information about the pool using
      the function
      <function>cyg_mempool_var_get_info()</function>. This takes a
      pointer to the structure <structname>cyg_mempool_info</structname> which is:
      <programlisting>
typedef struct {
    cyg_int32 totalmem;
    cyg_int32 freemem;
    void      *base;
    cyg_int32 size;
    cyg_int32 blocksize;
    cyg_int32 maxfree;
} cyg_mempool_info;
      </programlisting>
     </para>
   </refsect1> 
  </refentry>
  <refentry id="fixed-pools">
  
    <refmeta>
    <refentrytitle>Fixed Size Allocation Pools</refentrytitle>
    </refmeta>

      <refnamediv>
      <refname>cyg_mempool_fix_create</refname>
      <refname>cyg_mempool_fix_delete</refname>
      <refname>cyg_mempool_fix_alloc</refname>
      <refname>cyg_mempool_fix_timed_alloc</refname>
      <refname>cyg_mempool_fix_try_alloc</refname>
      <refname>cyg_mempool_fix_free</refname>
      <refname>cyg_mempool_fix_waiting</refname>
      <refname>cyg_mempool_fix_get_info</refname>
      <refpurpose>Fixed Size Allocation Pools</refpurpose>
    </refnamediv>

    <refsynopsisdiv>
      <funcsynopsis>
        <funcsynopsisinfo>
#include &lt;cyg/kernel/kapi.h&gt;
        </funcsynopsisinfo>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_fix_create</function></funcdef>
          <paramdef>void* <parameter>base</parameter></paramdef>
          <paramdef>cyg_uint32 <parameter>size</parameter></paramdef>
          <paramdef>cyg_uint32 <parameter>blocksize</parameter></paramdef>
          <paramdef>cyg_handle_t* <parameter>fixpool</parameter></paramdef>
          <paramdef>cyg_mempool_var* <parameter>var</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_fix_delete</function></funcdef>
          <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void* <function>cyg_mempool_fix_alloc</function></funcdef>
          <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void* <function>cyg_mempool_fix_timed_alloc</function></funcdef>
          <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
          <paramdef>cyg_tick_count_t <parameter>abstime</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void* <function>cyg_mempool_fix_try_alloc</function></funcdef>
          <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_fix_free</function></funcdef>
          <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
          <paramdef>void* <parameter>p</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>cyg_bool_t <function>cyg_mempool_fix_waiting</function></funcdef>
          <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void <function>cyg_mempool_fix_get_info</function></funcdef>
          <paramdef>cyg_handle_t <parameter>fixpool</parameter></paramdef>
          <paramdef>cyg_mempool_info* <parameter>info</parameter></paramdef>
        </funcprototype>
      </funcsynopsis>
    </refsynopsisdiv>

    <refsect1 id="memalloc-fixed-description"><title>Description</title>

      <para>
      The fixed size memory pool functions are used for allocating
      blocks of the same size. The allocation and free functions are
      more efficient than the variable size pools, but are naturally
      limited to being only able to allocate blocks of a sized
      size. Before memory can be allocated the pool must first be
      created by calling
      <function>cyg_mempool_fix_create()</function>.  

      The parameter <parameter>base</parameter> is a point to the
      bottom of the memory area to be used by the pool and
      <parameter>size</parameter> is the size of the memory area in
      bytes. <parameter>blocksize</parameter> indicates the size of
      each allocation in bytes. The function also takes a pointer to a
      <structname>cyg_mempool_fix</structname> data structure which is
      typically statically allocated, and may be part of a larger data
      structure. It should be noted that some memory is take from the
      pool for book keeping purposes. If a memory pool is no longer
      required and there are not threads waiting to allocate memory
      from it, it can be destroyed with
      <function>cyg_mempool_fix_delete()</function>.  
      </para>

      <para>
      Memory can be allocated from the pool using a number of
      functions. They all take the
      paramter<parameter>fixpool</parameter>which indicates which pool
      should be used. <function>cyg_mempool_fix_alloc()</function>
      will block until the memory becomes
      available. <function>cyg_mempool_tryalloc()</function>will try
      not block if no memory is available and will return
      <literal>NULL</literal>. Otherwise a pointer to the allocated
      memory will be returned.
      <function>cyg_mempool_fix_timed_alloc()</function> will block if
      memory is not available and wait for memory to become available
      until the time<parameter>abstime</parameter> is reached. It will
      then return <literal>NULL</literal>.  </para>

      <para>
      Allocated memory can be freed using the function <function>cyg_mempool_fix_free()</function>.
      </para>
      
      <para>
      Lastly it is possible to query information about the pool using
      the function
      <function>cyg_mempool_fix_get_info()</function>. This takes a
      pointer to the structure <structname>cyg_mempool_info</structname> which is:
      <programlisting>
typedef struct {
    cyg_int32 totalmem;
    cyg_int32 freemem;
    void      *base;
    cyg_int32 size;
    cyg_int32 blocksize;
    cyg_int32 maxfree;
} cyg_mempool_info;
      </programlisting>
     </para>
   </refsect1> 
 </refentry>

 <refentry id="malloc-pools">
 
    <refmeta>
    <refentrytitle>stdlib malloc Pools</refentrytitle>
    </refmeta>

      <refnamediv>
      <refname>malloc</refname>
      <refname>calloc</refname>
      <refname>free</refname>
      <refname>realloc</refname>
      <refname>mallinfo</refname>
      <refpurpose>stdlib malloc pool</refpurpose>
      </refnamediv>

    <refsynopsisdiv>
      <funcsynopsis>
        <funcsynopsisinfo>
#include &lt;stdlib.h&gt;
        </funcsynopsisinfo>
        <funcprototype>
          <funcdef>void *<function>malloc</function></funcdef>
          <paramdef>size_t <parameter>size</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void *<function>calloc</function></funcdef>
          <paramdef>size_t <parameter>size</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void <function>free</function></funcdef>
          <paramdef>void *<parameter>ptr</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>void *<function>realloc</function></funcdef>
          <paramdef>void *<parameter>ptr</parameter></paramdef>
          <paramdef>size_t <parameter>size</parameter></paramdef>
        </funcprototype>
        <funcprototype>
          <funcdef>struct mallinfo <function>mallinfo</function></funcdef>
          <void>
        </funcprototype>
       </funcsynopsis>
    </refsynopsisdiv>

    <refsect1 id="memalloc-stdlib-description"><title>Description</title>

      <para>
      eCos provides the standard library functions used for allocating
      memory from the heap. <function>malloc()</function> allocates a
      block of memory of
      <parameter>size</parameter> bytes. <function>calloc()</function>
      performs the same, but also sets the memory to zero. The
      function <function>free()</function> returns a block to the
      pool. <function>realloc</function> resizes a block of
      memory. Lastly, <function>mallinfo()</function> returns
      information about the heap, as described by the structure
      <structname>mallinfo</structname>:

      <programlisting>
struct mallinfo {
    int arena;    /* total size of memory arena */
    int ordblks;  /* number of ordinary memory blocks */
    int smblks;   /* number of small memory blocks */
    int hblks;    /* number of mmapped regions */
    int hblkhd;   /* total space in mmapped regions */
    int usmblks;  /* space used by small memory blocks */
    int fsmblks;  /* space available for small memory blocks */
    int uordblks; /* space used by ordinary memory blocks */
    int fordblks; /* space free for ordinary blocks */
    int keepcost; /* top-most, releasable (via malloc_trim) space */
    int maxfree;  /* (NON-STANDARD EXTENSION) size of largest free block */
};
      </programlisting>
     </para>
   </refsect1> 
 </refentry>
</SECT1>
</CHAPTER> 
</PART>