Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory...

41
Carnegie Mellon

Transcript of Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory...

Page 1: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 2: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 3: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

malloc

⬛malloc

.text

.data

bss

brk

Page 4: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪▪ malloc free

▪▪

Page 5: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

malloc#include <stdlib.h>

void *malloc(size_t size)▪

▪ size

▪ size == 0

▪ errno

void free(void *p)▪ p

▪ p malloc realloc

▪ calloc malloc

▪ realloc:▪ sbrk

Page 6: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

malloc#include <stdio.h>#include <stdlib.h>

void foo(int n) { int i, *p;

/* Allocate a block of n ints */ p = (int *) malloc(n * sizeof(int)); if (p == NULL) { perror("malloc"); exit(0); }

/* Initialize allocated block */ for (i=0; i<n; i++)

p[i] = i;

/* Return allocated block to the heap */ free(p);}

Page 7: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 8: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

p1 = malloc(4*SIZ)

p2 = malloc(5*SIZ)

p3 = malloc(6*SIZ)

free(p2)

p4 = malloc(2*SIZ)

#define SIZ sizeof(int)

Page 9: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪ malloc free

▪ free malloc

▪▪ malloc

▪▪

▪▪

▪▪ malloc

Page 10: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

⬛ malloc free▪

▪▪

▪ malloc free▪

Page 11: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

⬛ malloc free▪

⬛▪ malloc(p) p▪

⬛▪

▪ sbrk

⬛▪ ≤

Page 12: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪▪

Page 13: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪▪▪

Page 14: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

p4 = malloc(7*SIZ)

#define SIZ sizeof(int)

p1 = malloc(4*SIZ)

p2 = malloc(5*SIZ)

p3 = malloc(6*SIZ)

free(p2)

Page 15: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 16: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪▪

p0 = malloc(4*SIZ)p0

free(p0)

Page 17: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

4

4

Page 18: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 19: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪⬛

▪▪▪

Page 20: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 21: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪▪

▪▪▪

▪▪▪

p = start; while ((p < end) && \\ not passed end ((*p & 1) || \\ already allocated (*p <= len))) \\ too small p = p + (*p & -2); \\ goto next block (word addressed)

Page 22: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) << 1; // round up to even int oldsize = *p & -2; // mask out low bit *p = newsize | 1; // set new length if (newsize < oldsize) *(p+newsize) = oldsize - newsize; // set length in remaining} // part of block

addblock(p, 4)

Page 23: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) << 1; // round up to even int oldsize = *p & -2; // mask out low bit *p = newsize | 1; // set new length if (newsize < oldsize) *(p+newsize) = oldsize - newsize; // set length in remaining} // part of block

addblock(p, 4)

Page 24: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) << 1; // round up to even int oldsize = *p & -2; // mask out low bit *p = newsize | 1; // set new length if (newsize < oldsize) *(p+newsize) = oldsize - newsize; // set length in remaining} // part of block

addblock(p, 4)

Page 25: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) << 1; // round up to even int oldsize = *p & -2; // mask out low bit *p = newsize | 1; // set new length if (newsize < oldsize) *(p+newsize) = oldsize - newsize; // set length in remaining} // part of block

addblock(p, 4)

Page 26: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪ void free_block(ptr p) { *p = *p & -2 }

free(p) p

malloc(5*SIZ)

Page 27: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

void free_block(ptr p) { *p = *p & -2; // clear allocated flag next = p + *p; // find next block if ((*next & 1) == 0) *p = *p + *next; // add to this block if} // not allocated

free(p) p

Page 28: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

⬛▪▪▪

Page 29: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 30: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 31: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 32: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 33: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 34: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪▪

Page 35: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 36: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 37: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 38: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 39: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

Page 40: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪▪▪

▪▪

▪ free▪ free

▪ malloc▪

Page 41: Dynamic Memory Allocation: Basic Concepts213/lectures/19-malloc-basic.pdf · Dynamic Memory Allocation: Basic Concepts 15-213: ... (size_t size) Successful: Returns a pointer to a

Carnegie Mellon

▪⬛

▪▪

▪▪

⬛ malloc/free