<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>Crystal Space 1.2.1: csutil/fixedsizeallocator.h Source File (Crystal Space 1.2.1 Public API Reference)</title> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <table border="0" cellpadding="0" cellspacing="0" width="100%" class="head"> <tr height="59"> <td class="head" width="202" valign="bottom" style="padding-left:0;"><a href="http://www.crystalspace3d.org/"><img src="csblur.png" width="236" height="59" alt="CrystalSpace" border="0"></a></td> <td class="head"><h2>Public API Reference</h2></td> </tr> <tr height="11"> <td colspan="2" class="headshadow" valign="top" style="padding-left:0;"><img src="csblurb.png" width="236" height="11" alt="" border="0"></td> </tr> </table> <div class="content"> <!-- Generated by Doxygen 1.5.3 --> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="classes.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> </ul> </div> <h1>csutil/fixedsizeallocator.h</h1><a href="fixedsizeallocator_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> Crystal Space Fixed Size Block Allocator</span> <a name="l00003"></a>00003 <span class="comment"> Copyright (C) 2005 by Eric Sunshine <sunshine@sunshineco.com></span> <a name="l00004"></a>00004 <span class="comment"> (C) 2006 by Frank Richter</span> <a name="l00005"></a>00005 <span class="comment"></span> <a name="l00006"></a>00006 <span class="comment"> This library is free software; you can redistribute it and/or</span> <a name="l00007"></a>00007 <span class="comment"> modify it under the terms of the GNU Library General Public</span> <a name="l00008"></a>00008 <span class="comment"> License as published by the Free Software Foundation; either</span> <a name="l00009"></a>00009 <span class="comment"> version 2 of the License, or (at your option) any later version.</span> <a name="l00010"></a>00010 <span class="comment"></span> <a name="l00011"></a>00011 <span class="comment"> This library is distributed in the hope that it will be useful,</span> <a name="l00012"></a>00012 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <a name="l00013"></a>00013 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span> <a name="l00014"></a>00014 <span class="comment"> Library General Public License for more details.</span> <a name="l00015"></a>00015 <span class="comment"></span> <a name="l00016"></a>00016 <span class="comment"> You should have received a copy of the GNU Library General Public</span> <a name="l00017"></a>00017 <span class="comment"> License along with this library; if not, write to the Free</span> <a name="l00018"></a>00018 <span class="comment"> Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</span> <a name="l00019"></a>00019 <span class="comment">*/</span> <a name="l00020"></a>00020 <span class="preprocessor">#ifndef __CSUTIL_FIXEDSIZEALLOCATOR_H__</span> <a name="l00021"></a>00021 <span class="preprocessor"></span><span class="preprocessor">#define __CSUTIL_FIXEDSIZEALLOCATOR_H__</span> <a name="l00022"></a>00022 <span class="preprocessor"></span> <a name="l00027"></a>00027 <span class="preprocessor">#include "csextern.h"</span> <a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="csutil_2array_8h.html" title="Generic Array Template.">csutil/array.h</a>"</span> <a name="l00029"></a>00029 <span class="preprocessor">#include "<a class="code" href="bitarray_8h.html" title="A one-dimensional array of bits, similar to STL bitset.">csutil/bitarray.h</a>"</span> <a name="l00030"></a>00030 <span class="preprocessor">#include "<a class="code" href="sysfunc_8h.html" title="Common system functions.">csutil/sysfunc.h</a>"</span> <a name="l00031"></a>00031 <a name="l00032"></a>00032 <span class="preprocessor">#ifdef CS_DEBUG</span> <a name="l00033"></a>00033 <span class="preprocessor"></span><span class="preprocessor">#include <typeinfo></span> <a name="l00034"></a>00034 <span class="preprocessor">#endif</span> <a name="l00035"></a>00035 <span class="preprocessor"></span> <a name="l00036"></a>00036 <span class="preprocessor">#if defined(CS_DEBUG) && !defined(CS_FIXEDSIZEALLOC_DEBUG)</span> <a name="l00037"></a>00037 <span class="preprocessor"></span><span class="preprocessor">#define _CS_FIXEDSIZEALLOC_DEBUG_DEFAULTED</span> <a name="l00038"></a>00038 <span class="preprocessor"></span><span class="preprocessor">#define CS_FIXEDSIZEALLOC_DEBUG</span> <a name="l00039"></a>00039 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00040"></a>00040 <span class="preprocessor"></span> <a name="l00059"></a>00059 <span class="keyword">template</span> <<span class="keywordtype">size_t</span> Size, <span class="keyword">class</span> Allocator = CS::Memory::AllocatorMalloc> <a name="l00060"></a><a class="code" href="classcsFixedSizeAllocator.html">00060</a> <span class="keyword">class </span><a class="code" href="classcsFixedSizeAllocator.html" title="This class implements a memory allocator which can efficiently allocate objects that...">csFixedSizeAllocator</a> <a name="l00061"></a>00061 { <a name="l00062"></a>00062 <span class="keyword">public</span>: <a name="l00063"></a><a class="code" href="classcsFixedSizeAllocator.html#9ff9dd8306e8da2b752f2c6df4352969">00063</a> <span class="keyword">typedef</span> <a class="code" href="classcsFixedSizeAllocator.html" title="This class implements a memory allocator which can efficiently allocate objects that...">csFixedSizeAllocator<Size, Allocator></a> <a class="code" href="classcsFixedSizeAllocator.html" title="This class implements a memory allocator which can efficiently allocate objects that...">ThisType</a>; <a name="l00064"></a><a class="code" href="classcsFixedSizeAllocator.html#edea983e540a0c4c20770e401525962b">00064</a> <span class="keyword">typedef</span> Allocator <a class="code" href="classcsFixedSizeAllocator.html#edea983e540a0c4c20770e401525962b">AllocatorType</a>; <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keyword">protected</span>: <span class="comment">// 'protected' allows access by test-suite.</span> <a name="l00067"></a><a class="code" href="structcsFixedSizeAllocator_1_1FreeNode.html">00067</a> <span class="keyword">struct </span><a class="code" href="structcsFixedSizeAllocator_1_1FreeNode.html">FreeNode</a> <a name="l00068"></a>00068 { <a name="l00069"></a><a class="code" href="structcsFixedSizeAllocator_1_1FreeNode.html#483be44e73a585af04368fc7ac4e7a8f">00069</a> <a class="code" href="structcsFixedSizeAllocator_1_1FreeNode.html">FreeNode</a>* <a class="code" href="structcsFixedSizeAllocator_1_1FreeNode.html#483be44e73a585af04368fc7ac4e7a8f">next</a>; <a name="l00070"></a>00070 }; <a name="l00071"></a>00071 <a name="l00072"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html">00072</a> <span class="keyword">struct </span><a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html">BlockKey</a> <a name="l00073"></a>00073 { <a name="l00074"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#7cd45c6e32ab1ab336e79ccd54f2f7eb">00074</a> <a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a> <span class="keyword">const</span>* <a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#7cd45c6e32ab1ab336e79ccd54f2f7eb">addr</a>; <a name="l00075"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#effc38fdc01939d9fcfc5d5947119e03">00075</a> <span class="keywordtype">size_t</span> <a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#effc38fdc01939d9fcfc5d5947119e03">blocksize</a>; <a name="l00076"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#b694157735400d65c7fae3ded018f04c">00076</a> <a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#b694157735400d65c7fae3ded018f04c">BlockKey</a>(<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a> <span class="keyword">const</span>* p, <span class="keywordtype">size_t</span> n) : <a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#7cd45c6e32ab1ab336e79ccd54f2f7eb">addr</a>(p), <a class="code" href="structcsFixedSizeAllocator_1_1BlockKey.html#effc38fdc01939d9fcfc5d5947119e03">blocksize</a>(n) {} <a name="l00077"></a>00077 }; <a name="l00078"></a>00078 <a name="l00079"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html">00079</a> <span class="keyword">struct </span><a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html">BlocksWrapper</a> : <span class="keyword">public</span> Allocator <a name="l00080"></a>00080 { <a name="l00081"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">00081</a> <a class="code" href="classcsArray.html" title="A templated array class.">csArray<uint8*></a> <a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>; <a name="l00082"></a>00082 <a name="l00083"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#3ee0d742923beff686a6c15daf8f30c5">00083</a> <a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#3ee0d742923beff686a6c15daf8f30c5">BlocksWrapper</a> () {} <a name="l00084"></a><a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#93fb576395684ef4a605b3125f4a55f3">00084</a> <a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#3ee0d742923beff686a6c15daf8f30c5">BlocksWrapper</a> (<span class="keyword">const</span> Allocator& alloc) : Allocator (alloc) {} <a name="l00085"></a>00085 }; <a name="l00087"></a><a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8">00087</a> BlocksWrapper <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>; <a name="l00089"></a><a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105">00089</a> <span class="keywordtype">size_t</span> <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a>; <a name="l00091"></a><a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465">00091</a> <span class="keywordtype">size_t</span> <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>; <a name="l00093"></a><a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30">00093</a> <span class="keywordtype">size_t</span> <a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a>; <a name="l00095"></a><a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8">00095</a> FreeNode* <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>; <a name="l00101"></a><a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884">00101</a> <span class="keywordtype">bool</span> <a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a>; <a name="l00102"></a>00102 <a name="l00109"></a><a class="code" href="classcsFixedSizeAllocator.html#25b9f1503fccb1784317f69f9e232875">00109</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classcsFixedSizeAllocator.html#25b9f1503fccb1784317f69f9e232875" title="Comparison function for FindBlock() which does a &quot;fuzzy&quot; search given an...">FuzzyCmp</a>(<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* <span class="keyword">const</span>& block, BlockKey <span class="keyword">const</span>& k) <a name="l00110"></a>00110 { <a name="l00111"></a>00111 <span class="keywordflow">return</span> (block + k.blocksize <= k.addr ? -1 : (block > k.addr ? 1 : 0)); <a name="l00112"></a>00112 } <a name="l00113"></a>00113 <a name="l00117"></a><a class="code" href="classcsFixedSizeAllocator.html#e69493d266e0ed6a92fb954547b4e827">00117</a> <span class="keywordtype">size_t</span> <a class="code" href="classcsFixedSizeAllocator.html#e69493d266e0ed6a92fb954547b4e827" title="Find the memory block which contains the given memory.">FindBlock</a>(<span class="keywordtype">void</span> <span class="keyword">const</span>* m)<span class="keyword"> const</span> <a name="l00118"></a>00118 <span class="keyword"> </span>{ <a name="l00119"></a>00119 <span class="keywordflow">return</span> <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.FindSortedKey( <a name="l00120"></a>00120 <a class="code" href="classcsArrayCmp.html" title="A functor template which encapsulates a key and a comparison function for use with...">csArrayCmp<uint8*,BlockKey></a>(BlockKey((<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>*)m, <a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a>), <a class="code" href="classcsFixedSizeAllocator.html#25b9f1503fccb1784317f69f9e232875" title="Comparison function for FindBlock() which does a &quot;fuzzy&quot; search given an...">FuzzyCmp</a>)); <a name="l00121"></a>00121 } <a name="l00122"></a>00122 <a name="l00128"></a><a class="code" href="classcsFixedSizeAllocator.html#6e7974ee7871949250822b3937554caa">00128</a> <a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* <a class="code" href="classcsFixedSizeAllocator.html#6e7974ee7871949250822b3937554caa" title="Allocate a block and initialize its free-node chain.">AllocBlock</a>() <a name="l00129"></a>00129 { <a name="l00130"></a>00130 <a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* block = (<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>*)<a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.Alloc (<a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a>); <a name="l00131"></a>00131 <a name="l00132"></a>00132 <span class="comment">// Build the free-node chain (all nodes are free in the new block).</span> <a name="l00133"></a>00133 FreeNode* nextfree = 0; <a name="l00134"></a>00134 <a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* node = block + (<a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a> - 1) * <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>; <a name="l00135"></a>00135 <span class="keywordflow">for</span> ( ; node >= block; node -= <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>) <a name="l00136"></a>00136 { <a name="l00137"></a>00137 FreeNode* slot = (FreeNode*)node; <a name="l00138"></a>00138 slot->next = nextfree; <a name="l00139"></a>00139 nextfree = slot; <a name="l00140"></a>00140 } <a name="l00141"></a>00141 <a class="code" href="cssysdef_8h.html#c380bd47888ecfe73e7b7a40b6f827a1" title="Assertion.">CS_ASSERT</a>((<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>*)nextfree == block); <a name="l00142"></a>00142 <span class="keywordflow">return</span> block; <a name="l00143"></a>00143 } <a name="l00144"></a>00144 <a name="l00148"></a><a class="code" href="classcsFixedSizeAllocator.html#36126c87742e8fcbe2085dc404a5b166">00148</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#36126c87742e8fcbe2085dc404a5b166" title="Dispose of a block.">FreeBlock</a>(<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* p) <a name="l00149"></a>00149 { <a name="l00150"></a>00150 <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.Free (p); <a name="l00151"></a>00151 } <a name="l00152"></a>00152 <a name="l00156"></a>00156 <span class="keyword">template</span><<span class="keyword">typename</span> Disposer> <a name="l00157"></a><a class="code" href="classcsFixedSizeAllocator.html#57b6c1127a18ae1fa780c68af92a2916">00157</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#57b6c1127a18ae1fa780c68af92a2916" title="Destroy an object.">DestroyObject</a> (Disposer& disposer, <span class="keywordtype">void</span>* p)<span class="keyword"> const</span> <a name="l00158"></a>00158 <span class="keyword"> </span>{ <a name="l00159"></a>00159 disposer.Dispose (p); <a name="l00160"></a>00160 <span class="preprocessor">#ifdef CS_FIXEDSIZEALLOC_DEBUG</span> <a name="l00161"></a>00161 <span class="preprocessor"></span> memset (p, 0xfb, <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>); <a name="l00162"></a>00162 <span class="preprocessor">#endif</span> <a name="l00163"></a>00163 <span class="preprocessor"></span> } <a name="l00164"></a>00164 <a name="l00169"></a><a class="code" href="classcsFixedSizeAllocator.html#fc9173862050c0e56b035ad5e5a8bf4e">00169</a> <a class="code" href="classcsBitArray.html" title="A one-dimensional array of bits, similar to STL bitset.">csBitArray</a> <a class="code" href="classcsFixedSizeAllocator.html#fc9173862050c0e56b035ad5e5a8bf4e" title="Get a usage mask showing all used (1&#39;s) and free (0&#39;s) nodes in the entire...">GetAllocationMap</a>()<span class="keyword"> const</span> <a name="l00170"></a>00170 <span class="keyword"> </span>{ <a name="l00171"></a>00171 <a class="code" href="classcsBitArray.html" title="A one-dimensional array of bits, similar to STL bitset.">csBitArray</a> mask(<a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a> * <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.GetSize()); <a name="l00172"></a>00172 mask.<a class="code" href="classcsBitArrayTweakable.html#837beaac57a9180922ed6ff8c552264a" title="Change value of all bits.">FlipAllBits</a>(); <a name="l00173"></a>00173 <span class="keywordflow">for</span> (FreeNode* p = <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>; p != 0; p = p-><a class="code" href="structcsFixedSizeAllocator_1_1FreeNode.html#483be44e73a585af04368fc7ac4e7a8f">next</a>) <a name="l00174"></a>00174 { <a name="l00175"></a>00175 <span class="keywordtype">size_t</span> <span class="keyword">const</span> n = <a class="code" href="classcsFixedSizeAllocator.html#e69493d266e0ed6a92fb954547b4e827" title="Find the memory block which contains the given memory.">FindBlock</a>(p); <a name="l00176"></a>00176 <a class="code" href="cssysdef_8h.html#c380bd47888ecfe73e7b7a40b6f827a1" title="Assertion.">CS_ASSERT</a>(n != <a class="code" href="group__util__containers.html#g7477ed9887527029069ab5d5c79e2f70" title="This value is returned whenever an array item could not be located or does not exist...">csArrayItemNotFound</a>); <a name="l00177"></a>00177 <span class="keywordtype">size_t</span> <span class="keyword">const</span> slot = ((<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>*)p - <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>[n]) / <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>; <span class="comment">// Slot in block.</span> <a name="l00178"></a>00178 mask.ClearBit(n * <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a> + slot); <a name="l00179"></a>00179 } <a name="l00180"></a>00180 <span class="keywordflow">return</span> mask; <a name="l00181"></a>00181 } <a name="l00182"></a>00182 <a name="l00184"></a><a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html">00184</a> <span class="keyword">class </span><a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html" title="Default disposer mixin, just reporting leaks.">DefaultDisposer</a> <a name="l00185"></a>00185 { <a name="l00186"></a>00186 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00187"></a>00187 <span class="preprocessor"></span> <span class="keywordtype">bool</span> doWarn; <a name="l00188"></a>00188 <span class="keyword">const</span> <span class="keywordtype">char</span>* parentClass; <a name="l00189"></a>00189 <span class="keyword">const</span> <span class="keywordtype">void</span>* parent; <a name="l00190"></a>00190 <span class="keywordtype">size_t</span> count; <a name="l00191"></a>00191 <span class="preprocessor"> #endif</span> <a name="l00192"></a>00192 <span class="preprocessor"></span> <span class="keyword">public</span>: <a name="l00193"></a>00193 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00194"></a>00194 <span class="preprocessor"></span> <span class="keyword">template</span><<span class="keyword">typename</span> BA> <a name="l00195"></a>00195 <a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html#1992e5f5e81f3fb0a79e539227d3abfd">DefaultDisposer</a> (<span class="keyword">const</span> BA& ba, <span class="keywordtype">bool</span> legit) : <a name="l00196"></a>00196 doWarn (!legit), parentClass (<span class="keyword">typeid</span>(BA).name()), parent (&ba), <a name="l00197"></a>00197 count (0) <a name="l00198"></a>00198 { <a name="l00199"></a>00199 } <a name="l00200"></a>00200 <span class="preprocessor"> #else</span> <a name="l00201"></a>00201 <span class="preprocessor"></span> <span class="keyword">template</span><<span class="keyword">typename</span> BA> <a name="l00202"></a><a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html#1992e5f5e81f3fb0a79e539227d3abfd">00202</a> <a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html#1992e5f5e81f3fb0a79e539227d3abfd">DefaultDisposer</a> (<span class="keyword">const</span> BA&, <span class="keywordtype">bool</span> legit) <a name="l00203"></a>00203 { (void)legit; } <a name="l00204"></a>00204 <span class="preprocessor"> #endif</span> <a name="l00205"></a><a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html#aa3a504da5960a4237de913a004a2f4a">00205</a> <span class="preprocessor"></span> <a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html#aa3a504da5960a4237de913a004a2f4a">~DefaultDisposer</a>() <a name="l00206"></a>00206 { <a name="l00207"></a>00207 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00208"></a>00208 <span class="preprocessor"></span> <span class="keywordflow">if</span> ((count > 0) && doWarn) <a name="l00209"></a>00209 { <a name="l00210"></a>00210 <a class="code" href="group__util.html#g9bd60492f7f494e915605a6cbc42a54b" title="CS version of fprintf (stderr, str, .">csPrintfErr</a>(<span class="stringliteral">"%s %p leaked %zu objects.\n"</span>, parentClass, (<span class="keywordtype">void</span>*)<span class="keyword">this</span>, <a name="l00211"></a>00211 count); <a name="l00212"></a>00212 } <a name="l00213"></a>00213 <span class="preprocessor"> #endif</span> <a name="l00214"></a>00214 <span class="preprocessor"></span> } <a name="l00215"></a><a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html#a2d7409a6fdba7b7fe443cff4076ba45">00215</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator_1_1DefaultDisposer.html#a2d7409a6fdba7b7fe443cff4076ba45">Dispose</a> (<span class="keywordtype">void</span>* <span class="comment">/*p*/</span>) <a name="l00216"></a>00216 { <a name="l00217"></a>00217 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00218"></a>00218 <span class="preprocessor"></span> count++; <a name="l00219"></a>00219 <span class="preprocessor"> #endif</span> <a name="l00220"></a>00220 <span class="preprocessor"></span> } <a name="l00221"></a>00221 }; <a name="l00227"></a>00227 <span class="keyword">template</span><<span class="keyword">typename</span> Disposer> <a name="l00228"></a><a class="code" href="classcsFixedSizeAllocator.html#2d6916d415daaa8d83aeccbf922b010a">00228</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#2d6916d415daaa8d83aeccbf922b010a" title="Destroys all living objects and releases all memory allocated by the pool.">DisposeAll</a>(Disposer& disposer) <a name="l00229"></a>00229 { <a name="l00230"></a>00230 <a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a> = <span class="keyword">true</span>; <a name="l00231"></a>00231 <a class="code" href="classcsBitArray.html" title="A one-dimensional array of bits, similar to STL bitset.">csBitArray</a> <span class="keyword">const</span> mask(<a class="code" href="classcsFixedSizeAllocator.html#fc9173862050c0e56b035ad5e5a8bf4e" title="Get a usage mask showing all used (1&#39;s) and free (0&#39;s) nodes in the entire...">GetAllocationMap</a>()); <a name="l00232"></a>00232 <span class="keywordtype">size_t</span> node = 0; <a name="l00233"></a>00233 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> b = 0, bN = <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.GetSize(); b < bN; b++) <a name="l00234"></a>00234 { <a name="l00235"></a>00235 <span class="keywordflow">for</span> (<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a> *p = <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>[b], *pN = p + <a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a>; p < pN; p += <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>) <a name="l00236"></a>00236 <span class="keywordflow">if</span> (mask.<a class="code" href="classcsBitArrayTweakable.html#19e077803aeb46a70b7fe038b7d596c5" title="Returns true if the bit at position pos is true.">IsBitSet</a>(node++)) <a name="l00237"></a>00237 <a class="code" href="classcsFixedSizeAllocator.html#57b6c1127a18ae1fa780c68af92a2916" title="Destroy an object.">DestroyObject</a> (disposer, p); <a name="l00238"></a>00238 <a class="code" href="classcsFixedSizeAllocator.html#36126c87742e8fcbe2085dc404a5b166" title="Dispose of a block.">FreeBlock</a>(<a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>[b]); <a name="l00239"></a>00239 } <a name="l00240"></a>00240 <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.DeleteAll(); <a name="l00241"></a>00241 <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> = 0; <a name="l00242"></a>00242 <a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a> = <span class="keyword">false</span>; <a name="l00243"></a>00243 } <a name="l00244"></a>00244 <a name="l00250"></a>00250 <span class="keyword">template</span><<span class="keyword">typename</span> Disposer> <a name="l00251"></a><a class="code" href="classcsFixedSizeAllocator.html#69cb1c8ff50f68d5779b180bd32de2c6">00251</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#69cb1c8ff50f68d5779b180bd32de2c6" title="Deallocate a chunk of memory.">Free</a> (Disposer& disposer, <span class="keywordtype">void</span>* p) <a name="l00252"></a>00252 { <a name="l00253"></a>00253 <span class="keywordflow">if</span> (p != 0 && !<a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a>) <a name="l00254"></a>00254 { <a name="l00255"></a>00255 <a class="code" href="cssysdef_8h.html#c380bd47888ecfe73e7b7a40b6f827a1" title="Assertion.">CS_ASSERT</a>(<a class="code" href="classcsFixedSizeAllocator.html#e69493d266e0ed6a92fb954547b4e827" title="Find the memory block which contains the given memory.">FindBlock</a>(p) != <a class="code" href="group__util__containers.html#g7477ed9887527029069ab5d5c79e2f70" title="This value is returned whenever an array item could not be located or does not exist...">csArrayItemNotFound</a>); <a name="l00256"></a>00256 <a class="code" href="classcsFixedSizeAllocator.html#57b6c1127a18ae1fa780c68af92a2916" title="Destroy an object.">DestroyObject</a> (disposer, p); <a name="l00257"></a>00257 FreeNode* f = (FreeNode*)p; <a name="l00258"></a>00258 f->next = <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>; <a name="l00259"></a>00259 <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> = f; <a name="l00260"></a>00260 } <a name="l00261"></a>00261 } <a name="l00268"></a>00268 <span class="keyword">template</span><<span class="keyword">typename</span> Disposer> <a name="l00269"></a><a class="code" href="classcsFixedSizeAllocator.html#eae28b243f7ec94c18f88cd028289b54">00269</a> <span class="keywordtype">bool</span> <a class="code" href="classcsFixedSizeAllocator.html#eae28b243f7ec94c18f88cd028289b54" title="Try to delete a chunk of memory.">TryFree</a> (Disposer& disposer, <span class="keywordtype">void</span>* p) <a name="l00270"></a>00270 { <a name="l00271"></a>00271 <span class="keywordflow">if</span> (p != 0 && !<a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a>) <a name="l00272"></a>00272 { <a name="l00273"></a>00273 <span class="keywordflow">if</span> (<a class="code" href="classcsFixedSizeAllocator.html#e69493d266e0ed6a92fb954547b4e827" title="Find the memory block which contains the given memory.">FindBlock</a>(p) == <a class="code" href="group__util__containers.html#g7477ed9887527029069ab5d5c79e2f70" title="This value is returned whenever an array item could not be located or does not exist...">csArrayItemNotFound</a>) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00274"></a>00274 <a class="code" href="classcsFixedSizeAllocator.html#57b6c1127a18ae1fa780c68af92a2916" title="Destroy an object.">DestroyObject</a> (disposer, p); <a name="l00275"></a>00275 FreeNode* f = (FreeNode*)p; <a name="l00276"></a>00276 f->next = <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>; <a name="l00277"></a>00277 <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> = f; <a name="l00278"></a>00278 } <a name="l00279"></a>00279 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00280"></a>00280 } <a name="l00281"></a>00281 <a name="l00283"></a><a class="code" href="classcsFixedSizeAllocator.html#cd9305bed36709e36b0d52222a4a11b8">00283</a> <span class="keywordtype">void</span>* <a class="code" href="classcsFixedSizeAllocator.html#cd9305bed36709e36b0d52222a4a11b8" title="Find and allocate a block.">AllocCommon</a> () <a name="l00284"></a>00284 { <a name="l00285"></a>00285 <span class="keywordflow">if</span> (<a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a>) <a name="l00286"></a>00286 { <a name="l00287"></a>00287 <a class="code" href="group__util.html#g9bd60492f7f494e915605a6cbc42a54b" title="CS version of fprintf (stderr, str, .">csPrintfErr</a>(<span class="stringliteral">"ERROR: csFixedSizeAllocator(%p) tried to allocate memory "</span> <a name="l00288"></a>00288 <span class="stringliteral">"while inside DisposeAll()"</span>, (<span class="keywordtype">void</span>*)<span class="keyword">this</span>); <a name="l00289"></a>00289 <a class="code" href="cssysdef_8h.html#c380bd47888ecfe73e7b7a40b6f827a1" title="Assertion.">CS_ASSERT</a>(<span class="keyword">false</span>); <a name="l00290"></a>00290 } <a name="l00291"></a>00291 <a name="l00292"></a>00292 <span class="keywordflow">if</span> (<a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> == 0) <a name="l00293"></a>00293 { <a name="l00294"></a>00294 <a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* p = <a class="code" href="classcsFixedSizeAllocator.html#6e7974ee7871949250822b3937554caa" title="Allocate a block and initialize its free-node chain.">AllocBlock</a>(); <a name="l00295"></a>00295 <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.InsertSorted(p); <a name="l00296"></a>00296 <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> = (FreeNode*)p; <a name="l00297"></a>00297 } <a name="l00298"></a>00298 <span class="keywordtype">void</span>* <span class="keyword">const</span> node = <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>; <a name="l00299"></a>00299 <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> = <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>-><a class="code" href="structcsFixedSizeAllocator_1_1FreeNode.html#483be44e73a585af04368fc7ac4e7a8f">next</a>; <a name="l00300"></a>00300 <span class="preprocessor">#ifdef CS_FIXEDSIZEALLOC_DEBUG</span> <a name="l00301"></a>00301 <span class="preprocessor"></span> memset (node, 0xfa, <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>); <a name="l00302"></a>00302 <span class="preprocessor">#endif</span> <a name="l00303"></a>00303 <span class="preprocessor"></span> <span class="keywordflow">return</span> node; <a name="l00304"></a>00304 } <a name="l00305"></a>00305 <span class="keyword">private</span>: <a name="l00306"></a>00306 <span class="keywordtype">void</span> operator= (<a class="code" href="classcsFixedSizeAllocator.html" title="This class implements a memory allocator which can efficiently allocate objects that...">csFixedSizeAllocator</a> <span class="keyword">const</span>&); <span class="comment">// Illegal; unimplemented.</span> <a name="l00307"></a>00307 <span class="keyword">public</span>: <a name="l00309"></a>00309 <a name="l00319"></a><a class="code" href="classcsFixedSizeAllocator.html#f4534ee4e4e4b18b4b1fef3fd2ae8eda">00319</a> <a class="code" href="classcsFixedSizeAllocator.html#f4534ee4e4e4b18b4b1fef3fd2ae8eda" title="Construct a new fixed size allocator.">csFixedSizeAllocator</a> (<span class="keywordtype">size_t</span> nelem = 32) : <a name="l00320"></a>00320 <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a> (nelem), <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>(Size), <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>(0), <a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a>(false) <a name="l00321"></a>00321 { <a name="l00322"></a>00322 <span class="preprocessor">#ifdef CS_MEMORY_TRACKER</span> <a name="l00323"></a>00323 <span class="preprocessor"></span> <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.SetMemTrackerInfo (<span class="keyword">typeid</span>(*this).name()); <a name="l00324"></a>00324 <span class="preprocessor">#endif</span> <a name="l00325"></a>00325 <span class="preprocessor"></span> <span class="keywordflow">if</span> (<a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a> < <span class="keyword">sizeof</span> (FreeNode)) <a name="l00326"></a>00326 <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a> = <span class="keyword">sizeof</span> (FreeNode); <a name="l00327"></a>00327 <a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a> = <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a> * <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a>; <a name="l00328"></a>00328 } <a name="l00329"></a><a class="code" href="classcsFixedSizeAllocator.html#ccdb55f9c6b2ef2b0714784516151a44">00329</a> <a class="code" href="classcsFixedSizeAllocator.html#f4534ee4e4e4b18b4b1fef3fd2ae8eda" title="Construct a new fixed size allocator.">csFixedSizeAllocator</a> (<span class="keywordtype">size_t</span> nelem, <span class="keyword">const</span> Allocator& alloc) : <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a> (alloc), <a name="l00330"></a>00330 <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a> (nelem), <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>(Size), <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a>(0), <a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a>(false) <a name="l00331"></a>00331 { <a name="l00332"></a>00332 <span class="preprocessor">#ifdef CS_MEMORY_TRACKER</span> <a name="l00333"></a>00333 <span class="preprocessor"></span> <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.SetMemTrackerInfo (<span class="keyword">typeid</span>(*this).name()); <a name="l00334"></a>00334 <span class="preprocessor">#endif</span> <a name="l00335"></a>00335 <span class="preprocessor"></span> <span class="keywordflow">if</span> (<a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a> < <span class="keyword">sizeof</span> (FreeNode)) <a name="l00336"></a>00336 <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a> = <span class="keyword">sizeof</span> (FreeNode); <a name="l00337"></a>00337 <a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a> = <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a> * <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a>; <a name="l00338"></a>00338 } <a name="l00340"></a>00340 <a name="l00348"></a><a class="code" href="classcsFixedSizeAllocator.html#2f59f90c657b22afe7cd4a23ec127041">00348</a> <a class="code" href="classcsFixedSizeAllocator.html#f4534ee4e4e4b18b4b1fef3fd2ae8eda" title="Construct a new fixed size allocator.">csFixedSizeAllocator</a> (<a class="code" href="classcsFixedSizeAllocator.html" title="This class implements a memory allocator which can efficiently allocate objects that...">csFixedSizeAllocator</a> <span class="keyword">const</span>& other) : <a name="l00349"></a>00349 <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a> (other.<a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a>), <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a> (other.<a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>), <a name="l00350"></a>00350 <a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a> (other.<a class="code" href="classcsFixedSizeAllocator.html#d6bf973ab1bc9ba9289be6fdec8bdf30" title="Size in bytes per block.">blocksize</a>), <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> (0), <a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a> (false) <a name="l00351"></a>00351 { <a name="l00352"></a>00352 <span class="comment">/* Technically, an allocator can be empty even with freenode != 0 */</span> <a name="l00353"></a>00353 <a class="code" href="cssysdef_8h.html#c380bd47888ecfe73e7b7a40b6f827a1" title="Assertion.">CS_ASSERT</a>(other.<a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> == 0); <a name="l00354"></a>00354 } <a name="l00355"></a>00355 <a name="l00359"></a><a class="code" href="classcsFixedSizeAllocator.html#154d1536a1edd1837038488be2107e47">00359</a> <a class="code" href="classcsFixedSizeAllocator.html#154d1536a1edd1837038488be2107e47" title="Destroy all allocated objects and release memory.">~csFixedSizeAllocator</a>() <a name="l00360"></a>00360 { <a name="l00361"></a>00361 DefaultDisposer disposer (*<span class="keyword">this</span>, <span class="keyword">false</span>); <a name="l00362"></a>00362 <a class="code" href="classcsFixedSizeAllocator.html#2d6916d415daaa8d83aeccbf922b010a" title="Destroys all living objects and releases all memory allocated by the pool.">DisposeAll</a> (disposer); <a name="l00363"></a>00363 } <a name="l00364"></a>00364 <a name="l00370"></a><a class="code" href="classcsFixedSizeAllocator.html#8684d2f1d4b9c68d779f8b213fc89a2b">00370</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#8684d2f1d4b9c68d779f8b213fc89a2b" title="Destroy all chunks allocated.">Empty</a>() <a name="l00371"></a>00371 { <a name="l00372"></a>00372 DefaultDisposer disposer (*<span class="keyword">this</span>, <span class="keyword">true</span>); <a name="l00373"></a>00373 <a class="code" href="classcsFixedSizeAllocator.html#2d6916d415daaa8d83aeccbf922b010a" title="Destroys all living objects and releases all memory allocated by the pool.">DisposeAll</a> (disposer); <a name="l00374"></a>00374 } <a name="l00375"></a>00375 <a name="l00380"></a><a class="code" href="classcsFixedSizeAllocator.html#3492691739cb105d7274da3a5b938980">00380</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#3492691739cb105d7274da3a5b938980" title="Compact the allocator so that all blocks that are completely unused are removed.">Compact</a>() <a name="l00381"></a>00381 { <a name="l00382"></a>00382 <span class="keywordflow">if</span> (<a class="code" href="classcsFixedSizeAllocator.html#59d708c29be72cbbd957aff63c3ba884" title="Flag to ignore calls to Compact() and Free() if they&#39;re called recursively while...">insideDisposeAll</a>) <span class="keywordflow">return</span>; <a name="l00383"></a>00383 <a name="l00384"></a>00384 <span class="keywordtype">bool</span> compacted = <span class="keyword">false</span>; <a name="l00385"></a>00385 <a class="code" href="classcsBitArray.html" title="A one-dimensional array of bits, similar to STL bitset.">csBitArray</a> mask(<a class="code" href="classcsFixedSizeAllocator.html#fc9173862050c0e56b035ad5e5a8bf4e" title="Get a usage mask showing all used (1&#39;s) and free (0&#39;s) nodes in the entire...">GetAllocationMap</a>()); <a name="l00386"></a>00386 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> b = <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.GetSize(); b-- > 0; ) <a name="l00387"></a>00387 { <a name="l00388"></a>00388 <span class="keywordtype">size_t</span> <span class="keyword">const</span> node = b * <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a>; <a name="l00389"></a>00389 <span class="keywordflow">if</span> (!mask.<a class="code" href="classcsBitArrayTweakable.html#404f5ba789cecf357bc9003f796aa853" title="Checks whether at least one of count bits is set starting at pos.">AreSomeBitsSet</a>(node, elcount)) <a name="l00390"></a>00390 { <a name="l00391"></a>00391 <a class="code" href="classcsFixedSizeAllocator.html#36126c87742e8fcbe2085dc404a5b166" title="Dispose of a block.">FreeBlock</a>(<a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>[b]); <a name="l00392"></a>00392 <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.DeleteIndex(b); <a name="l00393"></a>00393 mask.<a class="code" href="classcsBitArrayTweakable.html#fd7ee919a37d5bac355d5034dbe52605" title="Delete from the array count bits starting at pos, making the array shorter.">Delete</a>(node, elcount); <a name="l00394"></a>00394 compacted = <span class="keyword">true</span>; <a name="l00395"></a>00395 } <a name="l00396"></a>00396 } <a name="l00397"></a>00397 <a name="l00398"></a>00398 <span class="comment">// If blocks were deleted, then free-node chain broke, so rebuild it.</span> <a name="l00399"></a>00399 <span class="keywordflow">if</span> (compacted) <a name="l00400"></a>00400 { <a name="l00401"></a>00401 FreeNode* nextfree = 0; <a name="l00402"></a>00402 <span class="keywordtype">size_t</span> <span class="keyword">const</span> bN = <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>.GetSize(); <a name="l00403"></a>00403 <span class="keywordtype">size_t</span> node = bN * <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a>; <a name="l00404"></a>00404 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> b = bN; b-- > 0; ) <a name="l00405"></a>00405 { <a name="l00406"></a>00406 <a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* <span class="keyword">const</span> p0 = <a class="code" href="classcsFixedSizeAllocator.html#424bd61f9cd708b78284ebd8dd485eb8" title="List of allocated blocks; sorted by address.">blocks</a>.<a class="code" href="structcsFixedSizeAllocator_1_1BlocksWrapper.html#69533886727c8bdffe21762859578f0e">b</a>[b]; <a name="l00407"></a>00407 <span class="keywordflow">for</span> (<a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a>* p = p0 + (elcount - 1) * <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>; p >= p0; p -= <a class="code" href="classcsFixedSizeAllocator.html#0d4e723ccc2429a5378254db493ab465" title="Element size; &gt;= sizeof(void*).">elsize</a>) <a name="l00408"></a>00408 { <a name="l00409"></a>00409 <span class="keywordflow">if</span> (!mask.<a class="code" href="classcsBitArrayTweakable.html#19e077803aeb46a70b7fe038b7d596c5" title="Returns true if the bit at position pos is true.">IsBitSet</a>(--node)) <a name="l00410"></a>00410 { <a name="l00411"></a>00411 FreeNode* slot = (FreeNode*)p; <a name="l00412"></a>00412 slot->next = nextfree; <a name="l00413"></a>00413 nextfree = slot; <a name="l00414"></a>00414 } <a name="l00415"></a>00415 } <a name="l00416"></a>00416 } <a name="l00417"></a>00417 <a class="code" href="classcsFixedSizeAllocator.html#5f88bf9a4e898072c43525f0580b30c8" title="Head of the chain of free nodes.">freenode</a> = nextfree; <a name="l00418"></a>00418 } <a name="l00419"></a>00419 } <a name="l00420"></a>00420 <a name="l00424"></a><a class="code" href="classcsFixedSizeAllocator.html#a23579349fa21fff8f1fa6dc30f5f427">00424</a> <span class="keywordtype">void</span>* <a class="code" href="classcsFixedSizeAllocator.html#a23579349fa21fff8f1fa6dc30f5f427" title="Allocate a chunk of memory.">Alloc</a> () <a name="l00425"></a>00425 { <a name="l00426"></a>00426 <span class="keywordflow">return</span> <a class="code" href="classcsFixedSizeAllocator.html#cd9305bed36709e36b0d52222a4a11b8" title="Find and allocate a block.">AllocCommon</a>(); <a name="l00427"></a>00427 } <a name="l00428"></a>00428 <a name="l00433"></a><a class="code" href="classcsFixedSizeAllocator.html#da2f85b56e7819fb9bd694603d85f95d">00433</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#69cb1c8ff50f68d5779b180bd32de2c6" title="Deallocate a chunk of memory.">Free</a> (<span class="keywordtype">void</span>* p) <a name="l00434"></a>00434 { <a name="l00435"></a>00435 DefaultDisposer disposer (*<span class="keyword">this</span>, <span class="keyword">true</span>); <a name="l00436"></a>00436 <a class="code" href="classcsFixedSizeAllocator.html#69cb1c8ff50f68d5779b180bd32de2c6" title="Deallocate a chunk of memory.">Free</a> (disposer, p); <a name="l00437"></a>00437 } <a name="l00444"></a><a class="code" href="classcsFixedSizeAllocator.html#8556322fce063d9f0204555fd866356b">00444</a> <span class="keywordtype">bool</span> <a class="code" href="classcsFixedSizeAllocator.html#eae28b243f7ec94c18f88cd028289b54" title="Try to delete a chunk of memory.">TryFree</a> (<span class="keywordtype">void</span>* p) <a name="l00445"></a>00445 { <a name="l00446"></a>00446 DefaultDisposer disposer (*<span class="keyword">this</span>, <span class="keyword">true</span>); <a name="l00447"></a>00447 <span class="keywordflow">return</span> <a class="code" href="classcsFixedSizeAllocator.html#eae28b243f7ec94c18f88cd028289b54" title="Try to delete a chunk of memory.">TryFree</a> (disposer, p); <a name="l00448"></a>00448 } <a name="l00450"></a><a class="code" href="classcsFixedSizeAllocator.html#fbc85b60aab6f5d8f33040cb10c2cb6a">00450</a> <span class="keywordtype">size_t</span> <a class="code" href="classcsFixedSizeAllocator.html#fbc85b60aab6f5d8f33040cb10c2cb6a" title="Query number of elements per block.">GetBlockElements</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <a class="code" href="classcsFixedSizeAllocator.html#f205cf3779350b696deacc138890f105" title="Number of elements per block.">elcount</a>; } <a name="l00451"></a>00451 <a name="l00454"></a><a class="code" href="classcsFixedSizeAllocator.html#fb05aa47c8f5183fc623b393c06342b1">00454</a> <span class="keywordtype">void</span>* <a class="code" href="classcsFixedSizeAllocator.html#a23579349fa21fff8f1fa6dc30f5f427" title="Allocate a chunk of memory.">Alloc</a> (<span class="keywordtype">size_t</span> n) <a name="l00455"></a>00455 { <a name="l00456"></a>00456 <a class="code" href="cssysdef_8h.html#c380bd47888ecfe73e7b7a40b6f827a1" title="Assertion.">CS_ASSERT</a> (n == Size); <a name="l00457"></a>00457 <span class="keywordflow">return</span> <a class="code" href="classcsFixedSizeAllocator.html#a23579349fa21fff8f1fa6dc30f5f427" title="Allocate a chunk of memory.">Alloc</a>(); <a name="l00458"></a>00458 } <a name="l00459"></a><a class="code" href="classcsFixedSizeAllocator.html#d2eb8689527b8fead5596208a308e010">00459</a> <span class="keywordtype">void</span>* <a class="code" href="classcsFixedSizeAllocator.html#a23579349fa21fff8f1fa6dc30f5f427" title="Allocate a chunk of memory.">Alloc</a> (<span class="keywordtype">void</span>* p, <span class="keywordtype">size_t</span> newSize) <a name="l00460"></a>00460 { <a name="l00461"></a>00461 <a class="code" href="cssysdef_8h.html#c380bd47888ecfe73e7b7a40b6f827a1" title="Assertion.">CS_ASSERT</a> (newSize == Size); <a name="l00462"></a>00462 <span class="keywordflow">return</span> p; <a name="l00463"></a>00463 } <a name="l00464"></a><a class="code" href="classcsFixedSizeAllocator.html#4a7a649b1a90c358c950e4261e2f3ab1">00464</a> <span class="keywordtype">void</span> <a class="code" href="classcsFixedSizeAllocator.html#4a7a649b1a90c358c950e4261e2f3ab1">SetMemTrackerInfo</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* <span class="comment">/*info*/</span>) { } <a name="l00466"></a>00466 }; <a name="l00467"></a>00467 <a name="l00470"></a>00470 <span class="preprocessor">#ifdef _CS_FIXEDSIZEALLOC_DEBUG_DEFAULTED</span> <a name="l00471"></a>00471 <span class="preprocessor"></span><span class="preprocessor">#undef CS_FIXEDSIZEALLOC_DEBUG</span> <a name="l00472"></a>00472 <span class="preprocessor"></span><span class="preprocessor">#undef _CS_FIXEDSIZEALLOC_DEBUG_DEFAULTED</span> <a name="l00473"></a>00473 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00474"></a>00474 <span class="preprocessor"></span> <a name="l00475"></a>00475 <span class="preprocessor">#endif // __CSUTIL_FIXEDSIZEALLOCATOR_H__</span> </pre></div><hr size="1"><address><small>Generated for Crystal Space 1.2.1 by <a href="http://www.doxygen.org/index.html">doxygen</a> 1.5.3 </small></address> </div></body> </html>