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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*C
(c) 2005 bl0rg.net
**/
#include "conf.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "fec-group.h"
/*M
\emph{Maximal number of elements in the ring buffer.}
**/
unsigned int fec_rb_size = 0;
/*M
\emph{Index of first valid element in ring buffer.}
**/
static unsigned int fec_rb_start = 0;
/*M
\emph{Index of first invalid element in ring buffer.}
**/
static unsigned int fec_rb_end = 0;
/*M
\emph{Number of elements in the ring buffer.}
**/
unsigned int fec_rb_cnt = 0;
/*M
\emph{Ring buffer array.}
**/
fec_group_t *fec_rb = NULL;
void fec_rb_clear(void) {
fec_rb_start = 0;
fec_rb_end = 0;
fec_rb_cnt = 0;
int i;
for (i = 0; i < fec_rb_size; i++)
fec_group_clear(fec_rb + i);
}
void fec_rb_pop(void) {
assert(fec_rb != NULL);
assert(fec_rb_end != fec_rb_start);
fec_group_destroy(fec_rb + fec_rb_start);
fec_rb_start = (fec_rb_start + 1) % fec_rb_size;
fec_rb_cnt--;
}
void fec_rb_destroy(void) {
if (fec_rb != NULL) {
while (fec_rb_cnt)
fec_rb_pop();
free(fec_rb);
fec_rb = NULL;
}
fec_rb_size = 0;
fec_rb_clear();
}
void fec_rb_init(unsigned int size) {
fec_rb_destroy();
fec_rb = malloc(sizeof(fec_group_t) * size);
assert(fec_rb != NULL);
int i;
for (i = 0; i < fec_rb_size; i++)
fec_group_clear(fec_rb + i);
fec_rb_size = size;
fec_rb_clear();
}
unsigned int fec_rb_length(void) {
assert(fec_rb != NULL);
if (fec_rb_end >= fec_rb_start)
return fec_rb_end - fec_rb_start;
else
return fec_rb_end + (fec_rb_size - fec_rb_start);
}
int fec_rb_insert_pkt(fec_pkt_t *pkt, int idx) {
assert(pkt != NULL);
assert(fec_rb != NULL);
#ifdef DEBUG
fprintf(stderr, "insert packet at idx %d, gseq %d, pseq %d\n",
idx, pkt->hdr.group_seq, pkt->hdr.packet_seq);
#endif
if (idx < 0) {
/* try to grow the buffer downwards */
if ((fec_rb_length() - idx) <= fec_rb_size) {
fec_rb_start = (fec_rb_start + idx) % fec_rb_size;
idx = 0;
} else
/* drop the packet silently */
return 0;
} else if (idx >= fec_rb_size - 1) {
/* not enough place left in ring buffer */
return 0;
}
fec_group_t *dst_group = fec_rb + ((idx + fec_rb_start) % fec_rb_size);
if (dst_group->buf != NULL) {
assert(dst_group->seq == pkt->hdr.group_seq);
fec_group_insert_pkt(dst_group, pkt);
} else {
assert(dst_group->pkts == NULL);
fec_group_init(dst_group,
pkt->hdr.fec_k,
pkt->hdr.fec_n,
pkt->hdr.group_seq,
pkt->hdr.group_tstamp,
pkt->hdr.fec_len);
fec_group_insert_pkt(dst_group, pkt);
fec_rb_cnt++;
}
#ifdef DEBUG
fprintf(stderr, "packet inserted at %d, end: %d\n",
(idx + fec_rb_start) % fec_rb_size,
fec_rb_end);
#endif
/* adjust the end pointer */
if (idx >= fec_rb_length())
fec_rb_end = ((idx + fec_rb_start + 1) % fec_rb_size);
assert(fec_rb_start != fec_rb_end);
/* XXX kann anscheinend auch passieren wenn wraparound. Generell
muessen hier alle asserts abgefangen werden und mit einer error
struktur sauber verarbeitet werden. */
assert(fec_rb[fec_rb_end].buf == NULL);
return 1;
}
void fec_rb_print(void) {
assert(fec_rb != NULL);
fprintf(stderr, "start: %.3u, end: %.3u, len: %.3u\n",
fec_rb_start, fec_rb_end, fec_rb_length());
}
void fec_rb_print_rb(void) {
unsigned int i;
for (i = fec_rb_start; i != fec_rb_end; i = (i + 1) % fec_rb_size) {
if (fec_rb[i].buf != NULL)
fprintf(stderr, "%.3u: seq %.3u\n", i, fec_rb[i].seq);
}
}
fec_group_t *fec_rb_first(void) {
assert(fec_rb != NULL);
if (fec_rb_start == fec_rb_end)
return NULL;
/* first should always be the first in buffer */
return fec_rb + fec_rb_start;
}
|