Due: Saturday, March 5th, 2011, noon
Implement a two space copying collector, as described in lecture 11. Use four state variables in your collector: the allocation pointer, the active semi-space, and two pointers that you use only during collection, one pointing to the left and one to the right side of the queue for copying the data. (It is possible to do this with only three state variables, and it might be possible with only two, but I recommend four.)
You may assume that your heap size is a multiple of 2 and contains at least 12 cells.
You collector must be able to run these three programs without running out of space:
#lang plai/mutator (allocator-setup "gc.ss" 200) (define (count-down n) (cond [(zero? n) (count-down 20)] [else (count-down (- n 1))])) (count-down 0)
#lang plai/mutator (allocator-setup "gc.ss" 200) (define (mk-list n) (cond [(zero? n) '()] [else (cons n (mk-list (- n 1)))])) (define (forever) (mk-list 10) (forever)) (forever)
#lang plai/mutator (allocator-setup "gc.ss" 200) (define (proc-lst n) (cond [(zero? n) (lambda () 0)] [else (let ([n1 (proc-lst (- n 1))]) (lambda () (+ (n1) n)))])) (define (forever) ((proc-lst 10)) (forever)) (forever)
Your garbage collector may not do any PLAI-level allocation (except stack space). This means that the functions cons, map, and list are off limits. Similarly, the constructors generated by define-type are off limits (e.g., lam, id, num, add, and sub from earlier assignments). Closures also require allocation, so you can only define functions at the top-level of the file (no lambda expressions in nested scopes).
The only exceptions: you may call procedure-roots and get-root-list, both of which allocate at the PLAI level.
You may, of course, use functions that allocate in your tests to build up example heaps.
If you are unsure what primitives allocate memory, ask.
If you insert calls to read in the middle of your collector, drscheme will pause until you type something and hit return in the interactions window (but will still update the heap GUI). This allows you to see what is happening at various stages in the process of collection (or allocation) and thus can help you understand what is going on.
Only use the previous hint to help you formulate test cases. The edit/debug/fix cycle leads to much pain that the edit/debug/write-test-cases/fix cycle avoids. (And, of course, only enter into this cycle with some baseline set of test cases that you've written up front.)
Below is a list of the functions required to implement a garbage collector, along with their contracts and bogus implementations.
;; init-allocator : -> void
(define (init-allocator) (error 'this-collector-always-fails))
;; gc:deref : loc -> heap-value
;; must signal an error if fl-loc doesn't point to a flat value
(define (gc:deref fl-loc) #t)
;; gc:alloc-flat : heap-value -> loc
(define (gc:alloc-flat fv) 0)
;; gc:cons : loc loc -> loc
;; hd and tl are guaranteed to have been earlier
;; results from either gc:alloc-flat or gc:cons
(define (gc:cons hd tl) 0)
;; gc:first : loc -> loc
;; must signal an error of pr-loc does not point to a pair
(define (gc:first pr-loc) 0)
;; gc:rest : loc -> loc
;; must signal an error of pr-loc does not point to a pair
(define (gc:rest pr-loc) 0)
;; gc:flat? : loc -> boolean
;; loc is guaranteed to have been an earlier
;; result from either gc:alloc-flat or gc:cons
(define (gc:flat? loc) #t)
;; gc:cons? : loc -> boolean
;; loc is guaranteed to have been an earlier
;; result from either gc:alloc-flat or gc:cons
(define (gc:cons? loc) #f)
;; gc:set-first! : loc loc -> void
;; must signal an error of pr-loc does not point to a pair
(define (gc:set-first! pr-loc new) (void))
;; gc:set-rest! : loc loc -> void
;; must signal an error of pr-loc does not point to a pair
(define (gc:set-rest! pr-ptr new) (void))
Handin your garbage collector. It should begin with the line
#lang plai/collector
The handin server will not run any tests on your collector. This time it is your job to make sure you've tested well enough. Use unit testing, like we did in class. Use the example mutators above. Use the random mutator generator.
Last update: Friday, February 25th, 2011robby@eecs.northwestern.edu |