-
Notifications
You must be signed in to change notification settings - Fork 1
/
fifo.c
147 lines (135 loc) · 4.84 KB
/
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
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
/* Generic FIFO buffer Implementation
Copyright (C) 2014 Jesus Ruben Santa Anna Zamudio.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author website: http://geekfactory.mx
Author e-mail: ruben at geekfactory dot mx
*/
#include "FIFO.h"
#define true 1
#define false 0
/*-------------------------------------------------------------*
* Private function prototypes *
*-------------------------------------------------------------*/
static void fifo_copy_from(fifo_t, void *);
static void fifo_copy_to(fifo_t, const void *);
/*-------------------------------------------------------------*
* Public API implementation *
*-------------------------------------------------------------*/
fifo_t fifo_create(uint16_t count, size_t size)
{
fifo_t newfifo;
if (count > 0) {
newfifo = (struct fifo_descriptor *) vm_malloc(sizeof(struct fifo_descriptor));
if (newfifo != NULL) {
// Calculate the size in bytes of the buffer
size_t bsize = count * size;
// Try to allocate space for the buffer data
newfifo->itemspace = vm_malloc(bsize);
if (newfifo->itemspace != NULL) {
// Initialize structure members
newfifo->itemsize = size;
newfifo->allocatedbytes = bsize;
newfifo->readoffset = 0;
newfifo->writeoffset = 0;
newfifo->storedbytes = 0;
// Return the pointer to fifo_descriptor structure
return newfifo;
} else {
// Cannot allocate space for items, free struct resources
vm_free(newfifo);
}
}
}
// Return NULL if something fails
return NULL;
}
fifo_t fifo_create_static(fifo_t fifo, void * buf, uint16_t count, size_t size)
{
// Sanity check for memory and element sizes
if (buf != NULL && fifo != NULL && count != 0) {
fifo->itemspace = buf;
fifo->itemsize = size;
fifo->allocatedbytes = count * size;
fifo->readoffset = 0;
fifo->writeoffset = 0;
fifo->storedbytes = 0;
return fifo;
}
return NULL;
}
int fifo_add(fifo_t fifo, void * item)
{
if (!fifo_is_full(fifo)) {
fifo_copy_to(fifo, item);
fifo->storedbytes += fifo->itemsize;
return true;
} else {
return false;
}
}
int fifo_get(fifo_t fifo, void * item)
{
if (!fifo_is_empty(fifo)) {
fifo_copy_from(fifo, item);
fifo->storedbytes -= fifo->itemsize;
return true;
} else {
return false;
}
}
int fifo_is_full(fifo_t fifo)
{
if (fifo->storedbytes >= fifo->allocatedbytes)
return true;
else
return false;
}
int fifo_is_empty(fifo_t fifo)
{
if (fifo->storedbytes == 0)
return true;
else
return false;
}
int fifo_discard(fifo_t fifo, uint16_t count, enum fifo_side side)
{
uint16_t t;
t = fifo->itemsize * count; // Compute byte size of elements to be deleted
if (t <= fifo->storedbytes) // Check if we can remove the requested ammount of data
{
if (side == E_FIFO_FRONT) {
fifo->readoffset = (fifo->readoffset + t) % fifo->allocatedbytes; // Increase read pointer n elements
fifo->storedbytes -= t; // Decrease stored bytes number
} else if (side == E_FIFO_BACK) {
fifo->writeoffset = (fifo->writeoffset - t) % fifo->allocatedbytes; // Decrease write pointer n elements
fifo->storedbytes -= t; // Decrease stored bytes number
}
return true;
}
return false;
}
static void fifo_copy_from(fifo_t fifo, void * item)
{
memcpy(item, (unsigned char*)fifo->itemspace + fifo->readoffset, fifo->itemsize);
fifo->readoffset += fifo->itemsize;
if (fifo->readoffset >= fifo->allocatedbytes) {
fifo->readoffset = 0;
}
}
static void fifo_copy_to(fifo_t fifo, const void *item)
{
memcpy((unsigned char*)fifo->itemspace + fifo->writeoffset, item, fifo->itemsize);
fifo->writeoffset += fifo->itemsize;
if (fifo->writeoffset >= fifo->allocatedbytes) {
fifo->writeoffset = 0;
}
}