53
���� �� ����� Utilization �� Throughput

my.eng.utah.educs4400/f17/malloc-2.pdfwhile (GET_SIZE(HDRP(bp)) != 0) {if (!GET_ALLOC(HDRP(bp)) && (GET_SIZE(HDRP(bp)) >= new_size)) {set_allocated(bp, new_size); return bp;} bp =

  • Upload
    others

  • View
    41

  • Download
    1

Embed Size (px)

Citation preview

������������������������������������������������

���������������������������������������

Utilization

���������������������������������

Throughput

�����������������������������������

���

���������������������

212 6

06 3

13 4

04 0

1

p2 = malloc(1) 212 3

13 3

03 3

13 4

04 0

1

���������������������

212 6

06 3

13 4

04 0

1

p2 = malloc(1) 212 3

13 3

03 3

13 4

04 0

1

p4 = malloc(4)

First fit:���������������������������������������������������

�����������������������

���

���������������������

212 6

06 3

13 4

04 0

1

p2 = malloc(1) 212 6

06 3

13 4

14 0

1

���������������������

212 6

06 3

13 4

04 0

1

p2 = malloc(1) 212 6

06 3

13 4

14 0

1

p4 = malloc(4) 212 6

16 3

13 4

14 0

1

Best fit:�������������������������������������������������������

�����������������������������

����

������������������������

��������������������������������������

while (GET_SIZE(HDRP(bp)) != 0) { if (!GET_ALLOC(HDRP(bp)) && (GET_SIZE(HDRP(bp)) >= new_size)) { set_allocated(bp, new_size); return bp; } bp = NEXT_BLKP(bp);}

��

�����������������������

�����������������

void *best_bp = NULL; while (GET_SIZE(HDRP(bp)) != 0) { if (!GET_ALLOC(HDRP(bp)) && (GET_SIZE(HDRP(bp)) >= new_size)) { if (!best_bp || (GET_SIZE(HDRP(bp)) < GET_SIZE(HDRP(best_bp)))) best_bp = bp; } bp = NEXT_BLKP(bp);}if (best_bp) { set_allocated(best_bp, new_size); return best_bp;}

����

�����������������������������������

����������������������

p = mm_malloc(8);memset(p, 0, 8);

p �������

block_header 0 0 0 0 0 0 0 0 block_footer

��

����������������������

p = mm_malloc(8);memset(p, 0, 8);

p �������

block_header 0 0 0 0 0 0 0 0 block_footer ��������������������

��

����������������������

p = mm_malloc(8);memset(p, 0, 8);

p �������

block_header 0 0 0 0 0 0 0 0 block_footer ��������������������

� ��������������������������������������������������������������

Internal fragmentation�����������������������������������������������������������������������������

�����

����������������������

p = mm_malloc(8);memset(p, 0, 8);

p �������

block_header 0 0 0 0 0 0 0 0 block_footer ��������������������

� �����������������������������������

� �������������������

� ���������������������������������

� �������������������������������

��

�������������������������������������� �������

block_header 0 0 0 0 0 0 0 0 block_footer

typedef struct { size_t size; char allocated;} block_header;

typedef struct { size_t size; int filler;} block_footer;

�����

�������������������������������������� �������

block_header 0 0 0 0 0 0 0 0 block_footer

typedef size_t block_header; typedef size_t block_footer;

����������������������������������������������������������������������

���������������������������������������������������

�����

�������������������������������������� �������

block_header 0 0 0 0 0 0 0 0 block_footer

typedef size_t block_header; typedef size_t block_footer;

#define GET(p) (*(size_t *)(p)) #define GET_ALLOC(p) (GET(p) & 0x1)#define GET_SIZE(p) (GET(p) & ~0xF)

#define PUT(p, val) (*(size_t *)(p) = (val)) #define PACK(size, alloc) ((size) | (alloc))

�����

������������

#include <stdio.h>#include <stdlib.h> #define GET(p) (*(size_t *)(p))#define PUT(p, val) (*(size_t *)(p) = (val)) #define GET_ALLOC(p) (GET(p) & 0x1)#define GET_SIZE(p) (GET(p) & ~0xF) #define PACK(size, alloc) ((size) | (alloc)) int main() { void *p = malloc(sizeof(size_t)); PUT(p, PACK(48, 1)); printf("%ld %s\n", GET_SIZE(p), (GET_ALLOC(p) ? "alloc" : "unalloc"));}

����

��

���������

��������������������������������������������������

��

first_bp0x50008

0x50000

�������

block_header 0 0 0 0 0 0 0 0 block_footer

���������

� ����������first_bp����������������������

��

first_bp0x50010

0x50008

�������

block_header 0 0 0 0 0 0 0 0 block_footer

� ����������������������������������������

�����

��������������������������������

���������������������������������������������

typedef int block_header;typedef int block_footer;#define GET(p) (*(int *)(p))#define PUT(p, val) (*(int *)(p) = (val))

�������

int 0 0 0 0 0 0 0 0 int

���������������������������������������������������

��

���������������������������������������������

��������������������������������������������������������

212 6

06 3

13 4

14 0

1

�������������������������������������previous�������������������

51 0

5 20 1

31 1

61 0

6 00 1

���������������������������������������������������������

free(p2)�⇒���������������������������������������PREV_BLKP

�����

���������������������������������������������

��������������������������������������������������������

212 6

06 3

13 4

14 0

1

�������������������������������������previous�������������������

51 0

5 20 1

31 1

61 0

6 00 1

���������������������������������������������������������

free(p3)�⇒�������������������������������������������PREV_BLKP

��

��������������������������������

first_bp

41

41

41

41

41

41

41

41

41

41

41

41

41

41

41

41

��

��������������������������������

41

41

41

41

41

41

41

41

41

41

40

41

41

41

41

11

��

��������������������������������

41

41

41

41

41

41

41

41

41

41

40

41

41

41

41

11

��������������������������������������������������������������

��

��������������������������������

41

41

41

41

41

41

41

41

41

41

40

41

41

41

41

11

��������������������������������������������������������������

�����������������������������all�����������������������������������������

���������������������������explicit free list��������������implicit free list

��

�������������������

�����������������������������������������������������������������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

free(p3)

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

�����

�������������������

�����������������������������������������������������������������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

free(p3)

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��

�������������������

�����������������������������������������������������������������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

free(p3)

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��

�������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��

�������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

free(p4)

��

�������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

������������������������������������������������

free(p4)

��

�������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

free(p4)

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��

�������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

free(p4)

free_listfirst_bp

212 4

14 3

03 3

13 9

09 0

1

��

�������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

���������������������������������������������

free(p4)

free_listfirst_bp

212 4

14 3

03 3

13 9

09 0

1

��

�������������������������

free_listfirst_bp

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

free(p4)

free_listfirst_bp

212 4

14 3

03 3

13 9

09 0

1

������������������������������������������������������

��

������������������������

���������������������������

�������������������������

typedef struct list_node { struct list_node *prev; struct list_node *next;} list_node; .... void *mm_malloc(size_t size) { int need_size = max(size, sizeof(list_node)); int new_size = ALIGN(need_size + OVERHEAD); ....}

�����

������������������������

���������������������������

�������������������������

void *coalesce(void *bp) { .... if (prev_alloc && next_alloc) { /* Case 1 */ add_to_free_list((list_node *)bp); } ....}

��

��������������������������

First fit�����best fit���������������������������������������������

�������������

LIFO�����������������������������������������������������������������������������������������������������

address ordered���������������������������������������������������������

�����

��������������������

��segregated free list���������������������������������������������������������������������������������

free_lists

30

3 30

3 30

3

40

4 40

4 40

4

50

5 50

5 50

5

60

6 70

7

100

10

��

��������������������

��segregated free list���������������������������������������������������������������������������������

free_lists

30

3 30

3 30

3

40

4 40

4 40

4

50

5 50

5 50

5

60

6 70

7

100

10

�������������������������������

��

��������������������

��segregated free list���������������������������������������������������������������������������������

free_lists

30

3 30

3 30

3

40

4 40

4 40

4

50

5 50

5 50

5

60

6 70

7

100

10

���������������������

��

��������������������

��������������������������free tree����������������������������

free_tree

60

6

50

5

50

5 60

6

60

6

70

7

��

���������������������������mmap

�����������������������������������������������������������������

first_bp

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

�������������������mmap������������sbrk������mmap���������������������������

�������sbrk�����������mmap����������������������������������������������

�����

���������������������������mmap

������������������������������������sbrk����������������

first_chunk

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

212 4

14 3

03 3

13 9

09 0

1

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��

���������������������������mmap

������������������������������������sbrk����������������

first_chunk

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

212 4

14 3

03 3

13 9

09 0

1

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

�������������������mmap��������������������������������

��

���������������������������mmap

������������������������������������sbrk����������������

first_chunk

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

212 4

14 3

03 3

13 9

09 0

1

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

����������������������first_bp���������������������������

��

���������������������������mmap

������������������������������������sbrk����������������

first_chunk

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

212 19

019 0

1

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��

���������������������������mmap

������������������������������������sbrk����������������

first_chunk

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

212 19

019 0

1

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

���������������������������������������������munmap

��

���������������������������mmap

������������������������������������sbrk����������������

first_chunk

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��

���������������������������mmap

������������������������������������sbrk����������������

free_listfirst_chunk

212 4

14 3

03 3

13 3

13 3

13 3

03 0

1

212 4

14 3

03 3

13 3

03 3

13 3

03 0

1

��������������������������

��

�����������������������������������������������������

���������������������������������������������

� ���������free��������������������������������

Based on the address: the allocator keeps a mapping ofaddress ranges to block sizes

�����

�����������������������������������������������������

���������������������������������������������

� �������������������������������������

Through a free list or chunk-specific bitmap

��

�����������������������������������������������������

���������������������������������������������

� ������������������������������������������������������

Any unallocated block will work within a chunk that holds theblock size

��

�����������������������������������������������������

���������������������������������������������

� ���������������������������������������

Block sizes must be rounded up to match some chunk’scontent

��

�����������������������������������������������������

���������������������������������������������

� �������������������������������������������

When no chunk for the block size has any unallocatedblocks

��