| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 1998,1999,2001 Ross Combs (rocombs@cs.nmsu.edu)
|
| 3 |
|
|
*
|
| 4 |
|
|
* This program is free software; you can redistribute it and/or
|
| 5 |
|
|
* modify it under the terms of the GNU General Public License
|
| 6 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 7 |
|
|
* of the License, or (at your option) any later version.
|
| 8 |
|
|
*
|
| 9 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
|
|
* GNU General Public License for more details.
|
| 13 |
|
|
*
|
| 14 |
|
|
* You should have received a copy of the GNU General Public License
|
| 15 |
|
|
* along with this program; if not, write to the Free Software
|
| 16 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
|
|
*/
|
| 18 |
|
|
#define QUEUE_INTERNAL_ACCESS
|
| 19 |
|
|
#include "common/setup_before.h"
|
| 20 |
|
|
#ifdef HAVE_STDDEF_H
|
| 21 |
|
|
# include <stddef.h>
|
| 22 |
|
|
#else
|
| 23 |
|
|
# ifndef NULL
|
| 24 |
|
|
# define NULL ((void *)0)
|
| 25 |
|
|
# endif
|
| 26 |
|
|
#endif
|
| 27 |
|
|
#ifdef STDC_HEADERS
|
| 28 |
|
|
# include <stdlib.h>
|
| 29 |
|
|
#else
|
| 30 |
|
|
# ifdef HAVE_MALLOC_H
|
| 31 |
|
|
# include <malloc.h>
|
| 32 |
|
|
# endif
|
| 33 |
|
|
#endif
|
| 34 |
|
|
#ifdef HAVE_STRING_H
|
| 35 |
|
|
# include <string.h>
|
| 36 |
|
|
#else
|
| 37 |
|
|
# ifdef HAVE_STRINGS_H
|
| 38 |
|
|
# include <strings.h>
|
| 39 |
|
|
# endif
|
| 40 |
|
|
# ifdef HAVE_MEMORY_H
|
| 41 |
|
|
# include <memory.h>
|
| 42 |
|
|
# endif
|
| 43 |
|
|
#endif
|
| 44 |
|
|
#include "common/packet.h"
|
| 45 |
|
|
#include "common/eventlog.h"
|
| 46 |
|
|
#include "common/xalloc.h"
|
| 47 |
|
|
#include "common/queue.h"
|
| 48 |
|
|
#include "common/setup_after.h"
|
| 49 |
|
|
|
| 50 |
|
|
#define QUEUE_QUANTUM 10 /* allocate ring buffer slots for 10 packets at once */
|
| 51 |
|
|
|
| 52 |
|
|
extern t_packet * queue_pull_packet(t_queue * * queue)
|
| 53 |
|
|
{
|
| 54 |
|
|
t_queue * temp;
|
| 55 |
|
|
t_packet * packet;
|
| 56 |
|
|
|
| 57 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue %p", queue);
|
| 58 |
|
|
if (!queue)
|
| 59 |
|
|
{
|
| 60 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL queue pointer");
|
| 61 |
|
|
return NULL;
|
| 62 |
|
|
}
|
| 63 |
|
|
|
| 64 |
|
|
temp = *queue;
|
| 65 |
|
|
|
| 66 |
|
|
if (!temp || !temp->ulen)
|
| 67 |
|
|
return NULL;
|
| 68 |
|
|
|
| 69 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "getting element from tail (%d/%d head/tail %d/%d)", temp->alen, temp->ulen, temp->head, temp->tail);
|
| 70 |
|
|
/* getting entry from tail and updating queue */
|
| 71 |
|
|
packet = temp->ring[temp->tail];
|
| 72 |
|
|
temp->tail = (temp->tail + 1) % temp->alen;
|
| 73 |
|
|
temp->ulen--;
|
| 74 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "read %p element from tail (%d/%d head/tail %d/%d)", packet, temp->alen, temp->ulen, temp->head, temp->tail);
|
| 75 |
|
|
|
| 76 |
|
|
if (!packet)
|
| 77 |
|
|
{
|
| 78 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"NULL packet in queue");
|
| 79 |
|
|
return NULL;
|
| 80 |
|
|
}
|
| 81 |
|
|
|
| 82 |
|
|
return packet;
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
|
| 86 |
|
|
extern t_packet * queue_peek_packet(t_queue const * const * queue)
|
| 87 |
|
|
{
|
| 88 |
|
|
t_packet * packet;
|
| 89 |
|
|
|
| 90 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue %p", queue);
|
| 91 |
|
|
if (!queue)
|
| 92 |
|
|
{
|
| 93 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL queue pointer");
|
| 94 |
|
|
return NULL;
|
| 95 |
|
|
}
|
| 96 |
|
|
if (!*queue || !(*queue)->ulen)
|
| 97 |
|
|
return NULL;
|
| 98 |
|
|
|
| 99 |
|
|
packet = (*queue)->ring[(*queue)->tail];
|
| 100 |
|
|
|
| 101 |
|
|
if (!packet)
|
| 102 |
|
|
{
|
| 103 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"NULL packet in queue");
|
| 104 |
|
|
return NULL;
|
| 105 |
|
|
}
|
| 106 |
|
|
|
| 107 |
|
|
return packet;
|
| 108 |
|
|
}
|
| 109 |
|
|
|
| 110 |
|
|
|
| 111 |
|
|
extern void queue_push_packet(t_queue * * queue, t_packet * packet)
|
| 112 |
|
|
{
|
| 113 |
|
|
t_queue * temp;
|
| 114 |
|
|
void *ptr;
|
| 115 |
|
|
|
| 116 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue %p packet %p", queue, packet);
|
| 117 |
|
|
if (!queue)
|
| 118 |
|
|
{
|
| 119 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL queue pointer");
|
| 120 |
|
|
return;
|
| 121 |
|
|
}
|
| 122 |
|
|
if (!packet)
|
| 123 |
|
|
{
|
| 124 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL packet");
|
| 125 |
|
|
return;
|
| 126 |
|
|
}
|
| 127 |
|
|
|
| 128 |
|
|
temp = *queue;
|
| 129 |
|
|
|
| 130 |
|
|
if (!temp)
|
| 131 |
|
|
{
|
| 132 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "queue is NULL , initilizing");
|
| 133 |
|
|
temp = xmalloc(sizeof(t_queue));
|
| 134 |
|
|
temp->alen = temp->ulen = 0;
|
| 135 |
|
|
temp->ring = NULL;
|
| 136 |
|
|
temp->head = temp->tail = 0;
|
| 137 |
|
|
*queue = temp;
|
| 138 |
|
|
}
|
| 139 |
|
|
|
| 140 |
|
|
if (temp->ulen == temp->alen) { /* ring queue is full, need to allocate some memory */
|
| 141 |
|
|
/* FIXME: find a solution
|
| 142 |
|
|
if (temp->alen)
|
| 143 |
|
|
eventlog(eventlog_level_error, __FUNCTION__, "queue is full (resizing) (oldsize: %u)", temp->alen);
|
| 144 |
|
|
*/
|
| 145 |
|
|
|
| 146 |
|
|
ptr = xrealloc(temp->ring, sizeof(t_packet *) * (temp->alen + QUEUE_QUANTUM));
|
| 147 |
|
|
temp->ring = (t_packet **)ptr;
|
| 148 |
|
|
temp->alen += QUEUE_QUANTUM;
|
| 149 |
|
|
|
| 150 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "queue new size %d/%d head/tail %d/%d", temp->alen, temp->ulen, temp->head, temp->tail);
|
| 151 |
|
|
if (temp->head) {
|
| 152 |
|
|
unsigned moved;
|
| 153 |
|
|
|
| 154 |
|
|
moved = (QUEUE_QUANTUM <= temp->head) ? QUEUE_QUANTUM : temp->head;
|
| 155 |
|
|
memmove(temp->ring + temp->ulen, temp->ring, sizeof(t_packet *) * moved);
|
| 156 |
|
|
if (temp->head > QUEUE_QUANTUM) {
|
| 157 |
|
|
memmove(temp->ring, temp->ring + moved, sizeof(t_packet *) * (temp->head - moved));
|
| 158 |
|
|
temp->head -= moved;
|
| 159 |
|
|
} else if (temp->head < QUEUE_QUANTUM)
|
| 160 |
|
|
temp->head = temp->ulen + moved;
|
| 161 |
|
|
else temp->head = 0;
|
| 162 |
|
|
} else temp->head = temp->ulen;
|
| 163 |
|
|
|
| 164 |
|
|
}
|
| 165 |
|
|
|
| 166 |
|
|
temp->ring[temp->head] = packet_add_ref(packet);
|
| 167 |
|
|
|
| 168 |
|
|
temp->head = (temp->head + 1) % temp->alen;
|
| 169 |
|
|
temp->ulen++;
|
| 170 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "packet added (%d/%d head/tail %d/%d)", temp->alen, temp->ulen, temp->head, temp->tail);
|
| 171 |
|
|
}
|
| 172 |
|
|
|
| 173 |
|
|
|
| 174 |
|
|
extern int queue_get_length(t_queue const * const * queue)
|
| 175 |
|
|
{
|
| 176 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue %p", queue);
|
| 177 |
|
|
if (!queue)
|
| 178 |
|
|
{
|
| 179 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL queue pointer");
|
| 180 |
|
|
return 0;
|
| 181 |
|
|
}
|
| 182 |
|
|
|
| 183 |
|
|
if (*queue == NULL) return 0;
|
| 184 |
|
|
|
| 185 |
|
|
return (*queue)->ulen;
|
| 186 |
|
|
}
|
| 187 |
|
|
|
| 188 |
|
|
|
| 189 |
|
|
extern void queue_clear(t_queue * * queue)
|
| 190 |
|
|
{
|
| 191 |
|
|
t_packet * temp;
|
| 192 |
|
|
|
| 193 |
|
|
// eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue %p", queue);
|
| 194 |
|
|
if (!queue)
|
| 195 |
|
|
{
|
| 196 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL queue pointer");
|
| 197 |
|
|
return;
|
| 198 |
|
|
}
|
| 199 |
|
|
|
| 200 |
|
|
if (*queue) {
|
| 201 |
|
|
while ((temp = queue_pull_packet(queue)))
|
| 202 |
|
|
packet_del_ref(temp);
|
| 203 |
|
|
|
| 204 |
|
|
if ((*queue)->ring) xfree((void*)((*queue)->ring));
|
| 205 |
|
|
xfree((void*)(*queue));
|
| 206 |
|
|
}
|
| 207 |
|
|
}
|