CSE 351: Week 9 Tom Bergan, TA 1 Today Lab 5 Reference counting - - PowerPoint PPT Presentation

cse 351 week 9
SMART_READER_LITE
LIVE PREVIEW

CSE 351: Week 9 Tom Bergan, TA 1 Today Lab 5 Reference counting - - PowerPoint PPT Presentation

CSE 351: Week 9 Tom Bergan, TA 1 Today Lab 5 Reference counting 2 Lab 5: Explicit free list allocator Your goals Implement: void* mm_malloc(size_t bytes); Implement: void mm_free(void *ptr); We give you a starting point 3 Lab 5:


slide-1
SLIDE 1

CSE 351: Week 9

Tom Bergan, TA

1

slide-2
SLIDE 2

Today

  • Lab 5
  • Reference counting

2

slide-3
SLIDE 3

Lab 5: Explicit free list allocator

3

Your goals Implement: void* mm_malloc(size_t bytes); Implement: void mm_free(void *ptr); We give you a starting point

slide-4
SLIDE 4

Lab 5: Explicit free list allocator

4

The free-block list is a doubly-linked list

  • !"

!" !" !" #" #" !" !" !" !" $%&'(&)"*+,-./"01+23" 4(52"*6&,7/"01+23" 8" 4" 9"

Physical view (blocks can be in any order)

slide-5
SLIDE 5

Lab 5: Block format

5

Free Block

size next prev tags size

Allocated Block

size payload padding tags header footer header no footer! (more later)

slide-6
SLIDE 6

6

Free Block

size next prev tags size

Allocated Block

size payload padding tags

size size

Lab 5: Block format

header header

The size includes the whole block

slide-7
SLIDE 7

Lab 5: Block format

7

Allocated Block

size payload padding tags

mm_malloc() returns a pointer to here

(the first byte of the payload) header no footer! (more later)

slide-8
SLIDE 8

Lab 5: Block format

8

Free Block Allocated Block

. . .

header 8 bytes next 8 bytes prev 8 bytes footer 8 bytes (empty space)

...

header 8 bytes

mm_malloc() returns a pointer to here

(the first byte of the payload)

slide-9
SLIDE 9

9

Free Block Size

header footer size next prev tags size

63 62 61 ... 3 2 1

Tag

Size: aligned to 23 = 8 Tag bit 0: this block allocated? Tag bit 1: preceding block allocated? Tag bit 2: unused Adjacent blocks in memory precedes the red block

Lab 5: Block tags

slide-10
SLIDE 10

10

Block 1 Block 2 Block 3

preceding preceding prev size next prev tags size size payload padding tags size next prev tags size

Lab 5: Adjacent blocks

slide-11
SLIDE 11

11

size next prev tags size size payload padding tags size next prev tags size

Block 1 Block 2 Block 3

Tag bits

this allocated: 0 preeceding allocated: ? ... ? ... 1 ... 1

Tag bits

this allocated: 1 preceding allocated: 0

Tag bits

this allocated: 0 preeceding allocated: 1

Lab 5: Adjacent blocks

slide-12
SLIDE 12

Lab 5: Block data structure

12

struct BlockInfo { size_t sizeAndTags; struct BlockInfo *next; struct BlockInfo *prev; };

Free Block

size next prev tags size

mm.c

slide-13
SLIDE 13

Lab 5: Block data structure

13

struct BlockInfo { size_t sizeAndTags; struct BlockInfo *next; struct BlockInfo *prev; }; // global variable FREE_LIST_HEAD

Free list

size next prev tags size

mm.c

size next prev tags size

slide-14
SLIDE 14

Lab 5: Source code overview

14

We give you

void* searchFreeList(int reqSize) // Manipulate the free list (assumes freeBlock is on the free list) void insertFreeBlock(BlockInfo *freeBlock) void removeFreeBlock(BlockInfo *freeBlock) void coalesceFreeBlock(BlockInfo *freeBlock) // Get more memory from the OS and add it to the free list void requestMoreSpace(size_t reqSize)

You implement

void* mm_malloc(size_t size) void mm_free(void *ptr)

slide-15
SLIDE 15

Lab 5: Hints

15

− You will have to do some pointer arithmetic ... use: POINTER_ADD(), POINTER_SUB() − You don’t need any more global variables − You don’t need to call any functions in memlib.h − We did a lot of the tedious linked-list manipulation for you ... use it! − Do the simple thing first ... our solution is ~75 lines total, including comments − Debug with gdb ... use command: print *(BlockInfo*)p ... this allows you to view pointer p through the “lens”

  • f a BlockInfo, i.e., it pretends that p points to a BlockInfo
slide-16
SLIDE 16

Today

  • Lab 5
  • Reference counting

16

slide-17
SLIDE 17

Reference Counting

17

void foo() { int *x = malloc(4); }

Basic idea: count the # of pointers to each object Example Heap

1 size payload padding tags (in the header)

Ref count

slide-18
SLIDE 18

Reference Counting

18

void foo() { int *x = malloc(4); int *p = x; ... }

Example Heap

2 size payload padding tags

Ref count

Basic idea: count the # of pointers to each object

slide-19
SLIDE 19

Reference Counting

19

void foo() { int *x = malloc(4); int *p = x; ... // x and p go out-of-scope }

Example Heap

size payload padding tags

Ref count is zero

(automatic free!)

When the count=0, free the object

slide-20
SLIDE 20

Reference Counting

20

Example: a tree

(ref counts in purple)

root

1 1 1 1 1 1 1

slide-21
SLIDE 21

Reference Counting

21

Example: a tree

(ref counts in purple)

root

2 1 1 1 1 1 1

tmp

(e.g., for a traversal)

slide-22
SLIDE 22

Reference Counting

22

Example: a tree

(ref counts in purple)

root

1 2 1 1 1 1 1

tmp

(e.g., for a traversal)

slide-23
SLIDE 23

Reference Counting

23

Example: a tree

(ref counts in purple)

root

1 1 2 1 1 1 1

tmp

(e.g., for a traversal)

slide-24
SLIDE 24

Reference Counting

24

Example: a tree

(ref counts in purple)

root

1 1 1 1 1 1 1

slide-25
SLIDE 25

Reference Counting

25

Example: a tree

(ref counts in purple)

1 1 1 1 1 1

X

root = NULL

slide-26
SLIDE 26

Reference Counting

26

Example: a tree

(ref counts in purple)

1 1 1 1

root = NULL

slide-27
SLIDE 27

Reference Counting

27

Example: a tree

(ref counts in purple)

1

root = NULL

slide-28
SLIDE 28

Reference Counting

28

Example: a tree

(ref counts in purple)

root = NULL

slide-29
SLIDE 29

Reference Counting

29

Example: a directed graph

(ref counts in purple)

root

1 1 1 1 1 1

slide-30
SLIDE 30

Reference Counting

30

Example: a directed graph

(ref counts in purple)

root

1 1 1 2 1 1

slide-31
SLIDE 31

Reference Counting

31

Example: a directed graph

(ref counts in purple)

root

1 1 2 2 1 1

slide-32
SLIDE 32

Reference Counting

32

Example: a directed graph

(ref counts in purple)

1 2 2 1 1

root = NULL

slide-33
SLIDE 33

Reference Counting

33

Example: a directed graph

(ref counts in purple)

1 2 1 1

root = NULL

slide-34
SLIDE 34

Reference Counting

34

Example: a directed graph

(ref counts in purple)

1 1 1 1

root = NULL Reference counting cannot garbage collect cycles!