X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=win32%2Fperllib.c;h=87b79c031dbcde5e3fb8e0a45bfbe0228690c6bd;hb=16b7a9a47be196cb33bf757faad24e73ceffc2fc;hp=1b8516c5028a8d9017277265442bac5386203002;hpb=1d7c184104c076988718a01b77c8706aae05b092;p=p5sagit%2Fp5-mst-13.2.git diff --git a/win32/perllib.c b/win32/perllib.c index 1b8516c..87b79c0 100644 --- a/win32/perllib.c +++ b/win32/perllib.c @@ -1,8 +1,7 @@ /* * "The Road goes ever on and on, down from the door where it began." */ - - +#define PERLIO_NOT_STDIO 0 #include "EXTERN.h" #include "perl.h" @@ -87,8 +86,6 @@ perl_get_host_info(struct IPerlMemInfo* perlMemInfo, } } -#ifdef PERL_OBJECT - EXTERN_C PerlInterpreter* perl_alloc_override(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared, struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv, @@ -97,33 +94,26 @@ perl_alloc_override(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared, struct IPerlProc** ppProc) { PerlInterpreter *my_perl = NULL; - try - { - CPerlHost* pHost = new CPerlHost(ppMem, ppMemShared, ppMemParse, ppEnv, - ppStdIO, ppLIO, ppDir, ppSock, ppProc); - - if (pHost) { - my_perl = perl_alloc_using(pHost->m_pHostperlMem, - pHost->m_pHostperlMemShared, - pHost->m_pHostperlMemParse, - pHost->m_pHostperlEnv, - pHost->m_pHostperlStdIO, - pHost->m_pHostperlLIO, - pHost->m_pHostperlDir, - pHost->m_pHostperlSock, - pHost->m_pHostperlProc); - if (my_perl) { - CPerlObj* pPerl = (CPerlObj*)my_perl; - w32_internal_host = pHost; - } + CPerlHost* pHost = new CPerlHost(ppMem, ppMemShared, ppMemParse, ppEnv, + ppStdIO, ppLIO, ppDir, ppSock, ppProc); + + if (pHost) { + my_perl = perl_alloc_using(pHost->m_pHostperlMem, + pHost->m_pHostperlMemShared, + pHost->m_pHostperlMemParse, + pHost->m_pHostperlEnv, + pHost->m_pHostperlStdIO, + pHost->m_pHostperlLIO, + pHost->m_pHostperlDir, + pHost->m_pHostperlSock, + pHost->m_pHostperlProc); + if (my_perl) { +#ifdef PERL_OBJECT + CPerlObj* pPerl = (CPerlObj*)my_perl; +#endif + w32_internal_host = pHost; } } - catch(...) - { - win32_fprintf(stderr, "%s\n", "Error: Unable to allocate memory"); - my_perl = NULL; - } - return my_perl; } @@ -131,35 +121,37 @@ EXTERN_C PerlInterpreter* perl_alloc(void) { PerlInterpreter* my_perl = NULL; - try - { - CPerlHost* pHost = new CPerlHost(); - if (pHost) { - my_perl = perl_alloc_using(pHost->m_pHostperlMem, - pHost->m_pHostperlMemShared, - pHost->m_pHostperlMemParse, - pHost->m_pHostperlEnv, - pHost->m_pHostperlStdIO, - pHost->m_pHostperlLIO, - pHost->m_pHostperlDir, - pHost->m_pHostperlSock, - pHost->m_pHostperlProc); - if (my_perl) { - CPerlObj* pPerl = (CPerlObj*)my_perl; - w32_internal_host = pHost; - } + CPerlHost* pHost = new CPerlHost(); + if (pHost) { + my_perl = perl_alloc_using(pHost->m_pHostperlMem, + pHost->m_pHostperlMemShared, + pHost->m_pHostperlMemParse, + pHost->m_pHostperlEnv, + pHost->m_pHostperlStdIO, + pHost->m_pHostperlLIO, + pHost->m_pHostperlDir, + pHost->m_pHostperlSock, + pHost->m_pHostperlProc); + if (my_perl) { +#ifdef PERL_OBJECT + CPerlObj* pPerl = (CPerlObj*)my_perl; +#endif + w32_internal_host = pHost; } } - catch(...) - { - win32_fprintf(stderr, "%s\n", "Error: Unable to allocate memory"); - my_perl = NULL; - } - return my_perl; } EXTERN_C void +win32_delete_internal_host(void *h) +{ + CPerlHost *host = (CPerlHost*)h; + delete host; +} + +#ifdef PERL_OBJECT + +EXTERN_C void perl_construct(PerlInterpreter* my_perl) { CPerlObj* pPerl = (CPerlObj*)my_perl; @@ -171,10 +163,7 @@ perl_construct(PerlInterpreter* my_perl) { win32_fprintf(stderr, "%s\n", "Error: Unable to construct data structures"); - CPerlHost* pHost = (CPerlHost*)w32_internal_host; - Perl_free(); - delete pHost; - SetPerlInterpreter(NULL); + perl_free(my_perl); } } @@ -199,32 +188,30 @@ EXTERN_C void perl_free(PerlInterpreter* my_perl) { CPerlObj* pPerl = (CPerlObj*)my_perl; + void *host = w32_internal_host; #ifdef DEBUGGING - CPerlHost* pHost = (CPerlHost*)w32_internal_host; Perl_free(); - delete pHost; #else try { - CPerlHost* pHost = (CPerlHost*)w32_internal_host; Perl_free(); - delete pHost; } catch(...) { } #endif - SetPerlInterpreter(NULL); + win32_delete_internal_host(host); + PERL_SET_THX(NULL); } EXTERN_C int perl_run(PerlInterpreter* my_perl) { CPerlObj* pPerl = (CPerlObj*)my_perl; + int retVal; #ifdef DEBUGGING - return Perl_run(); + retVal = Perl_run(); #else - int retVal; try { retVal = Perl_run(); @@ -234,8 +221,8 @@ perl_run(PerlInterpreter* my_perl) win32_fprintf(stderr, "Error: Runtime exception\n"); retVal = -1; } - return retVal; #endif + return retVal; } EXTERN_C int @@ -263,55 +250,16 @@ perl_parse(PerlInterpreter* my_perl, void (*xsinit)(CPerlObj*), int argc, char** #undef PL_perl_destruct_level #define PL_perl_destruct_level int dummy -#else /* !PERL_OBJECT */ - -EXTERN_C PerlInterpreter* -perl_alloc(void) -{ - PerlInterpreter *my_perl = NULL; - CPerlHost* pHost = new CPerlHost(); - if (pHost) { - my_perl = perl_alloc_using(pHost->m_pHostperlMem, - pHost->m_pHostperlMemShared, - pHost->m_pHostperlMemParse, - pHost->m_pHostperlEnv, - pHost->m_pHostperlStdIO, - pHost->m_pHostperlLIO, - pHost->m_pHostperlDir, - pHost->m_pHostperlSock, - pHost->m_pHostperlProc); - if (my_perl) { - w32_internal_host = pHost; - } - } - return my_perl; -} - #endif /* PERL_OBJECT */ #endif /* PERL_IMPLICIT_SYS */ EXTERN_C HANDLE w32_perldll_handle; -static DWORD g_TlsAllocIndex; - -EXTERN_C DllExport bool -SetPerlInterpreter(void *interp) -{ - return TlsSetValue(g_TlsAllocIndex, interp); -} - -EXTERN_C DllExport void* -GetPerlInterpreter(void) -{ - return TlsGetValue(g_TlsAllocIndex); -} - EXTERN_C DllExport int RunPerl(int argc, char **argv, char **env) { int exitstatus; PerlInterpreter *my_perl, *new_perl = NULL; - struct perl_thread *thr; #ifndef __BORLANDC__ /* XXX this _may_ be a problem on some compilers (e.g. Borland) that @@ -341,7 +289,7 @@ RunPerl(int argc, char **argv, char **env) if (!(my_perl = perl_alloc())) return (1); - perl_construct( my_perl ); + perl_construct(my_perl); PL_perl_destruct_level = 0; exitstatus = perl_parse(my_perl, xs_init, argc, argv, env); @@ -364,18 +312,18 @@ RunPerl(int argc, char **argv, char **env) # else new_perl = perl_clone(my_perl, 1); # endif - exitstatus = perl_run( new_perl ); - SetPerlInterpreter(my_perl); + exitstatus = perl_run(new_perl); + PERL_SET_THX(my_perl); #else - exitstatus = perl_run( my_perl ); + exitstatus = perl_run(my_perl); #endif } - perl_destruct( my_perl ); - perl_free( my_perl ); + perl_destruct(my_perl); + perl_free(my_perl); #ifdef USE_ITHREADS if (new_perl) { - SetPerlInterpreter(new_perl); + PERL_SET_THX(new_perl); perl_destruct(new_perl); perl_free(new_perl); } @@ -386,6 +334,16 @@ RunPerl(int argc, char **argv, char **env) return (exitstatus); } +EXTERN_C void +set_w32_module_name(void); + +EXTERN_C void +EndSockets(void); + + +#ifdef __MINGW32__ +EXTERN_C /* GCC in C++ mode mangles the name, otherwise */ +#endif BOOL APIENTRY DllMain(HANDLE hModule, /* DLL module handle */ DWORD fdwReason, /* reason called */ @@ -403,16 +361,26 @@ DllMain(HANDLE hModule, /* DLL module handle */ setmode( fileno( stderr ), O_BINARY ); _fmode = O_BINARY; #endif - g_TlsAllocIndex = TlsAlloc(); - DisableThreadLibraryCalls(hModule); + DisableThreadLibraryCalls((HMODULE)hModule); w32_perldll_handle = hModule; + set_w32_module_name(); break; /* The DLL is detaching from a process due to * process termination or call to FreeLibrary. */ case DLL_PROCESS_DETACH: - TlsFree(g_TlsAllocIndex); + /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill() + anything here had better be harmless if: + A. Not called at all. + B. Called after memory allocation for Heap has been forcibly removed by OS. + PerlIO_cleanup() was done here but fails (B). + */ + EndSockets(); +#if defined(USE_THREADS) || defined(USE_ITHREADS) + if (PL_curinterp) + FREE_THREAD_KEY; +#endif break; /* The attached process creates a new thread. */