Presentation is loading. Please wait.

Presentation is loading. Please wait.

List Allocation and Garbage Collection

Similar presentations


Presentation on theme: "List Allocation and Garbage Collection"— Presentation transcript:

1 List Allocation and Garbage Collection
Example

2 List Cells L = (1,(2,3),4)

3 M = cons(x,L) M1 = cons(0,L) = (0,1,2,3)
car(M1) = 0, cdr(M1) = (1,2,3) M2 = cons((0),L) = ((0),1,2,3)

4 Extent (define extent (lambda (x) (if (not (pair? x))
(+ 1 (extent (car x)) (extent (cdr x))))))

5 Overlap M = (2,3) L = cons(1,cons(M,cons(4,()))) = (1,(2,3),4)

6 Extent and Overlap There is overlap when number of cells does not equal extent L = ((1,2),3,(1,2))

7 Memory Allocation AVAIL = 0  (0 0 0 0 0 0 0 0) Initialize Heap
1 AVAIL = 0  ( ) Initialize Heap denotes list 2 1 3 2 4 3 5 4 6 5 7 6 -1 7

8 Memory Allocation AVAIL = 0 (define L (list 1 2 3)) 
1 AVAIL = 0 (define L (list 1 2 3))  (cons 1 (cons 2 (cons 3 ‘()))) 2 1 3 2 4 3 5 4 6 5 7 6 -1 7

9 Memory Allocation AVAIL = 1 (define L (list 1 2 3)) 
-1 AVAIL = 1 (define L (list 1 2 3))  (cons 1 (cons 2 (cons 3 ‘()))) 2 1 3 2 4 3 5 4 6 5 7 6 -1 7

10 Memory Allocation AVAIL = 2 (define L (list 1 2 3)) 
-1 AVAIL = 2 (define L (list 1 2 3))  (cons 1 (cons 2 (cons 3 ‘()))) 2 1 3 2 4 3 5 4 6 5 7 6 -1 7

11 Memory Allocation AVAIL = 3  (0 0 0 0 0) L = 2  (1 2 3)
-1 AVAIL = 3  ( ) L = 2  (1 2 3) (define L (list 1 2 3))  (cons 1 (cons 2 (cons 3 ‘()))) 2 1 1 1 2 4 3 5 4 6 5 7 6 -1 7

12 Memory Allocation AVAIL = 5  (0 0 0), L = 2  (1 2 3), M = 4  (4 5)
-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)) 2 1 1 1 2 5 -1 3 4 3 4 6 5 7 6 -1 7

13 Memory Allocation AVAIL = -1  NULL, L = 2  (1 2 3), M = 4  (4 5)
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)) 2 1 1 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

14 Memory Allocation AVAIL = -1  NULL, L = 2  ((4 5) 2 3),
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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

15 Memory Allocation AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int]
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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

16 Memory Allocation AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int]
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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

17 Garbage Collection AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int]
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) (define N (list 1)); AVAIL = NULL  garbage collection 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

18 Mark AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
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) (define N (list 1)) Mark L 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

19 Mark AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
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) (define N (list 1)) Mark L 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

20 Mark AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
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) (define N (list 1)) Mark L 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

21 Mark AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
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) (define N (list 1)) Mark L 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

22 Mark AVAIL = -1  NULL, L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
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) (define N (list 1)) Mark L 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7

23 Sweep AVAIL = 5  (0) L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
-1 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 0) (define N (list 1)) Reclaim unmarked cells 2 1 4 1 2 5 -1 3 4 3 4 -1 5 7 5 6 6 6 7

24 Sweep AVAIL = 6  (0 0) L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
-1 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 0) (define N (list 1)) Reclaim unmarked cells 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 6 6 7

25 Sweep AVAIL = 7  (0 0 0) L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
-1 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 0) (define N (list 1)) Reclaim unmarked cells 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 6 7

26 Sweep AVAIL = 7  (0 0 0) L = 2  ((4 5) 2 3), M = 0 [int] N = 0 [int]
-1 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 0) (define N (list 1)) Unmark cells 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 6 7

27 Memory Allocation AVAIL = 6  (0 0) L = 2  ((4 5) 2 3), M = 0 [int]
-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 0) (define N (list 1)) 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 1 -1 7


Download ppt "List Allocation and Garbage Collection"

Similar presentations


Ads by Google