Files
sispro3/mm.c
2025-01-08 02:25:09 +01:00

213 lines
6.8 KiB
C
Executable File

// the allocator uses an implicit free list to manage memory blocks.
// it provides functionalities for allocation (mm_malloc), deallocation (mm_free), and heap initialization (mm_init). adjacent free blocks are coalesced to reduce fragmentation.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include "mm.h"
#include "memlib.h"
team_t team = {
"MA", "Marvin Aleksa", "marvin.aleksa@stud.uni-due.de", "", ""
};
#define SIZE_T_SIZE (ALIGN(sizeof(size_t))) // align to size_t bytes
#define ALIGN(size) (((size) + (7)) & ~0x7) // align to 8 bytes
#define WSIZE 4 // single word size
#define DSIZE 8 // double word size
#define CHUNKSIZE (1<<12) // initial heap size
#define PACK(size, alloc) ((size) | (alloc)) // pack size and allocated bit
#define GET(p) (*(unsigned int *)(p)) // read a word from address
#define PUT(p, val) (*(unsigned int *)(p) = (val)) // write a word to address
#define GET_SIZE(p) (GET(p) & ~0x7) // get size from address
#define GET_ALLOC(p) (GET(p) & 0x1) // get alloc bit from address
#define HDRP(bp) ((char *)(bp) - WSIZE) // get address of block header
#define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE) // get address of block footer
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE))) // get address of previous block
#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE))) // get address of next block
#define MAX(x, y) ((x) > (y) ? (x) : (y)) // get maximum value
// pointer to first block
static char *heap_listp = 0;
// find_fit - find a fit for a block with asize bytes.
static void *find_fit(size_t asize) {
char *bp;
// iterate through heap to find free block
for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
if (!GET_ALLOC(HDRP(bp)) && (GET_SIZE(HDRP(bp)) >= asize)) {
return bp;
}
}
return NULL; // no fit found
}
// place - place block of asize bytes at start of free block bp and split if remainder would be at least minimum block size.
static void place(void *bp, size_t asize) {
size_t csize = GET_SIZE(HDRP(bp)); // current block size
if ((csize - asize) >= (2 * DSIZE)) { // if remaining size is enough for a new block
PUT(HDRP(bp), PACK(asize, 1)); // mark block allocated
PUT(FTRP(bp), PACK(asize, 1));
bp = NEXT_BLKP(bp);
PUT(HDRP(bp), PACK(csize - asize, 0)); // create new free block
PUT(FTRP(bp), PACK(csize - asize, 0));
} else { // dont split
PUT(HDRP(bp), PACK(csize, 1));
PUT(FTRP(bp), PACK(csize, 1));
}
}
// coalesce - boundary tag coalescing. return pointer to coalesced block.
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));
if (prev_alloc && next_alloc) { // c1: both adjacent blocks allocated
return bp;
} else if (prev_alloc && !next_alloc) { // c2: next block free
size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size, 0));
} else if (!prev_alloc && next_alloc) { // c3: previous block free
size += GET_SIZE(HDRP(PREV_BLKP(bp)));
PUT(FTRP(bp), PACK(size, 0));
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
bp = PREV_BLKP(bp);
} else { // c4: both blocks free
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));
bp = PREV_BLKP(bp);
}
return bp;
}
// extend_heap - extend heap with free block and return its block pointer.
static void *extend_heap(size_t words) {
char *bp;
size_t size;
// allocate even number of words to maintain alignment
size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE;
if ((long)(bp = mem_sbrk(size)) == -1)
return NULL;
// initialize free block header/footer + 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 previous block free
return coalesce(bp);
}
// mm_init - initialize the malloc package.
int mm_init(void) {
// initialize heap with prologue block, initial free block, epilogue block
if ((heap_listp = mem_sbrk(4 * WSIZE)) == (void *)-1){
return -1;
}
PUT(heap_listp, 0); // alignment padding
PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 1)); // prologue header
PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 1)); // prologue footer
PUT(heap_listp + (3 * WSIZE), PACK(0, 1)); // epilogue header
heap_listp += (2 * WSIZE);
// extend empty heap with free block of CHUNKSIZE bytes
if (extend_heap(CHUNKSIZE / WSIZE) == NULL){
return -1;
}
return 0;
}
// mm_malloc - allocate a block of memory with at least the requested size. the block will be aligned to 8 bytes.
void *mm_malloc(size_t size) {
size_t asize; // adjusted block size
size_t extendsize; // amount to extend the heap if no fit found
char *bp;
// ignore spurious requests
if (size == 0)
return NULL;
// adjust block size to include overhead and alignment requirements
if (size <= DSIZE)
asize = 2 * DSIZE; // minimum block size
else
asize = DSIZE * ((size + (DSIZE) + (DSIZE - 1)) / DSIZE);
// search free list for a fit
if ((bp = find_fit(asize)) != NULL) {
place(bp, asize);
return bp;
}
// no fit found. get more memory and place block
extendsize = MAX(asize, CHUNKSIZE);
if ((bp = extend_heap(extendsize / WSIZE)) == NULL)
return NULL;
place(bp, asize);
return bp;
}
// mm_free - frees a block and coalesces it with adjacent free blocks if possible.
void mm_free(void *ptr) {
if (ptr == NULL) {
return; // ignore null pointers
}
// get size of block and mark as free
size_t size = GET_SIZE(HDRP(ptr));
PUT(HDRP(ptr), PACK(size, 0)); // update header to indicate free
PUT(FTRP(ptr), PACK(size, 0)); // update footer to indicate free
// coalesce block with adjacent free blocks
coalesce(ptr);
}
// mm_realloc - Implemented simply in terms of mm_malloc and mm_free
void *mm_realloc(void *ptr, size_t size)
{
void *oldptr = ptr;
void *newptr;
size_t copySize;
newptr = mm_malloc(size);
if (newptr == NULL)
return NULL;
copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE);
if (size < copySize)
copySize = size;
memcpy(newptr, oldptr, copySize);
mm_free(oldptr);
return newptr;
}