Commit 701ecc4e authored by Son Pham's avatar Son Pham

Lab 8 Completed

parent 8b3a0d24
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 = dlisttest memory-test best-fit-test worst-fit-test first-fit-test
all: $(EXECS)
doc:
doxygen
$(OBJ)/allocator.o: dlist.h dnode.h allocator.h allocator.c
$(CC) $(CFLAGS) -c $(SRC)/allocator.c -o $(OBJ)/allocator.o
$(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
dlisttest: $(SRC)/dlisttest.c $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/dlisttest.c -o $(BIN)/dlisttest
memory-test: $(SRC)/memory-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/memory-test.c -o $(BIN)/memory-test
first-fit-test: $(SRC)/first-fit-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/first-fit-test.c -o $(BIN)/first-fit-test
worst-fit-test: $(SRC)/worst-fit-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/worst-fit-test.c -o $(BIN)/worst-fit-test
best-fit-test: $(SRC)/best-fit-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/best-fit-test.c -o $(BIN)/best-fit-test
.PHONY: clean
clean:
/bin/rm -rf $(BIN)/* $(OBJ)/* $(DOC)/* core* *~
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 = dlisttest memory-test best-fit-test worst-fit-test first-fit-test
all: $(EXECS)
doc:
doxygen
$(OBJ)/allocator.o: dlist.h dnode.h allocator.h allocator.c
$(CC) $(CFLAGS) -c $(SRC)/allocator.c -o $(OBJ)/allocator.o
$(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
dlisttest: $(SRC)/dlisttest.c $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/dlisttest.c -o $(BIN)/dlisttest
memory-test: $(SRC)/memory-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/memory-test.c -o $(BIN)/memory-test
first-fit-test: $(SRC)/first-fit-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/first-fit-test -o $(BIN)/first-fit-test
worst-fit-test: $(SRC)/worst-fit-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/worst-fit-test.c -o $(BIN)/worst-fit-test
best-fit-test: $(SRC)/best-fit-test.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/best-fit-test.c -o $(BIN)/best-fit-test
.PHONY: clean
clean:
/bin/rm -rf $(BIN)/* $(OBJ)/* $(DOC)/* core* *~
# Name: Son Pham
# Class: CSCI 315
# Prof: Luiz Felipe Perrone
# designAPI.txt
dlist.c:
--------
I add function dlist_find_node so that I can find the node that corresponds to the memory. I feel that it is a lot easier to deal with the node than the memory of the node. This becomes important when I have to grab the size of the node that I want to deallocate
I also add two functions: dlist_get_front_node and dlist_get_back_node to get the address of the two nodes at the two ends of the list.
I add the dlist_print function to print the list in a convenient and tractable manner.
dnode.h:
--------
I add the size of the memory chunk as one of the piece of information of the node to make it easier to deal with.
First-fit policy:
-----------------
I simply traverse the entire list and find the first node that is available and has enough capacity to carry the whole size
Best-fit policy:
----------------
I traverse the list and find the smallest chunk of memory that still has enough capacity to carry the allocated size
Worst-fit policy:
-----------------
I traverse the list and find the largest chunk of memory that still has enough capacity to carry the allocated size.
# Name: Son Pham
# Class: CSCI 315
# Prof: Luiz Felipe Perrone
# designAPI.txt
dlist.c:
--------
I add function dlist_find_node so that I can find the node that corresponds to the memory. I feel that it is a lot easier to deal with the node than the memory of the node.
I also add two functions: dlist_get_front_node and dlist_get_back_node to get the address of the two nodes at the two ends of the list.
I add the dlist_print function to print the list in a convenient and tractable manner.
dnode.h:
--------
I add the size of the memory chunk as one of the piece of information of the node to make it easier to deal with.
First-fit policy:
-----------------
I simply traverse the entire list and find the first node that is available and has enough capacity to carry the whole size
Best-fit policy:
----------------
I traverse the list and find the smallest chunk of memory that still has enough capacity to carry the allocated size
Worst-fit policy:
-----------------
I traverse the list and find the largest chunk of memory that still has enough capacity to carry the allocated size.
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* allocator.h
*/
#ifndef _ALLOCATOR_H_
#define _ALLOCATOR_H_
#define FIRST_FIT 0
#define WORST_FIT 1
#define BEST_FIT 2
/* Necessary information about the doubly linked list */
struct dlist* free_list;
struct dlist* allocate_list;
int policy;
void* mem;
/**
* Initialize a memory allocator with an amount of size
* @param size indicating the size of the memory
*/
int allocator_init(size_t size);
/**
* Allocate a size amount of memory and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate(size_t size);
/**
* Allocate a size amount of memory base on first-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate_first_fit(size_t size);
/**
* Allocate a size amount of memory base on best-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate_best_fit(size_t size);
/**
* Allocate a size amount of memory base on worst-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate_worst_fit(size_t size);
/**
* Allocate a size amount of memory base on best-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
int deallocate(void* ptr);
#endif /* _ALLOCATOR_H_ */
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* allocator.h
*/
#ifndef _ALLOCATOR_H_
#define _ALLOCATOR_H_
#define FIRST_FIT 0
#define WORST_FIT 1
#define BEST_FIT 2
/* Necessary information about the doubly linked list */
struct dlist* free_list;
struct dlist* allocate_list;
int policy = FIRST_FIT;
void* mem;
/**
* Initialize a memory allocator with an amount of size
* @param size indicating the size of the memory
*/
int allocator_init(size_t size);
/**
* Allocate a size amount of memory and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate(size_t size);
/**
* Allocate a size amount of memory base on first-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate_first_fit(size_t size);
/**
* Allocate a size amount of memory base on best-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate_best_fit(size_t size);
/**
* Allocate a size amount of memory base on worst-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
void* allocate_worst_fit(size_t size);
/**
* Allocate a size amount of memory base on best-fit scheme
* and return a pointer to the memory
* Return NULL on error.
* @param: size indicating the size of the memory the user wants to allocate
*/
int deallocate(void* ptr);
#endif /* _ALLOCATOR_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();
/**
* 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);
/**
* 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);
/**
* 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
*/
void *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
*/
void *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
*/
void *dlist_find_remove(struct dlist *l, void *ptr);
/**
* Find the node that contains the pointer
* @param l pointer to the list (non-NULL)
* @param ptr pointer to the data item being sought
* @return pointer to the node containing the data.
*/
struct dnode* dlist_find_node(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);
/**
* Get the front node
*/
struct dnode* dlist_get_front_node(struct dlist *l);
/**
* Get the back node
*/
struct dnode* dlist_get_back_node(struct dlist *l);
/**
* Print the whole dlist out
*/
int dlist_print(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 _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();
/**
* 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);
/**
* 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);
/**
* 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
*/
void *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
*/
void *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
*/
void *dlist_find_remove(struct dlist *l, void *ptr);
/**
* Find the node that contains the pointer
* @param l pointer to the list (non-NULL)
* @param ptr pointer to the data item being sought
* @return pointer to the node containing the data.
*/
struct dnode* dlist_find_node(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);
/**
* Get the front node
*/
struct dnode* dlist_get_front_node(struct dlist *l);
/**
* Get the back node
*/
struct dnode* dlist_get_back_node(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;
int size;
void *data;
};
/**
* 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);
/**
* 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);
#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 _DNODE_H_
#define _DNODE_H_
/**
* Node in a doubly-linked list.
*/
struct dnode {
struct dnode *prev;
struct dnode *next;
void *data;
};
/**
* 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);
/**
* 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
*/