1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> 3<title>uIP 1.0: Memory block management functions</title> 4<link href="doxygen.css" rel="stylesheet" type="text/css"> 5<link href="tabs.css" rel="stylesheet" type="text/css"> 6</head><body> 7<!-- Generated by Doxygen 1.4.6 --> 8<div class="tabs"> 9 <ul> 10 <li><a href="main.html"><span>Main Page</span></a></li> 11 <li><a href="modules.html"><span>Modules</span></a></li> 12 <li><a href="classes.html"><span>Data Structures</span></a></li> 13 <li><a href="files.html"><span>Files</span></a></li> 14 <li><a href="examples.html"><span>Examples</span></a></li> 15 </ul></div> 16<h1>Memory block management functions</h1><hr><a name="_details"></a><h2>Detailed Description</h2> 17The memory block allocation routines provide a simple yet powerful set of functions for managing a set of memory blocks of fixed size. 18<p> 19A 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> 20<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> 21The 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. 22<p> 23<table border="0" cellpadding="0" cellspacing="0"> 24<tr><td></td></tr> 25<tr><td colspan="2"><br><h2>Files</h2></td></tr> 26<tr><td class="memItemLeft" nowrap align="right" valign="top">file </td><td class="memItemRight" valign="bottom"><a class="el" href="a00120.html">memb.c</a></td></tr> 27 28<tr><td class="mdescLeft"> </td><td class="mdescRight">Memory block allocation routines. <br></td></tr> 29 30<p> 31<tr><td class="memItemLeft" nowrap align="right" valign="top">file </td><td class="memItemRight" valign="bottom"><a class="el" href="a00121.html">memb.h</a></td></tr> 32 33<tr><td class="mdescLeft"> </td><td class="mdescRight">Memory block allocation routines. <br></td></tr> 34 35<p> 36<tr><td colspan="2"><br><h2>Data Structures</h2></td></tr> 37<tr><td class="memItemLeft" nowrap align="right" valign="top">struct </td><td class="memItemRight" valign="bottom"><a class="el" href="a00081.html">memb_blocks</a></td></tr> 38 39<tr><td colspan="2"><br><h2>Defines</h2></td></tr> 40<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)" --> 41#define </td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#ga680bc3f3a1a8a6aec20fe729d138cb8">MEMB_CONCAT2</a>(s1, s2) s1##s2</td></tr> 42 43<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)" --> 44#define </td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(s1, s2) MEMB_CONCAT2(s1, s2)</td></tr> 45 46<tr><td class="memItemLeft" nowrap align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB</a>(name, structure, num)</td></tr> 47 48<tr><td class="mdescLeft"> </td><td class="mdescRight">Declare a memory block. <a href="#gf31774d02a69fd3f1c2b282454438cba"></a><br></td></tr> 49<tr><td colspan="2"><br><h2>Functions</h2></td></tr> 50<tr><td class="memItemLeft" nowrap align="right" valign="top">void </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> 51 52<tr><td class="mdescLeft"> </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> 53<tr><td class="memItemLeft" nowrap align="right" valign="top">void * </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> 54 55<tr><td class="mdescLeft"> </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> 56<tr><td class="memItemLeft" nowrap align="right" valign="top">char </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> 57 58<tr><td class="mdescLeft"> </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> 59</table> 60<hr><h2>Define Documentation</h2> 61<a class="anchor" name="gf31774d02a69fd3f1c2b282454438cba"></a><!-- doxytag: member="memb.h::MEMB" ref="gf31774d02a69fd3f1c2b282454438cba" args="(name, structure, num)" --><p> 62<table class="mdTable" cellpadding="2" cellspacing="0"> 63 <tr> 64 <td class="mdRow"> 65 <table cellpadding="0" cellspacing="0" border="0"> 66 <tr> 67 <td class="md" nowrap valign="top">#define MEMB </td> 68 <td class="md" valign="top">( </td> 69 <td class="md" nowrap valign="top">name, <tr> 70 <td class="md" nowrap align="right"></td> 71 <td class="md"></td> 72 <td class="md" nowrap>structure, <tr> 73 <td class="md" nowrap align="right"></td> 74 <td class="md"></td> 75 <td class="md" nowrap>num </td> 76 <td class="mdname1" valign="top" nowrap> </td> 77 <td class="md" valign="top"> ) </td> 78 <td class="md" nowrap></td> 79 </tr> 80 </table> 81 </td> 82 </tr> 83</table> 84<table cellspacing="5" cellpadding="0" border="0"> 85 <tr> 86 <td> 87 88 </td> 89 <td> 90 91<p> 92<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]; \ 93 <span class="keyword">static</span> structure <a class="code" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(name,_memb_mem)[num]; \ 94 <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>, \ 95 <a class="code" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(name,_memb_count), \ 96 (<span class="keywordtype">void</span> *)<a class="code" href="a00159.html#g720ac440c7b24bdd07c53ba146e36fb2">MEMB_CONCAT</a>(name,_memb_mem)} 97</pre></div>Declare a memory block. 98<p> 99This 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> 100Example: <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); 101</pre></div><p> 102<dl compact><dt><b>Parameters:</b></dt><dd> 103 <table border="0" cellspacing="2" cellpadding="0"> 104 <tr><td valign="top"></td><td valign="top"><em>name</em> </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> 105 <tr><td valign="top"></td><td valign="top"><em>size</em> </td><td>The size of each memory chunk, in bytes.</td></tr> 106 <tr><td valign="top"></td><td valign="top"><em>num</em> </td><td>The total number of memory chunks in the block. </td></tr> 107 </table> 108</dl> 109<dl compact><dt><b>Examples: </b></dt><dd> 110<a class="el" href="a00044.html#a206">telnetd.c</a>.</dl> 111<p> 112Definition at line <a class="el" href="a00187.html#l00098">98</a> of file <a class="el" href="a00187.html">memb.h</a>. </td> 113 </tr> 114</table> 115<hr><h2>Function Documentation</h2> 116<a class="anchor" name="gfe5e93119035e14cc485760a176249ba"></a><!-- doxytag: member="memb.c::memb_alloc" ref="gfe5e93119035e14cc485760a176249ba" args="(struct memb_blocks *m)" --><p> 117<table class="mdTable" cellpadding="2" cellspacing="0"> 118 <tr> 119 <td class="mdRow"> 120 <table cellpadding="0" cellspacing="0" border="0"> 121 <tr> 122 <td class="md" nowrap valign="top">void * memb_alloc </td> 123 <td class="md" valign="top">( </td> 124 <td class="md" nowrap valign="top">struct <a class="el" href="a00081.html">memb_blocks</a> * </td> 125 <td class="mdname1" valign="top" nowrap> <em>m</em> </td> 126 <td class="md" valign="top"> ) </td> 127 <td class="md" nowrap></td> 128 </tr> 129 </table> 130 </td> 131 </tr> 132</table> 133<table cellspacing="5" cellpadding="0" border="0"> 134 <tr> 135 <td> 136 137 </td> 138 <td> 139 140<p> 141Allocate a memory block from a block of memory declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. 142<p> 143<dl compact><dt><b>Parameters:</b></dt><dd> 144 <table border="0" cellspacing="2" cellpadding="0"> 145 <tr><td valign="top"></td><td valign="top"><em>m</em> </td><td>A memory block previosly declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. </td></tr> 146 </table> 147</dl> 148<dl compact><dt><b>Examples: </b></dt><dd> 149<a class="el" href="a00044.html#a208">telnetd.c</a>.</dl> 150<p> 151Definition at line <a class="el" href="a00186.html#l00059">59</a> of file <a class="el" href="a00186.html">memb.c</a>. 152<p> 153References <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> 154 </tr> 155</table> 156<a class="anchor" name="gceb952d27de8125d5146ac0bee325b8f"></a><!-- doxytag: member="memb.c::memb_free" ref="gceb952d27de8125d5146ac0bee325b8f" args="(struct memb_blocks *m, void *ptr)" --><p> 157<table class="mdTable" cellpadding="2" cellspacing="0"> 158 <tr> 159 <td class="mdRow"> 160 <table cellpadding="0" cellspacing="0" border="0"> 161 <tr> 162 <td class="md" nowrap valign="top">char memb_free </td> 163 <td class="md" valign="top">( </td> 164 <td class="md" nowrap valign="top">struct <a class="el" href="a00081.html">memb_blocks</a> * </td> 165 <td class="mdname" nowrap> <em>m</em>, </td> 166 </tr> 167 <tr> 168 <td class="md" nowrap align="right"></td> 169 <td class="md"></td> 170 <td class="md" nowrap>void * </td> 171 <td class="mdname" nowrap> <em>ptr</em></td> 172 </tr> 173 <tr> 174 <td class="md"></td> 175 <td class="md">) </td> 176 <td class="md" colspan="2"></td> 177 </tr> 178 </table> 179 </td> 180 </tr> 181</table> 182<table cellspacing="5" cellpadding="0" border="0"> 183 <tr> 184 <td> 185 186 </td> 187 <td> 188 189<p> 190Deallocate a memory block from a memory block previously declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. 191<p> 192<dl compact><dt><b>Parameters:</b></dt><dd> 193 <table border="0" cellspacing="2" cellpadding="0"> 194 <tr><td valign="top"></td><td valign="top"><em>m</em> </td><td>m A memory block previosly declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>.</td></tr> 195 <tr><td valign="top"></td><td valign="top"><em>ptr</em> </td><td>A pointer to the memory block that is to be deallocated.</td></tr> 196 </table> 197</dl> 198<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> 199<dl compact><dt><b>Examples: </b></dt><dd> 200<a class="el" href="a00044.html#a209">telnetd.c</a>.</dl> 201<p> 202Definition at line <a class="el" href="a00186.html#l00079">79</a> of file <a class="el" href="a00186.html">memb.c</a>. 203<p> 204References <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> 205 </tr> 206</table> 207<a class="anchor" name="gd58a6c7e62ae59bf7a016ded12ca2910"></a><!-- doxytag: member="memb.c::memb_init" ref="gd58a6c7e62ae59bf7a016ded12ca2910" args="(struct memb_blocks *m)" --><p> 208<table class="mdTable" cellpadding="2" cellspacing="0"> 209 <tr> 210 <td class="mdRow"> 211 <table cellpadding="0" cellspacing="0" border="0"> 212 <tr> 213 <td class="md" nowrap valign="top">void memb_init </td> 214 <td class="md" valign="top">( </td> 215 <td class="md" nowrap valign="top">struct <a class="el" href="a00081.html">memb_blocks</a> * </td> 216 <td class="mdname1" valign="top" nowrap> <em>m</em> </td> 217 <td class="md" valign="top"> ) </td> 218 <td class="md" nowrap></td> 219 </tr> 220 </table> 221 </td> 222 </tr> 223</table> 224<table cellspacing="5" cellpadding="0" border="0"> 225 <tr> 226 <td> 227 228 </td> 229 <td> 230 231<p> 232Initialize a memory block that was declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. 233<p> 234<dl compact><dt><b>Parameters:</b></dt><dd> 235 <table border="0" cellspacing="2" cellpadding="0"> 236 <tr><td valign="top"></td><td valign="top"><em>m</em> </td><td>A memory block previosly declared with <a class="el" href="a00159.html#gf31774d02a69fd3f1c2b282454438cba">MEMB()</a>. </td></tr> 237 </table> 238</dl> 239<dl compact><dt><b>Examples: </b></dt><dd> 240<a class="el" href="a00044.html#a220">telnetd.c</a>.</dl> 241<p> 242Definition at line <a class="el" href="a00186.html#l00052">52</a> of file <a class="el" href="a00186.html">memb.c</a>. 243<p> 244References <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> 245 </tr> 246</table> 247<hr size="1"><address style="align: right;"><small>Generated on Mon Jun 12 10:23:02 2006 for uIP 1.0 by 248<a href="http://www.doxygen.org/index.html"> 249<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address> 250</body> 251</html> 252