List Allocation and Garbage Collection Example List Cells
List Allocation and Garbage Collection Example
List Cells • L = (1, (2, 3), 4)
M = cons(x, L) • M 1 = cons(0, L) = (0, 1, 2, 3) – car(M 1) = 0, cdr(M 1) = (1, 2, 3) • M 2 = cons((0), L) = ((0), 1, 2, 3)
Extent (define extent (lambda (x) (if (not (pair? x)) 0 (+ 1 (extent (car x)) (extent (cdr x))))))
Overlap • M = (2, 3) • L = cons(1, cons(M, cons(4, ()))) = (1, (2, 3), 4)
Extent and Overlap • There is overlap when number of cells does not equal extent • L = ((1, 2), 3, (1, 2))
Memory Allocation 0 0 1 AVAIL = 0 (0 0 0 0 0) 1 0 2 Initialize Heap 2 0 3 3 0 4 4 0 5 5 0 6 6 0 7 7 0 -1 denotes list
Memory Allocation 0 0 1 AVAIL = 0 1 0 2 2 0 3 (define L (list 1 2 3)) (cons 1 (cons 2 (cons 3 ‘()))) 3 0 4 4 0 5 5 0 6 6 0 7 7 0 -1
Memory Allocation 0 3 -1 AVAIL = 1 1 0 2 2 0 3 (define L (list 1 2 3)) (cons 1 (cons 2 (cons 3 ‘()))) 3 0 4 4 0 5 5 0 6 6 0 7 7 0 -1
Memory Allocation 0 3 -1 AVAIL = 2 1 2 0 3 (define L (list 1 2 3)) (cons 1 (cons 2 (cons 3 ‘()))) 3 0 4 4 0 5 5 0 6 6 0 7 7 0 -1
Memory Allocation 0 3 -1 1 2 0 2 1 1 3 0 4 4 0 5 5 0 6 6 0 7 7 0 -1 AVAIL = 3 (0 0 0) L = 2 (1 2 3) (define L (list 1 2 3)) (cons 1 (cons 2 (cons 3 ‘())))
Memory Allocation 0 3 -1 1 2 0 2 1 1 3 5 -1 4 4 3 5 0 6 6 0 7 7 0 -1 AVAIL = 5 (0 0 0), L = 2 (1 2 3), M = 4 (4 5) (define L (list 1 2 3)) (define M (list 4 5))
Memory Allocation 0 3 -1 1 2 0 2 1 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 (1 2 3), M = 4 (4 5) N = 7 (6 7 8) (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8))
Memory Allocation 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 4 (4 5) N = 7 (6 7 8) (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M); overlap
Memory Allocation 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 7 (6 7 8) (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) ; cells still accessible
Memory Allocation 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N 0) ; cells garbage
Garbage Collection 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)); AVAIL = NULL garbage collection
Mark 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Mark L
Mark 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Mark L
Mark 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Mark L
Mark 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Mark L
Mark 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 8 -1 6 7 5 7 6 6 AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Mark L
Sweep 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 0 -1 6 7 5 7 6 6 AVAIL = 5 (0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Reclaim unmarked cells
Sweep 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 0 -1 6 0 5 7 6 6 AVAIL = 6 (0 0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Reclaim unmarked cells
Sweep 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 0 -1 6 0 5 7 0 6 AVAIL = 7 (0 0 0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Reclaim unmarked cells
Sweep 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 0 -1 6 0 5 7 0 6 AVAIL = 7 (0 0 0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1)) Unmark cells
Memory Allocation 0 3 -1 1 2 0 2 4 1 3 5 -1 4 4 3 5 0 -1 6 0 5 7 1 -1 AVAIL = 6 (0 0) L = 2 ((4 5) 2 3), M = 0 [int] N = 7 (1) (define L (list 1 2 3)) (define M (list 4 5)) (define N (list 6 7 8)) (set-car! L M) (define M 0) (define N (list 1))
- Slides: 27