Commit 790bb27f authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

Lab 7 complete

parent 2a9fade6
......@@ -11,7 +11,7 @@ BIN = ./bin
vpath %.h ./include
vpath %.c ./src
EXECS = dlisttest alloc-dlist_test memory-test
EXECS = dlisttest alloc-dlist_test memory-test memory-test2 memory-test3
all: $(EXECS)
......@@ -36,6 +36,13 @@ alloc-dlist_test: $(SRC)/alloc-dlist_test.c $(OBJ)/dnode.o $(OBJ)/dlist.o
memory-test: $(SRC)/memory-test.c $(OBJ)/dnode.o $(OBJ)/dlist.o $(OBJ)/allocator.o
$(CC) $(CFLAGS) $(OBJ)/dnode.o $(OBJ)/dlist.o $(OBJ)/allocator.o $(SRC)/memory-test.c -o $(BIN)/memory-test
memory-test2: $(SRC)/memory-test2.c $(OBJ)/dnode.o $(OBJ)/dlist.o $(OBJ)/allocator.o
$(CC) $(CFLAGS) $(OBJ)/dnode.o $(OBJ)/dlist.o $(OBJ)/allocator.o $(SRC)/memory-test2.c -o $(BIN)/memory-test2
memory-test3: $(SRC)/memory-test3.c $(OBJ)/dnode.o $(OBJ)/dlist.o $(OBJ)/allocator.o
$(CC) $(CFLAGS) $(OBJ)/dnode.o $(OBJ)/dlist.o $(OBJ)/allocator.o $(SRC)/memory-test3.c -o $(BIN)/memory-test3
.PHONY: clean
......
In my allocator.c file, the allocate function takes an extra parameter that specifies which type of allocation policy to use: 0 - First Fit, 1 - Best Fit, 2 - Worst Fit. After the type of allocation policy is specified, then you can proceed as normal.
......@@ -36,49 +36,56 @@ int allocator_init(size_t size){
void *allocate(size_t size, int alloc_type){
struct dnode *curr_data;
void *ptr;
// First-fit policy
if (alloc_type == 0){
curr_data = dlist_iter_begin(free_list);
while (curr_data->size < size){
curr_data = dlist_iter_next(free_list);
if (alloc_type == 0){
for (curr_data = dlist_iter_begin(free_list); curr_data != NULL; curr_data = dlist_iter_next(free_list)) {
if(curr_data->size > size){
ptr = curr_data->data;
dlist_add_back(allocated_list,ptr,size);
curr_data->size = curr_data->size - size;
curr_data->data = curr_data->data + size;
break;
}
}
ptr = curr_data->data;
dlist_add_back(allocated_list,curr_data->data,size);
curr_data->size = curr_data->size - size;
curr_data->data = curr_data->data + size;
}
// Best-fit policy
if (alloc_type == 1){
curr_data = dlist_iter_begin(free_list);
size_t min = curr_data->size - size;
size_t min = curr_data->size;
struct dnode *smallest_block;
//for (curr_data = dlist_iter_begin(free_list); curr_data != NULL;
// curr_data = dlist_iter_next(free_list)) {
// if ((curr_data->size > size) && ((curr_data->size - size) < min)){
// min = curr_data->size - size;
// smallest_block = curr_data;
// }
//}
for (curr_data = dlist_iter_begin(free_list); curr_data != NULL; curr_data = dlist_iter_next(free_list)) {
if(curr_data->size - size < min){
smallest_block = curr_data;
}
}
ptr = smallest_block->data;
dlist_add_back(allocated_list,ptr,size);
smallest_block->size = curr_data->size - size;
smallest_block->data = curr_data->data + size;
}
//dnode_print(smallest_block);
//ptr = smallest_block->data;
//dlist_add_back(allocated_list,smallest_block->data,size);
//curr_data->size = (curr_data->size) - size;
//curr_data->data = curr_data->data + size;
// Worst-fit policy
if (alloc_type == 2){
curr_data = dlist_iter_begin(free_list);
size_t max = 0;
struct dnode *largest_block;
for (curr_data = dlist_iter_begin(free_list); curr_data != NULL; curr_data = dlist_iter_next(free_list)) {
if(curr_data->size - size > max ){
largest_block = curr_data;
}
}
ptr = largest_block->data;
dlist_add_back(allocated_list,ptr,size);
largest_block->size = curr_data->size - size;
largest_block->data = curr_data->data + size;
}
return ptr;
}
......
......@@ -25,62 +25,58 @@ int main(int argc, char *argv[]){
printf("\nallocated-list\n");
dlist_print(allocated_list);
// printf("\n--------First Fit--------\n");
// allocate(100,0);
// void *ptr1 = allocate(50,0);
// allocate(200,0);
// printf("free-list\n");
// dlist_print(free_list);
printf("\n--------First Fit--------\n");
printf("\nAllocating 100\n");
allocate(100,0);
printf("free-list\n");
dlist_print(free_list);
// printf("allocated-list\n");
// dlist_print(allocated_list);
//printf("\n------Deallocating node------\n");
// deallocate(ptr1);
// printf("\nfree-list\n");
// dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
// printf("allocated-list\n");
// dlist_print(allocated_list);
printf("\n--------Best Fit--------\n");
printf("\nAllocating 50\n");
allocator_init(1024);
void *ptr1 = allocate(50,0);
allocate(100,1);
void *ptr2 = allocate(50,1);
allocate(200,1);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
//printf("\n------Deallocating node------\n");
deallocate(ptr2);
printf("\nfree-list\n");
printf("\nAllocating 200\n");
allocate(200,0);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
//printf("\n------Deallocating node------\n");
printf("\nDeallocating 50\n");
allocate(40,1);
deallocate(ptr1);
printf("\nfree-list\n");
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\nAllocating 74\n");
allocate(74,0);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
}
/**
* Lindsay Knupp
* 10/26/21
* Lab 7 - memory-test2.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "allocator.h"
#include "dlist.h"
int main(int argc, char *argv[]){
// Initialize memory lists
allocator_init(1024);
printf("Memory-test running\n");
printf("\n-----Initializing lists------\n");
printf("free-list\n");
dlist_print(free_list);
printf("\nallocated-list\n");
dlist_print(allocated_list);
printf("\n--------Best Fit--------\n");
printf("\nAllocating 100\n");
allocate(100,1);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\nAllocating 50\n");
void *ptr1 = allocate(50,1);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\nAllocating 200\n");
allocate(200,1);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
//printf("\n------Deallocating node------\n");
printf("\nDeallocating 50\n");
deallocate(ptr1);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\nAllocating 40\n");
allocate(40,1);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
}
/**
* Lindsay Knupp
* 10/26/21
* Lab 7 - memory-test3.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "allocator.h"
#include "dlist.h"
int main(int argc, char *argv[]){
// Initialize memory lists
allocator_init(1024);
printf("Memory-test running\n");
printf("\n-----Initializing lists------\n");
printf("free-list\n");
dlist_print(free_list);
printf("\nallocated-list\n");
dlist_print(allocated_list);
printf("\n--------Worst Fit--------\n");
printf("\nAllocating 100\n");
allocate(100,2);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\nAllocating 50\n");
void *ptr1 = allocate(50,2);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\nAllocating 200\n");
allocate(200,2);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
//printf("\n------Deallocating node------\n");
printf("\nDeallocating 50\n");
deallocate(ptr1);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\nAllocating 74\n");
allocate(74,2);
printf("free-list\n");
dlist_print(free_list);
printf("allocated-list\n");
dlist_print(allocated_list);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment