ExternalStream.st
branchjv
changeset 18786 237a87b4fe8f
parent 18678 a9b30d72dff9
parent 18783 a424e4025209
child 18800 02724cc719b6
--- a/ExternalStream.st	Wed Sep 30 07:05:15 2015 +0200
+++ b/ExternalStream.st	Thu Oct 01 06:52:24 2015 +0200
@@ -1,5 +1,3 @@
-"{ Encoding: utf8 }"
-
 "
  COPYRIGHT (c) 1988 by Claus Gittinger
 	      All Rights Reserved
@@ -2207,8 +2205,8 @@
      No error of the stream is not open."
 
     self isOpen ifTrue:[
-	Lobby unregister:self.
-	self closeFile.
+        self unregisterForFinalization.
+        self closeFile.
     ].
 !
 
@@ -2217,8 +2215,8 @@
      see comment there"
 
     self isOpen ifTrue:[
-	Lobby unregister:self.
-	self closeFile
+        self unregisterForFinalization.
+        self closeFile
     ]
 
     "Modified: 30.8.1996 / 00:39:21 / cg"
@@ -2234,7 +2232,7 @@
 
     copy := super copy.
     copy dupFd.
-    Lobby register:copy.
+    copy registerForFinalization.
     ^ copy
 
     "
@@ -2521,6 +2519,23 @@
     [
 	self closeFile
     ] valueWithTimeout:30 seconds.
+!
+
+reRegisterForFinalization
+    Lobby registerChange:self.
+!
+
+registerForFinalization
+
+    "keep myself in newSpace, so it will be finalized early"
+    ObjectMemory preventTenureOf:self. 
+    Lobby register:self.
+!
+
+unregisterForFinalization
+    Lobby unregister:self.
+     "probably the object will be collected soon..."
+    ObjectMemory setAgeOf:self to:0.
 ! !
 
 !ExternalStream methodsFor:'initialization'!
@@ -4595,59 +4610,59 @@
 
     if (__isStringLike(openmode)) {
 #ifdef WIN32
-	__stxWrapApiEnterCritical();
+        __stxWrapApiEnterCritical();
 #endif
-	if (__isSmallInteger(aFileDescriptor)) {
-	    fd = __intVal(aFileDescriptor);
-	}
+        if (__isSmallInteger(aFileDescriptor)) {
+            fd = __intVal(aFileDescriptor);
+        }
 #ifdef WIN32
-	else if (__isExternalAddressLike(aFileDescriptor)) {
-	    fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY);
-	    if (fd < 0) {
-		if (__threadErrno == 0) {
-		    // no more file descriptors
-		    __threadErrno = EMFILE;
-		}
-		error = __mkSmallInteger(__threadErrno);
-		__stxWrapApiLeaveCritical();
-		goto out;
-	    }
-	}
+        else if (__isExternalAddressLike(aFileDescriptor)) {
+            fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY);
+            if (fd < 0) {
+                if (__threadErrno == 0) {
+                    // no more file descriptors
+                    __threadErrno = EMFILE;
+                }
+                error = __mkSmallInteger(__threadErrno);
+                __stxWrapApiLeaveCritical();
+                goto out;
+            }
+        }
 #endif
-	f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode));
+        f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode));
 #ifdef WIN32
-	__stxWrapApiLeaveCritical();
+        __stxWrapApiLeaveCritical();
 #endif
