-
Notifications
You must be signed in to change notification settings - Fork 0
/
TxRxQueues.c
47 lines (42 loc) · 1006 Bytes
/
TxRxQueues.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
/*
* Desc: Circular FIFO queues.
* Author: Prangon Das ([email protected])
*/
#include "TxRxQueues.h"
#define FAIL (0)
#define SUCCESS (1)
void init_Queue(struct qRecord *record, char *buffer, int capacity)
{
record->inPointer = buffer;
record->outPointer = buffer;
record->bufferStart = buffer;
record->bufferEnd = buffer + capacity;
record->bufferSize = capacity;
record->numEnqueued = 0;
}
int enqueue(char character, struct qRecord *record)
{
if (record->numEnqueued < record->bufferSize) {
*record->inPointer++ = character;
if (record->inPointer >= record->bufferEnd) {
record->inPointer = record->bufferStart;
}
record->numEnqueued++;
} else {
return FAIL;
}
return SUCCESS;
}
int dequeue(char *character, struct qRecord *record)
{
if(record->numEnqueued != 0){
*character = *record->outPointer++;
if (record->outPointer >= record->bufferEnd) {
record->outPointer = record->bufferStart;
}
record->numEnqueued--;
}else{
return FAIL;
}
return SUCCESS;
}