Modestus Moon OS  R4
CS 450 project
linked_list.h File Reference
#include "string.h"
#include <core/mpx_supt.h>
Include dependency graph for linked_list.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  s_ll_node
 struct for a node of the linked list More...
 
struct  s_ll
 struct definition for the linked list More...
 

Macros

#define MAX_NUM_OF_LL_NODES   256
 
#define VARIABLE_LL_LENGTH(LIST_LENGTH)
 

Typedefs

typedef struct s_ll linkedList_t
 
typedef struct s_ll_node node_t
 

Functions

void initLinkedList (linkedList_t *list)
 initilize list and the optional array that backs the list More...
 
node_tinsertAfterNode (node_t *preceedingNode, node_t *newNode)
 insert a node into the parent list of the preceedingNode after preceedingNode More...
 
node_tinsertBeforeNode (node_t *nodeToFollow, node_t *newNode)
 insert a node into the parent list of the nodeToFollow before nodeToFollow More...
 
node_tinsertNode (linkedList_t *list, node_t *newNode)
 Will insert the node into the list. This function will attempt to call the list's insert comparison function if defined. The function will insert the new node into the list after this function returns > 0. If the insert comparison function is not defined for this list, the inserting function will placed the new node at the end of the list before the tail. More...
 
node_tmakeNewNode (linkedList_t *list, void *data)
 get new node, allocate a new node or find an unused node from the list's pool More...
 
node_tmoveNodeToNewList (node_t *nodeToMove, linkedList_t *newList)
 If a node needs to move lists and lists are array backed, instead of just moving preceeding and suceeding pointers of the node, the node actaully needs to move backing arrays so one list's nodes are not the only ones being consumed. The equivalent process will be to execute the parent list's removeNode funcion, execute the makeNewNode function on the newList, copy all structure members to the new node, and return the new node. The list of nodeToMove freeNodeFunc will be called with nodeToMov as the argument. If the lists are not array backed, the function simply removes the node from its containing list and calls insertNode on the new list. More...
 
void setFreeFunction (linkedList_t *list, int(*newFreeFunc)(node_t *))
 sets the function whose job it is to free the node's memory More...
 
void setInsertComparisonFunction (linkedList_t *list, int(*newCompFunc)(void *, void *))
 takes in a function pointer and sets the library shared comparison pointer More...
 
void setPrintFunction (linkedList_t *list, void(*newPrintFunc)(void *))
 sets the function whose job it is to print the list to the screen. More...
 
void setSearchComparisonFunction (linkedList_t *list, int(*newSearchFunc)(void *, void *))
 sets the function whose job it is to compare the input data with the data in the list. the first void parameter will always be the data contained in the list, the second will be the data passed to the search function. More...
 
node_tremoveNode (node_t *nodeToRemove)
 will remove the node from the list. the list hierarchy will change to reflect the missing node. More...
 
node_tsearchList (linkedList_t *listToSearch, void *data)
 goes through the list using the list's comparison function to find the node which matches the data. if the search function is undefined for this list, the function returns null immedietly. More...
 
void printList (linkedList_t *list)
 test function to show list functionality. uses const char* as test data More...
 
void listTest ()
 test the linked_list class with const char* data and output to screen the results of the test More...
 

Variables

struct s_ll_node __attribute__
 

Detailed Description

This module provides a generic linked list that can be array backed or use dynamic allocation

Definition in file linked_list.h.

Macro Definition Documentation

#define MAX_NUM_OF_LL_NODES   256

Definition at line 22 of file linked_list.h.

Referenced by initLinkedList(), and makeNewNode().

