-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_fifo.c
84 lines (64 loc) · 1.77 KB
/
simple_fifo.c
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
/*
* simple_fifo.c
*
* Created on: Mar 1, 2011
* Author: wibble
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simple_fifo.h"
int fifo_init(struct simple_fifo *fifo, long int size) {
int ret = 0;
fifo->size = size;
fifo->base = malloc(size);
if (fifo->base == NULL) {
ret = EFIFO_MEM;
}
fifo->tail = fifo->base+size;
fifo->head = fifo->tail;
return(ret);
}
int fifo_write(struct simple_fifo *fifo, char *new_data, long int new_size) {
long int span = fifo->size - new_size; // Amount of data to save
long int shift = fifo->head - fifo->base; // Current read point
// printf("fifo_write(): have %li, adding %li.\n", fifo->tail-fifo->head, new_size);fflush(stdout);
if (new_size >= shift) {
memmove(fifo->base, fifo->base+new_size, span);
memmove(fifo->base+span, new_data, new_size);
fifo->head = fifo->base;
} else {
memmove(fifo->head-new_size, fifo->head, fifo->tail-fifo->head);
memmove(fifo->tail-new_size, new_data, new_size);
fifo->head = fifo->head - new_size;
}
return(0);
}
long int fifo_read(char *out, struct simple_fifo *fifo, long int bytes) {
long int ret = 0;
void *pret;
// printf("fifo_read(): want %li, have %li.\n", bytes, fifo_avail(fifo));fflush(stdout);
if (bytes <= fifo_avail(fifo)) {
if (out == NULL) out = malloc(bytes); // Malloc if out is new
pret = memmove(out, fifo->head, bytes); // Move data
if (pret != NULL) {
fifo->head += bytes;
ret = bytes;
}
}
return(ret);
}
int fifo_kill(struct simple_fifo *fifo, long int bytes) {
int ret = 0;
if (bytes <= fifo_avail(fifo)) {
fifo->head += bytes;
ret = bytes;
}
return(ret);
}
void fifo_destroy(struct simple_fifo *fifo) {
free(fifo->base);
}
long int fifo_avail(struct simple_fifo *fifo) {
return(fifo->tail - fifo->head);
}