forked from gambit/gambit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mem.scm
510 lines (382 loc) · 11.5 KB
/
mem.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
; File: "mem.scm", Time-stamp: <2007-04-04 11:40:14 feeley>
; Copyright (c) 1996-2007 by Marc Feeley, All Rights Reserved.
; Test program for Gambit-C's memory management system.
;------------------------------------------------------------------------------
; Utilities:
; pseudo-random number generator
(define seed #f)
(define (reset-random)
(set! seed 1923202963))
(define (random n)
(let* ((hi (quotient seed 44488))
(lo (remainder seed 44488))
(test (- (* lo 48271) (* hi 3399))))
(set! seed (if (> test 0) test (+ test 2147483647)))
(modulo seed n)))
; repetition of an evaluation
(define (repeat n thunk)
(let loop ((i n))
(if (> i 0)
(begin
(thunk)
(loop (- i 1))))))
(define gc-reports? #t)
(define (setup-gc-reports)
(gc-report-set! gc-reports?))
(define (trigger-gc)
(##gc))
;------------------------------------------------------------------------------
; Test #1
;
; Allocate and deallocate medium size objects and large objects. The
; GC reports show that movable and non-movable objects are allocated
; and reclaimed.
(define (test1)
(setup-gc-reports)
(reset-random)
(trigger-gc)
(let* ((n 4)
(v (make-vector n #f)))
(repeat
200
(lambda ()
(let ((i (random n))
(size (if (= 0 (random 2)) 1000 35000)))
(vector-set! v i (make-vector size))))))
(trigger-gc))
;------------------------------------------------------------------------------
; Test #2
;
; Allocate and deallocate medium size objects and large objects. The
; GC reports show that movable and non-movable objects are allocated
; and reclaimed.
(define (test2)
(setup-gc-reports)
(reset-random)
(trigger-gc)
(set! test2-data '())
; allocate roughly 1 MB of data
(repeat
10
(lambda ()
(set! test2-data
(cons (make-vector (if (= 0 (random 2)) 1000 35000))
test2-data))))
(idle)
; release roughly .5 MB of data
(repeat
5
(lambda ()
(set! test2-data (cdr test2-data))))
(idle)
; allocate roughly 1 MB more
(repeat
10
(lambda ()
(set! test2-data
(cons (make-vector (if (= 0 (random 2)) 1000 35000))
test2-data))))
(idle)
; release it all
(set! test2-data #f)
(trigger-gc))
(define test2-data #f)
(define (idle)
(let loop ((i 20))
(if (> i 0)
(begin
(make-vector (if (= 0 (random 2)) 1000 35000))
(loop (- i 1))))))
;------------------------------------------------------------------------------
; Test #3
;
; Show how to recover from heap overflows.
;
; Note: make sure you specify a heap size limit when you start the
; compiler or interpreter, otherwise it may take a long time before
; the virtual memory is exhausted.
(define (test3)
(setup-gc-reports)
(trigger-gc)
(test3-helper 10) ; this call should terminate normally
(test3-helper 1000000) ; this one will cause a heap overflow
(test3-helper 10) ; this call should terminate normally
)
(define (test3-helper n)
(reset-random)
(let ((action
(call-with-current-continuation
(lambda (abort)
(with-exception-handler
(lambda (exc)
(abort
(lambda ()
(trigger-gc) ; clean up heap and recover overflow reserve
(display "caught exception ")
(write exc)
(newline))))
(lambda ()
(let ((data (create-big-set-of-data n)))
(lambda ()
(trigger-gc)
(write (map vector? data))
(newline)))))))))
(action)))
(define (create-big-set-of-data n)
(let loop ((lst '()) (i n))
(if (> i 0)
(loop
(cons (case (random 6)
((0) (make-string (random 80000)))
((1) (make-vector (random 40000)))
((2) (cons lst lst))
((3) (gensym))
((4) (lambda (x) lst))
(else (* 1.23 (random 100))))
lst)
(- i 1))
lst)))
;------------------------------------------------------------------------------
; Test #4
;
; Show how to recover from stack overflows.
;
; Note: make sure you specify a heap size limit when you start the
; compiler or interpreter, otherwise it may take a long time before
; the virtual memory is exhausted.
(define (test4)
(setup-gc-reports)
(trigger-gc)
(test4-helper 10) ; this call should terminate normally
(test4-helper 1000000) ; this one will cause a heap overflow
(test4-helper 10) ; this call should terminate normally
)
(define (test4-helper n)
(reset-random)
(let ((action
(call-with-current-continuation
(lambda (abort)
(with-exception-handler
(lambda (exc)
(abort
(lambda ()
(trigger-gc) ; clean up heap and recover overflow reserve
(display "caught exception ")
(write exc)
(newline))))
(lambda ()
(let ((data (deeply-nested-computation n)))
(lambda ()
(trigger-gc)
(write (map even? data))
(newline)))))))))
(action)))
(define (deeply-nested-computation n)
(if (> n 0)
(cons n (deeply-nested-computation (- n 1)))
'()))
;------------------------------------------------------------------------------
; Test #5
;
; Allocation of Scheme objects from C.
(c-declare "
#include <math.h>
static ___SCMOBJ obj1, obj2, obj3, obj4, obj5; /* some Scheme objects */
void foo (double x, int n, int (*f)(char*))
{
___EXT(___DOUBLE_to_SCMOBJ) (___CLIBEXT(sqrt) (x), &obj1, 0);
obj2 = ___EXT(___make_vector) (n, obj1, ___STILL);
___EXT(___CHARSTRING_to_SCMOBJ) (\"hello world!\", &obj3, 0);
___EXT(___CHARSTRING_to_SCMOBJ) (\"another string\", &obj4, 0);
obj5 = ___EXT(___make_pair) (obj2, obj3, ___STILL);
___EXT(___still_obj_refcount_dec) (obj1); /* no direct need for obj1, etc */
___EXT(___still_obj_refcount_dec) (obj2);
___EXT(___still_obj_refcount_dec) (obj3);
f (\"was called from C\");
}
___SCMOBJ bar ()
{
return obj4;
}
___SCMOBJ baz ()
{
___EXT(___still_obj_refcount_dec) (obj5);
return obj5;
}
")
(c-define-type real double)
(c-define-type integer int)
(define foo (c-lambda (real integer (function (char-string) int)) void "foo"))
(define bar (c-lambda () scheme-object "bar"))
(define baz (c-lambda () scheme-object "baz"))
(c-define (hop str) (char-string) int "hop" ""
(write (list 'hop str))
(newline)
(string-length str))
(define (test5)
(setup-gc-reports)
(trigger-gc)
(hop "was called from Scheme")
(let ()
(declare (standard-bindings) (not safe) (flonum)) ; inline "sqrt"
(foo (sqrt (##first-argument .0625)) 5 hop)) ; create objects from C
(trigger-gc) ; they will show up as "not movable" objects in the GC report
(let ((x (bar))) ; get obj4 from C
(trigger-gc) ; no problem if a GC happens here
(write x)
(newline))
(trigger-gc)
(let ((y (baz)))
(trigger-gc)
(write y)
(newline))
(trigger-gc)) ; at this point, only obj4 is not reclaimed (because refcount != 0)
;------------------------------------------------------------------------------
; Test #6
;
; Finalization test.
(define (test6)
(setup-gc-reports)
(reset-random)
(trigger-gc)
(trigger-gc)
(let* ((n 100)
(v (make-vector n #f))
(c 0)
(expect 0)
(result 0))
(define (new x)
(let ((obj
(case (random 8)
((0)
(make-string (random 10)))
((1)
(make-vector (random 10) x))
((2)
(cons x x))
((3)
(gensym))
((4)
(lambda (y) x))
((5)
(make-will (cons x x) (lambda (o) #f)))
((6)
(let ((id c))
(set! expect (+ expect id))
(set! c (+ c 1))
(make-will (make-vector (random 40000))
(lambda (o) (set! result (+ result id))))))
(else
(+ 1.23 (random 100))))))
(if (= (random 2) 0)
(make-will obj (lambda (o) #f))
(let ((id c))
(set! expect (+ expect id))
(set! c (+ c 1))
(make-will obj
(lambda (o) (set! result (+ result id))))))
obj))
(repeat
2000
(lambda ()
(let ((i (random n)))
(vector-set! v i (new (vector-ref v (random n)))))))
(set! v #f)
(write '(final-gc))
(newline)
(trigger-gc)
(write expect)
(newline)
(write result)
(newline)
(trigger-gc)))
;------------------------------------------------------------------------------
; Test #7
;
; Finalization test.
(define (test7)
(define (printer name)
(lambda (o)
(display "===> calling the action procedure for object ")
(display name)
(display " = ")
(write o)
(newline)))
(define (gc)
(display "--------------------------------------- GC")
(newline)
(trigger-gc))
(let* ((obj1 (list 'obj1))
(obj2 (list 'obj2))
(obj3 (list 'obj3))
(obj4 (list 'obj4 (make-will (list 'obj4) (lambda (o) #f))))
(obj5 (list 'obj5 (make-will (list 'obj5) (lambda (o) #f))))
(obj6 (list 'obj6 (make-will (list 'obj6) (lambda (o) #f))))
(obj7 (list 'obj7 (make-will (list 'obj7) (lambda (o) #f))))
(obj8 (list 'obj8 (make-will (list 'obj8) (lambda (o) #f))))
(obj9 (vector 1 2 3 4 5)))
(let* ((will1 (make-will obj1 (lambda (o) #f)))
(will2 (make-will obj2 (printer 'obj2)))
(will3 (make-will obj3 (lambda (o) #f)))
(will4 (make-will obj4 (printer 'obj4)))
(will5 (make-will obj5 (lambda (o) #f)))
(will6 (make-will obj6 (printer 'obj6)))
(will7 (make-will obj7 (lambda (o) #f)))
(will8 (make-will obj8 (printer 'obj8))))
(define (print-testators)
(define (print name will)
(display "(will-testator ")
(display name)
(display ") = ")
(write (will-testator will))
(newline))
(print 'will1 will1)
(print 'will2 will2)
(print 'will3 will3)
(print 'will4 will4)
(print 'will5 will5)
(print 'will6 will6)
(print 'will7 will7)
(print 'will8 will8))
(let ((obj obj9)
(p (printer 'obj9)))
(make-will obj
(lambda (o) (p o) (set! obj9 o)))) ; resurect object
(setup-gc-reports)
(gc)
(gc)
(print-testators)
(gc)
(print-testators)
(set! obj1 123)
(gc)
(print-testators)
(set! obj2 123)
(set! obj3 123)
(set! obj4 123)
(set! obj5 123)
(set! obj6 123)
(set! obj7 123)
(gc)
(print-testators)
(set! obj8 123)
(set! obj9 123)
(pretty-print obj9)
(gc)
(print-testators)
(pretty-print obj9)
(gc)
(gc))))
;------------------------------------------------------------------------------
(define (test-all)
(set! gc-reports? #f)
(test1)
(test2)
(test3)
(test4)
(test5)
(test6)
(test7)
(pretty-print 'all-tests-done))
(test-all)
(exit)