| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus collect) |
| 18 |
(export do-overflow do-overflow-words do-vararg-overflow collect |
| 19 |
do-stack-overflow collect-key post-gc-hooks) |
| 20 |
(import |
| 21 |
(except (ikarus) collect collect-key post-gc-hooks) |
| 22 |
(ikarus system $fx) |
| 23 |
(ikarus system $arg-list)) |
| 24 |
|
| 25 |
|
| 26 |
(define post-gc-hooks |
| 27 |
(make-parameter '() |
| 28 |
(lambda (ls) |
| 29 |
;;; null? check so that we don't reference list? and andmap |
| 30 |
;;; at this stage of booting. |
| 31 |
(if (or (null? ls) (and (list? ls) (andmap procedure? ls))) |
| 32 |
ls |
| 33 |
(die 'post-gc-hooks "not a list of procedures" ls))))) |
| 34 |
|
| 35 |
(define (do-post-gc ls n) |
| 36 |
(let ([k0 (collect-key)]) |
| 37 |
(parameterize ([post-gc-hooks '()]) |
| 38 |
(for-each (lambda (x) (x)) ls)) |
| 39 |
(if (eq? k0 (collect-key)) |
| 40 |
(let ([was-enough? (foreign-call "ik_collect_check" n)]) |
| 41 |
;;; handlers ran without GC but there is was not enough |
| 42 |
;;; space in the nursery for the pending allocation, |
| 43 |
(unless was-enough? (do-post-gc ls n))) |
| 44 |
(let () |
| 45 |
;;; handlers did cause a GC, so, do the handlers again. |
| 46 |
(do-post-gc ls n))))) |
| 47 |
|
| 48 |
(define do-overflow |
| 49 |
(lambda (n) |
| 50 |
(foreign-call "ik_collect" n) |
| 51 |
(let ([ls (post-gc-hooks)]) |
| 52 |
(unless (null? ls) (do-post-gc ls n))))) |
| 53 |
|
| 54 |
(define do-overflow-words |
| 55 |
(lambda (n) |
| 56 |
(let ([n ($fxsll n 2)]) |
| 57 |
(foreign-call "ik_collect" n) |
| 58 |
(let ([ls (post-gc-hooks)]) |
| 59 |
(unless (null? ls) (do-post-gc ls n)))))) |
| 60 |
|
| 61 |
(define do-vararg-overflow do-overflow) |
| 62 |
|
| 63 |
(define collect |
| 64 |
(lambda () |
| 65 |
(do-overflow 4096))) |
| 66 |
|
| 67 |
(define do-stack-overflow |
| 68 |
(lambda () |
| 69 |
(foreign-call "ik_stack_overflow"))) |
| 70 |
|
| 71 |
(define dump-metatable |
| 72 |
(lambda () |
| 73 |
(foreign-call "ik_dump_metatable"))) |
| 74 |
|
| 75 |
(define dump-dirty-vector |
| 76 |
(lambda () |
| 77 |
(foreign-call "ik_dump_dirty_vector"))) |
| 78 |
|
| 79 |
(define (collect-key) |
| 80 |
(or ($collect-key) |
| 81 |
(begin |
| 82 |
($collect-key (gensym)) |
| 83 |
(collect-key)))) |
| 84 |
|
| 85 |
) |