All pastes #632368 Raw Edit

Someone

public c v1 · immutable
#632368 ·published 2007-07-24 20:26 UTC
rendered paste body
#include "mymallocwrapper.h"#include "memlib.h"#include "mymalloc.h"/* static function prototypes */static void *coalesce(void *bp);static void *find_fit(size_t asize);static void place(void *bp, size_t asize);static void *extend_heap(size_t words);/* global variable, marking first block of the heap */static char *heap_listp;/************************************************* * my_init - create the initial empty heap *************************************************/int my_init(void){    /* initialize empty heap */    if((heap_listp = mem_sbrk(4*WSIZE)) == NULL )        return -1;    PUT(heap_listp, 0);                         /* alignment padding */    PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1));   /* prologue header */    PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1));   /* prologue footer */    PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1));    /* epilogue header */    heap_listp = heap_listp + DSIZE;    /* extend the heap by CHUNKSIZE bytes */    if (extend_heap(CHUNKSIZE/WSIZE) == NULL)        return -1;    return 0;}/************************************************* * extend_heap - used to increase the size of the * heap *************************************************/static void *extend_heap(size_t words){    char *bp;    size_t size;    /* Allocate an even number of words to maintain alignment */    size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE;    if((int)(bp = mem_sbrk(size)) < 0)        return NULL;    /* Inititalize free block header/footer and the epilogue header */    PUT(HDRP(bp), PACK(size, 0));           /* free block header */    PUT(FTRP(bp), PACK(size, 0));           /* free block footer */    PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));   /* new epilogue header */    /* coalesce if the previous block was free */    return coalesce(bp);}/*********************************************************** * my_free - mimic of libc free() function, used to free a  * previously allocated block of memory located in the heap ***********************************************************/void my_free(void *bp){    size_t size = GET_SIZE(HDRP(bp));    PUT(HDRP(bp), PACK(size, 0));    PUT(FTRP(bp), PACK(size, 0));    coalesce(bp);}/******************************************************************* * coalesce - used to merge adjacent free blocks of memory in a * "conservative" attempt to protect against internal fragmentation *******************************************************************/static void *coalesce(void *bp){    size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));    size_t size = GET_SIZE(HDRP(bp));    /* Previous and next bloacks are both allocated */    if(prev_alloc && next_alloc)    {        return 1;    }    /* Previous block is allocated and the next block is free */    else if(prev_alloc && !next_alloc)    {        size = size + GET_SIZE(HDRP(NEXT_BLKP(bp)));        PUT(HDRP(bp), PACK(size, 0));        PUT(FTRP(bp), PACK(size, 0));        return(bp);    }    /* Previous block is free and the next block is allocated */    else if(!prev_alloc && next_alloc)    {        size = size + GET_SIZE(HDRP(PREV_BLKP(bp)));        PUT(FTRP(bp), PACK(size, 0 ));        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));        return (PREV_BLKP(bp));    }    /* The previous and next blocks are both free */    else    {        size = size + GET_SIZE(HDRP(PREV_BLKP(bp))) +                        GET_SIZE(FTRP(NEXT_BLKP(bp)));        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));        PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));        return(PREV_BLKP(bp));    }}/******************************************************************* * my_malloc - mimic of libc malloc(), used to dynamically allocate * memory in the heap. *******************************************************************/void *my_malloc(size_t size){    size_t asize;       /* adjust block size */    size_t extendsize;  /* amount to extend heap if no fit */    char *bp;    /* ignore spurious requests */    if(size <= 0)        return NULL;    /* adjust block size to include overhead and alignment reqs. */    if(size <= DSIZE)        asize = DSIZE + OVERHEAD;    else        asize = DSIZE * ((size + (OVERHEAD) + (DSIZE-1)) / DSIZE);    /* Search the free list for a fit */    if((bp = find_fit(asize)) != NULL)    {        place(bp, asize);        return bp;    }    /* No fit found. Get more memory and place the block */    extendsize = MAX(asize, CHUNKSIZE);    if((bp = extend_heap(extendsize/WSIZE)) == NULL)        return NULL;    place(bp, asize);    return bp;}/********************************************************************** * find_fit - using a first fit search, will search through available * memory pointed to by heap_listp and return a pointer to the first * found block of memory that will satisfy the query **********************************************************************/static void *find_fit(size_t asize){    void *bp;    /* first fit search */    for(bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp))    {        if(!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp))))        {            return bp;        }    }        /* no fit found */    return NULL;}static void place(void *bp, size_t asize){    size_t csize = GET_SIZE(HDRP(bp));    if((csize - asize) >= (DSIZE + OVERHEAD))    {        PUT(HDRP(bp), PACK(asize, 1));        PUT(FTRP(bp), PACK(asize, 1));        bp = NEXT_BLKP(bp);        PUT(HDRP(bp), PACK(csize - asize, 0));        PUT(FTRP(bp), PACK(csize - asize, 0));    }    else     {        PUT(HDRP(bp), PACK(csize, 1));        PUT(FTRP(bp), PACK(csize, 1));    }}