File: ringbuf.c

package info (click to toggle)
ofono 2.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,064 kB
  • sloc: ansic: 224,979; sh: 5,012; python: 4,040; makefile: 956
file content (440 lines) | stat: -rw-r--r-- 9,251 bytes parent folder | download | duplicates (6)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * Embedded Linux library
 * Copyright (C) 2011-2015  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/uio.h>
#include <sys/param.h>

#include "private.h"
#include "useful.h"
#include "ringbuf.h"

/**
 * SECTION:ringbuf
 * @short_description: Ring Buffer support
 *
 * Ring Buffer support
 */

/**
 * l_ringbuf:
 *
 * Opaque object representing the Ring Buffer.
 */
struct l_ringbuf {
	void *buffer;
	size_t size;
	size_t in;
	size_t out;
	l_ringbuf_tracing_func_t in_tracing;
	void *in_data;
};

#define RINGBUF_RESET 0

/* Find last (most significant) set bit */
static inline unsigned int fls(unsigned int x)
{
	return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
}

/* Round up to nearest power of two */
static inline unsigned int align_power2(unsigned int u)
{
	return 1 << fls(u - 1);
}

/**
 * l_ringbuf_new:
 * @size: Minimum size of the ring buffer.
 *
 * Create a new ring buffer
 *
 * Returns: a newly allocated #l_ringbuf object
 **/
LIB_EXPORT struct l_ringbuf *l_ringbuf_new(size_t size)
{
	struct l_ringbuf *ringbuf;
	size_t real_size;

	if (size < 2 || size > UINT_MAX)
		return NULL;

	/* Find the next power of two for size */
	real_size = align_power2(size);

	ringbuf = l_new(struct l_ringbuf, 1);
	ringbuf->buffer = l_malloc(real_size);

	ringbuf->size = real_size;
	ringbuf->in = RINGBUF_RESET;
	ringbuf->out = RINGBUF_RESET;

	return ringbuf;
}

/**
 * l_ringbuf_free:
 * @ringbuf: Ring Buffer object
 *
 * Free the Ring Buffer object and associated memory.
 **/
LIB_EXPORT void l_ringbuf_free(struct l_ringbuf *ringbuf)
{
	if (!ringbuf)
		return;

	l_free(ringbuf->buffer);
	l_free(ringbuf);
}

/**
 * l_ringbuf_set_input_tracing:
 * @ringbuf: Ring Buffer object
 * @callback: Callback function
 * @user_data: user_data for the callback function
 *
 * Sets a tracing callback that will be called whenever input data is
 * processed.  @user_data will be passed to the callback.
 *
 * Returns: Whether setting the callback succeeded.
 **/
LIB_EXPORT bool l_ringbuf_set_input_tracing(struct l_ringbuf *ringbuf,
			l_ringbuf_tracing_func_t callback, void *user_data)
{
	if (!ringbuf)
		return false;

	ringbuf->in_tracing = callback;
	ringbuf->in_data = user_data;

	return true;
}

/**
 * l_ringbuf_capacity:
 * @ringbuf: Ring Buffer object
 *
 * Returns: Total capacity of the Ring Buffer.
 **/
LIB_EXPORT size_t l_ringbuf_capacity(struct l_ringbuf *ringbuf)
{
	if (!ringbuf)
		return 0;

	return ringbuf->size;
}

/**
 * l_ringbuf_len:
 * @ringbuf: Ring Buffer object
 *
 * Returns: Number of occupied bytes in the ring buffer
 **/
LIB_EXPORT size_t l_ringbuf_len(struct l_ringbuf *ringbuf)
{
	if (!ringbuf)
		return 0;

	return ringbuf->in - ringbuf->out;
}

/**
 * l_ringbuf_drain:
 * @ringbuf: Ring Buffer object
 * @count: Number of bytes to drain
 *
 * Drains a number of bytes specified.  The occupied bytes are discarded.
 *
 * Returns: Number of bytes drained
 **/
