1 /* dlutils.c - handy functions and definitions for dl_*.xs files
3 * Currently this file is simply #included into dl_*.xs/.c files.
4 * It should really be split into a dlutils.h and dlutils.c
7 * 29th Feburary 2000 - Alan Burlison: Added functionality to close dlopen'd
8 * files when the interpreter exits
11 #ifndef START_MY_CXT /* Some IDEs try compiling this standalone. */
18 # define XS_VERSION "0"
20 #define MY_CXT_KEY "DynaLoader::_guts" XS_VERSION
23 SV* x_dl_last_error; /* pointer to allocated memory for
25 int x_dl_nonlazy; /* flag for immediate rather than lazy
26 linking (spots unresolved symbol) */
27 #ifdef DL_LOADONCEONLY
28 HV * x_dl_loaded_files; /* only needed on a few systems */
31 my_cxtx_t x_dl_cxtx; /* extra platform-specific data */
34 int x_dl_debug; /* value copied from $DynaLoader::dl_debug */
40 #define dl_last_error (SvPVX(MY_CXT.x_dl_last_error))
41 #define dl_nonlazy (MY_CXT.x_dl_nonlazy)
42 #ifdef DL_LOADONCEONLY
43 #define dl_loaded_files (MY_CXT.x_dl_loaded_files)
46 #define dl_cxtx (MY_CXT.x_dl_cxtx)
49 #define dl_debug (MY_CXT.x_dl_debug)
53 #define DLDEBUG(level,code) \
56 if (dl_debug>=level) { code; } \
59 #define DLDEBUG(level,code) NOOP
62 #ifdef DL_UNLOAD_ALL_AT_EXIT
63 /* Close all dlopen'd files */
65 dl_unload_all_files(pTHX_ void *unused)
71 if ((sub = get_cv("DynaLoader::dl_unload_file", FALSE)) != NULL) {
72 dl_librefs = get_av("DynaLoader::dl_librefs", FALSE);
73 while ((dl_libref = av_pop(dl_librefs)) != &PL_sv_undef) {
78 XPUSHs(sv_2mortal(dl_libref));
80 call_sv((SV*)sub, G_DISCARD | G_NODEBUG);
89 dl_generic_private_init(pTHX) /* called by dl_*.xs dl_private_init() */
91 char *perl_dl_nonlazy;
94 MY_CXT.x_dl_last_error = newSVpvn("", 0);
96 #ifdef DL_LOADONCEONLY
97 dl_loaded_files = Nullhv;
101 SV *sv = get_sv("DynaLoader::dl_debug", 0);
102 dl_debug = sv ? SvIV(sv) : 0;
105 if ( (perl_dl_nonlazy = getenv("PERL_DL_NONLAZY")) != NULL )
106 dl_nonlazy = atoi(perl_dl_nonlazy);
108 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "DynaLoader bind mode is 'non-lazy'\n"));
109 #ifdef DL_LOADONCEONLY
110 if (!dl_loaded_files)
111 dl_loaded_files = newHV(); /* provide cache for dl_*.xs if needed */
113 #ifdef DL_UNLOAD_ALL_AT_EXIT
114 call_atexit(&dl_unload_all_files, (void*)0);
120 /* SaveError() takes printf style args and saves the result in dl_last_error */
122 SaveError(pTHX_ const char* pat, ...)
130 /* This code is based on croak/warn, see mess() in util.c */
133 msv = vmess(pat, &args);
136 message = SvPV(msv,len);
137 len++; /* include terminating null char */
139 /* Copy message into dl_last_error (including terminating null char) */
140 sv_setpvn(MY_CXT.x_dl_last_error, message, len) ;
141 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",dl_last_error));