The College of Engineering at the University of Utahcs4400/malloc-2.pdf · 2018. 11. 29. ·...

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

Transcript of The College of Engineering at the University of Utahcs4400/malloc-2.pdf · 2018. 11. 29. ·...

  • ������������������������������������������������

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

    Utilization

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

    Throughput

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

    ���

  • ���������������������

    21 2 60 6 31 3 40 4 01

    p2 = malloc(1) 21 2 31 3 30 3 31 3 40 4 01

  • ���������������������

    21 2 60 6 31 3 40 4 01

    p2 = malloc(1) 21 2 31 3 30 3 31 3 40 4 01

    p4 = malloc(4)

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

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

    ���

  • ���������������������

    21 2 60 6 31 3 40 4 01

    p2 = malloc(1) 21 2 60 6 31 3 41 4 01

  • ���������������������

    21 2 60 6 31 3 40 4 01

    p2 = malloc(1) 21 2 60 6 31 3 41 4 01

    p4 = malloc(4) 21 2 61 6 31 3 41 4 01

    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 #include #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

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

    ��

  • ���������������������������������������������

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

    21 2 60 6 31 3 41 4 01

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

    51 0 5 20 1 31 1 61 0 6 00 1

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

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

    �����

  • ���������������������������������������������

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

    21 2 60 6 31 3 41 4 01

    �������������������������������������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

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    free(p3)free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    �����

  • �������������������

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

    free_listfirst_bp

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    free(p3)free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    ��

  • �������������������

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

    free_listfirst_bp

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    free(p3)free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    ��

  • �������������������������

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    ��

  • �������������������������

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    free(p4)

    ��

  • �������������������������

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01������������������������������������������������

    free(p4)

    ��

  • �������������������������

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    free(p4)

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    ��

  • �������������������������

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    free(p4)

    free_listfirst_bp

    21 2 41 4 30 3 31 3 90 9 01

    ��

  • �������������������������

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01���������������������������������������������

    free(p4)

    free_listfirst_bp

    21 2 41 4 30 3 31 3 90 9 01

    ��

  • �������������������������

    free_listfirst_bp

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    free(p4)

    free_listfirst_bp

    21 2 41 4 30 3 31 3 90 9 01

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

    ��

  • ������������������������

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

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

    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

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

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

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

    �����

  • ���������������������������mmap

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

    first_chunk

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    21 2 41 4 30 3 31 3 90 9 01

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    ��

  • ���������������������������mmap

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

    first_chunk

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    21 2 41 4 30 3 31 3 90 9 01

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

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

    ��

  • ���������������������������mmap

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

    first_chunk

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    21 2 41 4 30 3 31 3 90 9 01

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

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

    ��

  • ���������������������������mmap

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

    first_chunk

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    21 2 190 19 01

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    ��

  • ���������������������������mmap

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

    first_chunk

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    21 2 190 19 01

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

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

    ��

  • ���������������������������mmap

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

    first_chunk

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

    ��

  • ���������������������������mmap

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

    free_listfirst_chunk

    21 2 41 4 30 3 31 3 31 3 31 3 30 3 01

    21 2 41 4 30 3 31 3 30 3 31 3 30 3 01

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

    ��

  • �����������������������������������������������������

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

    � ���������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

    ��