#define VARIABLE_LL_LENGTH (   LIST_LENGTH)
Value:
{ \
typedef struct s_ll_##LIST_LENGTH##_t linkedList_##LIST_LENGTH##_t \
struct s_ll_##LIST_LENGTH##_t \
{ \
node_t head; \
node_t tail; \
int (*searchCompFunc)(void*, void*); \
int (*comparisonFunc)(void*, void*); \
int (*freeNodeFunc)(node_t *nodeToFree); \
void (*printList)(void*); \
size_t length; \
unsigned int numNodesInUse; \
node_t backingArray[LIST_LENGTH]; \
}; \
}
typedef of linked list node (see s_ll_node).
void printList(linkedList_t *list)
test function to show list functionality. uses const char* as test data
Definition: linked_list.c:212
struct s_ll_node node_t
Definition: linked_list.h:38
unsigned int size_t
Definition: system.h:24

defines a macro that will generate a new type that has a different array backing size, new type has the name linkedList_<list_length>_t

Definition at line 81 of file linked_list.h.

Typedef Documentation

typedef struct s_ll linkedList_t

Definition at line 30 of file linked_list.h.

typedef struct s_ll_node node_t

Definition at line 38 of file linked_list.h.

Function Documentation

void initLinkedList ( linkedList_t list)

initilize list and the optional array that backs the list

Parameters
listpointer to list

Definition at line 3 of file linked_list.c.

References MAX_NUM_OF_LL_NODES.

Referenced by initHeap(), initPCBQueues(), and listTest().

4 {
5 #ifdef LL_IS_ARRAY_BACKED
6  int i;
7  for(i=0; i < MAX_NUM_OF_LL_NODES; i++)
8  {
9  list->backingArray[i].data=0;
10  list->backingArray[i].inUse=0;
11  list->backingArray[i].next=0;
12  list->backingArray[i].prev=0;
13  list->backingArray[i].parentList = list;
14  }
15 
16  list->head.parentList = list;
17  list->head.inUse=1;
18 
19  list->tail.parentList = list;
20  list->tail.inUse=1;
21 #endif
22 
23  list->head.data=0;
24  list->head.next =
25  list->head.prev = &list->tail;
26 
27  list->tail.data=0;
28  list->tail.next =
29  list->tail.prev = &list->head;
30 
31  list->length = 0;
32  list->insertCompFunc = 0;
33  list->freeNodeFunc = 0;
34  list->searchCompFunc = 0;
35 }
#define MAX_NUM_OF_LL_NODES
Definition: linked_list.h:22
node_t* insertAfterNode ( node_t preceedingNode,
node_t newNode 
)

insert a node into the parent list of the preceedingNode after preceedingNode

Parameters
preceedingNodenode to insert the new node afterwards
newNodethe new node to insert. if the list is array backed, the newNode will be copied to the list's node pool if it currently does not reside there
Returns
pointer to node inserted in the parent list of preceedingNode, null if insertion failed

Definition at line 78 of file linked_list.c.

References moveNodeToNewList().

79 {
80 #ifdef LL_IS_ARRAY_BACKED
81  if(preceedingNode->parentList != newNode->parentList)
82  {
83  //if the node to insert is not apart of the list to insert after, the function moveNodeToNewList makes a
84  //new node in the parent list of the preceedingNode and recursively calls this function with the newly moved node
85  return insertAfterNode(preceedingNode,
86  moveNodeToNewList(newNode, preceedingNode->parentList));
87  }
88 #endif
89 
90  newNode->next = preceedingNode->next;
91  newNode->prev = preceedingNode;
92 
93  newNode->next->prev = newNode;
94  newNode->prev->next = newNode;
95 
96  return newNode;
97 }
node_t * insertAfterNode(node_t *preceedingNode, node_t *newNode)
insert a node into the parent list of the preceedingNode after preceedingNode
Definition: linked_list.c:78
node_t * moveNodeToNewList(node_t *nodeToMove, linkedList_t *newList)
If a node needs to move lists and lists are array backed, instead of just moving preceeding and sucee...
Definition: linked_list.c:132
node_t* insertBeforeNode ( node_t nodeToFollow,
node_t newNode 
)

insert a node into the parent list of the nodeToFollow before nodeToFollow

