-
Notifications
You must be signed in to change notification settings - Fork 8
/
fifo_buf.c
174 lines (135 loc) · 3.58 KB
/
fifo_buf.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
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
/*
* MOC - music on console
* Copyright (C) 2005 Damian Pietras <[email protected]>
*
* 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 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stddef.h>
#include <sys/types.h>
#include <assert.h>
#include <string.h>
#include "common.h"
#include "fifo_buf.h"
struct fifo_buf
{
int size; /* Size of the buffer */
int pos; /* Current position */
int fill; /* Current fill */
char buf[]; /* The buffer content */
};
/* Initialize and return a new fifo_buf structure of the size requested. */
struct fifo_buf *fifo_buf_new (const size_t size)
{
struct fifo_buf *b;
assert (size > 0);
b = xmalloc (offsetof (struct fifo_buf, buf) + size);
b->size = size;
b->pos = 0;
b->fill = 0;
return b;
}
/* Destroy the buffer object. */
void fifo_buf_free (struct fifo_buf *b)
{
assert (b != NULL);
free (b);
}
/* Put data into the buffer. Returns number of bytes actually put. */
size_t fifo_buf_put (struct fifo_buf *b, const char *data, size_t size)
{
size_t written = 0;
assert (b != NULL);
assert (b->buf != NULL);
while (b->fill < b->size && written < size) {
size_t write_from;
size_t to_write;
if (b->pos + b->fill < b->size) {
write_from = b->pos + b->fill;
to_write = b->size - (b->pos + b->fill);
}
else {
write_from = b->fill - b->size + b->pos;
to_write = b->size - b->fill;
}
if (to_write > size - written)
to_write = size - written;
memcpy (b->buf + write_from, data + written, to_write);
b->fill += to_write;
written += to_write;
}
return written;
}
/* Copy data from the beginning of the buffer to the user buffer. Returns the
* number of bytes copied. */
size_t fifo_buf_peek (struct fifo_buf *b, char *user_buf, size_t user_buf_size)
{
size_t user_buf_pos = 0, written = 0;
ssize_t left, pos;
assert (b != NULL);
assert (b->buf != NULL);
left = b->fill;
pos = b->pos;
while (left && written < user_buf_size) {
size_t to_copy = pos + left <= b->size
? left : b->size - pos;
if (to_copy > user_buf_size - written)
to_copy = user_buf_size - written;
memcpy (user_buf + user_buf_pos, b->buf + pos, to_copy);
user_buf_pos += to_copy;
written += to_copy;
left -= to_copy;
pos += to_copy;
if (pos == b->size)
pos = 0;
}
return written;
}
size_t fifo_buf_get (struct fifo_buf *b, char *user_buf, size_t user_buf_size)
{
size_t user_buf_pos = 0, written = 0;
assert (b != NULL);
assert (b->buf != NULL);
while (b->fill && written < user_buf_size) {
size_t to_copy = b->pos + b->fill <= b->size
? b->fill : b->size - b->pos;
if (to_copy > user_buf_size - written)
to_copy = user_buf_size - written;
memcpy (user_buf + user_buf_pos, b->buf + b->pos, to_copy);
user_buf_pos += to_copy;
written += to_copy;
b->fill -= to_copy;
b->pos += to_copy;
if (b->pos == b->size)
b->pos = 0;
}
return written;
}
/* Get the amount of free space in the buffer. */
size_t fifo_buf_get_space (const struct fifo_buf *b)
{
assert (b != NULL);
assert (b->buf != NULL);
return b->size - b->fill;
}
size_t fifo_buf_get_fill (const struct fifo_buf *b)
{
assert (b != NULL);
return b->fill;
}
size_t fifo_buf_get_size (const struct fifo_buf *b)
{
assert (b != NULL);
return b->size;
}
void fifo_buf_clear (struct fifo_buf *b)
{
assert (b != NULL);
b->fill = 0;
}