Tumblelog 110216

This is likely to be the last of my tumblelog entries as I recently made the move to microblogging using twitter. This blog will of course remain and I will go on producing classic blog entries. If you have appreciated my tumblelog please follow dholmcom on Twitter.

TAU – Tuning and Analysis Utilities, are a set of tools for profiling and tracing applications written in C/C++, Java, Fortran or Python.

Intel Performance Tuning Utility, yet another tool for profiling code specifically targeting the x86, x86-64 and ia64 architectures based on Intel VTune.

Open Source License Comparison

GHDL, is an open source VHDL simulator.

ROCCC, provides an open source C to VHDL compiler which integrates with Eclipse.

John Kent’s VHDL FPGA Projects, has several good links on FPGAs and is a good starting place if you want to learn more about this particular subject.

Genode, an operating system framework based on the L4 microkernel architecture capable of running a plethora of kernels.

Programming from the Ground Up, is a nice little free book detailing all the basics of programming. Highly recommended for any aspiring software engineer.

Ghosts of Unix Past: a historical search for design patterns, a historical walk through memory design patterns lane.

Introduction to OpenCL, seeing how OpenCL has already picked up a lot of traction already this introduction might be posted a bit late but to the uninitiated it might prove useful.

Throwing Destructors, throwing from a C++ destructor is generally thought of as a big no-no and this article discusses all the issues related to it and suggests a couple of solutions. An interesting read!

Tumblelog 101220

The Dave Haynie Archives, where he has collected old Commodore documentation on various kinds of Amiga hardware.

Gprof2Dot, is a tool that produces call graph profiles based on data from a plethora of profiling tools.

The Maturity Climb by Virus Comix

A Bit of Physics Humour, or how to measure the height of a tall building by using a barometer.

Linux Compose Key, learn how to effectively write characters that aren’t available on most standard keyboards.

SCIgen – An Automatic CS Paper Generator, generate computer science papers on-the-fly. Would be cooler if you could actually control the subject, nevertheless it’s still an interesting concept.

The V4Z80P, a Z80 laptop. Mucho geeky!

google-perftools, provides profiling tools for multi-threaded C++ applications.

Tumblelog 101206

A Guide to Debouncing, an indepth guide on contact bouncing in electronics and different solutions to the problem.

10 Tools To Add Some Spice To Your UNIX Shell Scripts, some useful shell scripting tips like being able to trigger notifications in the desktop environment.

Notice by David Shrigley

Once, Weakly, advanced C++ topics which was once posted weekly.

Silhouette: The Story, of this highly mysterious almost perfect SNES emulator (during a time when the competition was still struggling to run homebrew).

Open64, is a open source compiler originally from SGI based on their MIPSPro compiler.

SGI Developer Books, an archive of different books on subjects relating to developing for different SGI technologies.

Mechanical Keyboard Guide, this is a guide to all the different kinds of computer keyboards that you will find including details on actuation, materials and virtually anything else that goes into defining the quality of the device.

UML Graphical Notation Overview and Reference, provides an excellent online reference for the various UML diagram standards.

Practical File System Design, is a freely available book by one of the developers of the Be File System (BeFS) and explains everything you ever wanted to know about file systems.

Dirty Coding Tricks, pulled off by various desperate game programmers on the virge of product release.

Tumblelog 101122

Data Structures and Algorithms with Object-Oriented Design Patterns in Python, learn algorithms and ADTs in Python by reading this online book.

C++ Templates: The Complete Guide, like the title says it is a complete guide to C++ templates.

Flying Bridge

Dynamic Re-compilation of Binary RISC Code for CISC Architectures, this is the thesis of the venerable Michael Steil on dynamic binary translation.

x86 Instruction Set Reference, 80386 Programmer’s Reference Manual and the X86 Opcode and Instruction Reference, these references are not as cumbersome to open as the offical Intel PDFs.

CUDA, Supercomputing for the Masses: Part 1, a very long running series of articles on nVidia CUDA by Rob Farber.

A Brief Introduction to Rvalue References, C++0x introduces rvalue references and this is an explanation of how they work and why we need to extend references.

OpenFst Library, build finite state transducers using C++ templates.

Hg Init: a Mercurial Tutorial by Joel Spolsky, as he has become a born again DVCS proponent Joel put together this awesome tutorial on mercurial.

PoCC: the Polyhedral Compiler Collection, is a compiler collection implementing the polyhedral model.

Tumblelog 101108

Wonders of Math – The Game of Life, learn the basic rules of the game of life.

How to Write an Operating System, a simple and basic introduction to writing an operating system kernel.

Motherboard City

Miniature Art on the Tip of Pencil by Dalton Ghetti, amazing art sculpted on the tips of pencils.

Rare IBM M15 split ergonomic keyboard hits eBay, bidding war pushes it over $1600, this is one piece of computer history that landed on eBay. A buckling spring, ergonomic and foldable IBM keyboard.

Einstein for Everyone, if you want to understand Einstein’s work but feel you do not know the basics of the physics supporting it.

A Git User’s Guide to Mercurial Queues, understand how Mercurial queues work and how they compare to git.

Homebrew Cray-1A, a miniature fully working Cray-1A implemented in an FPGA.

A Dynamically Recompiling ARM Emulator, is a very nice project report on how to implement a binary translator for running ARM code on an x86.

InteLib, will enable you to code Lisp S-expressions in C++.

Tumblelog 101025

CLiki, the definitive Common Lisp wiki.

Some AI Koans, from the MIT AI Lab.

Monster in the closet by EPalacios

Royal Precision Electronic Computer LGP-30 Programming Manual, the very same machine mastered by Mel, the worlds greatest programmer.

zzuf, is a deterministic input fuzzer which can be used as a tool to discover bugs in applications.

www.Visual6502.org, provides a visual simulation of a running MOS 6502 at the transistor level using HTML5.

Programming in Emacs Lisp, a complete guide to getting started with programming in Emacs Lisp.

Fuel Injector information & Specifications specs, a comprehensive guide to different types of fuel injectors and their pros and cons.

C++ Exceptions: Pros and Cons, provides an in-depth analysis of the use of exceptions in C++ and why or why not you should use them in your project.

Expressive C++: Why Template Errors Suck and What You Can Do About It, presents several valid techniques for improving error reporting when using templates.

Tumblelog 101011

Autodependencies with GNU make, leverage the GCC dependency calculation feature to automatically provide you with dependency chains for your makefiles.

Signals and Systems, a Wikibook on engineering signals and systems. This is a good starting place if you, for instance, want to start out in the DSP world.

The World of Programming by Adit Gupta

American and British english differences, understand the differences in these two languages before deciding which one is more suitable for your writing style.

SSE Performance Programming, guides you through the intricacies of SSE programming and the differences between it and AltiVec.

Data alignment: Straighten up and fly right, teaches why and how to properly align data in memory.

Labor of Division (Episode I), division isn’t always as simple for the CPU as one might expect.

Prex, a portable realtime microkernel for embedded systems released under a BSD license.

Stockfish Chess Engine, a very powerful open source chess engine.

Gambit Scheme, a portable implementation of Scheme which allows you to compile Scheme into C-code.

The Computer History Simulation Project, provides a simulator capable of simulation such machines as the DEC PDP-10, IBM System 3 and MITS Altair 3000 among others.

Binding C++ to Lua, Part 3; Pushing C++ Objects to Lua

Part 3 of my Lua C++ binding series has been 95% ready for several months now and I finally got around to finished the last five percent.

In this part I will add code which enables us to return a class instance usable from Lua as a value from a method or function call. By calling the static member push we can put objects onto Lua’s stack (see section 3.1 – The Stack in the Lua reference manual).

First we need to rewrite our garbage collection code so that it is able to differentiate between objects that are managed by Lua and objects that are deleted elsewhere. We will control this by adding a new field to our class instance metatable called “unmanaged“. If the metatable has the unmanaged field we will not delete the C++ object instance when Lua is executing its garbage collector.

static int gcT(lua_State* l) {
    if (luaL_getmetafield(l, 1, "unmanaged")) {
        lua_pushvalue(l, 1);
        lua_gettable(l, -2);
        if (!lua_isnil(l, -1)) {
            return 0;

    Userdata* ud = reinterpret_cast(lua_touserdata(l, 1));
    T* obj = ud->pT;
    delete obj;
    return 0;

The allocUserdata allocates room for a light userdata item in the specified table of the specified size and returns a pointer to the data region. Light userdata is a pointer to a C object stored in Lua and we use it to store the pointer to our object instance.

static Userdata* allocUserdata(lua_State* l, Index table, void* key, std::size_t size) {
    Userdata* userdata = NULL;
    lua_pushlightuserdata(l, key);
    lua_gettable(l, table);
    if (lua_isnil(l, -1)) {
        lua_pop(l, 1);
        lua_checkstack(l, 3);
        userdata = reinterpret_cast<Userdata*>(lua_newuserdata(l, size));
        lua_pushlightuserdata(l, key);
        lua_pushvalue(l, -2);
        lua_settable(l, -4);
    return userdata;

Weak tables are tables who reference data that is not to be garbace collected by Lua and they are explained in section 2.10.2 of the reference manual. The mode of the weak table defines whether the key, value or both are to be considered weak and protected from collection.

static void weaktable(lua_State* l, const char* mode) {
    lua_pushvalue(l, -1);
    lua_setmetatable(l, -2);
    lua_pushliteral(l, "__mode");

The mode field is a string literal whose values are defined in section 2.10.2 of the Lua reference manual.

    lua_pushstring(l, mode);
    lua_settable(l, -3);

A subtable to the metatable of our object is used to store the mode setting for garbage collection.

static void subtable(lua_State* l, Index metatable, const char* name, const char* mode) {
    lua_pushstring(l, name);
    lua_gettable(l, metatable);
    if (lua_isnil(l, -1)) {
        lua_pop(l, 1);

If the table hasn’t been defined we create a new one otherwise we just reuse the old one.

        lua_checkstack(l, 3);
        weaktable(l, mode);

Three spaces are allocated on the stack and we put a weak table with the specified mode parameter on the first position.

        lua_pushstring(l, name);
        lua_pushvalue(l, -2);
        lua_settable(l, metatable);

Finally we set metatable[name] = weaktable so that our garbage collection setting is associated with the object instance.


Now we have the necessary methods we need so let’s move on to the meat of it all, the push method.

static Index push(lua_State* l, T* instance, bool gc = false) {
    if (!instance) {
        return 0;

First we add a safety measure so that if a NULL instance is pushed we put a nil on Lua’s stack.

    luaL_getmetatable(l, T::s_lunaClassName);
    if (lua_isnil(l, -1)) {
        luaL_error(l, "[Luna::%s] Class %s has not been commited!", __func__, T::s_lunaClassName);
        return 0;
    Index metatable = lua_gettop(l);

Next we attempt to locate the metatable (the implementation) for the class type that we are going to push. If that particular class hasn’t been registered with Lua we raise an error.

    subtable(l, metatable, "userdata", "v");

Since we now have the metatable defining the class API we need to create a subtable to hold the user data (the instance pointer) which we can put on the stack and that makes sense to Lua.

    Userdata* userdata = allocUserdata(l, metatable, instance, sizeof(Userdata));

allocUserdata allocates an entry in metatable[instance] to hold a userdata (see 2.2 – Values and Types) instance for us to store the object instance pointer in.

Now we move on to the final part of the process which is to associate the object instance table with the class definition table and set the appropriate memory management type.

    if (userdata) {
        userdata->pT = instance;
        lua_pushvalue(l, metatable);
        lua_setmetatable(l, -2);

Here we push the class definition (var: metatable) onto the stack and associate the element at stack[-2] (which happens to be our userdata subtable) with it.

        if (!gc) {
            lua_checkstack(l, 3);
            subtable(l, metatable, "unmanaged", "k");
            lua_pushvalue(l, -2)
            lua_pushboolean(l, 1);
            lua_settable(l, -3);
            lua_pop(l, 1);

If we push the object instance with garbage collection disabled we have to add the “unmanaged” subtable that we talked about when we implemented the new garbage collector which acts as a tag to inform it that the object instance should not be automatically garbage collected (i.e. someone has to call delete on it from C++).

    lua_replace(l, metatable);
    lua_settop(l, metatable);
    return metatable;

Finally we replace the old metatable with our modified one containing the object instance, reset the stack and then returns the index of our metatable so that Lua knows where we put the object instance.