<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>#AltDevBlogADay &#187; Stefan Reinalter</title>
	<atom:link href="http://www.altdevblogaday.com/author/stefan-reinalter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.altdevblogaday.com</link>
	<description>Each day a little more #gamedev love</description>
	<lastBuildDate>Thu, 17 May 2012 03:06:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Building a memory system &#8211; Part 1: Fundamentals</title>
		<link>http://www.altdevblogaday.com/2012/02/10/building-a-memory-system-part-1-fundamentals/</link>
		<comments>http://www.altdevblogaday.com/2012/02/10/building-a-memory-system-part-1-fundamentals/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 10:10:44 +0000</pubDate>
		<dc:creator>Stefan Reinalter</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[memory alignment]]></category>
		<category><![CDATA[memory system]]></category>
		<category><![CDATA[new]]></category>
		<category><![CDATA[operator delete]]></category>
		<category><![CDATA[operator new]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=24062</guid>
		<description><![CDATA[<p>Today, I want to start a series on how to build your own memory system to be used in your game or engine. The series will cover how to handle allocations with vastly different lifetimes using specialized allocators, how to handle alignment restrictions, how to implement debugging features like memory tracking and tagging, and more. Before we can start, we need to delve into the inner workings of <em><strong>new, new[], </strong><strong>delete </strong></em>and<strong> </strong><em><strong>delete[] </strong></em>today &#8211; you may be surprised about some of the subleties involved.</p>
<p><a href="http://www.altdevblogaday.com/2012/02/10/building-a-memory-system-part-1-fundamentals/" class="more-link">Read more on Building a memory system &#8211; Part 1: Fundamentals&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today, I want to start a series on how to build your own memory system to be used in your game or engine. The series will cover how to handle allocations with vastly different lifetimes using specialized allocators, how to handle alignment restrictions, how to implement debugging features like memory tracking and tagging, and more. Before we can start, we need to delve into the inner workings of <em><strong>new, new[], </strong><strong>delete </strong></em>and<strong> </strong><em><strong>delete[] </strong></em>today &#8211; you may be surprised about some of the subleties involved.</p>
<p><span id="more-24062"></span>In order to keep things simpler and only concentrate on the crucial elements, we don&#8217;t deal with <em><strong>per-class new/delete</strong></em>, and we don&#8217;t want to mess with exceptions either, as they are rarely used in run-time game code.</p>
<h1>new operator / operator new</h1>
<p>The first thing to understand is that there is a difference between the <em><strong>new operator</strong></em> and <em><strong>operator new</strong></em>. Let&#8217;s look at a very simple statement involving the keyword <em><strong>new</strong>:</em></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">T<span style="color: #000040;">*</span> object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> T<span style="color: #008080;">;</span></pre></div></div>

<p>This is the simplest form of the <em><strong>new operator</strong></em>, probably used in many, many places in your code. What does it really do behind the scenes?</p>
<ol>
<li>First, a call to <em><strong>operator new</strong></em> (note the difference!) is made to request storage for a single T.</li>
<li>Second, the constructor for T is called which constructs the new instance of T at the memory address returned by the previous call to <em><strong>operator new</strong></em>.</li>
</ol>
<p>If T is of fundamental type (e.g. int, float, etc.), or does not have a constructor, no constructor will be called. The above statement will call the simplest form of <em><strong>operator new</strong></em>:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> operator <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> bytes<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Notice the <em><strong>size_t</strong></em> argument &#8211; the compiler will automatically insert code for calling <em><strong>operator new</strong></em> with the correct size for a given type, which is <em><strong>sizeof(T)</strong></em> in our case. Because operators behave like ordinary functions, they can be called manually, and can have different overloads as well. Those overloads can also be invoked by using different versions of the <em><strong>new operator</strong></em>, with the compiler generating code for calling the corresponding version of <em><strong>operator new</strong></em>. In fact, there&#8217;s another standard version of the <em><strong>new operator</strong></em>, with so called placement-syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> memoryAddress <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #208080;">0x100</span><span style="color: #008080;">;</span>
T<span style="color: #000040;">*</span> object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>memoryAddress<span style="color: #008000;">&#41;</span> T<span style="color: #008080;">;</span> <span style="color: #666666;">// placement-syntax, known as placement new</span></pre></div></div>

<p>This can be used to construct instances of classes at a certain place in memory, which in essence is the only way of calling constructors &#8220;directly&#8221;, because no memory allocation is involved here &#8211; the above calls a different overload of <em><strong>operator new</strong></em>, which is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> operator <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> bytes, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Even though this form of <em><strong>operator new</strong></em> takes <em><strong>size_t</strong></em> as the first argument, it does not allocate any memory, and just returns the pointer given in the second argument. That&#8217;s why our example simply invokes the constructor <em><strong>T::T()</strong></em> at address 0&#215;100.</p>
<p>The placement-syntax of the <em><strong>new operator</strong></em> is very powerful, because it allows us to invoke different overloads of <em><strong>operator new</strong></em> with an unlimited number of custom arguments. The only rule is that the first argument to every <em><strong>operator new</strong></em> must always be of type <em><strong>size_t</strong></em>, which will automatically be passed to it by the compiler.</p>
<p>Again, let&#8217;s look at an example illustrating the above:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> operator <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> bytes, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> file, <span style="color: #0000ff;">int</span> line<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// allocate bytes</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// calls operator new(sizeof(T), __FILE__, __LINE__) to allocate memory</span>
T<span style="color: #000040;">*</span> object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>__FILE__, __LINE__<span style="color: #008000;">&#41;</span> T<span style="color: #008080;">;</span></pre></div></div>

<p>Leaving differences between <em><strong>global operator new</strong></em> and <em><strong>class operator new</strong></em> out of the equation, every use of the placement form of the <em><strong>new operator</strong></em> boils down to the following internally:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// calls operator new(sizeof(T), a, b, c, d) to allocate memory</span>
T<span style="color: #000040;">*</span> object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>a, b, c, d<span style="color: #008000;">&#41;</span> T<span style="color: #008080;">;</span></pre></div></div>

<p>Which is semantically equivalent to:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">T<span style="color: #000040;">*</span> object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>operator <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, a, b, c, d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> T<span style="color: #008080;">;</span></pre></div></div>

<p>The magic of calling <em><strong>operator new</strong></em> is simply done by the compiler. Furthermore, remember that every overload of <em><strong>operator new</strong></em> can be called directly like ordinary functions, and we can do whatever we want with the different overloads. For example, we can even use templates if we want to:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> ALLOCATOR<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> operator <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> bytes, ALLOCATOR<span style="color: #000040;">&amp;</span> allocator, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> file, <span style="color: #0000ff;">int</span> line<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> allocator.<span style="color: #007788;">Allocate</span><span style="color: #008000;">&#40;</span>bytes<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This comes in handy later when we&#8217;re about to use different allocators, and want to provide additional arguments like e.g. alignment boundaries. The placement-syntax allows us to conveniently allocate memory with e.g. the following single-line statement:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">T<span style="color: #000040;">*</span> object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>allocator, alignment, __FILE__, __LINE__<span style="color: #008000;">&#41;</span> T<span style="color: #008080;">;</span></pre></div></div>

<h1>delete operator / operator delete<strong><br />
</strong></h1>
<p>This is probably no big surprise, but again, it is crucial to understand that there is a difference between the <em><strong>delete operator</strong></em> and <em><strong>operator delete.</strong></em> Calling the <em><strong>delete operator</strong></em> on a previously <em><strong>new</strong></em>&#8216;ed instance will first call the destructor, and then <em><strong>operator delete</strong></em>. Apart from the reverse order of operations, there&#8217;s another difference between <em><strong>new</strong></em> and <strong><em>delete</em></strong>: Regardless of which form of <em><strong>new</strong></em> we used to create the instance, the same version of <em><strong>operator delete</strong></em> will always be called (which is rather unfortunate when trying to implement advanced memory system techniques in later parts of the series):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// calls operator new(sizeof(T), a, b, c, d)</span>
<span style="color: #666666;">// calls T::T()</span>
T<span style="color: #000040;">*</span> object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>a, b, c, d<span style="color: #008000;">&#41;</span> T<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// calls T::~T()</span>
<span style="color: #666666;">// calls operator delete(void*)</span>
<span style="color: #0000dd;">delete</span> object<span style="color: #008080;">;</span></pre></div></div>

<p>The only time the corresponding <em><strong>operator delete</strong></em> is called by the compiler is when an exception is thrown inside <em><strong>operator new</strong></em>, so the memory can correctly be freed before the exception is propagated to the calling site. This is also the reason why every overload of <em><strong>operator new</strong></em> must always have a corresponding version of <em><strong>operator delete</strong></em>, even if it&#8217;s never called. But let&#8217;s not digress, we don&#8217;t want to deal with exceptions further.</p>
<p>Like <em><strong>operator new</strong></em>, <em><strong>operator delete</strong></em> can also be called directly (like an ordinary function):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> ALLOCATOR<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">void</span> operator <span style="color: #0000dd;">delete</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr, ALLOCATOR<span style="color: #000040;">&amp;</span> allocator, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> file, <span style="color: #0000ff;">int</span> line<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  allocator.<span style="color: #007788;">Free</span><span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// call operator delete directly</span>
operator <span style="color: #0000dd;">delete</span><span style="color: #008000;">&#40;</span>object, allocator, __FILE__, __LINE__<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>However, do not forget that the destructor is called by the <em><strong>delete operator</strong></em>, not <em><strong>operator delete</strong></em>. Hence, in the above example, the destructor needs to be called manually:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// call the destructor</span>
object<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>~T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// call operator delete directly</span>
operator <span style="color: #0000dd;">delete</span><span style="color: #008000;">&#40;</span>object, allocator, __FILE__, __LINE__<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>If instances are created wih the simple placement-form of <em><strong>new</strong></em>, the destructor must always be called manually. Using <em><strong>delete</strong></em> on such an instance would invoke undefined behaviour (because the memory was never allocated with a call to <em><strong>new</strong>).</em> Keep this in mind whenever you use <em><strong>placement new</strong></em>!</p>
<p>Having thoroughly discussed <em><strong>new/delete</strong></em>, let us take a look at their array siblings, <em><strong>new[]</strong></em> and <em><strong>delete[]</strong></em>.<em><br />
</em></p>
<h1>new[] / delete[]</h1>
<p>Even though you have probably used it a thousand times already, you may not realize that in something so fundamental such as <em><strong>new[]</strong></em> and <em><strong>delete[]</strong></em>, there&#8217;s already compiler magic involved. The reason for this is that the C++ standard just mandates <span style="text-decoration: underline">what</span> <em><strong>new[]</strong></em> and <em><strong>delete[]</strong></em> should do, but not <span style="text-decoration: underline">how</span>. Let us take a closer look, again starting with a simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">int</span> <span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Similar to the <em><strong>new operator</strong></em>, the above allocates storage for three <em><strong>ints</strong></em> by calling <em><strong>operator new[]</strong></em> (requesting memory), and since <em><strong>int</strong></em> is an integral type, there are no constructors to call. Similar to<em><strong> operator new</strong></em>, we can overload <em><strong>operator new[]</strong></em> and use placement-syntax as well:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// our own version of operator new[]</span>
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> operator <span style="color: #0000dd;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> bytes, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> file, <span style="color: #0000ff;">int</span> line<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// calls the above operator new[]</span>
<span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>__FILE__, __LINE__<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">int</span> <span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></pre></div></div>

<p><em><strong>delete[]</strong></em> and <em><strong>operator delete[]</strong></em> behave similar to<em><strong> delete</strong></em> and <em><strong>operator delete</strong></em> &#8211; we can call <em><strong>operator delete[]</strong></em> directly if we wish, but must make sure to call the destructors manually (in reverse order). Nothing too fancy, but what happens with non-<a title="POD types" href="http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html">POD</a> types?</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> Test
<span style="color: #008000;">&#123;</span>
  Test<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// do something</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ~Test<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// do something</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">int</span> a<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
Test<span style="color: #000040;">*</span> objects <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>__FILE__, __LINE__<span style="color: #008000;">&#41;</span> Test <span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Even though <em><strong>sizeof(Test) == 4</strong></em> (MSVC 2010, Windows 32-bit platform), our version of <em><strong>operator new[]</strong></em> will get called with an argument of 16 bytes, instead of 12 bytes. Why? Think about how the array needs to be deleted:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">delete</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> objects<span style="color: #008080;">;</span></pre></div></div>

<p>The compiler must somehow know how many instances of type <em><strong>Test</strong></em> are to be deleted &#8211; otherwise it can&#8217;t call the instances&#8217; destructors. So what almost every compiler does upon a call to <em><strong>new[]</strong></em> is the following:</p>
<ul>
<li>For N instances of type T, request an allocation for <em><strong>sizeof(T)*N + X &#8216;extra&#8217; </strong></em>bytes from <strong><em>operator new[]</em>.</strong></li>
<li>Store N somewhere in the first X bytes.</li>
<li>Construct N instances using <strong><em>placement new</em></strong>, starting at ptr + X<strong>.<br />
</strong></li>
<li>Return ptr + X to the user.</li>
</ul>
<p>Most of the time &#8211; if you don&#8217;t specify a particular alignment for a class using <a title="__declspec(align)" href="http://msdn.microsoft.com/en-us/library/83ythb65%28v=vs.80%29.aspx">__declspec(align)</a> (MSVC) or <a title="__attribute__((aligned))" href="http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Type-Attributes.html">__attribute__((aligned))</a> (GCC) &#8211; the amount of extra bytes requested will be 4, but that depends on both your class&#8217; alignment restrictions and the compiler. For example, if you specify <em><strong>Test</strong></em> to be 16-byte aligned using __declspec(align(16)), MSVC 2010 will request a total of 64 bytes in the example above.</p>
<p>As an example, let us use the definition of <em><strong>class Test</strong></em> from above: If your overload of <em><strong>operator new[]</strong></em> returns the memory address 0&#215;100,  <em><strong>Test* objects</strong></em> will point to 0&#215;104, because of the extra 4 bytes requested by the implementation! The memory layout of the 16 bytes would then be:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #208080;">0x100</span><span style="color: #008080;">:</span> <span style="color: #208080;">03</span> <span style="color: #208080;">00</span> <span style="color: #208080;">00</span> <span style="color: #208080;">00</span> <span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span> number of instances stored by the compiler<span style="color: #000040;">-</span>generated code
<span style="color: #208080;">0x104</span><span style="color: #008080;">:</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span> objects<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, Test<span style="color: #000040;">*</span> objects
<span style="color: #208080;">0x108</span><span style="color: #008080;">:</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span> objects<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>
<span style="color: #208080;">0x10c</span><span style="color: #008080;">:</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #008080;">??</span> <span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span> objects<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span></pre></div></div>

<p>When <em><strong>delete[]</strong></em> is used later on, the compiler inserts code which reads the number of instances N by going back 4 bytes from the given pointer, and calls the destructors in reverse order &#8211; if the type to be deleted is non-POD. Otherwise, there&#8217;s no 4 byte overhead added because no destructors need to be called (like in the <em><strong>new int[3]</strong></em> example above). If you ever wondered what the<a title="Vector deleting destructor" href="http://blogs.msdn.com/b/oldnewthing/archive/2004/02/03/66660.aspx"> vector deleting destructor() in MSVC</a> is for, there is your answer. Unfortunately, this compiler-defined behaviour causes problems when using our own overloads for<em><strong> operator new[]</strong></em> and <em><strong>operator delete[]</strong></em>.</p>
<p>As an example for using custom overloads, we might want to pass alignment restrictions to <em><strong>operator new[]</strong></em>, and return correctly aligned memory (e.g. on a 16-byte boundary). However, the compiler-implicit offset that is added to whatever we return will definitely screw with our alignment. Additionally, when we want to call <em><strong>operator delete[]</strong></em> directly, we somehow need to figure out how many destructors to call (if any).</p>
<p>Which we can&#8217;t.</p>
<p>The reason is that we can never be sure whether the compiler inserted some extra bytes (4, 8, or possibly more) in the allocation or not. This is totally compiler-dependent. It might work, but it could also horribly break with some user-defined types. And other compilers could do it differently altogether.</p>
<p>This is also the reason why using <em><strong>delete</strong></em> on instances allocated with <em><strong>new[]</strong></em> invokes undefined behaviour, and vice versa. The compiler-generated code simply tries to access memory which doesn&#8217;t belong to it (using <em><strong>delete[]</strong></em> for allocations via <em><strong>new</strong></em>), or not all instances of an array are correctly destructed (using <em><strong>delete</strong></em> for allocations via<em> <strong>new[]</strong></em>). This can have zero consequences (if the types don&#8217;t have a destructor), or crash your code (if the types have a destructor).</p>
<p>However, with the knowledge of what happens behind the scenes with calls to <em><strong>new</strong></em>, <em><strong>new[]</strong></em>, <em><strong>delete</strong></em> and <em><strong>delete[]</strong></em>, we can build our own allocation functions which correctly handle simple and array allocations for all types, can use our custom allocators, provide additional information like file name and line number, and more. The next post in this series will show how.</p>
<p>In the meantime, in case you want to read more about <em><strong>global operator new</strong></em> and <em><strong>class operator new</strong></em> (which we didn&#8217;t discuss here), here are recommended links:</p>
<p><a title="C++ new operator" href="http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr199.htm">new (C++)</a><br />
<a title="C++ delete operator" href="http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr199.htm">delete (C++)</a></p>
<p>Thanks to Michael Tedder, Paul Laska and Bruce Dawson for the feedback!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2012/02/10/building-a-memory-system-part-1-fundamentals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Policy-based design in C++</title>
		<link>http://www.altdevblogaday.com/2011/11/28/policy-based-design-in-c/</link>
		<comments>http://www.altdevblogaday.com/2011/11/28/policy-based-design-in-c/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 17:47:57 +0000</pubDate>
		<dc:creator>Stefan Reinalter</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[multiple inheritance]]></category>
		<category><![CDATA[policy-based design]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=20748</guid>
		<description><![CDATA[<p>One problem which often arises during programming is how to build a base set of functionality which can be extended by the user, while still being modular enough to make it easy to replace only certain parts of an implementation without having to resort to copy &#38; paste techniques. I guess everybody of us has faced this problem at least once, and came up with different solutions. There is a powerful and elegant technique called <strong>policy-based design</strong> for solving this kind of problem, which is what I want to show today by applying it to a mechanism common in game development: logging.</p>
<p><a href="http://www.altdevblogaday.com/2011/11/28/policy-based-design-in-c/" class="more-link">Read more on Policy-based design in C++&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>One problem which often arises during programming is how to build a base set of functionality which can be extended by the user, while still being modular enough to make it easy to replace only certain parts of an implementation without having to resort to copy &amp; paste techniques. I guess everybody of us has faced this problem at least once, and came up with different solutions. There is a powerful and elegant technique called <strong>policy-based design</strong> for solving this kind of problem, which is what I want to show today by applying it to a mechanism common in game development: logging.</p>
<h1>The problem</h1>
<p>Let us assume that we want to add logging facilities to our game, and for that purpose we build a simple base class called <em>Logger</em>, which can be extended (or completely replaced) simply by deriving from it and overriding a virtual function. Note that we are not concerned about how log messages are dispatched to the different logger implementations, but rather how new logger-classes with completely different functionality are implemented.</p>
<p>A simple base class for loggers might look like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Logger
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">virtual</span> ~Logger<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Log<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> channel, <span style="color: #0000ff;">size_t</span> type, <span style="color: #0000ff;">size_t</span> verbosity, <span style="color: #0000ff;">const</span> SourceInfo<span style="color: #000040;">&amp;</span> SourceInfo, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> format, <span style="color: #0000ff;">va_list</span> args<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>As you can see, it&#8217;s nothing more than a very simple base class with one virtual function. The arguments are the log-channel (e.g. &#8220;TextureManager&#8221;, &#8220;SoundEngine&#8221;, &#8220;Memory&#8221;, etc), the type of log (e.g. INFO, WARNING, ERROR, FATAL), the verbosity level, a wrapper around source-code information called <em>SourceInfo</em> (file name, function name, line number, etc.), and last but not least the message itself in terms of a format string and a variable number of arguments. Nothing spectacular so far.</p>
<p>One possible logger implementation could be the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> IdeLogger <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Logger
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Log<span style="color: #008000;">&#40;</span><span style="color: #ff0000; font-style: italic;">/*arguments omitted for brevity*/</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// format message</span>
    <span style="color: #666666;">// do additional filtering based on channel, verbosity, etc.</span>
    <span style="color: #666666;">// output to IDE/debugger</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The <em>IdeLogger</em> outputs all log messages to e.g. the MSVC output window by using OutputDebugString(). In order to do that, it formats the message in a certain kind of way, applies additional filtering based on the channel, verbosity, etc., and finally outputs the message. We might want another logger which logs to the console, and one which writes into a file, so we could simply add two additional classes called <em>ConsoleLogger</em> and <em>FileLogger</em>, which both derive from the <em>Logger</em> base class.</p>
<p>Sooner or later, this is where we run into problems, depending on what we want:</p>
<ul>
<li>A logger which writes to the console, but only filters based on the channel, not the verbosity level.</li>
<li>A logger which writes into a file, but doesn&#8217;t filter any messages because they are a useful tool for post-mortem debugging.</li>
<li>Slightly different formatting in one of the existing loggers, e.g. I like being able to click on log messages in Visual Studio&#8217;s output window because they are formatted like this:<br />
&#8220;C:/MyFilename.cpp(20): [TextureManager] (WARNING) Whatever.&#8221;</li>
<li>A logger sending messages over a TCP socket, without having to copy existing code for formatting/filtering.</li>
<li>Many more such features&#8230;</li>
</ul>
<h1>A deceivingly simple solution</h1>
<p>One solution which is sometimes applied to such problems is to put certain parts of an implementation into several base classes, and build a deep hierarchy of classes by using multiple inheritance. In essence, you end up with classes like <em>ConsoleLoggerWithVerbosityFilter</em>, <em>FileLoggerWithoutFilter</em> and <em>TcpLoggerWithExtendedFormatting</em> which multiply inherit from concrete implementations of base classes like <em>ILogFilter</em>, <em>ILogDestination</em> and <em>ILogFormat</em>. Personally, I tend to favor flat hierarchies without any coupling over deep hierarchies where leaf classes are sometimes affected by changes to some base class.</p>
<p>Furthermore, judging from experience you often need to add a bunch of virtual functions to such hierarchies only to make some leaf class work, or end up copy-pasting existing code because a seemingly innocent change might break some of the existing implementations, which is why I try to stay away from such solutions &#8211; they might work for certain problems, but I&#8217;ve often seen them break during the development of a product.</p>
<h1>Anatomy of the problem at hand</h1>
<p>Let us take another look at the problem, this time from a different angle, leaving out implementation details like base classes, inheritance, and so on.</p>
<p>What we essentially want is a mechanism which makes it easy to define new loggers based on existing functionality without copying any code, and without having to write a new logger implementation each and every time. What we want is a mechanism where parts of the implementation could essentially be assembled by writing only a few lines of code.</p>
<p>By splitting the logger&#8217;s responsibilities into smaller pieces, we can hopefully find some orthogonal functionality along the way. A logger essentially:</p>
<ul>
<li>filters the incoming message based on certain criteria, then</li>
<li>formats the message in a certain way, and finally</li>
<li>writes the formatted message to a certain output.</li>
</ul>
<p>If you think about it, these aspects are completely orthogonal to each other, which means that you can exchange the algorithm for filtering messages with any other without having to touch either the formatting or writing stage, and vice versa.</p>
<p>What we now would like to have is some mechanism for exchanging those aspects with very little amount of code. Such a mechanism can be achieved by making use of templates, like in the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> FilterPolicy, <span style="color: #0000ff;">class</span> FormatPolicy, <span style="color: #0000ff;">class</span> WritePolicy<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> LoggerImpl <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Logger
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Log<span style="color: #008000;">&#40;</span><span style="color: #ff0000; font-style: italic;">/*arguments omitted for brevity*/</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// pseudo code-ish...</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>m_filter.<span style="color: #007788;">Filter</span><span style="color: #008000;">&#40;</span>certain critera<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      m_formatter.<span style="color: #007788;">Format</span><span style="color: #008000;">&#40;</span>some buffer, criteria<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      m_writer.<span style="color: #007788;">Write</span><span style="color: #008000;">&#40;</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  FilterPolicy m_filter<span style="color: #008080;">;</span>
  FormatPolicy m_formatter<span style="color: #008080;">;</span>
  WritePolicy m_writer<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The above is a very generic logger which passes on the tasks of filtering, formatting and writing messages to certain policies, which are handed down to the implementation via template parameters. Each policy does only a very small amount of work, but by combining them you can come up with several different logger implementations with only a single line of code, by means of a simple typedef, as shown later.</p>
<h1>Example policies</h1>
<p>Before we can discuss the pros/cons of this approach, let us quickly identify what some of the policies might look like in order to gain a better understanding of how such a system works:</p>
<h3>Filter policies</h3>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> NoFilterPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> Filter<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Criteria<span style="color: #000040;">&amp;</span> criteria<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// no filter at all</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> VerbosityFilterPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> Filter<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Criteria<span style="color: #000040;">&amp;</span> criteria<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// filter based on verbosity</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> ChannelFilterPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> Filter<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Criteria<span style="color: #000040;">&amp;</span> criteria<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// filter based on channel</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>As you can see, each policy takes care of filtering messages based on certain criteria, nothing more.</p>
<h3>Format policies</h3>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> SimpleFormatPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">void</span> Format<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Buffer<span style="color: #000040;">&amp;</span> buffer, <span style="color: #0000ff;">const</span> Criteria<span style="color: #000040;">&amp;</span> criteria<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// simple format, e.g. &quot;[TextureManager] the log message&quot;;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> ExtendedFormatPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">void</span> Format<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Buffer<span style="color: #000040;">&amp;</span> buffer, <span style="color: #0000ff;">const</span> Criteria<span style="color: #000040;">&amp;</span> criteria<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// extended format, e.g. &quot;filename.cpp(10): [TextureManager] (INFO) the log message&quot;;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<h3>Writer policies</h3>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> IdeWriterPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">void</span> Write<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Buffer<span style="color: #000040;">&amp;</span> buffer<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// output to the IDE</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> ConsoleWriterPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">void</span> Write<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Buffer<span style="color: #000040;">&amp;</span> buffer<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// output to the console</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> FileWriterPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">void</span> Write<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Buffer<span style="color: #000040;">&amp;</span> buffer<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// write into a file</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<h1>Discussion</h1>
<p>By dissecting the problem into different aspects, we can now implement very small functions/structs called policies, which can be assembled together in any way we wish by using just a single line of code. Some examples:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> LoggerImpl<span style="color: #000080;">&lt;</span>NoFilterPolicy, ExtendedFormatPolicy, IdeWriterPolicy<span style="color: #000080;">&gt;</span> IdeLogger<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> LoggerImpl<span style="color: #000080;">&lt;</span>VerbosityFilterPolicy, SimpleFormatPolicy, ConsoleWriterPolicy<span style="color: #000080;">&gt;</span> ConsoleLogger<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> LoggerImpl<span style="color: #000080;">&lt;</span>NoFilterPolicy, SimpleFormatPolicy, FileWriterPolicy<span style="color: #000080;">&gt;</span> FileLogger<span style="color: #008080;">;</span></pre></div></div>

<h3>Advantages</h3>
<ul>
<li>With only 3 filter policies, 2 format policies, and 3 writer policies we are able to come up with 3*2*3 = 18 different implementations, just by using a simple typedef.</li>
<li>Each part of an implementation can be replaced separately, so if you e.g. add a <em>TcpWriterPolicy</em> you can combine it with any other filter or format policy, without having to resort to copy-pasting.</li>
<li>Pieces of policies can be assembled in any way we want.</li>
<li>New policies can be build using existing policies simply by combining them.</li>
<li>If any combination of policies does not exactly do what you want, you can still implement your own logger by deriving from the <em>Logger</em> base class, without being forced into a certain inheritance structure, except the single virtual function in the base class.</li>
<li>Simple, flat hierarchies, no multiple inheritance used.</li>
<li>One virtual function call instead of several ones (multiple inheritance of several interface classes).</li>
<li>Unit testing becomes a lot more easier because each policy has exactly one responsibility.</li>
</ul>
<p>An example of how to build new policies out of existing ones:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Policy1, <span style="color: #0000ff;">class</span> Policy2<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> CompositeFilterPolicy
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> Filter<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Criteria<span style="color: #000040;">&amp;</span> criteria<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>m_policy1.<span style="color: #007788;">Filter</span><span style="color: #008000;">&#40;</span>criteria<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> m_policy2.<span style="color: #007788;">Filter</span><span style="color: #008000;">&#40;</span>criteria<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  Policy1 m_policy1<span style="color: #008080;">;</span>
  Policy2 m_policy2<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">typedef</span> LoggerImpl<span style="color: #000080;">&lt;</span>CompositeFilterPolicy<span style="color: #000080;">&lt;</span>VerbosityFilterPolicy, ChannelFilterPolicy<span style="color: #000080;">&gt;</span>, ExtendedFormatPolicy, IdeWriterPolicy<span style="color: #000080;">&gt;</span> FilteredIdeLogger<span style="color: #008080;">;</span></pre></div></div>

<h3>Drawbacks</h3>
<ul>
<li>Template parameters are part of the class name, leading to longer class names, in turn making the code harder to debug if you&#8217;re not used to it.</li>
<li>Increased compile times if you&#8217;re not careful. I would recommend using explicit template instantiation for required logger implementations.</li>
</ul>
<h1>Conclusion</h1>
<p>Policy-based design is a powerful tool which can be used in several situations, not just the one shown here. It can lead to extendable and modular classes if applied correctly, but it isn&#8217;t a silver bullet which can forever solve our architectural problems, hence it shouldn&#8217;t be applied blindly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/11/28/policy-based-design-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++&#8217;s unary+ considered useful</title>
		<link>http://www.altdevblogaday.com/2011/11/11/cs-unary-considered-useful/</link>
		<comments>http://www.altdevblogaday.com/2011/11/11/cs-unary-considered-useful/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 15:49:28 +0000</pubDate>
		<dc:creator>Stefan Reinalter</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[unary+]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=20085</guid>
		<description><![CDATA[<p><em>No, this post is not an attempt at sneaking the word &#8220;plus&#8221; into the title three times. Instead, I want to share a short C++ trick today, which I stumbled upon half a year ago when I was teaching my students the difference between some of the operators in C and C++, so I hope this is useful to some of you.</em></p>
<p><a href="http://www.altdevblogaday.com/2011/11/11/cs-unary-considered-useful/" class="more-link">Read more on C++&#8217;s unary+ considered useful&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p><em>No, this post is not an attempt at sneaking the word &#8220;plus&#8221; into the title three times. Instead, I want to share a short C++ trick today, which I stumbled upon half a year ago when I was teaching my students the difference between some of the operators in C and C++, so I hope this is useful to some of you.</em></p>
<p>Dealing with function template code can sometimes result in errors being emitted by the compiler because template argument deduction is ambiguous. Most of the time, casts are used to battle these errors, but often there&#8217;s a more elegant solution to the problem.</p>
<p>The following code shows a very simple function template for returning the maximum of any two values, build as a template in order for it to work on any type:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
T Max<span style="color: #008000;">&#40;</span>T lhs, T rhs<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>lhs <span style="color: #000080;">&gt;=</span> rhs<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> lhs <span style="color: #008080;">:</span> rhs<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The function is built as a template in order to be able to specialize it for e.g. <strong>float</strong>, using fast, branch-less floating-point selects (<a title="fsel" href="http://publib.boulder.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.aixassem%2Fdoc%2Falangref%2Ffsel.htm">fsel</a>) on consoles. The same can be done for other functions such as <strong>Min</strong>, <strong>Clamp</strong>, etc. &#8211; but let&#8217;s not digress.</p>
<p>Such a template function can either be called with or without an explicit template argument:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// explicit template argument</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, <span style="color: #0000dd;">200</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// template argument omitted</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, <span style="color: #0000dd;">200</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>If the template argument is omitted when calling a template function, figuring out the correct type T is up to the compiler, which is done using a process known as <a title="Template argument deduction" href="http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Ftemplate_argument_deduction.htm">template argument deduction</a>.</p>
<p>But when dealing with template argument deduction, the values involved do not undergo <a title="Standard conversions" href="http://msdn.microsoft.com/en-us/library/aetzh118.aspx">standard conversions</a> such as type promotions:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">100</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span>i, 200u<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The above code will result in the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">error C2782<span style="color: #008080;">:</span> <span style="color: #FF0000;">'T Max(T,T)'</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">template</span> parameter <span style="color: #FF0000;">'T'</span> is ambiguous
could be <span style="color: #FF0000;">'unsigned int'</span>
or       <span style="color: #FF0000;">'int'</span></pre></div></div>

<p>Of course, in this example we could have just written <strong>200</strong> instead of <strong>200u</strong> and that would have fixed the problem, but it gets more interesting when dealing with enums and class conversion operators:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">enum</span> Test
<span style="color: #008000;">&#123;</span>
  VALUE_1 <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span>,
  VALUE_10 <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> TestClass
<span style="color: #008000;">&#123;</span>
  operator <span style="color: #0000ff;">int</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// ambiguous, eventhough enums can be implicitly converted to ints</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, VALUE_10<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// needs an explicit cast</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>VALUE_10<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// ambiguous, eventhough TestClass defines a conversion operator</span>
TestClass tc<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, tc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// again, needs an explicit cast</span>
TestClass tc<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>tc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>As can be seen, even though <strong>10</strong> and <strong>VALUE_10</strong> can easily be treated as <strong>ints</strong>, the template argument deduction process is ambiguous, because the types the compiler sees are <strong>int</strong> and <strong>Test</strong>, hence it doesn&#8217;t know which one to choose. The same happens with instances of type <strong>TestClass</strong>, which defines a conversion operator to int &#8211; in both cases we need to add an explicit cast as a workaround.</p>
<p>It would be nice if we could somehow tell the C++ compiler to carry out conversion and type promotion first, before deducing template arguments. Luckily, it turns out that there is a way to do that, by way of using the <a title="Unary+ operator" href="http://msdn.microsoft.com/de-de/library/ewkkxkwb.aspx">unary+ operator</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// works with type promotion</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, <span style="color: #000040;">+</span>VALUE_10<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// works with conversion operators</span>
TestClass tc<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> max <span style="color: #000080;">=</span> Max<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>, <span style="color: #000040;">+</span>tc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Notice the plus in front of <strong>VALUE_10</strong> and <strong>tc</strong>. Using this operator, the types used in template argument deduction will first undergo implicit conversions, solving such problems without having to either assign to a temporary variable, or using an explicit cast.</p>
<p><em> This is an <a title="A lesser known C++ operator" href="http://molecularmusings.wordpress.com/2011/09/12/a-lesser-known-c-operator/">older post from my blog</a>, but I figured it might be useful to others as well.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/11/11/cs-unary-considered-useful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quasi compile-time string hashing</title>
		<link>http://www.altdevblogaday.com/2011/10/27/quasi-compile-time-string-hashing/</link>
		<comments>http://www.altdevblogaday.com/2011/10/27/quasi-compile-time-string-hashing/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 17:59:23 +0000</pubDate>
		<dc:creator>Stefan Reinalter</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[interning]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=19359</guid>
		<description><![CDATA[<p>One scenario that is quite common in all game engines is having to look-up some resource (texture, shader parameter, material, script, etc.) based on a string, with e.g. code like the following:</p>
<p><a href="http://www.altdevblogaday.com/2011/10/27/quasi-compile-time-string-hashing/" class="more-link">Read more on Quasi compile-time string hashing&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>One scenario that is quite common in all game engines is having to look-up some resource (texture, shader parameter, material, script, etc.) based on a string, with e.g. code like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Texture<span style="color: #000040;">*</span> tex <span style="color: #000080;">=</span> textureLibrary<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>FindTexture<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;my_texture&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
ShaderParameter param <span style="color: #000080;">=</span> shader<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>FindParameter<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;matrixWVP&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>For today, we are not so much concerned about how the key-value pairs are stored, and how the look-up is done internally (<a title="Managing Decoupling Part 4" href="http://altdevblogaday.com/2011/09/23/managing-decoupling-part-4-the-id-lookup-table/">read this excellent post instead</a>), but rather how to get rid of any hashing done at run-time. You might be surprised at how good C++ compilers have become when it comes to evaluating expressions known at compile-time, so let us put that to good use.</p>
<p>Assuming all our textures are stored in a table using some kind of (Hash, Texture) key-value pair (be it in a std::map, a simple array, or something more advanced), the code for finding a texture would vaguely look as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Texture<span style="color: #000040;">*</span> TextureLibrary<span style="color: #008080;">::</span><span style="color: #007788;">FindTexture</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> hash <span style="color: #000080;">=</span> CalculateHash<span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// lookup the texture in some container using 'hash'</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So even for constant strings, <strong>CalculateHash()</strong> will still be called, and the hash has to be generated each and every time this function is called with a constant string. This is something we would like to get rid of.</p>
<p>The first step is to ensure that each and every class dealing with hashes makes use of the same functionality, while still retaining the same syntax for users of those classes. So instead of taking a <strong>const char*</strong> as argument, we could define a simple class holding nothing more than a hash internally, which will be passed to the function by value instead:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> StringHash
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> m_hash<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
Texture<span style="color: #000040;">*</span> TextureLibrary<span style="color: #008080;">::</span><span style="color: #007788;">FindTexture</span><span style="color: #008000;">&#40;</span>StringHash hash<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// no more hash calculations in here</span>
  <span style="color: #666666;">// lookup the texture in some container using 'hash'</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This way, our hashed string class can tackle the problem of distinguishing between constant and non-constant strings, and we can be sure that no hash calculations take place inside the function. But still, how do we make sure that constant strings are not re-hashed every time?</p>
<h2>A simple hash function</h2>
<p>Let&#8217;s assume that hashing strings is done using a simple <a title="FNV-1a" href="http://isthe.com/chongo/tech/comp/fnv/" target="_blank">FNV-1a</a> hash with the following implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> CalculateFNV<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> str<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">size_t</span> length <span style="color: #000080;">=</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> hash <span style="color: #000080;">=</span> 2166136261u<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>length<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    hash <span style="color: #000040;">^</span><span style="color: #000080;">=</span> <span style="color: #000040;">*</span>str<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
    hash <span style="color: #000040;">*</span><span style="color: #000080;">=</span> 16777619u<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> hash<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Looking at the above FNV-1a hash implementation, let&#8217;s try to figure out the resulting hash values for known strings (don&#8217;t forget about the null terminator):</p>
<ul>
<li>&#8220;&#8221; = (2166136261u^ 0) * 16777619u</li>
<li>&#8220;a&#8221; = (((2166136261u^ &#8216;a&#8217;) * 16777619u)^ 0) * 16777619u</li>
<li>&#8220;ab&#8221; = (((((2166136261u^ &#8216;a&#8217;) * 16777619u)^ &#8216;b&#8217;) * 16777619u)^ 0) * 16777619u)</li>
</ul>
<p>The algorithm&#8217;s offset and prime are compile-time constants (I used 2166136261u and 16777619u in the example above), so these expressions really are constant.</p>
<h2>Helping the compiler</h2>
<p>All we need to do is give the compiler some help, which can be achieved by providing concrete implementations for strings of different lengths. Let&#8217;s put those into our StringHash class:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> StringHash
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  ME_INLINE StringHash<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span> m_hash<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>2166136261u<span style="color: #000040;">^</span> str<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> 16777619u<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ME_INLINE StringHash<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span> m_hash<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>2166136261u<span style="color: #000040;">^</span> str<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> 16777619u<span style="color: #008000;">&#41;</span><span style="color: #000040;">^</span> str<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> 16777619u<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// other implementations omitted</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>In case you&#8217;re not familiar with the syntax (and admittedly it&#8217;s one of the more awkward ones in C++), the constructors take references to const-char-arrays of sizes 1, 2, and so on. Providing different implementations for strings of different length tremendously helps the compiler/optimizer to fold the constant expressions into the final hash. In addition, we force each constructor to be inlined using <strong>ME_INLINE</strong>, which is a platform-independent variant of e.g. <strong>__forceinline</strong> (Visual Studio 2010), passing an additional hint to the optimizer.</p>
<h2>Spotting the pattern</h2>
<p>Many of you may have already spotted the <em>(offset^constant)*prime</em> pattern in the above examples, and indeed we can easily implement our constructors by utilizing the preprocessor:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define PREFIX(n, data)		((</span>
<span style="color: #339900;">#define POSTFIX(n, data)	^ str[n]) * 16777619u)</span>
&nbsp;
<span style="color: #339900;">#define ME_STRING_HASH_CONSTRUCTOR(n)									\
ME_INLINE StringHash::StringHash(const char (&amp;str)[n])							\
  : m_hash(ME_PP_REPEAT(n, PREFIX, ~) 2166136261u ME_PP_REPEAT(n, POSTFIX, ~))				\
{													\
}</span>
&nbsp;
<span style="color: #0000ff;">class</span> StringHash
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">6</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span>
&nbsp;
  <span style="color: #666666;">// other constructors omitted</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<h2>Did it work?</h2>
<p>With this in place, let&#8217;s take a look at the assembly code generated by the compiler to see whether our trick really worked. We will use the following simple example for that:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hash test: %d&quot;</span>, StringHash<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">GetHash</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hash test: %d&quot;</span>, StringHash<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">GetHash</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hash test: %d&quot;</span>, StringHash<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;aLongerTest&quot;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">GetHash</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hash test: %d&quot;</span>, StringHash<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;aVeryLongTestWhichStillWorks&quot;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">GetHash</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The resulting assembly code (Visual Studio 2010) looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">printf<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;Hash test: %d&quot;</span><span style="color: #339933;">,</span> StringHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #339933;">.</span>GetHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
  <span style="color: #0000ff;">01311436</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #0000ff;">50C5D1Fh</span>
  0131143B  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #000000; font-weight: bold;">offset</span> string <span style="color: #7f007f;">&quot;Hash test: %d&quot;</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">13341ECh</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  <span style="color: #0000ff;">01311440</span>  <span style="color: #00007f; font-weight: bold;">call</span>        printf <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">1328DD0h</span><span style="color: #009900; font-weight: bold;">&#41;</span>
printf<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;Hash test: %d&quot;</span><span style="color: #339933;">,</span> StringHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;test&quot;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #339933;">.</span>GetHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
  <span style="color: #0000ff;">01311445</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #0000ff;">0AA234B7Fh</span>
  0131144A  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #000000; font-weight: bold;">offset</span> string <span style="color: #7f007f;">&quot;Hash test: %d&quot;</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">13341ECh</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  <span style="color: #0000ff;">0131144F</span>  <span style="color: #00007f; font-weight: bold;">call</span>        printf <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">1328DD0h</span><span style="color: #009900; font-weight: bold;">&#41;</span>
printf<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;Hash test: %d&quot;</span><span style="color: #339933;">,</span> StringHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;aLongerTest&quot;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #339933;">.</span>GetHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
  <span style="color: #0000ff;">01311454</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #0000ff;">444D1A47h</span>
  <span style="color: #0000ff;">01311459</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #000000; font-weight: bold;">offset</span> string <span style="color: #7f007f;">&quot;Hash test: %d&quot;</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">13341ECh</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  0131145E  <span style="color: #00007f; font-weight: bold;">call</span>        printf <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">1328DD0h</span><span style="color: #009900; font-weight: bold;">&#41;</span>
printf<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;Hash test: %d&quot;</span><span style="color: #339933;">,</span> StringHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">&quot;aVeryLongTestWhichStillWorks&quot;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #339933;">.</span>GetHash<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
  <span style="color: #0000ff;">01311463</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #0000ff;">6D9D8B39h</span>
  <span style="color: #0000ff;">01311468</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #000000; font-weight: bold;">offset</span> string <span style="color: #7f007f;">&quot;Hash test: %d&quot;</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">13341ECh</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  0131146D  <span style="color: #00007f; font-weight: bold;">call</span>        printf <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">1328DD0h</span><span style="color: #009900; font-weight: bold;">&#41;</span></pre></div></div>

<p>As can be seen from the generated instructions (<strong>push 50C5D1Fh, push 0AA234B7Fh, push 6D9D8B39h</strong> and <strong>push 6D9D8B39h</strong>), the compiler/optimizer was indeed able to fold every string into its corresponding hash at compile-time, completely eliminating all traces to any StringHash constructor.</p>
<h2>Finishing touches</h2>
<p>All is well for constant strings, but what about non-constant ones? Of course we might want to use some kind of non-hardcoded string (e.g. a std::string, or strings coming from a file) every now and then, and need to provide a constructor for those as well:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> StringHash
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  StringHash<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> str<span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span> m_hash<span style="color: #008000;">&#40;</span>CalculateFNV<span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span>
&nbsp;
  <span style="color: #666666;">// other constructors omitted</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Now we&#8217;re in trouble. C++ overload resolution dictates that <strong>const char (&amp;str)[N]</strong> is as good a match for any of the constructors as <strong>const char*</strong>, because every array decays into a pointer automatically. This means that our class no longer works, because every constructor call is ambiguous:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// error: constructor overload resolution was ambiguous</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hash test: %d&quot;</span>, StringHash<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">GetHash</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>What we need to do is make the overload resolution process jump through another hoop for <strong>const char*</strong> arguments, so that constant string are considered first, and non-constant ones second. This can easily be achieved by using the following trick:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> StringHash
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">struct</span> ConstCharWrapper
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">inline</span> ConstCharWrapper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> str<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> m_str<span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> m_str<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  StringHash<span style="color: #008000;">&#40;</span>ConstCharWrapper str<span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span> m_hash<span style="color: #008000;">&#40;</span>CalculateFNV<span style="color: #008000;">&#40;</span>str.<span style="color: #007788;">m_str</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span>
  ME_STRING_HASH_CONSTRUCTOR<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span>
&nbsp;
  <span style="color: #666666;">// other constructors omitted</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>By making the constructor take a <strong>ConstCharWrapper </strong>instead of a <strong>const char*</strong>, we have altered the outcome of the overload resolution process. All constructors taking a reference to an array are now considered better matches by the compiler, because the constructor taking a <strong>ConstCharWrapper</strong> has to go through one implicit conversion, making the other constructors win in the case of constant strings. Similarly, non-constant strings can only be converted to a <strong>ConstCharWrapper</strong> implicitly, again disambiguating overload resolution.</p>
<p>Note that in order for this to work the <strong>ConstCharWrapper</strong> constructor is non-explicit on purpose.</p>
<h2>Conclusion</h2>
<p>Using the StringHash class introduced above, you can turn run-time hashing of constant strings into quasi compile-time constants without having to change calling code. I say &#8220;quasi&#8221; because the results of course cannot be used as a true compile-time constant (e.g. in a switch-statement, or as a non-type template argument), but rather relies on the compiler/optimizer instead. Therefore, it should be pointed out that this works in Visual Studio, as well as all major console platforms (Xbox360, PS3, Wii).</p>
<p>In addition, employing this trick reduces the size of the executable, because the constant strings are not used anymore and will therefore never end up in the read-only section of the executable, resulting in maybe a few extra KB of memory to use. This could also be beneficial for other applications making use of the above, e.g. compile-time string encryption, because the source strings are nowhere to be found.</p>
<p>&nbsp;</p>
<p><strong><em>Update: As suggested in the comments, you don&#8217;t need to use the preprocessor in order to define different constructors, but can use template meta-programming for that.</em></strong></p>
<p>One such possible template solution is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> N, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> I<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> FnvHash
<span style="color: #008000;">&#123;</span>
  ME_INLINE <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> Hash<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span>N<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>FnvHash<span style="color: #000080;">&lt;</span>N, I<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Hash</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span> <span style="color: #000040;">^</span> str<span style="color: #008000;">&#91;</span>I<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>16777619u<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> N<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> FnvHash<span style="color: #000080;">&lt;</span>N, <span style="color: #0000dd;">1</span><span style="color: #000080;">&gt;</span>
<span style="color: #008000;">&#123;</span>
  ME_INLINE <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> Hash<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span>N<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>2166136261u <span style="color: #000040;">^</span> str<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>16777619u<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> StringHash
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> N<span style="color: #000080;">&gt;</span>
  ME_INLINE StringHash<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span>N<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008080;">:</span> m_hash<span style="color: #008000;">&#40;</span>FnvHash<span style="color: #000080;">&lt;</span>N, N<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Hash</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Instead of writing several different constructors, we only need one templated constructor for constant strings now. This constructor in turn simply calls the <strong>Hash()</strong> function of the class template <strong>FnvHash</strong>. As can be seen, the <strong>FnvHash::Hash</strong> function just calls itself recursively, working its way from the end of the string to the beginning. The partial template specialization <strong>FnvHash&lt;N, 1&gt;</strong> serves as a mechanism to end the recursion.</p>
<p>The resulting assembly generated is the same, though it should be noted that the template solution probably is more taxing for the compiler, and depending on the compiler you use, you might have to alter options like inline recursion depth in order to make it work for longer constant strings.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/10/27/quasi-compile-time-string-hashing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgrading assert() using the preprocessor</title>
		<link>http://www.altdevblogaday.com/2011/10/12/upgrading-assert-using-the-preprocessor/</link>
		<comments>http://www.altdevblogaday.com/2011/10/12/upgrading-assert-using-the-preprocessor/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 14:27:32 +0000</pubDate>
		<dc:creator>Stefan Reinalter</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[assert]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[preprocessor]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=18313</guid>
		<description><![CDATA[<p>Today we will see how the preprocessor and some lesser-used C++ features can be used to enhance the standard assert functionality, shipping with the C runtime.</p>
<p>assert() is a very useful tool for ensuring that pre- and post-conditions, as well as invariants, are met upon calling or exiting a function. If you have never used assertions before, start using them now &#8211; they will help you find bugs in your own code, and quickly highlight when code is not used in the way originally intended.</p>
<p><a href="http://www.altdevblogaday.com/2011/10/12/upgrading-assert-using-the-preprocessor/" class="more-link">Read more on Upgrading assert() using the preprocessor&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today we will see how the preprocessor and some lesser-used C++ features can be used to enhance the standard assert functionality, shipping with the C runtime.</p>
<p>assert() is a very useful tool for ensuring that pre- and post-conditions, as well as invariants, are met upon calling or exiting a function. If you have never used assertions before, start using them now &#8211; they will help you find bugs in your own code, and quickly highlight when code is not used in the way originally intended.</p>
<p>&nbsp;</p>
<h2>What to improve?</h2>
<p>There are a few bits missing in the standard assert(). Specifically, the features I miss are the following:</p>
<ul>
<li>No way to output a (formatted) message to the user.</li>
<li>When a debugger is connected, the assert() does not halt execution in the line the assert() fired, but rather somewhere in assert.c. The new assert should trigger a breakpoint at the exact location the assert fired.</li>
<li>No way to show the values of individual variables used in the condition expression in assert(), only the whole condition is shown.</li>
</ul>
<p>Consider the following example which will demonstrate the above:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// somewhere in a FIFO implementation, the original assert:</span>
<span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>GetSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> GetCapacity<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// an improved assert:</span>
ME_ASSERT<span style="color: #008000;">&#40;</span>GetSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> GetCapacity<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;Cannot push another value into an already full FIFO.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>GetSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, m_end, m_read, m_fillCount<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Using the standard assert(), all you know is that the &#8220;Assertion GetSize() &lt; GetCapacity() failed.&#8221; If the assert fired on anybody else&#8217;s PC, this is not really helpful. How large was the FIFO? What did GetSize() yield? Was the FIFO really empty, or were the internal pointers messed up because of e.g. a memory stomp? All questions unanswered.</p>
<p>What we would like is an improved assert() which is able to fill in those gaps, and provide answers to exactly these questions.</p>
<p>&nbsp;</p>
<h2>Improved syntax</h2>
<p>In order to fulfill the requirements stated above, one possible syntax for the new assert could be the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ME_ASSERT<span style="color: #008000;">&#40;</span>condition, message, optional comma<span style="color: #000040;">-</span>separated list of message parameters<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>optional comma<span style="color: #000040;">-</span>separated list of variables<span style="color: #000040;">/</span>values<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>As we will see later, this syntax allows us to log variables and their values, trigger a debugger breakpoint in the line the ME_ASSERT macro was written, and does not generate any instructions in a retail build. Let&#8217;s take a look at a few examples to see how we would like to use our assertion macro:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ME_ASSERT<span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>m_start, m_end, m_allocEnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
ME_ASSERT<span style="color: #008000;">&#40;</span>from <span style="color: #000080;">&gt;=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #008080;">::</span><span style="color: #007788;">min</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;Number to cast exceeds numeric limits.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>from<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
ME_ASSERT<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">false</span>, <span style="color: #FF0000;">&quot;Key %s could not be found.&quot;</span>, key.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Before we start worrying about how to put that into a macro, let&#8217;s try to come up with the equivalent C++ code first. What we first need is a mechanism which allows us to log a formatted message, and an optional, unlimited number of variables, in that order. Logging itself can be done in any way you want, but the unlimited number of variables needs special treatment. There&#8217;s a well-known C++ idiom which achieves something similar, the <a title="Named parameter idiom" href="http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Named_Parameter">Named parameter idiom</a> which can be used to initialize (an unlimited amount of) class members in any order. In the same vein, we can use a temporary class instance and call its member functions returning a reference-to-self, which nicely fits the bill for our purposes:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Assert
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// logs a formatted message internally</span>
  Assert<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> file, <span style="color: #0000ff;">int</span> line, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> format, ...<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  Assert<span style="color: #000040;">&amp;</span> Variable<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> name, <span style="color: #0000ff;">bool</span> var<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Assert<span style="color: #000040;">&amp;</span> Variable<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> name, <span style="color: #0000ff;">char</span> var<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Assert<span style="color: #000040;">&amp;</span> Variable<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> name, <span style="color: #0000ff;">short</span> var<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Assert<span style="color: #000040;">&amp;</span> Variable<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> name, <span style="color: #0000ff;">int</span> var<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// more overloads for built-in types...</span>
&nbsp;
  <span style="color: #666666;">// generic template</span>
  Assert<span style="color: #000040;">&amp;</span> Variable<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> name, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// example usage</span>
Assert<span style="color: #008000;">&#40;</span>__FILE__, __LINE__, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Variable</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;m_start&quot;</span>, m_start<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Variable</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;m_end&quot;</span>, m_end<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>In the example above, a temporary instance of the Assert class is created on the stack, and the Variable() member-function can then be used to chain an unlimited number of Variable()-calls together. With this system in place, we can output both a variable&#8217;s name and its value for built-in types as well as user-defined types (e.g. Strings, Vectors, etc.). The former will use one of the existing overloads, the latter will use the member function template and assume that the given type either offers a c_str() method or a &lt;&lt; stream operator.</p>
<p>&nbsp;</p>
<h2>Debugger break</h2>
<p>Remember that we wanted to trigger a debugger breakpoint in the same line the assert has fired, after all the variable&#8217;s states have been logged. Triggering a breakpoint is not too hard by itself:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#if !ME_MASTER</span>
<span style="color: #339900;">#  define ME_BREAKPOINT (IsDebuggerConnected() ? __debugbreak() : ME_UNUSED(true))</span>
<span style="color: #339900;">#else</span>
<span style="color: #339900;">#  define ME_BREAKPOINT ME_UNUSED(true)</span>
<span style="color: #339900;">#endif</span></pre></div></div>

<p>The ME_BREAKPOINT macro triggers a breakpoint if a debugger is connected (IsDebuggerConnected() is a platform-dependent implementation returning whether a debugger is connected or not, e.g. IsDebuggerPresent() on Windows), and does nothing otherwise. Furthermore, the macro evaluates to ME_UNUSED(true) in retail builds, not generating any instructions (as we will see later).</p>
<p>Using the comma operator, we can use the above macro to trigger a breakpoint whenever an assert fires:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Assert<span style="color: #008000;">&#40;</span>__FILE__, __LINE__, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Variable</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;m_start&quot;</span>, m_start<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Variable</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;m_end&quot;</span>, m_end<span style="color: #008000;">&#41;</span>, ME_BREAKPOINT<span style="color: #008080;">;</span></pre></div></div>

<p>The comma operator ensures that the temporary Assert&#8217;s destructor has been called before the breakpoint is triggered, hence both the formatted message and the variables (and their values) will have been logged already. Of course, one thing we need to make sure is that the temporary Assert instance is created and the breakpoint triggered only if the assert&#8217;s condition is not met. This can simply be done by using the <a title="Conditional operator" href="http://msdn.microsoft.com/de-de/library/e4213hs1%28v=vs.90%29.aspx">conditional (?:) operator</a>, like in the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000ff;">true</span> <span style="color: #008080;">:</span> <span style="color: #008000;">&#40;</span>Assert<span style="color: #008000;">&#40;</span>__FILE__, __LINE__, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Variable</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;m_start&quot;</span>, m_start<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">Variable</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;m_end&quot;</span>, m_end<span style="color: #008000;">&#41;</span>, ME_BREAKPOINT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Note the extra parentheses before the Assert and after the breakpoint! Without these, the ME_BREAKPOINT statement would not be part of the second conditional operand, but rather be its own statement &#8211; meaning that a breakpoint would always be triggered, no matter if the condition was met or not (due to the operator precedence of the conditional operator and the comma operator).</p>
<p>Of course we don&#8217;t want users of the new assert to write code like this &#8211; it is much too error-prone and lengthy. Furthermore, we cannot eliminate the unneccessary code in retail builds without manual user intervention. Macros to the rescue!</p>
<p>&nbsp;</p>
<h2>Stuffing everything into a macro</h2>
<p>Putting everything into a macro requires quite some preprocessor magic, which we&#8217;ll try to tackle now. First, let&#8217;s try to stuff the left part of the example above into a macro:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define ME_ASSERT(condition, format, ...) (condition) ? (void)true : Assert(__FILE__, __LINE__, &quot;Assertion \&quot;&quot; #condition &quot;\&quot; failed. &quot; format, __VA_ARGS__)</span></pre></div></div>

<p>As you can surely see, this will expand into the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// macro</span>
ME_ASSERT<span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// expansion</span>
<span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end<span style="color: #008000;">&#41;</span> <span style="color: #008080;">?</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000ff;">true</span> <span style="color: #008080;">:</span> Assert<span style="color: #008000;">&#40;</span>__FILE__, __LINE__, <span style="color: #FF0000;">&quot;Assertion <span style="color: #000099; font-weight: bold;">\&quot;</span> m_start + i &lt; m_end <span style="color: #000099; font-weight: bold;">\&quot;</span> failed. Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Stuffing the optional list of variables into a macro is admittedly somewhat harder. Because of supporting printf-style formatted messages, our macro already is variadic (note the &#8230; as macro parameter), so how can we offer an additional, variable number of arguments? The following clearly doesn&#8217;t work:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define ME_ASSERT(condition, format, ..., ...) // huh?</span></pre></div></div>

<p>There&#8217;s no way to distinguish where one list of arguments ends, and where the next starts. That is, a variable number of arguments (&#8230;) must always be the last argument to a macro. But, by introducing an additional pair of parentheses, we can &#8220;start&#8221; the expansion of any other (variadic) macro, because you can think of the preprocessor running multiple passes as long as new function macros are detected. A simple example will show what I&#8217;m talking about:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define SIMPLE_MACRO(format, ...) Assert(format, __VA_ARGS__) ANOTHER_MACRO</span>
<span style="color: #339900;">#define ANOTHER_MACRO(...) // doing something with __VA_ARGS__</span></pre></div></div>

<p>Putting on our preprocessor hat, we can see that the following happens:</p>
<ul>
<li>In the first pass, SIMPLE_MACRO() gets expanded into some source code, ending in ANOTHER_MACRO. So if you write &#8220;SIMPLE_MACRO(foo, bar, a, b, c)()&#8221; (note the parentheses), it will get expanded into &#8220;Assert(foo, bar, a, b, c) ANOTHER_MACRO()&#8221;.</li>
<li>In the second pass, the preprocessor finds ANOTHER_MACRO() and recognizes it as another function macro. Hence, you can put a variable number of arguments into the parentheses, and they will be the arguments to the ANOTHER_MACRO macro.</li>
</ul>
<p>This way, we can have a variable number of arguments to the formatted message, and a variable number of arguments for our list of variables. But still, we need a macro which expands every single argument of a __VA_ARGS__ parameter into whatever we want. Specifically, we want to turn</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">(a, b, c)</pre></div></div>

<p>into</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">.Variable(&quot;a&quot;, a).Variable(&quot;b&quot;, b).Variable(&quot;c&quot;, c)</pre></div></div>

<p>This would allow us to expand the list of variables into .Variable()-calls on the temporary Assert instance.</p>
<p>&nbsp;</p>
<h2>Excursion: A journey into preprocessor land</h2>
<p>In order to get the job done, what we first need is a macro which expands the given arguments into an operation, called with the correct arguments. Something like the following does the trick:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define ME_PP_EXPAND_ARGS_1(op, a1) op(a1)</span>
<span style="color: #339900;">#define ME_PP_EXPAND_ARGS_2(op, a1, a2) op(a1) op(a2)</span>
<span style="color: #339900;">#define ME_PP_EXPAND_ARGS_3(op, a1, a2, a3) op(a1) op(a2) op(a3)</span>
<span style="color: #339900;">#define ME_PP_EXPAND_ARGS_4(op, a1, a2, a3, a4) op(a1) op(a2) op(a3) op(a4)</span>
<span style="color: #666666;">// other macros omitted...</span></pre></div></div>

<p>This works, but assumes that the caller already knows the amount of arguments provided. What we need is a variadic macro, which dispatches to any of the above macros depending on the number of arguments given. This can be done by first counting the number of arguments (using a separate macro), and then combining the result with the name of the macro &#8211; an example will make it clear:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// variadic macro &quot;dispatching&quot; the arguments to the correct macro.</span>
<span style="color: #339900;">#define ME_PP_EXPAND_ARGS(op, ...) ME_PP_JOIN(ME_PP_EXPAND_ARGS_, ME_PP_NUM_ARGS(__VA_ARGS__)) ME_PP_PASS_ARGS(op, __VA_ARGS__)</span></pre></div></div>

<p>Ok, let us try to decipher the above step by step, by assuming that we used the macro like this: ME_PP_EXPAND_ARGS(op, a, b)</p>
<ul>
<li>ME_PP_NUM_ARGS is the macro which &#8220;counts&#8221; the number of arguments given, and yields 2 in this case. An example of how such a macro can be implemented can be found <a title="A plethora of macros" href="http://molecularmusings.wordpress.com/2011/07/12/a-plethora-of-macros/">here</a>.</li>
<li>ME_PP_JOIN is a simple macro which joins two arguments, by &#8220;stitching&#8221; them together. In the example above, the result would be ME_PP_EXPAND_ARGS_2 (note the underscore at the end of the first argument), which in turn is a macro itself!</li>
<li>ME_PP_PASS_ARGS is used to pass the given macro arguments to the ME_PP_EXPAND_ARGS_2 macro (working around preprocessor bugs in Visual Studio).</li>
<li>Finally, the resulting ME_PP_EXPAND_ARGS_2(op, __VA_ARGS__) is expanded once again by the preprocessor.</li>
</ul>
<p>Note that the argument <strong>op</strong> can be a macro itself! With the above in place, we can turn the arguments &#8220;(a, b, c)&#8221; into &#8220;.Variable(&#8220;a&#8221;, a).Variable(&#8220;b&#8221;, b).Variable(&#8220;c&#8221;, c) using the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define ME_ASSERT(condition, format, ...) (condition) ? ME_UNUSED(true) : (Assert(__FILE__, __LINE__, &quot;Assertion \&quot;&quot; #condition &quot;\&quot; failed. &quot; format, __VA_ARGS__) ME_ASSERT_IMPL_VARS</span>
&nbsp;
<span style="color: #339900;">#define ME_ASSERT_IMPL_VARS(...) ME_PP_EXPAND_ARGS ME_PP_PASS_ARGS(ME_ASSERT_IMPL_VAR, __VA_ARGS__), ME_BREAKPOINT)</span>
&nbsp;
<span style="color: #339900;">#define ME_ASSERT_IMPL_VAR(variable) .Variable(#variable, variable)</span></pre></div></div>

<p>Again, ME_ASSERT() is a variadic macro ending in ME_ASSERT_IMPL_VARS, which in turn is a variadic macro itself. ME_ASSERT_IMPL_VARS expands and forwards the given arguments to ME_ASSERT_IMPL_VAR(), which is exactly what we use for automatically expanding our list of variables into Variable()-calls on the temporary Assert instance.</p>
<p>Finally, we can use our improved assertion facility. The following shows the output of an assertion which fired:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ME_ASSERT<span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>m_start, m_end, m_allocEnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Array.inl(146): [Assert] Assertion &quot;m_start + i &lt; m_end&quot; failed. Item 10 cannot be accessed. Subscript out of range.
Array.inl(146): [Assert]   o Variable m_start = 0x2260608 (pointer)
Array.inl(146): [Assert]   o Variable m_end = 0x2260608 (pointer)
Array.inl(146): [Assert]   o Variable m_allocEnd = 0x2260630 (pointer)
molecule_core_d.exe has triggered a breakpoint</pre></div></div>

<p>And a debugger breakpoint will conveniently halt execution at Array.inl(146).</p>
<p>&nbsp;</p>
<h2>Retail builds</h2>
<p>The last piece of the puzzle still missing is how to get rid of all the extra code in retail builds. Most C/C++ programmers know about the following trick, which is e.g. used for silencing compiler warnings about unused variables:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// i is unused</span>
<span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>i<span style="color: #008080;">;</span></pre></div></div>

<p>You can combine this trick with the sizeof() operator, and have the compiler accept every possible, legal expression in C++ without generating any instructions at all, because sizeof() will always be evaluated at compile-time. This means that something like</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>a <span style="color: #000080;">&lt;</span> b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>will never generate any instructions in the executable. (Ab)using the sizeof() operator this way, we can simply change our ME_ASSERT macro to behave as follows in retail builds:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define ME_ASSERT(condition, format, ...)	ME_UNUSED(condition), ME_UNUSED(format), ME_UNUSED(__VA_ARGS__), ME_UNUSED</span></pre></div></div>

<p>ME_UNUSED is a macro employing the trick stated above, turning all given arguments into an &#8220;empty&#8221; statement. Hence, in retail builds, all arguments to the assert as well as the list of variables will expand into pure &#8220;nothingness&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// macro</span>
ME_ASSERT<span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>m_start, m_end, m_allocEnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// expanded</span>
<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>m_start<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>m_end<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>m_allocEnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This also makes sure that the compiler will not warn about any unused local variables or similar, which is a nice side effect.</p>
<p>&nbsp;</p>
<h2>Performance</h2>
<p>One thing we haven&#8217;t talked about yet is performance. Debug builds have a tendency to become slower during development anyway, so we should make sure that our improved assert doesn&#8217;t totally kill peformance.</p>
<p>It should be quite clear that each ME_ASSERT can only have two outcomes &#8211; either the assert fires, or it doesn&#8217;t. We&#8217;re not so much concerned about performance in the former case, because when an assert fires, the amount of cycles/instructions it needed don&#8217;t really matter. But we should take a closer look at the performance of non-triggered asserts.</p>
<p>&nbsp;<br />
<strong>Non-optimized, debug builds</strong></p>
<p>Again, let&#8217;s work on an example by using on of the heavier asserts introduced before:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ME_ASSERT<span style="color: #008000;">&#40;</span>m_start <span style="color: #000040;">+</span> i <span style="color: #000080;">&lt;</span> m_end, <span style="color: #FF0000;">&quot;Item %d cannot be accessed. Subscript out of range.&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>m_start, m_end, m_allocEnd<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The generated assembly (Visual Studio 2010 SP1, x86) will look something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">012F7B50</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900; font-weight: bold;">&#93;</span>  
<span style="color: #adadad; font-style: italic;">012F7B53</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span><span style="color: #339933;">+</span><span style="color: #0000ff;">4</span><span style="color: #009900; font-weight: bold;">&#93;</span>  
<span style="color: #adadad; font-style: italic;">012F7B56</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>i<span style="color: #009900; font-weight: bold;">&#93;</span>  
<span style="color: #adadad; font-style: italic;">012F7B59</span>  <span style="color: #00007f; font-weight: bold;">lea</span>         <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span><span style="color: #339933;">+</span><span style="color: #00007f;">edx</span><span style="color: #339933;">*</span><span style="color: #0000ff;">4</span><span style="color: #009900; font-weight: bold;">&#93;</span>  
<span style="color: #adadad; font-style: italic;">012F7B5C</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900; font-weight: bold;">&#93;</span>  
<span style="color: #adadad; font-style: italic;">012F7B5F</span>  <span style="color: #00007f; font-weight: bold;">cmp</span>         <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span><span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>  
<span style="color: #adadad; font-style: italic;">012F7B62</span>  <span style="color: #00007f; font-weight: bold;">jae</span>         core<span style="color: #339933;">::</span>Array<span style="color: #339933;">::</span>operator<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">39h</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">12F7B69h</span><span style="color: #009900; font-weight: bold;">&#41;</span>  
<span style="color: #adadad; font-style: italic;">012F7B64</span>  <span style="color: #00007f; font-weight: bold;">jmp</span>         core<span style="color: #339933;">::</span>Array<span style="color: #339933;">::</span>operator<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">0DBh</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">12F7C0Bh</span><span style="color: #009900; font-weight: bold;">&#41;</span>
<span style="color: #adadad; font-style: italic;">012F7B69</span>  
  lots of instructions omitted<span style="color: #339933;">:</span>
    setting up parameters
    pushing offsets to strings <span style="color: #009900; font-weight: bold;">&#40;</span>e<span style="color: #339933;">.</span>g<span style="color: #339933;">.</span> <span style="color: #7f007f;">&quot;Assertion &quot;</span>m_start <span style="color: #339933;">+</span> i &lt; m_end<span style="color: #7f007f;">&quot; failed.&quot;</span><span style="color: #009900; font-weight: bold;">&#41;</span> onto the <span style="color: #000000; font-weight: bold;">stack</span>
    calling the Assert constructor
    calling Assert<span style="color: #339933;">::</span>Variable
    calling IsDebuggerConnected
<span style="color: #adadad; font-style: italic;">012F7C0A</span>  <span style="color: #00007f; font-weight: bold;">int</span>         <span style="color: #0000ff;">3</span>
<span style="color: #adadad; font-style: italic;">012F7C0B</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900; font-weight: bold;">&#93;</span>  
  rest of <span style="color: #000000; font-weight: bold;">code</span></pre></div></div>

<p>Don&#8217;t worry, there&#8217;s no need to completely understand all of the above. The important things are the following:</p>
<ul>
<li>The first instructions load <strong>m_start</strong> and <strong>m_end</strong> from memory (those are member variables).</li>
<li>The <strong>cmp</strong> and subsequent <strong>jae/jmp</strong> instructions carry out the comparison (m_start +i &lt; m_end).</li>
<li>If the condition is not true, the next instruction to be executed is at address 012F7B69.</li>
<li>If the condition is true, the next instruction to be executed is at address 012F7C0B.</li>
</ul>
<p>So in the case of the assert not firing, there&#8217;s some loads, a compare, and a jump instruction to execute. That&#8217;s not too much instruction-wise, but the code has to jump over a lot of instructions not executed (about 160 bytes), which will cause instruction-cache misses. If that causes too big a performance hit, one possible solution is to use somewhat more light-weight asserts deep inside your codebase, e.g. in leaf functions which get called <strong>a lot</strong>.</p>
<p>A possible implementation of such a light-weight assert could be the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define ME_LIGHTWEIGHT_ASSERT(cond)		(cond) ? true : __debugbreak()</span></pre></div></div>

<p>This is about as simple as it gets, and it will only ever do a comparison (and any loads needed), and a jump. The pattern to look for in the assembly code would then look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">cmp</span>         <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">,</span>that
<span style="color: #00007f; font-weight: bold;">jne</span>         fire 
<span style="color: #00007f; font-weight: bold;">jmp</span>         nofire
fire<span style="color: #339933;">:</span>       
  <span style="color: #00007f; font-weight: bold;">int</span> <span style="color: #0000ff;">3</span> 
nofire<span style="color: #339933;">:</span>
  rest of <span style="color: #000000; font-weight: bold;">code</span></pre></div></div>

<p><strong>__debugbreak</strong> is an intrinsic function available in Visual Studio which generates the <strong>int 3</strong> instruction, which in turn triggers a breakpoint inside the debugger. Similar functions/instructions exist on PowerPC as well, therefore such a light-weight assert can be built for any console platform as well.</p>
<p>&nbsp;<br />
<strong>Optimized builds</strong></p>
<p>Even though the compiler is smarter in optimized builds and manages to eliminate some unnecessary memory loads, the code for calling the assert constructor, setting up parameters and everything still takes about 100 bytes, so instruction-cache misses will likely be caused in those builds as well. Again, the light-weight assert can be of help here &#8211; in optimized builds, the compiler will be smart enough to get rid of one of the <strong>jmp </strong>instructions, generating even less code.</p>
<p>&nbsp;</p>
<h2>Static asserts</h2>
<p>Another thing worth mentioning is that this post only enhanced run-time asserts. There&#8217;s another type of assertions, so-called <strong>static </strong>or <strong>compile-time asserts</strong>. The new C++11 standard defines the static_assert keyword for that purpose, and Visual Studio 2010 and GCC already support it.</p>
<p>On other compilers, you can build your own static asserts using some template magic (found in the book &#8220;Modern C++&#8221; by Andrei Alexandrescu), or <a title="Static assert" href="http://andyfirth.blogspot.com/2010/08/static-assert-with-real-message.html">compiler-dependent features</a>.</p>
<p>&nbsp;</p>
<h2>Download</h2>
<p>The complete assert implementation can be downloaded <a href="http://altdevblogaday.com/wp-content/uploads/2011/10/Assert.txt">here</a> (released under the <a href="http://www.gzip.org/zlib/zlib_license.html" title="zlib license">zlib license</a>), and comes with the following:</p>
<ul>
<li>General preprocessor stuff like ME_PP_IF and ME_PP_TO_BOOL.</li>
<li>ME_JOIN macro to concatenate two tokens, even when the tokens are macros themselves.</li>
<li>ME_PP_IS_EMPTY macro for checking whether a variadic macro had an empty argument list or not.</li>
<li>ME_PP_NUM_ARGS macro for counting the number of arguments to a variadic macro.</li>
<li>ME_PP_EXPAND_ARGS macro for expanding an arbitrary amount of arguments into an operation, called with the correct arguments.</li>
<li>ME_UNUSED macro for turning any legal C++ expression into nothing, not generating any instructions.</li>
<li>ME_BREAKPOINT macro for triggering a breakpoint when a debugger is attached.</li>
<li>ME_ASSERT macro as described above, along with a simple implementation of the Assert class.</li>
</ul>
<p>Word of warning: most of the preprocessor magic is not for the faint of heart.</p>
<p>(This post is a more detailed cross-post of this <a title="Molecular Musings" href="http://molecularmusings.wordpress.com/2011/07/22/an-improved-assert/">blog entry</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/10/12/upgrading-assert-using-the-preprocessor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How the austrian guy ended up working in the games industry</title>
		<link>http://www.altdevblogaday.com/2011/09/27/how-the-austrian-guy-ended-up-working-in-the-games-industry/</link>
		<comments>http://www.altdevblogaday.com/2011/09/27/how-the-austrian-guy-ended-up-working-in-the-games-industry/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 08:42:49 +0000</pubDate>
		<dc:creator>Stefan Reinalter</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=17445</guid>
		<description><![CDATA[<p>I figured it would be quite nice to have some sort of introductory post here on #altdevblogaday, so what could be better than telling my story of how a guy from the small country of Austria (we&#8217;re the ones <strong>without</strong> the kangaroos, we&#8217;re living next door to Germany/Italy/Switzerland) ended up working in the games industry? So here goes&#8230;</p>
<p><a href="http://www.altdevblogaday.com/2011/09/27/how-the-austrian-guy-ended-up-working-in-the-games-industry/" class="more-link">Read more on How the austrian guy ended up working in the games industry&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I figured it would be quite nice to have some sort of introductory post here on #altdevblogaday, so what could be better than telling my story of how a guy from the small country of Austria (we&#8217;re the ones <strong>without</strong> the kangaroos, we&#8217;re living next door to Germany/Italy/Switzerland) ended up working in the games industry? So here goes&#8230;</p>
<p>&nbsp;</p>
<p><strong>Early addictions</strong><strong></strong></p>
<p>It all started more than 20 years ago, when my father introduced me to video gaming on the <a title="ColecoVision" href="http://en.wikipedia.org/wiki/ColecoVision">ColecoVision </a>console at the age of 5 or so &#8211; I still have fond memories of playing <a title="Mouse Trap" href="http://www.consoleclassix.com/colecovision/mouse_trap.html">Mouse Trap</a>, <a title="Donkey Kong" href="http://www.maniacworld.com/colecovision-donkey-kong.html">Donkey Kong</a>, and <a title="Pitfall" href="http://en.wikipedia.org/wiki/Pitfall!">Pitfall</a> on our old TV. Because games were so expensive back then (around ~100€ nowadays, not taking into account inflation!), my family used to buy games only twice or three times a year, leaving us with an incredible collection of about 8 games. Still, we used to play them whenever we were allowed to.</p>
<p>&nbsp;</p>
<p><strong>Arrival of the C64</strong></p>
<p>One or two years later, my father invested his money in our first computer &#8211; the Commodore 64. Ah, the C64!</p>
<p>Not only did it feature a vast collection of games, it also made it possible to copy games (piracy was a big problem) &#8211; there weren&#8217;t many legal retailers around in <a title="Austria" href="http://en.wikipedia.org/wiki/Austria">Austria </a>at that time, so we more or less had to get them by copying from somebody. Turned out my father knew a few people, so we ended up playing tons of games. Even though it&#8217;s been more than 20 years ago, thinking of some of the games and especially some of the SID chiptunes gives me goosebumps. Everybody of us put up with loading times somewhere around 10 mins. (possibly more) for larger games on the datasette, simply because the games were great. What a machine the C64 was, I loved it!</p>
<p>In addition to having a great collection of games, you could also write and run BASIC programs on the C64. Before even knowing what BASIC and programming in general was all about, my father introduced me to LOGO (which supported turtle graphics!), which must have been my very first contact with a simple programming language. Not long after figuring out how to draw simple snow-flakes using the turtle and its &#8220;turn-left&#8221;, &#8220;move-forward&#8221;, &#8220;turn-right&#8221; commands, I got introduced to BASIC.</p>
<p>Even though I was not able to grasp high-level concepts like for-loops and GOSUBs at that age, I remember that writing very simple BASIC programs was a lot of fun, and I used to sneak into my parents&#8217; bedroom when they weren&#8217;t around, just to use the C64. More often than not, I was trying to recreate simple stuff I had seen in other games, just to figure out how you could actually do something like that. Of course I had no idea how to use sprites, or what the C64&#8242;s architecture was all about, but getting the computer to do something I wanted was so enthralling.</p>
<p>&nbsp;</p>
<p><strong>Baby steps in programming</strong></p>
<p>A few years later, my father spent an awful lot of money on our first PC &#8211; a 386 with 33MHz. We used to play all-time classics like Wing Commander, Syndicate, UFO, and Doom &#8211; and invite people to our home, in order to participate in nights full of Doom deathmatches. I still remember how much I admired people working in the games industry, and I knew that being a game developer working on such a game would be like a dream come true for me. We still used to play games on the C64, but the PC was able to do so much more, so it attracted my attention. Around that time, my older brother began programming using Turbo Pascal for MSDOS, so naturally I wanted to learn as much as possible about PC programming, bothering him with questions endlessly.</p>
<p>That&#8217;s when one day I got introduced to the PC demo scene.</p>
<p>This must have been the point which sparked my thirst for low-level programming, hardware details, working at the assembly level, and getting the most out of any hardware. I just didn&#8217;t believe how something like <a title="Second Reality by FutureCrew" href="http://www.youtube.com/watch?v=8G_aUxbbqWU">Second Reality</a> or <a title="Crystal Dream 2" href="http://www.youtube.com/watch?v=hhbNNB_gUCE">Crystal Dream 2</a> was possible on my father&#8217;s poor 386 (those demos have to be one of the best demos ever made, and will always be remembered by me). I was stoked. I wanted to know every little detail about texture mapping, 3d algorithms, and mode 13h programming. But I didn&#8217;t know where to start. Luckily, my brother was already in college, and helped me learn a lot about vectors and matrices, some assembly, and programming in general.</p>
<p>&nbsp;</p>
<p><strong>Education</strong></p>
<p>I soon went to college (called HTL here in Austria) myself, and slowly started to learn C/C++ in my spare time &#8211; surprisingly, we were never really taught any programming language in school (except for a bit of Pascal), which is kind of uncommon for a college specialized in engineering. At that time, I was still tinkering around with texture mapping and the likes, but never really got that far with my own game. I was always more impressed with technological stuff, technical articles I read on the internet. All that mumbo-jumbo about 3d graphics and complicated sounding stuff just made it so much more interesting. I was addicted to technology and low-level details.</p>
<p>After having finished college, I knew that I didn&#8217;t want to go back to engineering, because somehow that topic lost its appeal over the years. Not really knowing what and where to study, I came upon a computer science master&#8217;s program at the Vienna University of Technology, so I ended up moving to Vienna and studying Computer Graphics &amp; Digital Image Processing there.</p>
<p>&nbsp;</p>
<p><strong>First payoff</strong></p>
<p>In retrospective, some of the stuff I was taught at university wasn&#8217;t the stuff I expected (to say the least), but some of it paved my way into the games industry. It was in the third semester of the Bachelor&#8217;s program where groups of students had to program their own 3d game using OpenGL. I knew that this was my chance to enhance my skills, and learn some state-of-the-art 3d programming. And of couse I was thrilled by the idea that we could write a game and actually get a grade for it, that I put lots and lots of time into it. In the end, our game ended up getting the &#8220;<a title="Best Game 2004" href="http://www.cg.tuwien.ac.at/courses/CG23/HallOfFame/2004/index.html">Best Game of 2004</a>&#8221; award!</p>
<p>Later that year during summer, I contacted one of the guys I knew from university who was working for a local game development studio (the Austrian game development scene is very, very small), and asked whether his employer was offering internships. It turned out that this guy was no longer working for said company, so he wasn&#8217;t aware of any internships. But he told me he could maybe arrange a job interview at his current employer if I&#8217;d be interested. Well, of course I was, so I took the chance, showed them my portfolio, showed them the game we did at university, and started work the next monday. After all those years, that&#8217;s how I ended up in the games industry.</p>
<p>&nbsp;</p>
<p><strong>Fast forward</strong></p>
<p>In the last 7 years, I&#8217;ve learned a lot about programming during my time in the industry. I&#8217;ve worked with some very talented, passionate and creative people. And even though I have been through times of crunch where I had to put in more than 100 hours a week, I can whole-heartedly recommend the industry as a great place to work with like-minded people. Joining the games industry has been one of the best decisions in my life, and it has always been a dream of mine.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/09/27/how-the-austrian-guy-ended-up-working-in-the-games-industry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

