<?xml version="1.0" encoding="UTF-8"?>
<rss 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/" version="2.0">

<channel>
	<title>trevorpounds.com</title>
	
	<link>http://www.trevorpounds.com/blog</link>
	<description>clean code from the digital bath</description>
	<lastBuildDate>Thu, 05 Jul 2012 01:16:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/trevorpounds" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="trevorpounds" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Building Older GCC Versions on x86-64 Debian/Ubuntu: Redux</title>
		<link>http://www.trevorpounds.com/blog/?p=513</link>
		<comments>http://www.trevorpounds.com/blog/?p=513#comments</comments>
		<pubDate>Wed, 23 May 2012 04:21:20 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c/c++]]></category>
		<category><![CDATA[compilers]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[GNU]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[x86-64]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=513</guid>
		<description><![CDATA[After 2+ years running Lucid Lynx I decided to upgrade to a recent version of Ubuntu, Oneiric Ocelot. I tend to stay behind the curve a bit to give early adopters the opportunity to work out most of the kinks. In the past I have been burned in my haste to stay on the bleeding [...]]]></description>
			<content:encoded><![CDATA[<p>After 2+ years running Lucid Lynx I decided to upgrade to a recent version of Ubuntu, Oneiric Ocelot. I tend to stay behind the curve a bit to give early adopters the opportunity to work out most of the kinks. In the past I have been burned in my haste to stay on the bleeding edge. Being 1-2 versions behind seems to work well to avoid any potential issues. Upgrading to Oneiric was mostly uneventful and I didn&#8217;t notice any compatibility issues until I tried building some code against an <a href="http://www.trevorpounds.com/blog/?p=111">older version of GCC</a> I previously built in Lucid. I encountered the following error anytime I tried to compile using the <code>-m64</code> flag:</p>
<pre>
/usr/bin/ld: cannot find crt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
collect2: ld returned 1 exit status
</pre>
<p>It turns out that recent Debian releases and their derivatives have transitioned to <a href="http://wiki.debian.org/Multiarch">multiarch</a> support. The idea is to allow seamless support of binaries built for different architectures, related or not, to live concurrently on the same system. In theory it should be more portable than the lib32/lib64 conventions of the past. Unfortunately, the side effect is that some applications depending on the old paths are now woefully broken. The good news is a simple workaround will allow older GCC versions to build without issue. Building an older version of GCC is still straightforward when following the <a href="http://www.trevorpounds.com/blog/?p=111">instructions in my previous post</a>, applying the necessary patches and running the following command to fix-up the compiler paths:</p>
<pre>
sed -i 's|\.\./lib64|x86_64-linux-gnu|' gcc-&lt;version&gt;/gcc/config/i386/t-linux64
</pre>
<p>For example, the following will build and install GCC 4.7.0 from source to a custom prefix:</p>
<pre>
tar xzf gcc-4.7.0.tar.gz
sed -i 's|\.\./lib64|x86_64-linux-gnu|' gcc-4.7.0/gcc/config/i386/t-linux64
mkdir ../gcc-4.7.0-objdir
cd ../gcc-4.7.0-objdir
../gcc-4.7.0/configure --prefix=/opt/x86_64/gcc/gcc-4.7.0 --enable-languages=c,c++
make
make install
</pre>
<p>Voila! Older GCC versions should now build and compile applications without issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=513</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Java DOM’s Iteration Irritation</title>
		<link>http://www.trevorpounds.com/blog/?p=344</link>
		<comments>http://www.trevorpounds.com/blog/?p=344#comments</comments>
		<pubDate>Sun, 25 Mar 2012 23:27:41 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdk]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=344</guid>
		<description><![CDATA[The last time I had to interact with Java&#8217;s DOM API I noticed that it had not been updated to make use of any recent Java 5 enhancements. Specifically, the org.w3c.dom.NodeList interface does not implement java.lang.Iterable. The unfortunate side effect is ugly and error prone code. // iteration sadness :( final NodeList nodes = document.getChildNodes&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>The last time I had to interact with Java&#8217;s DOM API I noticed that it had not been updated to make use of any recent Java 5 enhancements. Specifically, the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/NodeList.html">org.w3c.dom.NodeList</a> interface does not implement <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Iterable.html">java.lang.Iterable</a>. The unfortunate side effect is ugly and error prone code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// iteration sadness :(</span>
<span style="color: #000000; font-weight: bold;">final</span> NodeList nodes <span style="color: #339933;">=</span> document.<span style="color: #006633;">getChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> nodes.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">final</span> Node node <span style="color: #339933;">=</span> nodes.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// use node</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ideally, the code could have been written utilizing Java 5&#8242;s foreach loop.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// iteration happiness which is currently impossible</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Node node <span style="color: #339933;">:</span> document.<span style="color: #006633;">getChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// use node</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As a workaround, I&#8217;ve written a simple wrapper class for org.w3c.dom.NodeList that makes it iterable. Hopefully, it&#8217;s useful for others.</p>
<p>Download: <a href="http://www.trevorpounds.com/blog/wp-content/uploads/2012/03/IterableNodeList.java">IterableNodeList.java</a></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.trevorpounds.dom.util</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.NoSuchElementException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.Node</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.NodeList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Simple org.w3c.dom.NodeList iterable wrapper
 * that can be used with Java's foreach loop.
 *
 * @author Trevor Pounds
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IterableNodeList <span style="color: #000000; font-weight: bold;">implements</span> Iterable<span style="color: #339933;">&lt;</span>Node<span style="color: #339933;">&gt;</span>, Iterator<span style="color: #339933;">&lt;</span>Node<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #008000; font-style: italic; font-weight: bold;">/** List of nodes */</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> NodeList nodeList<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/** List of nodes current index. */</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/** Constructs iterable node list. */</span>
   <span style="color: #000000; font-weight: bold;">public</span> IterableNodeList<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> NodeList nodeList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nodeList</span> <span style="color: #339933;">=</span> nodeList<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/** {@inheritDoc} */</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasNext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> index <span style="color: #339933;">&lt;</span> nodeList.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/** {@inheritDoc} */</span>
   <span style="color: #000000; font-weight: bold;">public</span> Iterator<span style="color: #339933;">&lt;</span>Node<span style="color: #339933;">&gt;</span> iterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/** {@inheritDoc} */</span>
   <span style="color: #000000; font-weight: bold;">public</span> Node next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NoSuchElementException</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>hasNext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NoSuchElementException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> nodeList.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/** {@inheritDoc} */</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> remove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IllegalStateException</span>, <span style="color: #003399;">UnsupportedOperationException</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Utilizing the wrapper class we can rewrite the sad code above to the following.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Node node <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> IterableNodeList<span style="color: #009900;">&#40;</span>document.<span style="color: #006633;">getChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// use node</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=344</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JNI: Emulating Pointer-to-Pointers</title>
		<link>http://www.trevorpounds.com/blog/?p=480</link>
		<comments>http://www.trevorpounds.com/blog/?p=480#comments</comments>
		<pubDate>Mon, 28 Mar 2011 21:15:07 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdk]]></category>
		<category><![CDATA[jni]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=480</guid>
		<description><![CDATA[I recently ran into an instance where I wanted to use several native libraries directly in Java and have the method signature model the original C API as closely as possible, allowing me to store opaque pointer types directly within a Java class. For anyone not familiar, opaque pointers are typically stored within Java classes [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into an instance where I wanted to use several native libraries directly in Java and have the method signature model the original C API as closely as possible, allowing me to store opaque pointer types directly within a Java class. For anyone not familiar, opaque pointers are typically stored within Java classes using the long primitive type which is capable of storing most target platforms native pointer types (i.e. 8 or less bytes). One drawback of storing native pointers in pure Java is a lot of C API calls allocate resources using pointer-to-pointers or double pointers (i.e. <a href="http://www.sqlite.org/c3ref/open.html">sqlite3_open(&#8230;)</a>). Unfortunately, JNI does not have a concept for manipulating pointer-to-pointers or reference types. That said, it is possible to emulate a pointer-to-pointer via JNI by using a primitive array wrapper (i.e. long[]).</p>
<p>For example suppose I wanted to model the following C function that returns a simple pass/fail result and outputs a newly allocated integer using a pointer-to-pointer.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> alloc_opaque_type<span style="color: #009900;">&#40;</span>T<span style="color: #339933;">**</span> ppT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The JNI equivalent would instead substitute the method&#8217;s first argument with a long primitive array.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #000066; font-weight: bold;">int</span> alloc_opaque_type<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> ppT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The native JNI implementation then requires a slight pointer fixup between the Java primitive array type and the native pointer type which is accomplished using a simple union cast below.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">JNIEXPORT jint JNICALL Java_Wrapper_alloc_1opaque_type
   <span style="color: #009900;">&#40;</span>JNIEnv<span style="color: #339933;">*</span> env<span style="color: #339933;">,</span> jclass klass<span style="color: #339933;">,</span> jlongArray ppT<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">union</span>
   <span style="color: #009900;">&#123;</span>
      T<span style="color: #339933;">*</span> p<span style="color: #339933;">;</span>
      jlong    l<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> __u<span style="color: #339933;">;</span>
   <span style="color: #993333;">int</span> retval <span style="color: #339933;">=</span> alloc_opaque_type<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>__u.<span style="color: #202020;">p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   env<span style="color: #339933;">-&gt;</span>SetLongArrayRegion<span style="color: #009900;">&#40;</span>ppT<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>__u.<span style="color: #202020;">l</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> retval<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>After the native code is compiled and loaded, you can then use the method with an opaque pointer as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> allocAndGetPointer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> opaque_ptr_ptr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>alloc_opaque_type<span style="color: #009900;">&#40;</span>opaque_ptr_ptr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not allocate opaque pointer!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=480</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Java Internals: Unavoidable Object Overhead</title>
		<link>http://www.trevorpounds.com/blog/?p=351</link>
		<comments>http://www.trevorpounds.com/blog/?p=351#comments</comments>
		<pubDate>Mon, 27 Sep 2010 19:23:14 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdk]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=351</guid>
		<description><![CDATA[A few months back I completed work on an internal library at AMD to optimize memory usage for reading a large number (tens of millions) of unique records into memory from a compressed proprietary data format. Overall the process contained many new and interesting challenges, most related to reducing the current bloated data representations down [...]]]></description>
			<content:encoded><![CDATA[<p>A few months back I completed work on an internal library at <a href="http://www.amd.com">AMD</a> to optimize memory usage for reading a large number (tens of millions) of unique records into memory from a compressed proprietary data format. Overall the process contained many new and interesting challenges, most related to reducing the current bloated data representations down to something manageable. I won&#8217;t get too much into the details but it boiled down to several strategies: substitution of primitive wrappers (i.e. java.lang.Integer) with the corresponding primitive formats, real-time bytecode generation and object interning. Although, these strategies offered impressive memory compression (> 90% in some cases!) over earlier version of the library, one problem still remained, the unavoidable overhead of java.lang.Object&#8217;s internal header. Unfortunately, the java.lang.Object header overhead is incurred by all Java objects and is tragically unavoidable. Typically, Java VM&#8217;s make use of an internal VM object model header consisting of at least two fields: a klass_type pointer and a lock_type pointer. Additionally, a Java Array object includes an additional field for the length.</p>
<table width="100%">
<tr>
<td style="vertical-align: bottom; padding: 20px;">

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> VMObject
<span style="color: #008000;">&#123;</span>
   klass_type<span style="color: #000040;">*</span> klass<span style="color: #008080;">;</span>
   lock_type<span style="color: #000040;">*</span>  lock<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><center>Typical Internal VM Object Representation</center>
</td>
<td style="vertical-align: bottom; padding: 20px;">

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> VMArray <span style="color: #008080;">:</span> VMObject
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">size_t</span> length<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><center>Typical Internal VM Array Representation</center>
</td>
</tr>
</table>
<p>I won&#8217;t get into too much detail about the klass_type pointer since this one speaks for itself. It&#8217;s primary purpose is to provide all of the runtime type and method dispatch information to the VM. The lock_type is also straight forward providing the internal mechanism for Java&#8217;s synchronized keyword. However, it also happens to double every object&#8217;s overhead whether or not synchronization is used at all.  A good resource that I found touching on the subject is available on the <a href="http://c2.com/cgi/wiki?JavaObjectOverheadIsRidiculous">c2.com wiki</a> which outlines the pitfalls of the Java object model and parallels my own independent discoveries. Unfortunately, Java was never designed for data-only types (i.e. structs in C/C++). As a result, all objects in Java require both fields regardless of synchronization or virtual dispatch. I came to the same conclusion as the authors at <a href="http://c2.com/cgi/wiki?JavaObjectOverheadIsRidiculous">c2.com wiki</a>; the memory overhead can only be reduced in two ways: (1) provide a method to mark objects as unsynchronizable and (2) incorporate a data-only type. The changes are not impossible; in fact both could be retrofitted into future Java releases with a little bit of effort. The addition of an java.lang.concurrent.Unsynchronizable interface would provide a backward compatible way for the Java compiler and VM&#8217;s object model to support a lockless subtype. Supporting a data-only type is a bit trickier but could be achieved again using an updated Java object model in addition to some compile-time and run-time optimizations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=351</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using Multiple Java Annotations</title>
		<link>http://www.trevorpounds.com/blog/?p=314</link>
		<comments>http://www.trevorpounds.com/blog/?p=314#comments</comments>
		<pubDate>Fri, 19 Feb 2010 16:23:16 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdk]]></category>
		<category><![CDATA[junitbench]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=314</guid>
		<description><![CDATA[The other day I was experimenting with some concepts for JUnitBench related to user specified metadata supplied via annotation types. The concept would allow a developer to supply additional column data to the performance measurement output, specifically CSV, which can be used in custom post-processing calculations. The basic interface I wanted to provide for the [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was experimenting with some concepts for <a href="http://www.junitbench.org">JUnitBench</a> related to user specified metadata supplied via annotation types.  The concept would allow a developer to supply additional column data to the performance measurement output, specifically CSV, which can be used in custom post-processing calculations. The basic interface I wanted to provide for the metadata was something along the lines of the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Metadata<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;size (KB)&quot;</span>, value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1024&quot;</span><span style="color: #009900;">&#41;</span>
@Metadata<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;size (MB)&quot;</span>, value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span>
@Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myPerfTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// do stuff</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Unfortunately, multiple annotations are not allowed; this is apparent when you try to compile via javac.</p>
<pre>
./org/junitbench/examples/MetadataTest.java:[23, 18] duplicate annotation
</pre>
<p>Additionally, the java reflection API does not provide a mechanism for retrieving multiple annotations by name (see: <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/AnnotatedElement.html#getAnnotation%28java.lang.Class%29">java.lang.reflect.AnnotatedElement.getAnnotation(&#8230;)</a>). If it was explicitly allowed by the language I could probably scrape by using one of the existing reflection API&#8217;s get*Annotations(&#8230;) methods with a custom annotation filter. However, since it is not allowed in any capacity another workaround was required; I did this by defining an enclosing annotation type that accepted an unlimited number of nested annotation types.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Metadata<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   @Data<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;size (KB)&quot;</span>, value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1024&quot;</span><span style="color: #009900;">&#41;</span>,
   @Data<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;size (MB)&quot;</span>, value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
@Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myPerfTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// do stuff</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above annotation types are defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Inherited
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Target<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> Data
<span style="color: #009900;">&#123;</span>
   <span style="color: #003399;">String</span> name<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">String</span> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Inherited
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Target<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> Metadata
<span style="color: #009900;">&#123;</span>
   Data<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The important thing to be aware of is the use of the nested @Data annotation as the array value type within the enclosing @Metadata annotation definition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=314</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting GCC Versions with SCons</title>
		<link>http://www.trevorpounds.com/blog/?p=130</link>
		<comments>http://www.trevorpounds.com/blog/?p=130#comments</comments>
		<pubDate>Mon, 15 Feb 2010 21:06:07 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c/c++]]></category>
		<category><![CDATA[compilers]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[libstdc++]]></category>
		<category><![CDATA[multilib]]></category>
		<category><![CDATA[SCons]]></category>
		<category><![CDATA[x86-64]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=130</guid>
		<description><![CDATA[A few weeks ago I was tasked to add support for a C++ project&#8217;s build system to detect unknown (i.e. currently unsupported) platforms and compilers. At the time, the project only supported platforms for which we had built pre-compiled binaries, including several GNU/Linux/GCC, Windows/MSVC, and Solaris/GCC targets. The build system is based on SCons, which [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I was tasked to add support for a C++ project&#8217;s build system to detect unknown (i.e. currently unsupported) platforms and compilers.  At the time, the project only supported platforms for which we had built pre-compiled binaries, including several GNU/Linux/GCC, Windows/MSVC, and Solaris/GCC targets. The build system is based on <a href="http://www.scons.org/">SCons</a>, which by extension supports all of the functionality available from the Python language. As a result, I was able to utilize portable Python functionality to supplement the current build system&#8217;s platform/compiler detection scheme. The first step of the detection algorithm involved parsing the results from Python&#8217;s os.uname() and setting several build system variables. Then, the compiler environment is configured by pulling the CXX SCons environment variable, matching it against supported types (e.g. GCC, MSVC, etc.) and performing a compiler dependent version look-up.  A fairly simple approach used to determine the GCC version can be done by adding similar code to a SCons build script.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">env = Environment<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
GCC_VERSION = <span style="color: #dc143c;">commands</span>.<span style="color: black;">getoutput</span><span style="color: black;">&#40;</span>env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'CXX'</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">' -dumpversion'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Note, in the example I opted to use the <em>-dumpversion</em> flag instead of the <em>-v</em> or <em>&#8211;version</em> flags because from my experience I have found the results can be inconsistent, from a parsing aspect, depending on the GCC build being used (i.e. differences in vanilla and GNU/Linux distribution patched builds).  After detecting the version, the results are used to configure the appropriate compile time and link flags (e.g. -O1 vs. -O2, -m32 vs. -m64, etc.).  The version is also used to determine which run-time dependencies (i.e. libstdc++.so, libgcc_s.so) need to be linked and distributed with the application. Fortunately, a convenient mechanism exists, the <em>-print-file-name</em> option, which can be used to determine GCC&#8217;s library awareness.  For example, the option can be used to resolve the libstdc++.so and libgcc+_s.so dependencies as follows.</p>
<pre>
g++-4.1 -print-file-name=libgcc_s.so.1
/usr/lib/gcc/x86_64-linux-gnu/4.1.3/../../../../lib/libgcc_s.so.1

g++-4.1 -m32 -print-file-name=libgcc_s.so.1
/usr/lib/gcc/x86_64-linux-gnu/4.1.3/../../../../lib32/libgcc_s.so.1

g++-4.1 -print-file-name=libstdc++.so.6
/usr/lib/gcc/x86_64-linux-gnu/4.1.3/../../../../lib/libstdc++.so.6

g++-4.1 -m32 -print-file-name=libstdc++.so.6
/usr/lib/gcc/x86_64-linux-gnu/4.1.3/../../../../lib32/libstdc++.so.6
</pre>
<p>Additionally, the <em>-print-file-name</em> option works in conjunction with the <em>-m32</em> or <em>-m64</em> flags. This allows the build system, depending on the bit-ness of the target system, to resolve the absolute path of the respective i386 or x86-64 compatible libraries. However, I did find that the <em>-print-file-name</em> option in GCC versions <= 3.2.1 seemed to resolve to invalid paths in some circumstances.  For those particular compilers another mechanism would need to be used.</p>
<p>As an addendum, I've compiled a quick cross-reference of various GCC features which I acquired from some of my previous <a href="http://www.trevorpounds.com/blog/?p=111">work</a> as well as utilizing some of the compiler options above.</p>
<style type="text/css">
tr.d1 td, th {
   color: black;
   padding: 3px 3px;
   text-align: center;
}
tr.d0 td, th {
   color: black;
   background-color: #E4E4E4;
   padding: 3px 3px;
   text-align: center;
}
</style>
<p><center></p>
<table border="1">
<tr class="d0">
<th>GCC</th>
<th>libstdc++</th>
<th>multilib (i386/x86-64)</th>
<th>-dumpversion</th>
<th>-print-file-name</th>
</tr>
<tr class="d1">
<td>2.95.x</td>
<td>3?</td>
<td>no</td>
<td>works</td>
<td>not tested</td>
</tr>
<tr class="d0">
<td>3.0.x</td>
<td>3</td>
<td>no</td>
<td>works</td>
<td>not tested</td>
</tr>
<tr class="d1">
<td>3.1.x</td>
<td>4</td>
<td>yes *</td>
<td>works</td>
<td>not tested</td>
</tr>
<tr class="d0">
<td>3.2.x &#8211; 3.3.x</td>
<td>5</td>
<td>yes *</td>
<td>works</td>
<td>works **</td>
</tr>
<tr class="d1">
<td>3.4.x &#8211; 4.4.x</td>
<td>6</td>
<td>yes</td>
<td>works</td>
<td>works</td>
</tr>
</table>
<p></center></p>
<p>* Technically i386/x86-64 multilib support is available in these versions but does not seem to be as widley supported. See the notes in my <a href="http://www.trevorpounds.com/blog/?p=111">previous post</a> for more info.</p>
<p>** Does not work in all cases. In my limited amount of testing I found that GCC 3.2.1 and earlier had trouble resolving libstdc++.so shipped with the compiler. I would test this option thoroughly if it needs to be used with older compilers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=130</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Older GCC Versions on x86-64 Debian/Ubuntu</title>
		<link>http://www.trevorpounds.com/blog/?p=111</link>
		<comments>http://www.trevorpounds.com/blog/?p=111#comments</comments>
		<pubDate>Sat, 23 Jan 2010 23:40:18 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c/c++]]></category>
		<category><![CDATA[compilers]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[GNU]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[multilib]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[x86-64]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=111</guid>
		<description><![CDATA[Some time ago, while working on MockitoPP, I was interested to see what compilers were compatible with several non-standard compiler dependent techniques (e.g. vtable, data/code pointer sizes) in use. Early on I focused most of my attention on supporting the newer, more popular GCC (i.e. 4.x) and Visual Studio (e.g. VS .Net 2003) compilers. After [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, while working on <a href="http://code.google.com/p/mockitopp/">MockitoPP</a>, I was interested to see what compilers were compatible with several non-standard compiler dependent techniques (e.g. vtable, data/code pointer sizes) in use.  Early on I focused most of my attention on supporting the newer, more popular GCC (i.e. 4.x) and Visual Studio (e.g. VS .Net 2003) compilers. After I hardened the code base a bit I decided to see how well it faired with older versions of GCC. For the purposes of my experiment I decided to test against the latest GCC release from each major series (i.e. 2.95.x to 4.4.x). The build and target system I used for testing was Ubuntu 9.10 on an x86-64 architecture. The main features I wanted were the standard C/C++ languages and, if available, multilib (e.g. i386/x86-64) support. Unfortunately, building an older compiler using a newer tool-chain is not a trivial task in many circumstances; normally a new compiler is compiled using an older tool-chain! One obvious problem is that GCC compiler features have changed over the years which has complicated over time with the advent of more diverse Linux distos. It gets even worse when trying to build a usable 32-bit host/target compiler on a system with a 64-bit tool-chain (see: GCC 2.95.x and 3.0.x below). Additionally, older versions of GCC have limited multilb awareness support on Linux distributions that do not use a &#8220;standard&#8221; directory structure. For example, in most LSB 64-bit distributions the following lib path convention is used:</p>
<p><code>/lib</code> &#8211; 32-bit libraries<br />
<code>/lib64</code> &#8211; 64-bit libraries</p>
<p>However, on Debian based systems the following convention is used:</p>
<p><code>/lib</code> &#8211; 64-bit libraries<br />
<code>/lib64</code> &#8211; symlink to /lib<br />
<code>/lib32</code> &#8211; 32-bit libraries</p>
<p>Unfortunately, this convention confuses the older GCC configuration scripts. However, this limitation has been <a href="http://gcc.gnu.org/ml/gcc-patches/2008-03/msg00144.html">resolved</a> in later releases.</p>
<p><strong>GCC >= 4.2.4</strong><br />
To start off, I&#8217;ll talk about building modern GCC versions. From my limited testing I found that any version starting with 4.2.4 and later can be built by following the standard GCC build procedure outlined on the <a href="http://gcc.gnu.org/install/index.html">web</a>. However, for documentation purposes, I&#8217;ve listed the commands I used to build and install GCC 4.4.2 to a custom installation prefix (e.g. /opt).</p>
<pre>
tar xzf gcc-4.4.2.tar.gz
mkdir gcc-4.4.2-objdir
cd gcc-4.4.2-objdir
../gcc-4.4.2/configure --prefix=/opt/x86_64/gcc/gcc-4.4.2 --enable-languages=c,c++
make
make install
</pre>
<p class="info">
multilib support is auto-enabled using the above configure options but it can also be explicitly enabled by adding <em>&#8211;enable-multilib</em> to the argument list.</p>
<p><strong>GCC 3.3.x to 4.2.3</strong><br />
Building versions 3.3.x to 4.2.3 is a little more involved but essentially boils down to applying a slightly modified version of the lib path awareness patch mentioned above. For simplicity&#8217;s sake I&#8217;ve attached the patch (<a href='http://www.trevorpounds.com/blog/wp-content/uploads/2010/01/gcc-multilib-fix-v3.3.x-to-v4.2.3.debian.x86_64.diff'>gcc-multilib-fix-v3.3.x-to-v4.2.3.debian.x86_64.diff</a>) and used the following commands to build the compiler.</p>
<pre>
tar xzf gcc-3.4.6.tar.gz
cd gcc-3.4.6
patch -p0 < gcc-multilib-fix-v3.3.x-to-v4.2.3.debian.x86_64.diff
mkdir ../gcc-3.4.6-objdir
cd ../gcc-3.4.6-objdir
../gcc-3.4.6/configure --prefix=/opt/x86_64/gcc/gcc-3.4.6 --enable-languages=c,c++
make
make install
</pre>
<p><strong>GCC 3.2.x</strong><br />
Building GCC 3.2.x requires following the same basic steps as above but requires an additional fix that prevents emitting an .eh_frame for the crt*S.o objects. The original bug fix can be found on the <a href="http://gcc.gnu.org/ml/gcc-patches/2002-11/msg01671.html">GCC mailing list</a>. To build this version of GCC, download the attached patch (<a href='http://www.trevorpounds.com/blog/wp-content/uploads/2010/01/gcc-multilib-fix-v3.2.x.debian.x86_64.diff'>gcc-multilib-fix-v3.2.x.debian.x86_64.diff</a>) and perform the following commands.</p>
<pre>
tar xzf gcc-3.2.3.tar.gz
cd gcc-3.2.3
patch -p0 < gcc-multilib-fix-v3.2.x.debian.x86_64.diff
mkdir ../gcc-3.2.3-objdir
cd ../gcc-3.2.3-objdir
../gcc-3.2.3/configure --prefix=/opt/x86_64/gcc/gcc-3.2.3 --enable-languages=c,c++
make
make install
</pre>
<p class="info">
multilib i386/x86-64 support is available in this version, however, I seem to recall reading on the web that proper x86-64 support did not become production ready until the 3.3.x releases.  At the time of this post I could not find any resource to back this claim. However, I did find the quote, "<em>The x86-64 port has been significantly improved</em>," in the <a href="http://gcc.gnu.org/gcc-3.3/changes.html">3.3.x change log</a> which potentially confirms this. In any case, I compiled a few simple programs for an x86-64 target and none of them had any run-time problems as far as invalid calculations or heinously crashing but if anyone knows for sure I would be interested to know.</p>
<p><strong>GCC 3.1.x</strong><br />
Up until now most of the builds were fairly easy to "frontport" to run on x86-64 Debian. With GCC 3.1.x, I began to run into more difficult problems. Along with the .eh_frame patch used above, this version needed work to fix the lib32/lib64 path lookup mechanisms for multilib support; FWIW, this is the first GCC version that supported x86-64 host/targets. Also, an additional workaround was needed to forward some older libc symbols (e.g. __ctype_*) to their corresponding available equivalents (e.g. __ctype_*_loc).  I've compiled the corresponding fixes into a patch (<a href="http://www.trevorpounds.com/blog/wp-content/uploads/2010/01/gcc-multilib-fix-v3.1.x.debian.x86_64.diff">gcc-multilib-fix-v3.1.x.debian.x86_64.diff</a>) which can be used to build this version.</p>
<pre>
tar xzf gcc-3.1.1.tar.gz
cd gcc-3.1.1
patch -p0 < gcc-multilib-fix-v3.1.x.debian.x86_64.diff
mkdir ../gcc-3.1.1-objdir
cd ../gcc-3.1.1-objdir
../gcc-3.1.1/configure --prefix=/opt/x86_64/gcc/gcc-3.1.1 --enable-languages=c,c++ --enable-threads=posix
make
make install
</pre>
<p class="info">
Without the --enable-threads=posix flag, the configure script will build using the default thread model (e.g single).  If you do not wish to support posix threads the option can be omitted.</p>
<p><strong>GCC 3.0.x</strong><br />
As I mentioned above, the GCC 3.1.x series of compilers were the first to natively support x86-64 targets. In order to build GCC 3.0.x (for an i386 target) on a x86-64 host the source required a change to the GCC standard library prefix directories (e.g. /lib to /lib32) and a few tool-chain workarounds (e.g. -m32 flags). Probably, the most interesting part of the changeset is the addition of <em>--32</em> to the GCC assembler spec options. Without the flag, the assembler chokes with an invalid operand error (e.g. <code>Error: suffix or operands invalid for `push'</code>). Alternatively, I could have specified <em>-Wa,--32</em> on the GCC command line to workaround the tool-chain differences but doing this is more burdensome and error prone than having the default behavior compiled into the spec file. Additionally, the same __ctype_* symbol lookup change in the GCC 3.1.x patch is also required. Again, I've provided the compatibility changes in a patch (<a href="http://www.trevorpounds.com/blog/wp-content/uploads/2010/01/gcc-v3.0.x.debian.x86_64.diff">gcc-v3.0.x.debian.x86_64.diff</a>) which can be built using the following commands.</p>
<pre>
tar xzf gcc-3.0.4.tar.gz
cd gcc-3.0.4
patch -p0 < gcc-v3.0.x.debian.x86_64.diff
mkdir ../gcc-3.0.4-objdir
cd ../gcc-3.0.4-objdir
../gcc-3.0.4/configure --prefix=/opt/i386/gcc/gcc-3.0.4 --enable-languages=c,c++ --enable-threads=posix --host i386-pc-linux-gnu
make
make install
</pre>
<p><strong>GCC 2.95.x</strong><br />
Finally! We come to the GCC 2.95.x series which, rightfully so, required quite a bit of effort to configure and build for an x86_64 host. About half of the changes needed for GCC 3.0.x are also applicable here (e.g. Makefile fixes, lib32 fix-ups, spec flags, etc). Unfortunately, the other half are workarounds for compiler and glibc nuances that are fixed in later releases. One of the big changes needed is a workaround for the internal use of an <em>_IO_MTSAFE_IO</em> define.  This define causes an error due to a missing header due to difference in glibc headers included with modern Linux distributions (see <a href="http://lists.diy-linux.org/pipermail/diy-linux-dev/2006-January/000710.html">here</a> and <a href="https://bugzilla.redhat.com/show_bug.cgi?id=162634">here</a> for more info). Luckily, a straightforward workaround is to use glibc's generic stdio-lock.h header, which in my case needed to be pulled from the glibc 2.10.1 source to match my installed version. Additionally, another glibc fix is required due to an unnamed union declaration (see this <a href="http://bugs.debian.org/475839">bug</a>) inside a pthread header which is not supported by GCC 2.95.x. To workaround the issue I pulled in the glibc 2.10.1 system dependent pthreadtypes.h header from the source and appended a variable name (e.g. __gcc_295_workaround__) to the unnamed union declaration.  Build this version using the provided patch (<a href="http://www.trevorpounds.com/blog/wp-content/uploads/2010/01/gcc-v2.95.x.debian.x86_64.diff">gcc-v2.95.x.debian.x86_64.diff</a>) and the following instructions.</p>
<pre>
tar xzf glibc-2.10.1.tar.gz
mkdir -p gcc-2.95.3/glibc-workaround/include/bits
cp glibc-2.10.1/bits/stdio-lock.h gcc-2.95.3/glibc-workaround/include/bits
cp glibc-2.10.1/nptl/sysdeps/unix/sysv/linux/x86_64/bits/pthreadtypes.h gcc-2.95.3/glibc-workaround/include/bits
sed -i -n '1h;1!H;${;g;s/\(__pthread_slist_t __list;\n[ \t]*}\)/\1 __gcc_295_workaround__/g;p;}' gcc-2.95.3/glibc-workaround/include/bits/pthreadtypes.h
tar xzf gcc-2.95.3.tar.gz
cd gcc-2.95.3
patch -p0 < gcc-v2.95.x.debian.x86_64.diff
mkdir ../gcc-2.95.3-objdir
cd ../gcc-2.95.3-objdir
../gcc-2.95.3/configure --prefix=/opt/i386/gcc/gcc-2.95.3 --enable-languages=c,c++ --enable-threads=posix --enable-shared --host i386-pc-linux-gnu
make
make install
</pre>
<p class="info">
Be sure to use the same version of the glibc source for the patches as the one of the target system. Since my target/host system uses glibc 2.10.1 I made sure to pull the corresponding source.</p>
<p><strong>Additional Notes</strong><br />
I used GCC 3.4.6 in all cases when building the GCC 2.95.x and 3.x.x versions. The host compiler can be configured by setting the CC environment variable to the full path of your compiler (e.g. CC=/opt/x86_64/gcc/gcc-3.4.6/bin/gcc).  All of the GCC 4.x.x versions can pretty much be built with any of the older GCC compilers. However, your mileage may vary in some circumstances. In those cases you can always try to build with a different host compiler when the current one is not working out. What seemed to work best for me was building each subsequent version in descending order and bootstrapping a failed build using an older GCC host if necessary.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=111</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>Linking to Older Versioned Symbols (glibc)</title>
		<link>http://www.trevorpounds.com/blog/?p=103</link>
		<comments>http://www.trevorpounds.com/blog/?p=103#comments</comments>
		<pubDate>Tue, 20 Oct 2009 03:51:55 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c/c++]]></category>
		<category><![CDATA[compilers]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[GNU]]></category>
		<category><![CDATA[linkers]]></category>
		<category><![CDATA[symbol versioning]]></category>
		<category><![CDATA[symver]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=103</guid>
		<description><![CDATA[In my last post I gave a brief overview of the mechanism used internally by glibc for versioning symbols within shared libraries. As an addendum to my previous article I would like to discuss a simple way to force linking against older glibc symbols. Why would you do this you may ask? Well, suppose you [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.trevorpounds.com/blog/?p=33">last post</a> I gave a brief overview of the mechanism used internally by glibc for versioning symbols within shared libraries.  As an addendum to my previous article I would like to discuss a simple way to force linking against older glibc symbols.  Why would you do this you may ask?  Well, suppose you have several GNU/Linux systems with varying glibc installs across them but you want to deliver a binary that would be compatible across them. One option is to statically link your binary, my vote <img src='http://www.trevorpounds.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , the other is to link to an older symbol within the shared library. I would like to mention that using an older symbol has the obvious drawback, possible advantage, of using something that was deprecated for a reason in the past (i.e. broken, behavior changes, performed poorly, new arch support, etc.). The reason I say it is possibly advantageous to link against an older symbol may be because it is known to behave in a desired way, broken or not!  I present the following merely for education purposes, so use with care.</p>
<p>To force linking against a particular symbol you need to use the same <em>.symver</em> pseudo-op that is used for defining versioned symbols in the first place. In the following example I make use of glibc&#8217;s <em>realpath</em>, but want to make sure it is linked against an older 2.2.5 version.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;limits.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
__asm__<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.symver realpath,realpath@GLIBC_2.2.5&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">char</span><span style="color: #339933;">*</span> unresolved <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;/lib64&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #993333;">char</span>  resolved<span style="color: #009900;">&#91;</span>PATH_MAX<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>realpath<span style="color: #009900;">&#40;</span>unresolved<span style="color: #339933;">,</span> resolved<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> resolved<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you were to use <em>objdump</em> on the resulting binary you would see that it is indeed using realpath@GLIBC_2.2.5!  Also note that other symbols have been resolved to their defaults so you need to make sure you add a <em>.symver</em> pseudo-op for each symbol you want to force to an older version.</p>
<pre>
0000000000000000      F *UND*  0000000000000000         realpath@GLIBC_2.2.5
...
0000000000000000      F *UND*  0000000000000000         __stack_chk_fail@@GLIBC_2.4
</pre>
<p>The <em>.symver</em> pseudo-op can be used this way to force any symbol to be linked against an older one so long as it is valid.  To ease linking against older glibc versions I&#8217;ve provided a simple header which can be used to force linking against the minimum glibc version for a give x86 architecture.  The minimum versions I am using were taken from <a href="http://sourceware.org/git/?p=glibc.git;a=blob;f=shlib-versions;h=65e772bbb5ffd631e4f7a83e3581e8c09dd30122;hb=HEAD">shlib-versions</a> file of the glibc git tree.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef __GLIBC_COMPAT_SYMBOL_H__</span>
<span style="color: #339933;">#define __GLIBC_COMPAT_SYMBOL_H__ 1</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * add other architecures below
 */</span>
<span style="color: #339933;">#ifdef __amd64__</span>
   <span style="color: #339933;">#define GLIBC_COMPAT_SYMBOL(FFF) __asm__(&quot;.symver &quot; #FFF &quot;,&quot; #FFF &quot;@GLIBC_2.2.5&quot;);</span>
<span style="color: #339933;">#else</span>
   <span style="color: #339933;">#define GLIBC_COMPAT_SYMBOL(FFF) __asm__(&quot;.symver &quot; #FFF &quot;,&quot; #FFF &quot;@GLIBC_2.0&quot;);</span>
<span style="color: #339933;">#endif /*__amd64__*/</span>
&nbsp;
<span style="color: #339933;">#endif /*__GLIBC_COMPAT_SYMBOL_H__*/</span></pre></div></div>

<p>To use the glibc compatible header with the <em>realpath</em> example above, we merely use the GLIBC_COMPAT_SYMBOL macro with the appropriate symbol:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">GLIBC_COMPAT_SYMBOL<span style="color: #009900;">&#40;</span>realpath<span style="color: #009900;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=103</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Versioning Symbols for Shared Libraries (glibc)</title>
		<link>http://www.trevorpounds.com/blog/?p=33</link>
		<comments>http://www.trevorpounds.com/blog/?p=33#comments</comments>
		<pubDate>Tue, 13 Oct 2009 04:43:31 +0000</pubDate>
		<dc:creator>Trevor Pounds</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c/c++]]></category>
		<category><![CDATA[compilers]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[GNU]]></category>
		<category><![CDATA[linkers]]></category>
		<category><![CDATA[symbol versioning]]></category>
		<category><![CDATA[symver]]></category>

		<guid isPermaLink="false">http://www.trevorpounds.com/blog/?p=33</guid>
		<description><![CDATA[Sometime back I got a request for running a piece of Java software on an older development system (2.4 kernel/2.2 glibc). The software makes use of a nifty little native compiled JVM launcher, Java Service Wrapper, which provides several features related to JVM configuration and life-cycle process management. Unfortunately, this software has a requirement on [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime back I got a request for running a piece of Java software on an older development system (2.4 kernel/2.2 glibc).  The software makes use of a nifty little native compiled JVM launcher, <a href="http://wrapper.tanukisoftware.org/">Java Service Wrapper</a>, which provides several features related to JVM configuration and life-cycle process management.  Unfortunately, this software has a requirement on &gt;= glibc 2.3 which isn&#8217;t supported by that system. Initially this did not surprise me, considering how old it was, but it did spark my curiosity as to how the dynamic loader enforces prerequisite versions on symbols even if an older version exists. Clearly shared library compatibility doesn&#8217;t end at versioning of the soname.</p>
<p>To get a basic idea of what shared libraries and symbols were listed in the wrapper binary I ran the following command:</p>
<pre>objdump -x wrapper-linux-x86-32</pre>
<p>Which gives quite a bit of useful info about the executable:</p>
<pre>...
Dynamic Section:
NEEDED               libm.so.6
NEEDED               libpthread.so.0
NEEDED               libc.so.6
...
Version References:
required from libpthread.so.0:
0x0d696912 0x00 05 GLIBC_2.2
0x0d696911 0x00 04 GLIBC_2.1
0x0d696910 0x00 03 GLIBC_2.0
required from libc.so.6:
0x0d696913 0x00 08 GLIBC_2.3
0x0d696911 0x00 07 GLIBC_2.1
0x0d696912 0x00 06 GLIBC_2.2
0x0d696910 0x00 02 GLIBC_2.0
...
SYMBOL TABLE:
...
00000000    F *UND*  00000167      strchr@@GLIBC_2.0
00000000    F *UND*  00000078      nanosleep@@GLIBC_2.0
...
00000000    F *UND*  00000496      realpath@@GLIBC_2.3
...
</pre>
<p>In the output above I&#8217;ve omitted the uninteresting parts and left the important tidbits for the purposes of this article. <em>(1)</em> We can see that this binary requires the shared libraries: libm.so.6, libpthread.so.0, libc.so.6, <em>(2)</em> it has various glibc version references: 2.0, 2.1, 2.2, 2.3, and <em>(3)</em> has some interesting symbols appended with a unique version suffix.</p>
<p>So what? Well, after grepping through the large output from <em>objdump</em> I found that the only requirement on glibc 2.3 was the <em>realpath</em> function. Hmm&#8230;that&#8217;s interesting, if it wasn&#8217;t for this one function the executable would be binary compatible with the glibc installed on this old 2.4 linux distribution. Also, after browsing through the git tree I found that <em>realpath</em> has been available since at least glibc 2.1 (i.e. <a href="http://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/canonicalize.c;h=889e24d0c00215625317b90dcfbafba4baf5cfd1;hb=b8f558b7ace3a2e5e3234ac24a600cbe230da8d1#l202">stdlib/canonicalize.c@202</a>). So why is the dynamic linker requiring a newer version of glibc even though the function has been around for so long? The answer has to do with the versioned symbol scheme introduced in glibc 2.1 (http://people.redhat.com/drepper/symbol-versioning) which is an &#8220;extension&#8221; of Sun&#8217;s own symbol versioning scheme (http://docs.sun.com/app/docs/doc/817-1984/appendixb-45356?a=view).</p>
<p>As I mentioned above <em>realpath</em> has been around since glibc 2.1, yet this executable requires 2.3.  The mechanism behind this is that the linker can be used to create global versioned symbol aliases to local symbols generated at compiled time.  The internal linker interface provides two mechanisms for defining the aliases and requires the source to be inlined with a simple assembly pseduo-op <em>.symver</em>.</p>
<p><code>.symver actual, alias@version</code></p>
<p><code>.symver actual, alias@@version</code></p>
<p>The single @ op can be used to define any number of versioned symbols with the same base name. The double @@ can only be defined once for a given symbol since it denotes the default version to use. The linker also requires the use of a map file for defining if a symbol is global or local.  A global symbol can be exported, while a local symbol is kept private.  Each entry within the map file should correspond to a given version within the source file&#8217;s <em>.symver</em> definition.</p>
<pre>
VER_1.0 {
   global: alias
   local: *
};
</pre>
<p>To illustrate the full mechanics of the versioned symbol linking mechanism I&#8217;ve provided a better example. Say for example we release a library version 1.0 with a function <em>foo</em>. We could version it by adding the simple assembly to the function definition in the source foo.c:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">__asm__<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.symver foo, foo@@FOO_1.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then we would need to create a map file (foo.map):</p>
<pre>
FOO_1.0 {
   foo;
};
</pre>
<p>After which we would compile our simple shared library.</p>
<pre>gcc -shared -fPIC -Wl,--version-script foo.map foo.c -o libfoo.so.1</pre>
<p>At this point we could distribute the library with the foo.h and let others compile against it to their hearts content. But what happens when we want to release another version of the library with potential behavioral changes that may affect the use of <em>foo</em> in other programs?  We could introduce a new <em>foo</em> with with the updated behavior and still keep the old fucntion around for legacy programs.  This can be done by renaming the original definition of <em>foo</em> to <em>foo_1_0</em> and adding a new definition of <em>foo</em> called <em>foo_1_1</em>, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* old foo */</span>
__asm__<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.symver foo_1_0, foo@FOO_1.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> foo_1_0<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* new foo */</span>
__asm__<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.symver foo_1_1, foo@@FOO_1.1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> foo_1_1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then we update our existing map file (foo.map) to the following:</p>
<pre>
FOO_1.0 {
   foo;
};
FOO_1.1 {
   foo;
} FOO_1.0;
</pre>
<p>After which we would compile our simple shared library again and redistribute to our customers noting the change in behavior. Programs compiled against the old version and new version can now operate concurrently utilizing the updated library.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trevorpounds.com/blog/?feed=rss2&amp;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
