-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuf.c
125 lines (103 loc) · 2.93 KB
/
ringbuf.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
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "ringbuf.h"
struct ringbuf *ringbuf_alloc(int nitems, size_t bufsize)
{
struct ringbuf *ringbuf;
ringbuf = malloc(sizeof(*ringbuf));
if (!ringbuf) {
perror("malloc");
return NULL;
}
ringbuf->n = nitems;
ringbuf->bufsize = bufsize;
ringbuf->items = malloc(ringbuf->n * sizeof(struct ringbuf_item *));
if (!ringbuf->items) {
perror("malloc");
free(ringbuf);
return NULL;
}
for (int i = 0; i < ringbuf->n; i++) {
ringbuf->items[i] = malloc(sizeof(struct ringbuf_item));
if (!ringbuf->items[i]) {
perror("malloc");
ringbuf_free(ringbuf);
return NULL;
}
ringbuf->items[i]->buf = malloc(bufsize);
if (!ringbuf->items[i]->buf) {
perror("malloc");
ringbuf_free(ringbuf);
return NULL;
}
memset(ringbuf->items[i]->buf, 0, bufsize);
ringbuf->items[i]->len = bufsize;
ringbuf->items[i]->pos = 0;
}
ringbuf->head = nitems - 1;
ringbuf->tail = 0;
return ringbuf;
}
void ringbuf_free(struct ringbuf *ringbuf)
{
if (ringbuf && ringbuf->items) {
for (int i = 0; i < ringbuf->n; i++) {
free(ringbuf->items[i]->buf);
free(ringbuf->items[i]);
}
free(ringbuf->items);
free(ringbuf);
}
}
int ringbuf_read_fd(int fd, struct ringbuf *ringbuf)
{
// Data from stdin is appended to tail
int cur = ringbuf->tail;
struct ringbuf_item *cur_item = ringbuf->items[cur];
ssize_t nread = read(fd,
cur_item->buf + cur_item->pos,
cur_item->len - cur_item->pos);
if (nread < 0) {
perror("read");
return -1;
}
cur_item->pos += nread;
// Read complete if there is an end of string
// or the buffer is filled
if (strchr(cur_item->buf, '\n') ||
cur_item->pos == cur_item->len) {
// Prepare buffer for output
cur_item->len = cur_item->pos;
cur_item->pos = 0;
// Advance tail index
cur = (cur + 1) % ringbuf->n;
ringbuf->tail = cur;
}
return 0;
}
int ringbuf_write_fd(int fd, struct ringbuf *ringbuf)
{
// Data to stdout is extracted from next to head
int cur = (ringbuf->head + 1) % ringbuf->n;
struct ringbuf_item *cur_item = ringbuf->items[cur];
ssize_t nwritten = write(fd,
cur_item->buf + cur_item->pos,
cur_item->len - cur_item->pos);
if (nwritten < 0) {
perror("write");
return -1;
}
cur_item->pos += nwritten;
// Write complete
if (cur_item->pos == cur_item->len) {
// Reset written buffer
memset(cur_item->buf, 0, cur_item->len);
cur_item->pos = 0;
cur_item->len = ringbuf->bufsize;
// Advance head index
ringbuf->head = cur;
}
return 0;
}