views:

102

answers:

1

I have a source code of a library which has a lot of strange IF, ELSE, FOR, etc. macros for all common C-keywords instead of using just usual if,else,for,while keywords. These macros are defined like this:

 #define IF( a) if( increment_if(), a)

where increment_if() function is defined so:

static __inline void increment_if( void) {
    // If the "IF" operator comes just after an "ELSE", its counter
    // must not be incremented.
    ... //implementation
}

I don't really understand, what is the purpose of such macros? This library is for a real-time application and I suppose that using such macros must slow-down an application.

+10  A: 

Those macros will have two versions, one that is just the plain if statement, and one that counts the number of executions of that statement. The reason for doing this is to make the profiling statistics. If you count the number of executions of each code block, you can account for the amount of time each takes.

In a realtime application, it is far more important that the timing of each operation is predictable so you can calculate if the application is meeting its deadlines. It's not enough to merely be fast, in fact, so long as the deadlines are met, that is all that is required.

Andrew McGregor