Parameters
nodeToFollownode to insert the newNode before
newNodethe new node to insert. if the list is array backed, the newNode will be copied to the list's node pool if it currently does not reside there
Returns
pointer to node inserted in the parent list of nodeToFollow, null if insertion failed

Definition at line 99 of file linked_list.c.

References moveNodeToNewList().

Referenced by insertNode().

100 {
101 #ifdef LL_IS_ARRAY_BACKED
102  if(nodeToFollow->parentList != newNode->parentList)
103  {
104  //if the node to insert is not apart of the list to insert after, the function moveNodeToNewList makes a
105  //new node in the parent list of the preceedingNode and recursively calls this function with the newly moved node
106  return insertBeforeNode(nodeToFollow,
107  moveNodeToNewList(newNode, nodeToFollow->parentList));
108  }
109 #endif
110 
111  newNode->next = nodeToFollow;
112  newNode->prev = nodeToFollow->prev;
113 
114  newNode->next->prev = newNode;
115  newNode->prev->next = newNode;
116 
117  return newNode;
118 }
node_t * moveNodeToNewList(node_t *nodeToMove, linkedList_t *newList)
If a node needs to move lists and lists are array backed, instead of just moving preceeding and sucee...
Definition: linked_list.c:132
node_t * insertBeforeNode(node_t *nodeToFollow, node_t *newNode)
insert a node into the parent list of the nodeToFollow before nodeToFollow
Definition: linked_list.c:99
node_t* insertNode ( linkedList_t list,
node_t newNode 
)

Will insert the node into the list. This function will attempt to call the list's insert comparison function if defined. The function will insert the new node into the list after this function returns > 0. If the insert comparison function is not defined for this list, the inserting function will placed the new node at the end of the list before the tail.

Parameters
listlist to place the node
newNodethe new node to insert. if the list is array backed, the newNode will be copied to the list's node pool if it currently does not reside there
Returns
pointer to the node in the new list, or null if inserting failed.

Definition at line 173 of file linked_list.c.

References insertBeforeNode(), and moveNodeToNewList().

Referenced by insertMCBIntoList(), insertPCB(), listTest(), and moveNodeToNewList().

174 {
175  //if newNode is not valid, do not insert any node
176  if(!newNode)
177  {
178  return 0;
179  }
180 
181 #ifdef LL_IS_ARRAY_BACKED
182  if(newNode->parentList != list)
183  {
184  //if the node to insert is not apart of the list to insert after, the function moveNodeToNewList makes a
185  //new node in the parent list of the preceedingNode and recursively calls this function with the newly moved node
186  return insertNode(list, moveNodeToNewList(newNode, list));
187  }
188 #endif
189 
190  if(list->insertCompFunc)
191  {
192  node_t * itNode = list->head.next;
193  for(; itNode != &(list->tail); itNode = itNode->next)
194  {
195  int result = ((*(list->insertCompFunc))(itNode->data, newNode->data));
196  if(result > 0)
197  {
198  list->length++;
199  return insertBeforeNode(itNode, newNode);
200  }
201  }
202  }
203  list->length++;
204  return insertBeforeNode(&(list->tail), newNode);
205 }
typedef of linked list node (see s_ll_node).
node_t * moveNodeToNewList(node_t *nodeToMove, linkedList_t *newList)
If a node needs to move lists and lists are array backed, instead of just moving preceeding and sucee...
Definition: linked_list.c:132
node_t * insertBeforeNode(node_t *nodeToFollow, node_t *newNode)
insert a node into the parent list of the nodeToFollow before nodeToFollow
Definition: linked_list.c:99
node_t * insertNode(linkedList_t *list, node_t *newNode)
Will insert the node into the list. This function will attempt to call the list&#39;s insert comparison f...
Definition: linked_list.c:173
void listTest ( )

test the linked_list class with const char* data and output to screen the results of the test

