blob: d018ce36d1ed8269bbd5c165ea037cb69d28aa62 (
plain)
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
|
/* This file is part of tagr.
Copyright (C) 2005, 2009 Sergey Poznyakoff
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <tagr.h>
void
queue_clear (queue_t *q)
{
q->head = q->tail = 0;
}
int
queue_count (queue_t *q)
{
int l;
l = q->tail - q->head;
if (l < 0)
l += q->size;
return l;
}
int
queue_is_full (queue_t *q)
{
return queue_count(q) == q->size - 1;
}
struct traffic_history *
queue_get_ptr (queue_t *q, int index)
{
if (q->head == q->tail || index < 0 || index >= queue_count (q))
return NULL;
return &q->queue[(q->head + index) % q->size];
}
struct traffic_history *
queue_get_tail (queue_t *q)
{
int n;
if (q->head == q->tail)
return NULL;
n = q->tail - 1;
if (n < 0)
n += q->size;
return &q->queue[n];
}
/* Insert an entry at the tail of the queue */
void
queue_put (queue_t *q, struct traffic_history *elt)
{
if (queue_is_full (q))
q->head = (q->head + 1) % q->size;
q->queue[q->tail] = *elt;
q->tail = (q->tail + 1) % q->size;
}
/* Insert an entry at the tail of the queue.
If the tail already contained an element, exchange it with the new one.
Return 1 if an old element was stored in ELT, 0 otherwise. */
int
queue_xchg (queue_t *q, struct traffic_history *elt)
{
struct traffic_history tmp;
int rc = queue_is_full (q);
if (rc)
q->head = (q->head + 1) % q->size;
q->tail %= q->size;
tmp = q->queue[q->tail];
q->queue[q->tail++] = *elt;
if (rc)
*elt = tmp;
return rc;
}
|