ass3_manual - ArmandasRokas/dtu_notes GitHub Wiki
How to begin with assignment 3
- First you need to initialize data structure that will represent the memory blocks by using
memoryList. It should be done under/* TODO: Initialize memory management structur*/in theinitmem()function in themymem.csource file. Here's how to create the first node for a double linked list and to assign it to the head:
head = (struct memoryList*) malloc(sizeof (struct memoryList));
head->last = NULL;
head->next = NULL;
head->size = sz; // initialy the first block size is equals to the memory pool size.
head->alloc = 0; // not allocated
head->ptr = myMemory; // points to the same memory adress as the memory pool
- Now you can begin to implement
mymallocfunction. In order to keep it simple you can just create a new block (a node in double linked list) next to the free block, whenmymallocfunction is called. Next you need to change the size of the current block to be requested size, while the size of the new block should be set to the remaining size. Lastly you should set the current block to be allocated (currBlock->alloc = 1) . Here's how the double linked list should look initially, after callingmymalloc(100)and aftermymalloc(50):
null <- 500(free) -> null // Initialy there is only one head node (a block) which size is equal to the whole memory pool
null <- 100(alloc) <-> 400(free) -> null // Efter mymalloc(100)
null <- 100(alloc) <-> 50(alloc) <-> 350(free) -> null // Efter mymalloc(50)
//and so on.
- In order to debug your implementation you can run:
-
make -
./mem -try first(if you implementing first-fit) -
Note : it is required that you have implemented at least
print_memory(), where you should basically loop the double linked list and print the size of each node and if they are allocated or not.