Commit 883b1aab authored by cdf009's avatar cdf009

IN TEARS THAT I GOT THIS TO WORK

parent 6a53895e
......@@ -14,8 +14,7 @@ int allocator_init(int size);
//this is where we will create and initialize 2 doubly linked lists
//one is called free_list and one is allocated_list
void *allocate(int size, int param);
struct dnode* allocate(int size, int param);
//this is basically malloc
//will allocate a chunk of memory of a size on the free list
......@@ -23,12 +22,12 @@ void *allocate(int size, int param);
int deallocate(void *ptr);
//this will free memory on the allocated list
int first_fit(int size);
struct dnode* first_fit(int size);
int best_fit(int size);
struct dnode* best_fit(int size);
int worst_fit(int size);
struct dnode* worst_fit(int size);
void allocator_print();
#endif /* _DLIST_H_ */
#endif /* _DLIST_H_ */
......@@ -71,7 +71,7 @@ void dlist_add_front(struct dlist *l, void *ptr, int size);
* @param l pointer to the list (non-NULL)
* @param ptr pointer to generic data to store in new list node
*/
void dlist_add_back(struct dlist *l, void *ptr, int size);
struct dnode* dlist_add_back(struct dlist *l, void *ptr, int size);
/**
* Remove the first node and return pointer to the data. It is the
......
No preview for this file type
No preview for this file type
......@@ -7,9 +7,12 @@
#include "dlist.h"
//struct dnode *dnode_curr;
struct dlist *free_list;
struct dlist *allocated_list;
struct dlist* free_list;
struct dlist* allocated_list;
void* data1;
void* datafirst;
void* databest;
void* dataworst;
int allocator_init(int size){
//void *data;
......@@ -36,23 +39,23 @@ int deallocate(void *ptr){
struct dnode* dealloc;
dealloc = dlist_find_remove(allocated_list, ptr);
printf("%x deallocating node %x, %d", ptr, dealloc->data, dealloc->size);
dlist_add_back(free_list, dealloc->data, dealloc->size);
return 0;
}
void allocator_print() {
// Create iterator to traverse list
struct dnode *iter = dlist_iter_begin(free_list);
struct dnode* iter = dlist_iter_begin(free_list);
printf("\n------------------------\n");
/* Print free list */
printf("Free List: |\n");
do{
printf("count: %d\n", free_list->counter);
printf("Pointer: %x, Size: %d ", iter->data, iter->size);
iter = dlist_iter_next(free_list);
printf("count: %d\n", allocated_list->counter);
}
while(iter);
if (dlist_num_elems(allocated_list) > 0) {
......@@ -61,8 +64,7 @@ void allocator_print() {
printf("\nAllocated List:\n");
do{
//int* demo = (int*) iter;
printf("count: %d\n", allocated_list->counter);
printf("Pointer: %x, Size: %d ", iter->data, iter->size);
printf("Pointer: %x, Size: %d\n", iter->data, iter->size);
iter = dlist_iter_next(allocated_list);
}
while(iter);
......@@ -73,13 +75,13 @@ void allocator_print() {
return;
}
int first_fit(int size){
struct dnode* first_fit(int size){
//this is where it will check and see if it fits
struct dnode *current = dlist_iter_begin(free_list);
struct dnode* current = dlist_iter_begin(free_list);
do{
if (size <= current->size){
dlist_add_back(allocated_list, current->data, size);
datafirst = dlist_add_back(allocated_list, current->data, size);
current->size -= size;
current->data += size;
if (0 == current->size){
......@@ -89,22 +91,23 @@ int first_fit(int size){
}
break;
}
if (free_list->back == current){
printf("No space in list");
return -1;
}
if (free_list->back == current){
printf("No space in list");
return -1;
}
current = dlist_iter_next(free_list);
}
while(current);
return 0;
return datafirst;
}
int best_fit(int size){
struct dnode *iter = dlist_iter_begin(free_list);
struct dnode *minimum;
struct dnode* best_fit(int size){
struct dnode* iter = dlist_iter_begin(free_list);
struct dnode* minimum;
int min_size = 0;
do{
......@@ -120,7 +123,7 @@ int best_fit(int size){
if (min_size == 0){
return -1;
}
dlist_add_back(allocated_list, minimum->data, size);
databest = dlist_add_back(allocated_list, minimum->data, size);
minimum->size -= size;
minimum->data += size;
......@@ -129,12 +132,12 @@ int best_fit(int size){
dlist_find_remove(free_list, minimum);
}
}
return 0;
return databest;
}
int worst_fit(int size){
struct dnode *iter = dlist_iter_begin(free_list);
struct dnode *maximum;
struct dnode* worst_fit(int size){
struct dnode* iter = dlist_iter_begin(free_list);
struct dnode* maximum;
int max_size = 0;
do{
......@@ -150,7 +153,7 @@ int worst_fit(int size){
if (max_size == 0){
return -1;
}
dlist_add_back(allocated_list, maximum->data, size);
dataworst = dlist_add_back(allocated_list, maximum->data, size);
maximum->size -= size;
maximum->data += size;
......@@ -159,19 +162,25 @@ int worst_fit(int size){
dlist_find_remove(free_list, maximum);
}
}
return 0;
return dataworst;
}
int allocate(int size, int param){
struct dnode* allocate(int size, int param){
struct dnode* x = NULL;
if (param == 0){
first_fit(size);
x = first_fit(size);
return x;
}
if (param == 1){
best_fit(size);
x = best_fit(size);
return x;
}
if (param == 2){
worst_fit(size);
x = worst_fit(size);
return x;
}
return 0;
}
......@@ -28,7 +28,7 @@ dlist_create() {
return calloc(1, sizeof(struct dlist));
}
void
void
dlist_destroy(struct dlist *l) {
struct dnode *p = l->front;
......@@ -44,20 +44,20 @@ dlist_destroy(struct dlist *l) {
}
void
void
dlist_obliterate(struct dlist *l) {
dlist_destroy(l);
free(l);
}
void
void
dlist_add_front(struct dlist *l, void *ptr, int size) {
struct dnode *n = dnode_create();
n->data = ptr;
n->size = size;
n->size = size;
if (0 == l->counter) {
l->front = l->back = n;
l->front = l->back = n;
l->counter = 1;
} else {
n->next = l->front;
......@@ -73,14 +73,14 @@ dlist_add_front(struct dlist *l, void *ptr, int size) {
#endif /* DEBUG */
}
void
struct dnode*
dlist_add_back(struct dlist *l, void *ptr, int size) {
struct dnode *n = dnode_create();
n->data = ptr;
n->size = size;
if (0 == l->counter) {
l->front = l->back = n;
l->front = l->back = n;
l->counter = 1;
} else {
n->prev = l->back;
......@@ -94,11 +94,14 @@ dlist_add_back(struct dlist *l, void *ptr, int size) {
printf("front= %s\n", (char *) l->front->data);
printf("back= %s\n\n", (char *) l->back->data);
#endif /* DEBUG */
return n;
}
struct dnode* dlist_remove_front(struct dlist *l) {
struct dnode *n = l->front;
void* ptr = n->data;
void* ptr = dnode_create();
dnode_setdata(ptr, n->data, n->size);
if (1 == l->counter) {
l->front = l->back = NULL;
......@@ -112,10 +115,11 @@ struct dnode* dlist_remove_front(struct dlist *l) {
return ptr;
}
struct dnode*
struct dnode*
dlist_remove_back(struct dlist *l) {
struct dnode *n = l->back;
void *ptr = n->data;
void *ptr = dnode_create();
dnode_setdata(ptr, n->data, n->size);
if (1 == l->counter) {
l->front = l->back = NULL;
......@@ -129,16 +133,17 @@ dlist_remove_back(struct dlist *l) {
return ptr;
}
struct dnode*
struct dnode*
dlist_find_remove(struct dlist *l, void *ptr) {
struct dnode *n = l->front;
void *ret_ptr = dnode_create();
struct dnode *ret_ptr = dnode_create();
while ((n != NULL) && (n->data != ptr)) {
n = n->next;
}
if (n != NULL) {
dnode_setdata(ret_ptr, n->data, n->size);
if (l->front == n) {
return dlist_remove_front(l);
} else if (l->back == n) {
......@@ -152,15 +157,15 @@ dlist_find_remove(struct dlist *l, void *ptr) {
}
(l->counter)--;
}
//ret_ptr = n->data;
free(n);
}
return ret_ptr;
return ret_ptr;
}
uint32_t
uint32_t
dlist_num_elems(struct dlist *l) {
return l->counter;
}
......@@ -171,7 +176,7 @@ dlist_iter_begin(struct dlist *l) {
l->iter = l->front;
if (l->iter != NULL) {
ret_val = l->iter->data;
ret_val = l->iter;
}
return ret_val;
......@@ -184,14 +189,14 @@ dlist_iter_next(struct dlist *l) {
if (l->iter != NULL) {
l->iter = l->iter->next;
if (l->iter != NULL) {
ret_val = l->iter->data;
ret_val = l->iter;
}
}
return ret_val;
}
bool
bool
dlist_iter_has_next(struct dlist *l) {
bool ret_val = false;
......@@ -236,7 +241,7 @@ dlist_iter_prev(struct dlist *l) {
return ret_val;
}
bool
bool
dlist_iter_has_prev(struct dlist *l) {
bool ret_val = false;
......
......@@ -20,14 +20,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dnode.h"
#include "dlist.h"
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)) {
str = (char *) dlist_iter_next(l)) {
printf("string = %s\n", str);
}
}
......@@ -36,7 +36,7 @@ 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)) {
str = (char *) dlist_iter_prev(l)) {
printf("string = %s\n", str);
}
}
......@@ -45,81 +45,81 @@ int main(int argc, char *argv[]) {
char *mystr;
struct dlist *mylist = dlist_create();
printf("dlisttest running...\n");
char *str1 = malloc(20);
strncpy(str1, "animal", 20);
char *str2 = malloc(20);
strncpy(str2, "barnacle", 20);
char *str3 = malloc(20);
strncpy(str3, "carnage", 20);
char *str4 = malloc(20);
strncpy(str4, "demented", 20);
char *str5 = malloc(20);
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);
traverse_forward(mylist);
traverse_backward(mylist);
traverse_backward(mylist);
dlist_destroy(mylist);
printf("\nlist destroyed\n");
traverse_forward(mylist);
traverse_backward(mylist);
traverse_backward(mylist);
printf("\ntraversal of empty list completed\n");
str1 = malloc(20);
strncpy(str1, "folly", 20);
str2 = malloc(20);
strncpy(str2, "gunk", 20);
str3 = malloc(20);
strncpy(str3, "hospital", 20);
str4 = malloc(20);
strncpy(str4, "ignorant", 20);
str5 = malloc(20);
strncpy(str5, "jelly", 20);
printf("\ntesting dlist_add_front\n");
dlist_add_front(mylist, str1);
dlist_add_front(mylist, str2);
dlist_add_front(mylist, str3);
dlist_add_front(mylist, str4);
dlist_add_front(mylist, str5);
traverse_forward(mylist);
traverse_backward(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_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_backward(mylist);
printf("\ntesting find_remove\n");
mystr = dlist_find_remove(mylist, str3);
printf("removed string = %s\n", mystr);
......@@ -128,4 +128,3 @@ int main(int argc, char *argv[]) {
traverse_forward(mylist);
traverse_backward(mylist);
}
......@@ -38,4 +38,4 @@ void *dnode_destroy(struct dnode *n) {
void dnode_obliterate(struct dnode *n) {
free(n->data);
free(n);
}
}
......@@ -7,8 +7,8 @@
struct dnode* opt1;
struct dnode* opt2;
struct dnode* opt1;
struct dnode* opt2;
struct dnode* opt3;
......@@ -20,59 +20,63 @@ int main(){
printf("\n");
printf( "First Fit: ");
printf("\n");
printf("Allocating 300");
printf("Allocating 400");
printf("\n");
allocate(400, 1);
options = allocate(400, 0);
allocator_print();
printf("\n");
printf("Allocating 300");
printf("\n");
options = allocate(300, 1);
opt1 = allocate(300, 0);
allocator_print();
printf("\n");
printf("D-Allocating 300");
printf("\n");
deallocate(options->data);
deallocate(opt1->data);
allocator_print();
printf("\n");
printf("Allocating 800");
printf("\n");
allocate(800, 1);
allocate(800, 0);
allocator_print();
printf("No change because there is not enough memory");
printf("\n");
printf("\n");
allocator_init(1000);
printf("\n");
printf( "Best Fit: ");
printf("\n");
printf("Allocating 300");
printf("Allocating 500");
printf("\n");
allocate(400, 2);
opt1 = allocate(500, 1);
allocator_print();
printf("\n");
printf("Allocating 300");
printf("Allocating 200");
printf("\n");
allocate(300, 2);
opt2 = allocate(200, 1);
allocator_print();
printf("\n");
printf("D-Allocating 300");
printf("D-Allocating 200");
printf("\n");
deallocate(options->data);
deallocate(opt2->data);
allocator_print();
printf("\n");
printf("Allocating 800");
printf("Allocating 900");
printf("\n");
allocate(800, 2);
allocate(800, 1);
allocator_print();
printf("No change because there is not enough memory");
printf("\n");
printf("\n");
allocator_init(1000);
......@@ -81,27 +85,30 @@ int main(){
printf("\n");
printf("Allocating 300");
printf("\n");
allocate(400, 3);
opt1 = allocate(300, 2);
allocator_print();
printf("\n");
printf("Allocating 300");
printf("Allocating 100");
printf("\n");
allocate(300, 3);
opt2 = allocate(100, 2);
allocator_print();
printf("\n");
printf("D-Allocating 300");
printf("D-Allocating 100");
printf("\n");
deallocate(options->data);
deallocate(opt2->data);
allocator_print();
printf("\n");
printf("Allocating 800");
printf("\n");
allocate(800, 3);
allocate(800, 2);
allocator_print();
printf("No change because there is not enough memory");
printf("\n");
printf("\n");
return 0;
}
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