forked from ssalbiz/RTOS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
queues.c
389 lines (345 loc) · 8.88 KB
/
queues.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "queues.h"
//Queue Allocators
//three types of queues used, priority process queues, FIFO process queues, and FIFO message envelopes
void ppq_allocate(priority_process_queue** ppq) { //NULL heads and tails
(*ppq) = (priority_process_queue*) malloc(sizeof(priority_process_queue));
assert((*ppq) != NULL);
(*ppq)->pq_head[0] = NULL; (*ppq)->pq_tail[0] = NULL;
(*ppq)->pq_head[1] = NULL; (*ppq)->pq_tail[1] = NULL;
(*ppq)->pq_head[2] = NULL; (*ppq)->pq_tail[2] = NULL;
(*ppq)->pq_head[3] = NULL; (*ppq)->pq_tail[3] = NULL;
}
void mq_allocate(message_queue** mq) {
(*mq) = (message_queue*) malloc(sizeof(message_queue));
assert((*mq) != NULL);
(*mq)->head = NULL;
(*mq)->tail = NULL;
}
void pq_allocate(process_queue** pq) {
(*pq) = (process_queue*) malloc(sizeof(process_queue));
assert((*pq) != NULL);
(*pq)->head = NULL;
(*pq)->tail = NULL;
}
void trace_allocate(trace_buffer** tq) {
(*tq) = (trace_buffer*) malloc(sizeof(trace_buffer));
assert((*tq) != NULL);
(*tq)->send = NULL;
(*tq)->send_tail = NULL;
(*tq)->receive = NULL;
(*tq)->receive_tail = NULL;
(*tq)->send_length = 0;
(*tq)->receive_length = 0;
}
//Queue is_empty (if queue head is NULL, no elements, therefore empty)
int mq_is_empty(message_queue* mq) {
assert(mq != NULL);
return (mq->head == NULL);
}
int ppq_is_empty(priority_process_queue* ppq) {
assert(ppq != NULL);
return (ppq->pq_head[0] == NULL &&
ppq->pq_head[1] == NULL &&
ppq->pq_head[2] == NULL &&
ppq->pq_head[3] == NULL);
}
int pq_is_empty(process_queue* pq) {
assert(pq != NULL);
return (pq->head == NULL);
}
int ppq_is_empty_p(int p, priority_process_queue *ppq) {
assert(ppq != NULL);
return (ppq->pq_head[p] == NULL);
}
int trace_is_empty(trace_buffer* tq, enum Event type) {
if (type == SEND)
return (tq->send == NULL);
else
return (tq->receive == NULL);
}
//Queue Enqueues
//add to tail, readjust tail pointer
//
//
void trace_enqueue(msg_event* msg, trace_buffer* tq) {
assert(tq != NULL && msg != NULL);
if (msg->type == SEND) {
if (tq->send_length < TRACE_LENGTH) { //spec'd at 16
if (trace_is_empty(tq, msg->type)) {
tq->send = msg;
tq->send_tail = msg;
} else {
tq->send_tail->next = msg;
tq->send_tail = tq->send_tail->next;
}
msg->next = NULL;
tq->send_length++;
} else {
msg_event *tmp = trace_dequeue(tq, msg->type);
free(tmp);
tq->send_tail->next = msg;
msg->next = NULL;
}
} else {
if (tq->receive_length < TRACE_LENGTH) {
if (trace_is_empty(tq, msg->type)) {
tq->receive = msg;
tq->receive_tail = msg;
} else {
tq->receive_tail->next = msg;
tq->receive_tail = tq->receive_tail->next;
}
msg->next = NULL;
tq->receive_length++;
} else {
msg_event *tmp = trace_dequeue(tq, msg->type);
free(tmp);
tq->receive_tail->next = msg;
msg->next = NULL;
}
}
}
void mq_enqueue(MessageEnvelope* env, message_queue* mq) {
assert(env != NULL && mq != NULL);
env->next = NULL;
if (mq_is_empty(mq)) {
mq->head = env;
mq->tail = mq->head;
} else {
(mq->tail)->next = env;
mq->tail = env;
}
}
void pq_enqueue(PCB* next, process_queue* pq) {
assert(pq != NULL && next != NULL);
next->p_next = NULL;
if (pq_is_empty(pq)) {
pq->head = next;
pq->tail = pq->head;
} else {
(pq->tail)->p_next = next;
pq->tail = next;
}
}
void ppq_enqueue(PCB* q_next, priority_process_queue* ppq) {
assert(q_next != NULL && ppq != NULL);
q_next->q_next = NULL;
int priority = q_next->priority;
if (ppq_is_empty_p(priority, ppq)) {
ppq->pq_head[priority] = q_next;
ppq->pq_tail[priority] = ppq->pq_head[priority];
} else {
ppq->pq_tail[priority]->q_next = q_next;
ppq->pq_tail[priority] = q_next;
}
}
//Queue dequeues
//remove head, reset head/tail pointer as necessary
//return NULL on empty list
msg_event* trace_dequeue(trace_buffer* tq, enum Event type) {
assert(tq != NULL);
msg_event* tmp;
if (trace_is_empty(tq, type)) return NULL;
if (type == SEND) {
tmp = tq->send;
tq->send = tq->send->next;
tq->send_length--;
} else {
tmp = tq->receive;
tq->receive = tq->receive->next;
tq->receive_length--;
}
return tmp;
}
PCB* ppq_dequeue(priority_process_queue* ppq) {
assert(ppq != NULL);
PCB* ret;
int i = 0;
if (ppq_is_empty(ppq)) return NULL;
for (; i < MIN_PRIORITY+1; i++) {
if (!ppq_is_empty_p(i, ppq)) {
ret = ppq->pq_head[i];
ppq->pq_head[i] = (ppq->pq_head[i])->q_next;
ret->q_next = NULL;
if (ppq->pq_head[i] == NULL)
ppq->pq_tail[i] = NULL;
return ret;
}
}
return NULL;
}
PCB* pq_dequeue(process_queue* pq) {
if (pq_is_empty(pq)) return NULL;
PCB* ret = NULL;
ret = pq->head;
ret->p_next = NULL;
pq->head = (pq->head)->p_next;
if (pq->head == NULL)
pq->tail = NULL;
return ret;
}
MessageEnvelope* mq_dequeue(message_queue* mq) {
assert(mq != NULL);
if (mq_is_empty(mq)) return NULL;
MessageEnvelope* deq = mq->head;
mq->head = (mq->head)->next;
if (mq->head == NULL)
mq->tail = NULL;
deq->next = NULL;
return deq;
}
//Queue peeks
//return head
PCB* ppq_peek(priority_process_queue* ppq) {
assert(ppq != NULL);
priority_process_queue _ppq = (*ppq);
int i = 0;
for (; i < MIN_PRIORITY; i++)
if (!ppq_is_empty_p(i, ppq))
return _ppq.pq_head[i];
return NULL;
}
PCB* pq_peek(process_queue* pq) {
assert(pq != NULL);
return pq->head;
}
MessageEnvelope* mq_peek(message_queue* mq) {
assert(mq != NULL);
return mq->head;
}
msg_event* trace_peek(trace_buffer* tq, enum Event type) {
assert(tq != NULL);
if (type == SEND)
return tq->send;
else
return tq->receive;
}
//Queue removes
//return target or NULL of target not contained
//if contained, remove target from queue
//iterate through queue until target is encountered, juggle references
PCB* ppq_remove(PCB* target, priority_process_queue* ppq) {
assert(ppq != NULL);
priority_process_queue _ppq = (*ppq);
int priority = target->priority;
PCB* t = _ppq.pq_head[priority];
if (target == _ppq.pq_head[priority]) {
_ppq.pq_head[priority] = _ppq.pq_head[priority]->q_next;
return target;
}
while (t->q_next != NULL) {
if (t->q_next == target) {
if (_ppq.pq_tail[priority] == t->q_next) {
_ppq.pq_tail[priority] = t;
t->q_next = NULL;
return target;
}
t->q_next = (t->q_next)->q_next;
return target;
} else {
t = t->q_next;
}
}
return NULL;
}
PCB* pq_remove(PCB* target, process_queue* pq) {
assert(target != NULL && pq != NULL);
PCB* next = pq->head;
if (target == pq->head) {
pq->head = (pq->head)->p_next;
return target;
}
while (next->p_next != target && next->p_next != NULL) {
next = next->p_next;
}
if (next->p_next == target) {
if (next->p_next == pq->tail) {
pq->tail = next;
next->p_next = NULL;
return target;
}
next->p_next = (next->p_next)->p_next;
return target;
} else { return NULL; }
}
MessageEnvelope* mq_remove(MessageEnvelope* target, message_queue* mq) {
assert(target != NULL && mq != NULL);
MessageEnvelope* next = mq->head;
if (mq_is_empty(mq)) return NULL;
if (target == mq->head) {
mq->head = (mq->head)->next;
return target;
}
while (next->next != target && next->next != NULL) {
next = next->next;
}
if (next->next == target) {
if (next->next == mq->tail) {
mq->tail = next;
next->next = NULL;
return target;
}
next->next = (next->next)->next;
return target;
} else {
return NULL;
}
}
//Queue deallocators
//called at cleanup
void mq_free(message_queue* mq) {
assert(mq != NULL);
MessageEnvelope* env = NULL;
while (!mq_is_empty(mq)) {
env = mq_dequeue(mq);
assert(env != NULL);
free(env);
}
free(mq);
}
void pq_free(process_queue** pq) {
assert((*pq) != NULL);
PCB* next = (*pq)->head;
PCB* store;
while (next != NULL) {
store = next->p_next;
//free stack
assert(next->stack_head != NULL);
free(next->stack_head);
assert(next->message_send != NULL && next->message_receive != NULL);
mq_free(next->message_send);
mq_free(next->message_receive);
free(next);
next = store;
}
(*pq)->head = NULL;
(*pq)->tail = NULL;
free(*pq);
*pq = NULL;
}
int ppq_free(priority_process_queue* ppq) {
assert(ppq != NULL);
//assume all PCBs have been safely deallocated, only free queue memory
//TODO: make less retarded
free(ppq);
return 0;
}
void trace_free(trace_buffer** tq) {
assert (*tq != NULL);
msg_event* tmp = (*tq)->send;
msg_event* next = NULL;
while (tmp != NULL) {
next = tmp->next;
free(tmp);
tmp = next;
}
tmp = (*tq)->receive;
next = NULL;
while (tmp != NULL) {
next = tmp->next;
free(tmp);
tmp = next;
}
free(*tq);
*tq = NULL;
}