1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
1 Memory Allocation Professor Jennifer Rexford COS 217.
1 C Review and Dissection V: Dynamic Memory Allocation and Linked Lists These lecture notes created by Dr. Alex Dean, NCSU.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
CPSC 388 – Compiler Design and Construction
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Management Memory Areas and their use Memory Manager Tasks:
1 Optimizing Malloc and Free Professor Jennifer Rexford
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
CS61C L06 C Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
CS61C L06 C Memory Management (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
Thrashing and Memory Management
CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal.
1 Dynamic Memory Management. 2 What’s worse?  50% of instructions/data are cache misses (fetched from RAM)  1% of instructions/data are page faults.
Memory Allocation CS Introduction to Operating Systems.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
1. Memory Manager 2 Memory Management In an environment that supports dynamic memory allocation, the memory manager must keep a record of the usage of.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Stack and Heap Memory Stack resident variables include:
ICS 145B -- L. Bic1 Project: Main Memory Management Textbook: pages ICS 145B L. Bic.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers,
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 Dynamic Memory Allocation: Basic Concepts. 2 Today Basic concepts Implicit free lists.
Writing You Own malloc() March 29, 2003 Topics Explicit Allocation Data structures Mechanisms Policies class19.ppt “The course that gives CMU its.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Dynamic Memory Allocation II
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Basic Concepts :
Dynamic Memory Allocation April 9, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies Reading: 10.9 Problems: and class21.ppt.
6-1 Embedded Systems C Programming Language Review and Dissection IV Lecture 6.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
Chapter 17 Free-Space Management
Stack and Heap Memory Stack resident variables include:
Memory Management Memory Areas and their use Memory Manager Tasks:
Lectures linked lists Chapter 6 of textbook
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Dynamic Memory Allocation
C Programming Language Review and Dissection IV
Memory Management Memory Areas and their use Memory Manager Tasks:
PA1 is out Best by Feb , 10:00 pm Enjoy early
The Hardware/Software Interface CSE351 Winter 2013
Optimizing Malloc and Free
Memory Management I: Dynamic Storage Allocation March 2, 2000
CS Introduction to Operating Systems
Dynamic Memory Allocation I
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Optimizing Dynamic Memory Management
Pointers and Arrays Beyond Chapter 16
Homework Continue with K&R Chapter 5 Skipping sections for now
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory – A Review
Presentation transcript:

1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217

2 Goals of the Next Two Lectures Understanding how the heap is managed  Malloc: allocate memory  Free: deallocate memory Today: K&R implementation (Section 8.7)  Maintaining a circular list of free blocks  Finding the first free block that is large enough  (Relevant pages of K&R available on Blackboard) Next time: optimizing malloc and free  Best fit (or good fit), rather than first fit  Separate free lists based on block size Programming outside confines of the type system  Reliance on error-prone pointer arithmetic and casting

3 Memory Layout: Heap char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Text Data BSS Stack Heap Needed when required memory size is not known until the program runs

4 Using Malloc and Free Types  void* : generic pointer to any type (can be converted to other pointer types)  size_t : unsigned integer type returned by sizeof() void *malloc(size_t size)  Returns a pointer to space of size size  … or NULL if the request cannot be satisfied  E.g., int* x = (int *) malloc(sizeof(int)); void free(void *p)  Deallocate the space pointed to by the pointer p  Pointer p must be pointer to space previously allocated  Do nothing if p is NULL

5 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1

6 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2

7 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3

8 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3

9 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 p4

10 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 p4

11 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

12 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

13 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

14 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

15 Challenges for Malloc and Free Handling arbitrary request sequences  Memory may be allocated and freed in different order Making immediate responses to requests  Cannot reorder/buffer requests to improve performance Using only the heap  Data structures used by malloc/free stored on the heap Aligning blocks (e.g., on 8-byte boundary)  Blocks must be able to hold any type of data object Not modifying allocated blocks  Can only manipulate or change free blocks  Cannot modify other blocks after they are allocated

16 K&R Implementation (Section 8.7) Free list data structure  Free block with header (pointer and size) and user data  Aligning the header with the largest data type  Circular linked list of free blocks Malloc function  Allocating memory in multiples of header size  Finding the first element in free list that is large enough  Allocating more memory from the OS, if needed Free function  Putting a block back in the free list  Coalescing with adjacent blocks, if any

17 Free Block: Pointer, Size, Data Free block in memory  Pointer to the next free block  Size of the free block  Free space (that can be allocated to user) p (address returned to the user) user data size

18 Free Block: Memory Alignment Define a structure s for the header  Pointer to the next free block ( ptr )  Size of the block ( size ) To simplify memory alignment  Make all memory blocks a multiple of the header size  Ensure header is aligned with largest data type (e.g., long ) Union: C technique for forcing memory alignment  Variable that may hold objects of different types and sizes  Made large enough to hold the largest data type, e.g., union Tag { int ival; float fval; char *sval; } u;

19 Free Block: Memory Alignment /* align to long boundary */ typedef long Align; union header { /* block header */ struct { union header *ptr; unsigned size; } s; Align x; /* Force alignment */ } typedef union header Header;

20 Allocate Memory in Units Keep memory aligned  Requested size is rounded up to multiple of header size Rounding up when asked for nbytes  Header has size sizeof(Header)  Round: (nbytes + sizeof(Header) – 1)/sizeof(Header) Example:  Suppose nbytes is 37  And sizeof(header) is 16 bytes  Then ( – 1)/16 is 52/16 which rounds down to

21 Allocate Memory in Units Allocate space for the header, too  So, need to allocate one more unit of memory void *malloc(unsigned int nbytes) { unsigned nunits; nunits = (nbytes + sizeof(Header) – 1)/sizeof(Header) + 1; … }

22 Free List: Circular Linked List Free blocks, linked together  Example: circular linked list Keep list in order of increasing addresses  Makes it easier to coalesce adjacent free blocks In use In use In use Free list

23 Allocation Algorithms Handling a request for memory (e.g., malloc)  Find a free block that satisfies the request  Must have a “size” that is big enough, or bigger Which block to return?  First-fit algorithm –Keep a linked list of free blocks –Search for the first one that is big enough  Best-fit algorithm –Keep a linked list of free blocks –Search for the smallest one that is big enough –Helps avoid fragmenting the free memory K&R presents a simpler “first fit” algorithm

24 Malloc: First-Fit Algorithm Start at the beginning of the list Sequence through the list  Keep a pointer to the previous element Stop when reaching first block that is big enough  Patch up the list  Return a block to the user ppprevp

25 First Case: A Perfect Fit Suppose the first fit is a perfect fit  Remove the element from the list  Link the previous element with the next element –prev->s.ptr = p->s.ptr  Return the current element to the user (skipping header) –return (void *) (p+1) pprev p+1

26 Second Case: Block is Too Big Suppose the block is bigger than requested  Divide the free block into two blocks  Keep first (now smaller) block in the free list –p->s.size -= nunits;  Allocate the second block to the user –p += p->s.size; –p->s.size = nunits; pp

27 Combining the Two Cases prevp = freep; /* start at beginning */ for (p=prevp->s.ptr; ; prevp=p, p=p->s.ptr) { if (p->s.size >= nunits) { if (p->s.size == nunits) /* fit */ prevp->s.ptr = p->s.ptr; else { /* too big, split in two */ p->s.size -= nunits; /* #1 */ p += p->s.size; /* #2 */ p->s.size = nunits; /* #2 */ } return (void *)(p+1); }

28 Beginning of the Free List Benefit of making free list a circular list  Any element in the list can be the beginning  Don’t have to handle the “end” of the list as special  Optimization: make head be where last block was found prevp = freep; /* start at beginning */ for (p=prevp->s.ptr; ; prevp=p, p=p->s.ptr) { if (p->s.size >= nunits) { /* Do stuff on previous slide */ … freep = prevp; /* move the head */ return (void *) (p+1); }

29 Oops, No Block is Big Enough! Cycling completely through the list  Check if the “for” loop returns back to the head of the list prevp = freep; /* start at beginning */ for (p=prevp->s.ptr; ; prevp=p, p=p->s.ptr) { if (p->s.size >= nunits) { /* Do stuff on previous slides */ … } if (p == freep) /* wrapped around */ Now, do something about it… }

30 What to Do When You Run Out Ask the operating system for additional memory  Ask for a very large chunk of memory  … and insert the new chunk into the free list ... and then try again, this time successfully Operating-system dependent  E.g., sbrk command in UNIX  See the morecore() function for details if (p == freep) /* wrapped around */ if ((p = morecore(nunits)) == NULL) return NULL; /* none left */

31 Free User passes a pointer to the memory block  void free(void *ap); Free function inserts block into the list  Identify the start of entry: bp = (Header *) ap – 1;  Find the location in the free list  Add to the list, coalescing entries, if needed apbp

32 Scanning Free List for the Spot Start at the beginning: p = freep; Sequence through the list: p = p->s.ptr; Stop at last entry before the to-be-freed element  (bp > p) && (bp s.ptr); In use FREE ME In use Free list bpp

33 Corner Cases: Beginning or End Check for wrap-around in memory  p >= p->s.ptr; See if to-be-freed element is located there  (bp > p) || (bp s.ptr); In use FREE ME In use Free list bpp

34 Inserting Into Free List New element to add to free list: bp Insert in between p and p->s.ptr  bp->s.ptr = p->s.ptr;  p->s.ptr = bp; But, there may be opportunities to coalesce bp pp->s.ptr

35 Coalescing With Neighbors Scanning the list finds the location for inserting  Pointer to to-be-freed element: bp  Pointer to previous element in free list: p Coalescing into larger free blocks  Check if contiguous to upper and lower neighbors In use FREE ME In use Free list bpp lowerupper

36 Coalesce With Upper Neighbor Check if next part of memory is in the free list  if (bp + bp->s.size == p->s.ptr) If so, make into one bigger block  Larger size: bp->s.size += p->s.ptr->s.size;  Copy next pointer: bp->s.ptr = p->s.ptr->s.ptr; Else, simply point to the next free element  bp->s.ptr = p->s.ptr; bp upper p lower p->s.ptr

37 Coalesce With Lower Neighbor Check if previous part of memory is in the free list  if (p + p->s.size == bp) If so, make into one bigger block  Larger size: p->s.size += bp->s.size;  Copy next pointer: p->s.ptr = bp->s.ptr; bp upper p lower p->s.ptr

38 Conclusions Elegant simplicity of K&R malloc and free  Simple header with pointer and size in each free block  Simple linked list of free blocks  Relatively small amount of code (~25 lines each) Limitations of K&R functions in terms of efficiency  Malloc requires scanning the free list –To find the first free block that is big enough  Free requires scanning the free list –To find the location to insert the to-be-freed block Next lecture, and programming assignment #4  Making malloc and free more efficient  Note: you are encouraged to do assignment in pairs