]> gitweb.factorcode.org Git - factor.git/commitdiff
io.launcher.windows.nt: don't call duplicate-handle, and fix memory leak; io.backend...
authorSlava Pestov <slava@factorcode.org>
Tue, 18 Aug 2009 08:46:46 +0000 (03:46 -0500)
committerSlava Pestov <slava@factorcode.org>
Tue, 18 Aug 2009 08:46:46 +0000 (03:46 -0500)
basis/io/backend/windows/windows.factor
basis/io/files/windows/windows.factor
basis/io/launcher/windows/nt/nt-tests.factor
basis/io/launcher/windows/nt/nt.factor
basis/io/launcher/windows/nt/test/input.txt [new file with mode: 0755]

index 2e9aac2ac9deb30de09baf4aa30f9aa312d51eae..fde5cf9b12bd12131c1df5ca99e868226dc50b3c 100755 (executable)
@@ -4,23 +4,36 @@ USING: alien alien.c-types arrays destructors io io.backend
 io.buffers io.files io.ports io.binary io.timeouts system
 strings kernel math namespaces sequences windows.errors
 windows.kernel32 windows.shell32 windows.types windows.winsock
-splitting continuations math.bitwise accessors ;
+splitting continuations math.bitwise accessors init sets assocs ;
 IN: io.backend.windows
 
+: win32-handles ( -- assoc )
+    \ win32-handles [ H{ } clone ] initialize-alien ;
+
+TUPLE: win32-handle < identity-tuple handle disposed ;
+
+M: win32-handle hashcode* handle>> hashcode* ;
+
 : set-inherit ( handle ? -- )
-    [ HANDLE_FLAG_INHERIT ] dip
+    [ handle>> HANDLE_FLAG_INHERIT ] dip
     >BOOLEAN SetHandleInformation win32-error=0/f ;
 
-TUPLE: win32-handle handle disposed ;
-
 : new-win32-handle ( handle class -- win32-handle )
-    new swap [ >>handle ] [ f set-inherit ] bi ;
+    new swap >>handle
+    dup f set-inherit
+    dup win32-handles conjoin ;
 
 : <win32-handle> ( handle -- win32-handle )
     win32-handle new-win32-handle ;
 
+ERROR: disposing-twice ;
+
+: unregister-handle ( handle -- )
+    win32-handles delete-at*
+    [ t >>disposed drop ] [ disposing-twice ] if ;
+
 M: win32-handle dispose* ( handle -- )
-    handle>> CloseHandle drop ;
+    [ unregister-handle ] [ handle>> CloseHandle win32-error=0/f ] bi ;
 
 TUPLE: win32-file < win32-handle ptr ;
 
index 444ba98c7ded16e78ad363d9890f7a88a0ec0f48..43463bd3f109d25f538f2da6c7d75ec78a42cc90 100755 (executable)
@@ -47,10 +47,8 @@ IN: io.files.windows
     GetLastError ERROR_ALREADY_EXISTS = not ;
 
 : set-file-pointer ( handle length method -- )
-    [ dupd d>w/w <uint> ] dip SetFilePointer
-    INVALID_SET_FILE_POINTER = [
-        CloseHandle "SetFilePointer failed" throw
-    ] when drop ;
+    [ [ handle>> ] dip d>w/w <uint> ] dip SetFilePointer
+    INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
 
 HOOK: open-append os ( path -- win32-file )
 
index 4587556e0c2692710c5b39ce3a191106e5666d72..f57f7b6d478a57db28d9156f9bf59f822b1fbaff 100755 (executable)
@@ -164,4 +164,19 @@ IN: io.launcher.windows.nt.tests
     "append-test" temp-file ascii file-contents
 ] unit-test
 
+[ "( scratchpad ) " ] [
+    console-vm "-run=listener" 2array
+    ascii [ "USE: system 0 exit" print flush readln ] with-process-stream
+] unit-test
+
+[ ] [
+    console-vm "-run=listener" 2array
+    ascii [ "USE: system 0 exit" print ] with-process-writer
+] unit-test
 
+[ ] [
+    <process>
+    console-vm "-run=listener" 2array >>command
+    "vocab:io/launcher/windows/nt/test/input.txt" >>stdin
+    try-process
+] unit-test
index 5ebb38abc27c599921aab563b3d778ea725ee581..e62373cbd7a9ee0def201fbadfead900a2092b63 100755 (executable)
@@ -10,21 +10,21 @@ IN: io.launcher.windows.nt
 
 : duplicate-handle ( handle -- handle' )
     GetCurrentProcess ! source process
-    swap ! handle
+    swap handle>> ! handle
     GetCurrentProcess ! target process
     f <void*> [ ! target handle
         DUPLICATE_SAME_ACCESS ! desired access
         TRUE ! inherit handle
-        DUPLICATE_CLOSE_SOURCE ! options
+        0 ! options
         DuplicateHandle win32-error=0/f
-    ] keep *void* ;
+    ] keep *void* <win32-handle> &dispose ;
 
 ! /dev/null simulation
 : null-input ( -- pipe )
-    (pipe) [ in>> handle>> ] [ out>> dispose ] bi ;
+    (pipe) [ in>> &dispose ] [ out>> dispose ] bi ;
 
 : null-output ( -- pipe )
-    (pipe) [ in>> dispose ] [ out>> handle>> ] bi ;
+    (pipe) [ in>> dispose ] [ out>> &dispose ] bi ;
 
 : null-pipe ( mode -- pipe )
     {
@@ -49,7 +49,7 @@ IN: io.launcher.windows.nt
     create-mode
     FILE_ATTRIBUTE_NORMAL ! flags and attributes
     f ! template file
-    CreateFile dup invalid-handle? <win32-file> &dispose handle>> ;
+    CreateFile dup invalid-handle? <win32-file> &dispose ;
 
 : redirect-append ( path access-mode create-mode -- handle )
     [ path>> ] 2dip
@@ -58,10 +58,10 @@ IN: io.launcher.windows.nt
     dup 0 FILE_END set-file-pointer ;
 
 : redirect-handle ( handle access-mode create-mode -- handle )
-    2drop handle>> duplicate-handle ;
+    2drop ;
 
 : redirect-stream ( stream access-mode create-mode -- handle )
-    [ underlying-handle handle>> ] 2dip redirect-handle ;
+    [ underlying-handle ] 2dip redirect-handle ;
 
 : redirect ( obj access-mode create-mode -- handle )
     {
@@ -72,7 +72,7 @@ IN: io.launcher.windows.nt
         { [ pick win32-file? ] [ redirect-handle ] }
         [ redirect-stream ]
     } cond
-    dup [ dup t set-inherit ] when ;
+    dup [ dup t set-inherit handle>> ] when ;
 
 : redirect-stdout ( process args -- handle )
     drop
diff --git a/basis/io/launcher/windows/nt/test/input.txt b/basis/io/launcher/windows/nt/test/input.txt
new file mode 100755 (executable)
index 0000000..99c3cc6
--- /dev/null
@@ -0,0 +1 @@
+USE: system 0 exit\r