Download MATLAB C Math Library 2.0 User's Guide

Transcript
Passing Functions As Arguments to Library Routines
How mlfFeval() Works
mlfFeval() uses a built-in table to find out how to execute a particular
function. The built-in table provides mlfFeval() with two pieces of
information: a pointer that points to the function to be executed and a pointer
to what’s called a “thunk function.”
As shipped, mlfFeval()’s built-in table contains each function in the MATLAB
C Math Library. If you want mlfFeval() to know how to execute a function that
you’ve written, you must extend the built-in table by creating a local function
table that identifies your function for mlfFeval().
It’s the thunk function, however, that actually knows how to execute your
function. In ‘‘Example Program: Passing Functions As Arguments (ex4.c)’’ on
page 6-22, the thunk function, _lorenz_thunk_fcn_, executes lorenz(). A
thunk function’s actions are solely determined by the number of input and
output arguments to the function it is calling. Therefore, any functions that
have the same number of input and output arguments can share the same
thunk function. For example, if you wrote three functions that each take two
inputs and produce three outputs, you only need to write one thunk function to
handle all three.
mlfFeval() calls the thunk function through the pointer it retrieves from the
built-in table, passing it a pointer to the function to be executed, the number of
input and output arguments, and the input and output argument arrays.
Thunk functions also use the mlfFeval() calling convention.
The thunk function then translates from the calling convention used by
mlfFeval() (arrays of arguments) to the standard C Math Library calling
convention (an explicit list of arguments), executes the function, and returns
the results to mlfFeval().
Extending the mlfFeval() Table
In order to extend the built-in mlfFeval() table, you must:
1 Write the function that you want a function-function to execute.
2 Write a thunk function that knows how to call your function.
6-21