<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>ReCode | ReCode / Recent Changes</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.RecentChanges?action=rss</link>
<description>ReCode.Recent Changes</description>
<lastBuildDate>Wed, 08 Sep 2010 02:48:16 GMT</lastBuildDate>
<item>
<title>ReCode / Google Animation</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.GoogleAnimation</link>
<description><![CDATA[<p>Lately the Google logo is nice and animated. Or was, depending on when you read this post. Anyway, this is what it looks/looked like:
</p>
<div class='vspace'></div><div  style='text-align: center;'><img src='http://www.revergestudios.com/reblog/uploads/ReCode/google.gif' alt='' title='' /></div>
<p class='vspace'>The colorful balls bounce away from the cursor and that's lots of fun I suppose, unless you're a geek and you notice that OMG this stupid little animation is taking 100% of my CPU time running at no more than 10 frames per second! And that comes from the same company whose product happily announces that it has found more than a billion results in 0.25 seconds. I know, not a fair comparison, but still -- I wonder how many millennia would the search engine need to find a billion matches had it been running on Javascript or whatever advanced, Just-In-Time compiled, Virtual Machine powered, Garbage Collected, uber-secure engine powers the colorful balls.
</p>
<p class='vspace'>What's the big deal, you say? It's just a bunch of colorful balls, they'll be gone in a day or two!
</p>
<p class='vspace'>I wish the same could be said about Java in general. Whenever I run any Java-powered program on my computer, even when the program is idle, the OS often needs to power up the fan. To collect a dead object perhaps, who knows. The point is, ever since Java's arrival, we've heard the same mantra over and over: <em>"Oh, it's slow now but don't you worry it'll get faster. Soon. Like, tomorrow. JIT FTW!"</em>
</p>
<p class='vspace'>I've lost count on how many years I've been hearing that.
</p>
<p class='vspace'>But aren't these colorful dots fun anyway? Not if you realize how wasteful and inefficient the whole thing is. It really is a <a class='urllink' href='http://en.wikipedia.org/wiki/Googleplex' rel='nofollow'>Googleplex</a> slower than it should be. :)
</p>
]]></description>
<dc:contributor>emil</dc:contributor>
<dc:date>2010-09-08T02:48:16Z</dc:date>
<pubDate>Wed, 08 Sep 2010 02:48:16 GMT</pubDate>
</item>
<item>
<title>ReCode / From DirectX 9 to DirectX 10/11</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.FromDirectX9ToDirectX10And11</link>
<description><![CDATA[<p class='vspace'>Recently, here at Reverge we have been adding DirectX 10/11 support to some of our internal libraries. In the process we discovered a couple of problems in the new API design.
</p>
<div class='vspace'></div><h2>Gone: CreateRenderTarget</h2>
<p>In DirectX 9 there were three types of "surfaces":
</p>
<div class='vspace'></div><ul><li>Pure textures, which the GPU can only read,
</li><li>Render targets, which the GPU can only write (render) into, and
</li><li>Textures that are also render targets, which the GPU can read and write.
</li></ul><p class='vspace'>With this system, pure textures can be used by the CPU to efficiently write data to video memory for the GPU to read and render targets can be used to efficiently read data that was written by the GPU.
</p>
<p class='vspace'>Textures that are also render targets can not be accessed by the CPU at all, but that's fine because if the CPU needs to read the result of a GPU pass, that pass can render into a render target (that is not a texture) which the CPU <em>can</em> read.
</p>
<p class='vspace'>In DirectX 10/11 render targets that are not textures are no longer supported, so while the CPU can efficiently write data for the GPU to read, there is no efficient way for the application to read data written by the GPU. Instead, it is required to copy the data from a render target texture into another (non-render-target) texture which can then be mapped by the CPU for reading
</p>
<div class='vspace'></div><h2>D3D9 LockRect vs. D3D10/11 Map</h2>
<p>In the good old DirectX 9 days, if the application needed to write data into a texture, it called the LockRect function to get a pointer. The data was written in the memory pointed by the pointer, and then UnlockRect was called.
</p>
<p class='vspace'>The nice thing about this API is that it does not specify what kind of memory the returned pointer points. Depending on texture creation flags, the requested access (read/write/write-discard), the current state of the Direct3D device, and the capabilities of the graphics card, the driver might choose to map the texture video memory directly, or it might map a separate temporary video memory buffer, or it might even return a pointer to a system memory buffer which is copied to video memory when UnlockRect is called.
</p>
<p class='vspace'>Instead, in DirectX 10 and 11 there seems to be only one choice: video memory is mapped directly if possible; if not, we get an error.
</p>
<p class='vspace'>The new semantics are in fact in accord with a major overall architectural shift introduced with DirectX 10. It calls for removing the so-called "API magic": undocumented internal behavior designed to hide inefficiencies and/or lack of capabilities in the hardware. The rationale for avoiding API magic is that it makes application performance inconsistent across different hardware and driver versions.
</p>
<p class='vspace'>It is true that depending on the actual behavior of LoctRect, the application performance can vary significantly. That is indeed a downside, but I wonder if the designers of DirectX 10 and 11 are old enough to remember DirectX 3. It also lacked any API magic. The idea was that the application would create execute buffers that would reside on the graphics card, removing all abstraction and maximizing performance. Yet, because the interface was designed with hardware engineers in mind, it was basically unusable. The more abstract DirectX 5 interface was much easier to use yet (surprise!) didn't lead to lower frame rates.
</p>
<p class='vspace'>Similarly, the more abstract interface of LockRect in DirectX 9 is more practical than the less sophisticated DirectX 10/11 Map behavior. Sure, in the simplest use cases the difference is negligible, but then again in the simplest cases LockRect wouldn't use API magic either, so the user would not be penalized by it. It is the non-trivial, less frequent yet performance-critical uses that need a more abstract interface.
</p>
<p class='vspace'>For example, if the texture is created and locked with the correct flags, DirectX 9 could let the application download new data to a texture even while it is currently used by a shader, which isn't possible in DirectX 10/11. How does that work in DirectX 9? If write-discard locking is requested, the driver could map a separate temporary video memory buffer for access by the application. As soon as the device is done using the texture, the new memory buffer can be associated with the texture at the cost of a pointer swap.
</p>
<p class='vspace'>It can be argued that this is done behind the user's back: if that's what the application wanted to do it could do it itself right? The problem is that this type of systems are difficult to program. Besides, even if the user has the skill and resources needed to provide a high-quality implementation, they can't possibly compete with a driver programmer who is not only a specialist but also has the advantage of writing hardware-specific code and is enjoying much lower-level access to the graphics card.
<a name='comment1' id='comment1'></a>
</p><div class='messagehead' > 
<h5>OvermindDL1  &#8212;  <span style='font-size:83%'>16 August 2010, 16:40</span>  </h5>
</div><div class='messageitem' > 
<p>What about how the latest OpenGL handles such things, how does it compare?
</p></div>
<p class='vspace'><a name='comment2' id='comment2'></a>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>17 August 2010, 12:14</span>  </h5>
</div><div class='messageitem' > 
<p>As far as I know, in terms of video memory access, OpenGL's API only provides memcpy-type functionality.
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>Emil</dc:contributor>
<dc:date>2010-08-17T19:14:25Z</dc:date>
<pubDate>Tue, 17 Aug 2010 19:14:25 GMT</pubDate>
</item>
<item>
<title>ReCode / Boost LA</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.BoostLA</link>
<description><![CDATA[<p>I am guessing there is some trace of "proper" game programming mentality in me after all. :) Going against <a class='wikilink' href='http://www.revergestudios.com/reblog/index.php?n=ReCode.MathLibrary'>my previous rant</a>, I've designed "yet another" vector/matrix math library and I've submitted it for a preliminary <a class='urllink' href='http://www.boost.org' rel='nofollow'>Boost</a> review.
</p>
<p class='vspace'>Why do I think that writing this library was a good idea? Because it makes no sense for any of us to have to spell out how a 3x3 matrix is multiplied by another 3x3 matrix; <em>there should be a way to express that algorithm generically and apply it to any and all 3x3 matrix types in the world.</em>
</p>
<p class='vspace'>The only tricky part is that operations such as matrix multiplication should use operator overloads (seriously, it's retarded not to) and that presents a challenge: how do you define type-safe operator overloads without using specific matrix types? That's basically what (Boost) LA pulls off, using <a class='urllink' href='http://www.google.com/#q=sfinae' rel='nofollow'>SFINAE</a>.
</p>
<div class='vspace'></div><h2>The scoop</h2>
<p>A <strong><em>user-defined</em></strong> vector type float3 can be introduced to (Boost) LA like this:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="co2">#include &lt;boost/la/vector_traits.hpp&gt; //Note: this library is NOT part of Boost</span><br />
<br />
<span class="kw1">struct</span> float3 <span class="br0">&#123;</span> <span class="kw2">float</span> a<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>; <span class="br0">&#125;</span>;<br />
<br />
<span class="kw1">namespace</span><br />
boost<br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw1">namespace</span><br />
&#160; &#160; la<br />
&#160; &#160; &#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; &#160; &#160; <span class="kw1">template</span> &lt;&gt;<br />
&#160; &#160; &#160; &#160; <span class="kw1">struct</span><br />
&#160; &#160; &#160; &#160; vector_traits&lt;float3&gt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="kw1">static</span> <span class="kw2">int</span> <span class="kw2">const</span> dim=<span class="nu0">3</span>;<br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="kw1">typedef</span> <span class="kw2">float</span> scalar_type;<br />
<br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="kw1">template</span> &lt;int I&gt; <span class="kw1">static</span> <span class="kw1">inline</span> scalar_type &amp; w<span class="br0">&#40;</span> float3 &amp; v <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; <span class="br0">&#123;</span> <span class="kw1">return</span> v.<span class="me1">a</span><span class="br0">&#91;</span>I<span class="br0">&#93;</span>; <span class="br0">&#125;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="kw1">template</span> &lt;int I&gt; <span class="kw1">static</span> <span class="kw1">inline</span> scalar_type r<span class="br0">&#40;</span> float3 <span class="kw2">const</span> &amp; v <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; <span class="br0">&#123;</span> <span class="kw1">return</span> v.<span class="me1">a</span><span class="br0">&#91;</span>I<span class="br0">&#93;</span>; <span class="br0">&#125;</span><br />
<br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="kw1">static</span> <span class="kw1">inline</span> scalar_type &amp; iw<span class="br0">&#40;</span> <span class="kw2">int</span> i, float3 &amp; v <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; <span class="br0">&#123;</span> <span class="kw1">return</span> v.<span class="me1">a</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>; <span class="br0">&#125;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="kw1">static</span> <span class="kw1">inline</span> scalar_type ir<span class="br0">&#40;</span> <span class="kw2">int</span> i, float3 <span class="kw2">const</span> &amp; v <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; <span class="br0">&#123;</span> <span class="kw1">return</span> v.<span class="me1">a</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>; <span class="br0">&#125;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; <span class="br0">&#125;</span>;<br />
&#160; &#160; &#160; &#160; <span class="br0">&#125;</span><br />
&#160; &#160; <span class="br0">&#125;</span></div></div>
</div>
<p class='vspace'>After a similar specialization of the matrix_traits template for a user-defined 3x3 matrix type float33, a full range of vector and matrix operations defined in (Boost) LA headers become available automatically:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;">float3 v;<br />
v|X = <span class="nu0">0</span>;<br />
v|Y = <span class="nu0">0</span>;<br />
v|Z = <span class="nu0">7</span>;<br />
<span class="kw2">float</span> vmag = magnitude<span class="br0">&#40;</span>v<span class="br0">&#41;</span>;<br />
float33 m = rotx_matrix&lt;<span class="nu0">3</span>&gt;<span class="br0">&#40;</span><span class="nu0">3</span>.14159f<span class="br0">&#41;</span>;<br />
float3 vrot = m * v;</div></div>
</div>
<p class='vspace'>The full documentation and source code, released under the Boost Software License, is <a class='urllink' href='http://www.revergestudios.com/boost-la' rel='nofollow'>here</a>.
</p><div class='messagehead' > 
<h5>Arseny Kapoulkine  &#8212;  <span style='font-size:83%'>20 October 2009, 22:16</span>  </h5>
</div><div class='messageitem' > 
<p>This suits boost very much, but is anything but proper. IMHO.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>gemicha  &#8212;  <span style='font-size:83%'>20 October 2009, 23:02</span>  </h5>
</div><div class='messageitem' > 
<p>v|X = 0; .... You gotta be kidding me 
</p></div>
<p class='vspace'><a name='comment3' id='comment3'></a>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>21 October 2009, 01:38</span>  </h5>
</div><div class='messageitem' > 
<p>I had posted a somewhat sarcastic reply to gemicha, but I realize that his comment was not unreasonable with only this example in mind.
</p>
<p class='vspace'>The expression v|X should be read as "v pipe X". It is logical part of a more general system of view proxies in Boost LA. For example, you could say m|col&lt;2&gt;|YXZ which gets you a (mutable) reference to column 2 of m with its X and Y components swapped.
</p>
<p class='vspace'>Gemicha's comment also prompted me to write this <a class='urllink' href='http://www.revergestudios.com/boost-la/operator_bitor_rationale.html' rel='nofollow'>rationale</a>.
</p></div>
<p class='vspace'><a name='comment4' id='comment4'></a>
</p><div class='messagehead' > 
<h5>Alex  &#8212;  <span style='font-size:83%'>02 January 2010, 22:07</span>  </h5>
</div><div class='messageitem' > 
<p>I've experimented with using this library in a project, and so far I like what I see. I do have to agree that the pipe syntax is rather unusual. IMHO the following would be nicer syntax and accomplish the same stuff as detailed in the rationale:
</p>
<div class='vspace'></div><pre class='escaped'>
v[X] = 0;  // before v|X = 0;
m2[col&lt;1&gt;][YXZ] = m1 * v; // before: m2|col&lt;1&gt;|YXZ = m1 * v;
m2[col&lt;1&gt;] = m1 * v[YXZ]; // no precedence issues. before: m2|col&lt;1&gt; = m1 * (v|YXZ);
</pre>
<p class='vspace'>This would, of course, be accomplished by overloading the [] operator for typeof(XYZ) index type variants etc. In fact, this syntax can be taken slightly further:
</p>
<div class='vspace'></div><pre class='escaped'>
v2 = v1[Z,X,Y]; // instead of: v2 = v1|ZXY;
v2 = v1[Y,-X];  // v1 and v2 are now perpendicular. Not possible with current syntax.
</pre>
<p class='vspace'>Here, the structs X, Y, and Z would inherit from a common base that overloads the comma and negation operators. I like this, not only because it's more flexible, but also because it looks more natural.
</p>
<p class='vspace'>I'd love to hear your thoughts on this!
</p></div>
<p class='vspace'><a name='comment5' id='comment5'></a>
</p><div class='messagehead' > 
<h5>Alex  &#8212;  <span style='font-size:83%'>02 January 2010, 22:15</span>  </h5>
</div><div class='messageitem' > 
<p>Sorry, for the very last indexing example: v1 and v2 are 2D vectors, unlike the previous examples. I realized I should've named them differently so as to avoid any confusion.
</p></div>
<p class='vspace'><a name='comment6' id='comment6'></a>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>08 January 2010, 13:02</span>  </h5>
</div><div class='messageitem' > 
<p>Using op[] is better indeed, but can not be done because it must be a member. Otherwise, it would be the natural choice, same as in HLSL.
</p></div>
<p class='vspace'><a name='comment7' id='comment7'></a>
</p><div class='messagehead' > 
<h5>Gregory  &#8212;  <span style='font-size:83%'>14 January 2010, 16:17</span>  </h5>
</div><div class='messageitem' > 
<p>From ISO-IEC 14882-2003
</p>
<p class='vspace'>"13.5.5 Subscripting
</p>
<p class='vspace'>operator[] shall be a non-static member function with exactly one parameter."
</p>
<p class='vspace'>Hence, v1[Z,X,Y] is not possible.
</p></div>
<p class='vspace'><a name='comment8' id='comment8'></a>
</p><div class='messagehead' > 
<h5>Alex  &#8212;  <span style='font-size:83%'>14 January 2010, 21:21</span>  </h5>
</div><div class='messageitem' > 
<p>@Emil
Oh, that's right. Bummer. I guess the only way around that is by way of a macro that would need to be placed inside the vector class for op[] to become available. It's slightly more intrusive than the current way of registering a custom vector class, but I can't seem to think of any real downsides other than that. What do you think?
</p>
<p class='vspace'>@Gregory
It is possible as the comma operator can be overloaded for *structs* X/Y/Z. So (X,Y,Z) would return some callable type that is passed to the [] operator, much like XYZ is passed to the |operator in the current implementation.
</p></div>
<p class='vspace'><a name='comment9' id='comment9'></a>
</p><div class='messagehead' > 
<h5>Alex  &#8212;  <span style='font-size:83%'>14 January 2010, 22:02</span>  </h5>
</div><div class='messageitem' > 
<p>Hm, I realized that the macro is of no use if you're employing a vector class as defined by some external library. Being able to only use op[] for user but not library classes would probably be more confusing than the pipe syntax and its precedence issues.
</p>
<p class='vspace'>Ultimately, the end the user is free to define their own op[] that simply forwards to op|, and this is what I'll most likely do for my own project.
</p></div>
<p class='vspace'><a name='comment10' id='comment10'></a>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>15 January 2010, 11:05</span>  </h5>
</div><div class='messageitem' > 
<p>Let's use the Boost Developers mailing list for this discussion, others might want to participate.
</p>
<p class='vspace'><a class='urllink' href='http://lists.boost.org/mailman/listinfo.cgi/boost' rel='nofollow'>http://lists.boost.org/mailman/listinfo.cgi/boost</a>
</p></div>
<p class='vspace'><a name='comment11' id='comment11'></a>
</p><div class='messagehead' > 
<h5>John Smith  &#8212;  <span style='font-size:83%'>15 July 2010, 17:44</span>  </h5>
</div><div class='messageitem' > 
<p>Just wanted to add that we are using Boost LA in conjunction with some OpenGL code and it is really nice. We were able to roll our own Matrix/Vector types that were compatible with OpenGL's matrix/vector types without being float[i][j] arrays that are way too complicated to work with.
</p>
<p class='vspace'>Good luck with this library, hopefully it makes it into boost.
</p>
<p class='vspace'>Though it wouldn't hurt to have quite a few more functions, even something as simple as normalize or unit vector are missing.
</p></div>
<p class='vspace'><a name='comment12' id='comment12'></a>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>16 July 2010, 14:52</span>  </h5>
</div><div class='messageitem' > 
<p>There is a new version that I haven't released yet which has the few missing bits plus quaternion support. It will be released as soon as I get around to write the documentation.
</p></div>
<p class='vspace'><a name='comment13' id='comment13'></a>
</p><div class='messagehead' > 
<h5>OvermindDL1  &#8212;  <span style='font-size:83%'>16 August 2010, 17:19</span>  </h5>
</div><div class='messageitem' > 
<p>Actually the:
</p><pre>  op[X,Y,-Z]
</pre><p>syntax and such would be easy to pull off if you used Boost.Proto instead of rolling your own, plus you could create a host of transforms that could optimize things as well.
</p>
<p class='vspace'>Note: Yes, something like op[-Y,X*2,log(Z)] is completely possible using Boost.Proto. 
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>emil</dc:contributor>
<dc:date>2010-08-17T05:19:18Z</dc:date>
<pubDate>Tue, 17 Aug 2010 05:19:18 GMT</pubDate>
</item>
<item>
<title>ReCode / It's Not Just About Memory!</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.ItsNotJustAboutMemory</link>
<description><![CDATA[<p>I stumbled upon a very <a class='urllink' href='http://dtrace.org/blogs/bmc/2008/11/03/concurrencys-shysters/' rel='nofollow'>interesting post on concurrency</a>. The author criticizes the <a class='urllink' href='http://en.wikipedia.org/wiki/Transactional_memory' rel='nofollow'>transactional memory</a> approach to simplifying multi-threading programming, compared to classical lock-based multi-threading models. Indeed, it is easy for a novice programmer to create a mess when using locks, the typical feared result being a system that exhibits random, very difficult to diagnose crashes and deadlocks.
</p>
<p class='vspace'>What caught my eye is the focus of the author on a single reason why the TM model is flawed: complex software systems <strong>do not merely operate on memory</strong>.
</p>
<p class='vspace'>This strikes me as important because I've observed a similar issue with garbage collection systems and languages. Like multi-threading programming, dealing with memory is difficult and it is easy for novice programmers to end up with a unmanageable mess of dangling pointers and memory leaks. And the solution? Simply don't worry about memory -- use as much as you need whenever you need it, the garbage collector will do its magic and it'll just work.
</p>
<p class='vspace'>The problem with garbage collection systems is exactly the same: complex software systems <strong>do not merely operate on memory</strong>. Indeed, garbage collectors themselves do <strong>not</strong> manage memory. They manage objects lifetimes.
</p>
<p class='vspace'>In the twisted mind of a Java developer we shouldn't worry about <em>when</em> the object will be collected: it can lurk around until an opportune moment arrives when the system has nothing better to do, and then it'll take care of it; and because the only type of resource most objects acquire is memory, most of the time we don't care when the object will be collected.
</p>
<p class='vspace'>Yet, in the real world we must deal with all kinds of resources: files, sockets, locks, etc. We can't be sloppy with their acquisition and release. Their lifetime is just as hard to manage as memory and it is just as easy for a novice programmer to create a mess.
</p>
<p class='vspace'>Solve that problem with <code class='escaped'>finally</code> and you're back to square one.
</p><div class='messagehead' > 
<h5>Steve  &#8212;  <span style='font-size:83%'>01 August 2010, 08:05</span>  </h5>
</div><div class='messageitem' > 
<p>I recently attended a code review of a rather convoluted C++ scheme (with timers, threads, and smart pointers) for acquiring and releasing a resource in a timely manner.  I saw that everything of interest happened between a pair of curly braces and suggested wrapping the code (in this case a bunch of ODBC) with a simple stack-based object (RAII).  I was taken aback by their response that this was 'Java-based thinking' and that I was trying to 'do garbage collection in C++', revealing no understanding of the difference between eventual vs. deterministic freeing of resources.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>03 August 2010, 16:17</span>  </h5>
</div><div class='messageitem' > 
<p>Right, this is typical for programmers who learned C or C++ after they've had some experience writing Java-style code. Even the ones who do understand the semantics of local objects in C++ don't use them because they feel alien to them.
</p>
<p class='vspace'>And so they use smart pointers to solve an artificial problem they created themselves. :)
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>OvermindDL1  &#8212;  <span style='font-size:83%'>16 August 2010, 16:31</span>  </h5>
</div><div class='messageitem' > 
<p>I would prefer to handle memory like I handle files and sockets and such in C++ as I would handle them in Erlang.  Erlang has no memory sharing between threads, that vastly simplifies things and allows Erlang to perform a variety of optimizations.  You should take a look at that and write a blog about your thoughts (not the language of Erlang, prolog-like, but the style it operates in, pure Actor-oriented).
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>OvermindDL1</dc:contributor>
<dc:date>2010-08-16T23:31:52Z</dc:date>
<pubDate>Mon, 16 Aug 2010 23:31:52 GMT</pubDate>
</item>
<item>
<title>ReCode / Assertions</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.Assertions</link>
<description><![CDATA[<p>What happens when assertions fail? It depends.
</p>
<p class='vspace'>Java throws an exception.
</p>
<p class='vspace'>C# and Visual Basic display a dialog box by default but provide an interface for extending and customizing program behavior when assertions fail.
</p>
<p class='vspace'>In C and C++, the default behavior for assertion violations is to abort the program immediately. Not surprisingly, many C and C++ programmers create their own assertion-checking macros that are, well, "smarter".
</p>
<p class='vspace'>What is the <em>correct</em> behavior then?
</p>
<p class='vspace'>Some library developers would answer that there is no single correct behavior, and they'll point out at all the different behaviors people find useful. Why make a difficult design decision when you can shift that responsibility to the user?
</p>
<p class='vspace'>What this train of thought is missing is that when an assertion fires, we can no longer reason about the state of the program; there is no way to know what will happen if the program is not terminated.
</p>
<p class='vspace'>"But it might be important to minimize the damage! In some instances, ignoring a particular assertion might be better than terminating the program!"
</p>
<p class='vspace'>Certainly, except that when something the programmer considered impossible has just happened, all bets are off. Aborting seems drastic? Indeed it is, but any other alternative would be like making a zombie run a marathon.
</p>
<p class='vspace'>Besides, it isn't always realistic to assume that the user knows the correct answer. What would you reply to
</p>
<p class='vspace'  style='text-align: center;'><strong>Assertion failure: "Your nuclear reactor has overheated. Abort, Debug, Ignore?"</strong>
</p>
<p class='vspace'>:)
</p><div class='messagehead' > 
<h5>Gregory  &#8212;  <span style='font-size:83%'>29 March 2010, 05:03</span>  </h5>
</div><div class='messageitem' > 
<p>My assertions are "smarter" because I want them to be logged in a sqlite db.
</p>
<p class='vspace'>Then, for instance with MSVC++ assert doesn't even break where the assertion failed, but in a sub-function.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>sorrynoname  &#8212;  <span style='font-size:83%'>25 July 2010, 00:25</span>  </h5>
</div><div class='messageitem' > 
<p>Where is the definition of your assert macro then?
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>26 July 2010, 18:00</span>  </h5>
</div><div class='messageitem' > 
<p>In &lt;assert.h&gt;? :)
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>Emil</dc:contributor>
<dc:date>2010-07-27T01:00:31Z</dc:date>
<pubDate>Tue, 27 Jul 2010 01:00:31 GMT</pubDate>
</item>
<item>
<title>ReCode / MSDN Search</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.MSDNSearch</link>
<description><![CDATA[<p>One problem I've always had with MSDN is that whenever I search for something, it finds stuff that I really don't care about. Typically, I'd look for a C function from the Windows API or DirectX, and it'd find me some Visual Basic function which resembles the name of the C function.
</p>
<p class='vspace'>I thought, OK, maybe few people use C any more. So I'd just shrug and try another search.
</p>
<p class='vspace'>However, it seems that the problem is deeper than that. Just now I looked up D3D11_PRIMITIVE_TOPOLOGY on MSDN and I got the following result:
</p>
<div class='vspace'></div><ul><li>D3D10_PRIMITIVE_TOPOLOGY Enumeration (Windows)
</li><li>SimpleBezier11 Sample
</li><li>effect files tesselation features - XNA Community Forums
</li><li>The Lepidoptera Project
</li><li>...
</li></ul><p class='vspace'>Obviously this search query is very specific. There is no way D3D10_PRIMITIVE_TOPOLOGY to be a better match than the D3D11_PRIMITIVE_TOPOLOGY page that I <em>know</em> exists on MSDN (click <a class='urllink' href='http://social.msdn.microsoft.com/Search/en-US?query=D3D11_PRIMITIVE_TOPOLOGY' rel='nofollow'>here</a> to see current MSDN search results for this query.)
</p>
<p class='vspace'>Is it that there is something wrong with my search? Google doesn't think so: with the same query, the D3D11_PRIMITIVE_TOPOLOGY page is at the top of its <a class='urllink' href='http://www.google.com/#hl=en&amp;q=D3D11_PRIMITIVE_TOPOLOGY' rel='nofollow'>search results</a>.
</p>
<p class='vspace'>Is this a problem with MSDN itself? Maybe it's using old search technology that Microsoft hasn't been able to update yet? Surely, Bing can do better? Not really, here is what Bing found:
</p>
<div class='vspace'></div><ul><li>D3D10_PRIMITIVE_TOPOLOGY Enumeration (Windows)
</li><li>SimpleBezier11 Sample
</li><li>Anteru’s blog » directx
</li><li>Anteru’s blog » 2010 » March » 01
</li><li>...
</li></ul><p class='vspace'>(Click <a class='urllink' href='http://www.bing.com/search?q=D3D11_PRIMITIVE_TOPOLOGY' rel='nofollow'>here</a> for current Bing search results for this query.)
</p>
<p class='vspace'>So there you have it: Google seems better at searching the MSDN pages than Microsoft. :)
</p><div class='messagehead' > 
<h5>Arseny Kapoulkine  &#8212;  <span style='font-size:83%'>05 June 2010, 01:11</span>  </h5>
</div><div class='messageitem' > 
<p>I always do a google search "msdn topic/function/whatever" when I need something from MSDN :)
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Reg  &#8212;  <span style='font-size:83%'>06 June 2010, 11:16</span>  </h5>
</div><div class='messageitem' > 
<p>I always have MSDN Library installed offline on my PC. I just can't imagine launching web browser and doing full-text search (like the one described here) to find the documentation of a function, class or enum from C++ STL, WinAPI, .NET or DirectX. Especially as offline MSDN has something much more useful than full-text search that the online version lacks - the "Index"!
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>Reg</dc:contributor>
<dc:date>2010-06-06T18:16:23Z</dc:date>
<pubDate>Sun, 06 Jun 2010 18:16:23 GMT</pubDate>
</item>
<item>
<title>ReCode / My String Class Can Kick Your String Class' Ass</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.MyStringClassCanKickYourStringClassAss</link>
<description><![CDATA[<p class='vspace'>The first programming language I learned was Applesoft Basic for Apple ][. It had 3 types of variables:
</p>
<div class='vspace'></div><ul><li>floats, which was the default,
</li><li>integers, designated by a % after their name, and
</li><li>strings, designated by a $ after their name.
</li></ul><p class='vspace'>So if you wanted to use a string, you just type:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;">A$ = <span class="st0">&#34;Hello World!&#34;</span></div></div>
</div>
<p class='vspace'>Most programming languages today have similar level of built-in string type support but not C and C++ where string support is provided through the standard library.
</p>
<p class='vspace'>However, the result is essentially the same:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;">std::<span class="me2">string</span> a = <span class="st0">&#34;Hello World!&#34;</span>;</div></div>
</div>
<p class='vspace'>The problem is that string support is indeed so basic and so trivial that pretty much any decent C++ programmer can implement a string class of their own.
</p>
<p class='vspace'>And so they do.
</p>
<p class='vspace'>And then, some poor soul decides to learn C++ after having been programming in other languages.
</p>
<p class='vspace'>And they can't believe that someone -- an otherwise rational person most programmers are -- could seriously think that writing yet another string class is a good idea.
</p><div class='messagehead' > 
<h5>Jamie Fenton  &#8212;  <span style='font-size:83%'>10 May 2009, 17:19</span>  </h5>
</div><div class='messageitem' > 
<p>The problem is that much worse when the C or C++ program interacts with a mature operating system. Windows has a lengthy list of string types including single and double byte zero terminated strings, BSTRs with a length in front,  Registry values (which can have a size kept seperate by ths OS memory manager, multiple encoding representations ascii, DOS, ANSI, UTF-8, Unicode,..., and life-cycle management policys from GC, reference counting, user-managed, to eternal binding. These are all used by half a dozen application frameworks like MFC, ATL, WTL, .NET, Visual Basic &amp; Automation, COM, &amp; more. I have seen articles that try to explain all this with respect to the String data type, and they make the Standard Model of Particle Physics look simple.
</p>
<p class='vspace'>Some programmers look at this mess and think "The only way I can understand what I am doing is to 'roll my own' string class" - which fails due to Fenton's law: "adding additional features to something that is already complicated can't make it simplier".
</p>
<p class='vspace'>I eventually found a C++ string package, implemented as a huge set of templates, that covers most of this, and has a user community behind it, called StdString.
</p>
<p class='vspace'><a class='urllink' href='http://www.codeproject.com/KB/string/stdstring.aspx' rel='nofollow'>http://www.codeproject.com/KB/string/stdstring.aspx</a>
</p>
<p class='vspace'>It isn't perfect, but it has let me avoid creating too many more String classes.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>11 May 2009, 22:30</span>  </h5>
</div><div class='messageitem' > 
<p>I guess my question was, what's wrong with std::string? Oh well. :)
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Steve  &#8212;  <span style='font-size:83%'>16 September 2009, 13:37</span>  </h5>
</div><div class='messageitem' > 
<p>We make heavy use of CStdStringW as well, mainly for interoperability with CString.
</p>
<p class='vspace'>Not sure if it was clear from the above, but CStdString is a subclass of std::string, so in a sense the answer to your question is "nothing".
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Gregory  &#8212;  <span style='font-size:83%'>24 February 2010, 02:34</span>  </h5>
</div><div class='messageitem' > 
<p>We're in 2010, what's with std::string not coping with UTF-8...
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>TeMPOraL  &#8212;  <span style='font-size:83%'>16 May 2010, 15:36</span>  </h5>
</div><div class='messageitem' > 
<p>Use std::wstring :)
<a class='urllink' href='http://stackoverflow.com/questions/402283/stdwstring-vs-stdstring' rel='nofollow'>http://stackoverflow.com/questions/402283/stdwstring-vs-stdstring</a>
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>22 May 2010, 22:00</span>  </h5>
</div><div class='messageitem' > 
<p>Gregory, UTF-8 is outside of the scope of std::string due to complexity requirements for various string algorithms.
</p>
<p class='vspace'>TeMPOraL, std::wstring should have been fine for unicode strings except that this isn't portable because on Windows wchar_t is 16 bit and in general strings use UTF-16 encoding on Windows.
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>Emil</dc:contributor>
<dc:date>2010-05-23T05:00:49Z</dc:date>
<pubDate>Sun, 23 May 2010 05:00:49 GMT</pubDate>
</item>
<item>
<title>ReCode / OpenGL Madness</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.OpenGLMadness</link>
<description><![CDATA[<p class='vspace'>Through my career I've been working with proprietary graphics APIs, but Apple platforms use OpenGL and since it is also available on Windows and on other OSes, it seems the right choice for graphics today.
</p>
<p class='vspace'>It also feels good to support an open standard like that. I love open standards. For example I find it ridiculous that Windows is not POSIX; even though it does serve their corporate interests, Apple demonstrated that the same corporate interests can be served with a POSIX-compliant OS.
</p>
<p class='vspace'>And then OpenGL showed its ugly head.
</p>
<div class='vspace'></div><h2>Extensions</h2>
<p>How has OpenGL survived for so many years? By extensions.
</p>
<p class='vspace'>Every time a graphics chip manufacturer implements a cool new feature in hardware, OpenGL allows them to provide whatever functions they feel like in the form of extensions. For example, if ATI needed to implement a cool new function, they simply document it and compile it in the video driver shared libraries/DLLs, using a function name like glCoolNewFeatureATI.
</p>
<p class='vspace'>If two or more graphics chip manufacturers agree on the same API for a cool new feature, then the OpenGL stardard allows them to use the suffix EXT: glCoolNewFeatureEXT. That way, programs can look for and link to that function without having to know what graphics hardware they're running on.
</p>
<p class='vspace'>Then there is the OpenGL Architecture Review Board which looks at various extensions that appear and if they like an EXT function, it gets the ARB blessing and vendors are allowed to use the suffix ARB with the name, so we get glCoolNewFeatureARB.
</p>
<p class='vspace'>Finally, most (all?) ARB functions are made part of the next revision of the OpenGL standard, at which point all suffixes are dropped and we're left with glCoolNewFeature added to the rest of the OpenGL functions.
</p>
<p class='vspace'>That's how committees work: because nobody is in charge, nobody can take the responsibility for making the correct architectural decision. Because nobody can take that responsibility, it is necessary to have a cumbersome system of checks and balances to minimize the possibility of breaking the standard.
</p>
<p class='vspace'>But the result is a mess anyway. Users end up with potentially 4 different versions for many OpenGL functions. Which one should they call?
</p>
<p class='vspace'>You might think that a reasonable approach is to look up the suffix-less version first, if that fails look up the ARB version, and if that fails look up the EXT version, and finally if that also fails just announce to the user that their OpenGL driver sucks and exit. But that's easier said than done.
</p>
<p class='vspace'>For starters, some of these functions take enums as parameters. The names of the enums also feature the corresponding suffix. For example, a possible value for the first parameter of glBindBufferARB is GL_ARRAY_BUFFER_ARB, but if you're calling glBindBufferEXT you should be using GL_ARRAY_BUFFER_EXT instead, and if you're calling simply glBindBuffer then you'd use GL_ARRAY_BUFFER.
</p>
<p class='vspace'>This arrangement makes it difficult for the graphics programmer to abstract the logic for selecting the correct function: it isn't sufficient to simply store a function pointer or something similar. The only viable solution is to create a wrapper layer over OpenGL with its own naming conventions and its own enums, which then get translated depending on which of the 3 or 4 variants of the function should be called on the target platform.
</p>
<p class='vspace'>Or, you could just pick one variant and use only that or fail if it isn't available. For example, I thought that it is reasonable for my program to require vertex buffer support in the OpenGL driver, so I started writing code which simply calls glBindBuffer without any suffixes. And it turned out that on my system glBindBuffer is available but is broken. glBindBufferARB is also available, and it works fine.
</p>
<p class='vspace'>So, I feel stuck. So much so that blogging about the problem seemed more appealing than trying to figure out what's the correct solution. :)
</p><div class='messagehead' > 
<h5>rotoglup  &#8212;  <span style='font-size:83%'>21 May 2010, 10:12</span>  </h5>
</div><div class='messageitem' > 
<p>Your 'glBindBuffer' requirement is more than reasonable ! It is a core feature since GL 1.5...
</p>
<p class='vspace'>Ok, the extension jungle is a pain. But bad GL implementations remain the main problem.
</p>
<p class='vspace'>I don't have experience on Mac + GL though... On Windows, OpenGL + Intel graphics is a "no go" zone for us.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>21 May 2010, 14:17</span>  </h5>
</div><div class='messageitem' > 
<p>Reasonable it is. It also crashes on my laptop -- which admittedly is old and has crappy Intel graphics -- yet glBindBufferARB works fine.
</p>
<p class='vspace'>Assuming that it's more important for my program to work than to be reasonable, I still don't know what's the best way to deal with this issue.
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>Emil</dc:contributor>
<dc:date>2010-05-21T21:17:28Z</dc:date>
<pubDate>Fri, 21 May 2010 21:17:28 GMT</pubDate>
</item>
<item>
<title>ReCode / CALL-151</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.CALL-151</link>
<description><![CDATA[<p>It is only natural for people to think with nostalgia about the good 'ol days: "oh man, back in the day things were so simple!" Objectively speaking, obviously, the "good 'ol days" sucked: that simplicity is only skin deep.
</p>
<p class='vspace'>Yes, a telephone was much easier to use in the 70s, but then again it didn't do <em>that</em> much, did it? Could you imagine having to memorize the phone numbers of everyone you need to text or call? Right, that would have sucked. How about having to fast forward in order to get to the next song on a compact cassette? Ouch.
</p>
<p class='vspace'>Yet, some things from the past are worth admiring. To me, a steam engine is prettier than the most advanced internal combustion engines of today. Not really practical, yet pretty.
</p>
<p class='vspace'>But I'm a programmer, and a lot of the old stuff I admire is software. And while I have (if only once) programmed a computer with punchcards, those were already antiques when I was in school; the computer I grew up programming was the Apple ][.
</p>
<div class='vspace'></div><div class='indent'><em>Actually, not quite: I grew up in Bulgaria which was a member of <a class='urllink' href='http://en.wikipedia.org/wiki/COMECON' rel='nofollow'>COMECON</a>, an economic organization of the communist states. According to Wikipedia, at its peak Bulgaria produced 40% of the computers in COMECON. Some of what we did was what China does today: copying and mass-producing western technology. So I grew up programming the Pravetz-82, which was an Apple ][ clone built with Bulgarian-made clone of the <a class='urllink' href='http://en.wikipedia.org/wiki/MOS_Technology_6502' rel='nofollow'>6502</a> CPU. :)</em>
</div><p class='vspace'>So here we go: at the risk of wasting my time raving about something most programmers today can't appreciate, I'll try to explain but just a tiny bit of Wozniak's brilliancy:
</p>
<div class='vspace'></div><h2>The Apple ][ floppy disk controller bootstrapping routine</h2>
<p>Some background: on physical magnetic media, you can't "just record data". Instead, 1 is encoded as a magnetic polarity transition (also known as flux reversal), and 0 is encoded as, well, lack of transition. Basically, for each bit there is a limited time window in which flux reversal is expected to occur. If it occurs, the bit is 1, otherwise it's 0. The problem is that the time window is so small that there is a limit on the accuracy with which the data window length can be measured. So, while detecting a series of ones is not a problem, it's difficult to know how many zeroes are encoded in a measured period of lack of flux reversal.
</p>
<p class='vspace'>Wozniak's original hardware design imposed two constraints on the raw data:
</p>
<div class='vspace'></div><ul><li>between any two 1-bits, there can be no more than one 0-bit, and
</li><li>each 8-bit byte must start with a 1-bit
</li></ul><p class='vspace'>However, using simple FM encoding "wastes" 4 bits per raw byte, which would allow only 10 256-byte sectors per track to be recorded. Instead, he devised a more complex encoding scheme that was based on the fact that there are 34 8-bit numbers which have the top bit set and no two 0-bits in a row. That way, 13 sectors per track could be recorded. Later on the hardware design of the floppy disk controller was modified to allow no more than one occurrence of two consecutive 0-bits in a byte, which lead to different encoding called 6&2; it allowed 16 sectors per track.
</p>
<p class='vspace'>A conventional floppy disk controller design would put all this bit twiddling in silicone which just DMAs the decoded bytes in memory -- but it would cost more in hardware. So what is a Wozniak to do? Obviously: screw hardware, do everything in software.
</p>
<p class='vspace'>That would have been impressive enough on a 6502 CPU, but even more impressive is what is required of the bootstrapping routine. It must:
</p>
<div class='vspace'></div><ul><li>generate the 6&amp;2 decoding table in RAM
</li><li>seek the disk drive to track zero
</li><li>look for the beginning marker of sector zero and read the raw data in RAM
</li><li>decode the data and jump into it
</li></ul><p class='vspace'>...all in no more than 256 bytes of code, which is the addressing limit for the ROM on Apple ][ expansion cards.
</p>
<p class='vspace'>By the way, there are no timers on the Apple ][, all timing is based on the CPU clock, e.g. you'd know that an INX instruction takes 2 microseconds to execute. The 6502 has three 8-bit registers: an accumulator A which can add, subtract, shift, rotate, and, or, xor, etc., and two 8-bit index registers X and Y which can do none of those operations. :)
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="br0">&#93;</span>CALL<span class="nu0">-151</span><br />
*C600L<br />
<br />
C600-&#160; &#160;A2 <span class="nu0">20</span>&#160; &#160; &#160; &#160;LDX&#160; &#160;<span class="co2">#$20</span><br />
C602-&#160; &#160;A0 <span class="nu0">00</span>&#160; &#160; &#160; &#160;LDY&#160; &#160;<span class="co2">#$00</span><br />
C604-&#160; &#160;A2 <span class="nu0">03</span>&#160; &#160; &#160; &#160;LDX&#160; &#160;<span class="co2">#$03</span><br />
C606-&#160; &#160;<span class="nu0">86</span> 3C&#160; &#160; &#160; &#160;STX&#160; &#160;$3C<br />
C608-&#160; &#160;8A&#160; &#160; &#160; &#160; &#160; TXA<br />
C609-&#160; &#160;0A&#160; &#160; &#160; &#160; &#160; ASL<br />
C60A-&#160; &#160;<span class="nu0">24</span> 3C&#160; &#160; &#160; &#160;BIT&#160; &#160;$3C<br />
C60C-&#160; &#160;F0 <span class="nu0">10</span>&#160; &#160; &#160; &#160;BEQ&#160; &#160;$C61E<br />
C60E-&#160; &#160;<span class="nu0">05</span> 3C&#160; &#160; &#160; &#160;ORA&#160; &#160;$3C<br />
C610-&#160; &#160;<span class="nu0">49</span> FF&#160; &#160; &#160; &#160;EOR&#160; &#160;<span class="co2">#$FF</span><br />
C612-&#160; &#160;<span class="nu0">29</span> 7E&#160; &#160; &#160; &#160;AND&#160; &#160;<span class="co2">#$7E</span><br />
C614-&#160; &#160;B0 <span class="nu0">08</span>&#160; &#160; &#160; &#160;BCS&#160; &#160;$C61E<br />
C616-&#160; &#160;4A&#160; &#160; &#160; &#160; &#160; LSR<br />
C617-&#160; &#160;D0 FB&#160; &#160; &#160; &#160;BNE&#160; &#160;$C614<br />
C619-&#160; &#160;<span class="nu0">98</span>&#160; &#160; &#160; &#160; &#160; TYA<br />
C61A-&#160; &#160;9D <span class="nu0">56</span> <span class="nu0">03</span>&#160; &#160; STA&#160; &#160;$<span class="nu0">0356</span>,X<br />
C61D-&#160; &#160;C8&#160; &#160; &#160; &#160; &#160; INY<br />
C61E-&#160; &#160;E8&#160; &#160; &#160; &#160; &#160; INX<br />
C61F-&#160; &#160;<span class="nu0">10</span> E5&#160; &#160; &#160; &#160;BPL&#160; &#160;$C606<br />
C621-&#160; &#160;<span class="nu0">20</span> <span class="nu0">58</span> FF&#160; &#160; JSR&#160; &#160;$FF58<br />
C624-&#160; &#160;BA&#160; &#160; &#160; &#160; &#160; TSX<br />
C625-&#160; &#160;BD <span class="nu0">00</span> <span class="nu0">01</span>&#160; &#160; LDA&#160; &#160;$<span class="nu0">0100</span>,X<br />
C628-&#160; &#160;0A&#160; &#160; &#160; &#160; &#160; ASL<br />
C629-&#160; &#160;0A&#160; &#160; &#160; &#160; &#160; ASL<br />
C62A-&#160; &#160;0A&#160; &#160; &#160; &#160; &#160; ASL<br />
C62B-&#160; &#160;0A&#160; &#160; &#160; &#160; &#160; ASL<br />
C62C-&#160; &#160;<span class="nu0">85</span> 2B&#160; &#160; &#160; &#160;STA&#160; &#160;$2B<br />
C62E-&#160; &#160;AA&#160; &#160; &#160; &#160; &#160; TAX<br />
C62F-&#160; &#160;BD 8E C0&#160; &#160; LDA&#160; &#160;$C08E,X<br />
C632-&#160; &#160;BD 8C C0&#160; &#160; LDA&#160; &#160;$C08C,X<br />
C635-&#160; &#160;BD 8A C0&#160; &#160; LDA&#160; &#160;$C08A,X<br />
C638-&#160; &#160;BD <span class="nu0">89</span> C0&#160; &#160; LDA&#160; &#160;$C089,X<br />
C63B-&#160; &#160;A0 <span class="nu0">50</span>&#160; &#160; &#160; &#160;LDY&#160; &#160;<span class="co2">#$50</span><br />
C63D-&#160; &#160;BD <span class="nu0">80</span> C0&#160; &#160; LDA&#160; &#160;$C080,X<br />
C640-&#160; &#160;<span class="nu0">98</span>&#160; &#160; &#160; &#160; &#160; TYA<br />
C641-&#160; &#160;<span class="nu0">29</span> <span class="nu0">03</span>&#160; &#160; &#160; &#160;AND&#160; &#160;<span class="co2">#$03</span><br />
C643-&#160; &#160;0A&#160; &#160; &#160; &#160; &#160; ASL<br />
C644-&#160; &#160;<span class="nu0">05</span> 2B&#160; &#160; &#160; &#160;ORA&#160; &#160;$2B<br />
C646-&#160; &#160;AA&#160; &#160; &#160; &#160; &#160; TAX<br />
C647-&#160; &#160;BD <span class="nu0">81</span> C0&#160; &#160; LDA&#160; &#160;$C081,X<br />
C64A-&#160; &#160;A9 <span class="nu0">56</span>&#160; &#160; &#160; &#160;LDA&#160; &#160;<span class="co2">#$56</span><br />
C64C-&#160; &#160;<span class="nu0">20</span> A8 FC&#160; &#160; JSR&#160; &#160;$FCA8<br />
C64F-&#160; &#160;<span class="nu0">88</span>&#160; &#160; &#160; &#160; &#160; DEY<br />
C650-&#160; &#160;<span class="nu0">10</span> EB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C63D<br />
C652-&#160; &#160;<span class="nu0">85</span> <span class="nu0">26</span>&#160; &#160; &#160; &#160;STA&#160; &#160;$<span class="nu0">26</span><br />
C654-&#160; &#160;<span class="nu0">85</span> 3D&#160; &#160; &#160; &#160;STA&#160; &#160;$3D<br />
C656-&#160; &#160;<span class="nu0">85</span> <span class="nu0">41</span>&#160; &#160; &#160; &#160;STA&#160; &#160;$<span class="nu0">41</span><br />
C658-&#160; &#160;A9 <span class="nu0">08</span>&#160; &#160; &#160; &#160;LDA&#160; &#160;<span class="co2">#$08</span><br />
C65A-&#160; &#160;<span class="nu0">85</span> <span class="nu0">27</span>&#160; &#160; &#160; &#160;STA&#160; &#160;$<span class="nu0">27</span><br />
C65C-&#160; &#160;<span class="nu0">18</span>&#160; &#160; &#160; &#160; &#160; CLC<br />
C65D-&#160; &#160;<span class="nu0">08</span>&#160; &#160; &#160; &#160; &#160; PHP<br />
C65E-&#160; &#160;BD 8C C0&#160; &#160; LDA&#160; &#160;$C08C,X<br />
C661-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C65E<br />
C663-&#160; &#160;<span class="nu0">49</span> D5&#160; &#160; &#160; &#160;EOR&#160; &#160;<span class="co2">#$D5</span><br />
C665-&#160; &#160;D0 F7&#160; &#160; &#160; &#160;BNE&#160; &#160;$C65E<br />
C667-&#160; &#160;BD 8C C0&#160; &#160; LDA&#160; &#160;$C08C,X<br />
C66A-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C667<br />
C66C-&#160; &#160;C9 AA&#160; &#160; &#160; &#160;CMP&#160; &#160;<span class="co2">#$AA</span><br />
C66E-&#160; &#160;D0 F3&#160; &#160; &#160; &#160;BNE&#160; &#160;$C663<br />
C670-&#160; &#160;EA&#160; &#160; &#160; &#160; &#160; NOP<br />
C671-&#160; &#160;BD 8C C0&#160; &#160; LDA&#160; &#160;$C08C,X<br />
C674-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C671<br />
C676-&#160; &#160;C9 <span class="nu0">96</span>&#160; &#160; &#160; &#160;CMP&#160; &#160;<span class="co2">#$96</span><br />
C678-&#160; &#160;F0 <span class="nu0">09</span>&#160; &#160; &#160; &#160;BEQ&#160; &#160;$C683<br />
C67A-&#160; &#160;<span class="nu0">28</span>&#160; &#160; &#160; &#160; &#160; PLP<br />
C67B-&#160; &#160;<span class="nu0">90</span> DF&#160; &#160; &#160; &#160;BCC&#160; &#160;$C65C<br />
C67D-&#160; &#160;<span class="nu0">49</span> AD&#160; &#160; &#160; &#160;EOR&#160; &#160;<span class="co2">#$AD</span><br />
C67F-&#160; &#160;F0 <span class="nu0">25</span>&#160; &#160; &#160; &#160;BEQ&#160; &#160;$C6A6<br />
C681-&#160; &#160;D0 D9&#160; &#160; &#160; &#160;BNE&#160; &#160;$C65C<br />
C683-&#160; &#160;A0 <span class="nu0">03</span>&#160; &#160; &#160; &#160;LDY&#160; &#160;<span class="co2">#$03</span><br />
C685-&#160; &#160;<span class="nu0">85</span> <span class="nu0">40</span>&#160; &#160; &#160; &#160;STA&#160; &#160;$<span class="nu0">40</span><br />
C687-&#160; &#160;BD 8C C0&#160; &#160; LDA&#160; &#160;$C08C,X<br />
C68A-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C687<br />
C68C-&#160; &#160;2A&#160; &#160; &#160; &#160; &#160; ROL<br />
C68D-&#160; &#160;<span class="nu0">85</span> 3C&#160; &#160; &#160; &#160;STA&#160; &#160;$3C<br />
C68F-&#160; &#160;BD 8C C0&#160; &#160; LDA&#160; &#160;$C08C,X<br />
C692-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C68F<br />
C694-&#160; &#160;<span class="nu0">25</span> 3C&#160; &#160; &#160; &#160;AND&#160; &#160;$3C<br />
C696-&#160; &#160;<span class="nu0">88</span>&#160; &#160; &#160; &#160; &#160; DEY<br />
C697-&#160; &#160;D0 EC&#160; &#160; &#160; &#160;BNE&#160; &#160;$C685<br />
C699-&#160; &#160;<span class="nu0">28</span>&#160; &#160; &#160; &#160; &#160; PLP<br />
C69A-&#160; &#160;C5 3D&#160; &#160; &#160; &#160;CMP&#160; &#160;$3D<br />
C69C-&#160; &#160;D0 BE&#160; &#160; &#160; &#160;BNE&#160; &#160;$C65C<br />
C69E-&#160; &#160;A5 <span class="nu0">40</span>&#160; &#160; &#160; &#160;LDA&#160; &#160;$<span class="nu0">40</span><br />
C6A0-&#160; &#160;C5 <span class="nu0">41</span>&#160; &#160; &#160; &#160;CMP&#160; &#160;$<span class="nu0">41</span><br />
C6A2-&#160; &#160;D0 B8&#160; &#160; &#160; &#160;BNE&#160; &#160;$C65C<br />
C6A4-&#160; &#160;B0 B7&#160; &#160; &#160; &#160;BCS&#160; &#160;$C65D<br />
C6A6-&#160; &#160;A0 <span class="nu0">56</span>&#160; &#160; &#160; &#160;LDY&#160; &#160;<span class="co2">#$56</span><br />
C6A8-&#160; &#160;<span class="nu0">84</span> 3C&#160; &#160; &#160; &#160;STY&#160; &#160;$3C<br />
C6AA-&#160; &#160;BC 8C C0&#160; &#160; LDY&#160; &#160;$C08C,X<br />
C6AD-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C6AA<br />
C6AF-&#160; &#160;<span class="nu0">59</span> D6 <span class="nu0">02</span>&#160; &#160; EOR&#160; &#160;$02D6,Y<br />
C6B2-&#160; &#160;A4 3C&#160; &#160; &#160; &#160;LDY&#160; &#160;$3C<br />
C6B4-&#160; &#160;<span class="nu0">88</span>&#160; &#160; &#160; &#160; &#160; DEY<br />
C6B5-&#160; &#160;<span class="nu0">99</span> <span class="nu0">00</span> <span class="nu0">03</span>&#160; &#160; STA&#160; &#160;$<span class="nu0">0300</span>,Y<br />
C6B8-&#160; &#160;D0 EE&#160; &#160; &#160; &#160;BNE&#160; &#160;$C6A8<br />
C6BA-&#160; &#160;<span class="nu0">84</span> 3C&#160; &#160; &#160; &#160;STY&#160; &#160;$3C<br />
C6BC-&#160; &#160;BC 8C C0&#160; &#160; LDY&#160; &#160;$C08C,X<br />
C6BF-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C6BC<br />
C6C1-&#160; &#160;<span class="nu0">59</span> D6 <span class="nu0">02</span>&#160; &#160; EOR&#160; &#160;$02D6,Y<br />
C6C4-&#160; &#160;A4 3C&#160; &#160; &#160; &#160;LDY&#160; &#160;$3C<br />
C6C6-&#160; &#160;<span class="nu0">91</span> <span class="nu0">26</span>&#160; &#160; &#160; &#160;STA&#160; &#160;<span class="br0">&#40;</span>$<span class="nu0">26</span><span class="br0">&#41;</span>,Y<br />
C6C8-&#160; &#160;C8&#160; &#160; &#160; &#160; &#160; INY<br />
C6C9-&#160; &#160;D0 EF&#160; &#160; &#160; &#160;BNE&#160; &#160;$C6BA<br />
C6CB-&#160; &#160;BC 8C C0&#160; &#160; LDY&#160; &#160;$C08C,X<br />
C6CE-&#160; &#160;<span class="nu0">10</span> FB&#160; &#160; &#160; &#160;BPL&#160; &#160;$C6CB<br />
C6D0-&#160; &#160;<span class="nu0">59</span> D6 <span class="nu0">02</span>&#160; &#160; EOR&#160; &#160;$02D6,Y<br />
C6D3-&#160; &#160;D0 <span class="nu0">87</span>&#160; &#160; &#160; &#160;BNE&#160; &#160;$C65C<br />
C6D5-&#160; &#160;A0 <span class="nu0">00</span>&#160; &#160; &#160; &#160;LDY&#160; &#160;<span class="co2">#$00</span><br />
C6D7-&#160; &#160;A2 <span class="nu0">56</span>&#160; &#160; &#160; &#160;LDX&#160; &#160;<span class="co2">#$56</span><br />
C6D9-&#160; &#160;CA&#160; &#160; &#160; &#160; &#160; DEX<br />
C6DA-&#160; &#160;<span class="nu0">30</span> FB&#160; &#160; &#160; &#160;BMI&#160; &#160;$C6D7<br />
C6DC-&#160; &#160;B1 <span class="nu0">26</span>&#160; &#160; &#160; &#160;LDA&#160; &#160;<span class="br0">&#40;</span>$<span class="nu0">26</span><span class="br0">&#41;</span>,Y<br />
C6DE-&#160; &#160;5E <span class="nu0">00</span> <span class="nu0">03</span>&#160; &#160; LSR&#160; &#160;$<span class="nu0">0300</span>,X<br />
C6E1-&#160; &#160;2A&#160; &#160; &#160; &#160; &#160; ROL<br />
C6E2-&#160; &#160;5E <span class="nu0">00</span> <span class="nu0">03</span>&#160; &#160; LSR&#160; &#160;$<span class="nu0">0300</span>,X<br />
C6E5-&#160; &#160;2A&#160; &#160; &#160; &#160; &#160; ROL<br />
C6E6-&#160; &#160;<span class="nu0">91</span> <span class="nu0">26</span>&#160; &#160; &#160; &#160;STA&#160; &#160;<span class="br0">&#40;</span>$<span class="nu0">26</span><span class="br0">&#41;</span>,Y<br />
C6E8-&#160; &#160;C8&#160; &#160; &#160; &#160; &#160; INY<br />
C6E9-&#160; &#160;D0 EE&#160; &#160; &#160; &#160;BNE&#160; &#160;$C6D9<br />
C6EB-&#160; &#160;E6 <span class="nu0">27</span>&#160; &#160; &#160; &#160;INC&#160; &#160;$<span class="nu0">27</span><br />
C6ED-&#160; &#160;E6 3D&#160; &#160; &#160; &#160;INC&#160; &#160;$3D<br />
C6EF-&#160; &#160;A5 3D&#160; &#160; &#160; &#160;LDA&#160; &#160;$3D<br />
C6F1-&#160; &#160;CD <span class="nu0">00</span> <span class="nu0">08</span>&#160; &#160; CMP&#160; &#160;$<span class="nu0">0800</span><br />
C6F4-&#160; &#160;A6 2B&#160; &#160; &#160; &#160;LDX&#160; &#160;$2B<br />
C6F6-&#160; &#160;<span class="nu0">90</span> DB&#160; &#160; &#160; &#160;BCC&#160; &#160;$C6D3<br />
C6F8-&#160; &#160;4C <span class="nu0">01</span> <span class="nu0">08</span>&#160; &#160; JMP&#160; &#160;$<span class="nu0">0801</span><br />
C6FB-&#160; &#160;<span class="nu0">00</span>&#160; &#160; &#160; &#160; &#160; BRK<br />
C6FC-&#160; &#160;<span class="nu0">00</span>&#160; &#160; &#160; &#160; &#160; BRK<br />
C6FD-&#160; &#160;<span class="nu0">00</span>&#160; &#160; &#160; &#160; &#160; BRK<br />
C6FE-&#160; &#160;<span class="nu0">00</span>&#160; &#160; &#160; &#160; &#160; BRK<br />
C6FF-&#160; &#160;<span class="nu0">00</span>&#160; &#160; &#160; &#160; &#160; BRK</div></div>
</div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Christer Ericson  &#8212;  <span style='font-size:83%'>15 September 2009, 11:33</span>  </h5>
</div><div class='messageitem' > 
<p>The Apple II disk controller and associated hardware is probably the most impressive design I know.
</p>
<p class='vspace'>You forgot to mention there's a second 256 byte "program" which is the controller state machine for shifting bits in (and out) of the memory mapped register(s) that the boot ROM addresses (the &#36;C08C,X location, etc).
</p>
<p class='vspace'>Changing both programs is what allowed the transition from DOS 3.2 to DOS 3.3 (13 to 16 sectors per track).
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Vladimir Strinski  &#8212;  <span style='font-size:83%'>15 September 2009, 14:40</span>  </h5>
</div><div class='messageitem' > 
<p>To add on top of that - he not only coded everything within the 256 bytes, he had a few bytes at the end to spare!
</p>
<p class='vspace'>AND on top of that - the first two bytes are a reserved value so the BIOS would recognize the controller, so you can't really use them.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>15 September 2009, 15:29</span>  </h5>
</div><div class='messageitem' > 
<p>Here are some pictures I found of Bulgarian-made Apple ][ clones: the first two are of Pravetz-82 from the late 70s/early 80s, the third picture is of Pravetz-8C from the late 80s/early 90s. These Bulgarian computers were compatible with Apple ][, but their evolution didn't entirely follow the evolution of Apple ][, e.g. some models didn't have an exact original.
</p>
<div class='vspace'></div><div><img src='http://www.revergestudios.com/reblog/uploads/ReCode/Pravetz-82_1.jpg' alt='' title='' /></div>
<div class='vspace'></div><div><img src='http://www.revergestudios.com/reblog/uploads/ReCode/Pravetz-82_2.jpg' alt='' title='' /></div>
<div class='vspace'></div><div><img src='http://www.revergestudios.com/reblog/uploads/ReCode/Pravetz-8C.jpg' alt='' title='' /></div>
<div class='vspace'></div></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Brad Snickenberg  &#8212;  <span style='font-size:83%'>19 April 2010, 19:03</span>  </h5>
</div><div class='messageitem' > 
<p>Back in the '70s you didn't memorize everyone's #, you had them written in your address book (an actual book) and if they weren't in there you looked them up in the phone book which had everyone in town's names and phone #'s listed. OMG privacy!
</p>
<p class='vspace'>yo! <a class='urllink' href='http://www.appleIIe.com' rel='nofollow'>http://www.appleIIe.com</a>
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>Brad Snickenberg</dc:contributor>
<dc:date>2010-04-20T02:03:22Z</dc:date>
<pubDate>Tue, 20 Apr 2010 02:03:22 GMT</pubDate>
</item>
<item>
<title>ReCode / shared_ptr</title>
<link>http://www.revergestudios.com/reblog/index.php?n=ReCode.SharedPtr</link>
<description><![CDATA[<p class='vspace'>Several times I've started a blog post on <code>boost::shared_ptr</code> but, somehow, I was never happy with the end result. At some point I was even wondering if it is a good idea at all to blog about <code>shared_ptr</code>: doesn't everyone know what it is and how to use it? But when I visited BoostCon this year, I was surprised that at least some of the attendees didn't really understand <code>shared_ptr</code>; in their mind it was "just a refcounting smart pointer."
</p>
<p class='vspace'>So, what's so special about <code>shared_ptr</code>? Let me begin with parsing the smart pointer concept: what does it mean to be "smart" and what does it mean to be a "pointer"?
</p>
<div class='vspace'></div><h2>1) Smart</h2>
<p>The smartness is the ability of smart pointers to keep track of the lifetime of the objects they refer to.
</p>
<p class='vspace'>There are multiple ways to implement "smartness". Arguably, the most practical implementation is through refcounting, because other (possibly more optimal) solutions typically lead to non-deterministic termination, and that renders RAII useless and forces programmers to write <code>finally</code> clauses like there's no tomorrow. If <code>finally</code> doesn't bug you, you must be a Java programmer and in this case I'm wondering how did you get this far in my post to begin with. :)
</p>
<div class='vspace'></div><h2>2) Pointer</h2>
<p>Pointers are both a primary reason why C and C++ programs are very efficient, and a major obstacle for compiler optimizations. Regardless, they are at the very base of these languages; we C and C++ programmers can't live without pointers.
</p>
<p class='vspace'>What's so cool about pointers? First and foremost, pointers give us universal means to refer to any object whatsoever (I'm specifically ignoring type safety here, because it just makes it less likely that we make a silly mistake by forgetting what's the type of the object we point to.)
</p>
<p class='vspace'>Using plain old C pointers, a factory function can create an object of some type and return a pointer to the user. The user can hold on to the pointer, make (and destroy) any number of copies of the pointer, organize pointers in containers, pass pointers to other functions, etc. For example:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="kw1">struct</span> foo;<br />
foo * foo_create<span class="br0">&#40;</span>....<span class="br0">&#41;</span>;<br />
<span class="kw2">void</span> foo_use<span class="br0">&#40;</span> foo * x <span class="br0">&#41;</span>;<br />
<span class="kw2">void</span> foo_destroy<span class="br0">&#40;</span> foo * x <span class="br0">&#41;</span>;</div></div>
</div>
<p class='vspace'>As this interface example illustrates, copying a pointer doesn't require a complete type. In fact, the type of the pointee is irrelevant as far as copying or destroying the pointer itself is concerned -- even (typeless) void pointers can be freely copied around. The trick is to not destroy (forget about) all pointer copies we've made, or else we get a leak.
</p>
<div class='vspace'></div><h2>Smart, pointer</h2>
<p>C++ allows <code>operator-&gt;</code> and <code>operator*</code> overloads specifically so that (in combination with RAII) programmers can implement smart pointers:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="kw1">template</span> &lt;class T&gt;<br />
<span class="kw1">class</span><br />
refcount_ptr<br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw1">public</span>:<br />
<br />
&#160; &#160; refcount_ptr<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="kw1">explicit</span> refcount_ptr<span class="br0">&#40;</span> T * <span class="br0">&#41;</span>;<br />
&#160; &#160; refcount_ptr<span class="br0">&#40;</span> refcount_ptr <span class="kw2">const</span> &amp; <span class="br0">&#41;</span>;<br />
&#160; &#160; refcount_ptr &amp; <span class="kw1">operator</span>=<span class="br0">&#40;</span> refcount_ptr <span class="kw2">const</span> &amp; <span class="br0">&#41;</span>;<br />
&#160; &#160; ~refcount_ptr<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&#160; &#160; T * operator-&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">const</span>;<br />
&#160; &#160; T &amp; <span class="kw1">operator</span>*<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">const</span>;<br />
<br />
&#160; &#160; <span class="kw1">private</span>:<br />
<br />
&#160; &#160; T * p_;<br />
&#160; &#160; <span class="kw2">int</span> n_;<br />
&#160; &#160; <span class="br0">&#125;</span>;</div></div>
</div>
<p class='vspace'>Let's leave the refcounting mechanics aside for now and focus on the <code>refcount_ptr</code> destructor. Its implementation appears straight-forward:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="kw1">template</span> &lt;class T&gt;<br />
refcount_ptr&lt;T&gt;::~refcount_ptr<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw1">if</span><span class="br0">&#40;</span> !--n_ <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; <span class="kw1">delete</span> p_;<br />
&#160; &#160; <span class="br0">&#125;</span></div></div>
</div>
<p class='vspace'>Right? Wrong! Remember, a (smart) <em>pointer</em> must be copyable (and destroyable) in contexts where the type of the pointee (<code>T</code> in our <code>refcount_ptr</code> example) is incomplete. But in such contexts, <code>~refcount_ptr</code> won't compile: <code>delete</code> requires a complete type!
</p>
<p class='vspace'>Solve that problem, and you'll discover that <code>refcount_ptr</code> can not only work with incomplete types, but it also supports type erasure -- <code>refcount_ptr&lt;void&gt;</code> would work just fine.
</p>
<p class='vspace'>And by that time you'll be well on your way to reinventing <code>shared_ptr</code> -- all the rest of its features follow from it being a <em>pointer</em>, just "smart".
</p>
<div class='vspace'></div><h2>Aliasing support</h2>
<p>Here's something else you could do with plain old C pointers:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="kw1">struct</span><br />
foo<br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw2">int</span> x;<br />
&#160; &#160; <span class="br0">&#125;</span>;<br />
<br />
<span class="kw2">void</span> bar<span class="br0">&#40;</span> <span class="kw2">int</span> * x <span class="br0">&#41;</span>;<br />
<br />
<span class="kw2">void</span><br />
foobar<span class="br0">&#40;</span> foo * a <span class="br0">&#41;</span><br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw2">int</span> * b = &amp;a-&gt;x;<br />
&#160; &#160; bar<span class="br0">&#40;</span> b <span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="br0">&#125;</span></div></div>
</div>
<p class='vspace'>The above code is so basic it's actually easy to fail to appreciate its importance. It demonstrates that we can use the <code>&amp;</code> operator to make a pointer that points to any object whatsoever, including an <code>int</code> that is a member of some other object <code>foo</code>. The <code>x</code> member of <code>a</code> can now be accessed as <code>a-&gt;x</code> or by dereferencing the other pointer, <code>*b</code>.
</p>
<p class='vspace'>Imagine now that <code>foobar</code> takes a <em>smart</em> pointer of type <code>foo</code>. Could it then make a smart pointer of type <code>int</code> from it? Yep, <code>shared_ptr</code> can do that too:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="kw2">void</span> bar<span class="br0">&#40;</span> shared_ptr&lt;int&gt; <span class="kw2">const</span> &amp; x <span class="br0">&#41;</span>;<br />
<br />
<span class="kw2">void</span><br />
foobar<span class="br0">&#40;</span> shared_ptr&lt;foo&gt; <span class="kw2">const</span> &amp; a <span class="br0">&#41;</span><br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; shared_ptr&lt;int&gt; b<span class="br0">&#40;</span>a,&amp;a-&gt;x<span class="br0">&#41;</span>;<br />
&#160; &#160; bar<span class="br0">&#40;</span> b <span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="br0">&#125;</span></div></div>
</div>
<p class='vspace'>Note: the <code>b</code> object <em>shares ownership</em> with <code>a</code>; that is, creating/destroying <code>a</code> or <code>b</code> copies will manipulate the same refcount. And when that refcount reaches zero, <em>the foo destructor will be called, even if the last reference to it was through a <code>shared_ptr&lt;int&gt;</code></em>.
</p>
<div class='vspace'></div><h2>Pointer arithmetics?</h2>
<p>It's even possible to provide <code>operator++/--</code> for <code>shared_ptr</code>:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;"><span class="kw1">template</span> &lt;class T&gt;<br />
shared_ptr&lt;T&gt; &amp;<br />
<span class="kw1">operator</span>++<span class="br0">&#40;</span> shared_ptr&lt;T&gt; &amp; x <span class="br0">&#41;</span><br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; shared_ptr&lt;T&gt;<span class="br0">&#40;</span>x,x.<span class="me1">get</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="nu0">+1</span><span class="br0">&#41;</span>.<span class="me1">swap</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="kw1">return</span> x;<br />
&#160; &#160; <span class="br0">&#125;</span></div></div>
</div>
<p class='vspace'>This of course would be the prefix version of <code>operator++</code>, a postfix overload can be defined by using a second, unused parameter of type <code>int</code>. With this and a few additional similar overloads, you can use <code>shared_ptr&lt;T&gt;</code> to iterate over arrays of <code>T</code> managed by <code>shared_ptr</code>. This is way cool, if not that practical. :)
</p>
<div class='vspace'></div><h2>Using shared_ptr to manage system resources</h2>
<p>Here is how you can hold on to a <code>Direct3D9</code> object using <code>shared_ptr</code>:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;">boost::<span class="me2">shared_ptr</span>&lt;IDirect3D9&gt;<br />
direct3d_create<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw1">if</span><span class="br0">&#40;</span> IDirect3D9 * p = Direct3DCreate9<span class="br0">&#40;</span>D3D_SDK_VERSION<span class="br0">&#41;</span> <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; <span class="kw1">return</span> boost::<span class="me2">shared_ptr</span>&lt;IDirect3D9&gt;<span class="br0">&#40;</span>p,boost::<span class="me2">bind</span><span class="br0">&#40;</span>&amp;IUnknown::<span class="me2">Release</span>,_1<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="kw1">else</span><br />
&#160; &#160; &#160; &#160; BOOST_THROW_EXCEPTION<span class="br0">&#40;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; x_d3d<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; xi_function<span class="br0">&#40;</span><span class="st0">&#34;Direct3DCreate9&#34;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="br0">&#125;</span></div></div>
</div>
<p class='vspace'>Similar use, holding on to a <code>FILE</code>:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;">boost::<span class="me2">shared_ptr</span>&lt;FILE&gt;<br />
file_open<span class="br0">&#40;</span> <span class="kw2">char</span> <span class="kw2">const</span> * name, <span class="kw2">char</span> <span class="kw2">const</span> * mode <span class="br0">&#41;</span><br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw1">if</span><span class="br0">&#40;</span> FILE * f = fopen<span class="br0">&#40;</span>name,mode<span class="br0">&#41;</span> <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; <span class="kw1">return</span> boost::<span class="me2">shared_ptr</span>&lt;FILE&gt;<span class="br0">&#40;</span>f,&amp;fclose<span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="kw1">else</span><br />
&#160; &#160; &#160; &#160; BOOST_THROW_EXCEPTION<span class="br0">&#40;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; x_file_open<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; xi_errno<span class="br0">&#40;</span>errno<span class="br0">&#41;</span> &lt;&lt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; xi_function<span class="br0">&#40;</span><span class="st0">&#34;fopen&#34;</span><span class="br0">&#41;</span> &lt;&lt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; xi_file_name<span class="br0">&#40;</span>name<span class="br0">&#41;</span> &lt;&lt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; xi_file_mode<span class="br0">&#40;</span>mode<span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="br0">&#125;</span></div></div>
</div>
<p class='vspace'>The two examples above take advantage of the fact that the type returned by the API is a pointer, so the <code>shared_ptr</code> template can be directly instantiated with it.
</p>
<p class='vspace'>With a bit of trickery, <code>shared_ptr</code> can also be used to manage handles (of non-pointer type), for example OpenGL texture IDs:
</p>
<div class='vspace'></div><div class='sourceblock'>
 <div class='sourceblocktext'><div class="cpp" style="font-family: monospace;color: #303030; margin-left: 10px;">boost::<span class="me2">shared_ptr</span>&lt;GLuint const&gt;<br />
gl_gen_texture<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; GLuint b=<span class="nu0">0</span>;<br />
&#160; &#160; glGenTextures<span class="br0">&#40;</span><span class="nu0">1</span>,&amp;b<span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="kw1">if</span><span class="br0">&#40;</span> GLenum err=glGetError<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><br />
&#160; &#160; &#160; &#160; BOOST_THROW_EXCEPTION<span class="br0">&#40;</span><br />
&#160; &#160; &#160; &#160; &#160; &#160; x_opengl<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; xi_function<span class="br0">&#40;</span><span class="st0">&#34;glGenTextures&#34;</span><span class="br0">&#41;</span> &lt;&lt;<br />
&#160; &#160; &#160; &#160; &#160; &#160; xi_glerror<span class="br0">&#40;</span>err<span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&#160; &#160; assert<span class="br0">&#40;</span>b<span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="kw1">return</span> make_handle<span class="br0">&#40;</span>b,gl_delete_textures<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#160; &#160; <span class="br0">&#125;</span><br />
<br />
<span class="kw1">struct</span><br />
gl_delete_textures<br />
&#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; <span class="kw1">typedef</span> <span class="kw2">void</span> result_type;<br />
&#160; &#160; <span class="kw2">void</span><br />
&#160; &#160; <span class="kw1">operator</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#40;</span> GLuint b <span class="br0">&#41;</span> <span class="kw2">const</span><br />
&#160; &#160; &#160; &#160; <span class="br0">&#123;</span><br />
&#160; &#160; &#160; &#160; glDeleteTextures<span class="br0">&#40;</span><span class="nu0">1</span>,&amp;b<span class="br0">&#41;</span>;<br />
&#160; &#160; &#160; &#160; <span class="br0">&#125;</span><br />
&#160; &#160; <span class="br0">&#125;</span>;</div></div>
</div>
<p class='vspace'>(The <a class='wikilink' href='http://www.revergestudios.com/reblog/index.php?n=ReCode.SharedPtrHandles'>make_handle</a> function template used above is not part of Boost. Follow the link to see how it's defined.)
</p>
<p class='vspace'>In all of these examples, besides keeping track of the shared count and releasing the resource when the last <code>shared_ptr</code> copy expires, we gain the ability to use <code>weak_ptr</code> with those resources.
</p>
<div class='vspace'></div><h2>Using shared_ptr in games</h2>
<p>When talking about using <code>shared_ptr</code> in games, many programmers dismiss its benefits because they think in terms of the narrow scope of other smart "pointers", which merely know when to call <code>delete</code>. Due to their very dynamic nature, games must manage memory carefully and this often means avoiding <code>new</code> and <code>delete</code> altogether.
</p>
<p class='vspace'>But the ability of <code>shared_ptr</code> to decouple user code from the exact mechanics of allocating and freeing resources is, I would say, critically important in game development. For example, <code>shared_ptr</code> makes it trivial to implement a factory which allocates objects from a pre-allocated pool of static size, <em>without putting any burden on the user</em> to remember how to destroy them. What's more, the factory can <em>dynamically</em> choose between multiple pools when allocating each individual object <em>instance</em>, or even resort to a less optimal allocation when all pools have been exhausted.
</p>
<p class='vspace'>And since game programmers are especially prone to the "I'll write my own" syndrome, this is a good place to quote Scott Meyers:
</p>
<div class='vspace'></div><div class='indent'><em>"The STL itself contains no reference-counting smart pointer, and writing a good one -- one that works correctly all the time -- is tricky enough that you don't want to do it unless you have to. I published the code for a reference-counting smart pointer in More Effective C++ in 1996, and despite basing it on established smart pointer implementations and submitting it to extensive pre-publication reviewing by experienced developers, a small parade of valid bug reports has trickled in for years. The number of subtle ways in which reference-counting smart pointers can fail is remarkable."</em>
</div><div class='vspace'></div><h2>Overhead</h2>
<p>Yes, sometimes you can't afford to use <code>shared_ptr</code>. But do yourself a favor and don't <em>assume</em> that an alternative solution would outperform it. Only believe hard evidence; especially avoid basing your decisions on profiling some toy example. Only data from profiling your actual program (or a relevant part of it) is valid evidence.
</p>
<p class='vspace'>And even when faced with hard evidence that <code>shared_ptr</code> is slowing you down, ask yourself:
</p>
<div class='vspace'></div><ul><li>Have you considered the use of custom allocators with <code>shared_ptr</code>? You can optimize allocations <em>per object instance</em>, not only depending on object type. For example, you can easily use different allocation strategy for "small" vs. "big" object instances of the same type <code>T</code>.
</li><li>Have you considered storing the control block (which keeps the refcounts) and the object itself in the same memory block? This trick often outperforms even intrusive refcounting solutions.
</li><li>Have you considered the ability to store multiple control blocks (for different objects) in the same memory block? This trick avoids cache misses in use cases that are intensive on refcount changes.
</li></ul><p class='vspace'>In my experience if I can't afford <code>shared_ptr</code>, I can't afford any smart pointer. Which isn't the end of the world, I love plain old C pointers too. :)
</p><div class='messagehead' > 
<h5>Rotoglup  &#8212;  <span style='font-size:83%'>01 June 2009, 13:48</span>  </h5>
</div><div class='messageitem' > 
<p>Thanks for the summary presentation ! I often use boost shared_ptr but I wasn't aware of the aliasing support ! :]
</p>
<p class='vspace'>As you do not mention it, from your experience, do the systematically thread-safe increments/decrements of the refcounts (if threads are enabled in boost) have a significant overhead ?
</p>
<p class='vspace'>Could you please elaborate a little (or give a pointer for more information) on the possibilities to control the memory location of the control block ?
</p>
<p class='vspace'>Thanks again, cheers
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>01 June 2009, 15:34</span>  </h5>
</div><div class='messageitem' > 
<p>For me personally, the refcounting speed has never been an issue. If you pass <code>shared_ptr</code> objects by <code>const &amp;</code> (no reason not to!) about the only way you could get a significant hit from refcounting is if you put <code>shared_ptr</code> objects in a large <code>std::vector</code> which happens to reallocate. Easy cure for that case is to use <code>reserve</code>.
</p>
<p class='vspace'>About controlling the control block allocations -- basically, you just use the <a class='urllink' href='http://www.boost.org/doc/libs/release/libs/smart_ptr/shared_ptr.htm#allocator_constructor' rel='nofollow'>allocator support</a> in <code>shared_ptr</code>. Using a custom allocator requires the use of a custom deleter too, which means that the only allocator allocation <code>shared_ptr</code> will do is for the control block, since you pass the object pointer yourself (after having allocated it however you want) and of course you handle the object deallocation in the deleter.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Gregory  &#8212;  <span style='font-size:83%'>26 February 2010, 07:13</span>  </h5>
</div><div class='messageitem' > 
<p>For the record, Scott Meyers discusses whether on should use shared_ptr or auto_ptr as return types for factory functions.
</p>
<p class='vspace'><a class='urllink' href='http://www.aristeia.com/Papers/resourceReturnProblem.txt' rel='nofollow'>http://www.aristeia.com/Papers/resourceReturnProblem.txt</a>
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Nevin ":-)"  &#8212;  <span style='font-size:83%'>26 February 2010, 14:07</span>  </h5>
</div><div class='messageitem' > 
<p>I don't tend to pass smart pointers by const reference, preferring the paradigm that if the callee isn't participating in managing the lifetime of the object, then pass in either a raw pointer by value or pass the object itself in by const reference.
</p></div>
<p class='vspace'>
</p><div class='messagehead' > 
<h5>Emil  &#8212;  <span style='font-size:83%'>05 March 2010, 22:09</span>  </h5>
</div><div class='messageitem' > 
<p>Nevin ":-)" - Passing a shared_ptr by const reference still allows the callee to make a copy of it, right? So this doesn't violate the paradigm you're adhering to.
</p></div>
<div class='vspace'></div>
]]></description>
<dc:contributor>Emil</dc:contributor>
<dc:date>2010-03-06T06:09:24Z</dc:date>
<pubDate>Sat, 06 Mar 2010 06:09:24 GMT</pubDate>
</item>
</channel>
</rss>
