-
Notifications
You must be signed in to change notification settings - Fork 1
/
extra-gc.chezscheme.sls
55 lines (47 loc) · 1.17 KB
/
extra-gc.chezscheme.sls
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
(library (extra-gc)
(export
add-collector!
custom-collect
make-custom-collector)
(import (chezscheme))
(define custom-collectors '())
(define custom-collection-count 100)
(meta-cond
[(threaded?)
(define m (make-mutex 'extra-gc))
(define-syntax critical
(syntax-rules ()
[(_ b b* ...)
(with-mutex m
b b* ...)]))]
[else
(alias critical begin)])
(define (add-collector! c)
(assert (procedure? c))
(critical
(set! custom-collectors
(cons c custom-collectors))))
(define make-custom-collector
(case-lambda
[(free guardian)
(make-custom-collector free guardian custom-collection-count)]
[(free guardian count)
(assert
(and (procedure? free)
(procedure? guardian)
(integer? count)
(exact? count)
(positive? count)))
(lambda ()
(do ([count count (- count 1)]
[obj (guardian) (guardian)])
[(or (not obj)
(zero? count))]
(free obj)))]))
(define (custom-collect)
(collect)
(critical
(for-each (lambda (c) (c))
custom-collectors)))
(collect-request-handler custom-collect)
)