This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get rid of the _() macro since (1) we require ANSI anyway
[perl5.git] / win32 / perlhost.h
index 475158f..d828885 100644 (file)
@@ -7,6 +7,8 @@
  *    License or the Artistic License, as specified in the README file.
  */
 
+#define CHECK_HOST_INTERP
+
 #ifndef ___PerlHost_H___
 #define ___PerlHost_H___
 
@@ -210,30 +212,42 @@ protected:
 
     DWORD   m_dwEnvCount;
     LPSTR*  m_lppEnvList;
-    BOOL    m_bTopLevel;       /* is this a toplevel host? */
+    BOOL    m_bTopLevel;       // is this a toplevel host?
     static long num_hosts;
 public:
     inline  int LastHost(void) { return num_hosts == 1L; };
+    struct interpreter *host_perl;
 };
 
 long CPerlHost::num_hosts = 0L;
 
+extern "C" void win32_checkTLS(struct interpreter *host_perl);
 
-#define STRUCT2PTR(x, y) (CPerlHost*)(((LPBYTE)x)-offsetof(CPerlHost, y))
+#define STRUCT2RAWPTR(x, y) (CPerlHost*)(((LPBYTE)x)-offsetof(CPerlHost, y))
+#ifdef CHECK_HOST_INTERP
+inline CPerlHost* CheckInterp(CPerlHost *host) 
+{
+ win32_checkTLS(host->host_perl);
+ return host;
+}
+#define STRUCT2PTR(x, y) CheckInterp(STRUCT2RAWPTR(x, y))
+#else
+#define STRUCT2PTR(x, y) STRUCT2RAWPTR(x, y)
+#endif
 
 inline CPerlHost* IPerlMem2Host(struct IPerlMem* piPerl)
 {
-    return STRUCT2PTR(piPerl, m_hostperlMem);
+    return STRUCT2RAWPTR(piPerl, m_hostperlMem);
 }
 
 inline CPerlHost* IPerlMemShared2Host(struct IPerlMem* piPerl)
 {
-    return STRUCT2PTR(piPerl, m_hostperlMemShared);
+    return STRUCT2RAWPTR(piPerl, m_hostperlMemShared);
 }
 
 inline CPerlHost* IPerlMemParse2Host(struct IPerlMem* piPerl)
 {
-    return STRUCT2PTR(piPerl, m_hostperlMemParse);
+    return STRUCT2RAWPTR(piPerl, m_hostperlMemParse);
 }
 
 inline CPerlHost* IPerlEnv2Host(struct IPerlEnv* piPerl)
@@ -1436,9 +1450,7 @@ PerlSockSocket(struct IPerlSock* piPerl, int af, int type, int protocol)
 int
 PerlSockSocketpair(struct IPerlSock* piPerl, int domain, int type, int protocol, int* fds)
 {
-    dTHX;
-    Perl_croak(aTHX_ "socketpair not implemented!\n");
-    return 0;
+    return Perl_my_socketpair(domain, type, protocol, fds);
 }
 
 int
@@ -1683,6 +1695,7 @@ win32_start_child(LPVOID arg)
 
 
     PERL_SET_THX(my_perl);
+    win32_checkTLS(my_perl);
 
     /* set $$ to pseudo id */
 #ifdef PERL_SYNC_FORK
@@ -1695,8 +1708,12 @@ win32_start_child(LPVOID arg)
            w32_pseudo_id = -pid;
     }
 #endif
-    if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
-       sv_setiv(GvSV(tmpgv), -(IV)w32_pseudo_id);
+    if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV)) {
+       SV *sv = GvSV(tmpgv);
+       SvREADONLY_off(sv);
+       sv_setiv(sv, -(IV)w32_pseudo_id);
+       SvREADONLY_on(sv);
+    }
     hv_clear(PL_pidstatus);
 
     /* push a zero on the stack (we are the child) */
@@ -1745,9 +1762,11 @@ restart:
        JMPENV_POP;
 
        /* XXX hack to avoid perl_destruct() freeing optree */
+        win32_checkTLS(my_perl);
        PL_main_root = Nullop;
     }
 
+    win32_checkTLS(my_perl);
     /* close the std handles to avoid fd leaks */
     {
        do_close(gv_fetchpv("STDIN", TRUE, SVt_PVIO), FALSE);
@@ -1756,7 +1775,9 @@ restart:
     }
 
     /* destroy everything (waits for any pseudo-forked children) */
+    win32_checkTLS(my_perl);
     perl_destruct(my_perl);
+    win32_checkTLS(my_perl);
     perl_free(my_perl);
 
 #ifdef PERL_SYNC_FORK
@@ -1793,6 +1814,7 @@ PerlProcFork(struct IPerlProc* piPerl)
                                                 h->m_pHostperlProc
                                                 );
     new_perl->Isys_intern.internal_host = h;
+    h->host_perl = new_perl;
 #  ifdef PERL_SYNC_FORK
     id = win32_start_child((LPVOID)new_perl);
     PERL_SET_THX(aTHX);
