author | Claus Gittinger <cg@exept.de> |
Mon, 27 Jan 2020 13:47:24 +0100 | |
changeset 25204 | b12f8693fe6f |
parent 20994 | a210e2fb2993 |
child 21026 | 81e280fc1b93 |
permissions | -rw-r--r-- |
20994 | 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 |
[author:] |
|
20043 | 55 |
Claus Gittinger (cg@exept) |
4434 | 56 |
|
57 |
[see also:] |
|
58 |
||
59 |
[instance variables:] |
|
20043 | 60 |
keptObjects Collection hard referenced objects |
61 |
cacheSize Integer number of hard references |
|
4434 | 62 |
" |
63 |
! ! |
|
64 |
||
20393 | 65 |
!CachingRegistry class methodsFor:'instance creation'! |
66 |
||
67 |
new:cacheSize |
|
20394 | 68 |
^ (super new:cacheSize) cacheSize:cacheSize |
20393 | 69 |
! ! |
70 |
||
20388 | 71 |
!CachingRegistry methodsFor:'enumerating'! |
72 |
||
73 |
detect:aBlock ifNone:exceptionValue |
|
74 |
"... additionaly move it to the front of the LRU chain" |
|
75 |
||
20401 | 76 |
|cnt| |
77 |
||
78 |
"first a quick lookup |
|
79 |
(most recent entry is at the end, because #removeIdentical makes room at the end)..." |
|
80 |
||
81 |
cnt := 1. |
|
82 |
keptReferences reverseDo:[:obj| |
|
83 |
(aBlock value:obj) ifTrue:[ |
|
84 |
"if not at the end, put it to the end. |
|
85 |
but avoid to much remove/add actions" |
|
20435 | 86 |
cnt > (cacheSize // 4) ifTrue:[ |
20401 | 87 |
keptReferences removeIdentical:obj ifAbsent:[]. |
88 |
keptReferences addLast:obj. |
|
89 |
]. |
|
90 |
^ obj |
|
91 |
]. |
|
92 |
cnt := cnt + 1. |
|
93 |
]. |
|
94 |
||
95 |
"check the whole registry..." |
|
20994 | 96 |
keyArray validElementsDo:[:eachElement | |
97 |
eachElement ~~ DeletedEntry ifTrue:[ |
|
98 |
|realObject| |
|
99 |
||
100 |
realObject := eachElement. |
|
101 |
eachElement == NilEntry ifTrue:[realObject := nil]. |
|
102 |
(aBlock value:realObject) ifTrue:[ |
|
103 |
keptReferences size >= cacheSize ifTrue:[ |
|
104 |
keptReferences removeFirst. |
|
105 |
]. |
|
106 |
keptReferences addLast:realObject. |
|
107 |
^ realObject |
|
20395 | 108 |
]. |
20388 | 109 |
]. |
110 |
]. |
|
111 |
^ exceptionValue value |
|
112 |
! ! |
|
113 |
||
4434 | 114 |
!CachingRegistry methodsFor:'private'! |
115 |
||
116 |
cacheSize:aNumber |
|
117 |
keptReferences := OrderedCollection new:aNumber. |
|
118 |
cacheSize := aNumber. |
|
119 |
! ! |
|
120 |
||
121 |
!CachingRegistry methodsFor:'registering objects'! |
|
122 |
||
123 |
register:anObject as:aHandle |
|
124 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
20043 | 125 |
aHandle notNil ifTrue:[ |
20395 | 126 |
keptReferences size >= cacheSize ifTrue:[ |
20043 | 127 |
keptReferences removeFirst. |
128 |
]. |
|
20395 | 129 |
keptReferences addLast:anObject. |
4434 | 130 |
]. |
131 |
super register:anObject as:aHandle. |
|
132 |
! |
|
133 |
||
20043 | 134 |
removeKey:anObject ifAbsent:absentBlock |
135 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
136 |
super removeKey:anObject ifAbsent:absentBlock. |
|
4434 | 137 |
! |
138 |
||
20043 | 139 |
safeRemoveKey:anObject |
4434 | 140 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
20043 | 141 |
super safeRemoveKey:anObject. |
4434 | 142 |
! ! |
143 |
||
144 |
!CachingRegistry class methodsFor:'documentation'! |
|
145 |
||
146 |
version |
|
20043 | 147 |
^ '$Header$' |
20394 | 148 |
! |
149 |
||
150 |
version_CVS |
|
151 |
^ '$Header$' |
|
4434 | 152 |
! ! |
20043 | 153 |