diff options
author | Alan Mishchenko <alanmi@berkeley.edu> | 2005-09-15 08:01:00 -0700 |
---|---|---|
committer | Alan Mishchenko <alanmi@berkeley.edu> | 2005-09-15 08:01:00 -0700 |
commit | 2d782f7bc966fb19c9d849ac70366709f04d25d8 (patch) | |
tree | d11e13b3c07e2282be6493cf4d7e3b808e6b5c2f /src/base/main | |
parent | 0f6eeaea3c9d8fb7f4b4aa97f6e640d39e3c0afa (diff) | |
download | abc-2d782f7bc966fb19c9d849ac70366709f04d25d8.tar.gz abc-2d782f7bc966fb19c9d849ac70366709f04d25d8.tar.bz2 abc-2d782f7bc966fb19c9d849ac70366709f04d25d8.zip |
Version abc50915
Diffstat (limited to 'src/base/main')
-rw-r--r-- | src/base/main/libSupport.c | 164 | ||||
-rw-r--r-- | src/base/main/main.c | 2 | ||||
-rw-r--r-- | src/base/main/mainInit.c | 6 | ||||
-rw-r--r-- | src/base/main/module.make | 3 |
4 files changed, 172 insertions, 3 deletions
diff --git a/src/base/main/libSupport.c b/src/base/main/libSupport.c new file mode 100644 index 00000000..51c137cf --- /dev/null +++ b/src/base/main/libSupport.c @@ -0,0 +1,164 @@ +/**CFile**************************************************************** + + FileName [libSupport.c] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [The main package.] + + Synopsis [Support for external libaries.] + + Author [Mike Case] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: libSupport.c,v 1.1 2005/09/06 19:59:51 casem Exp $] + +***********************************************************************/ + +#include "mainInt.h" +#include <stdio.h> +#include <string.h> + +#ifndef WIN32 +# include <sys/types.h> +# include <dirent.h> +# include <dlfcn.h> +#endif + +#define MAX_LIBS 256 +static void* libHandles[MAX_LIBS+1]; // will be null terminated + +typedef void (*lib_init_end_func) (Abc_Frame_t * pAbc); + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// This will find all the ABC library extensions in the current directory and load them all. +//////////////////////////////////////////////////////////////////////////////////////////////////// +void open_libs() { + int curr_lib = 0; + +#ifdef WIN32 + printf("Warning: open_libs WIN32 not implemented.\n"); +#else + DIR* dirp; + struct dirent* dp; + + dirp = opendir("."); + while ((dp = readdir(dirp)) != NULL) { + if ((strncmp("libabc_", dp->d_name, 7) == 0) && + (strcmp(".so", dp->d_name + strlen(dp->d_name) - 3) == 0)) { + + // make sure we don't overflow the handle array + if (curr_lib >= MAX_LIBS) { + printf("Warning: maximum number of ABC libraries (%d) exceeded. Not loading %s.\n", + MAX_LIBS, + dp->d_name); + } + + // attempt to load it + else { + char* szPrefixed = malloc((strlen(dp->d_name) + 3) * sizeof(char)); + strcpy(szPrefixed, "./"); + strcat(szPrefixed, dp->d_name); + + libHandles[curr_lib] = dlopen(szPrefixed, RTLD_NOW | RTLD_LOCAL); + + // did the load succeed? + if (libHandles[curr_lib] != 0) { + printf("Loaded ABC library: %s (Abc library extension #%d)\n", szPrefixed, curr_lib); + curr_lib++; + } else { + printf("Warning: failed to load ABC library %s:\n\t%s\n", szPrefixed, dlerror()); + } + + free(szPrefixed); + } + } + } + closedir(dirp); +#endif + + // null terminate the list of handles + libHandles[curr_lib] = 0; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// This will close all open ABC library extensions +//////////////////////////////////////////////////////////////////////////////////////////////////// +void close_libs() { +#ifdef WIN32 + printf("Warning: close_libs WIN32 not implemented.\n"); +#else + int i; + for (i = 0; libHandles[i] != 0; i++) { + if (dlclose(libHandles[i]) != 0) { + printf("Warning: failed to close library %d\n", i); + } + libHandles[i] = 0; + } +#endif +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// This will get a pointer to a function inside of an open library +//////////////////////////////////////////////////////////////////////////////////////////////////// +void* get_fnct_ptr(int lib_num, char* sym_name) { +#ifdef WIN32 + printf("Warning: get_fnct_ptr WIN32 not implemented.\n"); + return 0; +#else + return dlsym(libHandles[lib_num], sym_name); +#endif +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// This will call an initialization function in every open library. +//////////////////////////////////////////////////////////////////////////////////////////////////// +void call_inits(Abc_Frame_t* pAbc) { + int i; + lib_init_end_func init_func; + for (i = 0; libHandles[i] != 0; i++) { + init_func = (lib_init_end_func) get_fnct_ptr(i, "abc_init"); + if (init_func == 0) { + printf("Warning: Failed to initialize library %d.\n", i); + } else { + (*init_func)(pAbc); + } + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// This will call a shutdown function in every open library. +//////////////////////////////////////////////////////////////////////////////////////////////////// +void call_ends(Abc_Frame_t* pAbc) { + int i; + lib_init_end_func end_func; + for (i = 0; libHandles[i] != 0; i++) { + end_func = (lib_init_end_func) get_fnct_ptr(i, "abc_end"); + if (end_func == 0) { + printf("Warning: Failed to end library %d.\n", i); + } else { + (*end_func)(pAbc); + } + } +} + +void Libs_Init(Abc_Frame_t * pAbc) +{ + open_libs(); + call_inits(pAbc); +} + +void Libs_End(Abc_Frame_t * pAbc) +{ + call_ends(pAbc); + + // It's good practice to close our libraries at this point, but this can mess up any backtrace printed by Valgind. + // close_libs(); +} + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// diff --git a/src/base/main/main.c b/src/base/main/main.c index 43ad6956..6668d088 100644 --- a/src/base/main/main.c +++ b/src/base/main/main.c @@ -216,7 +216,7 @@ int main( int argc, char * argv[] ) } // if the memory should be freed, quit packages - if ( fStatus == -2 ) + if ( fStatus < 0 ) { // perform uninitializations Abc_FrameEnd( pAbc ); diff --git a/src/base/main/mainInit.c b/src/base/main/mainInit.c index 13710dcb..186d58fe 100644 --- a/src/base/main/mainInit.c +++ b/src/base/main/mainInit.c @@ -14,7 +14,7 @@ Date [Ver. 1.0. Started - June 20, 2005.] - Revision [$Id: mainInit.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + Revision [$Id: mainInit.c,v 1.3 2005/09/14 22:53:37 casem Exp $] ***********************************************************************/ @@ -38,6 +38,8 @@ extern void Mio_Init( Abc_Frame_t * pAbc ); extern void Mio_End ( Abc_Frame_t * pAbc ); extern void Super_Init( Abc_Frame_t * pAbc ); extern void Super_End ( Abc_Frame_t * pAbc ); +extern void Libs_Init(Abc_Frame_t * pAbc); +extern void Libs_End(Abc_Frame_t * pAbc); //////////////////////////////////////////////////////////////////////// /// FUNCTION DEFITIONS /// @@ -63,6 +65,7 @@ void Abc_FrameInit( Abc_Frame_t * pAbc ) Map_Init( pAbc ); Mio_Init( pAbc ); Super_Init( pAbc ); + Libs_Init( pAbc ); } @@ -86,6 +89,7 @@ void Abc_FrameEnd( Abc_Frame_t * pAbc ) Map_End( pAbc ); Mio_End( pAbc ); Super_End( pAbc ); + Libs_End( pAbc ); } diff --git a/src/base/main/module.make b/src/base/main/module.make index 59e1315e..7a04e533 100644 --- a/src/base/main/module.make +++ b/src/base/main/module.make @@ -1,4 +1,5 @@ SRC += src/base/main/main.c \ src/base/main/mainFrame.c \ src/base/main/mainInit.c \ - src/base/main/mainUtils.c + src/base/main/mainUtils.c \ + src/base/main/libSupport.c |