forked from facebookarchive/fb-adb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuf.c
193 lines (168 loc) · 4.33 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <assert.h>
#include <errno.h>
#include <sys/uio.h>
#include <string.h>
#include <limits.h>
#include "ringbuf.h"
#include "util.h"
struct ringbuf {
size_t nr_removed;
size_t nr_added;
size_t capacity;
char* __restrict__ mem;
};
struct ringbuf_io {
struct iovec v[2];
};
struct ringbuf*
ringbuf_new(size_t capacity)
{
if (capacity == 0)
die(ERANGE, "buffer too small");
capacity = nextpow2sz(capacity);
if (capacity == 0)
die(ERANGE, "buffer too large");
struct ringbuf* rb = xcalloc(sizeof (*rb));
rb->capacity = capacity;
rb->mem = xalloc(capacity);
return rb;
}
static size_t
ringbuf_clip(const struct ringbuf* rb, size_t pos)
{
/* rb->capacity is always a nonzero power of two */
assert(XPOW2P(rb->capacity));
return pos & (rb->capacity - 1);
}
size_t
ringbuf_capacity(const struct ringbuf* rb)
{
return rb->capacity;
}
size_t
ringbuf_size(const struct ringbuf* rb)
{
return rb->nr_added - rb->nr_removed;
}
size_t
ringbuf_room(const struct ringbuf* rb)
{
return ringbuf_capacity(rb) - ringbuf_size(rb);
}
void
ringbuf_consume(struct ringbuf* rb, size_t nr)
{
assert(nr <= ringbuf_size(rb));
rb->nr_removed += nr;
}
static struct ringbuf_io
ringbuf_io_region(const struct ringbuf* rb, size_t pos, size_t len)
{
assert(len <= rb->capacity);
size_t idx = ringbuf_clip(rb, pos);
struct ringbuf_io rio;
rio.v[0].iov_base = &rb->mem[idx];
rio.v[0].iov_len = len;
if (idx + len > rb->capacity) {
rio.v[0].iov_len = rb->capacity - idx;
rio.v[1].iov_base = rb->mem;
rio.v[1].iov_len = len - rio.v[0].iov_len;
} else {
rio.v[1].iov_base = NULL;
rio.v[1].iov_len = 0;
}
assert(rio.v[0].iov_len + rio.v[1].iov_len == len);
return rio;
}
size_t
ringbuf_note_added(struct ringbuf* rb, size_t nr)
{
assert(nr <= ringbuf_room(rb));
rb->nr_added += nr;
return nr;
}
size_t
ringbuf_note_removed(struct ringbuf* rb, size_t nr)
{
assert(nr <= ringbuf_size(rb));
rb->nr_removed += nr;
return nr;
}
size_t
ringbuf_read_in(struct ringbuf* rb, int fd, size_t sz)
{
sz = XMIN(sz, SSIZE_MAX);
assert(sz <= ringbuf_room(rb));
struct ringbuf_io rio = ringbuf_io_region(rb, rb->nr_added, sz);
ssize_t ret;
{
WITH_IO_SIGNALS_ALLOWED();
ret = readv(fd, rio.v, ARRAYSIZE(rio.v));
}
if (ret < 0)
die_errno("readv");
return (size_t) ret;
}
void
ringbuf_copy_in(struct ringbuf* rb, const void* buf, size_t sz)
{
assert(sz <= ringbuf_room(rb));
struct ringbuf_io rio = ringbuf_io_region(rb, rb->nr_added, sz);
memcpy(rio.v[0].iov_base, buf, rio.v[0].iov_len);
buf = (char*) buf + rio.v[0].iov_len;
memcpy(rio.v[1].iov_base, buf, rio.v[1].iov_len);
}
size_t
ringbuf_write_out(const struct ringbuf* rb, int fd, size_t sz)
{
sz = XMIN(sz, SSIZE_MAX);
assert(sz <= ringbuf_size(rb));
struct ringbuf_io rio = ringbuf_io_region(rb, rb->nr_removed, sz);
ssize_t ret;
{
WITH_IO_SIGNALS_ALLOWED();
ret = writev(fd, rio.v, ARRAYSIZE(rio.v));
}
if (ret < 0)
die_errno("writev");
return (size_t) ret;
}
void
ringbuf_copy_out(const struct ringbuf* rb, void* buf, size_t sz)
{
assert(sz <= ringbuf_size(rb));
struct ringbuf_io rio = ringbuf_io_region(rb, rb->nr_removed, sz);
memcpy(buf, rio.v[0].iov_base, rio.v[0].iov_len);
buf = (char*) buf + rio.v[0].iov_len;
memcpy(buf, rio.v[1].iov_base, rio.v[1].iov_len);
}
void
ringbuf_readable_iov(const struct ringbuf* rb,
struct iovec iov[2],
size_t sz)
{
assert(sz <= ringbuf_size(rb));
struct ringbuf_io rio = ringbuf_io_region(rb, rb->nr_removed, sz);
iov[0] = rio.v[0];
iov[1] = rio.v[1];
}
void
ringbuf_writable_iov(const struct ringbuf* rb,
struct iovec iov[2],
size_t sz)
{
assert(sz <= ringbuf_room(rb));
struct ringbuf_io rio = ringbuf_io_region(rb, rb->nr_added, sz);
iov[0] = rio.v[0];
iov[1] = rio.v[1];
}