| 1 | /* |
| 2 | * Copyright (C) 1998,1999,2001 Ross Combs ([email protected]) |
| 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 | #include "common/setup_before.h" |
| 19 | #define QUEUE_INTERNAL_ACCESS |
| 20 | #include "common/queue.h" |
| 21 | |
| 22 | #include <cstring> |
| 23 | |
| 24 | #include "common/packet.h" |
| 25 | #include "common/eventlog.h" |
| 26 | #include "common/xalloc.h" |
| 27 | #include "common/setup_after.h" |
| 28 | |
| 29 | #define QUEUE_QUANTUM 10 /* allocate ring buffer slots for 10 packets at once */ |
| 30 | |
| 31 | namespace pvpgn |
| 32 | { |
| 33 | |
| 34 | extern t_packet * queue_pull_packet(t_queue * * queue) |
| 35 | { |
| 36 | t_queue * temp; |
| 37 | t_packet * packet; |
| 38 | |
| 39 | // eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue {:p}", queue); |
| 40 | if (!queue) |
| 41 | { |
| 42 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL queue pointer"); |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | temp = *queue; |
| 47 | |
| 48 | if (!temp || !temp->ulen) |
| 49 | return NULL; |
| 50 | |
| 51 | // eventlog(eventlog_level_debug, __FUNCTION__, "getting element from tail ({}/{} head/tail {}/{})", temp->alen, temp->ulen, temp->head, temp->tail); |
| 52 | /* getting entry from tail and updating queue */ |
| 53 | packet = temp->ring[temp->tail]; |
| 54 | temp->tail = (temp->tail + 1) % temp->alen; |
| 55 | temp->ulen--; |
| 56 | // eventlog(eventlog_level_debug, __FUNCTION__, "read {:p} element from tail ({}/{} head/tail {}/{})", packet, temp->alen, temp->ulen, temp->head, temp->tail); |
| 57 | |
| 58 | if (!packet) |
| 59 | { |
| 60 | eventlog(eventlog_level_error, __FUNCTION__, "NULL packet in queue"); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | return packet; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | extern t_packet * queue_peek_packet(t_queue const * const * queue) |
| 69 | { |
| 70 | t_packet * packet; |
| 71 | |
| 72 | // eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue {:p}", queue); |
| 73 | if (!queue) |
| 74 | { |
| 75 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL queue pointer"); |
| 76 | return NULL; |
| 77 | } |
| 78 | if (!*queue || !(*queue)->ulen) |
| 79 | return NULL; |
| 80 | |
| 81 | packet = (*queue)->ring[(*queue)->tail]; |
| 82 | |
| 83 | if (!packet) |
| 84 | { |
| 85 | eventlog(eventlog_level_error, __FUNCTION__, "NULL packet in queue"); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | return packet; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | extern void queue_push_packet(t_queue * * queue, t_packet * packet) |
| 94 | { |
| 95 | t_queue * temp; |
| 96 | void *ptr; |
| 97 | |
| 98 | // eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue {:p} packet {:p}", queue, packet); |
| 99 | if (!queue) |
| 100 | { |
| 101 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL queue pointer"); |
| 102 | return; |
| 103 | } |
| 104 | if (!packet) |
| 105 | { |
| 106 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | temp = *queue; |
| 111 | |
| 112 | if (!temp) |
| 113 | { |
| 114 | // eventlog(eventlog_level_debug, __FUNCTION__, "queue is NULL , initilizing"); |
| 115 | temp = (t_queue*)xmalloc(sizeof(t_queue)); |
| 116 | temp->alen = temp->ulen = 0; |
| 117 | temp->ring = NULL; |
| 118 | temp->head = temp->tail = 0; |
| 119 | *queue = temp; |
| 120 | } |
| 121 | |
| 122 | if (temp->ulen == temp->alen) { /* ring queue is full, need to allocate some memory */ |
| 123 | /* FIXME: find a solution |
| 124 | if (temp->alen) |
| 125 | eventlog(eventlog_level_error, __FUNCTION__, "queue is full (resizing) (oldsize: {})", temp->alen); |
| 126 | */ |
| 127 | |
| 128 | ptr = xrealloc(temp->ring, sizeof(t_packet *)* (temp->alen + QUEUE_QUANTUM)); |
| 129 | temp->ring = (t_packet **)ptr; |
| 130 | temp->alen += QUEUE_QUANTUM; |
| 131 | |
| 132 | // eventlog(eventlog_level_debug, __FUNCTION__, "queue new size {}/{} head/tail {}/{}", temp->alen, temp->ulen, temp->head, temp->tail); |
| 133 | if (temp->head) { |
| 134 | unsigned moved; |
| 135 | |
| 136 | moved = (QUEUE_QUANTUM <= temp->head) ? QUEUE_QUANTUM : temp->head; |
| 137 | std::memmove(temp->ring + temp->ulen, temp->ring, sizeof(t_packet *)* moved); |
| 138 | if (temp->head > QUEUE_QUANTUM) { |
| 139 | std::memmove(temp->ring, temp->ring + moved, sizeof(t_packet *)* (temp->head - moved)); |
| 140 | temp->head -= moved; |
| 141 | } |
| 142 | else if (temp->head < QUEUE_QUANTUM) |
| 143 | temp->head = temp->ulen + moved; |
| 144 | else temp->head = 0; |
| 145 | } |
| 146 | else temp->head = temp->ulen; |
| 147 | |
| 148 | } |
| 149 | |
| 150 | temp->ring[temp->head] = packet_add_ref(packet); |
| 151 | |
| 152 | temp->head = (temp->head + 1) % temp->alen; |
| 153 | temp->ulen++; |
| 154 | // eventlog(eventlog_level_debug, __FUNCTION__, "packet added ({}/{} head/tail {}/{})", temp->alen, temp->ulen, temp->head, temp->tail); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | extern int queue_get_length(t_queue const * const * queue) |
| 159 | { |
| 160 | // eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue {:p}", queue); |
| 161 | if (!queue) |
| 162 | { |
| 163 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL queue pointer"); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | if (*queue == NULL) return 0; |
| 168 | |
| 169 | return (*queue)->ulen; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | extern void queue_clear(t_queue * * queue) |
| 174 | { |
| 175 | t_packet * temp; |
| 176 | |
| 177 | // eventlog(eventlog_level_debug, __FUNCTION__, "entered: queue {:p}", queue); |
| 178 | if (!queue) |
| 179 | { |
| 180 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL queue pointer"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (*queue) { |
| 185 | while ((temp = queue_pull_packet(queue))) |
| 186 | packet_del_ref(temp); |
| 187 | |
| 188 | if ((*queue)->ring) xfree((void*)((*queue)->ring)); |
| 189 | xfree((void*)(*queue)); |
| 190 | /* poison the queue, this should make invalid |
| 191 | * accessed queues crash earlier */ |
| 192 | *queue = 0; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | } |