Useful GCC-only trick: warn if return value is unused

December 14th, 2008

So I found this useful thing that you can add to your functions on GCC compilers… basically, you add an attribute to the end of the function definition and GCC will warn anytime that function is called and the return value isn’t used. Ubuntu 8.10 has enabled this for a *ton* of system calls, and its pretty useful (except when you’re just trying to do something without bothering with error checking). Of course if you don’t like -Wall then you won’t like this either…

#ifdef __GNUC__
    #define WARN_IF_UNUSED __attribute__ ((warn_unused_result))
#else
    #define WARN_IF_UNUSED
#endif

int some_function() WARN_IF_UNUSED;

I has a whiteboard!

November 11th, 2008

So I was talking with a co-worker on Friday, and he mentioned that a friend of his has walls in his kitchen that he can write on using dry erase markers. It sounded really cool, but I said that I did not have the space for such a thing. However, I was sitting at my computer on Saturday when I realized that there was this *giant* empty space on my wall… so I started googling and decided to go get some tileboard from Home Depot.

And I found some stuff called “Thrifty White Tile Board”, which is approximately the same thing as a dry erase board, except I guess they take less time to wear out. It was 8ft x 4ft, and fits perfectly in the space where I had room (though, it did not fit in my car.. ). As you can see from the picture below, its worked out well so far!

The giant $12 whiteboard I got from home depot

The giant $12 whiteboard I got from home depot

Note: Yes, I realize the grammar for the title is bad.

C++ template-fu: delete_if

November 9th, 2008

I’ve been doing a lot of work with templates lately, for my R* tree implementation (which is morphing into a R* tree and a B tree library) and some things that I’ve been playing around with for Roadnav, especially in regards to serialization.

Heres a neat snippet that I created:

template <bool>
struct delete_if {
	template <typename T>
	static void del(T t) { delete t; }
};

template <>
struct delete_if
{
	template <typename T>
	static void del(T t) { /* no-op, doesn't delete */ }
};

This uses a partial specialization to do the decision portion, and a static member template to eliminate some of the typing (you could get rid of the member template, and you would end up calling delete_if<type, delete_item>::del ).

So what this is intended for is those cases where you are implementing some generic container, and you don’t know whether the type you have is a pointer or not. You *could* use a normal bool as a parameter:

if (delete_it) delete var;

But primary problem with this is that it won’t compile if the type isn’t a pointer. So a sample of something similar to what I’m using it for is something like this:

template < typename T, bool delete_item >
class test {
public:

	~test()
	{
		// only release the item if the client specifies
		// that they want to
		delete_if<delete_item>::del(val);
	}

private:
	T val;
};

Obviously this is just a really simple example of how you can use this. One could also use boost::is_pointer to detect whether the type is a pointer, and pass *that* to delete_if while using a ‘normal’ variable to determine whether the client wants to have the item be deleted or not. I leave that as an exercise for the reader.

Easy Green PC

October 28th, 2008

My former roommate had the idea a few months ago to build a ‘green’ PC, which would be very low power, easy for anyone to use for things such as email and office applications, and (of course, the clincher) cheap. So he went ahead and did it… it worked out quite well. Its a shiny lil box, actually. The performance isn’t that bad either, and it is in fact power efficient.

Its running Ubuntu on it (of course, its supposed to be cheap!), and he mentioned some plans to research patches and tweaks to make the Linux kernel more power efficient, and include other peoples power-related patches and such in there as well.

Anyways, visit his site, it doesn’t have a whole lot of content there yet but he was planning on adding some more…

Power efficient ‘green’ linux-based PC

Best Election Twist *ever*

October 27th, 2008

Best election twist *ever*:

Wargames III: The Obama Code: The mysterious hacking of Joe the Plumber’s motor vehicle records was America’s only warning of Barack Obama’s secret past as “The0n3,” a founding member of the 1980’s hacker group Legion of Doom. Once in office, Obama reassembles the now-middle-aged cyberpunk gang for “the l33test Cabinet ever,” and new defense secretary “NinjaBoy” promptly declares cyberwar on China, Russia and, oddly, Canada (“I always hated that poser Mafiaboy.”) Cyber Armageddon seems inevitable until rival hackers from MOD change all the White House phone lines into pay phones. (by Threat Level)

R* Tree Implementation for C++

October 4th, 2008

