Commit d8be8e9c authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

end of working

parent ff561a73
......@@ -22,9 +22,10 @@ int allocator_init(size_t size);
* Allocate a block of memory from the free memory
* list. Node gets placed into allocated list.
* @param size number of bytes to allocate
* @param alloc_type to specify allocation procedure
* @return NULL if any error condition is found
*/
void *allocate(size_t size);
void *allocate(size_t size, int alloc_type);
/**
......
......@@ -130,12 +130,12 @@ bool dlist_iter_has_next(struct dlist *l);
/**
* Initialize iterator for traversing a dlist backwards.
*/
void *dlist_iter_end(struct dlist *l);
struct dnode *dlist_iter_end(struct dlist *l);
/**
* Returns current item and advances iterator to previous item in list.
*/
void *dlist_iter_prev(struct dlist *l);
struct dnode *dlist_iter_prev(struct dlist *l);
/**
* Check if iterator has reached the start of the list.
......
......@@ -16,8 +16,9 @@
int allocator_init(size_t size){
free_list = malloc(size);
struct dnode *first_node = dnode_create();
dnode_setdata(first_node, (void*) free_list, size);
allocated_list = dlist_create();
allocated_list->front = NULL;
allocated_list->back = NULL;
......@@ -27,10 +28,30 @@ int allocator_init(size_t size){
return -1;
}
printf("free-list\n");
dlist_print(free_list);
printf("\nallocated-list\n");
dlist_print(allocated_list);
return 0;
}
//void *allocate(size_t size){
//
//
//}
void *allocate(size_t size, int alloc_type){
struct dnode *n = malloc(size);
struct dnode *curr_data;
// 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);
}
curr_data->size = curr_data->size - size;
curr_data->data = curr_data->data + (void*)size;
}
}
......@@ -206,10 +206,7 @@ dlist_iter_next(struct dlist *l) {
}
}
return ret_p;
}
bool
......@@ -231,30 +228,50 @@ dlist_iter_has_next(struct dlist *l) {
}
void *
struct dnode *
dlist_iter_end(struct dlist *l) {
void *ret_val = NULL;
// 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) {
ret_val = l->iter->data;
if (l->iter != NULL){
ret_p = l->iter;
}
return ret_val;
return ret_p;
}
void *
struct dnode *
dlist_iter_prev(struct dlist *l) {
void *ret_val = NULL;
// void *ret_val = NULL;
if (l->iter != 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){
l->iter = l->iter->prev;
if (l->iter != NULL) {
ret_val = l->iter->data;
if (l->iter != NULL){
ret_p = l->iter;
}
}
return ret_val;
return ret_p;
}
bool
......@@ -270,11 +287,11 @@ 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;
data = dlist_iter_next(l)) {
printf("[%zu,%p]-- ",data->size,data);
......
......@@ -26,8 +26,8 @@
void traverse_forward(struct dlist *l) {
char *str;
printf("forward traversal\n");
for (str = dlist_iter_begin(l); str != NULL;
str = (char *) dlist_iter_next(l)) {
for (str = dlist_iter_begin(l)->data; str != NULL;
str = (char *) dlist_iter_next(l)->data) {
printf("string = %s\n", str);
}
}
......@@ -35,8 +35,8 @@ void traverse_forward(struct dlist *l) {
void traverse_backward(struct dlist *l) {
char *str;
printf("backward traversal\n");
for (str = dlist_iter_end(l); str != NULL;
str = (char *) dlist_iter_prev(l)) {
for (str = dlist_iter_end(l)->data; str != NULL;
str = (char *) dlist_iter_prev(l)->data) {
printf("string = %s\n", str);
}
}
......@@ -70,13 +70,13 @@ int main(int argc, char *argv[]) {
dlist_add_back(mylist, str4);
dlist_add_back(mylist, str5);
traverse_forward(mylist);
traverse_backward(mylist);
//traverse_forward(mylist);
//traverse_backward(mylist);
dlist_destroy(mylist);
printf("\nlist destroyed\n");
traverse_forward(mylist);
traverse_backward(mylist);
//traverse_forward(mylist);
//traverse_backward(mylist);
printf("\ntraversal of empty list completed\n");
str1 = malloc(20);
......@@ -101,31 +101,31 @@ int main(int argc, char *argv[]) {
dlist_add_front(mylist, str4);
dlist_add_front(mylist, str5);
traverse_forward(mylist);
traverse_backward(mylist);
//traverse_forward(mylist);
//traverse_backward(mylist);
printf("\ntesting remove front\n");
mystr = dlist_remove_front(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
printf("list length= %d\n\n", dlist_num_elems(mylist));
traverse_forward(mylist);
traverse_backward(mylist);
//traverse_forward(mylist);
//traverse_backward(mylist);
printf("\ntesting remove back\n");
mystr = dlist_remove_back(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
printf("list length= %d\n\n", dlist_num_elems(mylist));
traverse_forward(mylist);
traverse_backward(mylist);
//traverse_forward(mylist);
//traverse_backward(mylist);
printf("\ntesting find_remove\n");
mystr = dlist_find_remove(mylist, str3);
printf("removed string = %s\n", mystr);
free(mystr);
printf("contents of the list\n");
traverse_forward(mylist);
traverse_backward(mylist);
//traverse_forward(mylist);
//traverse_backward(mylist);
}
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