This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More leak fixes, by Jarkko
authorRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Fri, 20 Oct 2006 10:20:18 +0000 (10:20 +0000)
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Fri, 20 Oct 2006 10:20:18 +0000 (10:20 +0000)
p4raw-id: //depot/perl@29060

dosish.h
perl.h
perlio.c
perliol.h
perlvars.h
unixish.h

index 0667b54..a5eeace 100644 (file)
--- a/dosish.h
+++ b/dosish.h
 #endif /* DJGPP */
 
 #ifndef PERL_SYS_TERM
+# ifdef USE_PERLIO
+#  define PERL_SYS_TERM() HINTS_REFCNT_TERM; OP_REFCNT_TERM; PERLIO_TERM; MALLOC_TERM
+# else
 #  define PERL_SYS_TERM() HINTS_REFCNT_TERM; OP_REFCNT_TERM; MALLOC_TERM
+# endif
 #endif
 #define dXSUB_SYS
 
diff --git a/perl.h b/perl.h
index be6dfca..bb6437d 100644 (file)
--- a/perl.h
+++ b/perl.h
@@ -3863,6 +3863,21 @@ typedef Sighandler_t Sigsave_t;
 # define RUNOPS_DEFAULT Perl_runops_standard
 #endif
 
+#ifdef USE_PERLIO
+void PerlIO_teardown(pTHX);
+# ifdef USE_THREADS
+#  define PERLIO_INIT MUTEX_INIT(&PL_perlio_mutex)
+#  define PERLIO_TERM                          \
+       STMT_START {                            \
+               PerlIO_teardown(aTHX);          \
+               MUTEX_DESTROY(&PL_perlio_mutex);\
+       } STMT_END
+# else
+#  define PERLIO_INIT
+#  define PERLIO_TERM  PerlIO_teardown(aTHX)
+# endif
+#endif
+
 #ifdef MYMALLOC
 #  ifdef MUTEX_INIT_CALLS_MALLOC
 #    define MALLOC_INIT                                        \
index 6434a9b..dbe1108 100644 (file)
--- a/perlio.c
+++ b/perlio.c
@@ -2253,13 +2253,9 @@ PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
     return f;
 }
 
-#ifdef USE_THREADS
-perl_mutex PerlIO_mutex;
-#endif
-
 /* PL_perlio_fd_refcnt[] is in intrpvar.h */
 
