+ All Categories
Home > Documents > Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen...

Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen...

Date post: 19-Jan-2018
Category:
Upload: jack-hawkins
View: 215 times
Download: 0 times
Share this document with a friend
Description:
Carnegie Mellon 3 Malloc Lab Is out. Due at Thursday, July 23 rd. Please start early.
18
Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu
Transcript
Page 1: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

1

Malloc Lab

15-213: Introduction to Computer SystemsFriday, July 10, 2015

Shen Chen Xu

Page 2: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

2

Today Dynamic memory allocation and malloc lab. Pointers. Simple implementations of malloc/free.

Page 3: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

3

Malloc Lab Is out. Due at Thursday, July 23rd. Please start early.

Page 4: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

4

Malloc Lab void *malloc(size_t size); void *realloc(void *ptr, size_t size); void *calloc(size_t nmemb, size_t size);

void free(void *ptr); int mm_init(void); void mm_checkheap(int);

Page 5: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

5

Watch out for… Driver will complain about:

Memory alignment. Garbled bytes. Out of memory. Etc.

But most of the time…

Page 6: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

6

Debugging gdb and gcc with option -g. valgrind: illegal accesses, unintialized

values, etc. Hard to reason about a such complicated

program only using these generic tools. Use your heap checker to print out more

information before it crash and burns!

Page 7: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

7

Pointers Declaring a pointer:

int *ptr; Getting the address of a variable:

&x; Dereferencing a pointer:

*ptr;

Page 8: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

8

Pointers and Arrays

Page 9: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

9

Pointers and Arrays In fact, &arr[0] and arr are interchangeable. What if ptr is did not come from an array?

What is (ptr + 1) in this case? (ptr + i) points to the i-th element that

comes after ptr, as if ptr points to the start of an array.

Page 10: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

10

Pointer arithmetic Recall: pointers are simply numbers that

index into memory.

For a pointer p of type T, (ptr + i)is equivalent to

((unsigned) ptr) + i * sizeof(T)

(*assuming unsigned has the same size as a pointer)

Page 11: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

11

Casting Pointers Pointers have types! Casting to a different type only changes

compiler’s interpretation, not the value. However this affects pointer arithmetic!

Page 12: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

12

Casting Pointers

Page 13: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

13

More Casting

Page 14: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

14

Complicated Declarations Declaration agrees with usage.

Page 15: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

15

Allocator Designs We evaluate you based on throughput and

memory utilization.

Page 16: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

16

Allocator Designs Free list

Implicit, explicit, segregated, etc. Placement

First-it, next-fit, best-fit, etc. Splitting

Splitting vs tolerating internal fragmentation. Coalescing

Immediate vs deferred.

Page 17: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

17

Example Allocator #1 Suppose we are on a 32-bit machine. We only want to allocate and free

memories that fits one ‘double’. Simple strategy:

Singly linked explicit free list. First fit placement policy.

Code walkthrough…

Page 18: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.

Carnegie Mellon

18

Example Allocator #2 Brian W. Kernighan and Dennis M. Ritchie,

The C Programming Language, Second Edition, Prentice Hall, 1988.

Explicit List of free blocks. Header as a basic unit of allocation. Code walkthrough…


Recommended