Make ReadFile and WriteFile use exception handler instead of IsBadReadPtr

Dmitry Timoshkov dmitry at baikal.ru
Thu Nov 18 09:35:48 CST 2004


Hello,

using IsBadReadPtr in ReadFile is useless in any case, but scanning
the whole memory block twice and using an exception handler inside
of IsBadReadPtr makes the things a bit not consistent. So, I decided
to convert them to use exception handler, that should make them slightly
faster by excluding job of IsBadReadPtr.

I had to reformat function bodies to make them readable after the change.

Changelog:
    Dmitry Timoshkov <dmitry at codeweavers.com>
    Make ReadFile and WriteFile use exception handler instead of IsBadReadPtr.

--- cvs/hq/wine/dlls/kernel/file.c	2004-10-12 08:51:46.000000000 +0900
+++ wine/dlls/kernel/file.c	2004-11-18 23:03:25.000000000 +0800
@@ -351,6 +351,7 @@ BOOL WINAPI ReadFile( HANDLE hFile, LPVO
     PIO_STATUS_BLOCK    io_status = &iosb;
     HANDLE              hEvent = 0;
     NTSTATUS            status;
+    BOOL ret = TRUE;
 
     TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToRead,
           bytesRead, overlapped );
@@ -358,36 +359,43 @@ BOOL WINAPI ReadFile( HANDLE hFile, LPVO
     if (bytesRead) *bytesRead = 0;  /* Do this before anything else */
     if (!bytesToRead) return TRUE;
 
-    if (IsBadReadPtr(buffer, bytesToRead))
-    {
-        SetLastError(ERROR_WRITE_FAULT); /* FIXME */
-        return FALSE;
-    }
-    if (is_console_handle(hFile))
-        return ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
-
-    if (overlapped != NULL)
+    __TRY
     {
-        offset.u.LowPart = overlapped->Offset;
-        offset.u.HighPart = overlapped->OffsetHigh;
-        poffset = &offset;
-        hEvent = overlapped->hEvent;
-        io_status = (PIO_STATUS_BLOCK)overlapped;
-    }
-    io_status->u.Status = STATUS_PENDING;
-    io_status->Information = 0;
+        if (is_console_handle(hFile))
+            ret = ReadConsoleA(hFile, buffer, bytesToRead, bytesRead, NULL);
+        else
+        {
+            if (overlapped != NULL)
+            {
+                offset.u.LowPart = overlapped->Offset;
+                offset.u.HighPart = overlapped->OffsetHigh;
+                poffset = &offset;
+                hEvent = overlapped->hEvent;
+                io_status = (PIO_STATUS_BLOCK)overlapped;
+            }
+            io_status->u.Status = STATUS_PENDING;
+            io_status->Information = 0;
 
-    status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
+            status = NtReadFile(hFile, hEvent, NULL, NULL, io_status, buffer, bytesToRead, poffset, NULL);
 
-    if (status != STATUS_PENDING && bytesRead)
-        *bytesRead = io_status->Information;
+            if (status != STATUS_PENDING && bytesRead)
+                *bytesRead = io_status->Information;
 
-    if (status && status != STATUS_END_OF_FILE)
+            if (status && status != STATUS_END_OF_FILE)
+            {
+                SetLastError( RtlNtStatusToDosError(status) );
+                ret = FALSE;
+            }
+        }
+    }
+    __EXCEPT(page_fault)
     {
-        SetLastError( RtlNtStatusToDosError(status) );
+        SetLastError( ERROR_INVALID_HANDLE );
         return FALSE;
     }
-    return TRUE;
+    __ENDTRY
+
+    return ret;
 }
 
 
@@ -435,39 +443,45 @@ BOOL WINAPI WriteFile( HANDLE hFile, LPC
     NTSTATUS status;
     IO_STATUS_BLOCK iosb;
     PIO_STATUS_BLOCK piosb = &iosb;
+    BOOL ret = TRUE;
 
     TRACE("%p %p %ld %p %p\n", hFile, buffer, bytesToWrite, bytesWritten, overlapped );
 
-    if (is_console_handle(hFile))
-        return WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
-
-    if (IsBadReadPtr(buffer, bytesToWrite))
+    __TRY
     {
-        SetLastError(ERROR_READ_FAULT); /* FIXME */
-        return FALSE;
-    }
+        if (is_console_handle(hFile))
+            ret = WriteConsoleA(hFile, buffer, bytesToWrite, bytesWritten, NULL);
+        else
+        {
+            if (overlapped)
+            {
+                offset.u.LowPart = overlapped->Offset;
+                offset.u.HighPart = overlapped->OffsetHigh;
+                poffset = &offset;
+                hEvent = overlapped->hEvent;
+                piosb = (PIO_STATUS_BLOCK)overlapped;
+            }
+            piosb->u.Status = STATUS_PENDING;
+            piosb->Information = 0;
 
-    if (overlapped)
-    {
-        offset.u.LowPart = overlapped->Offset;
-        offset.u.HighPart = overlapped->OffsetHigh;
-        poffset = &offset;
-        hEvent = overlapped->hEvent;
-        piosb = (PIO_STATUS_BLOCK)overlapped;
+            status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
+                                 buffer, bytesToWrite, poffset, NULL);
+            if (status)
+            {
+                SetLastError( RtlNtStatusToDosError(status) );
+                ret = FALSE;
+            }
+            if (bytesWritten) *bytesWritten = piosb->Information;
+        }
     }
-    piosb->u.Status = STATUS_PENDING;
-    piosb->Information = 0;
-
-    status = NtWriteFile(hFile, hEvent, NULL, NULL, piosb,
-                         buffer, bytesToWrite, poffset, NULL);
-    if (status)
+    __EXCEPT(page_fault)
     {
-        SetLastError( RtlNtStatusToDosError(status) );
+        SetLastError( ERROR_INVALID_HANDLE );
         return FALSE;
     }
-    if (bytesWritten) *bytesWritten = piosb->Information;
+    __ENDTRY
 
-    return TRUE;
+    return ret;
 }
 
 






More information about the wine-patches mailing list