From 231674d824f63a338f8f71e1e05dac3651b09e9c Mon Sep 17 00:00:00 2001
From: Alexandre Julliard <julliard@winehq.org>
Date: Wed, 9 Aug 2000 22:30:18 +0000
Subject: [PATCH] Removed the DOS handles table from the PDB and made it a
 static variable.

---
 files/file.c       | 65 ++++++++++++++++++----------------------------
 include/file.h     |  1 -
 loader/dos/dosvm.c |  2 --
 3 files changed, 25 insertions(+), 43 deletions(-)

diff --git a/files/file.c b/files/file.c
index ad9e86bcade..3ab0cf780da 100644
--- a/files/file.c
+++ b/files/file.c
@@ -42,7 +42,6 @@
 #include "heap.h"
 #include "msdos.h"
 #include "ldt.h"
-#include "process.h"
 #include "task.h"
 #include "wincon.h"
 #include "debugtools.h"
@@ -58,6 +57,8 @@ DEFAULT_DEBUG_CHANNEL(file);
 /* Size of per-process table of DOS handles */
 #define DOS_TABLE_SIZE 256
 
+static HANDLE dos_handles[DOS_TABLE_SIZE];
+
 
 /***********************************************************************
  *              FILE_ConvertOFMode
@@ -990,19 +991,13 @@ HFILE WINAPI OpenFile( LPCSTR name, OFSTRUCT *ofs, UINT mode )
  * Allocates the default DOS handles for a process. Called either by
  * AllocDosHandle below or by the DOSVM stuff.
  */
-BOOL FILE_InitProcessDosHandles( void ) {
-	HANDLE *ptr;
-
-        if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
-                               sizeof(*ptr) * DOS_TABLE_SIZE )))
-            return FALSE;
-        PROCESS_Current()->dos_handles = ptr;
-        ptr[0] = GetStdHandle(STD_INPUT_HANDLE);
-        ptr[1] = GetStdHandle(STD_OUTPUT_HANDLE);
-        ptr[2] = GetStdHandle(STD_ERROR_HANDLE);
-        ptr[3] = GetStdHandle(STD_ERROR_HANDLE);
-        ptr[4] = GetStdHandle(STD_ERROR_HANDLE);
-	return TRUE;
+static void FILE_InitProcessDosHandles( void )
+{
+    dos_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
+    dos_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
+    dos_handles[2] = GetStdHandle(STD_ERROR_HANDLE);
+    dos_handles[3] = GetStdHandle(STD_ERROR_HANDLE);
+    dos_handles[4] = GetStdHandle(STD_ERROR_HANDLE);
 }
 
 /***********************************************************************
@@ -1014,25 +1009,17 @@ BOOL FILE_InitProcessDosHandles( void ) {
 HFILE16 FILE_AllocDosHandle( HANDLE handle )
 {
     int i;
-    HANDLE *ptr = PROCESS_Current()->dos_handles;
 
     if (!handle || (handle == INVALID_HANDLE_VALUE))
         return INVALID_HANDLE_VALUE16;
 
-    if (!ptr) {
-    	if (!FILE_InitProcessDosHandles())
-	    goto error;
-	ptr = PROCESS_Current()->dos_handles;
-    }
-
-    for (i = 0; i < DOS_TABLE_SIZE; i++, ptr++)
-        if (!*ptr)
+    for (i = 5; i < DOS_TABLE_SIZE; i++)
+        if (!dos_handles[i])
         {
-            *ptr = handle;
+            dos_handles[i] = handle;
             TRACE("Got %d for h32 %d\n", i, handle );
             return i;
         }
-error:
     CloseHandle( handle );
     SetLastError( ERROR_TOO_MANY_OPEN_FILES );
     return INVALID_HANDLE_VALUE16;
@@ -1046,13 +1033,13 @@ error:
  */
 HANDLE FILE_GetHandle( HFILE16 hfile )
 {
-    HANDLE *table = PROCESS_Current()->dos_handles;
-    if ((hfile >= DOS_TABLE_SIZE) || !table || !table[hfile])
+    if (hfile < 5 && !dos_handles[hfile]) FILE_InitProcessDosHandles();
+    if ((hfile >= DOS_TABLE_SIZE) || !dos_handles[hfile])
     {
         SetLastError( ERROR_INVALID_HANDLE );
         return INVALID_HANDLE_VALUE;
     }
-    return table[hfile];
+    return dos_handles[hfile];
 }
 
 
