From a85d95aa8911ffa85c6763890ea45815a882d51c Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Thu, 24 Apr 2025 13:57:54 -0300 Subject: [PATCH] Improve 3.rkt a bit --- Final/3.rkt | 67 ++++++++++++++--------------------------------------- 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/Final/3.rkt b/Final/3.rkt index dd003f5..e7c7ef2 100644 --- a/Final/3.rkt +++ b/Final/3.rkt @@ -98,55 +98,28 @@ ;; malloc : size -> address (define (malloc n . other-roots) - (when (> (+ (ptr) n (length other-roots)) (space-size)) + (when (> (+ (ptr) n) (space-size)) (gc!)) - (when (> (+ (ptr) n (length other-roots)) (space-size)) + (when (> (+ (ptr) n) (space-size)) (error 'malloc "out of memory!")) - (heap-set! LOC:PTR (+ (ptr) n (length other-roots))) - (+ (heap-ref LOC:OFFSET) (- (ptr) n (length other-roots)))) + (heap-set! LOC:PTR (+ (ptr) n)) + (+ (heap-ref LOC:OFFSET) (- (ptr) n))) (define (forward/root thing) (define addr (read-root thing)) + (define fwrd (+ 1 addr)) (cond - [(gc:flat? addr) (gc:alloc-flat (gc:deref addr))] - [(gc:cons? addr) (gc:cons (gc:first thing) (gc:rest addr))] - [(gc:closure? addr) (gc:closure (gc:closure-code-ptr addr) (empty))])) - -(define (forward/loc thing) - (define tag (heap-ref thing)) - (cond - [(equal? tag 'forwarded) - (void)] - [(equal? tag 'flat) - (heap-set! thing 'forwarded) - (heap-set! (+ thing 1) (gc:alloc-flat (heap-ref (+ thing 1))))] - [(equal? tag 'cons) - (heap-set! thing 'forwarded) - (heap-set! (+ thing 1) (gc:cons - (simple-root (heap-ref (+ thing 1))) - (simple-root (heap-ref (+ thing 2)))))] - [(equal? tag 'clos) - (let* ([code-ptr (heap-ref (+ thing 1))] - [n-vars (heap-ref (+ thing 2))] - [free-vars (for/list ([i (in-range n-vars)]) - (simple-root (heap-ref (+ thing 3 i))))]) - (heap-set! thing 'forwarded) - (heap-set! (+ thing 1) (gc:closure code-ptr free-vars)))] - [else - (error 'forward/loc "unknown tag: ~a" tag)])) - - -(define (object-size addr) - (define tag (heap-ref addr)) - (cond - [(equal? tag 'flat) 2] - [(equal? tag 'cons) 3] - [(equal? tag 'clos) - (let ([n-vars (+ addr 2)]) - (+ 3 n-vars))] - [(equal? tag 'forwarded) 2] - [else - (error 'object-size "unknown tag: ~a" tag)])) + [(gc:flat? addr) (heap-set! fwrd (gc:alloc-flat (gc:deref addr)))] + ; maybe forward root on each cons cell? could fix what seems to be memory corruption + [(gc:cons? addr) (heap-set! fwrd (gc:cons (simple-root (gc:first addr)) (simple-root (gc:rest addr))))] + [(gc:closure? addr) + (let ([code-ptr (gc:closure-code-ptr addr)] + [n-vars (heap-ref (+ addr 2))]) + (heap-set! fwrd + (gc:closure code-ptr + (for/list ([i (in-range n-vars)]) + (forward/root (simple-root (gc:closure-env-ref addr i)))))))]) + (heap-set! addr 'forwarded)) (define (gc! . other-roots) (swap-spaces!) @@ -154,13 +127,7 @@ ;; Forward all roots to the new semi-space (for-each forward/root (get-root-set)) - (for-each forward/root other-roots) - - ;; Forward all objects reachable from the roots - (let loop ([scan (off)]) - (when (< scan (ptr)) - (forward/loc scan) - (loop (+ scan (object-size scan)))))) + (for-each forward/root other-roots)) ;; gc:alloc-flat : flat-value -> address (define (gc:alloc-flat value)