Commit 5a83ede0 authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

Lab 7 completed

parent f26e0a2a
......@@ -35,55 +35,76 @@ int allocator_init(size_t size){
void *allocate(size_t size, int alloc_type){
struct dnode *curr_data;
void *ptr;
void *ptr = NULL;
int counter = 0;
// First-fit policy
if (alloc_type == 0){
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){
counter ++;
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;
}
}
if ((counter == dlist_num_elems(free_list)) && (ptr == NULL)) {
printf("No space to allocate this\n");
return NULL;
}
}
}
// Best-fit policy
if (alloc_type == 1){
curr_data = dlist_iter_begin(free_list);
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 (alloc_type == 1){
if((curr_data->size - size < min) && (curr_data->size > size) ){
size_t min = RAND_MAX;
struct dnode *smallest_block = NULL;
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) ){
smallest_block = curr_data;
min = curr_data->size - size;
}
}
if (min == RAND_MAX) {
printf("No space to allocate this\n");
return NULL;
}
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;
smallest_block->size = smallest_block->size - size;
smallest_block->data = smallest_block->data + size;
}
// Worst-fit policy
if (alloc_type == 2){
curr_data = dlist_iter_begin(free_list);
size_t max = 0;
struct dnode *largest_block;
struct dnode *largest_block = NULL;
for (curr_data = dlist_iter_begin(free_list); curr_data != NULL; curr_data = dlist_iter_next(free_list)) {
if((curr_data->size - size > max) && (curr_data->size > size) ){
largest_block = curr_data;
max = curr_data->size - size;
}
}
if (max == 0) {
printf("No space to allocate this\n");
return NULL;
}
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;
largest_block->size = largest_block->size - size;
largest_block->data = largest_block->data + size;
}
return ptr;
......@@ -94,7 +115,7 @@ int deallocate(void *ptr){
removed_data = dlist_find_remove(allocated_list, ptr);
dlist_add_back(free_list,removed_data->data,removed_data->size);
free(removed_data);
return 0;
}
......
......@@ -99,8 +99,6 @@ dlist_add_back(struct dlist *l, void *ptr,size_t size) {
struct dnode *
dlist_remove_front(struct dlist *l) {
struct dnode *n = l->front;
// void* ptr = n->data;
struct dnode* ptr = n;
if (1 == l->counter) {
......@@ -118,8 +116,6 @@ dlist_remove_front(struct dlist *l) {
struct dnode *
dlist_remove_back(struct dlist *l) {
struct dnode *n = l->back;
//void *ptr = n->data;
struct dnode *ptr = n;
if (1 == l->counter) {
......@@ -137,15 +133,9 @@ dlist_remove_back(struct dlist *l) {
struct dnode *
dlist_find_remove(struct dlist *l, void *ptr) {
struct dnode *n = l->front;
//void *ret_ptr = NULL;
//printf("pointer value %p\n",ptr);
struct dnode *ret_p = NULL;
while ((n != NULL) && (n->data != ptr)) {
//printf("n->data %p\n",n->data);
n = n->next;
}
......@@ -165,10 +155,8 @@ dlist_find_remove(struct dlist *l, void *ptr) {
}
ret_p = n;
//ret_ptr = n->data;
free(n);
}
return ret_p;
}
......@@ -179,15 +167,6 @@ dlist_num_elems(struct dlist *l) {
struct dnode *
dlist_iter_begin(struct dlist *l) {
// void *ret_val = NULL;
//
// l->iter = l->front;
// if (l->iter != NULL) {
// ret_val = l->iter->data;
// }
// return ret_val;
struct dnode *ret_p = NULL;
l->iter = l->front;
if(l->iter != NULL){
......@@ -199,17 +178,6 @@ dlist_iter_begin(struct dlist *l) {
struct dnode *
dlist_iter_next(struct dlist *l) {
// void *ret_val = NULL;
// if (l->iter != NULL) {
// l->iter = l->iter->next;
// if (l->iter != NULL) {
// ret_val = l->iter->data;
// }
// }
// return ret_val;
struct dnode *ret_p = NULL;
if(l->iter != NULL) {
......@@ -243,15 +211,6 @@ dlist_iter_has_next(struct dlist *l) {
struct dnode *
dlist_iter_end(struct dlist *l) {
// void *ret_val = NULL;
// l->iter = l->back;
// if (l->iter != NULL) {
// ret_val = l->iter->data;
// }
//
// return ret_val;
struct dnode *ret_p = NULL;
l->iter = l->back;
if (l->iter != NULL){
......@@ -264,17 +223,6 @@ dlist_iter_end(struct dlist *l) {
struct dnode *
dlist_iter_prev(struct dlist *l) {
// void *ret_val = NULL;
// if (l->iter != NULL) {
// l->iter = l->iter->prev;
// if (l->iter != NULL) {
// ret_val = l->iter->data;
// }
// }
// return ret_val;
struct dnode *ret_p = NULL;
if (l->iter != NULL){
......@@ -300,9 +248,6 @@ dlist_iter_has_prev(struct dlist *l) {
void dlist_print(struct dlist *l){
printf("dlist: -");
// char *data;
struct dnode *data;
for (data = dlist_iter_begin(l); data != NULL;
......
......@@ -43,7 +43,6 @@ void traverse_backward(struct dlist *l) {
int main(int argc, char *argv[]) {
// char *mystr;
struct dlist *mylist = dlist_create();
printf("dlisttest running...\n");
......
......@@ -46,7 +46,6 @@ int main(int argc, char *argv[]){
dlist_print(allocated_list);
printf("\nAllocating 200\n");
allocate(200,1);
......@@ -57,7 +56,6 @@ int main(int argc, char *argv[]){
printf("allocated-list\n");
dlist_print(allocated_list);
//printf("\n------Deallocating node------\n");
printf("\nDeallocating 50\n");
deallocate(ptr1);
......
......@@ -56,8 +56,7 @@ int main(int argc, char *argv[]){
printf("allocated-list\n");
dlist_print(allocated_list);
//printf("\n------Deallocating node------\n");
printf("\nDeallocating 50\n");
deallocate(ptr1);
......
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