Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 437 lines (379 sloc) 11.75 kb
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Copyright 2011 Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "internal.h"
19
19a3e34 @trondn Refactor the API
trondn authored
20 static lcb_size_t minimum(lcb_size_t a, lcb_size_t b)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
21 {
22 return (a < b) ? a : b;
23 }
24
19a3e34 @trondn Refactor the API
trondn authored
25 int ringbuffer_initialize(ringbuffer_t *buffer, lcb_size_t size)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
26 {
27 memset(buffer, 0, sizeof(ringbuffer_t));
28 buffer->root = malloc(size);
29 if (buffer->root == NULL) {
358d3c1 @trondn Step one on our way to C89
trondn authored
30 return 0;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
31 }
32 buffer->size = size;
33 buffer->write_head = buffer->root;
34 buffer->read_head = buffer->root;
358d3c1 @trondn Step one on our way to C89
trondn authored
35 return 1;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
36 }
37
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
38 void ringbuffer_reset(ringbuffer_t *buffer)
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
39 {
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
40 ringbuffer_consumed(buffer,
41 ringbuffer_get_nbytes(buffer));
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
42 }
43
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
44 void ringbuffer_destruct(ringbuffer_t *buffer)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
45 {
46 free(buffer->root);
47 buffer->root = buffer->read_head = buffer->write_head = NULL;
48 buffer->size = buffer->nbytes = 0;
49 }
50
19a3e34 @trondn Refactor the API
trondn authored
51 int ringbuffer_ensure_capacity(ringbuffer_t *buffer, lcb_size_t size)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
52 {
53 char *new_root;
19a3e34 @trondn Refactor the API
trondn authored
54 lcb_size_t new_size = buffer->size << 1;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
55 if (new_size == 0) {
56 new_size = 128;
57 }
58
59 if (size < (buffer->size - buffer->nbytes)) {
358d3c1 @trondn Step one on our way to C89
trondn authored
60 /* we've got capacity! */
61 return 1;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
62 }
63
358d3c1 @trondn Step one on our way to C89
trondn authored
64 /* determine the new buffer size... */
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
65 while ((new_size - buffer->nbytes) < size) {
66 new_size <<= 1;
67 }
68
358d3c1 @trondn Step one on our way to C89
trondn authored
69 /* go ahead and allocate a bigger block */
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
70 if ((new_root = malloc(new_size)) == NULL) {
358d3c1 @trondn Step one on our way to C89
trondn authored
71 /* Allocation failed! */
72 return 0;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
73 } else {
358d3c1 @trondn Step one on our way to C89
trondn authored
74 /* copy the data over :) */
cc98226 @trondn Fix compile problems on win32
trondn authored
75 char *old;
19a3e34 @trondn Refactor the API
trondn authored
76 lcb_size_t nbytes = buffer->nbytes;
77 lcb_size_t nr = ringbuffer_read(buffer, new_root, nbytes);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
78 if (nr != nbytes) {
79 abort();
80 }
cc98226 @trondn Fix compile problems on win32
trondn authored
81 old = buffer->root;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
82 buffer->size = new_size;
83 buffer->root = new_root;
84 buffer->nbytes = nbytes;
85 buffer->read_head = buffer->root;
86 buffer->write_head = buffer->root + nbytes;
87 free(old);
358d3c1 @trondn Step one on our way to C89
trondn authored
88 return 1;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
89 }
90 }
91
19a3e34 @trondn Refactor the API
trondn authored
92 lcb_size_t ringbuffer_get_size(ringbuffer_t *buffer)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
93 {
94 return buffer->size;
95 }
96
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
97 void *ringbuffer_get_start(ringbuffer_t *buffer)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
98 {
99 return buffer->root;
100 }
101
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
102 void *ringbuffer_get_read_head(ringbuffer_t *buffer)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
103 {
104 return buffer->read_head;
105 }
106
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
107 void *ringbuffer_get_write_head(ringbuffer_t *buffer)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
108 {
109 return buffer->write_head;
110 }
111
19a3e34 @trondn Refactor the API
trondn authored
112 lcb_size_t ringbuffer_write(ringbuffer_t *buffer,
113 const void *src,
114 lcb_size_t nb)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
115 {
116 const char *s = src;
19a3e34 @trondn Refactor the API
trondn authored
117 lcb_size_t nw = 0;
118 lcb_size_t space;
119 lcb_size_t toWrite;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
120
121 if (buffer->write_head >= buffer->read_head) {
358d3c1 @trondn Step one on our way to C89
trondn authored
122 /* write up to the end with data.. */
19a3e34 @trondn Refactor the API
trondn authored
123 space = buffer->size - (lcb_size_t)(buffer->write_head - buffer->root);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
124 toWrite = minimum(space, nb);
125
126 if (src != NULL) {
127 memcpy(buffer->write_head, s, toWrite);
128 }
129 buffer->nbytes += toWrite;
130 buffer->write_head += toWrite;
131 nw = toWrite;
132
133 if (buffer->write_head == (buffer->root + buffer->size)) {
134 buffer->write_head = buffer->root;
135 }
136
137 if (nw == nb) {
358d3c1 @trondn Step one on our way to C89
trondn authored
138 /* everything is written to the buffer.. */
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
139 return nw;
140 }
141
142 nb -= toWrite;
f920d9b @avsej Fix ringbuffer for wrapped write/read operations
avsej authored
143 s += toWrite;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
144 }
145
358d3c1 @trondn Step one on our way to C89
trondn authored
146 /* Copy data up until we catch up with the read head */
19a3e34 @trondn Refactor the API
trondn authored
147 space = (lcb_size_t)(buffer->read_head - buffer->write_head);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
148 toWrite = minimum(space, nb);
149 if (src != NULL) {
150 memcpy(buffer->write_head, s, toWrite);
151 }
152 buffer->nbytes += toWrite;
153 buffer->write_head += toWrite;
154 nw += toWrite;
155
156 if (buffer->write_head == (buffer->root + buffer->size)) {
157 buffer->write_head = buffer->root;
158 }
159
160 return nw;
161 }
162
74c75aa @avsej Fix windows build
avsej authored
163 lcb_size_t ringbuffer_strcat(ringbuffer_t *buffer, const char *str)
164 {
165 lcb_size_t len = strlen(str);
166 if (!ringbuffer_ensure_capacity(buffer, len)) {
167 return 0;
168 }
169 return ringbuffer_write(buffer, str, len);
170 }
171
2b7cd18 @avsej make reformat
avsej authored
172 static void maybe_reset(ringbuffer_t *buffer)
173 {
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
174 if (buffer->nbytes == 0) {
175 buffer->write_head = buffer->root;
176 buffer->read_head = buffer->root;
177 }
178 }
179
180
19a3e34 @trondn Refactor the API
trondn authored
181 lcb_size_t ringbuffer_read(ringbuffer_t *buffer, void *dest, lcb_size_t nb)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
182 {
183 char *d = dest;
19a3e34 @trondn Refactor the API
trondn authored
184 lcb_size_t nr = 0;
185 lcb_size_t space;
186 lcb_size_t toRead;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
187
f920d9b @avsej Fix ringbuffer for wrapped write/read operations
avsej authored
188 if (buffer->nbytes == 0) {
189 return 0;
190 }
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
191 if (buffer->read_head >= buffer->write_head) {
358d3c1 @trondn Step one on our way to C89
trondn authored
192 /* read up to the wrap point */
19a3e34 @trondn Refactor the API
trondn authored
193 space = buffer->size - (lcb_size_t)(buffer->read_head - buffer->root);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
194 toRead = minimum(space, nb);
195
196 if (dest != NULL) {
197 memcpy(d, buffer->read_head, toRead);
198 }
199 buffer->nbytes -= toRead;
200 buffer->read_head += toRead;
201 nr = toRead;
202
203 if (buffer->read_head == (buffer->root + buffer->size)) {
204 buffer->read_head = buffer->root;
205 }
206
207 if (nr == nb) {
208 maybe_reset(buffer);
209 return nr;
210 }
211
212 nb -= toRead;
213 d += toRead;
214 }
215
19a3e34 @trondn Refactor the API
trondn authored
216 space = (lcb_size_t)(buffer->write_head - buffer->read_head);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
217 toRead = minimum(space, nb);
218
219 if (dest != NULL) {
220 memcpy(d, buffer->read_head, toRead);
221 }
222 buffer->nbytes -= toRead;
223 buffer->read_head += toRead;
f920d9b @avsej Fix ringbuffer for wrapped write/read operations
avsej authored
224 nr += toRead;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
225
226 if (buffer->read_head == (buffer->root + buffer->size)) {
227 buffer->read_head = buffer->root;
228 }
229
230 maybe_reset(buffer);
231 return nr;
232 }
233
19a3e34 @trondn Refactor the API
trondn authored
234 lcb_size_t ringbuffer_peek(ringbuffer_t *buffer, void *dest, lcb_size_t nb)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
235 {
236 ringbuffer_t copy = *buffer;
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
237 return ringbuffer_read(&copy, dest, nb);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
238 }
239
19a3e34 @trondn Refactor the API
trondn authored
240 void ringbuffer_produced(ringbuffer_t *buffer, lcb_size_t nb)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
241 {
19a3e34 @trondn Refactor the API
trondn authored
242 lcb_size_t n = ringbuffer_write(buffer, NULL, nb);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
243 if (n != nb) {
244 abort();
245 }
246 }
247
19a3e34 @trondn Refactor the API
trondn authored
248 void ringbuffer_consumed(ringbuffer_t *buffer, lcb_size_t nb)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
249 {
19a3e34 @trondn Refactor the API
trondn authored
250 lcb_size_t n = ringbuffer_read(buffer, NULL, nb);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
251 if (n != nb) {
252 abort();
253 }
254 }
255
19a3e34 @trondn Refactor the API
trondn authored
256 lcb_size_t ringbuffer_get_nbytes(ringbuffer_t *buffer)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
257 {
258 return buffer->nbytes;
259 }
260
19a3e34 @trondn Refactor the API
trondn authored
261 lcb_size_t ringbuffer_update(ringbuffer_t *buffer,
262 ringbuffer_direction_t direction,
263 const void *src, lcb_size_t nb)
4f62acf @avsej Implement ringbuffer_update()
avsej authored
264 {
b24a096 @trondn Fix compile warnings due to constness
trondn authored
265 const char *s = src;
19a3e34 @trondn Refactor the API
trondn authored
266 lcb_size_t nw, ret = 0;
4f62acf @avsej Implement ringbuffer_update()
avsej authored
267
268 if (direction == RINGBUFFER_READ) {
269 if (buffer->read_head <= buffer->write_head) {
270 nw = minimum(nb, buffer->nbytes);
271 memcpy(buffer->read_head, s, nw);
272 ret += nw;
273 } else {
19a3e34 @trondn Refactor the API
trondn authored
274 nw = minimum(nb, buffer->size - (lcb_size_t)(buffer->read_head - buffer->root));
4f62acf @avsej Implement ringbuffer_update()
avsej authored
275 memcpy(buffer->read_head, s, nw);
276 nb -= nw;
277 s += nw;
278 ret += nw;
279 if (nb) {
19a3e34 @trondn Refactor the API
trondn authored
280 nw = minimum(nb, (lcb_size_t)(buffer->write_head - buffer->root));
4f62acf @avsej Implement ringbuffer_update()
avsej authored
281 memcpy(buffer->root, s, nw);
282 ret += nw;
283 }
284 }
285 } else {
286 if (buffer->write_head >= buffer->read_head) {
287 nw = minimum(nb, buffer->nbytes);
288 memcpy(buffer->write_head - nw, s, nw);
289 ret += nw;
290 } else {
291 nb = minimum(nb, buffer->nbytes);
19a3e34 @trondn Refactor the API
trondn authored
292 nw = minimum(nb, (lcb_size_t)(buffer->write_head - buffer->root));
4f62acf @avsej Implement ringbuffer_update()
avsej authored
293 memcpy(buffer->write_head - nw, s + nb - nw, nw);
294 nb -= nw;
295 ret += nw;
296 if (nb) {
19a3e34 @trondn Refactor the API
trondn authored
297 nw = minimum(nb, buffer->size - (lcb_size_t)(buffer->read_head - buffer->root));
4f62acf @avsej Implement ringbuffer_update()
avsej authored
298 memcpy(buffer->root + buffer->size - nw, s, nw);
299 ret += nw;
300 }
301 }
302 }
303 return ret;
304 }
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
305
306
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
307 void ringbuffer_get_iov(ringbuffer_t *buffer,
308 ringbuffer_direction_t direction,
19a3e34 @trondn Refactor the API
trondn authored
309 struct lcb_iovec_st *iov)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
310 {
311 iov[1].iov_base = buffer->root;
312 iov[1].iov_len = 0;
313
314 if (direction == RINGBUFFER_READ) {
315 iov[0].iov_base = buffer->read_head;
316 iov[0].iov_len = buffer->nbytes;
317 if (buffer->read_head >= buffer->write_head) {
318 ptrdiff_t chunk = buffer->root + buffer->size - buffer->read_head;
19a3e34 @trondn Refactor the API
trondn authored
319 if (buffer->nbytes > (lcb_size_t)chunk) {
320 iov[0].iov_len = (lcb_size_t)chunk;
321 iov[1].iov_len = buffer->nbytes - (lcb_size_t)chunk;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
322 }
323 }
324 } else {
9b49cc8 @trondn Fix up get_iov for write
trondn authored
325 assert(direction == RINGBUFFER_WRITE);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
326 iov[0].iov_base = buffer->write_head;
327 iov[0].iov_len = buffer->size - buffer->nbytes;
328 if (buffer->write_head >= buffer->read_head) {
f8feaa2 @trondn Fix errors reported by lint
trondn authored
329 /* I may write all the way to the end! */
74c75aa @avsej Fix windows build
avsej authored
330 iov[0].iov_len = (lcb_size_t)((buffer->root + buffer->size) - buffer->write_head);
f8feaa2 @trondn Fix errors reported by lint
trondn authored
331 /* And all the way up to the read head */
74c75aa @avsej Fix windows build
avsej authored
332 iov[1].iov_len = (lcb_size_t)(buffer->read_head - buffer->root);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
333 }
334 }
335 }
336
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
337 int ringbuffer_is_continous(ringbuffer_t *buffer,
338 ringbuffer_direction_t direction,
19a3e34 @trondn Refactor the API
trondn authored
339 lcb_size_t nb)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
340 {
358d3c1 @trondn Step one on our way to C89
trondn authored
341 int ret;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
342
343 if (direction == RINGBUFFER_READ) {
344 ret = (nb <= buffer->nbytes);
345
346 if (buffer->read_head >= buffer->write_head) {
347 ptrdiff_t chunk = buffer->root + buffer->size - buffer->read_head;
19a3e34 @trondn Refactor the API
trondn authored
348 if (nb > (lcb_size_t)chunk) {
358d3c1 @trondn Step one on our way to C89
trondn authored
349 ret = 0;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
350 }
351 }
352 } else {
353 ret = (nb <= buffer->size - buffer->nbytes);
354 if (buffer->write_head >= buffer->read_head) {
355 ptrdiff_t chunk = buffer->root + buffer->size - buffer->write_head;
19a3e34 @trondn Refactor the API
trondn authored
356 if (nb > (lcb_size_t)chunk) {
358d3c1 @trondn Step one on our way to C89
trondn authored
357 ret = 0;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
358 }
359 }
360 }
361 return ret;
362 }
363
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
364 int ringbuffer_append(ringbuffer_t *src, ringbuffer_t *dest)
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
365 {
366 char buffer[1024];
19a3e34 @trondn Refactor the API
trondn authored
367 lcb_size_t nr, nw;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
368
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
369 while ((nr = ringbuffer_read(src, buffer,
370 sizeof(buffer))) != 0) {
371 if (!ringbuffer_ensure_capacity(dest, nr)) {
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
372 abort();
358d3c1 @trondn Step one on our way to C89
trondn authored
373 return 0;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
374 }
375
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
376 nw = ringbuffer_write(dest, buffer, nr);
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
377 if (nw != nr) {
378 abort();
358d3c1 @trondn Step one on our way to C89
trondn authored
379 return 0;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
380 }
381 }
382
358d3c1 @trondn Step one on our way to C89
trondn authored
383 return 1;
668f683 @trondn Add a ringbuffer to use instead of the buffer_t
trondn authored
384 }
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
385
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
386 int ringbuffer_memcpy(ringbuffer_t *dst, ringbuffer_t *src,
19a3e34 @trondn Refactor the API
trondn authored
387 lcb_size_t nbytes)
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
388 {
389 ringbuffer_t copy = *src;
19a3e34 @trondn Refactor the API
trondn authored
390 struct lcb_iovec_st iov[2];
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
391 int ii = 0;
19a3e34 @trondn Refactor the API
trondn authored
392 lcb_size_t towrite = nbytes;
393 lcb_size_t toread, nb;
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
394
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
395 if (nbytes > ringbuffer_get_nbytes(src)) {
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
396 /* EINVAL */
397 return -1;
398 }
399
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
400 if (!ringbuffer_ensure_capacity(dst, nbytes)) {
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
401 /* Failed to allocate space */
402 return -1;
403 }
404
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
405 ringbuffer_get_iov(dst, RINGBUFFER_WRITE, iov);
36afdb2 @avsej Fix ringbuffer_memcpy()
avsej authored
406 toread = minimum(iov[ii].iov_len, nbytes);
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
407 do {
408 assert(ii < 2);
057f872 @avsej Merge branch 'release10'
avsej authored
409 nb = ringbuffer_read(&copy, iov[ii].iov_base, toread);
36afdb2 @avsej Fix ringbuffer_memcpy()
avsej authored
410 toread -= nb;
411 towrite -= nb;
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
412 ++ii;
413 } while (towrite > 0);
00ca15d @trondn Refactor: rename libcouchbase_ringbuffer_ to ringbuffer_
trondn authored
414 ringbuffer_produced(dst, nbytes);
227b0f4 @trondn Implement operation timeouts (CCBC-20)
trondn authored
415 return 0;
416 }
2f4f5d2 @trondn Fix alignment for sparc platforms
trondn authored
417
418 int ringbuffer_ensure_alignment(ringbuffer_t *c)
419 {
420 #ifdef __sparc
421 intptr_t addr = (intptr_t)c->read_head;
422
423 if (addr % 8 != 0) {
424 ringbuffer_t copy;
425 if (ringbuffer_initialize(&copy, c->size) == 0 ||
19a3e34 @trondn Refactor the API
trondn authored
426 ringbuffer_memcpy(&copy, c, ringbuffer_get_nbytes(c)) == -1) {
2f4f5d2 @trondn Fix alignment for sparc platforms
trondn authored
427 return -1;
428 }
429 ringbuffer_destruct(c);
430 *c = copy;
431 }
432 #else
433 (void)c;
434 #endif
435 return 0;
436 }
Something went wrong with that request. Please try again.