Definition at line 250 of file linked_list.c.

References compFunction(), initLinkedList(), insertNode(), makeNewNode(), printf, printList(), searchcompFunction(), searchList(), setInsertComparisonFunction(), setPrintFunction(), setSearchComparisonFunction(), and testPrintFunc().

251 {
252  linkedList_t list1;
253  linkedList_t list2;
254 
255  initLinkedList(&list1);
256  initLinkedList(&list2);
257 
261 
262  insertNode(&list1, makeNewNode(&list2, (void*)"test3"));
263  insertNode(&list1, makeNewNode(&list2, (void*)"test1"));
264  insertNode(&list1, makeNewNode(&list2, (void*)"test5"));
265  insertNode(&list1, makeNewNode(&list2, (void*)"test2"));
266  insertNode(&list1, makeNewNode(&list2, (void*)"test7"));
267  insertNode(&list1, makeNewNode(&list2, (void*)"test4"));
268  insertNode(&list1, makeNewNode(&list2, (void*)"test8"));
269  insertNode(&list1, makeNewNode(&list2, (void*)"test3"));
270 
271 
272  // node_t * nodeFound = searchList(&list1, (void*)"test3");
273 
274  node_t * nodeFound = searchList(&list1, (void*)"test5");
275  printf("looking for node whose data is: %s\r\nsearch return: %d\r\n", "test5", (int)nodeFound);
276 
277  if(nodeFound)
278  {
279  printf("found node:: data string = %s\r\n", (const char*)nodeFound->data);
280  }
281 
282  printList(&list1);
283 }
void setInsertComparisonFunction(linkedList_t *list, int(*newCompFunc)(void *, void *))
takes in a function pointer and sets the library shared comparison pointer
Definition: linked_list.c:63
typedef of linked list node (see s_ll_node).
void setPrintFunction(linkedList_t *list, void(*newPrintFunc)(void *))
sets the function whose job it is to print the list to the screen.
Definition: linked_list.c:207
int searchcompFunction(void *nodeData1, void *nodeData2)
Definition: linked_list.c:238
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
typedef of linked list (see s_ll).
void printList(linkedList_t *list)
test function to show list functionality. uses const char* as test data
Definition: linked_list.c:212
void testPrintFunc(void *nodeData)
Definition: linked_list.c:245
int compFunction(void *nodeData1, void *nodeData2)
Definition: linked_list.c:231
void setSearchComparisonFunction(linkedList_t *list, int(*newSearchFunc)(void *, void *))
sets the function whose job it is to compare the input data with the data in the list. the first void parameter will always be the data contained in the list, the second will be the data passed to the search function.
Definition: linked_list.c:73
node_t * searchList(linkedList_t *listToSearch, void *data)
goes through the list using the list&#39;s comparison function to find the node which matches the data...
Definition: linked_list.c:154
void initLinkedList(linkedList_t *list)
initilize list and the optional array that backs the list
Definition: linked_list.c:3
node_t * insertNode(linkedList_t *list, node_t *newNode)
Will insert the node into the list. This function will attempt to call the list&#39;s insert comparison f...
Definition: linked_list.c:173
node_t * makeNewNode(linkedList_t *list, void *data)
get new node, allocate a new node or find an unused node from the list&#39;s pool
Definition: linked_list.c:37
node_t* makeNewNode ( linkedList_t list,
void *  data 
)

get new node, allocate a new node or find an unused node from the list's pool

Parameters
listto find an unused node from. This argument is ignored if the list is dynamically allocated
datapointer to the data the new node should contain, the pointer must be valid obviously
Returns
pointer to new node, null if no new node could be found or created

Definition at line 37 of file linked_list.c.

References MAX_NUM_OF_LL_NODES, and sys_alloc_mem().

Referenced by allocatePCB(), listTest(), and moveNodeToNewList().

