From a1d5d438d3360f679d7a8a9e552768cff8fea1a9 Mon Sep 17 00:00:00 2001 From: tradke Date: Wed, 26 Mar 2003 15:52:34 +0000 Subject: Changed the recovery function registry to use a table to store function pointers rather than a cHandledData data structure (which can store only 'void *' pointers). This prevents potential problems on platforms where function pointers have a different memory representation than data pointers. git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/IOUtil/trunk@187 b32723a9-ab3a-4a60-88e2-2e5d99d7c17a --- src/CheckpointRecovery.c | 111 +++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/src/CheckpointRecovery.c b/src/CheckpointRecovery.c index 3e96edb..bfc4fd8 100644 --- a/src/CheckpointRecovery.c +++ b/src/CheckpointRecovery.c @@ -12,7 +12,6 @@ #include "cctk.h" #include "cctk_Parameters.h" #include "cctk_FortranString.h" -#include "StoreHandledData.h" #include "util_Table.h" #include "ioGH.h" @@ -41,9 +40,8 @@ void CCTK_FCALL CCTK_FNAME (IOUtil_RecoverVarsFromDatafiles) /******************************************************************** ******************** Static Variables ************************* ********************************************************************/ -/* Local data holding info on Recover Functions */ -static cHandledData *RecoverFunctions = NULL; -static int num_functions = 0; +/* table to store registered recovery functions */ +static int recover_fn_table = -1; static int checkpoint_file_exists = 0; @@ -56,6 +54,7 @@ typedef struct int iteration; } filelist_t; +typedef int (*recover_fn_t) (cGH *GH, const char *basefilename,int called_from); /******************************************************************** ******************** Internal Routines ************************ @@ -86,37 +85,38 @@ static int CompareFiles (const void *a, const void *b); @vdesc The name of the function for recovery @vtype const char * @vio in - @vcomment + @endvar + @var recover_fn + @vdesc the recovery function to register + @vtype recover_fn_t + @vio in @endvar @returntype int @returndesc - -1 method with this name already registered, or - return code of @seeroutine Util_NewHandle + 0 for success, or
+ -1 method with this name already registered @endreturndesc @@*/ -int IOUtil_RegisterRecover (const char *name, - int (*func) (cGH *, const char *, int)) +int IOUtil_RegisterRecover (const char *name, recover_fn_t recover_fn) { - int handle; + int retval; - /* Check that the method hasn't already been registered */ - handle = Util_GetHandle (RecoverFunctions, name, NULL); - if (handle < 0) + /* create the function pointer table on the first time through */ + if (recover_fn_table < 0) { - /* New function - get a handle for it. */ - handle = Util_NewHandle (&RecoverFunctions, name, func); - - /* Remember how many methods there are */ - num_functions++; + recover_fn_table = Util_TableCreate (UTIL_TABLE_FLAGS_DEFAULT); } - else + + /* check that the method hasn't already been registered */ + retval = Util_TableQueryValueInfo (recover_fn_table, NULL,NULL,name) ? -1 : 0; + if (! retval) { - handle = -1; + Util_TableSetFnPointer (recover_fn_table, (CCTK_FPOINTER) recover_fn, name); } - return (handle); + return (retval); } @@ -278,33 +278,39 @@ static int IOUtil_RecoverFromFile (cGH *GH, const char *basefilename, int called_from) { - int handle; - int retval; - int (*recoverFn) (cGH *, const char *, int); + int keylen, ihandle, retval; + char *key; + CCTK_FPOINTER func; + recover_fn_t recover_fn; - retval = -1; - - for (handle = 0; handle < num_functions; handle++) + if (Util_TableQueryNKeys (recover_fn_table) <= 0) { + CCTK_WARN (1, "IOUtil_RecoverFromFile: No recovery routines " + "were registered"); + return (-1); + } + + keylen = Util_TableQueryMaxKeyLength (recover_fn_table) + 1; + key = malloc (keylen); - recoverFn = (int (*) (cGH *, const char *, int)) - Util_GetHandledData (RecoverFunctions, handle); - if (recoverFn) + for (ihandle = Util_TableItCreate (recover_fn_table), retval = -1; + Util_TableItQueryIsNonNull (ihandle) > 0 && retval < 0; + Util_TableItAdvance (ihandle)) + { + Util_TableItQueryKeyValueInfo (ihandle, keylen, key, NULL, NULL); + Util_TableGetFnPointer (recover_fn_table, &func, key); + if (func) { - retval = recoverFn (GH, basefilename, called_from); - if (retval >= 0) - { - break; - } + recover_fn = (recover_fn_t) func; + retval = recover_fn (GH, basefilename, called_from); } } - - if (num_functions <= 0) + if (keylen > 0) { - CCTK_WARN (1, "IOUtil_RecoverFromFile: No recovery routines " - "were registered"); + free (key); } + Util_TableItDestroy (ihandle); return (retval); } @@ -385,11 +391,11 @@ int IOUtil_RecoverVarsFromDatafiles (cGH *GH, DECLARE_CCTK_PARAMETERS - myGH = (ioGH *) CCTK_GHExtension (GH, "IO"); + myGH = CCTK_GHExtension (GH, "IO"); if (CCTK_NumVars () > 0) { - myGH->do_inVars = (CCTK_INT *) calloc (CCTK_NumVars (), sizeof (CCTK_INT)); + myGH->do_inVars = calloc (CCTK_NumVars (), sizeof (CCTK_INT)); CCTK_TraverseString (in_vars, SetInputFlag, myGH->do_inVars, CCTK_GROUP_OR_VAR); } @@ -501,7 +507,7 @@ void IOUtil_RecoverIDFromDatafiles (cGH *GH) plus a checkpoint filename. @enddesc - @var recoverFn + @var recover_fn @vdesc callback function for recovery of parameters from a given checkpoint file @vtype int (*) (cGH *, const char *, int) @@ -528,9 +534,9 @@ void IOUtil_RecoverIDFromDatafiles (cGH *GH) -2 if in "auto*" mode and recovery dir doesn't exist @endreturndesc @@*/ -int IOUtil_RecoverParameters (int (*recoverFn) (cGH *GH, - const char *basefilename, - int called_from), +int IOUtil_RecoverParameters (int (*recover_fn) (cGH *GH, + const char *basefilename, + int called_from), const char *fileExtension, const char *fileType) { @@ -610,12 +616,11 @@ int IOUtil_RecoverParameters (int (*recoverFn) (cGH *GH, /* found a recovery file by that basename */ if (num_files == 0) { - tmp = (filelist_t *) malloc (sizeof (filelist_t)); + tmp = malloc (sizeof (filelist_t)); } else { - tmp = (filelist_t *) realloc (filelist, - (num_files+1) * sizeof (filelist_t)); + tmp = realloc (filelist, (num_files+1) * sizeof (filelist_t)); } if (tmp == NULL) { @@ -642,13 +647,13 @@ int IOUtil_RecoverParameters (int (*recoverFn) (cGH *GH, /* loop over all recovery files found and call the callback routine; skip all following files after the first successful recovery (when - recoverFn() returned a positive value) */ + recover_fn() returned a positive value) */ while (num_files--) { if (retval <= 0) { - retval = recoverFn (NULL, filelist[num_files].basename, - CP_RECOVER_PARAMETERS); + retval = recover_fn (NULL, filelist[num_files].basename, + CP_RECOVER_PARAMETERS); } free (filelist[num_files].basename); } @@ -675,7 +680,7 @@ int IOUtil_RecoverParameters (int (*recoverFn) (cGH *GH, else { /* just call the recovery routine */ - retval = (*recoverFn) (NULL, recover_file, CP_RECOVER_PARAMETERS); + retval = (*recover_fn) (NULL, recover_file, CP_RECOVER_PARAMETERS); } if (retval < 0) @@ -771,7 +776,7 @@ char *IOUtil_GetAllParameters (const cGH *GH, int all) { max_len *= 2; } - tmp = (char *) realloc (retval, max_len); + tmp = realloc (retval, max_len); if (! tmp) { CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING, -- cgit v1.2.3