Convert win32 to build DynaLoader with EU::MM in ext/DynaLoader
[p5sagit/p5-mst-13.2.git] / ext / DynaLoader / dl_dlopen.xs
index 9f4d9a7..dfa9d06 100644 (file)
            SaveError("%s",dlerror()) ;
 
    Note that SaveError() takes a printf format string. Use a "%s" as
-   the first parameter if the error may contain and % characters.
+   the first parameter if the error may contain any % characters.
 
 */
 
@@ -155,12 +155,13 @@ BOOT:
     (void)dl_private_init(aTHX);
 
 
-void *
+void
 dl_load_file(filename, flags=0)
     char *     filename
     int                flags
   PREINIT:
     int mode = RTLD_LAZY;
+    void *handle;
   CODE:
 {
 #if defined(DLOPEN_WONT_DO_RELATIVE_PATHS)
@@ -174,8 +175,11 @@ dl_load_file(filename, flags=0)
     }
 #endif
 #ifdef RTLD_NOW
-    if (dl_nonlazy)
-       mode = RTLD_NOW;
+    {
+       dMY_CXT;
+       if (dl_nonlazy)
+           mode = RTLD_NOW;
+    }
 #endif
     if (flags & 0x01)
 #ifdef RTLD_GLOBAL
@@ -184,13 +188,13 @@ dl_load_file(filename, flags=0)
        Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
 #endif
     DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
-    RETVAL = dlopen(filename, mode) ;
-    DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%lx\n", (unsigned long) RETVAL));
+    handle = dlopen(filename, mode) ;
+    DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%lx\n", (unsigned long) handle));
     ST(0) = sv_newmortal() ;
-    if (RETVAL == NULL)
+    if (handle == NULL)
        SaveError(aTHX_ "%s",dlerror()) ;
     else
-       sv_setiv( ST(0), PTR2IV(RETVAL));
+       sv_setiv( ST(0), PTR2IV(handle));
 }
 
 
@@ -198,19 +202,21 @@ int
 dl_unload_file(libref)
     void *     libref
   CODE:
-    DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", libref));
+    DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", PTR2ul(libref)));
     RETVAL = (dlclose(libref) == 0 ? 1 : 0);
-    if (!RETVAL);
+    if (!RETVAL)
         SaveError(aTHX_ "%s", dlerror()) ;
     DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL));
   OUTPUT:
     RETVAL
 
 
-void *
+void
 dl_find_symbol(libhandle, symbolname)
     void *     libhandle
     char *     symbolname
+    PREINIT:
+    void *sym;
     CODE:
 #ifdef DLSYM_NEEDS_UNDERSCORE
     symbolname = Perl_form_nocontext("_%s", symbolname);
@@ -218,19 +224,19 @@ dl_find_symbol(libhandle, symbolname)
     DLDEBUG(2, PerlIO_printf(Perl_debug_log,
                             "dl_find_symbol(handle=%lx, symbol=%s)\n",
                             (unsigned long) libhandle, symbolname));
-    RETVAL = dlsym(libhandle, symbolname);
+    sym = dlsym(libhandle, symbolname);
     DLDEBUG(2, PerlIO_printf(Perl_debug_log,
-                            "  symbolref = %lx\n", (unsigned long) RETVAL));
+                            "  symbolref = %lx\n", (unsigned long) sym));
     ST(0) = sv_newmortal() ;
-    if (RETVAL == NULL)
+    if (sym == NULL)
        SaveError(aTHX_ "%s",dlerror()) ;
     else
-       sv_setiv( ST(0), PTR2IV(RETVAL));
+       sv_setiv( ST(0), PTR2IV(sym));
 
 
 void
 dl_undef_symbols()
-    PPCODE:
+    CODE:
 
 
 
@@ -240,20 +246,37 @@ void
 dl_install_xsub(perl_name, symref, filename="$Package")
     char *             perl_name
     void *             symref 
-    char *             filename
+    const char *       filename
     CODE:
-    DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%lx)\n",
-               perl_name, (unsigned long) symref));
-    ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
-                                       (void(*)(pTHX_ CV *))symref,
-                                       filename)));
+    DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%"UVxf")\n",
+               perl_name, PTR2UV(symref)));
+    ST(0) = sv_2mortal(newRV((SV*)newXS_flags(perl_name,
+                                             DPTR2FPTR(XSUBADDR_t, symref),
+                                             filename, NULL,
+                                             XS_DYNAMIC_FILENAME)));
 
 
 char *
 dl_error()
     CODE:
-    RETVAL = LastError ;
+    dMY_CXT;
+    RETVAL = dl_last_error ;
     OUTPUT:
     RETVAL
 
+#if defined(USE_ITHREADS)
+
+void
+CLONE(...)
+    CODE:
+    MY_CXT_CLONE;
+
+    /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid
+     * using Perl variables that belong to another thread, we create our 
+     * own for this thread.
+     */
+    MY_CXT.x_dl_last_error = newSVpvn("", 0);
+
+#endif
+
 # end.