DOC PREVIEW
CMU CS 15213 - Introduction to Computer Systems

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Carnegie Mellon Introduction to Computer Systems 15 213 18 243 spring 2009 20th Lecture Mar 31st Instructors Gregory Kesden and Markus P schel Carnegie Mellon Last Time Dynamic Memory Allocation p1 malloc 4 p2 malloc 5 p3 malloc 6 free p2 p4 malloc 2 Carnegie Mellon Last Time Fragmentation Internal Internal fragmentation block payload Internal fragmentation External p4 malloc 6 Oops what would happen now Carnegie Mellon Today Dynamic memory allocation Implicit free lists Explicit free lists Segregated free lists Garbage collection Carnegie Mellon Keeping Track of Free Blocks Method 1 Implicit free list using length links all blocks 5 6 2 Method 2 Explicit free list among the free blocks using pointers 5 4 4 6 2 Method 3 Segregated free list Different free lists for different size classes Method 4 Blocks sorted by size Can use a balanced tree e g Red Black tree with pointers within each free block and the length used as a key Carnegie Mellon Implicit List For each block we need length is allocated Could store this information in two words wasteful Standard trick If blocks are aligned some low order address bits are always 0 Instead of storing an always 0 bit use it as a allocated free flag When reading size word must mask out this bit 1 word size Format of allocated and free blocks payload a a 1 allocated block a 0 free block size block size payload application data allocated blocks only optional padding Carnegie Mellon Example Blackboard Assumptions word addressed memory 2 word alignment Start of heap Free word 2 0 4 1 8 0 4 1 0 1 Allocated word Allocated word unused 8 bytes 2 word alignment 8 byte alignment May require initial unused word Causes some internal fragmentation One word 0 1 to mark end of list Here block size in words for simplicity Carnegie Mellon Implicit List Finding a Free Block First fit Search list from beginning choose first free block that fits Cost p start while p end p 1 p len p p p 2 not passed end already allocated too Blackboard small will disappear goto next block word addressed Carnegie Mellon Implicit List Finding a Free Block First fit Search list from beginning choose first free block that fits Cost p start while p end p 1 p len p p p 2 not passed end already allocated too small goto next block word addressed Can take linear time in total number of blocks allocated and free In practice it can cause splinters at beginning of list Next fit Like first fit but search list starting where previous search finished Should often be faster than first fit avoids re scanning unhelpful blocks Some research suggests that fragmentation is worse Best fit Search the list choose the best free block fits with fewest bytes left over Keeps fragments small usually helps fragmentation Will typically run slower than first fit Carnegie Mellon Implicit List Allocating in Free Block Allocating in a free block splitting Since allocated space might be smaller than free space we might want to split the block 4 4 6 2 p addblock p 4 4 4 4 void addblock ptr p int len int newsize len 1 1 1 int oldsize p 2 p newsize 1 if newsize oldsize p newsize oldsize newsize 2 2 round up to even mask out low bit Blackboard set new length will disappear set length in remaining part of block Carnegie Mellon Implicit List Allocating in Free Block Allocating in a free block splitting Since allocated space might be smaller than free space we might want to split the block 4 4 6 2 p addblock p 4 4 4 4 void addblock ptr p int len int newsize len 1 1 1 int oldsize p 2 p newsize 1 if newsize oldsize p newsize oldsize newsize 2 2 round up to even mask out low bit set new length set length in remaining part of block Carnegie Mellon Implicit List Freeing a Block Simplest implementation Need only clear the allocated flag void free block ptr p p p 2 But can lead to false fragmentation 4 4 2 2 2 2 p free p 4 malloc 5 4 4 4 Oops There is enough free space but the allocator won t be able to find it Carnegie Mellon Implicit List Coalescing Join coalesce with next previous blocks if they are free Coalescing with next block 4 4 4 2 2 2 2 p free p 4 4 void free block ptr p p p 2 next p p if next 1 0 p p next 6 clear allocated flag find next block add to this block if not allocated But how do we coalesce with previous block logically gone Carnegie Mellon Implicit List Bidirectional Coalescing Boundary tags Knuth73 Replicate size allocated word at bottom end of free blocks Allows us to traverse the list backwards but requires extra space Important and general technique 4 4 4 Header Format of allocated and free blocks Boundary tag footer 4 6 size 6 4 a payload and padding size 4 a 1 allocated block a 0 free block size total block size a payload application data allocated blocks only Carnegie Mellon Constant Time Coalescing block being freed Case 1 Case 2 Case 3 Case 4 allocated allocated free free allocated free allocated free Carnegie Mellon Constant Time Coalescing Case 1 m1 1 m1 1 m1 1 m1 1 n 1 n 0 n 1 n 0 m2 1 m2 1 m2 1 m2 1 Carnegie Mellon Constant Time Coalescing Case 2 m1 1 m1 1 m1 1 m1 1 n 1 n m2 0 n 1 m2 0 m2 0 n m2 0 Carnegie Mellon Constant Time Coalescing Case 3 m1 0 n m1 0 m1 0 n 1 n 1 n m1 0 m2 1 m2 1 m2 1 m2 1 Carnegie Mellon Constant Time Coalescing Case 4 m1 0 m1 0 n 1 n 1 m2 0 m2 0 n m1 m2 0 n m1 m2 0 Carnegie Mellon Disadvantages of Boundary Tags Internal fragmentation Can it be optimized When does the previous block need the footer tag How to take advantage of that Carnegie Mellon Summary of Key Allocator Policies Placement policy First fit next fit best fit etc Trades off lower throughput for less fragmentation Interesting observation segregated free lists next lecture approximate a best fit placement policy without having to search entire free list Splitting policy When do we go ahead and split free blocks How much internal fragmentation are we willing to tolerate Coalescing policy Immediate coalescing coalesce each time free is called Deferred coalescing try to improve performance of free by deferring coalescing until needed Examples Coalesce as you scan the free list for malloc Coalesce when the amount of external fragmentation reaches some threshold Carnegie Mellon Implicit Lists Summary Implementation very simple Allocate cost linear time worst case Free cost constant time worst case even with coalescing Memory usage will depend on placement policy First fit next fit or best fit Not used in practice for malloc free because of linear time allocation used in many …