38 {
39 #ifdef LL_IS_ARRAY_BACKED
40  int i;
41  for(i=0; i < MAX_NUM_OF_LL_NODES; i++)
42  {
43  if(!list->backingArray[i].inUse)
44  {
45  list->backingArray[i].inUse = 1;
46  list->numNodesInUse++;
47  list->backingArray[i].data = data;
48  return &(list->backingArray[i]);
49  }
50  }
51  return 0;
52 #else
53  (void)list;
54  node_t * newNode = sys_alloc_mem(sizeof(node_t));
55  newNode->data = data;
56  newNode->next=0;
57  newNode->prev=0;
58  return newNode;
59 
60 #endif
61 }
void * sys_alloc_mem(u32int size)
Definition: mpx_supt.c:33
typedef of linked list node (see s_ll_node).
#define MAX_NUM_OF_LL_NODES
Definition: linked_list.h:22
node_t* moveNodeToNewList ( node_t nodeToMove,
linkedList_t newList 
)

If a node needs to move lists and lists are array backed, instead of just moving preceeding and suceeding pointers of the node, the node actaully needs to move backing arrays so one list's nodes are not the only ones being consumed. The equivalent process will be to execute the parent list's removeNode funcion, execute the makeNewNode function on the newList, copy all structure members to the new node, and return the new node. The list of nodeToMove freeNodeFunc will be called with nodeToMov as the argument. If the lists are not array backed, the function simply removes the node from its containing list and calls insertNode on the new list.

Parameters
nodeToMovethe node to move, this pointer is no longer guarenteed to be valid. Stop using this pointer,
newListthe list to move the node to, this will be the node's new parent list
Returns
the pointer of param nodeToMove will no longer be valid, the pointer to the node in the new list will be returned on sucess. Null on failure and nodeToMove will remain valid

Definition at line 132 of file linked_list.c.

References insertNode(), makeNewNode(), and removeNode().

Referenced by insertAfterNode(), insertBeforeNode(), and insertNode().

133 {
134 #ifdef LL_IS_ARRAY_BACKED
135  node_t * newNodeInList = makeNewNode(newList, nodeToMove->data);
136  if(!newNodeInList)
137  {
138  return 0;
139  }
140 
141  removeNode(nodeToMove);
142 
143  if(nodeToMove->parentList->freeNodeFunc)
144  {
145  (*(nodeToMove->parentList->freeNodeFunc))(nodeToMove);
146  }
147  return newNodeInList;
148 #else
149  node_t* removedNode = removeNode(nodeToMove);
150  return insertNode(newList, removedNode);
151 #endif
152 }
typedef of linked list node (see s_ll_node).
node_t * removeNode(node_t *nodeToRemove)
will remove the node from the list. the list hierarchy will change to reflect the missing node...
Definition: linked_list.c:120
node_t * insertNode(linkedList_t *list, node_t *newNode)
Will insert the node into the list. This function will attempt to call the list&#39;s insert comparison f...
Definition: linked_list.c:173
node_t * makeNewNode(linkedList_t *list, void *data)
get new node, allocate a new node or find an unused node from the list&#39;s pool
Definition: linked_list.c:37
void printList ( linkedList_t list)

test function to show list functionality. uses const char* as test data

Parameters
listlist to print the nodes of

Definition at line 212 of file linked_list.c.

References printf.

Referenced by heapTest(), listTest(), mcbFunc(), pcbTest(), printAlloc(), printFree(), showAllProcesses(), showBlockedProcesses(), and showReadyProcesses().

213 {
214  if(list->printNodeFunc)
215  {
216  node_t * itNode = list->head.next;
217  if(itNode == &(list->tail))
218  {
219  printf("%s","<List is Empty>\r\n");
220  }
221  for(; itNode != &(list->tail); itNode = itNode->next)
222  {
223  list->printNodeFunc(itNode->data);
224  }
225  }else
226  {
227  printf("No Search Function provided for List!%s\r\n"," ");
228  }
229 }
typedef of linked list node (see s_ll_node).
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
node_t* removeNode ( node_t nodeToRemove)

