+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_ITHREADS
+ MUTEX_LOCK(&PL_perlio_mutex);
+#endif
+ if (PL_perlio_fd_refcnt_size /* Assuming initial size of zero. */
+ && PL_perlio_fd_refcnt) {
+#ifdef PERL_TRACK_MEMPOOL
+ Malloc_t ptr = (Malloc_t)((char*)PL_perlio_fd_refcnt-sTHX);
+ struct perl_memory_debug_header *const header
+ = (struct perl_memory_debug_header *)ptr;
+ /* Only the thread that allocated us can free us. */
+ if (header->interpreter == aTHX)
+#endif
+ {
+ 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_ITHREADS
+ MUTEX_UNLOCK(&PL_perlio_mutex);
+#endif
+}
+