lecture25 - CIS 450 Computer Architecture and Organization...

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

View Full Document Right Arrow Icon
CIS 450 – Computer Architecture and Organization Lecture 25: Dynamic Memory Allocation Mitch Neilsen (neilsen@ksu.edu ) 219D Nichols Hall
Background image of page 1

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

View Full DocumentRight Arrow Icon
Topics ± Simple explicit allocators z Data structures z Mechanisms z Policies ± BrickOS Example
Background image of page 2
Harsh Reality Memory Matters Memory is not unbounded ± It must be allocated and managed ± Many applications are memory dominated z Especially those based on complex, graph algorithms Memory referencing bugs especially pernicious ± Effects are distant in both time and space Memory performance is not uniform ± Cache and virtual memory effects can greatly affect program performance ± Adapting program to characteristics of memory system can lead to major speed improvements
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 z E.g., malloc and free in C ± Implicit: application allocates, but does not free space z 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 simple explicit memory allocation today Application Dynamic Memory Allocator Heap Memory
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Malloc Package #include <stdlib.h> void *malloc(size_t size) ± If successful: z Returns a pointer to a memory block of at least size bytes, (typically) aligned to 8-byte boundary. z If size == 0 , returns NULL ± 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.
Background image of page 6
Malloc Example void foo(int n, int m) { int i, *p; /* allocate a block of n ints */ p = (int *)malloc(n * sizeof(int)); if (p == NULL) { perror("malloc"); exit(0); } for (i=0; i<n; i++) p[i] = i; /* add m bytes to end of p block */ if ((p = (int *) realloc(p, (n+m) * sizeof(int))) == NULL) { perror("realloc"); exit(0); } for (i=n; i < n+m; i++) p[i] = i; /* print new array */ for (i=0; i<n+m; i++) printf("%d\n", p[i]); free(p); /* return p to available memory pool */ }
Background image of page 7

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

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

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

View Full DocumentRight Arrow Icon
Constraints Applications: ± Can issue arbitrary sequence of allocation and free requests ± Free requests must correspond to an allocated block
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 43

lecture25 - CIS 450 Computer Architecture and Organization...

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

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