-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.c
284 lines (253 loc) · 5.88 KB
/
List.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "List.h"
#include "rcbc_data.h"
#include <stdio.h>
#include <assert.h>
#include "console.h"
/**
* List deconstructor.
* Recursivly free a linked list.
* @param rootnode Pointer to List to free.
* \memberof List
*/
void List_0List(List* rootnode) {
assert(rootnode);
DEBUG_M("Entering function...");
if(!rootnode) {
DEBUG_H("\tNo root node...");
return;
}
ListNode* node_ptr = rootnode->first;
ListNode* node_ptr_tmp;
while(node_ptr) {
DEBUG_H("\tPointer...");
node_ptr_tmp = node_ptr->next;
if(node_ptr->data) {
ERROR("\tMemory leak!: Data still in deleting list node...");
// BREAK();
//free(node_ptr->data);
//DELETE(node_ptr);
}
/*node_ptr->prev = NULL;
DELETE(node_ptr);*/
List_DeleteNode(rootnode, node_ptr);
node_ptr = node_ptr_tmp;
}
free(rootnode);
}
/**
* ListNode deconstructor.
* Deletes a node from the linked list.
* @param node Pointer to ListNode to free
* \memberof ListNode
*/
void ListNode_0ListNode(ListNode* node) {
DEBUG_M("Entering function...");
if(node->prev) {
//node->prev->next = node->next;
}
node->prev = NULL;
node->next = NULL;
node->data = NULL;
free(node);
}
/**
* List class functions.
*/
static const ClassFunctions List_c = {
(void*)List_0List
};
/**
* ListNode class functions.
*/
static const ClassFunctions ListNode_c = {
(void*)ListNode_0ListNode
};
/**
* List constructor.
* Generates a linked list head node.
* @return Pointer to new List head node or NULL on error.
* \memberof List
*/
List* List_List() {
DEBUG_M("Entering function...");
//ALLOCATE(List, list);
List* list = malloc(sizeof(List));
list->class_ = &List_c;
list->count = 0;
list->first = NULL;
list->last = NULL;
return list;
}
/**
* ListNode constructor
* Generates a linked list node.
* @param data Data to be stored in the ListNode.
* @return Pointer to new ListNode or NULL on error.
* \memberof ListNode
*/
ListNode* ListNode_ListNode(void* data) {
DEBUG_M("Entering function...");
//ALLOCATE(ListNode, node);
ListNode* node = malloc(sizeof(ListNode));
if(!node) {
return NULL;
}
node->class_ = &ListNode_c;
node->data = data;
node->prev = NULL;
node->next = NULL;
return node;
}
/**
* Adds a new node to the linked list
* @param head List head node.
* @param data Data to be stored in the ListNode.
* @return New ListNode or NULL on error.
* \memberof List
*/
ListNode* ListAdd(List* head, void* data) {
DEBUG_M("Entering function...");
assert(head);
assert(data);
ListNode* newnode = NEW(ListNode, data);
if(!newnode) {
ERROR("Failed to allocate space for node... %s", SYMBOL_WARNING);
return NULL;
}
if(!head->first) {
head->first = newnode;
head->last = newnode;
return newnode;
}
newnode->prev = head->last;
head->last->next = newnode;
head->last = newnode;
head->count++;
return newnode;
}
/**
* DELETE()'s the data from the linked list assuming its an ooc class.
* Doesn't effect the list itself.
* @param list List with data to DELETE().
* @see List_FreeData()
* \memberof List
*/
void List_DeleteData(List* list) {
DEBUG_M("Entering function...");
assert(list);
ListNode* node = list->first;
while(node) {
DEBUG_H("\tNode %p...", node);
DEBUG_H("\tchecking data %p...", node->data);
if(node->data) {
DEBUG_H("\t\tDELETE [DATA] %p...", node->data);
DELETE(node->data);
//node->data = NULL;
}
node->data = NULL;
node = node->next;
//DEBUG_H("\tcalling List_DeleteNode...");
//List_DeleteNode(list, node);
}
DEBUG_H("Exiting...");
}
/**
* Deletes a node from the list, ensuring the list pointers remain correct.
* @param list The list containing the node.
* @param node The node to remove from the list.
* \memberof List
*/
void List_DeleteNode(List* list, ListNode* node) {
DEBUG_M("Entering function...");
assert(node);
assert(list);
DEBUG_M("\tnode:%p ,listlast:%p, listfirst:%p...", node, list->last, list->first);
if(list->first == node && list->last == node) {
DEBUG_H("\tNode is both the first and last node...");
list->first = NULL;
list->last = NULL;
} else {
DEBUG_H("\tChecking first...");
if(list->first == node) {
DEBUG_H("\tnode is first in list...");
list->first = node->next;
}
DEBUG_H("\tChecking last...");
if(list->last == node) {
DEBUG_H("\tnode is last in list...");
list->last = node->prev;
}
}
list->count--;
DELETE(node);
}
/**
* Prunes Images without refrences from the list.
* \memberof List
*/
void List_ScrubImages(List* list) {
DEBUG_M("Entering function...");
ListNode* node = list->first;
while(node) {
Image* image = node->data;
int refs = image->refs;
ListNode* deleteme = node;
node = node->next;
if(refs-1 <= 0) {
DEBUG_H("\tNo more refs for '%s' DELETE()ing...", image->filename);
if(deleteme->data) {
DEBUG_H("\tData remains, DELETE()ing...");
DELETE(deleteme->data);
deleteme->data = NULL;
}
DEBUG_H("\tDeleteNode...");
List_DeleteNode(list, deleteme);
DEBUG_H("\tDone with that...");
}
}
}
/**
* free()'s the data from the linked list, for non-complex data structs.
* Doesn't effect the list itself.
* @param list List with data to free().
* @see List_DeleteData()
* \memberof List
*/
void List_FreeData(List* list) {
DEBUG_M("Entering function...");
ListNode* node = list->first;
while(node) {
DEBUG_H("\tNode freeing...");
if(node->data) {
DEBUG_H("\tFree data...");
free(node->data);
}
node->data = NULL;
node = node->next;
}
}
/**
* Debuggin function.
* @param list List to dump.
* \memberof List
*/
void List_DumpList(List* list) {
DEBUG_M("Entering function...");
ListNode* node = list->first;
while(node) {
DEBUG_H("Node[%p]: node->data: %p");
}
}
/**
* Sets all the data pointers in a List to null.
* No memory freeing or DELETING is done.
* @param list The list with the data.
* \memberof List
*/
void List_NullifyData(List* list) {
ListNode* itr = list->first;
while(itr) {
itr->data = NULL;
itr = itr->next;
}
}