-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
51 lines (41 loc) · 1.25 KB
/
queue.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
/* Very simple queue
* * These are FIFO queues which discard the new data when full.
* *
* * Queue is empty when in == out.
* * If in != out, then
* * - items are placed into in before incrementing in
* * - items are removed from out before incrementing out
* * Queue is full when in == (out-1 + QUEUE_SIZE) % QUEUE_SIZE;
* *
* * The queue will hold QUEUE_ELEMENTS number of items before the
* * calls to QueuePut fail.
* */
#include "queue.h"
void QueueInit(Queue_t *q ) {
q->QueueIn = q->QueueOut = 0;
}
int QueuePut(Queue_t *q,void *new)
{
if(q->QueueIn == (( q->QueueOut - 1 + QUEUE_SIZE) % QUEUE_SIZE))
{
return -1; /* Queue Full*/
}
q->Queue[q->QueueIn] = new;
q->QueueIn = (q->QueueIn + 1) % QUEUE_SIZE;
return 0; // No errors
}
int QueueGet(Queue_t *q,void **old)
{
if(q->QueueIn == q->QueueOut)
{
return -1; /* Queue Empty - nothing to get*/
}
*old = q->Queue[q->QueueOut];
q->QueueOut = (q->QueueOut + 1) % QUEUE_SIZE;
return 0; // No errors
}
int QueueCount(Queue_t *q ) {
int diff = q->QueueIn - q->QueueOut;
diff += ( 0 > diff ) ? QUEUE_SIZE : 0;
return diff;
}