<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-2">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.0.6249.1">
<TITLE>AW: [En-Nut-Discussion] Understanding NutHeapAlloc()/NutHeapFree()</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">Hi Jan,</FONT></SPAN>
</P>
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">> > To answer your question 6 is the sizeof(0xDEADBEEF) + the</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">> > sizeof(HEAPNODE:hn_size) </FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">> But why not sizeof(HEAPNODE)? I still can't understand this.</FONT></SPAN>
</P>
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">That's simple. The HEAPNODE structure is only fully used, if it marks a free block. In this case hn_size contains the size of the free block and hn_next points to the next free block.</FONT></SPAN></P>
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">Then if you use NetHeapAlloc() and a free block is allocated, the block is not any longer part of the free block list and therefore the hn_next member isn't used any more.</FONT> <FONT SIZE=2 FACE="Courier New">So the RAM space needed for hn_next can now be used for the newly allocated ram. Thats the reason why hn_size is the first and hn_next the last member of HEAPNODE.</FONT></SPAN></P>
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New">The reserved memory looks like this:</FONT></SPAN>
</P>
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">+-----</FONT><FONT SIZE=2 FACE="Courier New">-</FONT><FONT SIZE=2 FACE="Courier New CE">-+------------------------------------------------+----------+</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">|</FONT><FONT SIZE=2 FACE="Courier New">hn_size</FONT><FONT SIZE=2 FACE="Courier New CE">| available for a thread |0xDEADBEEF|</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE">+-----</FONT><FONT SIZE=2 FACE="Courier New">-</FONT><FONT SIZE=2 FACE="Courier New CE">-+------------------------------------------------+----------+</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE"> sizeof(</FONT><FONT SIZE=2 FACE="Courier New">hn_size</FONT><FONT SIZE=2 FACE="Courier New CE">) N bytes 4 bytes</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE"> bytes</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE"> </FONT> <FONT SIZE=2 FACE="Courier New"></FONT> <FONT SIZE=2 FACE="Courier New CE">^</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New CE"> </FONT> <FONT SIZE=2 FACE="Courier New"></FONT> <FONT SIZE=2 FACE="Courier New CE">+-- pointer returned to a thread (application)</FONT></SPAN>
</P>
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New">sizeof(hn_size) is actually 2 byte, because it is a u_short.</FONT></SPAN>
</P>
<P><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New">Cheers,</FONT></SPAN>
<BR><SPAN LANG="de"><FONT SIZE=2 FACE="Courier New">Oliver.</FONT></SPAN>
</P>
</BODY>
</HTML>