blob: cf68cdac7527e24f7bbdc74af7d150acabd4c3c3 (
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
|
//fifo.h
// Simple fifo
// http://stratifylabs.co/embedded%20design%20tips/2013/10/02/Tips-A-FIFO-Buffer-Implementation/
#include <stdio.h>
#include <stdlib.h>
#define __STDC_FORMAT_MACROS // Required for format specifiers
#include <inttypes.h>
#include "fifo.h"
//This initializes the FIFO structure with the given buffer and size
void fifo_init(fifo_t * f, uchar * buf, int size) {
f->head = 0;
f->tail = 0;
f->size = size;
f->buf = buf;
}
//This reads nbytes bytes from the FIFO
//The number of bytes read is returned
int fifo_read(fifo_t * f, uchar * buf, int nbytes) {
int i;
uchar * p;
p = buf;
for(i=0; i < nbytes; i++){
if( f->tail != f->head ){ //see if any data is available
*p++ = f->buf[f->tail]; //grab a byte from the buffer
f->tail++; //increment the tail
if( f->tail == f->size ){ //check for wrap-around
f->tail = 0;
}
} else {
return i; //number of bytes read
}
}
return nbytes;
}
//This writes up to nbytes bytes to the FIFO
//If the head runs in to the tail, not all bytes are written
//The number of bytes written is returned
int fifo_write(fifo_t * f, const uchar * buf, int nbytes) {
int i;
const uchar * p;
p = buf;
for(i=0; i < nbytes; i++) {
//first check to see if there is space in the buffer
if ( (f->head + 1 == f->tail) ||
( (f->head + 1 == f->size) && (f->tail == 0) )) {
return i; //no more room
} else {
f->buf[f->head] = *p++;
f->head++; //increment the head
if( f->head == f->size ){ //check for wrap-around
f->head = 0;
}
}
}
return nbytes;
}
// --------- $Id: vdr-convert,v 1.3 2016/09/01 13:01:48 richard Exp $ ---------- END
|