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:''
|
2
|
15 |
classVariableNames:'InternalErrorHandler UserInterruptHandler TimerInterruptHandler
|
|
16 |
SpyInterruptHandler StepInterruptHandler ExceptionInterruptHandler
|
|
17 |
ErrorInterruptHandler MemoryInterruptHandler SignalInterruptHandler
|
|
18 |
ChildSignalInterruptHandler DisposeInterruptHandler
|
|
19 |
RecursionInterruptHandler IOInterruptHandler
|
|
20 |
|
|
21 |
AllocationFailureSignal
|
|
22 |
IncrementalGCLimit'
|
1
|
23 |
poolDictionaries:''
|
|
24 |
category:'System-Support'
|
|
25 |
!
|
|
26 |
|
|
27 |
ObjectMemory comment:'
|
|
28 |
|
|
29 |
COPYRIGHT (c) 1992 -93 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
3
|
32 |
$Header: /cvs/stx/stx/libbasic/ObjectMemory.st,v 1.3 1993-10-13 00:16:47 claus Exp $
|
2
|
33 |
'!
|
|
34 |
|
|
35 |
!ObjectMemory class methodsFor:'documentation'!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
1
|
39 |
This class contains access methods to the system memory -
|
|
40 |
in previous versions this stuff used to be in the Smalltalk class.
|
|
41 |
It has been separated for better overall structure.
|
|
42 |
|
2
|
43 |
kludge:
|
|
44 |
The InterruptHandler variables are known by the runtime system -
|
|
45 |
they are the objects that get an interrupt message when the event
|
|
46 |
occurs.
|
|
47 |
|
|
48 |
ClassVariables:
|
|
49 |
|
|
50 |
InternalErrorHandler gets informed, when some runtime error occurs
|
|
51 |
(usually fatal)
|
|
52 |
|
|
53 |
UserInterruptHandler gets informed when CNTL-C is pressed
|
|
54 |
TimerInterruptHandler gets alarm timer interrupts
|
|
55 |
SpyInterruptHandler another alarm timer
|
|
56 |
StepInterruptHandler gets single step interrupts
|
|
57 |
ExceptionInterruptHandler gets floating point exceptions
|
|
58 |
ErrorInterruptHandler gets graphic device errors
|
|
59 |
MemoryInterruptHandler gets out of memory conditions
|
|
60 |
SignalInterruptHandler gets unix signals
|
|
61 |
ChildSignalInterruptHandler gets child death signals
|
|
62 |
DisposeInterruptHandler gets informed, when an object is disposed from
|
|
63 |
a shadowArray
|
|
64 |
RecursionInterruptHandler gets recursion limit violations
|
|
65 |
IOInterruptHandler get SIGIO unix signals
|
|
66 |
|
|
67 |
AllocationFailureSignal signal raised when a new fails (see Behavior)
|
|
68 |
IngrementalGCLimit number of bytes, that must be allocated since
|
|
69 |
last full garbage collect to turn on incremental
|
|
70 |
collector.
|
|
71 |
"
|
|
72 |
! !
|
|
73 |
|
|
74 |
!ObjectMemory class methodsFor:'initialization'!
|
|
75 |
|
|
76 |
initialize
|
|
77 |
AllocationFailureSignal isNil ifTrue:[
|
|
78 |
AllocationFailureSignal := (Signal new) mayProceed:true.
|
|
79 |
AllocationFailureSignal notifierString:'allocation failure'.
|
|
80 |
].
|
|
81 |
IncrementalGCLimit := 500000.
|
|
82 |
! !
|
|
83 |
|
|
84 |
!ObjectMemory class methodsFor:'signal access'!
|
|
85 |
|
|
86 |
allocationFailureSignal
|
|
87 |
^ AllocationFailureSignal
|
|
88 |
! !
|
1
|
89 |
|
|
90 |
!ObjectMemory class methodsFor:'cache management'!
|
|
91 |
|
|
92 |
flushInlineCachesForClass:aClass
|
|
93 |
"flush inlinecaches for calls to aClass"
|
|
94 |
|
|
95 |
%{ /* NOCONTEXT */
|
|
96 |
__flushInlineCachesFor(aClass);
|
|
97 |
%}
|
|
98 |
!
|
|
99 |
|
|
100 |
flushInlineCachesWithArgs:nargs
|
|
101 |
"flush inlinecaches for calls with nargs arguments"
|
|
102 |
|
|
103 |
%{ /* NOCONTEXT */
|
|
104 |
__flushInlineCaches(_intVal(nargs));
|
|
105 |
%}
|
|
106 |
!
|
|
107 |
|
|
108 |
flushInlineCachesFor:aClass withArgs:nargs
|
|
109 |
"flush inlinecaches for calls to aClass with nargs arguments"
|
|
110 |
|
|
111 |
%{ /* NOCONTEXT */
|
|
112 |
__flushInlineCachesForAndNargs(aClass, _intVal(nargs));
|
|
113 |
%}
|
|
114 |
!
|
|
115 |
|
|
116 |
flushInlineCaches
|
|
117 |
"flush all inlinecaches"
|
|
118 |
|
|
119 |
%{ /* NOCONTEXT */
|
|
120 |
__flushAllInlineCaches();
|
|
121 |
%}
|
|
122 |
!
|
|
123 |
|
|
124 |
flushMethodCacheFor:aClass
|
|
125 |
"flush the method cache for sends to aClass"
|
|
126 |
|
|
127 |
%{ /* NOCONTEXT */
|
|
128 |
__flushMethodCacheFor(aClass);
|
|
129 |
%}
|
|
130 |
!
|
|
131 |
|
|
132 |
flushMethodCache
|
|
133 |
"flush the method cache"
|
|
134 |
|
|
135 |
%{ /* NOCONTEXT */
|
|
136 |
__flushMethodCache();
|
|
137 |
%}
|
|
138 |
!
|
|
139 |
|
2
|
140 |
flushCachesFor:aClass
|
|
141 |
"flush method and inline caches for aClass"
|
|
142 |
|
|
143 |
%{ /* NOCONTEXT */
|
|
144 |
__flushMethodCacheFor(aClass);
|
|
145 |
__flushInlineCachesFor(aClass);
|
|
146 |
%}
|
|
147 |
!
|
|
148 |
|
1
|
149 |
flushCaches
|
2
|
150 |
"flush method and inline caches for all classes"
|
1
|
151 |
|
|
152 |
%{ /* NOCONTEXT */
|
|
153 |
__flushMethodCache();
|
|
154 |
__flushAllInlineCaches();
|
|
155 |
%}
|
|
156 |
! !
|
|
157 |
|
|
158 |
!ObjectMemory class methodsFor:'enumeration'!
|
|
159 |
|
|
160 |
allObjectsDo:aBlock
|
|
161 |
"evaluate the argument, aBlock for all objects in the system"
|
2
|
162 |
|
|
163 |
|work|
|
1
|
164 |
%{
|
2
|
165 |
nonTenuringScavenge(__context);
|
|
166 |
/*
|
|
167 |
* allObjectsDo needs a temporary to hold newSpace objects
|
|
168 |
*/
|
|
169 |
__allObjectsDo(&aBlock, &work COMMA_CON);
|
|
170 |
%}
|
|
171 |
!
|
|
172 |
|
|
173 |
allOldObjectsDo:aBlock
|
|
174 |
"evaluate the argument, aBlock for all old objects in the system.
|
|
175 |
For debugging and tests only - do not use"
|
|
176 |
%{
|
|
177 |
__allObjectsDo(&aBlock, (OBJ *)0 COMMA_CON);
|
1
|
178 |
%}
|
|
179 |
! !
|
|
180 |
|
2
|
181 |
!ObjectMemory class methodsFor:'handler access'!
|
|
182 |
|
|
183 |
internalErrorHandler
|
|
184 |
^ InternalErrorHandler
|
|
185 |
!
|
|
186 |
|
|
187 |
userInterruptHandler
|
|
188 |
^ UserInterruptHandler
|
|
189 |
!
|
|
190 |
|
|
191 |
userInterruptHandler:aHandler
|
|
192 |
UserInterruptHandler := aHandler
|
|
193 |
!
|
|
194 |
|
|
195 |
timerInterruptHandler
|
|
196 |
^ TimerInterruptHandler
|
|
197 |
!
|
|
198 |
|
|
199 |
spyInterruptHandler
|
|
200 |
^ SpyInterruptHandler
|
|
201 |
!
|
|
202 |
|
|
203 |
spyInterruptHandler:aHandler
|
|
204 |
SpyInterruptHandler := aHandler
|
|
205 |
!
|
|
206 |
|
|
207 |
stepInterruptHandler
|
|
208 |
^ StepInterruptHandler
|
|
209 |
!
|
|
210 |
|
|
211 |
stepInterruptHandler:aHandler
|
|
212 |
StepInterruptHandler := aHandler
|
|
213 |
!
|
|
214 |
|
|
215 |
exceptionInterruptHandler
|
|
216 |
^ ExceptionInterruptHandler
|
|
217 |
!
|
|
218 |
|
|
219 |
errorInterruptHandler
|
|
220 |
^ ErrorInterruptHandler
|
|
221 |
!
|
|
222 |
|
|
223 |
errorInterruptHandler:aHandler
|
|
224 |
ErrorInterruptHandler := aHandler
|
|
225 |
!
|
|
226 |
|
|
227 |
memoryInterruptHandler
|
|
228 |
^ MemoryInterruptHandler
|
|
229 |
!
|
|
230 |
|
|
231 |
signalInterruptHandler
|
|
232 |
^ SignalInterruptHandler
|
|
233 |
!
|
|
234 |
|
|
235 |
childSignalInterruptHandler
|
|
236 |
^ ChildSignalInterruptHandler
|
|
237 |
!
|
|
238 |
|
|
239 |
disposeInterruptHandler
|
|
240 |
^ DisposeInterruptHandler
|
|
241 |
!
|
|
242 |
|
|
243 |
disposeInterruptHandler:aHandler
|
|
244 |
DisposeInterruptHandler := aHandler
|
|
245 |
!
|
|
246 |
|
|
247 |
recursionInterruptHandler
|
|
248 |
^ RecursionInterruptHandler
|
|
249 |
!
|
|
250 |
|
|
251 |
ioInterruptHandler
|
|
252 |
^ IOInterruptHandler
|
|
253 |
!
|
|
254 |
|
|
255 |
ioInterruptHandler:aHandler
|
|
256 |
IOInterruptHandler := aHandler
|
|
257 |
! !
|
|
258 |
|
1
|
259 |
!ObjectMemory class methodsFor:'queries'!
|
|
260 |
|
|
261 |
newSpaceUsed
|
|
262 |
"return the number of bytes allocated for new objects"
|
|
263 |
|
|
264 |
%{ /* NOCONTEXT */
|
|
265 |
RETURN ( _MKSMALLINT(__newSpaceUsed()) );
|
|
266 |
%}
|
|
267 |
"ObjectMemory newSpaceUsed"
|
|
268 |
!
|
|
269 |
|
|
270 |
oldSpaceUsed
|
|
271 |
"return the number of bytes allocated for old objects"
|
|
272 |
|
|
273 |
%{ /* NOCONTEXT */
|
|
274 |
RETURN ( _MKSMALLINT(__oldSpaceUsed()) );
|
|
275 |
%}
|
|
276 |
"ObjectMemory oldSpaceUsed"
|
|
277 |
!
|
|
278 |
|
|
279 |
bytesUsed
|
|
280 |
"return the number of bytes allocated for objects -
|
|
281 |
this number is not exact, since some objects may be dead"
|
|
282 |
|
|
283 |
%{ /* NOCONTEXT */
|
|
284 |
RETURN ( _MKSMALLINT(__oldSpaceUsed() + __newSpaceUsed()) );
|
|
285 |
%}
|
|
286 |
"ObjectMemory bytesUsed"
|
|
287 |
!
|
|
288 |
|
2
|
289 |
oldSpaceAllocatedSinceLastGC
|
|
290 |
"return the number of bytes allocated for old objects since the
|
|
291 |
last garbage collect occured"
|
|
292 |
|
|
293 |
%{ /* NOCONTEXT */
|
|
294 |
RETURN ( _MKSMALLINT(__oldSpaceAllocatedSinceLastGC()) );
|
|
295 |
%}
|
|
296 |
"ObjectMemory oldSpaceAllocatedSinceLastGC"
|
|
297 |
!
|
|
298 |
|
|
299 |
incrementalGCLimit
|
|
300 |
"return the limit for incremental GC activation"
|
|
301 |
|
|
302 |
^ IncrementalGCLimit
|
|
303 |
|
|
304 |
"ObjectMemory incrementalGCLimit"
|
|
305 |
!
|
|
306 |
|
1
|
307 |
numberOfObjects
|
|
308 |
"return the number of objects in the system"
|
|
309 |
|
|
310 |
|tally|
|
|
311 |
|
|
312 |
tally := 0.
|
|
313 |
self allObjectsDo:[:obj | tally := tally + 1].
|
|
314 |
^ tally
|
|
315 |
|
|
316 |
"ObjectMemory numberOfObjects"
|
|
317 |
!
|
|
318 |
|
|
319 |
printReferences:anObject
|
|
320 |
"debugging: print referents to anObject"
|
|
321 |
|
|
322 |
%{
|
|
323 |
_printRefChain(__context, anObject);
|
|
324 |
%}
|
|
325 |
!
|
|
326 |
|
|
327 |
whoReferences:anObject
|
|
328 |
"return a collection of objects referencing the argument, anObject"
|
|
329 |
|
|
330 |
|aCollection|
|
|
331 |
|
|
332 |
aCollection := IdentitySet new.
|
|
333 |
self allObjectsDo:[:o |
|
|
334 |
(o references:anObject) ifTrue:[
|
|
335 |
aCollection add:o
|
|
336 |
]
|
|
337 |
].
|
|
338 |
(aCollection size == 0) ifTrue:[
|
|
339 |
"actually this cannot happen - there is always one"
|
|
340 |
^ nil
|
|
341 |
].
|
|
342 |
^ aCollection
|
|
343 |
! !
|
|
344 |
|
|
345 |
!ObjectMemory class methodsFor:'garbage collector control'!
|
|
346 |
|
|
347 |
garbageCollect
|
|
348 |
"search for and free garbage in the oldSpace
|
|
349 |
(newSpace is cleaned automatically)
|
|
350 |
- can take a long time if paging is involved
|
|
351 |
- when no paging is involved, its faster than I thought :-)"
|
|
352 |
%{
|
|
353 |
__garbageCollect(__context);
|
|
354 |
%}
|
|
355 |
|
|
356 |
"ObjectMemory garbageCollect"
|
|
357 |
!
|
|
358 |
|
|
359 |
scavenge
|
|
360 |
"for debugging only - collect newspace stuff"
|
|
361 |
%{
|
|
362 |
nonTenuringScavenge(__context);
|
|
363 |
%}
|
|
364 |
|
|
365 |
"ObjectMemory scavenge"
|
|
366 |
!
|
|
367 |
|
|
368 |
tenure
|
2
|
369 |
"force all new stuff into old-space"
|
1
|
370 |
%{
|
|
371 |
tenure(__context);
|
|
372 |
%}
|
|
373 |
|
|
374 |
"ObjectMemory tenure"
|
|
375 |
!
|
|
376 |
|
|
377 |
markAndSweep
|
|
378 |
"mark/sweep garbage collector"
|
|
379 |
|
|
380 |
%{
|
|
381 |
markAndSweep(__context);
|
|
382 |
%}
|
|
383 |
|
|
384 |
"ObjectMemory markAndSweep"
|
|
385 |
!
|
|
386 |
|
|
387 |
gcStep
|
|
388 |
"one incremental garbage collect step"
|
|
389 |
%{
|
|
390 |
incrGCstep(__context);
|
|
391 |
%}
|
|
392 |
!
|
|
393 |
|
2
|
394 |
incrementalGCLimit:aNumber
|
|
395 |
"set the limit for incremental GC activation"
|
|
396 |
|
|
397 |
IncrementalGCLimit := aNumber
|
|
398 |
|
|
399 |
"ObjectMemory incrementalGCLimit:100000"
|
|
400 |
!
|
|
401 |
|
|
402 |
turnGarbageCollectorOff
|
|
403 |
"turn off garbage collector by forcing new objects to be
|
|
404 |
allocated in oldSpace (instead of newSpace)
|
1
|
405 |
this method is somewhat dangerous: if collector is turned off,
|
|
406 |
and too many objects are created, the system may run into trouble.
|
|
407 |
Use this only for measurement purposes or when realtime behavior
|
2
|
408 |
is required for a limited time period. No warranty"
|
1
|
409 |
%{
|
|
410 |
allocForceSpace(0);
|
|
411 |
%}
|
|
412 |
!
|
|
413 |
|
2
|
414 |
turnGarbageCollectorOn
|
|
415 |
"turn garbage collector on again"
|
1
|
416 |
|
|
417 |
%{
|
|
418 |
allocForceSpace(1);
|
|
419 |
%}
|
|
420 |
! !
|
|
421 |
|
|
422 |
!ObjectMemory class methodsFor:'system management'!
|
|
423 |
|
|
424 |
loadClassBinary:aClassName
|
|
425 |
"find the object file for aClassName and -if found - load it;
|
|
426 |
this one loads precompiled object files"
|
|
427 |
|
|
428 |
|fName newClass upd|
|
|
429 |
|
|
430 |
fName := self fileNameForClass:aClassName.
|
|
431 |
fName notNil ifTrue:[
|
|
432 |
upd := Class updateChanges:false.
|
|
433 |
[
|
|
434 |
self loadBinary:(fName , '.o')
|
|
435 |
] valueNowOrOnUnwindDo:[
|
|
436 |
Class updateChanges:upd
|
|
437 |
].
|
|
438 |
newClass := self at:(aClassName asSymbol).
|
|
439 |
(newClass notNil and:[newClass implements:#initialize]) ifTrue:[
|
|
440 |
newClass initialize
|
|
441 |
]
|
|
442 |
]
|
|
443 |
!
|
|
444 |
|
|
445 |
snapShot
|
|
446 |
"create a snapshot"
|
|
447 |
|
|
448 |
ImageName isNil ifTrue:[
|
|
449 |
ImageName := 'st.img'
|
|
450 |
].
|
|
451 |
self snapShotOn:ImageName
|
|
452 |
|
|
453 |
"ObjectMemory snapShot"
|
|
454 |
!
|
|
455 |
|
|
456 |
snapShotOn:aFileName
|
|
457 |
"create a snapshot in the given file"
|
|
458 |
|
|
459 |
"give others a chance to fix things"
|
|
460 |
self changed:#save.
|
|
461 |
%{
|
|
462 |
OBJ __snapShotOn();
|
|
463 |
|
|
464 |
if (_isString(aFileName)) {
|
|
465 |
RETURN ( __snapShotOn(__context, _stringVal(aFileName)) );
|
|
466 |
}
|
|
467 |
%}
|
|
468 |
.
|
|
469 |
^ self primitiveFailed
|
|
470 |
|
|
471 |
"ObjectMemory snapShotOn:'myimage.img'"
|
|
472 |
!
|
|
473 |
|
|
474 |
applicationImageOn:aFileName for:startupClass selector:startupSelector
|
|
475 |
"create a snapshot which will come up without any views
|
|
476 |
but starts up an application by sending startupClass the startupSelector"
|
|
477 |
|
2
|
478 |
|viewsKnown savedIdleBlocks savedTimeoutBlocks savedTranscript
|
|
479 |
savedRoot|
|
1
|
480 |
|
|
481 |
viewsKnown := Display knownViews.
|
|
482 |
savedIdleBlocks := Display idleBlocks.
|
|
483 |
savedTimeoutBlocks := Display timeOutBlocks.
|
|
484 |
savedTranscript := Transcript.
|
2
|
485 |
savedRoot := RootView.
|
1
|
486 |
|
|
487 |
"a kludge: save image with modified knownViews ..."
|
|
488 |
|
|
489 |
Display knownViews:nil.
|
|
490 |
Display idleBlocks:nil.
|
|
491 |
Display timeOutBlocks:nil.
|
2
|
492 |
RootView := nil.
|
|
493 |
|
1
|
494 |
Transcript := Stderr.
|
|
495 |
StartupClass := startupClass.
|
|
496 |
StartupSelector := startupSelector.
|
|
497 |
|
|
498 |
self snapShotOn:aFileName.
|
|
499 |
|
|
500 |
StartupClass := nil.
|
|
501 |
StartupSelector := nil.
|
2
|
502 |
RootView := savedRoot.
|
1
|
503 |
Transcript := savedTranscript.
|
|
504 |
Display knownViews:viewsKnown.
|
|
505 |
Display idleBlocks:savedIdleBlocks.
|
|
506 |
Display timeOutBlocks:savedTimeoutBlocks
|
|
507 |
|
|
508 |
"ObjectMemory applicationImageOn:'draw.img' for:DrawTool selector:#start"
|
|
509 |
"ObjectMemory applicationImageOn:'pm.img' for:PMSimulator selector:#start"
|
|
510 |
!
|
|
511 |
|
|
512 |
minimumApplicationImageOn:aFileName for:startupClass selector:startupSelector
|
|
513 |
"create a snapshot which will come up without any views
|
|
514 |
but starts up an application by sending startupClass the startupSelector.
|
|
515 |
All unneeded info is stripped from the saved image."
|
|
516 |
|
|
517 |
"create a temporary image, for continuation"
|
|
518 |
self snapShotOn:'temp.img'.
|
|
519 |
|
|
520 |
Display knownViews do:[:aView |
|
|
521 |
aView notNil ifTrue:[
|
|
522 |
aView superView isNil ifTrue:[
|
|
523 |
aView destroy
|
|
524 |
]
|
|
525 |
]
|
|
526 |
].
|
|
527 |
|
|
528 |
self stripImage.
|
|
529 |
|
|
530 |
self applicationImageOn:aFileName for:startupClass selector:startupSelector.
|
|
531 |
|
|
532 |
"continue in old image"
|
|
533 |
|
|
534 |
OperatingSystem exec:(Arguments at:1)
|
|
535 |
withArguments:#('smalltalk' '-i' 'temp.img') , (Arguments copyFrom:2)
|
|
536 |
|
|
537 |
"ObjectMemory minimumApplicationImageOn:'clock1.img' for:Clock selector:#start"
|
|
538 |
"ObjectMemory applicationImageOn:'clock2.img' for:Clock selector:#start"
|
|
539 |
!
|
|
540 |
|
|
541 |
stripImage
|
|
542 |
"remove all unneeded stuff from the image - much more is possible here"
|
|
543 |
|
2
|
544 |
"remove all class comments & source"
|
1
|
545 |
|
2
|
546 |
Smalltalk allBehaviorsDo:[:aClass |
|
1
|
547 |
aClass setComment:nil.
|
|
548 |
aClass methodDictionary do:[:aMethod |
|
|
549 |
aMethod source:''.
|
|
550 |
aMethod category:#none
|
|
551 |
]
|
|
552 |
].
|
|
553 |
self garbageCollect
|
|
554 |
! !
|