View Full Document

CMU CS 15213 - Introduction to Computer Systems

Documents in this Course
lecture

lecture

14 pages

lecture

lecture

46 pages

Caches

Caches

9 pages

lecture

lecture

39 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

56 pages

lecture

lecture

11 pages

lecture

lecture

9 pages

Lecture

Lecture

36 pages

Lecture

Lecture

37 pages

Exam

Exam

16 pages

Lecture

Lecture

10 pages

Lecture

Lecture

43 pages

Lecture

Lecture

8 pages

Lecture

Lecture

8 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

37 pages

Lecture

Lecture

6 pages

Lecture

Lecture

40 pages

coding

coding

2 pages

Exam

Exam

17 pages

Exam

Exam

14 pages

Lecture

Lecture

29 pages

Lecture

Lecture

34 pages

Exam

Exam

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

lecture

lecture

46 pages

Lecture

Lecture

33 pages

Lecture

Lecture

57 pages

Lecture

Lecture

32 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

6 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

18 pages

Exam

Exam

10 pages

Lecture

Lecture

45 pages

Lecture

Lecture

37 pages

Exam

Exam

24 pages

class09

class09

21 pages

class22

class22

37 pages

class20

class20

30 pages

class27

class27

33 pages

class25

class25

21 pages

class04

class04

31 pages

Lecture

Lecture

59 pages

class01a

class01a

14 pages

class12

class12

45 pages

class29

class29

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

6 pages

class03

class03

34 pages

lecture

lecture

42 pages

Lecture

Lecture

