previous | start | next

Better mm_malloc

    1   void *mm_malloc(size_t size)
    2   {
    3       size_t asize;      /* adjusted block size */
    4       size_t extendsize; /* amount to extend heap if no fit */
    5       char *bp;
    6   
    7       /* Ignore spurious requests */
    8       if (size <= 0)
    9           return NULL;
   10   
   11       /* Adjust block size to include overhead and alignment reqs. */
   12       if (size <= DSIZE)
   13           asize = DSIZE + OVERHEAD;
   14       else
   15           asize = DSIZE * ((size + (OVERHEAD) + (DSIZE-1)) / DSIZE);
   16   
   17       /* Search the free list for a fit */
   18       if ((bp = find_fit(asize)) != NULL) {
   19           place(bp, asize);
   20           return bp;
   21       }
   22   
   23       /* No fit found. Get more memory and place the block */
   24       extendsize = MAX(asize,CHUNKSIZE);
   25       if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
   26           return NULL;
   27       place(bp, asize);
   28       return bp;
   29   }


previous | start | next