rt-thread/components/net/uip/doc/html/a00159.html

252 lines
16 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>uIP 1.0: Memory block management functions</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.6 -->
<div class="tabs">
<ul>
<li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="classes.html"><span>Data&nbsp;Structures</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul></div>
<h1>Memory block management functions</h1><hr><a name="_details"></a><h2>Detailed Description</h2>
The memory block allocation routines provide a simple yet powerful set of functions for managing a set of memory blocks of fixed size.
<p>
A set of memory blocks is statically declared with the <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a> macro. Memory blocks are allocated from the declared memory by the <a class="el" href="a00159.html#gfe5e93119035e14cc485760a176249ba">memb_alloc()</a> function, and are deallocated with the <a class="el" href="a00159.html#gceb952d27de8125d5146ac0bee325b8f">memb_free()</a> function.<p>
<dl compact><dt><b>Note:</b></dt><dd>Because of namespace clashes only one <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a> can be declared per C module, and the name scope of a <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a> memory block is local to each C module.</dd></dl>
The following example shows how to declare and use a memory block called "cmem" which has 8 chunks of memory with each memory chunk being 20 bytes large.
<p>
<table border="0" cellpadding="0" cellspacing="0">
<tr><td></td></tr>
<tr><td colspan="2"><br><h2>Files</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">file &nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00120.html">memb.c</a></td></tr>
<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Memory block allocation routines. <br></td></tr>
<p>
<tr><td class="memItemLeft" nowrap align="right" valign="top">file &nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00121.html">memb.h</a></td></tr>
<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Memory block allocation routines. <br></td></tr>
<p>
<tr><td colspan="2"><br><h2>Data Structures</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">struct &nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00081.html">memb_blocks</a></td></tr>
<tr><td colspan="2"><br><h2>Defines</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="ga680bc3f3a1a8a6aec20fe729d138cb8"></a><!-- doxytag: member="memb::MEMB_CONCAT2" ref="ga680bc3f3a1a8a6aec20fe729d138cb8" args="(s1, s2)" -->
#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#ga680bc3f3a1a8a6aec20fe729d138cb8">MEMB_CONCAT2</a>(s1, s2)&nbsp;&nbsp;&nbsp;s1##s2</td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="g720ac440c7b24bdd07c53ba146e36fb2"></a><!-- doxytag: member="memb::MEMB_CONCAT" ref="g720ac440c7b24bdd07c53ba146e36fb2" args="(s1, s2)" -->
#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(s1, s2)&nbsp;&nbsp;&nbsp;MEMB_CONCAT2(s1, s2)</td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB</a>(name, structure, num)</td></tr>
<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Declare a memory block. <a href="#gf31774d02a69fd3f1c2b282454438cba"></a><br></td></tr>
<tr><td colspan="2"><br><h2>Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#gd58a6c7e62ae59bf7a016ded12ca2910">memb_init</a> (struct <a class="el" href="a00081.html">memb_blocks</a> *m)</td></tr>
<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Initialize a memory block that was declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. <a href="#gd58a6c7e62ae59bf7a016ded12ca2910"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#gfe5e93119035e14cc485760a176249ba">memb_alloc</a> (struct <a class="el" href="a00081.html">memb_blocks</a> *m)</td></tr>
<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Allocate a memory block from a block of memory declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. <a href="#gfe5e93119035e14cc485760a176249ba"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">char&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#gceb952d27de8125d5146ac0bee325b8f">memb_free</a> (struct <a class="el" href="a00081.html">memb_blocks</a> *m, void *ptr)</td></tr>
<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Deallocate a memory block from a memory block previously declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. <a href="#gceb952d27de8125d5146ac0bee325b8f"></a><br></td></tr>
</table>
<hr><h2>Define Documentation</h2>
<a class="anchor" name="gf31774d02a69fd3f1c2b282454438cba"></a><!-- doxytag: member="memb.h::MEMB" ref="gf31774d02a69fd3f1c2b282454438cba" args="(name, structure, num)" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
<tr>
<td class="mdRow">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="md" nowrap valign="top">#define MEMB </td>
<td class="md" valign="top">(&nbsp;</td>
<td class="md" nowrap valign="top">name, <tr>
<td class="md" nowrap align="right"></td>
<td class="md"></td>
<td class="md" nowrap>structure, <tr>
<td class="md" nowrap align="right"></td>
<td class="md"></td>
<td class="md" nowrap>num&nbsp;</td>
<td class="mdname1" valign="top" nowrap> </td>
<td class="md" valign="top">&nbsp;)&nbsp;</td>
<td class="md" nowrap></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
<tr>
<td>
&nbsp;
</td>
<td>
<p>
<b>Value:</b><div class="fragment"><pre class="fragment"><span class="keyword">static</span> <span class="keywordtype">char</span> <a class="code" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(name,_memb_count)[num]; \
<span class="keyword">static</span> structure <a class="code" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(name,_memb_mem)[num]; \
<span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="a00081.html">memb_blocks</a> name = {<span class="keyword">sizeof</span>(structure), <a class="code" href="a00081.html#164124d48fe85bc98d9a300382a5245d">num</a>, \
<a class="code" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(name,_memb_count), \
(<span class="keywordtype">void</span> *)<a class="code" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(name,_memb_mem)}
</pre></div>Declare a memory block.
<p>
This macro is used to staticall declare a block of memory that can be used by the block allocation functions. The macro statically declares a C array with a size that matches the specified number of blocks and their individual sizes.<p>
Example: <div class="fragment"><pre class="fragment"><a class="code" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB</a>(connections, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> connection), 16);
</pre></div><p>
<dl compact><dt><b>Parameters:</b></dt><dd>
<table border="0" cellspacing="2" cellpadding="0">
<tr><td valign="top"></td><td valign="top"><em>name</em>&nbsp;</td><td>The name of the memory block (later used with <a class="el" href="a00159.html#gd58a6c7e62ae59bf7a016ded12ca2910">memb_init()</a>, <a class="el" href="a00159.html#gfe5e93119035e14cc485760a176249ba">memb_alloc()</a> and <a class="el" href="a00159.html#gceb952d27de8125d5146ac0bee325b8f">memb_free()</a>).</td></tr>
<tr><td valign="top"></td><td valign="top"><em>size</em>&nbsp;</td><td>The size of each memory chunk, in bytes.</td></tr>
<tr><td valign="top"></td><td valign="top"><em>num</em>&nbsp;</td><td>The total number of memory chunks in the block. </td></tr>
</table>
</dl>
<dl compact><dt><b>Examples: </b></dt><dd>
<a class="el" href="a00044.html#a206">telnetd.c</a>.</dl>
<p>
Definition at line <a class="el" href="a00187.html#l00098">98</a> of file <a class="el" href="a00187.html">memb.h</a>. </td>
</tr>
</table>
<hr><h2>Function Documentation</h2>
<a class="anchor" name="gfe5e93119035e14cc485760a176249ba"></a><!-- doxytag: member="memb.c::memb_alloc" ref="gfe5e93119035e14cc485760a176249ba" args="(struct memb_blocks *m)" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
<tr>
<td class="mdRow">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="md" nowrap valign="top">void * memb_alloc </td>
<td class="md" valign="top">(&nbsp;</td>
<td class="md" nowrap valign="top">struct <a class="el" href="a00081.html">memb_blocks</a> *&nbsp;</td>
<td class="mdname1" valign="top" nowrap> <em>m</em> </td>
<td class="md" valign="top">&nbsp;)&nbsp;</td>
<td class="md" nowrap></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
<tr>
<td>
&nbsp;
</td>
<td>
<p>
Allocate a memory block from a block of memory declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>.
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
<table border="0" cellspacing="2" cellpadding="0">
<tr><td valign="top"></td><td valign="top"><em>m</em>&nbsp;</td><td>A memory block previosly declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. </td></tr>
</table>
</dl>
<dl compact><dt><b>Examples: </b></dt><dd>
<a class="el" href="a00044.html#a208">telnetd.c</a>.</dl>
<p>
Definition at line <a class="el" href="a00186.html#l00059">59</a> of file <a class="el" href="a00186.html">memb.c</a>.
<p>
References <a class="el" href="a00187.html#l00108">memb_blocks::count</a>, <a class="el" href="a00187.html#l00109">memb_blocks::mem</a>, <a class="el" href="a00187.html#l00107">memb_blocks::num</a>, and <a class="el" href="a00187.html#l00106">memb_blocks::size</a>. </td>
</tr>
</table>
<a class="anchor" name="gceb952d27de8125d5146ac0bee325b8f"></a><!-- doxytag: member="memb.c::memb_free" ref="gceb952d27de8125d5146ac0bee325b8f" args="(struct memb_blocks *m, void *ptr)" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
<tr>
<td class="mdRow">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="md" nowrap valign="top">char memb_free </td>
<td class="md" valign="top">(&nbsp;</td>
<td class="md" nowrap valign="top">struct <a class="el" href="a00081.html">memb_blocks</a> *&nbsp;</td>
<td class="mdname" nowrap> <em>m</em>, </td>
</tr>
<tr>
<td class="md" nowrap align="right"></td>
<td class="md"></td>
<td class="md" nowrap>void *&nbsp;</td>
<td class="mdname" nowrap> <em>ptr</em></td>
</tr>
<tr>
<td class="md"></td>
<td class="md">)&nbsp;</td>
<td class="md" colspan="2"></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
<tr>
<td>
&nbsp;
</td>
<td>
<p>
Deallocate a memory block from a memory block previously declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>.
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
<table border="0" cellspacing="2" cellpadding="0">
<tr><td valign="top"></td><td valign="top"><em>m</em>&nbsp;</td><td>m A memory block previosly declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>.</td></tr>
<tr><td valign="top"></td><td valign="top"><em>ptr</em>&nbsp;</td><td>A pointer to the memory block that is to be deallocated.</td></tr>
</table>
</dl>
<dl compact><dt><b>Returns:</b></dt><dd>The new reference count for the memory block (should be 0 if successfully deallocated) or -1 if the pointer "ptr" did not point to a legal memory block. </dd></dl>
<dl compact><dt><b>Examples: </b></dt><dd>
<a class="el" href="a00044.html#a209">telnetd.c</a>.</dl>
<p>
Definition at line <a class="el" href="a00186.html#l00079">79</a> of file <a class="el" href="a00186.html">memb.c</a>.
<p>
References <a class="el" href="a00187.html#l00108">memb_blocks::count</a>, <a class="el" href="a00187.html#l00109">memb_blocks::mem</a>, and <a class="el" href="a00187.html#l00106">memb_blocks::size</a>. </td>
</tr>
</table>
<a class="anchor" name="gd58a6c7e62ae59bf7a016ded12ca2910"></a><!-- doxytag: member="memb.c::memb_init" ref="gd58a6c7e62ae59bf7a016ded12ca2910" args="(struct memb_blocks *m)" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
<tr>
<td class="mdRow">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="md" nowrap valign="top">void memb_init </td>
<td class="md" valign="top">(&nbsp;</td>
<td class="md" nowrap valign="top">struct <a class="el" href="a00081.html">memb_blocks</a> *&nbsp;</td>
<td class="mdname1" valign="top" nowrap> <em>m</em> </td>
<td class="md" valign="top">&nbsp;)&nbsp;</td>
<td class="md" nowrap></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
<tr>
<td>
&nbsp;
</td>
<td>
<p>
Initialize a memory block that was declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>.
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
<table border="0" cellspacing="2" cellpadding="0">
<tr><td valign="top"></td><td valign="top"><em>m</em>&nbsp;</td><td>A memory block previosly declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. </td></tr>
</table>
</dl>
<dl compact><dt><b>Examples: </b></dt><dd>
<a class="el" href="a00044.html#a220">telnetd.c</a>.</dl>
<p>
Definition at line <a class="el" href="a00186.html#l00052">52</a> of file <a class="el" href="a00186.html">memb.c</a>.
<p>
References <a class="el" href="a00187.html#l00108">memb_blocks::count</a>, <a class="el" href="a00187.html#l00109">memb_blocks::mem</a>, <a class="el" href="a00187.html#l00107">memb_blocks::num</a>, and <a class="el" href="a00187.html#l00106">memb_blocks::size</a>. </td>
</tr>
</table>
<hr size="1"><address style="align: right;"><small>Generated on Mon Jun 12 10:23:02 2006 for uIP 1.0 by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
</body>
</html>