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 #define PERL_EUPXS_ALWAYS_EXPORT
12 #ifndef START_MY_CXT /* Some IDEs try compiling this standalone. */
19 # define XS_VERSION "0"
21 #define MY_CXT_KEY "DynaLoader::_guts" XS_VERSION
24 SV* x_dl_last_error; /* pointer to allocated memory for
26 int x_dl_nonlazy; /* flag for immediate rather than lazy
27 linking (spots unresolved symbol) */
28 #ifdef DL_LOADONCEONLY
29 HV * x_dl_loaded_files; /* only needed on a few systems */
32 my_cxtx_t x_dl_cxtx; /* extra platform-specific data */
35 int x_dl_debug; /* value copied from $DynaLoader::dl_debug */
41 #define dl_last_error (SvPVX(MY_CXT.x_dl_last_error))
42 #define dl_nonlazy (MY_CXT.x_dl_nonlazy)
43 #ifdef DL_LOADONCEONLY
44 #define dl_loaded_files (MY_CXT.x_dl_loaded_files)
47 #define dl_cxtx (MY_CXT.x_dl_cxtx)
50 #define dl_debug (MY_CXT.x_dl_debug)
54 #define DLDEBUG(level,code) \
57 if (dl_debug>=level) { code; } \
60 #define DLDEBUG(level,code) NOOP
63 #ifdef DL_UNLOAD_ALL_AT_EXIT
64 /* Close all dlopen'd files */
66 dl_unload_all_files(pTHX_ void *unused)
72 if ((sub = get_cvs("DynaLoader::dl_unload_file", 0)) != NULL) {
73 dl_librefs = get_av("DynaLoader::dl_librefs", 0);
74 while ((dl_libref = av_pop(dl_librefs)) != &PL_sv_undef) {
79 XPUSHs(sv_2mortal(dl_libref));
81 call_sv((SV*)sub, G_DISCARD | G_NODEBUG);
90 dl_generic_private_init(pTHX) /* called by dl_*.xs dl_private_init() */
92 char *perl_dl_nonlazy;
95 MY_CXT.x_dl_last_error = newSVpvn("", 0);
97 #ifdef DL_LOADONCEONLY
98 dl_loaded_files = NULL;
102 SV *sv = get_sv("DynaLoader::dl_debug", 0);
103 dl_debug = sv ? SvIV(sv) : 0;
106 if ( (perl_dl_nonlazy = getenv("PERL_DL_NONLAZY")) != NULL )
107 dl_nonlazy = atoi(perl_dl_nonlazy);
109 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "DynaLoader bind mode is 'non-lazy'\n"));
110 #ifdef DL_LOADONCEONLY
111 if (!dl_loaded_files)
112 dl_loaded_files = newHV(); /* provide cache for dl_*.xs if needed */
114 #ifdef DL_UNLOAD_ALL_AT_EXIT
115 call_atexit(&dl_unload_all_files, (void*)0);
121 /* SaveError() takes printf style args and saves the result in dl_last_error */
123 SaveError(pTHX_ const char* pat, ...)
131 /* This code is based on croak/warn, see mess() in util.c */
134 msv = vmess(pat, &args);
137 message = SvPV(msv,len);
138 len++; /* include terminating null char */
140 /* Copy message into dl_last_error (including terminating null char) */
141 sv_setpvn(MY_CXT.x_dl_last_error, message, len) ;
142 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",dl_last_error));