-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFIFOq.c
More file actions
356 lines (327 loc) · 10.1 KB
/
FIFOq.c
File metadata and controls
356 lines (327 loc) · 10.1 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Problem 3 - Discontinuities
* TCSS 422 A Spring 2016
* Bun Kak, Chris Ottersen, Mark Peters, Paul Zander
*/
#include "FIFOq.h"
static PCB_p null;
/* Constructs you a boring empty FIFO queue and also intializes it.
*
* error is the error pointer
*
* returns you a boring empty FIFO queue
*/
FIFOq_p FIFOq_construct(int *error) {
FIFOq_p this = (FIFOq_p) malloc(sizeof(struct FIFOq));
int init_error = ((!this) * FIFO_CONSTRUCT_ERROR) || FIFOq_init(this, error);
if (init_error && !is_null(this, error)) {
FIFOq_destruct(this, error);
}
return this;
}
/* Utterly destroys the FIFO queue and everything in it.
*
* this is the FIFO queue no more
* error is the error pointer
*/
void FIFOq_destruct(FIFOq_p this, int *error) {
if (!is_null(this, error)) {
Node_p node = this->head;
while (this->head != NULL) {
node = this->head->next_node;
PCB_destruct(this->head->data);
Node_destruct(this->head);
this->head = node;
}
free(this);
this = NULL;
}
}
/* Initializes the values of the FIFO queue.
*
* this is the FIFO queue that is hopelessly lost without values
* error is the error pointer and everything that is wrong with society
*
* returns an error int
*/
int FIFOq_init(FIFOq_p this, int *error) {
if (!is_null(this, error)) {
this->size = 0;
this->head = NULL;
}
return error == NULL ? 0 : *error;
}
/* Checks if the queue is empty and tells you politely.
*
* this is the queue that may or may not be empty
* error is the error pointer
*
* returns the error as a plain boring int
*/
int FIFOq_is_empty(FIFOq_p this, int *error) {
if (!is_null(this, error)) {
return this->size > 0 ? 0 : 1;
}
return error == NULL ? 0 : *error;
}
/* Enqueues the given node to the end (tail) of the queue, unless the queue is
* empty, in which case it enqueues to the head.
*
* this is the FIFO queue whose beautiful little village is growing by one
* next is the node to queue into the FIFO queue
* error is the pointer error
*
* returns nothing because too bad
*/
void FIFOq_enqueue(FIFOq_p this, Node_p next, int *error) {
if (!is_null(this, error) && !is_null(next, error)) {
// an enqueued PCB is in the ready state
//PCB_setState(next->data, 1);
if (this->head == NULL) {
if (!next->pos) {
next->pos = 1;
}
this->head = next;
this->tail = this->head;
} else {
if (!next->pos) {
next->pos = this->tail->pos + 1;
}
this->tail->next_node = next;
this->tail = next;
}
this->size++;
}
}
void FIFOq_enqueuePCB(FIFOq_p this, PCB_p pcb, int* error) {
FIFOq_enqueue(this, Node_construct(pcb, NULL, error), error);
}
/* Dequeues the node located at the head of the queue, destroying the node and
* returning the pcb pointer. The head of the fifo queue is re-chosen to be
* the next_node of the former head node.
*
* this is the FIFO queue we are dequeueing from
* error is the error pointer
* Oq_p
* returns the pcb pointer that was the head
*/
PCB_p FIFOq_dequeue(FIFOq_p this, int *error) {
if (!is_null(this, error) && !is_null(this->head, error)) {
Node_p node = this->head;
this->head = this->head->next_node;
if (this->head == NULL) {
this->tail = NULL;
}
this->size--;
PCB_p pcb = node->data;
// a dequeued PCB is in the ready state
//PCB_setState(pcb, 2);
Node_destruct(node);
return pcb;
}
return NULL;
}
/* FIFO toString command, which gives an output of each element in the queue
* with their node number plus the toString of the last pcb.
*
* this is the FIFO queue whose toString we're constructing
* str is the string to place the toString in
* stz is the size of that string bugger
* error is the error pointer
*
* return a version of the toString as a null-terminated char pointer
*/
char * FIFOq_toString(FIFOq_p this, char *str, int *stz, int *error) {
int usedChars = 0;
if (!is_null(this, error) && !is_null(str, error)) {
char pcbstr[PCB_TOSTRING_LEN];
// ">I/O %d added: %s\n"
usedChars += snprintf(str, *stz - usedChars, "Head:");
if (this->head != NULL) {
Node_p node = this->head;
usedChars += snprintf(str + strlen(str), *stz - usedChars, " %s\n-", PCB_toString(node->data, pcbstr, error));
node = node->next_node;
int newline = 1;
while (node != NULL) {
PCB_p pcb = node->data;
word pid = PCB_getPid(pcb, error);
//alternate P1->P2-* scheme
// usedChars += snprintf(str + strlen(str), *stz - usedChars, "%cP%lu-", node == this->head? ' ' : '>', pid, PCB_toString);
usedChars += snprintf(str + strlen(str), *stz - usedChars, "> PID: 0x%04lx%c-", pid, (newline++ % 5) ? ' ' : '\n');
node = node->next_node;
}
usedChars += snprintf(str + strlen(str), *stz - usedChars, "* (Queue Count = %d)", this->size);
}
*stz = *stz - usedChars;
return str;
} else {
return 0;
}
}
/* Constructs a node with the given parameters. For internal use only within
* the FIFO queue so thus acting as an inner class.
*
* data is the pcb to stick in the node
* next_node is the linked node that follows the current one
* error is the error pointer
*
* returns the node, duh
*/
Node_p Node_construct (PCB_p data, Node_p next_node, int *error) {
Node_p this = (Node_p) malloc(sizeof(struct Node_type));
int init_error = ((!this) * NODE_CONSTRUCT_ERROR) ||
Node_init(this, data, next_node, error);
init_error = init_error + (this == NULL) * NODE_NULL_ERROR;
if (init_error) {
Node_destruct(this);
}
return this;
}
/* Node data initializer that sets the node with the given values.
*
* this is the node we're initializing
* data is the pcb the node points to
* next_node is the subsequent node the node points to
* error is the error integer pointer
*
* returns an error integer
*/
int Node_init (Node_p this, PCB_p data, Node_p next_node, int *error) {
int init_error = (this == NULL) * NODE_NULL_ERROR;
if (error != NULL)
*error += init_error;
if (!init_error) {
this->pos = 0;
this->data = data;
this->next_node = next_node;
}
return error == NULL ? init_error : *error;
}
/* Deallocates and nullifies the given node.
*
* this is the node we hate so much it's got to go
*
* return an integer error pointer
*/
int Node_destruct (Node_p this) {
int error = (this == NULL) * NODE_NULL_ERROR;
if (!error) {
free(this);
this = NULL;
}
return error;
}
/* Gets the data (pcb) of the node and returns it.
*
* this is the node whose data we're reading
* ptr_error is the error pointer
*
* return the data (pcb) that the given node was pointing to
*/
PCB_p Node_getData (Node_p this, int * ptr_error) {
int error = this == NULL;
if (ptr_error != NULL) {
*ptr_error = error;
}
return error ? NULL : this->data;
}
/* Sets the node's data (pcb), overwriting any prior data.
*
* this is the node whose data we're writing to
* data is the pcb pointer we're giving to the node
*
* returns the error integer
*/
int Node_setData (Node_p this, PCB_p data) {
int error = (this == NULL) * NODE_NULL_ERROR;
if (!error) {
this->data = data;
}
return error;
}
/* Returns the next node of the given one.
*
* this is the node we want the next node of
* ptr_error is the error pointer
*
* returns a pointer to the next node
*/
Node_p Node_getNext (Node_p this, int * ptr_error) {
int error = (this == NULL) * NODE_NULL_ERROR;
if (ptr_error != NULL) {
*ptr_error += error;
}
return error ? NULL : this->next_node;
}
/* Sets the next node with the given one.
*
* this is the node whose next pointer is changing
* next_node is the node the this node will point t
*
* return an integer representing the error state
*/
int Node_setNext (Node_p this, Node_p next_node) {
int error = (this == NULL) * NODE_NULL_ERROR;
if (!error) {
this->next_node = next_node;
}
return error;
}
/* Returns a string describing the node. Not used anymore as PCB's toString
* is all that is needed for display.
*
* this is the node reference
* str is the input str for storing print data
* ptr_error is the error pointer
*
* returns the tostring
*/
char * Node_toString (Node_p this, char *str, int *ptr_error) {
int error = ((this == NULL) * NODE_NULL_ERROR |
(NODE_STRING_ERROR * (str == NULL)) |
(NODE_DATA_ERROR * (this->data == NULL)));
if (ptr_error != NULL) {
*ptr_error += error;
}
if (!error) {
snprintf(str, 10, "P%lx", PCB_getPid(this->data, NULL));
}
return str;
}
/* Checks if the given reference is null and updates error pointer if so.
* Note, Node was written before this idea so node has its function-dependent
* error checking.
*
* this is the unknown type we're checking for nullness
* ptr_error is the error pointer
*
* returns the error as an int as well for boolean tests
*/
int is_null(void *this, int *ptr_error) {
int error = (this == NULL) * FIFO_NULL_ERROR;
if (ptr_error != NULL) {
*ptr_error = error + *ptr_error;
}
return ptr_error == NULL ? error : error + *ptr_error;
}
Node_p FIFOq_remove_and_return_next(Node_p curr, Node_p prev, FIFOq_p list) {
if (curr == prev && curr == list->head) { // remove head
list->head = curr->next_node;//removes curr
curr->next_node = NULL;
if (list->head == NULL) { //it was the only node
list->tail = list->head;
}
list->size--;
return list->head;
} else if (prev->next_node == curr) {
prev->next_node = curr->next_node;
curr->next_node = NULL;
if(list->tail == curr) {
list->tail = prev;
}
list->size--;
return prev->next_node;
} else {
return NULL;
}
}