📄 lib0046.html
字号:
<span style="background-color:d9d9d9">for(i=0;i<lastAlloc;i++)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">if(indices[i].free==TRUE)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">indices[i].free=FALSE;</span>
<span style="background-color:d9d9d9">lastAlloc = i;</span>
<span style="background-color:d9d9d9">return(&indices[i]);</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">return(NULL);</span>
<span style="background-color:d9d9d9">}/*end alloc-----------------------------------------------*/</span>
<span style="background-color:d9d9d9">void SubAllocator::release(struct Indices *addr)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">//sanity check</span>
<span style="background-color:d9d9d9">if((addr>=&indices[0])&&(addr<=&indices[size-1]))</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">(*addr).free=TRUE;</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">else</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">printf("SubAllocator::release():");</span>
<span style="background-color:d9d9d9">printf("release failed, address out of bounds\n");</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">return;</span>
<span style="background-color:d9d9d9">}/*end release-----------------------------------------------*/</span>
<span style="background-color:d9d9d9">void SubAllocator::printList()</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">U4 i;</span>
<span style="background-color:d9d9d9">for(i=0;i<size;i++)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">if(indices[i].free==FALSE)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">printf("indices[%lu] ",i);</span>
<span style="background-color:d9d9d9">printf("[%lu, ",indices[i].index1);</span>
<span style="background-color:d9d9d9">printf("%lu,",indices[i].index2);<a name="644"></a><a name="IDX-347"></a></span>
<span style="background-color:d9d9d9">printf("%lu]\n",indices[i].index3);</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">else</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">printf("indices[%lu]=FREE\n",i);</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">return;</span>
<span style="background-color:d9d9d9">}/*end printList-----------------------------------------------*/</span>
<span style="background-color:d9d9d9">void main()</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">U4 ticks1,ticks2;</span>
<span style="background-color:d9d9d9">U4 nAllocations=1024;</span>
<span style="background-color:d9d9d9">U4 i;</span>
<span style="background-color:d9d9d9">SubAllocator *ptr;</span>
<span style="background-color:d9d9d9">struct Indices **addr;</span>
<span style="background-color:d9d9d9">ptr = new SubAllocator(nAllocations);</span>
<span style="background-color:d9d9d9">addr = (struct Indices**)malloc(nAllocations*sizeof(struct</span>
<span style="background-color:d9d9d9">Indices*));</span>
<span style="background-color:d9d9d9">ticks1 = GetTickCount();</span>
<span style="background-color:d9d9d9">for(i=0;i<nAllocations;i++)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">addr[i] = (*ptr).alloc();</span>
<span style="background-color:d9d9d9">if(addr[i]==NULL)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">printf("addr[%lu]==NULL\n",i);</span>
<span style="background-color:d9d9d9">exit(1);</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">for(i=0;i<nAllocations;i++)</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">(*ptr).release(addr[i]);</span>
<span style="background-color:d9d9d9">}</span>
<span style="background-color:d9d9d9">ticks2 = GetTickCount();</span>
<span style="background-color:d9d9d9">delete(ptr);</span>
<span style="background-color:d9d9d9">free(addr);</span>
<span style="background-color:d9d9d9">printf("msecs=%lu\n",ticks2-ticks1);</span>
<span style="background-color:d9d9d9">return;</span>
<span style="background-color:d9d9d9">}/*end main-----------------------------------------------*/</span><a name="645"></a><a name="IDX-348"></a>
</pre>
</div>
<p class="para">When this application is executed, the following output is produced:</p>
<div class="informalexample">
<pre class="literallayout">
msecs=0
</pre>
</div>
<p class="para">The allocation and release of 1,024 <span class="fixed">Indices</span> structures took <i class="emphasis">less than a millisecond</i>. This is obviously much faster than anything we have looked at so far.</p>
<p class="last-para">The moral of this story: If you have predictable application behavior, you can tailor a memory manager to exploit that predictability and derive significant performance gains.</p>
<a></a>
</div>
</div>
</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td valign="top" class="v2" align="right"><div STYLE="MARGIN-RIGHT: 0.15in"><a href="LiB0045.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0047.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -