Lecture15-memory-allocation

Lecture15-memory-allocation - CS 3214 Computer Systems...

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

View Full Document Right Arrow Icon
CS 3214 Computer Systems Godmar Back Lecture 15
Background image of page 1

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

View Full DocumentRight Arrow Icon
Announcements Project 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 1/2/12
Background image of page 2
Memory Management Part 1 1/2/12 CS 3214 Fall 2011 3 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Dynamic Memory Allocation Explicit vs. Implicit Memory Allocator 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 Application Dynamic Memory Allocator Heap Memory 1/2/12 4 CS 3214 Fall 2011
Background image of page 4
Process Memory Image kernel virtual memory Memory mapped region for shared libraries run-time heap (via malloc ) program text ( . text ) initialized data ( . data ) uninitialized data ( . bss ) stack 0 %esp memory invisible to user code the “ brk ” ptr Allocators request additional heap memory from the operating system using the sbrk function. Initial start of the heap is randomized (a bit above end of .bss, usually) 1/2/12 5 CS 3214 Fall 2011
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 If 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. 1/2/12 6 CS 3214 Fall 2011
Background image of page 6
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 1/2/12 7 CS 3214 Fall 2011
Background image of page 7

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

View Full DocumentRight Arrow Icon
Allocation Examples p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) 1/2/12 8 CS 3214 Fall 2011
Background image of page 8
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 1/2/12 9 CS 3214 Fall 2011
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 44

Lecture15-memory-allocation - CS 3214 Computer Systems...

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

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