Dynamic Memory Allocation
1 Dynamic Memory Allocation
Outline Implementation of a simple allocator Explicit Free List Segregated Free List 5 uggested reading:10.9,10.10,.10.110.12, 10.13 Segregate:隔离
2 Outline • Implementation of a simple allocator • Explicit Free List • Segregated Free List • Suggested reading: 10.9, 10.10, 10.11, 10.12, 10.13 Segregate:隔离
ynamic Memory Allocation P731 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
3 Dynamic Memory Allocation P731 • 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
Dynamic Memory Allocation Allocation In both cases the memory allocator provides an abstraction of memory as a set of blocks Doles out free memory blocks to application Doles:发放
4 Dynamic Memory Allocation • Allocation – In both cases the memory allocator provides an abstraction of memory as a set of blocks – Doles out free memory blocks to application Doles: 发放
10.9. 1 The malloc and free Functions
5 10.9.1 The malloc and free Functions
Malloc package P731 ·# include void * malloc(size t size if successful returns a pointer to a memory block of at least size bytes, aligned to 8-byte boundary if size==0. returnS NULL if unsuccessful returnS null 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, calloc or realloc
6 Malloc package P731 • #include • void *malloc(size_t size) – if successful: • returns a pointer to a memory block of at least size bytes, aligned to 8-byte boundary. • if size==0, returns NULL – if unsuccessful: returns NULL • 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,calloc or realloc
sarko Function P732 ·# include void* sbrk(int incr) If successful It returns the old value of brk If unsuccessful · It returns-1 · It sets errno to nomen If incr is zero It returns the current value incr can be a negative number
7 sbrk() Function P732 • #include • void *sbrk(int incr) – If successful • It returns the old value of brk – If unsuccessful • It returns –1 • It sets errno to ENOMEM – If incr is zero • It returns the current value – incr can be a negative number
Assumptions Assumptions made in this lecture memory is word addressed (each word can hold a pointer) Free word Allocated block Free block (4 words) 3 words) Allocated word
8 • 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 Assumptions
Allocation examples pl malloc(4) p2 malloc(5) 3=ma11oc(6) free(p2) p4 malloc(2) Figure 10.36 P733
9 p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) Allocation examples Figure 10.36 P733
10.9.2 Why Dynamic Memory Allocation
10 10.9.2 Why Dynamic Memory Allocation