abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
libSupport.c File Reference
#include <stdio.h>
#include <string.h>
#include "base/abc/abc.h"
#include "mainInt.h"
#include <sys/types.h>
#include <dirent.h>
#include <dlfcn.h>

Go to the source code of this file.

Macros

#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]
 

Macro Definition 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 47 of file libSupport.c.

Typedef Documentation

typedef void(* lib_init_end_func)(Abc_Frame_t *pAbc)

Definition at line 50 of file libSupport.c.

Function Documentation

void call_ends ( Abc_Frame_t pAbc)

Definition at line 180 of file libSupport.c.

180  {
181  int i;
182  lib_init_end_func end_func;
183  for (i = 0; libHandles[i] != 0; i++) {
184  end_func = (lib_init_end_func) get_fnct_ptr(i, "abc_end");
185  if (end_func == 0) {
186  printf("Warning: Failed to end library %d.\n", i);
187  } else {
188  (*end_func)(pAbc);
189  }
190  }
191 }
void * get_fnct_ptr(int lib_num, char *sym_name)
Definition: libSupport.c:152
void(* lib_init_end_func)(Abc_Frame_t *pAbc)
Definition: libSupport.c:50
static void * libHandles[MAX_LIBS+1]
Definition: libSupport.c:48
void call_inits ( Abc_Frame_t pAbc)

Definition at line 164 of file libSupport.c.

164  {
165  int i;
166  lib_init_end_func init_func;
167  for (i = 0; libHandles[i] != 0; i++) {
168  init_func = (lib_init_end_func) get_fnct_ptr(i, "abc_init");
169  if (init_func == 0) {
170  printf("Warning: Failed to initialize library %d.\n", i);
171  } else {
172  (*init_func)(pAbc);
173  }
174  }
175 }
void * get_fnct_ptr(int lib_num, char *sym_name)
Definition: libSupport.c:152
void(* lib_init_end_func)(Abc_Frame_t *pAbc)
Definition: libSupport.c:50
static void * libHandles[MAX_LIBS+1]
Definition: libSupport.c:48
void close_libs ( )

Definition at line 135 of file libSupport.c.

135  {
136 #ifdef WIN32
137  printf("Warning: close_libs WIN32 not implemented.\n");
138 #else
139  int i;
140  for (i = 0; libHandles[i] != 0; i++) {
141  if (dlclose(libHandles[i]) != 0) {
142  printf("Warning: failed to close library %d\n", i);
143  }
144  libHandles[i] = 0;
145  }
146 #endif
147 }
static void * libHandles[MAX_LIBS+1]
Definition: libSupport.c:48
void* get_fnct_ptr ( int  lib_num,
char *  sym_name 
)

Definition at line 152 of file libSupport.c.

152  {
153 #ifdef WIN32
154  printf("Warning: get_fnct_ptr WIN32 not implemented.\n");
155  return 0;
156 #else
157  return dlsym(libHandles[lib_num], sym_name);
158 #endif
159 }
static void * libHandles[MAX_LIBS+1]
Definition: libSupport.c:48
void Libs_End ( Abc_Frame_t pAbc)

Definition at line 199 of file libSupport.c.

200 {
201  call_ends(pAbc);
202 
203  // It's good practice to close our libraries at this point, but this can mess up any backtrace printed by Valgind.
204  // close_libs();
205 }
void call_ends(Abc_Frame_t *pAbc)
Definition: libSupport.c:180
void Libs_Init ( Abc_Frame_t pAbc)

Definition at line 193 of file libSupport.c.

194 {
195  open_libs();
196  call_inits(pAbc);
197 }
void open_libs()
Definition: libSupport.c:55
void call_inits(Abc_Frame_t *pAbc)
Definition: libSupport.c:164
void open_libs ( )

Definition at line 55 of file libSupport.c.

55  {
56  int curr_lib = 0;
57 
58 #ifdef WIN32
59 // printf("Warning: open_libs WIN32 not implemented.\n");
60 #else
61  DIR* dirp;
62  struct dirent* dp;
63  char *env, *init_p, *p;
64  int done;
65 
66  env = getenv ("ABC_LIB_PATH");
67  if (env == NULL) {
68 // printf("Warning: ABC_LIB_PATH not defined. Looking into the current directory.\n");
69  init_p = ABC_ALLOC( char, (2*sizeof(char)) );
70  init_p[0]='.'; init_p[1] = 0;
71  } else {
72  init_p = ABC_ALLOC( char, ((strlen(env)+1)*sizeof(char)) );
73  strcpy (init_p, env);
74  }
75 
76  // Extract directories and read libraries
77  done = 0;
78  p = init_p;
79  while (!done) {
80  char *endp = strchr (p,':');
81  if (endp == NULL) done = 1; // last directory in the list
82  else *endp = 0; // end of string
83 
84  dirp = opendir(p);
85  if (dirp == NULL) {
86 // printf("Warning: directory in ABC_LIB_PATH does not exist (%s).\n", p);
87  continue;
88  }
89 
90  while ((dp = readdir(dirp)) != NULL) {
91  if ((strncmp("libabc_", dp->d_name, 7) == 0) &&
92  (strcmp(".so", dp->d_name + strlen(dp->d_name) - 3) == 0)) {
93 
94  // make sure we don't overflow the handle array
95  if (curr_lib >= MAX_LIBS) {
96  printf("Warning: maximum number of ABC libraries (%d) exceeded. Not loading %s.\n",
97  MAX_LIBS,
98  dp->d_name);
99  }
100 
101  // attempt to load it
102  else {
103  char* szPrefixed = ABC_ALLOC( char, ((strlen(dp->d_name) + strlen(p) + 2) *
104  sizeof(char)) );
105  sprintf(szPrefixed, "%s/", p);
106  strcat(szPrefixed, dp->d_name);
107  libHandles[curr_lib] = dlopen(szPrefixed, RTLD_NOW | RTLD_LOCAL);
108 
109  // did the load succeed?
110  if (libHandles[curr_lib] != 0) {
111  printf("Loaded ABC library: %s (Abc library extension #%d)\n", szPrefixed, curr_lib);
112  curr_lib++;
113  } else {
114  printf("Warning: failed to load ABC library %s:\n\t%s\n", szPrefixed, dlerror());
115  }
116 
117  ABC_FREE(szPrefixed);
118  }
119  }
120  }
121  closedir(dirp);
122  p = endp+1;
123  }
124 
125  ABC_FREE(init_p);
126 #endif
127 
128  // null terminate the list of handles
129  libHandles[curr_lib] = 0;
130 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int strcmp()
char * strchr()
char * getenv()
char * sprintf()
char * strcpy()
#define MAX_LIBS
Definition: libSupport.c:47
#define ABC_FREE(obj)
Definition: abc_global.h:232
char * strcat()
int strncmp()
int strlen()
static void * libHandles[MAX_LIBS+1]
Definition: libSupport.c:48

Variable Documentation

void* libHandles[MAX_LIBS+1]
static

Definition at line 48 of file libSupport.c.