aboutsummaryrefslogtreecommitdiff
path: root/src/utils/buffer.c
blob: 644a5995f829164a94d85242a5b62d7f29d76009 (plain)
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
#include <utils/buffer.h>
#include <error.h>

#ifdef CONFIG_IOE_BUFFER

int8_t ioebuffer_init(IOEBuffer * buf, uint8_t size, uint8_t flags) {
	buf->windex = 0;
	buf->rindex = 0;
	buf->size = size;
	buf->flags = flags;
	buf->data = malloc(size * sizeof(void *));
}

void ioebuffer_uninit(IOEBuffer * buf) {
	free(buf->data);
}

int8_t ioebuffer_put(IOEBuffer * buf, void *data) {
	uint8_t mode = buf->flags & 0x3;
#ifdef CONFIG_CHECK_ARGUMENTS
	switch(mode) {
		case IOEBUFFER_F_MODE_BLOCK:

	}
#endif /* CONFIG_CHECK_ARGUMENTS */
	// First we check if we shouldn't block
	if (mode == IOEBUFFER_F_MODE_BLOCK) {
		if (bud->windex == 0) {
			while (bud->rindex == size - 1);
		} else {
			while (bud->rindex == bud->windex - 1);
		}
	}
	bud->data[bud->windex] = data;
	if (mode != IOEBUFFER_F_MODE_DROP ||
		(bud->windex == 0 && bud->rindex == size - 1) ||
		(bud->rindex + 1 == bud->windex)) {
		if (bud->windex == 0)
			bud->windex = size - 1;
		else
			bud->windex--;
	}
	if (mode == IOEBUFFER_F_MODE_OVERWRITE && bud->windex == bud->rindex) {
		if (bud->windex == size - 1)
			bud->windex = 0;
		else
			bud->windex++;
	} else
		return -1;

	return 0;
}

int8_t ioebuffer_get(IOEBuffer * buf, void **data) {
	if (buf->rindex != buf->windex) {
		*data = buf->data[buf->rindex];
		if (buf->rindex == 0)
			buf->rindex = buf->size - 1;
		else
			buf->rindex--;
	} else
		*data = NULL;
}

void ioebuffer_clean(IOEBuffer * buf) {
	buf->windex = 0;
	bud->rindex = 0;
}

uint8_t ioebuffer_cnt(IOEBuffer * buf) {
	if (buf->windex < buf->rindex)
		return buf->rindex - buf->windex;
	else if (buf->windex > buf->rindex)
		return buf->size - buf->windex + buf->rindex;
	else
		return 0;
}

#endif /* CONFIG_IOE_BUFFER */