author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 20 Sep 2016 11:37:33 +0100 | |
branch | jv |
changeset 20577 | a27e7b3031cb |
parent 20398 | 8cb53f870d39 |
parent 20395 | 50417882857e |
child 20578 | 39641ba8d6e0 |
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 |
||
20393 | 68 |
!CachingRegistry class methodsFor:'instance creation'! |
69 |
||
70 |
new:cacheSize |
|
20394 | 71 |
^ (super new:cacheSize) cacheSize:cacheSize |
20393 | 72 |
! ! |
73 |
||
20388 | 74 |
!CachingRegistry methodsFor:'enumerating'! |
75 |
||
76 |
detect:aBlock ifNone:exceptionValue |
|
77 |
"... additionaly move it to the front of the LRU chain" |
|
78 |
||
79 |
keyArray validElementsDo:[:obj | |
|
80 |
(obj ~~ DeletedEntry and:[aBlock value:obj]) ifTrue:[ |
|
20395 | 81 |
keptReferences removeIdentical:obj ifAbsent:[]. |
82 |
keptReferences size >= cacheSize ifTrue:[ |
|
83 |
keptReferences removeFirst. |
|
84 |
]. |
|
85 |
keptReferences addLast:obj. |
|
20388 | 86 |
^ obj |
87 |
]. |
|
88 |
]. |
|
89 |
^ exceptionValue value |
|
90 |
! ! |
|
91 |
||
4434 | 92 |
!CachingRegistry methodsFor:'private'! |
93 |
||
94 |
cacheSize:aNumber |
|
95 |
keptReferences := OrderedCollection new:aNumber. |
|
96 |
cacheSize := aNumber. |
|
97 |
! ! |
|
98 |
||
99 |
!CachingRegistry methodsFor:'registering objects'! |
|
100 |
||
101 |
register:anObject as:aHandle |
|
102 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
20043 | 103 |
aHandle notNil ifTrue:[ |
20395 | 104 |
keptReferences size >= cacheSize ifTrue:[ |
20043 | 105 |
keptReferences removeFirst. |
106 |
]. |
|
20395 | 107 |
keptReferences addLast:anObject. |
4434 | 108 |
]. |
109 |
super register:anObject as:aHandle. |
|
110 |
! |
|
111 |
||
20043 | 112 |
removeKey:anObject ifAbsent:absentBlock |
113 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
|
114 |
super removeKey:anObject ifAbsent:absentBlock. |
|
4434 | 115 |
! |
116 |
||
20043 | 117 |
safeRemoveKey:anObject |
4434 | 118 |
keptReferences removeIdentical:anObject ifAbsent:nil. |
20043 | 119 |
super safeRemoveKey:anObject. |
4434 | 120 |
! ! |
121 |
||
122 |
!CachingRegistry class methodsFor:'documentation'! |
|
123 |
||
124 |
version |
|
20043 | 125 |
^ '$Header$' |
20394 | 126 |
! |
127 |
||
128 |
version_CVS |
|
129 |
^ '$Header$' |
|
20577 | 130 |
! |
131 |
||
17865 | 132 |
version_SVN |
17911 | 133 |
^ '$Id: CachingRegistry.st 10761 2012-01-19 11:46:00Z vranyj1 $' |
4434 | 134 |
! ! |
20043 | 135 |
|
17910 | 136 |
|
17911 | 137 |