LIB_EXPORT size_t l_ringbuf_drain(struct l_ringbuf *ringbuf, size_t count)
{
	size_t len;

	if (!ringbuf)
		return 0;

	len = minsize(count, ringbuf->in - ringbuf->out);
	if (!len)
		return 0;

	ringbuf->out += len;

	if (ringbuf->out == ringbuf->in) {
		ringbuf->in = RINGBUF_RESET;
		ringbuf->out = RINGBUF_RESET;
	}

	return len;
}

/**
 * l_ringbuf_peek:
 * @ringbuf: Ring Buffer object
 * @offset: Offset into the ring buffer
 * @len_nowrap: Number of contiguous bytes starting from the current offset
 *
 * Peeks into the ring buffer at offset specified by @offset.  Since the ring
 * buffer can wrap around, the stored bytes might be in two contiguous
 * locations.  Typically offset of 0 is used first.  Then, if len_nowrap
 * is less than the length returned by l_ringbuf_len, the rest of the data
 * can be obtained by calling l_ringbuf_peek with offset set to len_nowrap.
 *
 * Returns: Pointer into ring buffer internal storage
 **/
LIB_EXPORT void *l_ringbuf_peek(struct l_ringbuf *ringbuf, size_t offset,
							size_t *len_nowrap)
{
	if (!ringbuf)
		return NULL;

	offset = (ringbuf->out + offset) & (ringbuf->size - 1);

	if (len_nowrap) {
		size_t len = ringbuf->in - ringbuf->out;
		*len_nowrap = minsize(len, ringbuf->size - offset);
	}

	return ringbuf->buffer + offset;
}

/**
 * l_ringbuf_write:
 * @ringbuf: Ring Buffer object
 * @fd: file descriptor to write to
 *
 * Tries to write the contents of the ring buffer out to a file descriptor
 *
 * Returns: Number of bytes written or -1 if the write failed.
 **/
LIB_EXPORT ssize_t l_ringbuf_write(struct l_ringbuf *ringbuf, int fd)
{
	size_t len, offset, end;
	struct iovec iov[2];
	ssize_t consumed;

	if (!ringbuf || fd < 0)
		return -1;

	/* Determine how much data is available */
	len = ringbuf->in - ringbuf->out;
	if (!len)
		return 0;

	/* Grab data from buffer starting at offset until the end */
	offset = ringbuf->out & (ringbuf->size - 1);
	end = minsize(len, ringbuf->size - offset);

	iov[0].iov_base = ringbuf->buffer + offset;
	iov[0].iov_len = end;

	/* Use second vector for remainder from the beginning */
	iov[1].iov_base = ringbuf->buffer;
	iov[1].iov_len = len - end;

	consumed = writev(fd, iov, 2);
	if (consumed < 0)
		return -1;

	ringbuf->out += consumed;

	if (ringbuf->out == ringbuf->in) {
		ringbuf->in = RINGBUF_RESET;
		ringbuf->out = RINGBUF_RESET;
	}

	return consumed;
}

/**
 * l_ringbuf_avail:
 * @ringbuf: Ring Buffer object
 *
 * Returns: Number of unoccupied bytes in the ring buffer
 **/
LIB_EXPORT size_t l_ringbuf_avail(struct l_ringbuf *ringbuf)
{
	if (!ringbuf)
		return 0;

	return ringbuf->size - ringbuf->in + ringbuf->out;
}

/**
 * l_ringbuf_printf:
 * @ringbuf: Ring Buffer object
 * @format: printf-style format string
 *
 * Writes contents to the ring buffer using printf-style semantics
 *
 * Returns: Number of bytes written
 **/
LIB_EXPORT int l_ringbuf_printf(struct l_ringbuf *ringbuf,
						const char *format, ...)
{
	va_list ap;
	int len;

	va_start(ap, format);
	len = l_ringbuf_vprintf(ringbuf, format, ap);
	va_end(ap);

	return len;
}

