carnegie mellon 1 malloc lab 15-213: introduction to computer systems friday, july 10, 2015 shen...
DESCRIPTION
Carnegie Mellon 3 Malloc Lab Is out. Due at Thursday, July 23 rd. Please start early.TRANSCRIPT
![Page 1: Carnegie Mellon 1 Malloc Lab 15-213: Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c4/html5/thumbnails/18.jpg)
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…