[Nagiosplug-devel] replacing common system calls with macros/inlines?

Andreas Ericsson ae at op5.se
Wed Mar 22 15:09:05 CET 2006


sean finney wrote:
> a thought for consideration:
> 
> currently, there are a large number of places in plugin code
> where we have stuff like
> 
> 	...
> 	foo=strdup(bar);
> 	if(foo==NULL) {
> 		printf("error message");
> 		exit(STATE_UNKNOWN);
> 	}
> 	...
> 
> does it make any sense to introduce some macros to condense
> code a bit?  for example:
> 
> 	static inline char* STRDUP(const char *foo){
> 		char *bar;
> 		bar=strdup(foo);
> 		if(bar==NULL){
> 			printf("error message");
> 			exit(STATE_UNKNOWN);
> 		}
> 	}
> 
> and then everywhere in our plugin code we could replace the strdup()+error
> detection calls with STRDUP(), and save ourselves some lines of code in
> many places (and introduce missing/needed error checking in others).
> 
> likewise, a similar approach could be taken for malloc, fopen, and
> probably others...
> 

The most common way of doing this is to prefix the wrapper function name 
with a single x and putting them in a header file (usually mm.h) where 
they are declared static and inline.

Uppercasing is usually reserved for macros that optimize away some 
function calls or are otherwise, wrt errors, indistinguishable from the 
actual function call they mimic, like so:

#define STRDUP(str) str ? strdup(str) : str
#define STRCMP(s1, s2) *s1 == *s2 ? strcmp(s1, s2) : *s1 - *s2

Doing things differently will almost certainly confuse people.

-- 
Andreas Ericsson                   andreas.ericsson at op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231





More information about the Devel mailing list