Commit 4a2d67d7 authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

Allocator working

parent 2ad302ea
......@@ -70,8 +70,9 @@ void dlist_add_front(struct dlist *l, void *ptr, size_t size);
*
* @param l pointer to the list (non-NULL)
* @param ptr pointer to generic data to store in new list node
* @param size of data
*/
void dlist_add_back(struct dlist *l, void *ptr);
void dlist_add_back(struct dlist *l, void *ptr, size_t size);
/**
* Remove the first node and return pointer to the data. It is the
......@@ -81,7 +82,7 @@ void dlist_add_back(struct dlist *l, void *ptr);
* @param l pointer to the list (non-NULL)
* @return pointer to data stored in front node
*/
void *dlist_remove_front(struct dlist *l);
struct dnode *dlist_remove_front(struct dlist *l);
/**
* Remove the last node and return pointer to the data. It is the
......@@ -91,7 +92,7 @@ void *dlist_remove_front(struct dlist *l);
* @param l pointer to the list (non-NULL)
* @return pointer to data stored in back node
*/
void *dlist_remove_back(struct dlist *l);
struct dnode *dlist_remove_back(struct dlist *l);
/**
* Seek out the node that contains a pointer and remove from list returning
......@@ -104,7 +105,7 @@ void *dlist_remove_back(struct dlist *l);
* @return pointer to data stored in the node that was found node
*/
void *dlist_find_remove(struct dlist *l, void *ptr);
struct dnode *dlist_find_remove(struct dlist *l, void *ptr);
/**
* Returns the number of elements in the list (nodes).
......
......@@ -45,11 +45,11 @@ int main(int argc, char *argv[]) {
strncpy(str5, "error", 20);
printf("\ntesting dlist_add_back\n");
dlist_add_back(mylist, str1);
dlist_add_back(mylist, str2);
dlist_add_back(mylist, str3);
dlist_add_back(mylist, str4);
dlist_add_back(mylist, str5);
dlist_add_back(mylist, str1,strlen(str1));
dlist_add_back(mylist, str2,strlen(str2));
dlist_add_back(mylist, str3,strlen(str3));
dlist_add_back(mylist, str4,strlen(str4));
dlist_add_back(mylist, str5,strlen(str5));
dlist_print(mylist);
......@@ -85,26 +85,26 @@ int main(int argc, char *argv[]) {
dlist_print(mylist);
printf("\ntesting remove front\n");
mystr = dlist_remove_front(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
// mystr = dlist_remove_front(mylist);
// printf("removed string = %s\n", mystr);
// free(mystr);
printf("list length= %d\n\n", dlist_num_elems(mylist));
dlist_print(mylist);
printf("\ntesting remove back\n");
mystr = dlist_remove_back(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
// mystr = dlist_remove_back(mylist);
// printf("removed string = %s\n", mystr);
// free(mystr);
printf("list length= %d\n\n", dlist_num_elems(mylist));
dlist_print(mylist);
printf("\ntesting find_remove\n");
mystr = dlist_find_remove(mylist, str3);
printf("removed string = %s\n", mystr);
free(mystr);
// mystr = dlist_find_remove(mylist, str3);
// printf("removed string = %s\n", mystr);
// free(mystr);
printf("contents of the list\n");
dlist_print(mylist);
......
......@@ -34,22 +34,37 @@ int allocator_init(size_t size){
}
void *allocate(size_t size, int alloc_type){
struct dnode *n = malloc(size);
struct dnode *curr_data;
struct dnode *curr_data;
void *size_p = (void *) size;
// 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);
}
dlist_add_back(allocated_list,curr_data->data,size);
curr_data->size = curr_data->size - size;
curr_data->data = curr_data->data + size_p;
}
curr_data->data = curr_data->data + size;
}
return 0;
}
int deallocate(void *ptr){
struct dnode *removed_data;
// curr_data = dlist_iter_begin(allocated_list);
// while (curr_data->data != ptr){
// curr_data = dlist_iter_next(allocated_list);
// }
removed_data = dlist_find_remove(allocated_list, ptr);
//dnode_print(removed_data);
//dlist_add_back(free_list,removed_data->data,removed_data->size);
return 0;
}
//}
......@@ -74,9 +74,10 @@ dlist_add_front(struct dlist *l, void *ptr, size_t size) {
}
void
dlist_add_back(struct dlist *l, void *ptr) {
dlist_add_back(struct dlist *l, void *ptr,size_t size) {
struct dnode *n = dnode_create();
n->data = ptr;
n->size = size;
if (0 == l->counter) {
l->front = l->back = n;
......@@ -95,10 +96,12 @@ dlist_add_back(struct dlist *l, void *ptr) {
#endif /* DEBUG */
}
void *
struct dnode *
dlist_remove_front(struct dlist *l) {
struct dnode *n = l->front;
void* ptr = n->data;
// void* ptr = n->data;
struct dnode* ptr = n;
if (1 == l->counter) {
l->front = l->back = NULL;
......@@ -112,10 +115,12 @@ dlist_remove_front(struct dlist *l) {
return ptr;
}
void *
struct dnode *
dlist_remove_back(struct dlist *l) {
struct dnode *n = l->back;
void *ptr = n->data;
//void *ptr = n->data;
struct dnode *ptr = n;
if (1 == l->counter) {
l->front = l->back = NULL;
......@@ -129,10 +134,12 @@ dlist_remove_back(struct dlist *l) {
return ptr;
}
void *
struct dnode *
dlist_find_remove(struct dlist *l, void *ptr) {
struct dnode *n = l->front;
void *ret_ptr = NULL;
//void *ret_ptr = NULL;
struct dnode *ret_p = NULL;
while ((n != NULL) && (n->data != ptr)) {
n = n->next;
......@@ -153,11 +160,12 @@ dlist_find_remove(struct dlist *l, void *ptr) {
(l->counter)--;
}
ret_ptr = n->data;
ret_p = n;
//ret_ptr = n->data;
free(n);
}
return ret_ptr;
return ret_p;
}
uint32_t
......@@ -295,7 +303,7 @@ void dlist_print(struct dlist *l){
for (data = dlist_iter_begin(l); data != NULL;
data = dlist_iter_next(l)) {
printf("[%zu,%p]-- ",data->size,data);
printf("[%zu,%p]-- ",data->size,data->data);
}
printf("\n");
......
......@@ -64,11 +64,11 @@ int main(int argc, char *argv[]) {
strncpy(str5, "error", 20);
printf("\ntesting dlist_add_back\n");
dlist_add_back(mylist, str1);
dlist_add_back(mylist, str2);
dlist_add_back(mylist, str3);
dlist_add_back(mylist, str4);
dlist_add_back(mylist, str5);
dlist_add_back(mylist, str1,strlen(str1));
dlist_add_back(mylist, str2,strlen(str2));
dlist_add_back(mylist, str3,strlen(str3));
dlist_add_back(mylist, str4,strlen(str4));
dlist_add_back(mylist, str5,strlen(str5));
//traverse_forward(mylist);
//traverse_backward(mylist);
......@@ -105,25 +105,25 @@ int main(int argc, char *argv[]) {
//traverse_backward(mylist);
printf("\ntesting remove front\n");
mystr = dlist_remove_front(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
// 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);
printf("\ntesting remove back\n");
mystr = dlist_remove_back(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
// 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);
printf("\ntesting find_remove\n");
mystr = dlist_find_remove(mylist, str3);
printf("removed string = %s\n", mystr);
free(mystr);
// 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);
......
......@@ -17,11 +17,37 @@ int main(int argc, char *argv[]){
// Initialize memory lists
allocator_init(1024);
printf("Memory-test running\n");
printf("-----Initializing lists------\n");
printf("free-list\n");
dlist_print(free_list);
printf("\nallocated-list\n");
dlist_print(allocated_list);
printf("------Allocating free list------\n");
int x;
int *num = &x;
*num = 50;
allocate(100,0);
allocate(*num,0);
allocate(200,0);
printf("free-list\n");
dlist_print(free_list);
printf("\nallocated-list\n");
dlist_print(allocated_list);
printf("------Deallocating node------\n");
deallocate(num);
printf("free-list\n");
dlist_print(free_list);
printf("\nallocated-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