will remove the node from the list. the list hierarchy will change to reflect the missing node.

Parameters
nodeToRemovepointer to the node to remove
Returns
pointer to the removed node

Definition at line 120 of file linked_list.c.

Referenced by allocateMemFromHeap(), freeHeapMem(), moveNodeToNewList(), reclaimFreeMem(), and removePCB().

121 {
122  if(nodeToRemove)
123  {
124  if(nodeToRemove->prev){nodeToRemove->prev->next = nodeToRemove->next;}
125  if(nodeToRemove->next){nodeToRemove->next->prev = nodeToRemove->prev;}
126  nodeToRemove->next = 0;
127  nodeToRemove->prev = 0;
128  }
129  return nodeToRemove;
130 }
node_t* searchList ( linkedList_t listToSearch,
void *  data 
)

goes through the list using the list's comparison function to find the node which matches the data. if the search function is undefined for this list, the function returns null immedietly.

Parameters
listToSearchthe list to search
datavalid pointer to data that can be dereferenced. this pointer will be the second argument to the list's search comparison function
Returns
null if not found, else pointer to the node that matches.

Definition at line 154 of file linked_list.c.

Referenced by findPCB(), freeHeapMem(), and listTest().

155 {
156  int (*compFunctCpy)(void*,void*) = (listToSearch->searchCompFunc) ?
157  listToSearch->searchCompFunc :
158  listToSearch->insertCompFunc;
159  if(compFunctCpy)
160  {
161  node_t * itNode = listToSearch->head.next;
162  for(; itNode != &(listToSearch->tail); itNode = itNode->next)
163  {
164  if(!(*(compFunctCpy))(itNode->data, data))
165  {
166  return itNode;
167  }
168  }
169  }
170  return 0;
171 }
typedef of linked list node (see s_ll_node).
void setFreeFunction ( linkedList_t list,
int(*)(node_t *)  newFreeFunc 
)

sets the function whose job it is to free the node's memory

Parameters
listthe list to set the function pointer for
newFreeFuncthe job of this function should be to free the memory with the node once it the list no longer needs it. This can be literally through free() or by other means.

Definition at line 68 of file linked_list.c.

69 {
70  list->freeNodeFunc = newFreeFunc;
71 }
void setInsertComparisonFunction ( linkedList_t list,
int(*)(void *, void *)  newCompFunc 
)

takes in a function pointer and sets the library shared comparison pointer

Parameters
listthe list to set the function pointer for
newCompFuncpointer to function whose job is to define the comparison of the data stored in the node this function should return 0 if the comparison succeeds. The function should return < 0 if the data to compare comes after the data in the node. The function should return > 0 if the data to compare comes before the data in the node. The first void argument is guarenteed to be the data from the list to compare against. The second void argument is the data in the node passed to the insertNode function

Definition at line 63 of file linked_list.c.

Referenced by initHeap(), and listTest().

64 {
65  list->insertCompFunc = newCompFunc;
66 }
void setSearchComparisonFunction ( linkedList_t list,
int(*)(void *, void *)  newSearchFunc 
)

sets the function whose job it is to compare the input data with the data in the list. the first void parameter will always be the data contained in the list, the second will be the data passed to the search function.

Parameters
listto set the search function for
newSeachFuncpointer to function whose job is to define the comparison of the data stored in the node this function should return 0 if the comparison succeeds. The function should return < 0 if the data to compare comes after the data in the node. The function should return > 0 if the data to compare comes before the data in the node. The first void argument is guarenteed to be the data from the list to compare against. The second void argument is the data passed to the searchList function

Definition at line 73 of file linked_list.c.

Referenced by initHeap(), initPCBQueues(), and listTest().

74 {
75  list->searchCompFunc = newSearchFunc;
76 }

Variable Documentation

struct s_ll_node __attribute__