<!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/allocator.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/allocator.h</h1><a href="allocator_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"> Copyright (C) 2006 by Frank Richter</span> <a name="l00003"></a>00003 <span class="comment"></span> <a name="l00004"></a>00004 <span class="comment"> This library is free software; you can redistribute it and/or</span> <a name="l00005"></a>00005 <span class="comment"> modify it under the terms of the GNU Library General Public</span> <a name="l00006"></a>00006 <span class="comment"> License as published by the Free Software Foundation; either</span> <a name="l00007"></a>00007 <span class="comment"> version 2 of the License, or (at your option) any later version.</span> <a name="l00008"></a>00008 <span class="comment"></span> <a name="l00009"></a>00009 <span class="comment"> This library is distributed in the hope that it will be useful,</span> <a name="l00010"></a>00010 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <a name="l00011"></a>00011 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span> <a name="l00012"></a>00012 <span class="comment"> Library General Public License for more details.</span> <a name="l00013"></a>00013 <span class="comment"></span> <a name="l00014"></a>00014 <span class="comment"> You should have received a copy of the GNU Library General Public</span> <a name="l00015"></a>00015 <span class="comment"> License along with this library; if not, write to the Free</span> <a name="l00016"></a>00016 <span class="comment"> Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</span> <a name="l00017"></a>00017 <span class="comment">*/</span> <a name="l00018"></a>00018 <a name="l00019"></a>00019 <span class="preprocessor">#ifndef __CS_CSUTIL_ALLOCATOR_H__</span> <a name="l00020"></a>00020 <span class="preprocessor"></span><span class="preprocessor">#define __CS_CSUTIL_ALLOCATOR_H__</span> <a name="l00021"></a>00021 <span class="preprocessor"></span> <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="alignedalloc_8h.html" title="Aligned memory allocations.">csutil/alignedalloc.h</a>"</span> <a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="csutil_2memdebug_8h.html" title="Memory debugging support.">csutil/memdebug.h</a>"</span> <a name="l00028"></a>00028 <a name="l00032"></a>00032 <span class="keyword">namespace </span>CS <a name="l00033"></a>00033 { <a name="l00034"></a>00034 <span class="keyword">namespace </span>Memory <a name="l00035"></a>00035 { <a name="l00039"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html">00039</a> <span class="keyword">class </span><a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html" title="A default memory allocator that allocates with cs_malloc().">AllocatorMalloc</a> <a name="l00040"></a>00040 { <a name="l00041"></a>00041 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00043"></a>00043 <span class="preprocessor"> csMemTrackerInfo* mti;</span> <a name="l00044"></a>00044 <span class="preprocessor"></span><span class="preprocessor"> #endif</span> <a name="l00045"></a>00045 <span class="preprocessor"></span> <span class="keyword">public</span>: <a name="l00046"></a>00046 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00047"></a>00047 <span class="preprocessor"></span> <a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html" title="A default memory allocator that allocates with cs_malloc().">AllocatorMalloc</a>() : mti (0) {} <a name="l00048"></a>00048 <span class="preprocessor"> #endif</span> <a name="l00050"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#fa17d40dbfdc7cf1b6d60ef3b827bc04">00050</a> <span class="preprocessor"> CS_ATTRIBUTE_MALLOC void* Alloc (const size_t n)</span> <a name="l00051"></a>00051 <span class="preprocessor"></span> { <a name="l00052"></a>00052 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00053"></a>00053 <span class="preprocessor"></span> <span class="keywordtype">size_t</span>* p = (<span class="keywordtype">size_t</span>*)<a class="code" href="cssysdef_8h.html#bcc451ff5f6451f8b8bcbe91c762bcf0">cs_malloc</a> (n + <span class="keyword">sizeof</span> (<span class="keywordtype">size_t</span>)); <a name="l00054"></a>00054 *p = n; <a name="l00055"></a>00055 p++; <a name="l00056"></a>00056 <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, 1, <span class="keywordtype">int</span> (n)); <a name="l00057"></a>00057 <span class="keywordflow">return</span> p; <a name="l00058"></a>00058 <span class="preprocessor"> #else</span> <a name="l00059"></a>00059 <span class="preprocessor"></span> <span class="keywordflow">return</span> <a class="code" href="cssysdef_8h.html#bcc451ff5f6451f8b8bcbe91c762bcf0">cs_malloc</a> (n); <a name="l00060"></a>00060 <span class="preprocessor"> #endif</span> <a name="l00061"></a>00061 <span class="preprocessor"></span> } <a name="l00063"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#193aeb12cd9693952e567f62cdca8afe">00063</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#193aeb12cd9693952e567f62cdca8afe" title="Free the block p.">Free</a> (<span class="keywordtype">void</span>* p) <a name="l00064"></a>00064 { <a name="l00065"></a>00065 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00066"></a>00066 <span class="preprocessor"></span> <span class="keywordtype">size_t</span>* x = (<span class="keywordtype">size_t</span>*)p; <a name="l00067"></a>00067 x--; <a name="l00068"></a>00068 <span class="keywordtype">size_t</span> allocSize = *x; <a name="l00069"></a>00069 <a class="code" href="cssysdef_8h.html#7af27c7b7c38c438787a3d2f4e5079c4">cs_free</a> (x); <a name="l00070"></a>00070 <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, -1, -<span class="keywordtype">int</span> (allocSize)); <a name="l00071"></a>00071 <span class="preprocessor"> #else</span> <a name="l00072"></a>00072 <span class="preprocessor"></span> <a class="code" href="cssysdef_8h.html#7af27c7b7c38c438787a3d2f4e5079c4">cs_free</a> (p); <a name="l00073"></a>00073 <span class="preprocessor"> #endif</span> <a name="l00074"></a>00074 <span class="preprocessor"></span> } <a name="l00076"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#862553872b0e7205c02c76b1212886e3">00076</a> <span class="keywordtype">void</span>* <a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#862553872b0e7205c02c76b1212886e3" title="Resize the allocated block p to size newSize.">Realloc</a> (<span class="keywordtype">void</span>* p, <span class="keywordtype">size_t</span> newSize) <a name="l00077"></a>00077 { <a name="l00078"></a>00078 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00079"></a>00079 <span class="preprocessor"></span> <span class="keywordflow">if</span> (p == 0) <span class="keywordflow">return</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#fa17d40dbfdc7cf1b6d60ef3b827bc04" title="Allocate a block of memory of size n.">Alloc</a> (newSize); <a name="l00080"></a>00080 <span class="keywordtype">size_t</span>* x = (<span class="keywordtype">size_t</span>*)p; <a name="l00081"></a>00081 x--; <a name="l00082"></a>00082 <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, -1, -<span class="keywordtype">int</span> (*x)); <a name="l00083"></a>00083 <span class="keywordtype">size_t</span>* np = (<span class="keywordtype">size_t</span>*)<a class="code" href="cssysdef_8h.html#b95c8e1d42fe5f3f3014d8c43606b392">cs_realloc</a> (x, newSize + <span class="keyword">sizeof</span> (<span class="keywordtype">size_t</span>)); <a name="l00084"></a>00084 *np = newSize; <a name="l00085"></a>00085 np++; <a name="l00086"></a>00086 <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, 1, <span class="keywordtype">int</span> (newSize)); <a name="l00087"></a>00087 <span class="keywordflow">return</span> np; <a name="l00088"></a>00088 <span class="preprocessor"> #else</span> <a name="l00089"></a>00089 <span class="preprocessor"></span> <span class="keywordflow">return</span> <a class="code" href="cssysdef_8h.html#b95c8e1d42fe5f3f3014d8c43606b392">cs_realloc</a> (p, newSize); <a name="l00090"></a>00090 <span class="preprocessor"> #endif</span> <a name="l00091"></a>00091 <span class="preprocessor"></span> } <a name="l00093"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#917b34d5fc3507c18d86b9924f74e7dc">00093</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorMalloc.html#917b34d5fc3507c18d86b9924f74e7dc" title="Set the information used for memory tracking.">SetMemTrackerInfo</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* info) <a name="l00094"></a>00094 { <a name="l00095"></a>00095 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00096"></a>00096 <span class="preprocessor"></span> mti = mtiRegister (info); <a name="l00097"></a>00097 <span class="preprocessor"> #else</span> <a name="l00098"></a>00098 <span class="preprocessor"></span> (void)info; <a name="l00099"></a>00099 <span class="preprocessor"> #endif</span> <a name="l00100"></a>00100 <span class="preprocessor"></span> } <a name="l00101"></a>00101 }; <a name="l00102"></a>00102 <a name="l00119"></a>00119 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keywordtype">size_t</span> N, <span class="keyword">class</span> ExcessAllocator = AllocatorMalloc> <a name="l00120"></a><a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html">00120</a> <span class="keyword">class </span><a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html" title="An allocator with a small local buffer.">LocalBufferAllocator</a> : <span class="keyword">public</span> ExcessAllocator <a name="l00121"></a>00121 { <a name="l00122"></a>00122 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">size_t</span> localSize = N * <span class="keyword">sizeof</span> (T); <a name="l00123"></a>00123 <a class="code" href="group__util.html#gdde6aaee8457bee49c2a92621fe22b79" title="unsigned 8-bit integer (0..255)">uint8</a> localBuf[localSize]; <a name="l00124"></a>00124 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00125"></a>00125 <span class="preprocessor"></span> <span class="keywordtype">bool</span> allocation; <a name="l00126"></a>00126 <span class="preprocessor"> #endif</span> <a name="l00127"></a>00127 <span class="preprocessor"></span> <span class="keyword">public</span>: <a name="l00128"></a>00128 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00129"></a>00129 <span class="preprocessor"></span> <a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#1e68557b7c0a4b7602669a1d2d0c330f">LocalBufferAllocator</a> () : allocation (<span class="keyword">false</span>) {} <a name="l00130"></a>00130 <a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#1e68557b7c0a4b7602669a1d2d0c330f">LocalBufferAllocator</a> (<span class="keyword">const</span> ExcessAllocator& xalloc) : <a name="l00131"></a>00131 ExcessAllocator (xalloc), allocation (<span class="keyword">false</span>) {} <a name="l00132"></a>00132 <span class="preprocessor"> #else</span> <a name="l00133"></a><a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#1e68557b7c0a4b7602669a1d2d0c330f">00133</a> <span class="preprocessor"></span> <a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#1e68557b7c0a4b7602669a1d2d0c330f">LocalBufferAllocator</a> () {} <a name="l00134"></a><a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#f10b53133eb1c31903df55d132c8301a">00134</a> <a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#1e68557b7c0a4b7602669a1d2d0c330f">LocalBufferAllocator</a> (<span class="keyword">const</span> ExcessAllocator& xalloc) : <a name="l00135"></a>00135 ExcessAllocator (xalloc) {} <a name="l00136"></a>00136 <span class="preprocessor"> #endif</span> <a name="l00137"></a><a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#2cac68c57f9f48e066c896db4c328125">00137</a> <span class="preprocessor"></span> T* <a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#2cac68c57f9f48e066c896db4c328125">Alloc</a> (<span class="keywordtype">size_t</span> allocSize) <a name="l00138"></a>00138 { <a name="l00139"></a>00139 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00140"></a>00140 <span class="preprocessor"></span> <a class="code" href="cssysdef_8h.html#98cf1ab82994ac8a7c2a25120f6f15d2" title="Same as CS_ASSERT(expr), but additionally prints msg to stderr.">CS_ASSERT_MSG</a>(<span class="stringliteral">"LocalBufferAllocator only allows one allocation a time!"</span>, <a name="l00141"></a>00141 !allocation); <a name="l00142"></a>00142 allocation = <span class="keyword">true</span>; <a name="l00143"></a>00143 <span class="preprocessor"> #endif</span> <a name="l00144"></a>00144 <span class="preprocessor"></span> <span class="keywordflow">if</span> (allocSize <= localSize) <a name="l00145"></a>00145 <span class="keywordflow">return</span> (T*)localBuf; <a name="l00146"></a>00146 <span class="keywordflow">else</span> <a name="l00147"></a>00147 <span class="keywordflow">return</span> (T*)ExcessAllocator::Alloc (allocSize); <a name="l00148"></a>00148 } <a name="l00149"></a>00149 <a name="l00150"></a><a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#f2a4045ea903c443321001a77d28627a">00150</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#f2a4045ea903c443321001a77d28627a">Free</a> (T* mem) <a name="l00151"></a>00151 { <a name="l00152"></a>00152 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00153"></a>00153 <span class="preprocessor"></span> <a class="code" href="cssysdef_8h.html#98cf1ab82994ac8a7c2a25120f6f15d2" title="Same as CS_ASSERT(expr), but additionally prints msg to stderr.">CS_ASSERT_MSG</a>(<span class="stringliteral">"Free() without prior allocation"</span>, <a name="l00154"></a>00154 allocation); <a name="l00155"></a>00155 allocation = <span class="keyword">false</span>; <a name="l00156"></a>00156 <span class="preprocessor"> #endif</span> <a name="l00157"></a>00157 <span class="preprocessor"></span> <span class="keywordflow">if</span> (mem != (T*)localBuf) ExcessAllocator::Free (mem); <a name="l00158"></a>00158 } <a name="l00159"></a>00159 <a name="l00160"></a>00160 <span class="comment">// The 'relevantcount' parameter should be the number of items</span> <a name="l00161"></a>00161 <span class="comment">// in the old array that are initialized.</span> <a name="l00162"></a><a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#19c8a164c882063e6f466adf9c16661b">00162</a> <span class="keywordtype">void</span>* <a class="code" href="classCS_1_1Memory_1_1LocalBufferAllocator.html#19c8a164c882063e6f466adf9c16661b">Realloc</a> (<span class="keywordtype">void</span>* p, <span class="keywordtype">size_t</span> newSize) <a name="l00163"></a>00163 { <a name="l00164"></a>00164 <span class="preprocessor"> #ifdef CS_DEBUG</span> <a name="l00165"></a>00165 <span class="preprocessor"></span> <a class="code" href="cssysdef_8h.html#98cf1ab82994ac8a7c2a25120f6f15d2" title="Same as CS_ASSERT(expr), but additionally prints msg to stderr.">CS_ASSERT_MSG</a>(<span class="stringliteral">"Realloc() without prior allocation"</span>, <a name="l00166"></a>00166 allocation); <a name="l00167"></a>00167 <span class="preprocessor"> #endif</span> <a name="l00168"></a>00168 <span class="preprocessor"></span> <span class="keywordflow">if</span> (p == localBuf) <a name="l00169"></a>00169 { <a name="l00170"></a>00170 <span class="keywordflow">if</span> (newSize <= localSize) <a name="l00171"></a>00171 <span class="keywordflow">return</span> p; <a name="l00172"></a>00172 <span class="keywordflow">else</span> <a name="l00173"></a>00173 { <a name="l00174"></a>00174 p = ExcessAllocator::Alloc (newSize); <a name="l00175"></a>00175 memcpy (p, localBuf, localSize); <a name="l00176"></a>00176 <span class="keywordflow">return</span> p; <a name="l00177"></a>00177 } <a name="l00178"></a>00178 } <a name="l00179"></a>00179 <span class="keywordflow">else</span> <a name="l00180"></a>00180 { <a name="l00181"></a>00181 <span class="keywordflow">if</span> (newSize <= localSize) <a name="l00182"></a>00182 { <a name="l00183"></a>00183 memcpy (localBuf, p, newSize); <a name="l00184"></a>00184 ExcessAllocator::Free (p); <a name="l00185"></a>00185 <span class="keywordflow">return</span> localBuf; <a name="l00186"></a>00186 } <a name="l00187"></a>00187 <span class="keywordflow">else</span> <a name="l00188"></a>00188 <span class="keywordflow">return</span> ExcessAllocator::Realloc (p, newSize); <a name="l00189"></a>00189 } <a name="l00190"></a>00190 } <a name="l00191"></a>00191 <span class="keyword">using</span> ExcessAllocator::SetMemTrackerInfo; <a name="l00192"></a>00192 }; <a name="l00193"></a>00193 <a name="l00199"></a>00199 <span class="keyword">template</span> <<span class="keywordtype">size_t</span> A = 1> <a name="l00200"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html">00200</a> <span class="keyword">class </span><a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html" title="This class implements an allocator policy which aligns the first element on given...">AllocatorAlign</a> <a name="l00201"></a>00201 { <a name="l00202"></a>00202 <span class="keyword">public</span>: <a name="l00206"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#0af0311b3d96698570df51a452fe5931">00206</a> <span class="keyword">static</span> <span class="keyword">inline</span> CS_ATTRIBUTE_MALLOC <span class="keywordtype">void</span>* <a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#0af0311b3d96698570df51a452fe5931" title="Allocate a raw block of given size.">Alloc</a> (<span class="keywordtype">size_t</span> size) <a name="l00207"></a>00207 { <a name="l00208"></a>00208 <span class="keywordflow">return</span> <a class="code" href="namespaceCS_1_1Memory.html#c840bbf3f092cb8d7cacb3b886930444" title="Allocate a block of memory, with the start address being aligned to a multiple of...">AlignedMalloc</a> (size, A); <a name="l00209"></a>00209 } <a name="l00210"></a>00210 <a name="l00215"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#91e940f351ae5f185115a8dba225ea96">00215</a> <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#91e940f351ae5f185115a8dba225ea96" title="Free a block.">Free</a> (<span class="keywordtype">void</span>* p) <a name="l00216"></a>00216 { <a name="l00217"></a>00217 <a class="code" href="namespaceCS_1_1Memory.html#24e1a01a11f8cd839a625bfe10c4b984" title="Free a block of memory allocated with AlignedMalloc.">AlignedFree</a> (p); <a name="l00218"></a>00218 } <a name="l00219"></a>00219 <a name="l00220"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#1459d0da19991a008eaaace55da6c1f1">00220</a> <span class="keywordtype">void</span>* <a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#1459d0da19991a008eaaace55da6c1f1">Realloc</a> (<span class="keywordtype">void</span>* p, <span class="keywordtype">size_t</span> newSize) <a name="l00221"></a>00221 { <a name="l00222"></a>00222 <span class="keywordflow">return</span> <a class="code" href="namespaceCS_1_1Memory.html#80bd77b1f5274e7cda7c95f26fff0d10" title="Reallocate a block of memory with a new size, with the start address being aligned...">AlignedRealloc</a> (p, newSize, A); <a name="l00223"></a>00223 } <a name="l00224"></a>00224 <a name="l00225"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#ad87ab278f8c990889b4cf2b991fda9b">00225</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorAlign.html#ad87ab278f8c990889b4cf2b991fda9b">SetMemTrackerInfo</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* info) <a name="l00226"></a>00226 { <a name="l00227"></a>00227 (void)info; <a name="l00228"></a>00228 } <a name="l00229"></a>00229 }; <a name="l00230"></a>00230 <a name="l00236"></a>00236 <span class="keyword">template</span><<span class="keywordtype">bool</span> Reallocatable = true> <a name="l00237"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html">00237</a> <span class="keyword">class </span><a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html" title="A default memory allocator that allocates using new char[].">AllocatorNewChar</a> <a name="l00238"></a>00238 { <a name="l00239"></a>00239 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00241"></a>00241 <span class="preprocessor"> csMemTrackerInfo* mti;</span> <a name="l00242"></a>00242 <span class="preprocessor"></span><span class="preprocessor"> #endif</span> <a name="l00243"></a>00243 <span class="preprocessor"></span> <span class="keyword">public</span>: <a name="l00244"></a>00244 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00245"></a>00245 <span class="preprocessor"></span> <a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html" title="A default memory allocator that allocates using new char[].">AllocatorNewChar</a>() : mti (0) {} <a name="l00246"></a>00246 <span class="preprocessor"> #endif</span> <a name="l00248"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#b98488bdaf2123a63109ad58ce5a72af">00248</a> <span class="preprocessor"> CS_ATTRIBUTE_MALLOC void* Alloc (const size_t n)</span> <a name="l00249"></a>00249 <span class="preprocessor"></span> { <a name="l00250"></a>00250 <span class="comment">// Note that with memtracking we store the alloc'ed size anyway.</span> <a name="l00251"></a>00251 <span class="preprocessor"> #ifndef CS_MEMORY_TRACKER</span> <a name="l00252"></a>00252 <span class="preprocessor"></span> <span class="keywordflow">if</span> (!Reallocatable) <a name="l00253"></a>00253 { <a name="l00254"></a>00254 <span class="keywordflow">return</span> <span class="keyword">new</span> <span class="keywordtype">char</span>[n]; <a name="l00255"></a>00255 } <a name="l00256"></a>00256 <span class="preprocessor"> #endif</span> <a name="l00257"></a>00257 <span class="preprocessor"></span> <span class="keywordtype">size_t</span>* p = (<span class="keywordtype">size_t</span>*)<span class="keyword">new</span> <span class="keywordtype">char</span>[n + <span class="keyword">sizeof</span> (<span class="keywordtype">size_t</span>)]; <a name="l00258"></a>00258 *p = n; <a name="l00259"></a>00259 p++; <a name="l00260"></a>00260 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00261"></a>00261 <span class="preprocessor"></span> <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, 1, <span class="keywordtype">int</span> (n)); <a name="l00262"></a>00262 <span class="preprocessor"> #endif</span> <a name="l00263"></a>00263 <span class="preprocessor"></span> <span class="keywordflow">return</span> p; <a name="l00264"></a>00264 } <a name="l00266"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#f6ecccbadc0986c3a06949e57d94ca8b">00266</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#f6ecccbadc0986c3a06949e57d94ca8b" title="Free the block p.">Free</a> (<span class="keywordtype">void</span>* p) <a name="l00267"></a>00267 { <a name="l00268"></a>00268 <span class="preprocessor"> #ifndef CS_MEMORY_TRACKER</span> <a name="l00269"></a>00269 <span class="preprocessor"></span> <span class="keywordflow">if</span> (!Reallocatable) <a name="l00270"></a>00270 { <a name="l00271"></a>00271 <span class="keyword">delete</span>[] (<span class="keywordtype">char</span>*)p; <a name="l00272"></a>00272 <span class="keywordflow">return</span>; <a name="l00273"></a>00273 } <a name="l00274"></a>00274 <span class="preprocessor"> #endif</span> <a name="l00275"></a>00275 <span class="preprocessor"></span> <span class="keywordtype">size_t</span>* x = (<span class="keywordtype">size_t</span>*)p; <a name="l00276"></a>00276 x--; <a name="l00277"></a>00277 <span class="keywordtype">size_t</span> allocSize = *x; <a name="l00278"></a>00278 <span class="keyword">delete</span>[] (<span class="keywordtype">char</span>*)x; <a name="l00279"></a>00279 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00280"></a>00280 <span class="preprocessor"></span> <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, -1, -<span class="keywordtype">int</span> (allocSize)); <a name="l00281"></a>00281 <span class="preprocessor"> #else</span> <a name="l00282"></a>00282 <span class="preprocessor"></span> (void)allocSize; <a name="l00283"></a>00283 <span class="preprocessor"> #endif</span> <a name="l00284"></a>00284 <span class="preprocessor"></span> } <a name="l00286"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#4b48261b650a74c55122f1573b327f6d">00286</a> <span class="keywordtype">void</span>* <a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#4b48261b650a74c55122f1573b327f6d" title="Resize the allocated block p to size newSize.">Realloc</a> (<span class="keywordtype">void</span>* p, <span class="keywordtype">size_t</span> newSize) <a name="l00287"></a>00287 { <a name="l00288"></a>00288 <a class="code" href="cssysdef_8h.html#98cf1ab82994ac8a7c2a25120f6f15d2" title="Same as CS_ASSERT(expr), but additionally prints msg to stderr.">CS_ASSERT_MSG</a>(<span class="stringliteral">"Realloc() called on non-reallocatable AllocatorNewChar"</span>, <a name="l00289"></a>00289 Reallocatable); <a name="l00290"></a>00290 <span class="keywordflow">if</span> (p == 0) <span class="keywordflow">return</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#b98488bdaf2123a63109ad58ce5a72af" title="Allocate a block of memory of size n.">Alloc</a> (newSize); <a name="l00291"></a>00291 <span class="keywordtype">size_t</span>* x = (<span class="keywordtype">size_t</span>*)p; <a name="l00292"></a>00292 x--; <a name="l00293"></a>00293 <span class="keywordtype">size_t</span> oldSize = *x; <a name="l00294"></a>00294 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00295"></a>00295 <span class="preprocessor"></span> <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, -1, -<span class="keywordtype">int</span> (oldSize)); <a name="l00296"></a>00296 <span class="preprocessor"> #endif</span> <a name="l00297"></a>00297 <span class="preprocessor"></span> <span class="keywordtype">size_t</span>* np = <a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#b98488bdaf2123a63109ad58ce5a72af" title="Allocate a block of memory of size n.">Alloc</a> (newSize); <a name="l00298"></a>00298 <span class="keywordflow">if</span> (newSize < oldSize) <a name="l00299"></a>00299 memcpy (np, p, newSize); <a name="l00300"></a>00300 <span class="keywordflow">else</span> <a name="l00301"></a>00301 memcpy (np, p, oldSize); <a name="l00302"></a>00302 <a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#f6ecccbadc0986c3a06949e57d94ca8b" title="Free the block p.">Free</a> (p); <a name="l00303"></a>00303 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00304"></a>00304 <span class="preprocessor"></span> <span class="keywordflow">if</span> (mti) mtiUpdateAmount (mti, 1, <span class="keywordtype">int</span> (newSize)); <a name="l00305"></a>00305 <span class="preprocessor"> #endif</span> <a name="l00306"></a>00306 <span class="preprocessor"></span> <span class="keywordflow">return</span> np; <a name="l00307"></a>00307 } <a name="l00309"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#89ee4c235459b571342b453650e3793f">00309</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorNewChar.html#89ee4c235459b571342b453650e3793f" title="Set the information used for memory tracking.">SetMemTrackerInfo</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* info) <a name="l00310"></a>00310 { <a name="l00311"></a>00311 <span class="preprocessor"> #ifdef CS_MEMORY_TRACKER</span> <a name="l00312"></a>00312 <span class="preprocessor"></span> mti = mtiRegister (info); <a name="l00313"></a>00313 <span class="preprocessor"> #else</span> <a name="l00314"></a>00314 <span class="preprocessor"></span> (void)info; <a name="l00315"></a>00315 <span class="preprocessor"> #endif</span> <a name="l00316"></a>00316 <span class="preprocessor"></span> } <a name="l00317"></a>00317 }; <a name="l00318"></a>00318 <a name="l00329"></a>00329 <span class="keyword">template</span><<span class="keyword">typename</span> T> <a name="l00330"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html">00330</a> <span class="keyword">class </span><a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html" title="A default memory allocator that allocates using new T[].">AllocatorNew</a> <a name="l00331"></a>00331 { <a name="l00332"></a>00332 <span class="keyword">public</span>: <a name="l00334"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#ff293dfae0db84989bb3f5a7cc869ebc">00334</a> CS_ATTRIBUTE_MALLOC <span class="keywordtype">void</span>* <a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#ff293dfae0db84989bb3f5a7cc869ebc" title="Allocate a block of memory of size n.">Alloc</a> (<span class="keyword">const</span> <span class="keywordtype">size_t</span> n) <a name="l00335"></a>00335 { <a name="l00336"></a>00336 <span class="keywordflow">return</span> <span class="keyword">new</span> T[((n + <span class="keyword">sizeof</span>(T) - 1) / <span class="keyword">sizeof</span>(T)) * <span class="keyword">sizeof</span>(T)]; <a name="l00337"></a>00337 } <a name="l00339"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#411c7db4183f7a676744e42690a4f896">00339</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#411c7db4183f7a676744e42690a4f896" title="Free the block p.">Free</a> (<span class="keywordtype">void</span>* p) <a name="l00340"></a>00340 { <a name="l00341"></a>00341 <span class="keyword">delete</span>[] (T*)p; <a name="l00342"></a>00342 } <a name="l00344"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#975b45b1341665712bc187aa87f9b625">00344</a> <span class="keywordtype">void</span>* <a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#975b45b1341665712bc187aa87f9b625" title="Resize the allocated block p to size newSize.">Realloc</a> (<span class="keywordtype">void</span>* p, <span class="keywordtype">size_t</span> newSize) <a name="l00345"></a>00345 { <a name="l00346"></a>00346 <a class="code" href="cssysdef_8h.html#98cf1ab82994ac8a7c2a25120f6f15d2" title="Same as CS_ASSERT(expr), but additionally prints msg to stderr.">CS_ASSERT_MSG</a>(<span class="stringliteral">"Realloc() called on AllocatorNew"</span>, <span class="keyword">false</span>); <a name="l00347"></a>00347 <span class="keywordflow">return</span> 0; <a name="l00348"></a>00348 } <a name="l00350"></a><a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#e1320d04e6f43c370767ee603d063005">00350</a> <span class="keywordtype">void</span> <a class="code" href="classCS_1_1Memory_1_1AllocatorNew.html#e1320d04e6f43c370767ee603d063005" title="Set the information used for memory tracking.">SetMemTrackerInfo</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* <span class="comment">/*info*/</span>) <a name="l00351"></a>00351 { <a name="l00352"></a>00352 } <a name="l00353"></a>00353 }; <a name="l00354"></a>00354 <a name="l00355"></a>00355 <a name="l00361"></a>00361 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> Allocator> <a name="l00362"></a><a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html">00362</a> <span class="keyword">struct </span><a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html" title="Class to store a pointer that is allocated from Allocator, to eliminate overhead...">AllocatorPointerWrapper</a> : <span class="keyword">public</span> Allocator <a name="l00363"></a>00363 { <a name="l00365"></a><a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#e3861de7c5f1a38d76881b2b47b51fa2">00365</a> T* <a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#e3861de7c5f1a38d76881b2b47b51fa2" title="The allocated pointer.">p</a>; <a name="l00366"></a>00366 <a name="l00367"></a><a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#aee8e659cce35cba8f31cc003a642a3d">00367</a> <a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#aee8e659cce35cba8f31cc003a642a3d">AllocatorPointerWrapper</a> () {} <a name="l00368"></a><a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#8a05f8c06315a8c97aef116eeac36e1b">00368</a> <a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#aee8e659cce35cba8f31cc003a642a3d">AllocatorPointerWrapper</a> (<span class="keyword">const</span> Allocator& alloc) : <a name="l00369"></a>00369 Allocator (alloc) {} <a name="l00370"></a><a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#a7c9ce443629475d2e9a1c8dba2b6a89">00370</a> <a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#aee8e659cce35cba8f31cc003a642a3d">AllocatorPointerWrapper</a> (T* <a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#e3861de7c5f1a38d76881b2b47b51fa2" title="The allocated pointer.">p</a>) : p (p) {} <a name="l00371"></a><a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#15e3a200d635ef663a0858351ca188b0">00371</a> <a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#aee8e659cce35cba8f31cc003a642a3d">AllocatorPointerWrapper</a> (<span class="keyword">const</span> Allocator& alloc, T* <a class="code" href="structCS_1_1Memory_1_1AllocatorPointerWrapper.html#e3861de7c5f1a38d76881b2b47b51fa2" title="The allocated pointer.">p</a>) : <a name="l00372"></a>00372 Allocator (alloc), p (p) {} <a name="l00373"></a>00373 }; <a name="l00374"></a>00374 <a name="l00375"></a>00375 } <span class="comment">// namespace Memory</span> <a name="l00376"></a>00376 } <span class="comment">// namespace CS</span> <a name="l00377"></a>00377 <a name="l00380"></a>00380 <span class="preprocessor">#endif // __CS_CSUTIL_ALLOCATOR_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>