-/* Must be called with PerlIO_mutex locked.  */
+/* Must be called with PL_perlio_mutex locked (if under 5.005 threads).  */
 static void
 S_more_refcounted_fds(pTHX_ const int new_fd) {
     dVAR;
@@ -2281,7 +2277,7 @@ S_more_refcounted_fds(pTHX_ const int new_fd) {
 
     if (!new_array) {
 #ifdef USE_THREADS
-       MUTEX_UNLOCK(&PerlIO_mutex);
+       MUTEX_UNLOCK(&PL_perlio_mutex);
 #endif
        /* Can't use PerlIO to write as it allocates memory */
        PerlLIO_write(PerlIO_fileno(Perl_error_log),
@@ -2305,7 +2301,7 @@ PerlIO_init(pTHX)
 {
  /* Place holder for stdstreams call ??? */
 #ifdef USE_THREADS
-    MUTEX_INIT(&PerlIO_mutex);
+    MUTEX_INIT(&PL_perlio_mutex);
 #else
     PERL_UNUSED_CONTEXT;
 #endif
@@ -2319,7 +2315,7 @@ PerlIOUnix_refcnt_inc(int fd)
        dVAR;
 
 #ifdef USE_THREADS
-       MUTEX_LOCK(&PerlIO_mutex);
+       MUTEX_LOCK(&PL_perlio_mutex);
 #endif
        if (fd >= PL_perlio_fd_refcnt_size)
            S_more_refcounted_fds(aTHX_ fd);
@@ -2328,7 +2324,7 @@ PerlIOUnix_refcnt_inc(int fd)
        PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
 
 #ifdef USE_THREADS
-       MUTEX_UNLOCK(&PerlIO_mutex);
+       MUTEX_UNLOCK(&PL_perlio_mutex);
 #endif
     }
 }
@@ -2341,7 +2337,7 @@ PerlIOUnix_refcnt_dec(int fd)
     if (fd >= 0) {
        dVAR;
 #ifdef USE_THREADS
-       MUTEX_LOCK(&PerlIO_mutex);
+       MUTEX_LOCK(&PL_perlio_mutex);
 #endif
        /* XXX should this be a panic?  */
        if (fd >= PL_perlio_fd_refcnt_size)
@@ -2351,7 +2347,7 @@ PerlIOUnix_refcnt_dec(int fd)
        cnt = --PL_perlio_fd_refcnt[fd];
        PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
 #ifdef USE_THREADS
-       MUTEX_UNLOCK(&PerlIO_mutex);
+       MUTEX_UNLOCK(&PL_perlio_mutex);
 #endif
     }
     return cnt;
@@ -2368,19 +2364,6 @@ PerlIO_cleanup(pTHX)
     PerlIO_debug("Cleanup layers\n");
 #endif
 
-#ifdef DEBUGGING
-    {
-       /* By now all filehandles should have been closed, so any
-        * stray (non-STD-)filehandles indicate *possible* (PerlIO)
-        * errors. */
-       for (i = 3; i < PL_perlio_fd_refcnt_size; i++) {
-           if (PL_perlio_fd_refcnt[i])
-               PerlIO_debug("PerlIO_cleanup: fd %d refcnt=%d\n",
-                            i, PL_perlio_fd_refcnt[i]);
-       }
-    }
-#endif
-
     /* Raise STDIN..STDERR refcount so we don't close them */
     for (i=0; i < 3; i++)
        PerlIOUnix_refcnt_inc(i);
@@ -2397,8 +2380,25 @@ PerlIO_cleanup(pTHX)
        PerlIO_list_free(aTHX_ PL_def_layerlist);
        PL_def_layerlist = NULL;
     }
+}
+
+void PerlIO_teardown(pTHX) /* Call only from PERL_SYS_TERM(). */
+{
+#ifdef DEBUGGING
+    {
+       /* By now all filehandles should have been closed, so any
+        * stray (non-STD-)filehandles indicate *possible* (PerlIO)
+        * errors. */
+       int i;
+       for (i = 3; i < PL_perlio_fd_refcnt_size; i++) {
+           if (PL_perlio_fd_refcnt[i])
+               PerlIO_debug("PerlIO_cleanup: fd %d refcnt=%d\n",
+                            i, PL_perlio_fd_refcnt[i]);
+       }
+    }
+#endif
 #ifdef USE_THREADS
-    MUTEX_UNLOCK(&PerlIO_mutex);
+    MUTEX_LOCK(&PL_perlio_mutex);
 #endif
     if (PL_perlio_fd_refcnt_size /* Assuming initial size of zero. */
         && PL_perlio_fd_refcnt) {
@@ -2410,13 +2410,13 @@ PerlIO_cleanup(pTHX)
         if (header->interpreter == aTHX)
 #endif
            {
-               Safefree(PL_perlio_fd_refcnt);
+               PerlMemShared_free(PL_perlio_fd_refcnt); /* Not Safefree() because was allocated with PerlMemShared_realloc(). */
                PL_perlio_fd_refcnt = NULL;
                PL_perlio_fd_refcnt_size = 0;
            }
     }
 #ifdef USE_THREADS
-    MUTEX_UNLOCK(&PerlIO_mutex);
+    MUTEX_UNLOCK(&PL_perlio_mutex);
 #endif
 }
 
index c6da2b6..f062225 100644 (file)
--- a/perliol.h
+++ b/perliol.h
@@ -167,6 +167,7 @@ PERL_EXPORT_C PerlIO_list_t *PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE
 PERL_EXPORT_C void PerlIO_list_free(pTHX_ PerlIO_list_t *list);
 PERL_EXPORT_C void PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg);
 PERL_EXPORT_C void PerlIO_list_free(pTHX_ PerlIO_list_t *list);
+PERL_EXPORT_C void PerlIO_teardown(pTHX);
 
 /*--------------------------------------------------------------------------------------*/
 /* Generic, or stub layer functions */
index b4f3e51..c8706f5 100644 (file)
@@ -138,3 +138,7 @@ PERLVARI(Gmy_cxt_index, int, 0)
 #if defined(USE_ITHREADS)
 PERLVAR(Ghints_mutex, perl_mutex)    /* Mutex for refcounted he refcounting */
 #endif
+
+#if defined(USE_THREADS)       /* Yes, 5.005 threads - should be removed. */
+PERLVAR(Gperlio_mutex, perl_mutex)    /* Mutex for perlio fd refcounts */
+#endif
index 631a619..a08e8ba 100644 (file)
--- a/unixish.h
+++ b/unixish.h
 #endif
 
 #ifndef PERL_SYS_TERM
-#define PERL_SYS_TERM()                HINTS_REFCNT_TERM; OP_REFCNT_TERM; MALLOC_TERM
+# ifdef USE_PERLIO
+#  define PERL_SYS_TERM()              HINTS_REFCNT_TERM; OP_REFCNT_TERM; PERLIO_TERM; MALLOC_TERM
+# else
+#  define PERL_SYS_TERM()              HINTS_REFCNT_TERM; OP_REFCNT_TERM; MALLOC_TERM
+# endif
 #endif
 
 #define BIT_BUCKET "/dev/null"