40 pages

Lecture

Lecture

47 pages

Exam

Exam

19 pages

R06-B

R06-B

25 pages

class17

class17

37 pages

class25

class25

31 pages

Lecture

Lecture

15 pages

final-f06

final-f06

17 pages

Lecture

Lecture

9 pages

lecture

lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

22 pages

class11

class11

45 pages

lecture

lecture

50 pages

Linking

Linking

37 pages

Lecture

Lecture

64 pages

Integers

Integers

40 pages

Exam

Exam

11 pages

Lecture

Lecture

37 pages

Lecture

Lecture

44 pages

Lecture

Lecture

37 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

45 pages

Final

Final

25 pages

lecture

lecture

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

16 pages

Final

Final

17 pages

Lecture

Lecture

8 pages

Exam

Exam

11 pages

Lecture

Lecture

47 pages

Lecture

Lecture

9 pages

lecture

lecture

39 pages

Exam

Exam

11 pages

lecture

lecture

41 pages

lecture

lecture

37 pages

Lecture

Lecture

59 pages

Lecture

Lecture

45 pages

Exam 1

Exam 1

18 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

30 pages

Lecture

Lecture

9 pages

Lecture

Lecture

9 pages

Lecture

Lecture

15 pages

Lecture

Lecture

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

4 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

65 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Lecture

Lecture

8 pages

Lecture

Lecture

34 pages

Lecture

Lecture

8 pages

Exam

Exam

13 pages

Lecture

Lecture

43 pages

Lecture

Lecture

9 pages

Lecture

Lecture

12 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

43 pages

Lecture

Lecture

7 pages

Lecture

Lecture

45 pages

Lecture

Lecture

24 pages

Lecture

Lecture

47 pages

Lecture

Lecture

12 pages

Lecture

Lecture

20 pages

Lecture

Lecture

9 pages

Exam

Exam

11 pages

Lecture

Lecture

52 pages

Lecture

Lecture

20 pages

Exam

Exam

11 pages

Lecture

Lecture

35 pages

Lecture

Lecture

47 pages

Lecture

Lecture

18 pages

Lecture

Lecture

30 pages

Lecture

Lecture

59 pages

Lecture

Lecture

37 pages

Lecture

Lecture

22 pages

Lecture

Lecture

35 pages

Exam

Exam

23 pages

Lecture

Lecture

9 pages

Lecture

Lecture

22 pages

class12

class12

32 pages

Lecture

Lecture

8 pages

Lecture

Lecture

39 pages

Lecture

Lecture

44 pages

Lecture

Lecture

38 pages

Lecture

Lecture

69 pages

Lecture

Lecture

41 pages

Lecture

Lecture

12 pages

Lecture

Lecture

52 pages

Lecture

Lecture

59 pages

Lecture

Lecture

39 pages

Lecture

Lecture

83 pages

Lecture

Lecture

59 pages

class01b

class01b

17 pages

Exam

Exam

21 pages

class07

class07

47 pages

Lecture

Lecture

11 pages

Odyssey

Odyssey

18 pages

multicore

multicore

66 pages

Lecture

Lecture

6 pages

lecture

lecture

41 pages

lecture

lecture

55 pages

lecture

lecture

52 pages

lecture

lecture

33 pages

lecture

lecture

46 pages

lecture

lecture

55 pages

lecture

lecture

17 pages

lecture

lecture

49 pages

Exam

Exam

17 pages

lecture

lecture

56 pages

Exam 2

Exam 2

16 pages

Exam 2

Exam 2

16 pages

Notes

Notes

37 pages

Lecture

Lecture

40 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Exam

Exam

13 pages

Lecture

Lecture

32 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

29 pages

Lecture

Lecture

59 pages

Lecture

Lecture

41 pages

Lecture

Lecture

50 pages

Exam

Exam

17 pages

Lecture

Lecture

29 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

33 pages

lecture

lecture

