previous | start | next

mm_malloc

A very simple initial version of mm_malloc allocates 8 extra bytes for the header and footer in addition to the payload:

/*
 * mm_malloc - Allocate a block by incrementing the brk pointer.
 *     Always allocate a block whose size is a multiple of the
      alignment.
 */
void *mm_malloc(size_t size)
{
    int newsize = ALIGN(size + 8);
    void *p = mem_sbrk(newsize);
    if ((int)p == -1)
        return NULL;
    else {
        *(size_t *)p = (newsize | 1);
        *(size_t *)(p + newsize - 4) = (newsize | 1);
        return (void *)(p + 4);
    }
}

A correspondingly simple version of mm_free is:

/*
 * mm_free - Freeing a block does nothing.
 */
void mm_free(void *ptr)
{
}


previous | start | next