[OpenSER-Devel] Request for comment: module interface extension
Dan Pascu
dan at ag-projects.com
Tue Dec 18 04:26:00 UTC 2007
On Monday 17 December 2007, Henning Westerholt wrote:
> If we go with the combination, i would suggest using only one fixed
> parameter, as most modules functions are defined like this at the
> moment.
Why would I want to do that? That way I'm forced to use varargs and
complicate my code. I'm only suggesting to use the ellipsis notation so
that we do not force module function to take 10 arguments, but only the
arguments they need. varargs would only be needed if one wants to
implement a function with multiple arguments without having to actually
write multiple functions. But if he wants to write multiple functions to
handle different function signatures then it works exactly like it does
now.
> Then we have a overhead of 5-10 x 32 bits on the stack for each
> function call, because the space for each parameter is reserved, even
> its NULL. This is the way this mechanism work, unfortunally.
I hardly see an issue with this.
> Ok, then the module function implementation would look like this:
>
> function(struct sip_msg *msg, ...){
> va_list ap;
> va_start(ap, msg);
> char * param, op_param1, op_param2;
>
> param = va_arg(ap, char*);
> if(param)
> op_param1 = param;
> param = va_arg(ap, char*);
> if(param)
> op_param2 = param;
>
> va_end(ap);
>
> // do some work with optional params
> bla(param1);
> blub(param2);
> // do some other work
> }
>
It's not. Why do you twist my idea and make it more complicated than
necessary? Why would I not define a mandatory argument and then use
varargs to complicate my code?
>
> The module function implementation according to our proposal would be:
>
> function(struct sip_msg *msg, unsigned int count, char** params){
> // do some work with optional params
> if (count > 0)
> bla(params[0]);
> if (count > 1)
> blub(params[1]);
> // do some other work
> }
>
> I think this option is much more cleaner and understandable.
Here is a function receiving no arguments:
function(struct sip_msg *msg, ...) {
// do something related to the current msg
}
And here is what a function receiving a char* uri and an int flags would
look like:
function(struct sip_msg *msg, char *uri, char* fl, ...) {
int flags = (int)(long)fl;
// do something with uri according to flags
//
}
So how is the array thing simpler than this? This is just a way to be able
to receive more arguments than 2 without having to force all module
functions to accept 10 arguments while still using the same mechanism we
already have. Only the script parser method of calling the functions has
to change (by passing more than 2 arguments) and by adding the ellipsis
to all module exported functions (we can even get away without the latter
by using typecasting so we don't even have to use the ellipsis notation
and varargs at all, unless we want to have a single function handle
multiple signatures).
--
Dan
More information about the Devel
mailing list