10 pages

Lecture

Lecture

27 pages

Lecture

Lecture

29 pages

Lecture

Lecture

39 pages

Lecture

Lecture

9 pages

Lecture

Lecture

29 pages

Lecture

Lecture

8 pages

Lecture

Lecture

43 pages

Lecture

Lecture

43 pages

Lecture

Lecture

75 pages

Lecture

Lecture

55 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lecture

Lecture

35 pages

lecture

lecture

36 pages

Exam

Exam

33 pages

lecture

lecture

56 pages

lecture

lecture

64 pages

lecture

lecture

8 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

36 pages

lecture

lecture

56 pages

lecture

lecture

75 pages

lecture

lecture

36 pages

Lecture

Lecture

50 pages

Lecture

Lecture

45 pages

Lecture

Lecture

13 pages

Exam

Exam

23 pages

Lecture

Lecture

10 pages

Lecture

Lecture

48 pages

Lecture

Lecture

83 pages

lecture

lecture

57 pages

Lecture

Lecture

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

33 pages

lecture

lecture

54 pages

Lecture

Lecture

30 pages

Exam

Exam

13 pages

Lecture

Lecture

36 pages

Lecture

Lecture

40 pages

Exam

Exam

17 pages

Lecture

Lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

44 pages

Lecture

Lecture

34 pages

Lecture

Lecture

24 pages

Lecture

Lecture

29 pages

class12

class12

43 pages

lecture

lecture

43 pages

class22

class22

22 pages

R06-B

R06-B

25 pages

class01b

class01b

19 pages

lecture

lecture

29 pages

lab1

lab1

8 pages

Caches

Caches

36 pages

lecture

lecture

55 pages

Lecture,

Lecture,

37 pages

Integers

Integers

40 pages

Linking

Linking

38 pages

lecture

lecture

45 pages

Lecture

Lecture

61 pages

Linking

Linking

33 pages

lecture

lecture

40 pages

lecture

lecture

40 pages

Lecture

Lecture

32 pages

lecture

lecture

48 pages

lecture

lecture

44 pages

Exam

Exam

11 pages

Lecture

Lecture

31 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

40 pages

Exam

Exam

12 pages

Lecture

Lecture

42 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

Lecture

Lecture

13 pages

Lecture

Lecture

35 pages

Lecture

Lecture

20 pages

Final

Final

19 pages

Lecture

Lecture

33 pages

Lecture

Lecture

50 pages

Lecture

Lecture

33 pages

Lecture

Lecture

27 pages

Lecture

Lecture

6 pages

Exam

Exam

15 pages

Lecture

Lecture

24 pages

Lecture

Lecture

23 pages

Lecture

Lecture

43 pages

Lecture

Lecture

32 pages

Lecture

Lecture

52 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

15 pages

lecture

lecture

21 pages

Lecture

Lecture

58 pages

Lecture

Lecture

49 pages

Lecture

Lecture

36 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

33 pages

Exam

Exam

15 pages

Lecture

Lecture

35 pages

Lecture

Lecture

10 pages

Lecture

Lecture

25 pages

Lecture

Lecture

31 pages

Lecture

Lecture

24 pages

Lecture

Lecture

34 pages

Lecture

Lecture

50 pages

lecture

lecture

35 pages

Lecture

Lecture

11 pages

Lecture

Lecture

39 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

exam1-f05

exam1-f05

11 pages

Lecture

Lecture

4 pages

Lecture

Lecture

17 pages

Exam

Exam

17 pages

malloc()

malloc()

12 pages

Lecture

Lecture

57 pages

Lecture

Lecture

30 pages

Lecture

Lecture

30 pages

Lecture

Lecture

47 pages

Lecture

Lecture

33 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lectures

Lectures

33 pages

Lecture

Lecture

36 pages

lecture

lecture

33 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Load more
Download Introduction to Computer Systems
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Computer Systems and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Computer Systems and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?