Reference counting:

Similar documents
Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

COMP2111 Glossary. Kai Engelhardt. Contents. 1 Symbols. 1 Symbols 1. 2 Hoare Logic 3. 3 Refinement Calculus 5. rational numbers Q, real numbers R.

A Short Introduction to Hoare Logic

Program Verification Using Separation Logic

6.001 Recitation 22: Streams

Solutions to EoPL3 Exercises

CIS 500 Software Foundations. Final Exam. May 9, Answer key. Hoare Logic

6.001 SICP September? Procedural abstraction example: sqrt (define try (lambda (guess x) (if (good-enuf? guess x) guess (try (improve guess x) x))))

Verified Characteristic Formulae for CakeML. Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 18, 2017

Principles of Program Analysis: Data Flow Analysis

A Modular Rewriting Semantics for CML

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Erasable Contracts. Abstract. 1. Introduction. Harvard University {jchinlee,

Relaxing Safely: Verified On-the-Fly Garbage Collection for x86-tso

Proof-producing decompilation and compilation

Abstractions and Decision Procedures for Effective Software Model Checking

Fig. S5: Same as Fig. S3 for the combination Sample

Collecting garbage concurrently (but correctly)

States and Actions: An Automata-theoretic Model of Objects

From Separation Logic to Systems Software

Math 391: Midterm 1.0 Spring 2016

CIS 500 Software Foundations Midterm II Answer key November 17, 2004

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

Recitation 7. Treaps and Combining BSTs. 7.1 Announcements. FingerLab is due Friday afternoon. It s worth 125 points.

Google Go illustrated on the basis of Fibonacci numbers

Notes. Relations. Introduction. Notes. Relations. Notes. Definition. Example. Slides by Christopher M. Bourke Instructor: Berthe Y.

CSE 505, Fall 2005, Midterm Examination 8 November Please do not turn the page until everyone is ready.

Modeling and Analysis of Hybrid Systems

ITI Introduction to Computing II

Roy L. Crole. Operational Semantics Abstract Machines and Correctness. University of Leicester, UK

Imperative Data Parallelism (Correctness)

References. 7 November. Fall Software Foundations CIS 500. Another example. Announcements. Homework 7 out today, due November 14.

Constructors - Cont. must be distinguished by the number or type of their arguments.

or simply: IC3 A Simplified Description

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Elementary Sorts 1 / 18

Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University

Finding similar items

Rodin and refinement

Dependent Types for JavaScript Appendix

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Math-2A Lesson 2-1. Number Systems

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

Principles of Program Analysis: Control Flow Analysis

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

CEGAR:Counterexample-Guided Abstraction Refinement

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

Imperative Insertion Sort

Prioritized Garbage Collection Using the Garbage Collector to Support Caching

Locksmith: Context-Sensitive Correlation Analysis for Race Detection

ITI Introduction to Computing II

Software Engineering

Relations Graphical View

Abstracting Definitional Interpreters. David Van Horn

COMP 515: Advanced Compilation for Vector and Parallel Processors. Vivek Sarkar Department of Computer Science Rice University

Computability and Complexity Results for a Spatial Assertion Language for Data Structures

Denotational Semantics of Programs. : SimpleExp N.

Information Flow Inference for ML

DM507 - Algoritmer og Datastrukturer Project 1

Storage Management in the Tektronix 32-Bit Smalltalk

Advanced parallel Fortran

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Program verification. Hoare triples. Assertional semantics (cont) Example: Semantics of assignment. Assertional semantics of a program

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

Cost of Substitution. (interp {with {x 1} {with {y 2} {+ 100 {+ 99 { {+ y x}}}}}} )

Theories of Programming Languages Assignment 5

Chapter 9: Relations Relations

Verification of Recursive Programs. Andreas Podelski February 8, 2012

Lecture 6: Introducing Complexity

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Tute 10. Liam O'Connor. May 23, 2017

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Space Complexity. The space complexity of a program is how much memory it uses.

Principles of Program Analysis: Data Flow Analysis

Discrete Mathematcs and Logic II.

Binary Search Trees. Motivation

Disjunctive relational abstract interpretation for interprocedural program analysis

Review. Principles of Programming Languages. Equality. The Diamond Property. The Church-Rosser Theorem. Corollaries. CSE 230: Winter 2007

Mark Scheme (Results) June 2008

Runtime Complexity. CS 331: Data Structures and Algorithms

λ S : A Lambda Calculus with Side-effects

Compiling Techniques

Minilecture 2: Sorting Algorithms

Abstract Interpretation II

More on Methods and Encapsulation

Übung Informatik I - Programmierung - Blatt 7

Recent developments in concurrent program logics

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

Linux. A Study on the Scope of Error Propagation in Linux. Takeshi Yoshimura, 1 Hiroshi Yamada 1, 2 and Kenji Kono 1, 2. Linux. Linux.

Semantics and Verification of Software

Probability and Statistics

State-Dependent Representation Independence (Technical Appendix)

Typed Arithmetic Expressions

CSE 311 Lecture 28: Undecidability of the Halting Problem. Emina Torlak and Kevin Zatloukal

What is a Linear Space/Vector Space?

Relational Parametricity and Separation Logic. Hongseok Yang, Queen Mary, Univ. of London Lars Birkedal, IT Univ. of Copenhagen

Softbound. March 23, Type 2. 2 Env primitives defined functions axioms well-formed environment...

Top Down Design. Gunnar Gotshalks 03-1

Math-2 Section 1-1. Number Systems

Transcription:

Invariant:

Reference counting:

Reference counting:

2

0 3

2

0 2

2 0

2

2

2 2

2

white gray r r r black

do not

;; init-allocator : -> void? (define (init-allocator) (for ([i (in-range 0 (heap-size))]) (heap-set! i 'free))) ;; gc:flat? : location? -> boolean? (define (gc:flat? loc) (equal? (heap-ref loc) 'flat)) ;; gc:deref : location? -> void? (define (gc:deref loc) (cond [(equal? (heap-ref loc) 'flat) (heap-ref (+ loc ))] [else (error 'gc:deref "attempted to deref a non flat value, loc ~s" loc)]))

;; gc:cons? : location? -> boolean? (define (gc:cons? loc) (equal? (heap-ref loc) 'pair)) ;; gc:first : location? -> location? (define (gc:first pr-ptr) (if (equal? (heap-ref pr-ptr) 'pair) (heap-ref (+ pr-ptr )) (error 'first "non pair"))) ;; gc:rest : location? -> location? (define (gc:rest pr-ptr) (if (equal? (heap-ref pr-ptr) 'pair) (heap-ref (+ pr-ptr 2)) (error 'rest "non pair")))

;; gc:set-first! : location? location? -> void? (define (gc:set-first! pr-ptr new) (if (equal? (heap-ref pr-ptr) 'pair) (heap-set! (+ pr-ptr ) new) (error 'set-first! "non pair"))) ;; gc:set-rest! : location? location? -> void? (define (gc:set-rest! pr-ptr new) (if (equal? (heap-ref pr-ptr) 'pair) (heap-set! (+ pr-ptr 2) new) (error 'set-first! "non pair")))

;; gc:closure-code-ptr : location? -> heap-value? (define (gc:closure-code-ptr loc) (if (gc:closure? loc) (heap-ref (+ loc )) (error 'gc:closure-code "non closure @ ~a, got ~s" loc (heap-ref loc))) ) ;; gc:closure-env-ref : location? integer? -> location? (define (gc:closure-env-ref loc i) (if (gc:closure? loc) (if (< i (heap-ref (+ loc 2))) (heap-ref (+ loc 3 i)) (error 'closure-env-ref "closure-env-ref out of bounds")) (error 'closure-env-ref "non closure"))) ;; gc:closure? : location? -> boolean? (define (gc:closure? loc) (equal? (heap-ref loc) 'proc))

;; gc:alloc-flat : heap-value? -> location? (define (gc:alloc-flat fv) (let ([ptr (alloc 2 #f #f)]) (heap-set! ptr 'flat) (heap-set! (+ ptr ) fv) ptr)) ;; gc:cons : root? root? -> location? (define (gc:cons hd tl) (define ptr (alloc 3 hd tl)) (heap-set! ptr 'pair) (heap-set! (+ ptr ) (read-root hd)) (heap-set! (+ ptr 2) (read-root tl)) ptr)

;; gc:closure : heap-value? (vectorof locaction?) -> location? (define (gc:closure code-ptr free-vars) (define free-vars-count (length free-vars)) (define next (alloc (+ free-vars-count 3) free-vars '())) (heap-set! next 'proc) (heap-set! (+ next ) code-ptr) (heap-set! (+ next 2) free-vars-count) (for ([x (in-range 0 free-vars-count)] [r (in-list free-vars)]) (heap-set! (+ next 3 x) (read-root r))) next)

a roots is either: - root? - location? - (listof roots?) ;; alloc : number? roots? roots? -> location? (define (alloc n some-roots some-more-roots) (let ([next (find-free-space 0 n)]) (cond [next next] [else (collect-garbage some-roots some-more-roots) (let ([next (find-free-space 0 n)]) (unless next (error 'alloc "out of space")) next)])))

;; find-free-space : location? number? -> (or/c location? #f) ;; start must be a valid pointer ;; (not to the middle of an object) (define (find-free-space start size) (cond [(= start (heap-size)) #f] [else (case (heap-ref start) [(free) (if (n-free-blocks? start size) start (find-free-space (+ start ) size))] [(flat) (find-free-space (+ start 2) size)] [(pair) (find-free-space (+ start 3) size)] [(proc) (find-free-space (+ start 3 (heap-ref (+ start 2))) size)] [else (error 'find-free-space "ack ~s" start)])]))

;; n-free-blocks? : location? integer? -> boolean? (define (n-free-blocks? start size) (cond [(= size 0) #t] [(= start (heap-size)) #f] [else (and (eq? 'free (heap-ref start)) (n-free-blocks? (+ start ) (- size )))]))

;; collect-garbage : roots? roots? -> void? (define (collect-garbage some-roots some-more-roots) (validate-heap) (mark-white! 0) (traverse/roots (get-root-set)) (traverse/roots some-roots) (traverse/roots some-more-roots) (free-white! 0) (validate-heap))

;; validate-heap : -> void? (define (validate-heap) (define (valid-pointer? i) (unless (memq (heap-ref i) '(flat pair proc)) (error 'validate-heap "found bad pointer @ ~a" i))) (let loop ([i 0]) (when (< i (heap-size)) (case (heap-ref i) [(flat) (loop (+ i 2))] [(pair) (valid-pointer? (heap-ref (+ i ))) (valid-pointer? (heap-ref (+ i 2))) (loop (+ i 3))] [(proc) (for ([x (in-range 0 (heap-ref (+ i 2)))]) (valid-pointer? (heap-ref (+ i 3 x)))) (loop (+ i 3 (heap-ref (+ i 2))))] [(free) (loop (+ i ))] [else (error 'validate-heap "corruption! @ ~a" i)]))))

;; mark-white! : location? -> void? (define (mark-white! i) (when (< i (heap-size)) (case (heap-ref i) [(pair) (heap-set! i 'white-pair) (mark-white! (+ i 3))] [(flat) (heap-set! i 'white-flat) (mark-white! (+ i 2))] [(proc) (heap-set! i 'white-proc) (mark-white! (+ i 3 (heap-ref (+ i 2))))] [(free) (mark-white! (+ i ))] [else (error 'mark-white! "unknown tag @ ~a" i)])))

;; free-white! : location? -> void? (define (free-white! i) (when (< i (heap-size)) (case (heap-ref i) [(pair) (free-white! (+ i 3))] [(flat) (free-white! (+ i 2))] [(proc) (free-white! (+ i 3 (heap-ref (+ i 2))))] [(white-pair) (heap-set! i 'free) (heap-set! (+ i ) 'free) (heap-set! (+ i 2) 'free) (free-white! (+ i 3))] [(white-flat) (heap-set! i 'free) (heap-set! (+ i ) 'free) (free-white! (+ i 2))] [(white-proc) (define closure-size (heap-ref (+ i 2))) (for ([dx (in-range 0 (+ closure-size 3))]) (heap-set! (+ i dx) 'free)) (free-white! (+ i 3 closure-size))] [(free) (free-white! (+ i ))] [else (error 'free-white! "unknown tag ~s" (heap-ref i))])))

;; traverse/roots : roots? -> void? (define (traverse/roots thing) (cond [(list? thing) (for-each traverse/roots thing)] [(root? thing) (traverse/roots (read-root thing))] [(number? thing) (traverse/loc thing)]))

;; traverse/loc : location? -> void? (define (traverse/loc loc) (case (heap-ref loc) [(white-pair) (heap-set! loc 'pair) (traverse/loc (heap-ref (+ loc ))) (traverse/loc (heap-ref (+ loc 2)))] [(white-flat) (heap-set! loc 'flat)] [(white-proc) (heap-set! loc 'proc) (for ([i (in-range (heap-ref (+ loc 2)))]) (traverse/loc (heap-ref (+ loc 3 i))))] [(pair) (void)] [(flat) (void)] [(proc) (void)] [else (error 'traverse/loc "crash ~s" loc)]))

two-space Allocator: to-space from-space to-space Collector: to-space from-space from-space to-space to-space

Invariant:

to-space

7 0 75 2 0 3 2 0 3 2 2 3 4

7 0 75 2 0 3 2 0 3 2 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2

7 0 75 2 0 3 2 0 3 2 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^

7 0 75 2 0 3 2 0 3 2 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ 0 0 0 0 0 0 0 0 0 0 0 0 0 ^^ 3 4 5 6 7 8 9 20 2 22 23 24 25

3 0 75 2 0 3 2 0 99 3 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ 3 2 2 0 0 0 0 0 0 0 0 0 0 ^ ^ 3 4 5 6 7 8 9 20 2 22 23 24 25

3 6 99 6 2 0 3 2 0 99 3 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ 3 2 2 75 0 0 0 0 0 0 0 0 ^ ^ 3 4 5 6 7 8 9 20 2 22 23 24 25

3 6 99 6 99 8 3 2 0 99 3 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ 3 2 8 75 2 0 0 0 0 0 0 0 ^ ^ 3 4 5 6 7 8 9 20 2 22 23 24 25

3 6 99 6 99 8 3 2 0 99 3 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ 3 2 8 75 2 0 0 0 0 0 0 0 ^ ^ 3 4 5 6 7 8 9 20 2 22 23 24 25

3 6 99 6 99 8 3 2 0 99 3 2 3 4 00 0 02 03 04 05 06 07 08 09 0 2 ^ ^ ^ ^ ^ 3 2 8 75 2 6 0 0 0 0 0 0 ^^ 3 4 5 6 7 8 9 20 2 22 23 24 25

ftp://ftp.cs.utexas.edu/pub/garbage/gcsurvey.ps