File: shm_ringbuf.c

package info (click to toggle)
s3d 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,708 kB
  • sloc: ansic: 23,609; python: 488; perl: 98; makefile: 31; sh: 29
file content (75 lines) | stat: -rw-r--r-- 1,758 bytes parent folder | download | duplicates (4)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/* SPDX-FileCopyrightText: 2004-2015  Simon Wunderlich <sw@simonwunderlich.de>
 */

#include "global.h"
#include <stdio.h>		/* printf(), getchar() */
#include <stdint.h>		/* uint32_t */
#include <string.h>		/* memcpy() */

int shm_write(struct buf_t *rb, char *buf, int n)
{
	int wrap = 0;
	int rs;
	int32_t e, s, size;
	char *data;

	e = rb->end;
	s = rb->start;
	size = rb->bufsize;
	data = ((char *)rb) + sizeof(struct buf_t);
	if (e < s) {
		wrap = 1;
	}
	if ((((s + size * (1 - wrap)) - e) < (n + 1))) {	/* checking free space */
		printf("buffer reached maxsize, no resizing possible");
	}
	if ((e + n) > size) {
		rs = size - e;
		memcpy(data + e, buf, rs);	/* copy the first part ... */
		memcpy(data, buf + rs, n - rs);	/* .. end the rest */
	} else {
		memcpy(data + e, buf, n);	/* plain copy */
	}
	rb->end = e + n;	/* update end of the buffer */
	if (rb->end >= rb->bufsize)
		rb->end -= rb->bufsize;
	return 0;
}

int shm_read(struct buf_t *rb, char *buf, int n)
{
	int wrap = 0;
	int mn;
	int rs;
	int32_t e, s, size;
	char *data;

	e = rb->end;
	s = rb->start;
	size = rb->bufsize;
	data = ((char *)rb) + sizeof(struct buf_t);
	if (e < s)
		wrap = 1;
	rs = (e + wrap * size - s);
	mn = (n > rs) ? rs : n;
	if ((wrap) && (mn > (size - s))) {
		rs = size - s;	/* size of the first part */
		memcpy(buf, data + s, rs);
		memcpy(buf + rs, data, mn - rs);
	} else {		/* no wrap (needed) */
		memcpy(buf, data + s, mn);
	}
	rb->start = s + mn;
	if (rb->start >= rb->bufsize)
		rb->start -= rb->bufsize;
	return mn;
}

void ringbuf_init(char *data, uint32_t init_size)
{
	struct buf_t *ringbuf = (struct buf_t *)data;
	ringbuf->start = 0;
	ringbuf->end = 0;
	ringbuf->bufsize = init_size - RB_OVERHEAD;
}