1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
"
|
|
12 |
|
|
13 |
Object subclass:#ObjectMemory
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
ObjectMemory comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1992 -93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
This class contains access methods to the system memory -
|
|
26 |
in previous versions this stuff used to be in the Smalltalk class.
|
|
27 |
It has been separated for better overall structure.
|
|
28 |
|
|
29 |
%W% %E%
|
|
30 |
'!
|
|
31 |
|
|
32 |
!ObjectMemory class methodsFor:'cache management'!
|
|
33 |
|
|
34 |
flushInlineCachesForClass:aClass
|
|
35 |
"flush inlinecaches for calls to aClass"
|
|
36 |
|
|
37 |
%{ /* NOCONTEXT */
|
|
38 |
__flushInlineCachesFor(aClass);
|
|
39 |
%}
|
|
40 |
!
|
|
41 |
|
|
42 |
flushInlineCachesWithArgs:nargs
|
|
43 |
"flush inlinecaches for calls with nargs arguments"
|
|
44 |
|
|
45 |
%{ /* NOCONTEXT */
|
|
46 |
__flushInlineCaches(_intVal(nargs));
|
|
47 |
%}
|
|
48 |
!
|
|
49 |
|
|
50 |
flushInlineCachesFor:aClass withArgs:nargs
|
|
51 |
"flush inlinecaches for calls to aClass with nargs arguments"
|
|
52 |
|
|
53 |
%{ /* NOCONTEXT */
|
|
54 |
__flushInlineCachesForAndNargs(aClass, _intVal(nargs));
|
|
55 |
%}
|
|
56 |
!
|
|
57 |
|
|
58 |
flushInlineCaches
|
|
59 |
"flush all inlinecaches"
|
|
60 |
|
|
61 |
%{ /* NOCONTEXT */
|
|
62 |
__flushAllInlineCaches();
|
|
63 |
%}
|
|
64 |
!
|
|
65 |
|
|
66 |
flushMethodCacheFor:aClass
|
|
67 |
"flush the method cache for sends to aClass"
|
|
68 |
|
|
69 |
%{ /* NOCONTEXT */
|
|
70 |
__flushMethodCacheFor(aClass);
|
|
71 |
%}
|
|
72 |
!
|
|
73 |
|
|
74 |
flushMethodCache
|
|
75 |
"flush the method cache"
|
|
76 |
|
|
77 |
%{ /* NOCONTEXT */
|
|
78 |
__flushMethodCache();
|
|
79 |
%}
|
|
80 |
!
|
|
81 |
|
|
82 |
flushCaches
|
|
83 |
"flush method and inline caches"
|
|
84 |
|
|
85 |
%{ /* NOCONTEXT */
|
|
86 |
__flushMethodCache();
|
|
87 |
__flushAllInlineCaches();
|
|
88 |
%}
|
|
89 |
! !
|
|
90 |
|
|
91 |
!ObjectMemory class methodsFor:'enumeration'!
|
|
92 |
|
|
93 |
allObjectsDo:aBlock
|
|
94 |
"evaluate the argument, aBlock for all objects in the system"
|
|
95 |
%{
|
|
96 |
#ifdef THIS_CONTEXT
|
|
97 |
__allObjectsDo(&aBlock);
|
|
98 |
#else
|
|
99 |
__allObjectsDo(&aBlock, __context);
|
|
100 |
#endif
|
|
101 |
%}
|
|
102 |
! !
|
|
103 |
|
|
104 |
!ObjectMemory class methodsFor:'queries'!
|
|
105 |
|
|
106 |
newSpaceUsed
|
|
107 |
"return the number of bytes allocated for new objects"
|
|
108 |
|
|
109 |
%{ /* NOCONTEXT */
|
|
110 |
RETURN ( _MKSMALLINT(__newSpaceUsed()) );
|
|
111 |
%}
|
|
112 |
"ObjectMemory newSpaceUsed"
|
|
113 |
!
|
|
114 |
|
|
115 |
oldSpaceUsed
|
|
116 |
"return the number of bytes allocated for old objects"
|
|
117 |
|
|
118 |
%{ /* NOCONTEXT */
|
|
119 |
RETURN ( _MKSMALLINT(__oldSpaceUsed()) );
|
|
120 |
%}
|
|
121 |
"ObjectMemory oldSpaceUsed"
|
|
122 |
!
|
|
123 |
|
|
124 |
bytesUsed
|
|
125 |
"return the number of bytes allocated for objects -
|
|
126 |
this number is not exact, since some objects may be dead"
|
|
127 |
|
|
128 |
%{ /* NOCONTEXT */
|
|
129 |
RETURN ( _MKSMALLINT(__oldSpaceUsed() + __newSpaceUsed()) );
|
|
130 |
%}
|
|
131 |
"ObjectMemory bytesUsed"
|
|
132 |
!
|
|
133 |
|
|
134 |
numberOfObjects
|
|
135 |
"return the number of objects in the system"
|
|
136 |
|
|
137 |
|tally|
|
|
138 |
|
|
139 |
tally := 0.
|
|
140 |
self allObjectsDo:[:obj | tally := tally + 1].
|
|
141 |
^ tally
|
|
142 |
|
|
143 |
"ObjectMemory numberOfObjects"
|
|
144 |
!
|
|
145 |
|
|
146 |
printReferences:anObject
|
|
147 |
"debugging: print referents to anObject"
|
|
148 |
|
|
149 |
%{
|
|
150 |
_printRefChain(__context, anObject);
|
|
151 |
%}
|
|
152 |
!
|
|
153 |
|
|
154 |
whoReferences:anObject
|
|
155 |
"return a collection of objects referencing the argument, anObject"
|
|
156 |
|
|
157 |
|aCollection|
|
|
158 |
|
|
159 |
aCollection := IdentitySet new.
|
|
160 |
self allObjectsDo:[:o |
|
|
161 |
(o references:anObject) ifTrue:[
|
|
162 |
aCollection add:o
|
|
163 |
]
|
|
164 |
].
|
|
165 |
(aCollection size == 0) ifTrue:[
|
|
166 |
"actually this cannot happen - there is always one"
|
|
167 |
^ nil
|
|
168 |
].
|
|
169 |
^ aCollection
|
|
170 |
! !
|
|
171 |
|
|
172 |
!ObjectMemory class methodsFor:'garbage collector control'!
|
|
173 |
|
|
174 |
garbageCollect
|
|
175 |
"search for and free garbage in the oldSpace
|
|
176 |
(newSpace is cleaned automatically)
|
|
177 |
- can take a long time if paging is involved
|
|
178 |
- when no paging is involved, its faster than I thought :-)"
|
|
179 |
%{
|
|
180 |
__garbageCollect(__context);
|
|
181 |
%}
|
|
182 |
|
|
183 |
"ObjectMemory garbageCollect"
|
|
184 |
!
|
|
185 |
|
|
186 |
scavenge
|
|
187 |
"for debugging only - collect newspace stuff"
|
|
188 |
%{
|
|
189 |
nonTenuringScavenge(__context);
|
|
190 |
%}
|
|
191 |
|
|
192 |
"ObjectMemory scavenge"
|
|
193 |
!
|
|
194 |
|
|
195 |
tenure
|
|
196 |
"forcae all new stuff into old-space"
|
|
197 |
%{
|
|
198 |
tenure(__context);
|
|
199 |
%}
|
|
200 |
|
|
201 |
"ObjectMemory tenure"
|
|
202 |
!
|
|
203 |
|
|
204 |
markAndSweep
|
|
205 |
"mark/sweep garbage collector"
|
|
206 |
|
|
207 |
%{
|
|
208 |
markAndSweep(__context);
|
|
209 |
%}
|
|
210 |
|
|
211 |
"ObjectMemory markAndSweep"
|
|
212 |
!
|
|
213 |
|
|
214 |
gcStep
|
|
215 |
"one incremental garbage collect step"
|
|
216 |
%{
|
|
217 |
incrGCstep(__context);
|
|
218 |
%}
|
|
219 |
!
|
|
220 |
|
|
221 |
turnOffGarbageCollector
|
|
222 |
"turn off garbage collector.
|
|
223 |
this method is somewhat dangerous: if collector is turned off,
|
|
224 |
and too many objects are created, the system may run into trouble.
|
|
225 |
Use this only for measurement purposes or when realtime behavior
|
|
226 |
is required for a limited time period. No waranty"
|
|
227 |
%{
|
|
228 |
allocForceSpace(0);
|
|
229 |
%}
|
|
230 |
!
|
|
231 |
|
|
232 |
turnOnGarbageCollector
|
|
233 |
"turn it on again"
|
|
234 |
|
|
235 |
%{
|
|
236 |
allocForceSpace(1);
|
|
237 |
%}
|
|
238 |
! !
|
|
239 |
|
|
240 |
!ObjectMemory class methodsFor:'system management'!
|
|
241 |
|
|
242 |
loadClassBinary:aClassName
|
|
243 |
"find the object file for aClassName and -if found - load it;
|
|
244 |
this one loads precompiled object files"
|
|
245 |
|
|
246 |
|fName newClass upd|
|
|
247 |
|
|
248 |
fName := self fileNameForClass:aClassName.
|
|
249 |
fName notNil ifTrue:[
|
|
250 |
upd := Class updateChanges:false.
|
|
251 |
[
|
|
252 |
self loadBinary:(fName , '.o')
|
|
253 |
] valueNowOrOnUnwindDo:[
|
|
254 |
Class updateChanges:upd
|
|
255 |
].
|
|
256 |
newClass := self at:(aClassName asSymbol).
|
|
257 |
(newClass notNil and:[newClass implements:#initialize]) ifTrue:[
|
|
258 |
newClass initialize
|
|
259 |
]
|
|
260 |
]
|
|
261 |
!
|
|
262 |
|
|
263 |
snapShot
|
|
264 |
"create a snapshot"
|
|
265 |
|
|
266 |
ImageName isNil ifTrue:[
|
|
267 |
ImageName := 'st.img'
|
|
268 |
].
|
|
269 |
self snapShotOn:ImageName
|
|
270 |
|
|
271 |
"ObjectMemory snapShot"
|
|
272 |
!
|
|
273 |
|
|
274 |
snapShotOn:aFileName
|
|
275 |
"create a snapshot in the given file"
|
|
276 |
|
|
277 |
"give others a chance to fix things"
|
|
278 |
self changed:#save.
|
|
279 |
%{
|
|
280 |
OBJ __snapShotOn();
|
|
281 |
|
|
282 |
if (_isString(aFileName)) {
|
|
283 |
RETURN ( __snapShotOn(__context, _stringVal(aFileName)) );
|
|
284 |
}
|
|
285 |
%}
|
|
286 |
.
|
|
287 |
^ self primitiveFailed
|
|
288 |
|
|
289 |
"ObjectMemory snapShotOn:'myimage.img'"
|
|
290 |
!
|
|
291 |
|
|
292 |
applicationImageOn:aFileName for:startupClass selector:startupSelector
|
|
293 |
"create a snapshot which will come up without any views
|
|
294 |
but starts up an application by sending startupClass the startupSelector"
|
|
295 |
|
|
296 |
|viewsKnown savedIdleBlocks savedTimeoutBlocks savedTranscript|
|
|
297 |
|
|
298 |
viewsKnown := Display knownViews.
|
|
299 |
savedIdleBlocks := Display idleBlocks.
|
|
300 |
savedTimeoutBlocks := Display timeOutBlocks.
|
|
301 |
savedTranscript := Transcript.
|
|
302 |
|
|
303 |
"a kludge: save image with modified knownViews ..."
|
|
304 |
|
|
305 |
Display knownViews:nil.
|
|
306 |
Display idleBlocks:nil.
|
|
307 |
Display timeOutBlocks:nil.
|
|
308 |
Transcript := Stderr.
|
|
309 |
StartupClass := startupClass.
|
|
310 |
StartupSelector := startupSelector.
|
|
311 |
|
|
312 |
self snapShotOn:aFileName.
|
|
313 |
|
|
314 |
StartupClass := nil.
|
|
315 |
StartupSelector := nil.
|
|
316 |
Transcript := savedTranscript.
|
|
317 |
Display knownViews:viewsKnown.
|
|
318 |
Display idleBlocks:savedIdleBlocks.
|
|
319 |
Display timeOutBlocks:savedTimeoutBlocks
|
|
320 |
|
|
321 |
"ObjectMemory applicationImageOn:'draw.img' for:DrawTool selector:#start"
|
|
322 |
"ObjectMemory applicationImageOn:'pm.img' for:PMSimulator selector:#start"
|
|
323 |
!
|
|
324 |
|
|
325 |
minimumApplicationImageOn:aFileName for:startupClass selector:startupSelector
|
|
326 |
"create a snapshot which will come up without any views
|
|
327 |
but starts up an application by sending startupClass the startupSelector.
|
|
328 |
All unneeded info is stripped from the saved image."
|
|
329 |
|
|
330 |
"create a temporary image, for continuation"
|
|
331 |
self snapShotOn:'temp.img'.
|
|
332 |
|
|
333 |
Display knownViews do:[:aView |
|
|
334 |
aView notNil ifTrue:[
|
|
335 |
aView superView isNil ifTrue:[
|
|
336 |
aView destroy
|
|
337 |
]
|
|
338 |
]
|
|
339 |
].
|
|
340 |
|
|
341 |
self stripImage.
|
|
342 |
|
|
343 |
self applicationImageOn:aFileName for:startupClass selector:startupSelector.
|
|
344 |
|
|
345 |
"continue in old image"
|
|
346 |
|
|
347 |
OperatingSystem exec:(Arguments at:1)
|
|
348 |
withArguments:#('smalltalk' '-i' 'temp.img') , (Arguments copyFrom:2)
|
|
349 |
|
|
350 |
"ObjectMemory minimumApplicationImageOn:'clock1.img' for:Clock selector:#start"
|
|
351 |
"ObjectMemory applicationImageOn:'clock2.img' for:Clock selector:#start"
|
|
352 |
!
|
|
353 |
|
|
354 |
stripImage
|
|
355 |
"remove all unneeded stuff from the image - much more is possible here"
|
|
356 |
|
|
357 |
"remove all class comments"
|
|
358 |
|
|
359 |
Smalltalk allClassesDo:[:aClass |
|
|
360 |
aClass setComment:nil.
|
|
361 |
aClass methodDictionary do:[:aMethod |
|
|
362 |
aMethod source:''.
|
|
363 |
aMethod category:#none
|
|
364 |
]
|
|
365 |
].
|
|
366 |
self garbageCollect
|
|
367 |
! !
|