So a few weeks ago I finished preliminary work on an R* Tree implementation in C++. My goal for this implementation is as follows:

  • Simple API – Most existing implementations I’ve seen are far from simple
  • Generic enough to use with most types, with low overhead
  • N dimensions for bounding boxes
  • Easy to integrate and use for Roadnav
  • Take advantage of modern C++ features to get decent performance

This is a header-only implementation of an R tree with an R* index, and makes heavy use of templates, STL, and STL-style functors; but it should work in any relatively modern C++ compiler. To do searching for nodes, this implementation uses the Visitor pattern via functors — there are examples and documentation in RStarVisitor.h.

If you use this, *please* let me know, I’m very interested to see what bugs may come up, and possible performance problems. Unfortunately, I haven’t gotten around to writing any comprehensive tests for it (but given the tests I’ve run so far, I’m reasonably sure the implementation is mostly correct), but I expect that once I get it integrated into Roadnav that this will follow shortly after. Also, integration of the R* tree into Roadnav will make a lot of potential errors pop out. Look for this in the near future.

Additionally, I am thinking about some of the following ideas too:

  • Adding the ability to work with extra-large datasets by saving/loading to/from disk (after all, that is the point of R trees…)
  • Adding a bulk insert function — there is pseudocode already for this, but I haven’t actually written an implementation for this yet. Also, a more efficient bulk delete function
  • Trying to implement it as a Boost intrusive container. I’ve been playing with some Boost stuff lately at work, and theres a lot of neat things there.

I’ll also use this opportunity to put in a plug for Roadnav — if you’re looking to work on an open source project, theres a bunch of (mostly) interesting stuff that I’m working on/planning for Roadnav. What I’m working on right now is totally removing US-specific stuff from Roadnav, and moving to an OpenStreetMap-only implementation. Theres a number of features that are getting thrown away, but I think that the dynamic community that OSM opens up to Roadnav will be well worth it.

Note: I’ve updated this quite a bit (though, remove functionality has been removed and it no longer uses the visitor pattern for data access), and its currently a part of libsdbx. Libsdbx can be found in Roadnav SVN at https://roadnav.svn.sourceforge.net/svnroot/roadnav/libsdbx/trunk/. I’ll write about this soon.. but its pretty neat. It supports R* and modified B+ trees, in memory or on disk. Check it out.

Note II: I’ve pushed the source code to github at https://github.com/virtuald/r-star-tree

Download the sourcecode here

ZDNet hotlinking

September 24th, 2008

So I found this rather amusing, on their story about the “Hunt for the Palin ‘Hacker'”, they were hotlinking an image from Project Gutenberg. You would think that they would know better… guess not.

AP Article Cleverly Disguised as OnStar ad

September 1st, 2008

I generally browse through Google News at least a few times each week, and I came across this article about how people are using OnStar to avoid Hurricane Gustav. After about the first few paragraphs, it quickly turned into an advertisement for OnStar. Great impartial news coverage… but I’ll let you decide:

Drivers rely on OnStar to evade Hurricane Gustav

pscanf: a sscanf replacement for C

August 6th, 2008

In general, I like C. Pointers don’t generally bother me all that much, and its really nice to use for a lot of different things. However, its string handling SUCKS (though, printf is nice). It seems like a lot of the string routines in the standard C library are designed to screw you in the most unexpected ways possible.

So recently I’ve been struggling with processing input from a user on the command line, and I was using sscanf to attempt to do really simple string processing, and it simply wasn’t matching the correct things. After much struggling and coming to the realization that I needed to do something similar to regular expressions… I sat down and wrote the following function, and it works quite well for what I want to do.

What it does do:

  • Match arbitrary strings using a perl regex, and return the matches to you

What it doesn’t do:

  • This is not a drop-in replacement for sscanf: you need to change the format string around and (possibly) your parameters.
  • It will not return anything but a string. Thats all you get. Of course, if you format your regex correctly then you can pretty much be guaranteed that strtol/strtod/etc will work…
  • Give you any kind of comprehensive error reporting. If you want that, then just use the pcre functions directly
  • Cook your meals or do your laundry

This is released under the license contained on http://www.virtualroadside.com/software/, enjoy!

Read the rest of this entry »

The Cathedral and the Bazaar

July 31st, 2008

So my work has a pretty decent library, and I was browsing through it and found The Cathedral and the Bazaar, by Eric S. Raymond. I had heard of it before, but had never gotten around to actually reading it. Seriously, its a wonderful book, and I recommend anyone involved (or thinking of being involved) in open source projects should read it. Definitely wish I had read it a few years ago… has a lot of good insight.