author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 01 Feb 2012 00:41:14 +0000 | |
changeset 99 | 677c81c943e4 |
parent 93 | cc18004ec5a5 |
child 109 | 9587e2df7029 |
permissions | -rw-r--r-- |
6 | 1 |
"{ Package: 'stx:goodies/xtreams/support' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
Object subclass:#RecyclingCenter |
|
6 |
instanceVariableNames:'recycled mutex' |
|
7 |
classVariableNames:'' |
|
93
cc18004ec5a5
XtreamsPool moved to Xtreams namespace.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
65
diff
changeset
|
8 |
poolDictionaries:'Xtreams::XtreamsPool' |
24
f906c95ca2ed
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
6
diff
changeset
|
9 |
category:'Xtreams-Support' |
6 | 10 |
! |
11 |
||
12 |
RecyclingCenter class instanceVariableNames:'objectspace1 fixedspace1 objectspaceN fixedspaceN cacheSize' |
|
13 |
||
14 |
" |
|
15 |
No other class instance variables are inherited by this class. |
|
16 |
" |
|
17 |
! |
|
18 |
||
19 |
RecyclingCenter comment:'Instance Variables |
|
20 |
recycled <IdentityDictionary key: Class value: (Array of: SequenceableCollection)> caches fixed number of collections (3) per sequenceable collection class |
|
21 |
mutex <Semaphore> |
|
22 |
||
23 |
Class Instance Variables |
|
24 |
objectspace1 <RecyclingCenter> old space collections size 1 |
|
25 |
fixedspace1 <RecyclingCenter> fixed space collections size 1 |
|
26 |
objectspaceN <RecyclingCenter> old space collections of cacheSize |
|
27 |
fixedspaceN <RecyclingCenter> fixed space collection of cacheSize |
|
28 |
cacheSize <SmallInteger> default collection size |
|
29 |
||
30 |
' |
|
31 |
! |
|
32 |
||
33 |
||
34 |
!RecyclingCenter class methodsFor:'class initialization'! |
|
35 |
||
36 |
initialize |
|
37 |
| cacheLimit | |
|
38 |
cacheSize := DefaultBufferSize. |
|
39 |
cacheLimit := 3. |
|
40 |
objectspace1 := self new initialize: cacheLimit. |
|
41 |
fixedspace1 := self new initialize: cacheLimit. |
|
42 |
objectspaceN := self new initialize: cacheLimit. |
|
43 |
fixedspaceN := self new initialize: cacheLimit |
|
44 |
! ! |
|
45 |
||
46 |
!RecyclingCenter class methodsFor:'recycling'! |
|
47 |
||
48 |
new: anInteger class: aClass |
|
49 |
anInteger == 1 ifTrue: [^objectspace1 new: anInteger class: aClass]. |
|
50 |
anInteger == cacheSize ifTrue: [^objectspaceN new: anInteger class: aClass]. |
|
51 |
^aClass withSize: anInteger |
|
52 |
! |
|
53 |
||
54 |
newInFixedSpace: anInteger class: aClass |
|
55 |
anInteger == 1 ifTrue: [^fixedspace1 newInFixedSpace: anInteger class: aClass]. |
|
56 |
anInteger == cacheSize ifTrue: [^fixedspaceN newInFixedSpace: anInteger class: aClass]. |
|
57 |
^aClass newInFixedSpace: anInteger |
|
58 |
! |
|
59 |
||
60 |
recycle: aCollection |
|
61 |
aCollection == nil ifTrue: [^nil]. |
|
62 |
aCollection isFixedArgument |
|
63 |
ifTrue: |
|
64 |
[aCollection size == 1 ifTrue: [fixedspace1 recycle: aCollection]. |
|
65 |
aCollection size == cacheSize ifTrue: [fixedspaceN recycle: aCollection]] |
|
66 |
ifFalse: |
|
67 |
[aCollection size == 1 ifTrue: [objectspace1 recycle: aCollection]. |
|
68 |
aCollection size == cacheSize ifTrue: [objectspaceN recycle: aCollection]] |
|
69 |
! ! |
|
70 |
||
71 |
!RecyclingCenter methodsFor:'initialize-release'! |
|
72 |
||
73 |
initialize: aCacheLimit |
|
74 |
mutex := Semaphore forMutualExclusion. |
|
75 |
recycled := IdentityDictionary new. |
|
76 |
SequenceableCollection allSubclassesDo: [:aClass | recycled at: aClass put: (Array new: aCacheLimit)]. |
|
77 |
recycled at: InterpretedBytes put: (Array new: aCacheLimit) |
|
78 |
! ! |
|
79 |
||
80 |
!RecyclingCenter methodsFor:'recycling'! |
|
81 |
||
82 |
existing: aClass |
|
83 |
| queue collection | |
|
84 |
queue := recycled at: aClass. |
|
85 |
mutex critical: [ |
|
86 |
1 to: queue size do: [:index | |
|
87 |
collection := queue at: index. |
|
88 |
collection == nil ifFalse: |
|
89 |
[queue at: index put: nil. |
|
90 |
mutex signal. |
|
91 |
^collection]]]. |
|
92 |
^nil |
|
93 |
! |
|
94 |
||
95 |
new: anInteger class: aClass |
|
96 |
| collection | |
|
97 |
(collection := self existing: aClass) == nil ifTrue: [collection := aClass withSize: anInteger]. |
|
98 |
^collection |
|
99 |
! |
|
100 |
||
101 |
newInFixedSpace: anInteger class: aClass |
|
102 |
| collection | |
|
103 |
(collection := self existing: aClass) == nil ifTrue: [collection := aClass newInFixedSpace: anInteger]. |
|
104 |
^collection |
|
105 |
! |
|
106 |
||
107 |
recycle: aCollection |
|
108 |
| queue | |
|
109 |
queue := recycled at: aCollection class. |
|
110 |
mutex critical: [ |
|
111 |
1 to: queue size do: [:index | |
|
112 |
(queue at: index) == nil ifTrue: |
|
113 |
[queue at: index put: aCollection. |
|
114 |
mutex signal. |
|
115 |
^self]]] |
|
116 |
! ! |
|
117 |
||
118 |
!RecyclingCenter class methodsFor:'documentation'! |
|
119 |
||
120 |
version_SVN |
|
121 |
^ '$Id$' |
|
122 |
! ! |
|
123 |
||
124 |
RecyclingCenter initialize! |