Mar 24, 2010

Robot Touchscreen Analysis

A bit of cross-posting...

Guys from the MOTO made a re-test of Touchscreen Analysis and this time using a robot ;)
Guess who won the latest tests?

But of course.... iPhone wins again.

Mar 15, 2010

News on C++0x

There are some results from the latest C++ committee meeting in Pittsburg.

  1. The final draft was excepted and it looks like the final standard will come in 2011. ;)
  2. Export templates were thrown out of the language :( EDG - was the only who really implemented it and they proposed to remove this feature.
  3. ..EDG reports that their experience in the field has been that nearly no one actually uses the feature, and that it would be right (and okay with EDG) to deprecate or remove it...

  4. Exception specifications has been deprecated. Finally... :D Now also a new keyword "noexcept" is added. Which will remove run-time overhead of exception specifications and will let compiler to better optimize the code in compile time.

See for more details a Trip Report: March 2010 ISO C++ Standards Meeting, from THE Guru Herb Sutter.

Mar 3, 2010

malloc/free interception on Mac OS X

There is a very easy way to hook malloc/free calls on Linux and on other systems, which support the GNU C library. Simply because the GNU C library lets you modify the behavior of malloc, realloc, and free by specifying appropriate hook functions.

Now, how about hooking malloc/free calls on Mac OS X?

Actually it appears to be also not a difficult task in some terms.
The very simple way of changing the behavior of malloc, calloc, realloc, and free routines is to implement your own zone (you have to be very careful with this one) or one can override calls of the default zone, which is automatically created for each process.
Let's try the second one.

We assume, for example, that we want to track all allocations and deallocations in a given application on Mac OS X.
The first step to achieve our goal consists of asking for our default zone structure. The following statement is doing that:
malloc_zone_t *zone = malloc_default_zone();
Don't forget to check for errors, and check that you got your zone.
The next step is to save default zone's callbacks to some tmp storage. You can either save this zone to a static malloc_zone_t variable (save the entire structure):
static malloc_zone_t original_zone;
original_zone = *zone;

or you can save each callback, you need to override, to a tmp variables:

system_malloc = zone->malloc;
system_free = zone->free;

the same you can do for calloc and realloc and company...

The final step is to override default hooks with custom ones:

zone->malloc = &my_malloc;
zone->free = &my_free;

which could look like the following:

void *(*system_malloc)(malloc_zone_t *zone, size_t size);
void (*system_free)(malloc_zone_t *zone, void *ptr);
void my_free(malloc_zone_t *zone, void *ptr)
malloc_printf("free(zone=%p, ptr=%p)\n", zone, ptr);
system_free(zone, ptr); // or if you save the whole zone: (*, ptr);
and so on...

In my_free, my_malloc and my_whatever, we can call some of our custom code to track allocations/deallocations and (if needed) we call the default function accordingly to let default zone do its job.

Snow Leopard
There is on thing you should know if you want to run your malloc hook on the Snow Leopard. According to Apple's C library malloc.c, implementing free hook is not enough to handle all frees.
Check the implementation of the free function from the library:

free(void *ptr) {
malloc_zone_t *zone;
size_t size;
if (!ptr)
zone = find_registered_zone(ptr, &size);
if (!zone) {
malloc_printf("*** error for object %p: pointer being freed was not allocated\n"
"*** set a breakpoint in malloc_error_break to debug\n", ptr);
} else if (zone->version >= 6 && zone->free_definite_size)
malloc_zone_free_definite_size(zone, ptr, size);
malloc_zone_free(zone, ptr);

As you can see, one should also override/hook a newly added call for malloc_zone_free_definite_size:

system_free_definite_size = zone->free_definite_size;
zone->free_definite_size = my_free_definite_size;

I would also use a condition before using free_definite_size. I prefer cmake, I therefore have something like that in my CMakeLists.txt:

# Check that malloc_zone_t has the free_definite_size memeber
void main () {
malloc_zone_t zone;
zone.free_definite_size = NULL;

and in the source code:

if (zone->version >= 6 && zone->free_definite_size)
system_free_definite_size = zone->free_definite_size;
zone->free_definite_size = my_free_definite_size;

That's It.
This technique works on Mac OS X 10.5-10.6. Enjoy!