Lecture15-memory-allocation

Lecture15-memory-allocation - Announcements Project 3 due...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS 3214 Computer Systems Godmar Back Lecture 15 Announcements •P ro jec t 3 due Oct 14 • Exercise 5 due Oct 18 – Extended by 1 day – see piazza • Midterm Oct 25 – See announcement • See sample midterms/final on website CS 3214 Fall 2011 2 10/17/2011 Some of the following slides are taken with permission from Complete Powerpoint Lecture Notes for Computer Systems: A Programmer's Perspective (CS:APP) Randal E. Bryant and David R. O'Hallaron http://csapp.cs.cmu.edu/public/lectures.html MEMORY MANAGEMENT Part 1 10/17/2011 CS 3214 Fall 2011 3 Dynamic Memory Allocation Explicit vs. Implicit Memory Allocator Application Dynamic Memory Allocator Heap Memory Explicit: application allocates and frees space • E.g., malloc and free in C – Implicit: application allocates, but does not free space • E.g. garbage collection in Java, ML or Lisp Allocation – In both cases the memory allocator provides an abstraction of memory as a set of blocks – Doles out free memory blocks to application Will discuss explicit memory allocation today 10/17/2011 4 CS 3214 Fall 2011 Process Memory Image kernel virtual memory Memory mapped region for shared libraries stack %esp memory invisible to user code Allocators request additional heap memory from the operating system using the sbrk function. run-time heap (via malloc ) program text ( . text ) initialized data ( . data ) uninitialized data ( . bss ) 0 the “ brk ” ptr Initial start of the heap is randomized (a bit above end of .bss, usually) 10/17/2011 5 CS 3214 Fall 2011 The Malloc API #include <stdlib.h> void *malloc(size_t size) – If successful: • Returns a pointer to a memory block of at least size bytes, (typically) aligned to 8-byte boundary; use memalign() for other alignments •I f size == 0 , may return either NULL or a pointer that must be freed (platform-dependent) If unsuccessful: returns NULL (0) and sets errno . void free(void *p) – Returns the block pointed at by p to pool of available memory –p must come from a previous call to malloc or realloc . void *realloc(void *p, size_t size) – Changes size of block p and returns pointer to new block. – Contents of new block unchanged up to min of old and new size. 10/17/2011 6 CS 3214 Fall 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Assumptions • Assumptions made in this lecture – Memory is word addressed (each word can hold a pointer) Allocated block (4 words) Free block (3 words) Free word Allocated word 10/17/2011 7 CS 3214 Fall 2011 Allocation Examples p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) 10/17/2011 8 CS 3214 Fall 2011 Constraints • Applications: (clients) – Can issue arbitrary sequence of allocation and free requests – Free requests must correspond to an allocated block • Allocators – Can’t control number or size of allocated blocks – Must respond immediately to all allocation requests • i.e., can’t reorder or buffer requests – Must allocate blocks from free memory • i.e., can place allocated blocks only in free memory • i.e., must maintain all data structures needed in memory they manage – Must align blocks so they satisfy all alignment requirements
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Lecture15-memory-allocation - Announcements Project 3 due...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online