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
12 /* pointer to allocated memory for last error message */
13 static char *LastError = (char*)NULL;
15 /* flag for immediate rather than lazy linking (spots unresolved symbol) */
16 static int dl_nonlazy = 0;
18 #ifdef DL_LOADONCEONLY
19 static HV *dl_loaded_files = Nullhv; /* only needed on a few systems */
24 static int dl_debug = 0; /* value copied from $DynaLoader::dl_debug */
25 #define DLDEBUG(level,code) if (dl_debug>=level) { code; }
27 #define DLDEBUG(level,code)
30 #ifdef DL_UNLOAD_ALL_AT_EXIT
31 /* Close all dlopen'd files */
33 dl_unload_all_files(pTHX_ void *unused)
39 if ((sub = get_cv("DynaLoader::dl_unload_file", FALSE)) != NULL) {
40 dl_librefs = get_av("DynaLoader::dl_librefs", FALSE);
41 while ((dl_libref = av_pop(dl_librefs)) != &PL_sv_undef) {
46 XPUSHs(sv_2mortal(dl_libref));
48 call_sv((SV*)sub, G_DISCARD | G_NODEBUG);
57 dl_generic_private_init(pTHX) /* called by dl_*.xs dl_private_init() */
59 char *perl_dl_nonlazy;
61 SV *sv = get_sv("DynaLoader::dl_debug", 0);
62 dl_debug = sv ? SvIV(sv) : 0;
64 if ( (perl_dl_nonlazy = getenv("PERL_DL_NONLAZY")) != NULL )
65 dl_nonlazy = atoi(perl_dl_nonlazy);
67 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "DynaLoader bind mode is 'non-lazy'\n"));
68 #ifdef DL_LOADONCEONLY
70 dl_loaded_files = newHV(); /* provide cache for dl_*.xs if needed */
72 #ifdef DL_UNLOAD_ALL_AT_EXIT
73 call_atexit(&dl_unload_all_files, (void*)0);
78 /* SaveError() takes printf style args and saves the result in LastError */
80 SaveError(pTHX_ char* pat, ...)
87 /* This code is based on croak/warn, see mess() in util.c */
90 msv = vmess(pat, &args);
93 message = SvPV(msv,len);
94 len++; /* include terminating null char */
96 /* Allocate some memory for the error message */
98 LastError = (char*)saferealloc(LastError, len) ;
100 LastError = (char *) safemalloc(len) ;
102 /* Copy message into LastError (including terminating null char) */
103 strncpy(LastError, message, len) ;
104 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "DynaLoader: stored error msg '%s'\n",LastError));