Registry.st
branchjv
changeset 18630 a74d669db937
parent 18066 89d51443ba6f
parent 18620 b4e9f25d6ce6
child 18800 02724cc719b6
--- a/Registry.st	Wed Jul 22 06:38:29 2015 +0200
+++ b/Registry.st	Fri Jul 24 08:06:37 2015 +0100
@@ -36,15 +36,15 @@
 
 documentation
 "
-    Registries provide an easy interface to using WeakArrays. 
+    Registries provide an easy interface to using WeakArrays.
     A class, which wants to be informed of instance-death, can put a created object
-    into a registry. The registry will create an executor, which is a (shallow-)copy 
-    of the object, and watch out for death of the original object. When it dies, 
+    into a registry. The registry will create an executor, which is a (shallow-)copy
+    of the object, and watch out for death of the original object. When it dies,
     the executor will be sent a #finalize message.
     The trick with the shallow copy is especially nice, you can think of it as
     being the original object which died.
 
-    All objects, which keep external resources (such as fileDescriptors, fonts, 
+    All objects, which keep external resources (such as fileDescriptors, fonts,
     colormap-entries etc.) should be registered, so that the underlying resource
     can be freed when the object goes away.
 
@@ -52,8 +52,8 @@
     death of an object.
 
     Registries use #executor to aquire the copy of the original,
-    this can be redefined in individual classes for faster copying 
-    (typically, not all internal state, but only some device handles are needed for 
+    this can be redefined in individual classes for faster copying
+    (typically, not all internal state, but only some device handles are needed for
     finalization). If the to-be-registered object is large, this method may also
     return a stub (placeHolder) object. (i.e. there is no need for the copy to be
     of the same class as the original, as long as it implements #finalize and frees
@@ -62,12 +62,12 @@
     Example uses are found in Form, Color, ExternalStream and Font
 
     [author:]
-        Claus Gittinger
+	Claus Gittinger
 
     [see also:]
-        WeakArray WeakIdentityDictionary WeakIdentitySet
-        Font Form Color Cursor ExternalStream
-        
+	WeakArray WeakIdentityDictionary WeakIdentitySet
+	Font Form Color Cursor ExternalStream
+
 "
 ! !
 
@@ -102,60 +102,60 @@
      o myHandleArray wasBlocked|
 
     something == #ElementExpired ifTrue:[
-        wasBlocked := OperatingSystem blockInterrupts.
-        [
-            myHandleArray := handleArray.
-            sz := myHandleArray size.
+	wasBlocked := OperatingSystem blockInterrupts.
+	[
+	    myHandleArray := handleArray.
+	    sz := myHandleArray size.
 
-            index := 1.
-            [index <= sz] whileTrue:[
-                o := registeredObjects at:index.
-                o == 0 ifTrue:[
-                    executor := myHandleArray at:index.
-                    "remove the executor from the handle array before informing the executor.
-                     This is critical in case of errors while executing the executor.
-                     See ObjectMemory>>finalize"
-                    registeredObjects at:index put:nil.
-                    tally := tally - 1.
-                    executor notNil ifTrue:[
-                        myHandleArray at:index put:nil.
+	    index := 1.
+	    [index <= sz] whileTrue:[
+		o := registeredObjects at:index.
+		o class == SmallInteger ifTrue:[
+		    executor := myHandleArray at:index.
+		    "remove the executor from the handle array before informing the executor.
+		     This is critical in case of errors while executing the executor.
+		     See ObjectMemory>>finalize"
+		    registeredObjects at:index put:nil.
+		    tally := tally - 1.
+		    executor notNil ifTrue:[
+			myHandleArray at:index put:nil.
 
-                        "/
-                        "/ allow interrupts for a while ...
-                        "/
-                        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
-                        self informDispose:executor.
-                        OperatingSystem blockInterrupts.
+			"/
+			"/ allow interrupts for a while ...
+			"/
+			wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+			self informDispose:executor.
+			OperatingSystem blockInterrupts.
 
-                        "/
-                        "/ any change in an interrupt or dispose handling ?
-                        "/
-                        handleArray ~~ myHandleArray ifTrue:[
-                            myHandleArray := handleArray.
-                            sz := myHandleArray size.
-                            "/ start again
-                            index := 0.
-                        ]
-                    ]
-                ].
-                index := index + 1.
-            ]
-        ] ensure:[
-            wasBlocked ifFalse:[
-                OperatingSystem unblockInterrupts
-            ]
-        ].
+			"/
+			"/ any change in an interrupt or dispose handling ?
+			"/
+			handleArray ~~ myHandleArray ifTrue:[
+			    myHandleArray := handleArray.
+			    sz := myHandleArray size.
+			    "/ start again
+			    index := 0.
+			]
+		    ]
+		].
+		index := index + 1.
+	    ]
+	] ensure:[
+	    wasBlocked ifFalse:[
+		OperatingSystem unblockInterrupts
+	    ]
+	].
 
-        (sz > 50 and:[tally < (sz // 2)]) ifTrue:[
-            "/ shrink
-            self resize
-        ]
+	(sz > 50 and:[tally < (sz // 2)]) ifTrue:[
+	    "/ shrink
+	    self resize
+	]
     ] ifFalse:[
-        something == #earlyRestart ifTrue:[
-            handleArray notNil ifTrue:[
-                handleArray atAllPut:nil.
-            ]
-        ]
+	something == #earlyRestart ifTrue:[
+	    handleArray notNil ifTrue:[
+		handleArray atAllPut:nil.
+	    ]
+	]
     ].
 
     "Created: 15.6.1996 / 15:24:41 / cg"
@@ -167,9 +167,9 @@
 
 detect:aBlock ifNone:exceptionValue
     registeredObjects notNil ifTrue:[
-        registeredObjects validElementsDo:[:obj |
-            (aBlock value:obj) ifTrue:[^ obj].
-        ].
+	registeredObjects validElementsDo:[:obj |
+	    (aBlock value:obj) ifTrue:[^ obj].
+	].
     ].
     ^ exceptionValue value
 !
@@ -197,14 +197,14 @@
     cnt := 0.
 
     1 to:sz do:[:index |
-        ((executor := registeredObjects at:index) notNil 
-        and:[executor ~~ 0]) ifTrue:[
-            indexTable at:executor put:index.
-            cnt := cnt + 1.
-        ] ifFalse:[
-            handleArray at:index put:nil.
-            registeredObjects at:index put:nil.
-        ]
+	((executor := registeredObjects at:index) notNil
+	and:[executor class ~~ SmallInteger]) ifTrue:[
+	    indexTable at:executor put:index.
+	    cnt := cnt + 1.
+	] ifFalse:[
+	    handleArray at:index put:nil.
+	    registeredObjects at:index put:nil.
+	]
     ].
 
     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
@@ -216,45 +216,45 @@
     |sz          "{ Class: SmallInteger }"
      dstIndex    "{ Class: SmallInteger }"
      realNewSize "{ Class: SmallInteger }"
-     newObjects newHandles wasBlocked 
+     newObjects newHandles wasBlocked
      executor|
 
     sz := registeredObjects size.
 
     (sz > 50 and:[tally < (sz // 2)]) ifTrue:[
-        "/ shrink
+	"/ shrink
 
-        wasBlocked := OperatingSystem blockInterrupts.
+	wasBlocked := OperatingSystem blockInterrupts.
 
-        sz := registeredObjects size.
-        realNewSize := tally * 3 // 2.
-        newObjects := WeakArray new:realNewSize.
-        newHandles := Array new:realNewSize.
-        indexTable := WeakIdentityDictionary new.
+	sz := registeredObjects size.
+	realNewSize := tally * 3 // 2.
+	newObjects := WeakArray new:realNewSize.
+	newHandles := Array new:realNewSize.
+	indexTable := WeakIdentityDictionary new.
 
-        dstIndex := 1.
-        1 to:sz do:[:index |
-            (executor := registeredObjects at:index) notNil ifTrue:[
-                dstIndex > realNewSize ifTrue:[
-                    'Registry [error]: size given is too small in resize' errorPrintCR.
-                    self repairTally.
-                    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
-                    ^ self
-                ].
-                newObjects at:dstIndex put:executor.
-                newHandles at:dstIndex put:(handleArray at:index).
-                indexTable at:executor put:dstIndex.
+	dstIndex := 1.
+	1 to:sz do:[:index |
+	    (executor := registeredObjects at:index) notNil ifTrue:[
+		dstIndex > realNewSize ifTrue:[
+		    'Registry [error]: size given is too small in resize' errorPrintCR.
+		    self repairTally.
+		    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+		    ^ self
+		].
+		newObjects at:dstIndex put:executor.
+		newHandles at:dstIndex put:(handleArray at:index).
+		indexTable at:executor put:dstIndex.
 
-                dstIndex := dstIndex + 1
-            ]
-        ].
+		dstIndex := dstIndex + 1
+	    ]
+	].
 
-        registeredObjects removeDependent:self.
-        newObjects addDependent:self.
-        registeredObjects := newObjects.
-        handleArray := newHandles.
+	registeredObjects removeDependent:self.
+	newObjects addDependent:self.
+	registeredObjects := newObjects.
+	handleArray := newHandles.
 
-        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
     ]
 
     "Created: 16.1.1997 / 18:08:00 / cg"
@@ -266,8 +266,8 @@
 
     handleArray at:index put:nil.
     registeredObjects at:index put:nil.
-    (anObject notNil and:[anObject ~~ 0]) ifTrue:[ 
-        indexTable removeKey:anObject ifAbsent:[]
+    (anObject notNil and:[anObject ~~ 0]) ifTrue:[
+	indexTable removeKey:anObject ifAbsent:[]
     ].
     tally := tally - 1.
 ! !
@@ -288,7 +288,7 @@
 
     executor := anObject executor.
     executor notNil ifTrue:[
-        self register:anObject as:executor.
+	self register:anObject as:executor.
     ].
 !
 
@@ -304,19 +304,19 @@
     wasBlocked := OperatingSystem blockInterrupts.
 
     registeredObjects size == 0 "isNil" ifTrue:[
-        registeredObjects := WeakArray new:10.
-        registeredObjects addDependent:self.
-        handleArray := Array basicNew:10.
-        indexTable := WeakIdentityDictionary new.
+	registeredObjects := WeakArray new:10.
+	registeredObjects addDependent:self.
+	handleArray := Array basicNew:10.
+	indexTable := WeakIdentityDictionary new.
 
-        registeredObjects at:1 put:anObject.
-        handleArray at:1 put:aHandle.
-        indexTable at:anObject put:1.
+	registeredObjects at:1 put:anObject.
+	handleArray at:1 put:aHandle.
+	indexTable at:anObject put:1.
 
-        tally := 1.
-        ObjectMemory addDependent:self.
-        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
-        ^ self
+	tally := 1.
+	ObjectMemory addDependent:self.
+	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+	^ self
     ].
 
     "/
@@ -324,24 +324,24 @@
     "/ (but continue with interrupts disabled)
     "/
     wasBlocked ifFalse:[
-        OperatingSystem unblockInterrupts.
-        OperatingSystem blockInterrupts.
+	OperatingSystem unblockInterrupts.
+	OperatingSystem blockInterrupts.
     ].
 
     "/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
     index := indexTable at:anObject ifAbsent:0.
     index ~~ 0 ifTrue:[
-        "/ double check ...
-        (registeredObjects at:index) ~~ anObject ifTrue:[
-            ('Registry [warning]: index table clobbered') errorPrintCR.
-        ].
+	"/ double check ...
+	(registeredObjects at:index) ~~ anObject ifTrue:[
+	    ('Registry [warning]: index table clobbered') errorPrintCR.
+	].
 
-        "already registered"
-        
-        handleArray at:index put:aHandle.
+	"already registered"
+
+	handleArray at:index put:aHandle.
 "/        ('Registry [info]: object (' , (registeredObjects at:index) printString , ') is already registered') infoPrintCR.
-        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
-        ^ self
+	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+	^ self
     ].
 
     "/
@@ -349,8 +349,8 @@
     "/ (but continue with interrupts disabled)
     "/
     wasBlocked ifFalse:[
-        OperatingSystem unblockInterrupts.
-        OperatingSystem blockInterrupts.
+	OperatingSystem unblockInterrupts.
+	OperatingSystem blockInterrupts.
     ].
 
     "/
@@ -360,26 +360,26 @@
     idx0 := 1.
     index := registeredObjects identityIndexOf:nil startingAt:idx0.
     [index ~~ 0] whileTrue:[
-        "is there a leftover ?"
-        p := handleArray at:index.
-        p isNil ifTrue:[
-            registeredObjects at:index put:anObject.
-            handleArray at:index put:aHandle.
-            indexTable at:anObject put:index.
+	"is there a leftover ?"
+	p := handleArray at:index.
+	p isNil ifTrue:[
+	    registeredObjects at:index put:anObject.
+	    handleArray at:index put:aHandle.
+	    indexTable at:anObject put:index.
 
-            tally := tally + 1.
-            wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
-            ^ self
-        ].
+	    tally := tally + 1.
+	    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+	    ^ self
+	].
 
-        "/ mhmh - a registeredObject vanished, but its
-        "/ executor is still there ...
+	"/ mhmh - a registeredObject vanished, but its
+	"/ executor is still there ...
 
-        "/
-        "/ this may happen, if the registries dispose handling is 
-        "/ currently being executed by a lower priority process,
-        "/ and the registeredObject has already been nilled,
-        "/ but the executor is being notified (in the other process).
+	"/
+	"/ this may happen, if the registries dispose handling is
+	"/ currently being executed by a lower priority process,
+	"/ and the registeredObject has already been nilled,
+	"/ but the executor is being notified (in the other process).
 
 "/        'Registry [info]: leftOver executor: ' infoPrint. p infoPrintCR.
 
@@ -389,8 +389,8 @@
 "/        self informDispose:p.
 "/        p := nil.
 
-        idx0 := index + 1.
-        index := registeredObjects identityIndexOf:nil startingAt:idx0.
+	idx0 := index + 1.
+	index := registeredObjects identityIndexOf:nil startingAt:idx0.
     ].
 
     "no free slot, add at the end"
@@ -425,21 +425,21 @@
 
     executor := anObject executor.
     executor isNil ifTrue:[
-        self unregister:anObject.
-        ^ self.
+	self unregister:anObject.
+	^ self.
     ].
 
     wasBlocked := OperatingSystem blockInterrupts.
     registeredObjects isNil ifTrue:[
-        index := 0
+	index := 0
     ] ifFalse:[
-        "/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
-        index := indexTable at:anObject ifAbsent:0.
+	"/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
+	index := indexTable at:anObject ifAbsent:0.
     ].
     index ~~ 0 ifTrue:[
-        handleArray at:index put:executor.
+	handleArray at:index put:executor.
     ] ifFalse:[
-        self register:anObject as:executor
+	self register:anObject as:executor
     ].
     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
 
@@ -454,15 +454,15 @@
     |index wasBlocked|
 
     registeredObjects notNil ifTrue:[
-        wasBlocked := OperatingSystem blockInterrupts.
-        "/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
-        index := indexTable at:anObject ifAbsent:0.
-        index ~~ 0 ifTrue:[
-            self unregister:anObject atIndex:index.
-        ].
-        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+	wasBlocked := OperatingSystem blockInterrupts.
+	"/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
+	index := indexTable at:anObject ifAbsent:0.
+	index ~~ 0 ifTrue:[
+	    self unregister:anObject atIndex:index.
+	].
+	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
 
-        self resize
+	self resize
     ]
 
     "Modified: 16.1.1997 / 18:08:42 / cg"
@@ -484,7 +484,7 @@
 
 	1 to:n do:[:index |
 	    obj := registeredObjects at:index.
-	    (obj notNil and:[obj ~~ 0]) ifTrue:[
+	    (obj notNil and:[obj class ~~ SmallInteger]) ifTrue:[
 		(aBlock value:obj) ifTrue:[
 		    self unregister:obj atIndex:index.
 		    any := true.
@@ -526,10 +526,10 @@
 !Registry class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.64 2013-06-03 18:02:40 cg Exp $'
+    ^ '$Header$'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.64 2013-06-03 18:02:40 cg Exp $'
+    ^ '$Header$'
 ! !