diff options
author | goodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2000-01-21 17:18:27 +0000 |
---|---|---|
committer | goodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2000-01-21 17:18:27 +0000 |
commit | 71ba48cb19532d47a96273e365f50ca89dea1728 (patch) | |
tree | 3e7ebe644cf17602ec6ba759c43692706da629f3 /src/main | |
parent | 905c99e7bf1a6ac4d8af906e9c5ea2c9cb13d65c (diff) |
New function registration stuff.
Basically can register functions which return void CCTK_INT or CCTK_REAL
and take up to CCTK_MAX_ARGS (currently 255) void * arguments.
The CCTK_FunctionCall function can then be used to call this function, timing
its execution.
Tom
git-svn-id: http://svn.cactuscode.org/flesh/trunk@1279 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/Functions.c | 216 | ||||
-rw-r--r-- | src/main/make.code.defn | 1 |
2 files changed, 217 insertions, 0 deletions
diff --git a/src/main/Functions.c b/src/main/Functions.c new file mode 100644 index 00000000..48fb3976 --- /dev/null +++ b/src/main/Functions.c @@ -0,0 +1,216 @@ + /*@@ + @file Functions.c + @date Sat Dec 4 13:39:12 1999 + @author Tom Goodale + @desc + File to hold all CCTK overloadable or registerable functions. + In principle all should be registered through here, but probably + need a bit more functionality first. + @enddesc + @@*/ + +static char *rcsid = "$Header$"; + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> + +#include "cctk.h" + +#include "CactusTimers.h" +#include "StoreHandledData.h" + +#include "cctki_Functions.h" + +static cHandledData *functions; + +typedef struct +{ + int n_args; + int type; +} cFunctionType; + +struct iFunctionData +{ + int timer; + + cFunctionType ftype; + + void (*function)(); +}; + + + /*@@ + @routine CCTK_FunctionRegister + @date Sat Dec 4 15:05:42 1999 + @author Tom Goodale + @desc + Registers a function. + @enddesc + @calls + @calledby + @history + + @endhistory + + @var name + @vdesc Function name + @vtype const char * + @vio in + @vcomment + The name of the function being registered. + @endvar + @var function + @vdesc Function being registered + @vtype void (*)(void) + @vio in + @vcomment + Pointer to the function being registered. + Doesn't matter what the real type is as long as it returns void. + @endvar + @var ftype + @vdesc The type of the function. + @vtype cFunctionType + @vio in + @vcomment + This is a structure which should be filled out with + the number of arguments the function takes + the return type of the function + @endvar + + @returntype int + @returndesc + 0 - success + -1 - memory failure + -2 - too many arguments + @endreturndesc + +@@*/ +int CCTK_FunctionRegister(const char *name, void (*function)(void), cFunctionType ftype) +{ + int handle; + struct iFunctionData *fdata; + + if(ftype.n_args <= CCTK_MAX_ARGS) + { + fdata = (struct iFunctionData *)malloc(sizeof(struct iFunctionData)); + + if(fdata) + { + fdata->function = function; + fdata->ftype.n_args = ftype.n_args; + fdata->ftype.type = ftype.type; + fdata->timer = CCTK_TimerCreateI(); + + handle = Util_NewHandle(&functions, name, fdata); + } + else + { + handle = -1; + } + } + else + { + handle = -2; + } + + return handle; +} + + /*@@ + @routine CCTK_FunctionCall + @date Sat Dec 4 15:10:36 1999 + @author Tom Goodale + @desc + Calls a function with the appropriate number of arguments. + @enddesc + @calls + @calledby + @history + + @endhistory + + @var handle + @vdesc The function handle + @vtype int + @vio in + @vcomment + This is the handle returned by CCTK_FunctionRegister + @endvar + @var ... + @vdesc Variable argument list + @vtype any number of void *s + @vio inout + @vcomment + This is 0 or more void *s which will be passed unchanged to the + function. The number here should match the number given to + CCTK_FunctionRegister + @endvar + +@@*/ +int CCTK_FunctionCall(int handle, ...) +{ + int retval; + int i; + va_list ap; + struct iFunctionData *fdata; + char *retchar; + CCTK_REAL *retreal; + CCTK_INT *retint; + + void *array[CCTK_MAX_ARGS]; + + fdata = (struct iFunctionData *)Util_GetHandledData(functions, handle); + + if(fdata) + { + va_start(ap, handle); + + if(fdata->ftype.type != CCTK_VARIABLE_VOID) + { + switch(fdata->ftype.type) + { + case CCTK_VARIABLE_REAL : retreal = va_arg(ap, CCTK_REAL *); break; + case CCTK_VARIABLE_INT : retint = va_arg(ap, CCTK_INT *); break; + default : + fprintf(stderr, "Unsupported return type for function at line %d of %s", + __LINE__, __FILE__); + } + } + + if(fdata->ftype.n_args > 0) + { + for(i = 0; i < fdata->ftype.n_args; i++) + { + array[i] = va_arg(ap, void *); + } + } + + va_end(ap); + + CCTK_TimerStartI(fdata->timer); + + switch(fdata->ftype.type) + { + case CCTK_VARIABLE_VOID : CCTK_CALLVOIDFUNC(handle, array, fdata->function); break; + case CCTK_VARIABLE_REAL : CCTK_CALLRETFUNC(*retreal, CCTK_REAL, handle, array, fdata->function); break; + case CCTK_VARIABLE_INT : CCTK_CALLRETFUNC(*retint, CCTK_INT, handle, array, fdata->function); break; + default : + fprintf(stderr, "Unsupported return type for function at line %d of %s", + __LINE__, __FILE__); + } + + CCTK_TimerStopI(fdata->timer); + + retval = 0; + } + else + { + retval = -1; + } + + return retval; +} + + + diff --git a/src/main/make.code.defn b/src/main/make.code.defn index a4674d67..a708aa50 100644 --- a/src/main/make.code.defn +++ b/src/main/make.code.defn @@ -17,6 +17,7 @@ DefaultTimers.c\ Dummies.c\ flesh.cc\ FortranWrappers.c\ +Functions.c\ GHExtensions.c\ Groups.c\ GroupsOnGH.c\ |