aboutsummaryrefslogtreecommitdiff
path: root/src/ring.c
blob: 0bb15535e2580fa979ac22e42ea60863ed5a9c5a (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
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
/*
 * melisma - extensible and scriptable audio engine
 * Copyright (C) 2026  Ludvig Gunne Lindström
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "mem.h"
#include "melisma/ring.h"

struct ml_ring
{
	_Atomic size_t  head;
	_Atomic size_t  tail;
	size_t          len;
	uint8_t         data[];
};


ml_ring *ml_ring_init(size_t len)
{
	size_t size = sizeof(ml_ring) + len;
	ml_ring *r = scalloc(1,size);
	r->len = len;
	return r;
}

void ml_ring_free(ml_ring *r)
{
	free(r);
}

int ml_ring_empty(ml_ring *r)
{
	size_t head = atomic_load_explicit(&r->head,memory_order_relaxed);
	size_t tail = atomic_load_explicit(&r->tail,memory_order_acquire);
	return tail == head;
}

int ml_ring_has_space(ml_ring *r, size_t len)
{
	size_t head = atomic_load_explicit(&r->head,memory_order_acquire);
	size_t tail = atomic_load_explicit(&r->tail,memory_order_relaxed);
	size_t used = tail >= head ? tail - head : r->len - head + tail;
	size_t free = r->len - 1 - used;
	return free >= len;
}

int ml_ring_put(ml_ring *r, uint8_t c)
{
	size_t tail = atomic_load_explicit(&r->tail,memory_order_relaxed);
	size_t head = atomic_load_explicit(&r->head,memory_order_acquire);
	size_t new_tail = (tail + 1) % r->len;

	if (new_tail == head)
	{
		return -1;
	}

	r->data[tail] = c;
	atomic_store_explicit(&r->tail,new_tail,memory_order_release);

	return 0;
}

int ml_ring_get(ml_ring *r, uint8_t *c)
{
	size_t tail = atomic_load_explicit(&r->tail,memory_order_acquire);
	size_t head = atomic_load_explicit(&r->head,memory_order_relaxed);
	size_t new_head = (head + 1) % r->len;

	if (head == tail)
	{
		return -1;
	}

	*c = r->data[head];
	atomic_store_explicit(&r->head,new_head,memory_order_release);

	return 0;
}

size_t ml_ring_write(ml_ring *r, const void *ptr, size_t len)
{
	size_t l = 0;
	const uint8_t *uptr = ptr;

	for (; l < len; l++)
	{
		if (ml_ring_put(r,uptr[l]) < 0)
		{
			break;
		}
	}

	return l;
}

size_t ml_ring_read(ml_ring *r, void *ptr, size_t len)
{
	size_t l = 0;
	uint8_t *uptr = ptr;

	for (; l < len; l++)
	{
		if (ml_ring_get(r,&uptr[l]) < 0)
		{
			break;
		}
	}

	return l;
}

void ml_ring_put_blocking(ml_ring *r, uint8_t c)
{
	while (ml_ring_put(r,c) < 0)
		;
}

uint8_t ml_ring_get_blocking(ml_ring *r)
{
	uint8_t c;
	while (ml_ring_get(r,&c) < 0)
		;
	return c;
}

void ml_ring_write_blocking(ml_ring *r, const void *ptr, size_t len)
{
	const uint8_t *uptr = ptr;

	while (len--)
	{
		ml_ring_put_blocking(r,*(uptr++));
	}
}

void ml_ring_read_blocking(ml_ring *r, void *ptr, size_t len)
{
	uint8_t *uptr = ptr;

	while (len--)
	{
		*(uptr++) = ml_ring_get_blocking(r);
	}
}