blob: acac2614f76a2033afa5ed3b5605069a6c87b4b6 (
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
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
|
/*---------------------------------------------------------------------------*\
FILE........: fifo.c
AUTHOR......: David Rowe
DATE CREATED: Oct 15 2012
A FIFO design useful in gluing the FDMDV modem and codec together in
integrated applications. The unittest/tfifo indicates these
routines are thread safe without the need for syncronisation
object, e.g. a different thread can read and write to a fifo at the
same time.
\*---------------------------------------------------------------------------*/
/*
Copyright (C) 2012 David Rowe
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1, as
published by the Free Software Foundation. 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 Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "codec2_fifo.h"
struct FIFO {
short *buf;
short *pin;
short *pout;
int nshort;
};
struct FIFO *fifo_create(int nshort) {
struct FIFO *fifo;
fifo = (struct FIFO *)malloc(sizeof(struct FIFO));
assert(fifo != NULL);
fifo->buf = (short*)malloc(sizeof(short)*nshort);
assert(fifo->buf != NULL);
fifo->pin = fifo->buf;
fifo->pout = fifo->buf;
fifo->nshort = nshort;
return fifo;
}
void fifo_destroy(struct FIFO *fifo) {
assert(fifo != NULL);
free(fifo->buf);
free(fifo);
}
int fifo_write(struct FIFO *fifo, short data[], int n) {
int i;
int fifo_free;
short *pdata;
short *pin = fifo->pin;
assert(fifo != NULL);
assert(data != NULL);
// available storage is one less than nshort as prd == pwr
// is reserved for empty rather than full
fifo_free = fifo->nshort - fifo_used(fifo) - 1;
if (n > fifo_free) {
return -1;
}
else {
/* This could be made more efficient with block copies
using memcpy */
pdata = data;
for(i=0; i<n; i++) {
*pin++ = *pdata++;
if (pin == (fifo->buf + fifo->nshort))
pin = fifo->buf;
}
fifo->pin = pin;
}
return 0;
}
int fifo_read(struct FIFO *fifo, short data[], int n)
{
int i;
short *pdata;
short *pout = fifo->pout;
assert(fifo != NULL);
assert(data != NULL);
if (n > fifo_used(fifo)) {
return -1;
}
else {
/* This could be made more efficient with block copies
using memcpy */
pdata = data;
for(i=0; i<n; i++) {
*pdata++ = *pout++;
if (pout == (fifo->buf + fifo->nshort))
pout = fifo->buf;
}
fifo->pout = pout;
}
return 0;
}
int fifo_used(struct FIFO *fifo)
{
short *pin = fifo->pin;
short *pout = fifo->pout;
unsigned int used;
assert(fifo != NULL);
if (pin >= pout)
used = pin - pout;
else
used = fifo->nshort + (unsigned int)(pin - pout);
return used;
}
|