src/base/main/libSupport.c File Reference

#include "mainInt.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <dlfcn.h>
Include dependency graph for libSupport.c:

Go to the source code of this file.

Defines

#define MAX_LIBS   256

Typedefs

typedef void(* lib_init_end_func )(Abc_Frame_t *pAbc)

Functions

void open_libs ()
void close_libs ()
void * get_fnct_ptr (int lib_num, char *sym_name)
void call_inits (Abc_Frame_t *pAbc)
void call_ends (Abc_Frame_t *pAbc)
void Libs_Init (Abc_Frame_t *pAbc)
void Libs_End (Abc_Frame_t *pAbc)

Variables

static void * libHandles [MAX_LIBS+1]

Define Documentation

#define MAX_LIBS   256

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

]

Definition at line 31 of file libSupport.c.


Typedef Documentation

typedef void(* lib_init_end_func)(Abc_Frame_t *pAbc)

Definition at line 34 of file libSupport.c.


Function Documentation

void call_ends ( Abc_Frame_t pAbc  ) 

Definition at line 164 of file libSupport.c.

00164                                   {
00165     int i;
00166     lib_init_end_func end_func;
00167     for (i = 0; libHandles[i] != 0; i++) {
00168         end_func = (lib_init_end_func) get_fnct_ptr(i, "abc_end");
00169         if (end_func == 0) {
00170             printf("Warning: Failed to end library %d.\n", i);
00171         } else {
00172             (*end_func)(pAbc);
00173         }
00174     }
00175 }

void call_inits ( Abc_Frame_t pAbc  ) 

Definition at line 148 of file libSupport.c.

00148                                    {
00149     int i;
00150     lib_init_end_func init_func;
00151     for (i = 0; libHandles[i] != 0; i++) {
00152         init_func = (lib_init_end_func) get_fnct_ptr(i, "abc_init");
00153         if (init_func == 0) {
00154             printf("Warning: Failed to initialize library %d.\n", i);
00155         } else {
00156             (*init_func)(pAbc);
00157         }
00158     }
00159 }

void close_libs (  ) 

Definition at line 119 of file libSupport.c.

00119                   {
00120 #ifdef WIN32
00121     printf("Warning: close_libs WIN32 not implemented.\n");
00122 #else
00123     int i;
00124     for (i = 0; libHandles[i] != 0; i++) {
00125         if (dlclose(libHandles[i]) != 0) {
00126             printf("Warning: failed to close library %d\n", i);
00127         }
00128         libHandles[i] = 0;
00129     }
00130 #endif
00131 }

void* get_fnct_ptr ( int  lib_num,
char *  sym_name 
)

Definition at line 136 of file libSupport.c.

00136                                                 {
00137 #ifdef WIN32
00138     printf("Warning: get_fnct_ptr WIN32 not implemented.\n");
00139     return 0;
00140 #else
00141     return dlsym(libHandles[lib_num], sym_name);
00142 #endif
00143 }

void Libs_End ( Abc_Frame_t pAbc  ) 

Definition at line 183 of file libSupport.c.

00184 {
00185     call_ends(pAbc);
00186 
00187     // It's good practice to close our libraries at this point, but this can mess up any backtrace printed by Valgind.
00188     //    close_libs();
00189 }

void Libs_Init ( Abc_Frame_t pAbc  ) 

Definition at line 177 of file libSupport.c.

00178 {
00179     open_libs();
00180     call_inits(pAbc);
00181 }

void open_libs (  ) 

Definition at line 39 of file libSupport.c.

00039                  {
00040     int curr_lib = 0;
00041 
00042 #ifdef WIN32
00043 //    printf("Warning: open_libs WIN32 not implemented.\n");
00044 #else
00045     DIR* dirp;
00046     struct dirent* dp;
00047     char *env, *init_p, *p;
00048     int done;
00049 
00050     env = getenv ("ABC_LIB_PATH");
00051     if (env == NULL) {
00052 //    printf("Warning: ABC_LIB_PATH not defined. Looking into the current directory.\n");
00053       init_p = malloc (2*sizeof(char));
00054       init_p[0]='.'; init_p[1] = 0;
00055     } else {
00056       init_p = malloc ((strlen(env)+1)*sizeof(char));
00057       strcpy (init_p, env);
00058     }
00059 
00060     // Extract directories and read libraries
00061     done = 0;
00062     p = init_p;
00063     while (!done) {
00064       char *endp = strchr (p,':');
00065       if (endp == NULL) done = 1; // last directory in the list
00066       else *endp = 0; // end of string
00067 
00068       dirp = opendir(p);
00069       if (dirp == NULL) {
00070 //      printf("Warning: directory in ABC_LIB_PATH does not exist (%s).\n", p);
00071         continue;
00072       }
00073 
00074       while ((dp = readdir(dirp)) != NULL) {
00075         if ((strncmp("libabc_", dp->d_name, 7) == 0) &&
00076             (strcmp(".so", dp->d_name + strlen(dp->d_name) - 3) == 0)) {
00077 
00078           // make sure we don't overflow the handle array
00079           if (curr_lib >= MAX_LIBS) {
00080             printf("Warning: maximum number of ABC libraries (%d) exceeded.  Not loading %s.\n",
00081                    MAX_LIBS,
00082                    dp->d_name);
00083           }
00084           
00085           // attempt to load it
00086           else {
00087             char* szPrefixed = malloc((strlen(dp->d_name) + strlen(p) + 2) * 
00088                                       sizeof(char));
00089             sprintf(szPrefixed, "%s/", p);
00090             strcat(szPrefixed, dp->d_name);
00091             libHandles[curr_lib] = dlopen(szPrefixed, RTLD_NOW | RTLD_LOCAL);
00092             
00093             // did the load succeed?
00094             if (libHandles[curr_lib] != 0) {
00095               printf("Loaded ABC library: %s (Abc library extension #%d)\n", szPrefixed, curr_lib);
00096               curr_lib++;
00097             } else {
00098               printf("Warning: failed to load ABC library %s:\n\t%s\n", szPrefixed, dlerror());
00099             }
00100             
00101             free(szPrefixed);
00102           }
00103         }
00104       }
00105       closedir(dirp);
00106       p = endp+1;
00107     }
00108 
00109     free(init_p);
00110 #endif
00111     
00112     // null terminate the list of handles
00113     libHandles[curr_lib] = 0;    
00114 }


Variable Documentation

void* libHandles[MAX_LIBS+1] [static]

Definition at line 32 of file libSupport.c.


Generated on Tue Jan 5 12:18:48 2010 for abc70930 by  doxygen 1.6.1