Commit 0147c701 authored by Lindsay Knupp's avatar Lindsay Knupp
Browse files

deallocate works

parent 4a2d67d7
......@@ -35,6 +35,7 @@ int allocator_init(size_t size){
void *allocate(size_t size, int alloc_type){
struct dnode *curr_data;
void *ptr;
// First-fit policy
if (alloc_type == 0){
......@@ -42,28 +43,37 @@ void *allocate(size_t size, int alloc_type){
while (curr_data->size < size){
curr_data = dlist_iter_next(free_list);
}
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;
}
return 0;
}
int deallocate(void *ptr){
struct dnode *removed_data;
// Best-fit policy
if (alloc_type == 1){
curr_data = dlist_iter_begin(free_list);
diff = (curr_data->size) - size;
if (diff < 0){return -1;}
while(curr_data != NULL){
// curr_data = dlist_iter_begin(allocated_list);
// while (curr_data->data != ptr){
// curr_data = dlist_iter_next(allocated_list);
// }
}
}
return ptr;
}
int deallocate(void *ptr){
struct dnode *removed_data;
removed_data = dlist_find_remove(allocated_list, ptr);
//dnode_print(removed_data);
// dnode_print(removed_data);
dlist_add_back(free_list,removed_data->data,removed_data->size);
//dlist_add_back(free_list,removed_data->data,removed_data->size);
return 0;
}
......
......@@ -137,12 +137,16 @@ 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)) {
n = n->next;
//printf("n->data %p\n",n->data);
n = n->next;
}
if (n != NULL) {
......
......@@ -18,21 +18,17 @@ int main(int argc, char *argv[]){
allocator_init(1024);
printf("Memory-test running\n");
printf("-----Initializing lists------\n");
printf("\n-----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("\n------Allocating free list------\n");
void *ptr1 = allocate(100,0);
void *ptr2 = allocate(50,0);
void *ptr3 = allocate(200,0);
printf("free-list\n");
dlist_print(free_list);
......@@ -40,14 +36,12 @@ int main(int argc, char *argv[]){
printf("\nallocated-list\n");
dlist_print(allocated_list);
printf("------Deallocating node------\n");
deallocate(num);
printf("\n------Deallocating node------\n");
deallocate(ptr2);
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