@@ -1063,11 +1050,11 @@ HANDLE FILE_GetHandle( HFILE16 hfile )
  */
 HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
 {
-    HANDLE *table = PROCESS_Current()->dos_handles;
     HANDLE new_handle;
 
-    if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) ||
-        !table || !table[hFile1])
+    if (hFile1 < 5 && !dos_handles[hFile1]) FILE_InitProcessDosHandles();
+
+    if ((hFile1 >= DOS_TABLE_SIZE) || (hFile2 >= DOS_TABLE_SIZE) || !dos_handles[hFile1])
     {
         SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
@@ -1078,12 +1065,12 @@ HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
         SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
     }
-    if (!DuplicateHandle( GetCurrentProcess(), table[hFile1],
+    if (!DuplicateHandle( GetCurrentProcess(), dos_handles[hFile1],
                           GetCurrentProcess(), &new_handle,
                           0, FALSE, DUPLICATE_SAME_ACCESS ))
         return HFILE_ERROR16;
-    if (table[hFile2]) CloseHandle( table[hFile2] );
-    table[hFile2] = new_handle;
+    if (dos_handles[hFile2]) CloseHandle( dos_handles[hFile2] );
+    dos_handles[hFile2] = new_handle;
     return hFile2;
 }
 
@@ -1093,22 +1080,20 @@ HFILE16 FILE_Dup2( HFILE16 hFile1, HFILE16 hFile2 )
  */
 HFILE16 WINAPI _lclose16( HFILE16 hFile )
 {
-    HANDLE *table = PROCESS_Current()->dos_handles;
-
     if (hFile < 5)
     {
         FIXME("stdio handle closed, need proper conversion\n" );
         SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
     }
-    if ((hFile >= DOS_TABLE_SIZE) || !table || !table[hFile])
+    if ((hFile >= DOS_TABLE_SIZE) || !dos_handles[hFile])
     {
         SetLastError( ERROR_INVALID_HANDLE );
         return HFILE_ERROR16;
     }
-    TRACE("%d (handle32=%d)\n", hFile, table[hFile] );
-    CloseHandle( table[hFile] );
-    table[hFile] = 0;
+    TRACE("%d (handle32=%d)\n", hFile, dos_handles[hFile] );
+    CloseHandle( dos_handles[hFile] );
+    dos_handles[hFile] = 0;
     return 0;
 }
 
diff --git a/include/file.h b/include/file.h
index 91c084d768f..9cca731c46e 100644
--- a/include/file.h
+++ b/include/file.h
@@ -46,7 +46,6 @@ extern LPVOID FILE_dommap( int unix_handle, LPVOID start,
                            int prot, int flags );
 extern int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low );
 extern HFILE16 FILE_AllocDosHandle( HANDLE handle );
-extern BOOL FILE_InitProcessDosHandles( void );
 extern HANDLE FILE_GetHandle( HFILE16 hfile );
 
 /* files/directory.c */
diff --git a/loader/dos/dosvm.c b/loader/dos/dosvm.c
index 0dea056bcef..790b5d33276 100644
--- a/loader/dos/dosvm.c
+++ b/loader/dos/dosvm.c
@@ -464,8 +464,6 @@ int DOSVM_Enter( CONTEXT86 *context )
     VM86.regs.eflags |= VIF_MASK;
  } else {
 /* initial setup */
-  /* allocate standard DOS handles */
-  FILE_InitProcessDosHandles(); 
   /* registers */
   memset(&VM86,0,sizeof(VM86));
   VM86.regs.cs=lpDosTask->init_cs;
-- 
GitLab