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

init lists working

parent d8be8e9c
......@@ -61,8 +61,9 @@ void dlist_obliterate(struct dlist *l);
*
* @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_front(struct dlist *l, void *ptr);
void dlist_add_front(struct dlist *l, void *ptr, size_t size);
/**
* Inserts new node in dlist after the last node.
......
......@@ -61,4 +61,6 @@ void *dnode_destroy(struct dnode *n);
*/
void dnode_obliterate(struct dnode *n);
void dnode_print(struct dnode *n);
#endif /* _DNODE_H_ */
......@@ -76,11 +76,11 @@ int main(int argc, char *argv[]) {
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);
dlist_add_front(mylist, str1,strlen(str1));
dlist_add_front(mylist, str2,strlen(str2));
dlist_add_front(mylist, str3,strlen(str3));
dlist_add_front(mylist, str4,strlen(str4));
dlist_add_front(mylist, str5,strlen(str5));
dlist_print(mylist);
......
......@@ -14,11 +14,13 @@
int allocator_init(size_t size){
free_list = malloc(size);
struct dnode *first_node = dnode_create();
dnode_setdata(first_node, (void*) free_list, size);
// Initalize free list
void *free_list_p = malloc(size);
free_list = dlist_create();
dlist_add_front(free_list,free_list_p,size);
// Initalize allocated list
allocated_list = dlist_create();
allocated_list->front = NULL;
allocated_list->back = NULL;
......@@ -28,12 +30,6 @@ 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;
}
......@@ -41,6 +37,8 @@ void *allocate(size_t size, int alloc_type){
struct dnode *n = malloc(size);
struct dnode *curr_data;
void *size_p = (void *) size;
// First-fit policy
if (alloc_type == 0){
......@@ -49,9 +47,9 @@ void *allocate(size_t size, int alloc_type){
curr_data = dlist_iter_next(free_list);
}
curr_data->size = curr_data->size - size;
curr_data->data = curr_data->data + (void*)size;
curr_data->data = curr_data->data + size_p;
}
}
//}
......@@ -51,9 +51,10 @@ dlist_obliterate(struct dlist *l) {
}
void
dlist_add_front(struct dlist *l, void *ptr) {
dlist_add_front(struct dlist *l, void *ptr, size_t size) {
struct dnode *n = dnode_create();
n->data = ptr;
n->data = ptr;
n->size = size;
if (0 == l->counter) {
l->front = l->back = n;
......
......@@ -95,11 +95,11 @@ int main(int argc, char *argv[]) {
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);
dlist_add_front(mylist, str1,strlen(str1));
dlist_add_front(mylist, str2,strlen(str2));
dlist_add_front(mylist, str3,strlen(str3));
dlist_add_front(mylist, str4,strlen(str4));
dlist_add_front(mylist, str5,strlen(str5));
//traverse_forward(mylist);
//traverse_backward(mylist);
......
......@@ -18,6 +18,7 @@
*/
#include <stdlib.h>
#include <stdio.h>
#include "dnode.h"
struct dnode *dnode_create() {
......@@ -38,4 +39,10 @@ void *dnode_destroy(struct dnode *n) {
void dnode_obliterate(struct dnode *n) {
free(n->data);
free(n);
}
void dnode_print(struct dnode *n){
printf("data = %p\n",n->data);
printf("\nsize = %zu\n",n->size);
}
......@@ -9,10 +9,19 @@
#include <string.h>
#include "allocator.h"
#include "dlist.h"
int main(int argc, char *argv[]){
// Initialize memory lists
allocator_init(1024);
printf("Memory-test running\n");
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