author | Stefan Vogel <sv@exept.de> |
Wed, 14 Sep 2016 14:58:20 +0200 | |
changeset 20388 | db4b243d97ce |
parent 20043 | 0be0ccf00b36 |
child 20393 | 4d74f02d1a9f |
child 20398 | 8cb53f870d39 |
permissions | -rw-r--r-- |
20388 | 1 |
"{ Encoding: utf8 }" |
2 |
||
4434 | 3 |
" |
4 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
18249
7d686f203624
oops package def was missing
Claus Gittinger <cg@exept.de>
parents:
4434
diff
changeset
|
5 |
All Rights Reserved |
4434 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
20043 | 14 |
"{ Package: 'stx:libbasic' }" |
4434 | 15 |
|
20043 | 16 |
"{ NameSpace: Smalltalk }" |
4434 | 17 |
|
18 |
Registry subclass:#CachingRegistry |
|
19 |
instanceVariableNames:'keptReferences cacheSize' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'System-Support' |
|
23 |
! |
|
24 |
||
25 |
!CachingRegistry class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
18249
7d686f203624
oops package def was missing
Claus Gittinger <cg@exept.de>
parents:
4434
diff
changeset
|
30 |
All Rights Reserved |
4434 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
A CachingRegistry behaves generally like a registry; |
|
45 |
However, it keeps hard references to the last n registered objects, |
|
46 |
preventing them from being garbage collected (and finalized). |
|
47 |
This is useful for resources, which do not cost too much memory, |
|
20043 | 48 |
but are expensive to allocate - a special candidate of this kind are |
4434 | 49 |
XFonts. With a CachingRegistry, fonts are kept a bit longer alive |
50 |
and can therefore often be reused - even if temporarily unreferenced. |
|
51 |
||
52 |
This is kind of experimental. |
|
53 |
||
54 |
||
55 |
[author:] |
|
20043 | 56 |
Claus Gittinger (cg@exept) |
4434 | 57 |
|
58 |
[see also:] |
|
59 |
||
60 |
[instance variables:] |
|
20043 | 61 |
keptObjects Collection hard referenced objects |
62 |
cacheSize Integer number of hard references |
|
4434 | 63 |
|
64 |
[class variables:] |
|
65 |
" |
|
66 |
! ! |
|
67 |
||
20388 | 68 |
!CachingRegistry methodsFor:'enumerating'! |
69 |
||
70 |
detect:aBlock ifNone:exceptionValue |
|
71 |
"... additionaly move it to the front of the LRU chain" |
|
72 |
||
73 |
keyArray validElementsDo:[:obj | |
|
74 |
(obj ~~ DeletedEntry and:[aBlock value:obj]) ifTrue:[ |
|
75 |
keptReferences |
|
76 |
removeIdentical:obj ifAbsent:[]; |
|
77 |
addLast:obj. |
|
78 |
^ obj |
|
79 |
]. |
|
80 |
]. |
|
81 |
^ exceptionValue value |
|
82 |
! ! |
|
83 |
||
4434 | 84 |
!CachingRegistry methodsFor:'private'! |
85 |
||
86 |
cacheSize:aNumber |
|
87 |
keptReferences := OrderedCollection new:aNumber. |
|
88 |
cacheSize := aNumber. |
|
89 |
! ! |
|
90 |
||
91 |
!CachingRegistry methodsFor:'registering objects'! |
|
92 |
||
93 |
register:anObject as:aHandle |
|
94 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
20043 | 95 |
aHandle notNil ifTrue:[ |
96 |
keptReferences addLast:anObject. |
|
97 |
keptReferences size > cacheSize ifTrue:[ |
|
98 |
keptReferences removeFirst. |
|
99 |
]. |
|
4434 | 100 |
]. |
101 |
super register:anObject as:aHandle. |
|
102 |
! |
|
103 |
||
20043 | 104 |
removeKey:anObject ifAbsent:absentBlock |
105 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
106 |
super removeKey:anObject ifAbsent:absentBlock. |
|
4434 | 107 |
! |
108 |
||
20043 | 109 |
safeRemoveKey:anObject |
4434 | 110 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
20043 | 111 |
super safeRemoveKey:anObject. |
4434 | 112 |
! ! |
113 |
||
114 |
!CachingRegistry class methodsFor:'documentation'! |
|
115 |
||
116 |
version |
|
20043 | 117 |
^ '$Header$' |
4434 | 118 |
! ! |
20043 | 119 |