Commit 6a53895e authored by cdf009's avatar cdf009

not fully working

parent 24d09662
This diff is collapsed.
CC = gcc -I ./include
CFLAGS = -std=gnu99 -Wall -g #-DDEBUG
INC = ./include
SRC = ./src
OBJ = ./obj
DOC = ./doc
BIN = ./bin
vpath %.h ./include
vpath %.c ./src
EXECS = mem_test
all: $(EXECS)
doc:
doxygen
$(OBJ)/dnode.o: dnode.h dnode.c
$(CC) $(CFLAGS) -c $(SRC)/dnode.c -o $(OBJ)/dnode.o
$(OBJ)/dlist.o: dlist.h dlist.c dnode.h
$(CC) $(CFLAGS) -c $(SRC)/dlist.c -o $(OBJ)/dlist.o
mem_test: $(SRC)/memory-test.c $(SRC)/allocator.c allocator.h $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(SRC)/allocator.c $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/memory-test.c -o $(BIN)/mem_test
.PHONY: clean
clean:
/bin/rm -rf $(BIN)/* $(OBJ)/* core* *~
#ifdef _DLIST_H_
#define _DLIST_H_
#include <stdint.h>
#include <stdbool.h>
#include "dlist.h"
#include "dnode.h"
//Casey Friend
//Lab 7
//allocator.h
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);
//this is basically malloc
//will allocate a chunk of memory of a size on the free list
int deallocate(void *ptr);
//this will free memory on the allocated list
int first_fit(int size);
int best_fit(int size);
int worst_fit(int size);
void allocator_print();
#endif /* _DLIST_H_ */
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#ifndef _DLIST_H_
#define _DLIST_H_
#include <stdint.h>
#include <stdbool.h>
#include "dnode.h"
/**
* Doubly-linked list.
*/
struct dlist {
struct dnode *front;
struct dnode *back;
struct dnode *iter;
uint32_t counter;
};
/**
* Allocates new dlist dynamically.
*
* @return pointer to the list.
*/
struct dlist *dlist_create();
struct dnode* dlist_find(struct dlist *l, void *ptr);
/**
* Deallocates all nodes in a dlist and the data they point to, but
* preserve the list.
*
* @param l pointer to the list (non-NULL)
*/
void dlist_destroy(struct dlist *l);
/**
* Deallocates all nodes in a dlist and the data they point to; deallocate
* the list itself as well. This is the inverse of dlist_create.
* @param l pointer to the list (non-NULL)
*/
void dlist_obliterate(struct dlist *l);
/**
* Inserts new node in dlist before the first node.
*
* @param l pointer to the list (non_NULL)
* @param ptr pointer to generic data to store in new list node
*/
void dlist_add_front(struct dlist *l, void *ptr, int size);
/**
* Inserts new node in dlist after the last node.
*
* @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);
/**
* Remove the first node and return pointer to the data. It is the
* responsibility of the caller to free the memory pointed by the return
* value.
*
* @param l pointer to the list (non-NULL)
* @return pointer to data stored in front node
*/
struct dnode *dlist_remove_front(struct dlist *l);
/**
* Remove the last node and return pointer to the data. It is the
* responsibility of the caller to free the memory pointed by the return
* value.
*
* @param l pointer to the list (non-NULL)
* @return pointer to data stored in back node
*/
struct dnode *dlist_remove_back(struct dlist *l);
/**
* Seek out the node that contains a pointer and remove from list returning
* the pointer to the data. It is the
* responsibility of the caller to free the memory pointed by the return
* value.
*
* @param l pointer to the list (non-NULL)
* @param ptr pointer to the data item been sought
* @return pointer to data stored in the node that was found node
*/
struct dnode *dlist_find_remove(struct dlist *l, void *ptr);
/**
* Returns the number of elements in the list (nodes).
*
* @param l pointer to the list (non-NULL)
*/
uint32_t dlist_num_elems(struct dlist *l);
/**
* Initialize iterator for traversing a dlist forwards.
*/
void *dlist_iter_begin(struct dlist *l);
/**
* Returns current item and advances iterator to next item in list.
*/
void *dlist_iter_next(struct dlist *l);
/**
* Check if iterator has reached the end of the list.
*/
bool dlist_iter_has_next(struct dlist *l);
/**
* Initialize iterator for traversing a dlist backwards.
*/
void *dlist_iter_end(struct dlist *l);
/**
* Returns current item and advances iterator to previous item in list.
*/
void *dlist_iter_prev(struct dlist *l);
/**
* Check if iterator has reached the start of the list.
*/
bool dlist_iter_has_prev(struct dlist *l);
#endif /* _DLIST_H_ */
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#ifndef _DNODE_H_
#define _DNODE_H_
/**
* Node in a doubly-linked list.
*/
struct dnode {
struct dnode *prev;
struct dnode *next;
void *data;
int size;
};
/**
* Allocates a new dnode leaving the pointer to data as null.
*
* @return pointer to a new dnode
*/
struct dnode *dnode_create();
/**
* Sets the pointer to data with the value passed in.
*
* @param n pointer to the dnone
* @param ptr pointer to a generic data type to store in dnode
*/
void dnode_setdata(struct dnode *n, void *ptr, int size);
/**
* Deallocates a dnode but not the data it points to. The caller must take
* the responsibility of deallocating the data later on.
*
* @param n pointer to the dnode
* @return pointer to the generic data type associated with node
*/
void *dnode_destroy(struct dnode *n);
/**
* Deallocates a dnode and also the data it points to.
*
* @param ptr pointer to the dnode
*/
void dnode_obliterate(struct dnode *n);
void print_node(struct dnode *node);
#endif /* _DNODE_H_ */
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#ifndef _READLINE_H_
#define _READLINE_H_
/**
* This function implements a safer way to read strings from a C stream
* (that is, safer than scanf and gets).
*
* The function causes characters to be read one at a time and placed into
* an input buffer that is automatically enlarged to fit the entire string.
* The returned value is a pointer to a dynamically allocated area of
* memory. Note that the caller of this function becomes responsible for
* disposing of the memory allocated for the string read in.
*
* @parm stream Pointer to an open C stream (such as stdin)
* @return A pointer to the line read from source on success or NULL on failure.
*/
extern char*
readline(FILE* stream);
#endif /* _READLINE_H_ */
#include "allocator.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include "dnode.h"
#include "dlist.h"
//struct dnode *dnode_curr;
struct dlist *free_list;
struct dlist *allocated_list;
void* data1;
int allocator_init(int size){
//void *data;
//struct dnode *node;
allocated_list = dlist_create();
free_list = dlist_create();
data1 = malloc(size);
if (data1 == NULL){
return -1;
}
//free_list dlist_create();
dlist_add_front(free_list, data1, size);
//allocated_list = dlist_create();
return 0;
}
int deallocate(void *ptr){
struct dnode* dealloc;
dealloc = dlist_find_remove(allocated_list, ptr);
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);
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);
}
while(iter);
if (dlist_num_elems(allocated_list) > 0) {
iter = dlist_iter_begin(allocated_list);
/* Print allocated list */
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);
iter = dlist_iter_next(allocated_list);
}
while(iter);
}
printf(" |\n");
printf("------------------------\n");
return;
}
int first_fit(int size){
//this is where it will check and see if it fits
struct dnode *current = dlist_iter_begin(free_list);
do{
if (size <= current->size){
dlist_add_back(allocated_list, current->data, size);
current->size -= size;
current->data += size;
if (0 == current->size){
if (dlist_num_elems(free_list) > 1){
dlist_find_remove(free_list, current);
}
}
break;
}
if (free_list->back == current){
printf("No space in list");
return -1;
}
current = dlist_iter_next(free_list);
}
while(current);
return 0;
}
int best_fit(int size){
struct dnode *iter = dlist_iter_begin(free_list);
struct dnode *minimum;
int min_size = 0;
do{
if (size <= iter->size){
if (iter->size <= min_size || min_size == 0){
min_size = iter->size;
minimum = iter;
}
}
iter = dlist_iter_next(free_list);
}
while(iter);
if (min_size == 0){
return -1;
}
dlist_add_back(allocated_list, minimum->data, size);
minimum->size -= size;
minimum->data += size;
if (0 == minimum->size){
if (dlist_num_elems(free_list) > 1){
dlist_find_remove(free_list, minimum);
}
}
return 0;
}
int worst_fit(int size){
struct dnode *iter = dlist_iter_begin(free_list);
struct dnode *maximum;
int max_size = 0;
do{
if (size <= iter->size){
if (iter->size >= max_size || max_size == 0){
max_size = iter->size;
maximum = iter;
}
}
iter = dlist_iter_next(free_list);
}
while (iter);
if (max_size == 0){
return -1;
}
dlist_add_back(allocated_list, maximum->data, size);
maximum->size -= size;
maximum->data += size;
if (0 == maximum->size){
if (dlist_num_elems(free_list) > 1){
dlist_find_remove(free_list, maximum);
}
}
return 0;
}
int allocate(int size, int param){
if (param == 0){
first_fit(size);
}
if (param == 1){
best_fit(size);
}
if (param == 2){
worst_fit(size);
}
return 0;
}
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdlib.h>
#include <stdio.h>
#include "dnode.h"
#include "dlist.h"
struct dlist *
dlist_create() {
return calloc(1, sizeof(struct dlist));
}
void
dlist_destroy(struct dlist *l) {
struct dnode *p = l->front;
do {
l->front = l->front->next;
free(p->data);
free(p);
p = l->front;
} while (l->front != NULL);
l->front = l->back = NULL;
l->counter = 0;
}
void
dlist_obliterate(struct dlist *l) {
dlist_destroy(l);
free(l);
}
void
dlist_add_front(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->counter = 1;
} else {
n->next = l->front;
l->front->prev = n;
l->front = n;
(l->counter)++;
}
#ifdef DEBUG
printf("counter= %d, %s\n", l->counter, (char *) ptr);
printf("front= %s\n", (char *) l->front->data);
printf("back= %s\n\n", (char *) l->back->data);
#endif /* DEBUG */
}
void
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->counter = 1;
} else {
n->prev = l->back;
l->back->next = n;
l->back = n;
(l->counter)++;
}
#ifdef DEBUG
printf("counter= %d, %s\n", l->counter, (char *) ptr);
printf("front= %s\n", (char *) l->front->data);
printf("back= %s\n\n", (char *) l->back->data);
#endif /* DEBUG */
}
struct dnode* dlist_remove_front(struct dlist *l) {
struct dnode *n = l->front;
void* ptr = n->data;
if (1 == l->counter) {
l->front = l->back = NULL;
} else {
l->front = l->front->next;
l->front->prev = NULL;
}
(l->counter)--;
free(n);
return ptr;
}
struct dnode*
dlist_remove_back(struct dlist *l) {
struct dnode *n = l->back;
void *ptr = n->data;
if (1 == l->counter) {
l->front = l->back = NULL;
} else {
l->back = l->back->prev;
l->back->next = NULL;
}
(l->counter)--;
free(n);
return ptr;
}
struct dnode*
dlist_find_remove(struct dlist *l, void *ptr) {
struct dnode *n = l->front;
void *ret_ptr = dnode_create();
while ((n != NULL) && (n->data != ptr)) {
n = n->next;
}
if (n != NULL) {
if (l->front == n) {
return dlist_remove_front(l);
} else if (l->back == n) {
return dlist_remove_back(l);
} else {
if (1 == l->counter) {
l->front = l->back = NULL;
} else {
n->prev->next = n->next;
n->next->prev = n->prev;
}
(l->counter)--;
}
//ret_ptr = n->data;
free(n);
}
return ret_ptr;
}
uint32_t
dlist_num_elems(struct dlist *l) {
return l->counter;
}
void *
dlist_iter_begin(struct dlist *l) {
void *ret_val = NULL;
l->iter = l->front;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
return ret_val;
}
void *
dlist_iter_next(struct dlist *l) {
void *ret_val = NULL;
if (l->iter != NULL) {
l->iter = l->iter->next;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
}
return ret_val;
}
bool
dlist_iter_has_next(struct dlist *l) {
bool ret_val = false;
if (l->iter != NULL) {
ret_val = (l->iter->next != NULL);
}
#ifdef DEBUG
if (ret_val) {
printf("dlist_has_next: current is %s\n", (char *) l->iter->data);
printf("dlist_has_next: returning %d\n\n", ret_val);
}
#endif /* DEBUG */
return ret_val;
}
void *
dlist_iter_end(struct dlist *l) {
void *ret_val = NULL;
l->iter = l->back;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
return ret_val;
}
void *
dlist_iter_prev(struct dlist *l) {
void *ret_val = NULL;
if (l->iter != NULL) {
l->iter = l->iter->prev;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
}
return ret_val;
}
bool
dlist_iter_has_prev(struct dlist *l) {
bool ret_val = false;
if (l->iter != NULL) {
ret_val = (l->iter->prev != NULL);