@@ -2051,7 +2073,7 @@ CPerlHost::CPerlHost(CPerlHost& host)
 
 CPerlHost::~CPerlHost(void)
 {
-/* Reset(); */
+//  Reset();
     InterlockedDecrement(&num_hosts);
     delete m_pvDir;
     m_pVMemParse->Release();
@@ -2078,7 +2100,7 @@ CPerlHost::Find(LPCSTR lpStr)
 
 int
 lookup(const void *arg1, const void *arg2)
-{   /* Compare strings */
+{   // Compare strings
     char*ptr1, *ptr2;
     char c1,c2;
 
@@ -2091,18 +2113,18 @@ lookup(const void *arg1, const void *arg2)
            if(c2 == '\0' || c2 == '=')
                break;
 
-           return -1; /* string 1 < string 2 */
+           return -1; // string 1 < string 2
        }
        else if(c2 == '\0' || c2 == '=')
-           return 1; /* string 1 > string 2 */
+           return 1; // string 1 > string 2
        else if(c1 != c2) {
            c1 = toupper(c1);
            c2 = toupper(c2);
            if(c1 != c2) {
                if(c1 < c2)
-                   return -1; /* string 1 < string 2 */
+                   return -1; // string 1 < string 2
 
-               return 1; /* string 1 > string 2 */
+               return 1; // string 1 > string 2
            }
        }
     }
@@ -2117,7 +2139,7 @@ CPerlHost::Lookup(LPCSTR lpStr)
 
 int
 compare(const void *arg1, const void *arg2)
-{   /* Compare strings */
+{   // Compare strings
     char*ptr1, *ptr2;
     char c1,c2;
 
@@ -2130,18 +2152,18 @@ compare(const void *arg1, const void *arg2)
            if(c1 == c2)
                break;
 
-           return -1; /* string 1 < string 2 */
+           return -1; // string 1 < string 2
        }
        else if(c2 == '\0' || c2 == '=')
-           return 1; /* string 1 > string 2 */
+           return 1; // string 1 > string 2
        else if(c1 != c2) {
            c1 = toupper(c1);
            c2 = toupper(c2);
            if(c1 != c2) {
                if(c1 < c2)
-                   return -1; /* string 1 < string 2 */
+                   return -1; // string 1 < string 2
        
-               return 1; /* string 1 > string 2 */
+               return 1; // string 1 > string 2
            }
        }
     }
@@ -2161,7 +2183,7 @@ CPerlHost::Add(LPCSTR lpStr)
 
     szBuffer[index] = '\0';
 
-    /* replacing ? */
+    // replacing ?
     lpPtr = Lookup(szBuffer);
     if(lpPtr != NULL) {
        Renew(*lpPtr, length, char);
@@ -2231,45 +2253,45 @@ CPerlHost::CreateLocalEnvironmentStrings(VDir &vDir)
     DWORD dwSize, dwEnvIndex;
     int nLength, compVal;
 
-    /* get the process environment strings */
+    // get the process environment strings
     lpAllocPtr = lpTmp = (LPSTR)GetEnvironmentStrings();
 
-    /* step over current directory stuff */
+    // step over current directory stuff
     while(*lpTmp == '=')
        lpTmp += strlen(lpTmp) + 1;
 
-    /* save the start of the environment strings */
+    // save the start of the environment strings
     lpEnvPtr = lpTmp;
     for(dwSize = 1; *lpTmp != '\0'; lpTmp += strlen(lpTmp) + 1) {
-       /* calculate the size of the environment strings */
+       // calculate the size of the environment strings
        dwSize += strlen(lpTmp) + 1;
     }
 
-    /* add the size of current directories */
+    // add the size of current directories
     dwSize += vDir.CalculateEnvironmentSpace();
 
-    /* add the additional space used by changes made to the environment */
+    // add the additional space used by changes made to the environment
     dwSize += CalculateEnvironmentSpace();
 
     New(1, lpStr, dwSize, char);
     lpPtr = lpStr;
     if(lpStr != NULL) {
-       /* build the local environment */
+       // build the local environment
        lpStr = vDir.BuildEnvironmentSpace(lpStr);
 
        dwEnvIndex = 0;
        lpLocalEnv = GetIndex(dwEnvIndex);
        while(*lpEnvPtr != '\0') {
            if(!lpLocalEnv) {
-               /* all environment overrides have been added */
-               /* so copy string into place */
+               // all environment overrides have been added
+               // so copy string into place
                strcpy(lpStr, lpEnvPtr);
                nLength = strlen(lpEnvPtr) + 1;
                lpStr += nLength;
                lpEnvPtr += nLength;
            }
            else {      
-               /* determine which string to copy next */
+               // determine which string to copy next
                compVal = compare(&lpEnvPtr, &lpLocalEnv);
                if(compVal < 0) {
                    strcpy(lpStr, lpEnvPtr);
@@ -2285,7 +2307,7 @@ CPerlHost::CreateLocalEnvironmentStrings(VDir &vDir)
                    }
                    lpLocalEnv = GetIndex(dwEnvIndex);
                    if(compVal == 0) {
-                       /* this string was replaced */
+                       // this string was replaced
                        lpEnvPtr += strlen(lpEnvPtr) + 1;
                    }
                }
@@ -2293,8 +2315,8 @@ CPerlHost::CreateLocalEnvironmentStrings(VDir &vDir)
        }
 
        while(lpLocalEnv) {
-           /* still have environment overrides to add */
-           /* so copy the strings into place if not an override */
+           // still have environment overrides to add
+           // so copy the strings into place if not an override
            char *ptr = strchr(lpLocalEnv, '=');
            if(ptr && ptr[1]) {
                strcpy(lpStr, lpLocalEnv);
@@ -2303,11 +2325,11 @@ CPerlHost::CreateLocalEnvironmentStrings(VDir &vDir)
            lpLocalEnv = GetIndex(dwEnvIndex);
        }
 
-       /* add final NULL */
+       // add final NULL
        *lpStr = '\0';
     }
 
-    /* release the process environment strings */
+    // release the process environment strings
     FreeEnvironmentStrings(lpAllocPtr);
 
     return lpPtr;
@@ -2412,3 +2434,4 @@ CPerlHost::Chdir(const char *dirname)
 }
 
 #endif /* ___PerlHost_H___ */
+