<?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>Codebloat</title>
	<atom:link href="http://www.codebloat.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codebloat.net</link>
	<description>Programming software technology.</description>
	<lastBuildDate>Wed, 08 Feb 2012 18:44:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Wordling Your Code Base With Vim</title>
		<link>http://www.codebloat.net/2012/02/wordling-your-code-base-with-vim/</link>
		<comments>http://www.codebloat.net/2012/02/wordling-your-code-base-with-vim/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 18:25:54 +0000</pubDate>
		<dc:creator>Lars Storjord</dc:creator>
				<category><![CDATA[Coding technique]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[visualization]]></category>
		<category><![CDATA[wordle]]></category>

		<guid isPermaLink="false">http://www.codebloat.net/?p=485</guid>
		<description><![CDATA[Visualizing your code base using]]></description>
			<content:encoded><![CDATA[<p>Visualizing your code base using <a href="http://www.wordle.net/create">wordle</a> is a nice technique to get a quick overview and impression of your source code. The downside is that you need to concatenate all your source code into a single text file before you can paste it into Wordle. In Linux, this is most easy to accomplish using command line tools. In Windows environments, however, you may not have cygwin installed, and so Vim is probably the easiest solution. (Update: A total of <strong>35</strong> key presses are required for this example.)<span id="more-485"></span></p>
<h3>Step 1: Generate a list of the file names in Vim.</h3>
<p>All the files I wanted were in the same folder, so I simply wrote</p>
<p><code>:r !dir /B *</code></p>
<p>If you have different folders, you would want file paths and not only file names. If you have other files in the same folder, you would want to specify a filter (like <code>*.cpp</code>).</p>
<h3>Step 2: Record a macro.</h3>
<p>Macros are wonderful if you want to repeat a few steps several times. First, position the cursor at the beginning of the line with the first file name. To record a macro, press <code>q</code> followed by the name of the register you want to record to macro to. Since I do not want to reuse this macro after I am done generating the file, I use the <code>q</code> register for my macro. You know, because I am too lazy to move my finger. Put together, this means you need to press <code>qq</code> in <em>normal mode</em> (i.e. after pressing Escape). The bottom left corner now says &#8216;recording&#8217; to indicate it is, well, recording your macro.</p>
<div id="attachment_497" class="wp-caption aligncenter" style="width: 426px"><a href="http://www.codebloat.net/wp-content/uploads/2012/02/recording.png"><img class=" wp-image-497 " title="recording" src="http://www.codebloat.net/wp-content/uploads/2012/02/recording.png" alt="recording the macro" width="416" height="280" /></a><p class="wp-caption-text">&#39;spiller inn&#39; means &#39;recording&#39;</p></div>
<p>&nbsp;</p>
<h3>Step 3: Read the contents of the current file name.</h3>
<p>You now want to press</p>
<p><code>dd:$r &lt;C-r&gt;"&lt;BS&gt;</code></p>
<p>The details are explained below. You should see something like this (before pressing backspace):</p>
<div id="attachment_500" class="wp-caption aligncenter" style="width: 426px"><a href="http://www.codebloat.net/wp-content/uploads/2012/02/running_command.png"><img class=" wp-image-500 " title="running_command" src="http://www.codebloat.net/wp-content/uploads/2012/02/running_command.png" alt="running the ex command" width="416" height="280" /></a><p class="wp-caption-text">Notice the first line is gone.</p></div>
<p>The <code>&lt;Key&gt;</code> construct is Vim notation to specify different keys and key combinations. <code>&lt;C-r&gt;</code> means &#8216;Hold down <strong>control</strong> and press <strong>r</strong>&#8216;, <code>&lt;BS&gt;</code> means backspace and <code>&lt;CR&gt;</code> means the enter/return key (<strong>C</strong>arriage <strong>R</strong>eturn). Let&#8217;s break this down:</p>
<p><code>dd</code> &#8211; delete the current line.<br />
<code>:</code> &#8211; start an Ex command.<br />
<code>$</code> &#8211; append at the end of the file.<br />
<code>r</code> &#8211; <strong>r</strong>ead the following into this buffer. This must be followed by a space.<br />
<code>&lt;C-r&gt;"</code> &#8211; paste contents of the given buffer. The <code>"</code> buffer contains the previously deleted line.<br />
<code>&lt;BS&gt;</code> &#8211; I need to press backspace to remove the <code>^M</code> (which means carriage return) from Windows line endings. YMMV.</p>
<p>And then finally execute the command with the enter key.</p>
<p>&nbsp;</p>
<div id="attachment_506" class="wp-caption aligncenter" style="width: 426px"><a href="http://www.codebloat.net/wp-content/uploads/2012/02/file_is_read.png"><img class=" wp-image-506 " title="file_is_read" src="http://www.codebloat.net/wp-content/uploads/2012/02/file_is_read.png" alt="file has been read" width="416" height="280" /></a><p class="wp-caption-text">And it goes a little something like something like this.</p></div>
<h3>Step 4: Prepare the next macro run.</h3>
<p>Take note of the line number above the cursor, i.e. the line with the last file name. In this example, the number is <strong>13</strong>. Now, press <code>gg</code> to return to the first line, then <code>q</code> to stop recording the macro. You may do a test run of the macro by pressing <code>@q</code> if you want to.</p>
<h3>Step 5: Finishing the text.</h3>
<p>Do you remember the number from the previous step? Good. Now press <code><em>n</em>@q</code>, but substitute <code><em>n</em></code> with the number from step 4. In our example, I would press <code>13@q</code>. This would run the <code>q</code> macro 13 times.</p>
<p><strong>Note:</strong> If you did a test run, decrement the number by one (since you have run the macro once), and press <code>@@</code> instead of <code>@q</code>. <code>@@</code> will re-run the previous macro and is faster to type than <code>@q</code>.</p>
<div id="attachment_508" class="wp-caption aligncenter" style="width: 426px"><a href="http://www.codebloat.net/wp-content/uploads/2012/02/finished.png"><img class=" wp-image-508 " title="finished" src="http://www.codebloat.net/wp-content/uploads/2012/02/finished.png" alt="all files read" width="416" height="280" /></a><p class="wp-caption-text">No files left, only beautiful code.</p></div>
<p>Voila! The files are all handily read into your buffer, ready to be pasted into Wordle.</p>
<hr />
<p>For completeness, here&#8217;s the generated result based on the example code base:</p>
<div id="attachment_517" class="wp-caption aligncenter" style="width: 620px"><a href="http://www.codebloat.net/wp-content/uploads/2012/02/example_wordle.png"><img class=" wp-image-517 " title="example_wordle" src="http://www.codebloat.net/wp-content/uploads/2012/02/example_wordle.png" alt="example wordle" width="610" height="428" /></a><p class="wp-caption-text">Words on a screen.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.codebloat.net/2012/02/wordling-your-code-base-with-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overwriting new and delete</title>
		<link>http://www.codebloat.net/2011/12/overwriting-new-and-delete/</link>
		<comments>http://www.codebloat.net/2011/12/overwriting-new-and-delete/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 09:17:05 +0000</pubDate>
		<dc:creator>Lars Storjord</dc:creator>
				<category><![CDATA[Coding technique]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[new]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[overloading]]></category>

		<guid isPermaLink="false">http://www.codebloat.net/?p=450</guid>
		<description><![CDATA[In C++, you can generally]]></description>
			<content:encoded><![CDATA[<p>In C++, you can generally use your own stuff if you do not like what you get from the language and standard library: You can overload functions, inherit classes and specialize templates &#8211; you can change things into what you want. You do not want to <a href="http://stackoverflow.com/questions/5133114/overloading-logical-operators-considered-bad-practice">overload &amp;&amp;, || and comma</a>. And you almost never want to overload the global operator new and operator delete.</p>
<p>But I am special, so I <em>do</em> want to.</p>
<p><span id="more-450"></span></p>
<p>First, let us recap what ::operator new and ::operator delete really are. (The double colons simply mean that they are in the global namespace, and <em>not</em> in the std namespace. But you probably already knew that.) <em>&#8220;Oh, that&#8217;s easy,&#8221;</em> you might say.<em>&#8221; When you say &#8216;</em>new Foo()<em>&#8216;, you call</em> ::operator new<em>, and when you </em>&#8216;delete foo&#8217;<em>, you call</em> ::operator delete<em>, right?&#8221;  </em>Wrong. Well, it is true that those operators are called, but they are just two pieces in the big picture. We need to separate between &#8216;the new <em>expression</em>&#8216; (sometimes called the new operator), and &#8216;operator new&#8217;.</p>
<div class="wp-caption alignright" style="width: 298px"><img class="  " title="operatornew" src="http://www.leoindustries.com/images/china_manufacturing_2.jpg" alt="operator new" width="288" height="192" /><p class="wp-caption-text">And so each bit allocated by operator new is flipped to its correct state</p></div>
<p><strong>Operator new</strong> is fairly simple, but comes in many forms. It is responsible for allocating memory, much like malloc(). It has two main forms: operator new and operator new[]. Each of these has three global overloads: The normal version, a <em>nothrow</em>-version and placement new. The latter two are simple: The nothrow variant should return a null pointer if allocation fails, rather than call any new_handlers or throw std::bad_alloc. Placement operator new just returns its pointer argument &#8211; it is basically a no-op used to trick the new-expression into calling the constructor for us. Apart from these six global variants, each class can overload its own operator new, thus allowing for an arbitrary number of different &#8216;operator new&#8217;s. The typical reason for overloading operator new is to allow for more efficient memory storage on a per-class basis.</p>
<p><strong>The new-expression</strong> is special. There are only two of them: new and new[]. Their mission is to allocate dynamic memory for whatever they are creating and call all the correct constructors. This includes constructors for each object in the case of new[]. The new-expression will also have to call the constructors for base classes or contained classes. They can not be overloaded or replaced.</p>
<p>As usual, Newton&#8217;s third law of C++ applies to new and delete &#8211; for every <em>new</em> there is an equal and opposite <em>delete</em>. Most of the above is equal (but opposite) for operator delete and the delete-expression.</p>
<p>&nbsp;</p>
<p>Normally, if you are not satisfied with ::operator new, then you overload it for your particular class or classes. But for me, the situation is different. I am developing embedded software, and the platform we work on needs to carefully place objects in memory. For C code, this means that we use a function called umalloc() rather than regular malloc(). For C++, we need to ensure that the new-expression gets its memory through umalloc(), which means replacing ::operator new.</p>
<p>Yes, <em>replacing</em>. Not overloading. The global &#8216;operator new&#8217;s and &#8216;operator delete&#8217;s are implicitly declared, and we can replace them simply by providing a new definition. But the behavior of ::operator new is not as straightforward as you might think. You can&#8217;t just write:</p>
<pre class="brush:cpp">void* operator new(size_t n) {
    void* new_memory = malloc(n);
    if (new_memory == nullptr) {
        return new_memory;
    }

    throw std::bad_alloc();
}</pre>
<p>As the accepted answer to <a href="http://stackoverflow.com/questions/7194127/how-should-i-write-iso-c-standard-conformant-custom-new-and-delete-operators">this question on StackOverflow</a> explains, there is more to it than that, and most of it boils down to new_handlers. I will spare you the boring details (you can read the answer for more information), and simply jump straight to the code I ended up with:</p>
<pre class="brush:cpp">#include &lt;new&gt;
#include &lt;stdexcept&gt;

// Scalar regular new
void* operator new(std::size_t n) throw(std::bad_alloc)
{
    using namespace std;

    for (;;) {
        void* allocated_memory = ::operator new(n, nothrow);
        if (allocated_memory != 0) return allocated_memory;

        // Store the global new handler
        new_handler global_handler = set_new_handler(0);
        set_new_handler(global_handler);

        if (global_handler) {
            global_handler();
        } else {
            throw bad_alloc();
        }
    }
}

// Scalar nothrow new
void* operator new(std::size_t n, std::nothrow_t const&amp;) throw()
{
    if (n == 0) n = 1;

    return malloc(n);
}

// Array regular new
void* operator new[](std::size_t n) throw(std::bad_alloc)
{
    return ::operator new(n);
}

// Array nothrow new
void* operator new[](std::size_t n, std::nothrow_t const&amp;) throw()
{
    return ::operator new(n, std::nothrow);
}

// Scalar regular delete (doesn't throw either)
void operator delete(void* p) throw()
{
    free(p);
}

// Scalar nothrow delete
void operator delete(void* p, std::nothrow_t const&amp;) throw()
{
    ::operator delete(p);
}

// Array regular delete
void operator delete[](void* p) throw()
{
    ::operator delete(p);
}

// Array nothrow delete
void operator delete[](void* p, std::nothrow_t const&amp;) throw()
{
    ::operator delete(p);
}</pre>
<p>The reason we have nothrow-versions of operator delete, even when regular delete never throws, is because of the law of equal and opposite <em>new</em>s and <em>delete</em>s<em> - </em>when memory has been allocated with nothrow new, it will be deallocated with nothrow delete.</p>
<p>With the exception of any bugs it may contain (I have not found any, so there are probably many left), this should be the typical way to replace ::operator new and ::operator delete. But you almost never want to do that anyway.</p>
<p>Unless you are special.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebloat.net/2011/12/overwriting-new-and-delete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mobile codebloat</title>
		<link>http://www.codebloat.net/2011/12/mobile-codebloat/</link>
		<comments>http://www.codebloat.net/2011/12/mobile-codebloat/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 14:50:57 +0000</pubDate>
		<dc:creator>Lars Storjord</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.codebloat.net/?p=445</guid>
		<description><![CDATA[Everyone has a smart phone]]></description>
			<content:encoded><![CDATA[<p>Everyone has a smart phone these days &#8212; they are so common that they are no longer smart phones, they are just <em>phones.</em> To make the mobile life easier for our readers, we are &#8211; at the request of <a href="http://twitter.com/kenneth_aa">@kenneth_aa</a> - putting some effort into making codebloat more mobile-friendly. Doing this is quite far from the expertise areas of any of us, so we would very much like input and feedback from you as to how we can make codebloat excellent reading on a mobile device.</p>
<p>The steps we have taken so far include:</p>
<ul>
<li>Using a more mobile-friendly theme when visiting the site from a mobile browser. (If you ever have any problems with the mobile browser detection, we would love to hear from you!)</li>
<li>Removing header image from the mobile theme.</li>
<li>Limiting the number of posts shown.</li>
<li>Splitting long posts into multiple pages when viewed on a mobile device.</li>
</ul>
<p>We hope you enjoy reading codebloat on a mobile device!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codebloat.net/2011/12/mobile-codebloat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Link Library &#8211; Saturday November 5</title>
		<link>http://www.codebloat.net/2011/11/link-library-saturday-november-5/</link>
		<comments>http://www.codebloat.net/2011/11/link-library-saturday-november-5/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 17:00:00 +0000</pubDate>
		<dc:creator>Håkon Robbestad Gylterud</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.codebloat.net/?p=430</guid>
		<description><![CDATA[Quick and Dirty Reinversion of]]></description>
			<content:encoded><![CDATA[<ul>
<li><a title="Sigfpe" href="http://blog.sigfpe.com/2011/10/quick-and-dirty-reinversion-of-control.html">Quick and Dirty Reinversion of Control</a></li>
<li><a href="http://en.wikibooks.org/wiki/Haskell/Zippers">Theseus and the Zipper</a></li>
<li><a href="http://blog.ezyang.com/2011/08/8-ways-to-report-errors-in-haskell-revisited/">8 Ways to Report Errors in Haskell Revisited</a></li>
<li><a href="http://blog.sigfpe.com/2008/09/on-writing-python-one-liners.html">On Writing Python One-liners</a></li>
<li><a href="http://blog.knatten.org/2011/10/28/the-difference-between-unspecified-and-undefined-behaviour/">The Difference Between Unspecified and Undefined Behaviour</a></li>
<li><a href="http://blog.knatten.org/2011/11/04/undefined-behaviour-%E2%80%94-worse-than-its-reputation/">Undefined Behaviour &#8211; Worse Than its Reputation?</a></li>
<li><a href="http://www.veritasium.com/2011/10/supersized-slow-mo-slinky-drop.html">Supersized Slow-Mo Slinky Drop</a></li>
<li><a href="http://math.andrej.com/2009/04/11/on-programming-language-design/">Programmers are just humans: forgetful, lazy, and they make every mistake imaginable</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codebloat.net/2011/11/link-library-saturday-november-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Link Library &#8211; Friday October 28</title>
		<link>http://www.codebloat.net/2011/10/link-library-friday-october-28/</link>
		<comments>http://www.codebloat.net/2011/10/link-library-friday-october-28/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 14:00:43 +0000</pubDate>
		<dc:creator>Lars Storjord</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.codebloat.net/?p=381</guid>
		<description><![CDATA[Is Java Really Losing Popularity]]></description>
			<content:encoded><![CDATA[<ul>
<li><a href="http://weblogs.java.net/blog/editor/archive/2011/10/22/java-really-losing-popularity-among-developers">Is Java Really Losing Popularity Among Developers?</a></li>
<li><a href="http://www.nytimes.com/2011/10/23/technology/at-waldorf-school-in-silicon-valley-technology-can-wait.html">A Silicon Valley School That Doesn&#8217;t Compute</a></li>
<li><a href="http://nathanmarz.com/blog/how-to-beat-the-cap-theorem.html">How to Beat the CAP Theorem</a></li>
<li><a href="http://robkennedy.com/2011/10/22/microsoft-releases-roslyn-project-open-compiler-api/">Microsoft Released Roslyn Project, Open Compiler API</a></li>
<li>StackOverflow: <a href="http://stackoverflow.com/q/7859424/667821">Why was mixing declarations and code forbidden up until C99?</a></li>
<li><a href="http://mhall119.com/2011/10/if-linux-was-a-car-haters-edition/">If Linux Was a Car (Hater&#8217;s Edition)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codebloat.net/2011/10/link-library-friday-october-28/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

