-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstream_queue.c
executable file
·97 lines (82 loc) · 1.72 KB
/
stream_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
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
#include <stdlib.h>
#include "comm.h"
#include "stream_queue.h"
struct stream_queue *streamq_alloc(int pktsiz, int nbpkts)
{
struct stream_queue *q;
if (pktsiz <= 0 || nbpkts <= 0)
return NULL;
q = (struct stream_queue *)calloc(1, sizeof(struct stream_queue) + pktsiz * nbpkts + sizeof(int) * nbpkts);
if (!q)
{
err("alloc memory failed for stream_queue\n");
return NULL;
}
q->pktsiz = pktsiz;
q->nbpkts = nbpkts;
q->pktlen = (int *)(((char *)q) + sizeof(struct stream_queue));
q->buf = (char *)(((char *)q) + sizeof(struct stream_queue) + sizeof(int) * nbpkts);
return q;
}
int streamq_query(struct stream_queue *q, int index, char **ppacket, int **ppktlen)
{
if (!q || index >= q->nbpkts)
return -1;
if (ppacket)
*ppacket = q->buf + index * q->pktsiz;
if (ppktlen)
*ppktlen = &q->pktlen[index];
return 0;
}
int streamq_inused(struct stream_queue *q, int index)
{
if (!q)
return -1;
if ((q->head <= index && index < q->tail) || (q->head > q->tail && (index >= q->head || index < q->tail)))
return 1;
return 0;
}
int streamq_next(struct stream_queue *q, int index)
{
if (!q)
return -1;
index = (index + 1) % q->nbpkts;
return index;
}
int streamq_head(struct stream_queue *q)
{
if (!q)
return -1;
return q->head;
}
int streamq_tail(struct stream_queue *q)
{
if (!q)
return -1;
return q->tail;
}
int streamq_push(struct stream_queue *q)
{
if (!q)
return -1;
if ((q->tail + 1) % q->nbpkts == q->head)
return -1;
q->tail = (q->tail + 1) % q->nbpkts;
return q->tail;
}
int streamq_pop(struct stream_queue *q)
{
if (!q)
return -1;
if (q->head == q->tail)
return -1;
q->head = (q->head + 1) % q->nbpkts;
return q->head;
}
void streamq_free(struct stream_queue *q)
{
if (q)
{
free(q);
}
}