Registry.st
changeset 2449 788bba151e8a
parent 2414 215e58d26a05
child 2507 8824b9953b54
equal deleted inserted replaced
2448:ab70ff46b982 2449:788bba151e8a
   154     ]
   154     ]
   155 ! !
   155 ! !
   156 
   156 
   157 !Registry methodsFor:'private'!
   157 !Registry methodsFor:'private'!
   158 
   158 
       
   159 repairTally
       
   160     |sz          "{ Class: SmallInteger }"
       
   161      cnt         "{ Class: SmallInteger }"
       
   162      phantom wasBlocked|
       
   163 
       
   164     wasBlocked := OperatingSystem blockInterrupts.
       
   165 
       
   166     indexTable := WeakIdentityDictionary new.
       
   167 
       
   168     sz := registeredObjects size.
       
   169     cnt := 0.
       
   170 
       
   171     1 to:sz do:[:index |
       
   172         ((phantom := registeredObjects at:index) notNil 
       
   173         and:[phantom ~~ 0]) ifTrue:[
       
   174             indexTable at:phantom put:index.
       
   175             cnt := cnt + 1.
       
   176         ] ifFalse:[
       
   177             handleArray at:index put:nil.
       
   178             registeredObjects at:index put:nil.
       
   179         ]
       
   180     ].
       
   181 
       
   182     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
   183 
       
   184     "Created: 6.3.1997 / 22:31:09 / cg"
       
   185 !
       
   186 
   159 resize
   187 resize
   160     |sz          "{ Class: SmallInteger }"
   188     |sz          "{ Class: SmallInteger }"
   161      dstIndex    "{ Class: SmallInteger }"
   189      dstIndex    "{ Class: SmallInteger }"
   162      realNewSize "{ Class: SmallInteger }"
   190      realNewSize "{ Class: SmallInteger }"
   163      newObjects newHandles wasBlocked 
   191      newObjects newHandles wasBlocked 
   166     sz := registeredObjects size.
   194     sz := registeredObjects size.
   167 
   195 
   168     (sz > 50 and:[tally < (sz // 2)]) ifTrue:[
   196     (sz > 50 and:[tally < (sz // 2)]) ifTrue:[
   169         "/ shrink
   197         "/ shrink
   170 
   198 
       
   199         wasBlocked := OperatingSystem blockInterrupts.
       
   200 
       
   201         sz := registeredObjects size.
   171         realNewSize := tally * 3 // 2.
   202         realNewSize := tally * 3 // 2.
   172         newObjects := WeakArray new:realNewSize.
   203         newObjects := WeakArray new:realNewSize.
   173         newHandles := Array new:realNewSize.
   204         newHandles := Array new:realNewSize.
   174 	indexTable := WeakIdentityDictionary new.
   205         indexTable := WeakIdentityDictionary new.
   175 
       
   176         wasBlocked := OperatingSystem blockInterrupts.
       
   177 
   206 
   178         dstIndex := 1.
   207         dstIndex := 1.
   179         1 to:sz do:[:index |
   208         1 to:sz do:[:index |
   180             (phantom := registeredObjects at:index) notNil ifTrue:[
   209             (phantom := registeredObjects at:index) notNil ifTrue:[
   181                 dstIndex > realNewSize ifTrue:[
   210                 dstIndex > realNewSize ifTrue:[
   182                     'Registry [info]: size given is too small in resize' infoPrintCR.
   211                     'Registry [error]: size given is too small in resize' errorPrintCR.
       
   212                     self repairTally.
   183                     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   213                     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   184                     ^ self
   214                     ^ self
   185                 ].
   215                 ].
   186                 newObjects at:dstIndex put:phantom.
   216                 newObjects at:dstIndex put:phantom.
   187                 newHandles at:dstIndex put:(handleArray at:index).
   217                 newHandles at:dstIndex put:(handleArray at:index).
   188 		indexTable at:phantom put:dstIndex.
   218                 indexTable at:phantom put:dstIndex.
   189 
   219 
   190                 dstIndex := dstIndex + 1
   220                 dstIndex := dstIndex + 1
   191             ]
   221             ]
   192         ].
   222         ].
   193 
   223 
   198 
   228 
   199         wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   229         wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   200     ]
   230     ]
   201 
   231 
   202     "Created: 16.1.1997 / 18:08:00 / cg"
   232     "Created: 16.1.1997 / 18:08:00 / cg"
   203     "Modified: 27.1.1997 / 15:04:40 / cg"
   233     "Modified: 6.3.1997 / 22:29:58 / cg"
   204 ! !
   234 ! !
   205 
   235 
   206 !Registry methodsFor:'registering objects'!
   236 !Registry methodsFor:'registering objects'!
   207 
   237 
   208 register:anObject
   238 register:anObject
   220      size  "{ Class: SmallInteger }"
   250      size  "{ Class: SmallInteger }"
   221      index "{ Class: SmallInteger }"
   251      index "{ Class: SmallInteger }"
   222      p wasBlocked|
   252      p wasBlocked|
   223 
   253 
   224     wasBlocked := OperatingSystem blockInterrupts.
   254     wasBlocked := OperatingSystem blockInterrupts.
       
   255 
   225     registeredObjects size == 0 "isNil" ifTrue:[
   256     registeredObjects size == 0 "isNil" ifTrue:[
   226         registeredObjects := WeakArray new:10.
   257         registeredObjects := WeakArray new:10.
   227         registeredObjects addDependent:self.
   258         registeredObjects addDependent:self.
   228         handleArray := Array basicNew:10.
   259         handleArray := Array basicNew:10.
   229 	indexTable := WeakIdentityDictionary new.
   260         indexTable := WeakIdentityDictionary new.
   230 
   261 
   231         registeredObjects at:1 put:anObject.
   262         registeredObjects at:1 put:anObject.
   232         handleArray at:1 put:aHandle.
   263         handleArray at:1 put:aHandle.
   233         indexTable at:anObject put:1.
   264         indexTable at:anObject put:1.
   234 
   265 
   244     ].
   275     ].
   245 
   276 
   246     "/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
   277     "/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
   247     index := indexTable at:anObject ifAbsent:0.
   278     index := indexTable at:anObject ifAbsent:0.
   248     index ~~ 0 ifTrue:[
   279     index ~~ 0 ifTrue:[
       
   280         "/ double check ...
       
   281         (registeredObjects at:index) ~~ anObject ifTrue:[
       
   282             ('Registry [warning]: index table clobbered') errorPrintCR.
       
   283         ].
       
   284 
   249         "already registered"
   285         "already registered"
       
   286         
   250         handleArray at:index put:aHandle.
   287         handleArray at:index put:aHandle.
   251         ('Registry [info]: object (' , (registeredObjects at:index) printString , ' is already registered') infoPrintCR.
   288         ('Registry [info]: object (' , (registeredObjects at:index) printString , ' is already registered') infoPrintCR.
   252         wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   289         wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   253         ^ self
   290         ^ self
   254     ].
   291     ].
   262     index := registeredObjects identityIndexOf:nil startingAt:1.
   299     index := registeredObjects identityIndexOf:nil startingAt:1.
   263     index ~~ 0 ifTrue:[
   300     index ~~ 0 ifTrue:[
   264         "is there a leftover ?"
   301         "is there a leftover ?"
   265         p := handleArray at:index.
   302         p := handleArray at:index.
   266         p notNil ifTrue:[
   303         p notNil ifTrue:[
   267             'Registry [info]: there should be no leftOvers' infoPrintCR.
   304             'Registry [warning]: there should be no leftOvers' errorPrintCR.
   268 
   305 
   269             "tell the phantom"
   306             "tell the phantom"
   270             handleArray at:index put:nil.
   307             handleArray at:index put:nil.
   271             tally := tally - 1.
   308             tally := tally - 1.
   272             self informDispose:p.
   309             self informDispose:p.
   273             p := nil.
   310             p := nil.
   274         ].
   311         ].
   275         registeredObjects at:index put:anObject.
   312         registeredObjects at:index put:anObject.
   276         handleArray at:index put:aHandle.
   313         handleArray at:index put:aHandle.
   277 	indexTable at:anObject put:index.
   314         indexTable at:anObject put:index.
   278 
   315 
   279         tally := tally + 1.
   316         tally := tally + 1.
   280         wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   317         wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   281         ^ self
   318         ^ self
   282     ].
   319     ].
   301     tally := tally + 1.
   338     tally := tally + 1.
   302 
   339 
   303     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   340     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   304 
   341 
   305     "Modified: 7.1.1997 / 16:56:03 / stefan"
   342     "Modified: 7.1.1997 / 16:56:03 / stefan"
   306     "Modified: 27.1.1997 / 15:18:07 / cg"
   343     "Modified: 6.3.1997 / 22:23:23 / cg"
   307 !
   344 !
   308 
   345 
   309 registerChange:anObject
   346 registerChange:anObject
   310     "a registered object has changed, create a new phantom"
   347     "a registered object has changed, create a new phantom"
   311 
   348 
   312     |index wasBlocked|
   349     |index wasBlocked copy|
   313 
   350 
   314     wasBlocked := OperatingSystem blockInterrupts.
   351     wasBlocked := OperatingSystem blockInterrupts.
   315     registeredObjects isNil ifTrue:[
   352     registeredObjects isNil ifTrue:[
   316         index := 0
   353         index := 0
   317     ] ifFalse:[
   354     ] ifFalse:[
   318         "/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
   355         "/ index := registeredObjects identityIndexOf:anObject ifAbsent:0.
   319 	index := indexTable at:anObject ifAbsent:0.
   356         index := indexTable at:anObject ifAbsent:0.
   320     ].
   357     ].
       
   358     copy := anObject shallowCopyForFinalization.
   321     index ~~ 0 ifTrue:[
   359     index ~~ 0 ifTrue:[
   322         handleArray at:index put:anObject shallowCopyForFinalization.
   360         handleArray at:index put:copy.
   323     ] ifFalse:[
   361     ] ifFalse:[
   324         self register:anObject
   362         self register:anObject as:copy
   325     ].
   363     ].
   326     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   364     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   327 
   365 
   328     "Modified: 22.6.1996 / 14:27:52 / cg"
   366     "Modified: 6.3.1997 / 22:24:15 / cg"
   329 !
   367 !
   330 
   368 
   331 unregister:anObject
   369 unregister:anObject
   332     "remove registration of anObject, without telling the phantom;
   370     "remove registration of anObject, without telling the phantom;
   333      should be sent, if we are no more interested in destruction of
   371      should be sent, if we are no more interested in destruction of
   386 ! !
   424 ! !
   387 
   425 
   388 !Registry class methodsFor:'documentation'!
   426 !Registry class methodsFor:'documentation'!
   389 
   427 
   390 version
   428 version
   391     ^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.40 1997-02-24 20:24:44 cg Exp $'
   429     ^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.41 1997-03-06 21:35:05 cg Exp $'
   392 ! !
   430 ! !