-	if (f == NULL) {
-	    error =__mkSmallInteger(__threadErrno);
-	} else {
-	    if (@global(FileOpenTrace) == true) {
-		fprintf(stderr, "fdopen [ExternalStream] %"_ld_" (%"_lx_") -> %"_lx_"\n", (INT)fd, (INT)fd, (INT)f);
-	    }
-
-	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
-	    retVal = self;
-	}
+        if (f == NULL) {
+            error =__mkSmallInteger(__threadErrno);
+        } else {
+            if (@global(FileOpenTrace) == true) {
+                fprintf(stderr, "fdopen [ExternalStream] %"_ld_" (%"_lx_") -> %"_lx_"\n", (INT)fd, (INT)fd, (INT)f);
+            }
+
+            fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
+            retVal = self;
+        }
     }
 out:;
 %}.
     retVal notNil ifTrue:[
-	position := 0.
-	buffered isNil ifTrue:[
-	    buffered := true.       "default is buffered"
-	].
-	handleType := handleTypeSymbol.
-	Lobby register:self.
-	^ retVal
+        position := 0.
+        buffered isNil ifTrue:[
+            buffered := true.       "default is buffered"
+        ].
+        handleType := handleTypeSymbol.
+        self registerForFinalization.
+        ^ retVal
     ].
     error notNil ifTrue:[
-	OperatingSystem closeFd:aFileDescriptor.
-	lastErrorNumber := error.
-	position := nil.
-	"
-	 the open failed for some reason ...
-	"
-	^ self openError:error
+        OperatingSystem closeFd:aFileDescriptor.
+        lastErrorNumber := error.
+        position := nil.
+        "
+         the open failed for some reason ...
+        "
+        ^ self openError:error
     ].
 !
 
@@ -4696,42 +4711,42 @@
 # if 1
        f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
 # else
-	__BEGIN_INTERRUPTABLE__
-	do {
-	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
-	} while ((f == NULL) && (__threadErrno == EINTR));
-	__END_INTERRUPTABLE__
+        __BEGIN_INTERRUPTABLE__
+        do {
+            f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
+        } while ((f == NULL) && (__threadErrno == EINTR));
+        __END_INTERRUPTABLE__
 # endif
 #else /* UNIX */
-	__BEGIN_INTERRUPTABLE__
-	do {
-	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
-	} while ((f == NULL) && (__threadErrno == EINTR));
-	__END_INTERRUPTABLE__
+        __BEGIN_INTERRUPTABLE__
+        do {
+            f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
+        } while ((f == NULL) && (__threadErrno == EINTR));
+        __END_INTERRUPTABLE__
 #endif /* UNIX */
-	if (f == NULL) {
-	    error = __mkSmallInteger(__threadErrno);
-	} else {
-	    if (@global(FileOpenTrace) == true) {
-		fprintf(stderr, "fopen %s [ExternalStream] -> %"_lx_"\n", __stringVal(aPath), (INT)f);
-	    }
-	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
-	    __INST(handleType) = @symbol(filePointer);
-	    ok = true;
-	}
+        if (f == NULL) {
+            error = __mkSmallInteger(__threadErrno);
+        } else {
+            if (@global(FileOpenTrace) == true) {
+                fprintf(stderr, "fopen %s [ExternalStream] -> %"_lx_"\n", __stringVal(aPath), (INT)f);
+            }
+            fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
+            __INST(handleType) = @symbol(filePointer);
+            ok = true;
+        }
     }
 %}.
     ok ifTrue:[
-	position := 0.
-	Lobby register:self.
-	^ self.
+        position := 0.
+        self registerForFinalization.
+        ^ self.
     ].
     error notNil ifTrue:[
-	"
-	 the open failed for some reason ...
-	"
-	lastErrorNumber := error.
-	self openError:error.
+        "
+         the open failed for some reason ...
+        "
+        lastErrorNumber := error.
+        self openError:error.
     ].
     self primitiveFailed.
 !
@@ -4743,7 +4758,7 @@
 
     self class name errorPrint. ' [warning]: automatic reOpen not supported - stream closed' errorPrintCR.
     handle := nil.
-    Lobby unregister:self.
+    self unregisterForFinalization.
 
     "Modified: 10.1.1997 / 17:50:44 / cg"
 !
@@ -6276,7 +6291,7 @@
     ].
 
     "
-	'Bönnigheim' asUnicode16String errorPrintCR
+	'Bönnigheim' asUnicode16String errorPrintCR
     "
 !
 
@@ -6477,7 +6492,7 @@
     "
 	(FileStream newTemporary
 	    nextPutUtf16:$B;
-	    nextPutUtf16:$Ä;
+	    nextPutUtf16:$Ä;
 	    nextPutUtf16:(Character codePoint:16r10CCCC);
 	    reset;
 	    binary;