Authors: Gavriel State <gavriels@corel.com>, Ulrich Czekalla <ulrichc@corel.com>
Added support for memory mapping of 4k aligned PE binaries.  This can
speed up load times significantly for some applications.

diff --git a/loader/module.c b/loader/module.c
index 22150ef..79ff9bb 100644
--- a/loader/module.c
+++ b/loader/module.c
@@ -109,8 +109,7 @@
         return TRUE;
 
 
-    TRACE("(%s,%s,%p) - CALL\n", 
-          wm->modname, typeName[type], lpReserved );
+    TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
 
     /* Call the initialization routine */
     switch ( wm->type )
@@ -129,8 +128,10 @@
         break;
     }
 
-    TRACE("(%s,%s,%p) - RETURN %d\n", 
-          wm->modname, typeName[type], lpReserved, retv );
+    /* The state of the module list may have changed due to the call
+       to PE_InitDLL. We cannot assume that this module has not been
+       deleted.  */
+    TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
 
     return retv;
 }
@@ -1378,10 +1379,13 @@
 		 */
 		switch(wm->type)
 		{
-		case MODULE32_PE:	PE_UnloadLibrary(wm);		break;
+                case MODULE32_PE:       if ( !(wm->flags & WINE_MODREF_INTERNAL) )
+                                               PE_UnloadLibrary(wm);
+                                        else
+                                               BUILTIN32_UnloadLibrary(wm);
+					break;
 		case MODULE32_ELF:	ELF_UnloadLibrary(wm);		break;
 		case MODULE32_ELFDLL:	ELFDLL_UnloadLibrary(wm);	break;
-		case MODULE32_BI:	BUILTIN32_UnloadLibrary(wm);	break;
 
 		default:
 			ERR("Invalid or unhandled MODREF type %d encountered (wm=%p)\n", wm->type, wm);
@@ -1462,12 +1466,12 @@
         MODULE_DllProcessDetach( FALSE, NULL );
         req->base = (void *)wm->module;
         server_call_noerr( REQ_UNLOAD_DLL );
+    
+        MODULE_FlushModrefs();
     }
 
     TRACE("END\n");
 
-    MODULE_FlushModrefs();
-
     return TRUE;
 }