/**
 * l_ringbuf_printf:
 * @ringbuf: Ring Buffer object
 * @format: printf-style format string
 * @ap: variable argument list
 *
 * Writes contents to the ring buffer using printf-style semantics
 *
 * Returns: Number of bytes written
 **/
LIB_EXPORT int l_ringbuf_vprintf(struct l_ringbuf *ringbuf,
						const char *format, va_list ap)
{
	size_t avail;
	char *str;
	int len;

	if (!ringbuf || !format)
		return -1;

	/* Determine maximum length available for string */
	avail = ringbuf->size - ringbuf->in + ringbuf->out;
	if (!avail)
		return -1;

	len = vasprintf(&str, format, ap);
	if (len < 0)
		return -1;

	if ((size_t) len > avail) {
		l_free(str);
		return -1;
	}

	len = l_ringbuf_append(ringbuf, str, (size_t) len);

	l_free(str);

	return len;
}

/**
 * l_ringbuf_read:
 * @ringbuf: Ring Buffer object
 * @fd: file descriptor to read from
 *
 * Reads data from a file descriptor given by @fd into the ring buffer.
 *
 * Returns: Number of bytes read or -1 if the read failed.
 **/
LIB_EXPORT ssize_t l_ringbuf_read(struct l_ringbuf *ringbuf, int fd)
{
	size_t avail, offset, end;
	struct iovec iov[2];
	ssize_t consumed;

	if (!ringbuf || fd < 0)
		return -1;

	/* Determine how much can actually be consumed */
	avail = ringbuf->size - ringbuf->in + ringbuf->out;
	if (!avail)
		return -1;

	/* Determine how much to consume before wrapping */
	offset = ringbuf->in & (ringbuf->size - 1);
	end = minsize(avail, ringbuf->size - offset);

	iov[0].iov_base = ringbuf->buffer + offset;
	iov[0].iov_len = end;

	/* Now put the remainder into the second vector */
	iov[1].iov_base = ringbuf->buffer;
	iov[1].iov_len = avail - end;

	consumed = readv(fd, iov, 2);
	if (consumed < 0)
		return -1;

	if (ringbuf->in_tracing) {
		size_t len = minsize((size_t) consumed, end);

		ringbuf->in_tracing(ringbuf->buffer + offset, len,
							ringbuf->in_data);

		if (consumed - len > 0)
			ringbuf->in_tracing(ringbuf->buffer, consumed - len,
							ringbuf->in_data);
	}

	ringbuf->in += consumed;

	return consumed;
}

/**
 * l_ringbuf_append:
 * @ringbuf: Ring Buffer object
 * @data: data to be appended
 * @len: data length
 *
 * Appends data to the ring buffer.
 *
 * Returns: Number of appended bytes or -1 if the append failed.
 **/
LIB_EXPORT ssize_t l_ringbuf_append(struct l_ringbuf *ringbuf,
						const void *data, size_t len)
{
	size_t avail;
	size_t offset;
	size_t end;
	size_t left;

	if (!ringbuf || data == NULL)
		return -1;

	/* Determine how much can actually be appended */
	avail = ringbuf->size - ringbuf->in + ringbuf->out;

	if (!avail)
		return -1;

	/* Determine how much to append before wrapping */
	offset = ringbuf->in & (ringbuf->size - 1);
	end = minsize(len, ringbuf->size - offset);
	memcpy(ringbuf->buffer + offset, data, end);

	if (ringbuf->in_tracing)
		ringbuf->in_tracing(ringbuf->buffer + offset, end,
						ringbuf->in_data);

	left = minsize(avail - end, len - end);

	if (left > 0) {
		memcpy(ringbuf->buffer, data + end, left);

		if (ringbuf->in_tracing)
			ringbuf->in_tracing(ringbuf->buffer, left,
						ringbuf->in_data);
	}

	ringbuf->in += end + left;

	return (end + left);
}