Commit 2a9fade6 authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

end of lab

parent f87273b9
......@@ -56,6 +56,7 @@ void dlist_destroy(struct dlist *l);
*/
void dlist_obliterate(struct dlist *l);
/**
* Inserts new node in dlist before the first node.
*
......
......@@ -24,7 +24,7 @@
#include "dlist.h"
int main(int argc, char *argv[]) {
char *mystr;
// char *mystr;
struct dlist *mylist = dlist_create();
printf("dlisttest running...\n");
......
......@@ -43,7 +43,7 @@ void *allocate(size_t size, int alloc_type){
while (curr_data->size < size){
curr_data = dlist_iter_next(free_list);
}
ptr = curr_data->data;
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;
......@@ -52,13 +52,31 @@ void *allocate(size_t size, int alloc_type){
// Best-fit policy
if (alloc_type == 1){
curr_data = dlist_iter_begin(free_list);
size_t diff = (curr_data->size) - size;
size_t min = curr_data->size - size;
struct dnode *smallest_block;
while(curr_data != 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)){
// min = curr_data->size - size;
// smallest_block = curr_data;
// }
//}
//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;
}
return ptr;
......@@ -68,12 +86,9 @@ int deallocate(void *ptr){
struct dnode *removed_data;
removed_data = dlist_find_remove(allocated_list, ptr);
// dnode_print(removed_data);
dlist_add_back(free_list,removed_data->data,removed_data->size);
free(removed_data);
return 0;
}
......@@ -43,7 +43,7 @@ void traverse_backward(struct dlist *l) {
int main(int argc, char *argv[]) {
char *mystr;
// char *mystr;
struct dlist *mylist = dlist_create();
printf("dlisttest running...\n");
......
......@@ -43,6 +43,6 @@ void dnode_obliterate(struct dnode *n) {
void dnode_print(struct dnode *n){
printf("data = %p\n",n->data);
printf("\nsize = %zu\n",n->size);
printf("size = %zu\n",n->size);
}
......@@ -25,23 +25,62 @@ int main(int argc, char *argv[]){
printf("\nallocated-list\n");
dlist_print(allocated_list);
printf("\n------Allocating free list------\n");
void *ptr1 = allocate(100,0);
void *ptr2 = allocate(50,0);
void *ptr3 = allocate(200,0);
// 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("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("\n--------Best Fit--------\n");
allocator_init(1024);
allocate(100,1);
void *ptr2 = allocate(50,1);
allocate(200,1);
printf("free-list\n");
dlist_print(free_list);
printf("\nallocated-list\n");
printf("allocated-list\n");
dlist_print(allocated_list);
printf("\n------Deallocating node------\n");
//printf("\n------Deallocating node------\n");
deallocate(ptr2);
printf("free-list\n");
printf("\nfree-list\n");
dlist_print(free_list);
printf("\nallocated-list\n");
printf("allocated-list\n");
dlist_print(allocated_list);
allocate(40,1);
printf("\nfree-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