author | Jan Vrany <jan.vrany@labware.com> |
Thu, 25 Mar 2021 20:30:03 +0000 | |
branch | jv |
changeset 25411 | 248600ba8fd9 |
parent 23547 | c69c97cec351 |
permissions | -rw-r--r-- |
4434 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
18249
7d686f203624
oops package def was missing
Claus Gittinger <cg@exept.de>
parents:
4434
diff
changeset
|
3 |
All Rights Reserved |
4434 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
20043 | 12 |
"{ Package: 'stx:libbasic' }" |
4434 | 13 |
|
20043 | 14 |
"{ NameSpace: Smalltalk }" |
4434 | 15 |
|
16 |
Registry subclass:#CachingRegistry |
|
17 |
instanceVariableNames:'keptReferences cacheSize' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Support' |
|
21 |
! |
|
22 |
||
23 |
!CachingRegistry class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
18249
7d686f203624
oops package def was missing
Claus Gittinger <cg@exept.de>
parents:
4434
diff
changeset
|
28 |
All Rights Reserved |
4434 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
A CachingRegistry behaves generally like a registry; |
|
43 |
However, it keeps hard references to the last n registered objects, |
|
44 |
preventing them from being garbage collected (and finalized). |
|
45 |
This is useful for resources, which do not cost too much memory, |
|
20043 | 46 |
but are expensive to allocate - a special candidate of this kind are |
4434 | 47 |
XFonts. With a CachingRegistry, fonts are kept a bit longer alive |
48 |
and can therefore often be reused - even if temporarily unreferenced. |
|
49 |
||
50 |
This is kind of experimental. |
|
51 |
||
52 |
[author:] |
|
20043 | 53 |
Claus Gittinger (cg@exept) |
4434 | 54 |
|
55 |
[see also:] |
|
56 |
||
57 |
[instance variables:] |
|
20043 | 58 |
keptObjects Collection hard referenced objects |
59 |
cacheSize Integer number of hard references |
|
4434 | 60 |
" |
61 |
! ! |
|
62 |
||
20393 | 63 |
!CachingRegistry class methodsFor:'instance creation'! |
64 |
||
65 |
new:cacheSize |
|
20394 | 66 |
^ (super new:cacheSize) cacheSize:cacheSize |
20393 | 67 |
! ! |
68 |
||
20388 | 69 |
!CachingRegistry methodsFor:'enumerating'! |
70 |
||
71 |
detect:aBlock ifNone:exceptionValue |
|
72 |
"... additionaly move it to the front of the LRU chain" |
|
73 |
||
20401 | 74 |
|cnt| |
75 |
||
76 |
"first a quick lookup |
|
77 |
(most recent entry is at the end, because #removeIdentical makes room at the end)..." |
|
78 |
||
79 |
cnt := 1. |
|
80 |
keptReferences reverseDo:[:obj| |
|
81 |
(aBlock value:obj) ifTrue:[ |
|
82 |
"if not at the end, put it to the end. |
|
83 |
but avoid to much remove/add actions" |
|
20435 | 84 |
cnt > (cacheSize // 4) ifTrue:[ |
20401 | 85 |
keptReferences removeIdentical:obj ifAbsent:[]. |
86 |
keptReferences addLast:obj. |
|
87 |
]. |
|
88 |
^ obj |
|
89 |
]. |
|
90 |
cnt := cnt + 1. |
|
91 |
]. |
|
92 |
||
93 |
"check the whole registry..." |
|
20994 | 94 |
keyArray validElementsDo:[:eachElement | |
95 |
eachElement ~~ DeletedEntry ifTrue:[ |
|
96 |
|realObject| |
|
97 |
||
98 |
realObject := eachElement. |
|
99 |
eachElement == NilEntry ifTrue:[realObject := nil]. |
|
100 |
(aBlock value:realObject) ifTrue:[ |
|
101 |
keptReferences size >= cacheSize ifTrue:[ |
|
102 |
keptReferences removeFirst. |
|
103 |
]. |
|
104 |
keptReferences addLast:realObject. |
|
105 |
^ realObject |
|
20395 | 106 |
]. |
20388 | 107 |
]. |
108 |
]. |
|
109 |
^ exceptionValue value |
|
110 |
! ! |
|
111 |
||
4434 | 112 |
!CachingRegistry methodsFor:'private'! |
113 |
||
114 |
cacheSize:aNumber |
|
115 |
keptReferences := OrderedCollection new:aNumber. |
|
116 |
cacheSize := aNumber. |
|
117 |
! ! |
|
118 |
||
119 |
!CachingRegistry methodsFor:'registering objects'! |
|
120 |
||
121 |
register:anObject as:aHandle |
|
122 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
20043 | 123 |
aHandle notNil ifTrue:[ |
20395 | 124 |
keptReferences size >= cacheSize ifTrue:[ |
20043 | 125 |
keptReferences removeFirst. |
126 |
]. |
|
20395 | 127 |
keptReferences addLast:anObject. |
4434 | 128 |
]. |
129 |
super register:anObject as:aHandle. |
|
130 |
! |
|
131 |
||
20043 | 132 |
removeKey:anObject ifAbsent:absentBlock |
133 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
134 |
super removeKey:anObject ifAbsent:absentBlock. |
|
4434 | 135 |
! |
136 |
||
20043 | 137 |
safeRemoveKey:anObject |
4434 | 138 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
20043 | 139 |
super safeRemoveKey:anObject. |
4434 | 140 |
! ! |
141 |
||
142 |
!CachingRegistry class methodsFor:'documentation'! |
|
143 |
||
144 |
version |
|
20043 | 145 |
^ '$Header$' |
20394 | 146 |
! |
147 |
||
148 |
version_CVS |
|
149 |
^ '$Header$' |
|
20577 | 150 |
! |
151 |
||
17865 | 152 |
version_SVN |
17911 | 153 |
^ '$Id: CachingRegistry.st 10761 2012-01-19 11:46:00Z vranyj1 $' |
4434 | 154 |
! ! |
20043 | 155 |
|
17910 | 156 |
|
17911 | 157 |