Consider the following situation in a C program. We have a set of functions, let the generic one be
void needs_param (void * param, ...);
, which depend on the value of some parameter param
. The argument param
is computed by a function, e.g.
void compute_param (..., void * param);
We would like to hide the details of the param
(type, size, value, ...) to a caller as much as possible, where the caller is a function that makes use of the functions compute_param
and needs_param
. How to implement this most efficiently and thread-safe, if you are free to modify both needs_param
and compute_param
?
I have considered and excluded the following possibilities:
a: Because we are free to modify needs_param
, one way to do that would be to pass gen_param
and its arguments instead of the param
, and let the needs_param
compute param
by itself. But param
is expensive to compute and it is needed several times as the argument to other functions.
b: We could implement compute_param
as
void * compute_param (...) {
static param_type param = ...
return ¶m;
}
However, this is not thread-safe, using openmp
we would have to protect the call
#pragama omp critical
{
void * param = compute_param (...)
}
which would severely affect the performance in a threaded program.
C: One solution would use malloc/free
void compute_param (..., void * param) {
param = malloc (sizeof (param_type));
....
*param = ...
}
which burdens the caller with the freeing of the allocated memory, while being inefficient because of the expensive heap memory.
d: Use of alloca
, e.g.
void * param = alloca (param_size);
compute_param (..., param);
...
needs_param (param, ...);
, needs the information about the param
size in the caller space, which we would prefer to hide.
Do you have any suggestion?