xref: /freebsd/usr.sbin/virtual_oss/virtual_oss/ring.c (revision 9cab9fde5edad9b409dd2317a2aec7815e6d6bed)
1 /*-
2  * Copyright (c) 2018 Hans Petter Selasky
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/queue.h>
27 #include <sys/types.h>
28 
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <time.h>
35 
36 #include "int.h"
37 
38 int
vring_alloc(struct virtual_ring * pvr,size_t size)39 vring_alloc(struct virtual_ring *pvr, size_t size)
40 {
41 
42 	if (pvr->buf_start != NULL)
43 		return (EBUSY);
44 	pvr->buf_start = malloc(size);
45 	if (pvr->buf_start == NULL)
46 		return (ENOMEM);
47 	pvr->pos_read = 0;
48 	pvr->total_size = size;
49 	pvr->len_write = 0;
50 	return (0);
51 }
52 
53 void
vring_free(struct virtual_ring * pvr)54 vring_free(struct virtual_ring *pvr)
55 {
56 
57 	if (pvr->buf_start != NULL) {
58 		free(pvr->buf_start);
59 		pvr->buf_start = NULL;
60 	}
61 }
62 
63 void
vring_reset(struct virtual_ring * pvr)64 vring_reset(struct virtual_ring *pvr)
65 {
66 	pvr->pos_read = 0;
67 	pvr->len_write = 0;
68 }
69 
70 void
vring_get_read(struct virtual_ring * pvr,uint8_t ** pptr,size_t * plen)71 vring_get_read(struct virtual_ring *pvr, uint8_t **pptr, size_t *plen)
72 {
73 	uint32_t delta;
74 
75 	if (pvr->buf_start == NULL) {
76 		*pptr = NULL;
77 		*plen = 0;
78 		return;
79 	}
80 	delta = pvr->total_size - pvr->pos_read;
81 	if (delta > pvr->len_write)
82 		delta = pvr->len_write;
83 
84 	*pptr = pvr->buf_start + pvr->pos_read;
85 	*plen = delta;
86 }
87 
88 void
vring_get_write(struct virtual_ring * pvr,uint8_t ** pptr,size_t * plen)89 vring_get_write(struct virtual_ring *pvr, uint8_t **pptr, size_t *plen)
90 {
91 	uint32_t delta;
92 	uint32_t len_read;
93 	uint32_t pos_write;
94 
95 	if (pvr->buf_start == NULL) {
96 		*pptr = NULL;
97 		*plen = 0;
98 		return;
99 	}
100 	pos_write = pvr->pos_read + pvr->len_write;
101 	if (pos_write >= pvr->total_size)
102 		pos_write -= pvr->total_size;
103 
104 	len_read = pvr->total_size - pvr->len_write;
105 
106 	delta = pvr->total_size - pos_write;
107 	if (delta > len_read)
108 		delta = len_read;
109 
110 	*pptr = pvr->buf_start + pos_write;
111 	*plen = delta;
112 }
113 
114 void
vring_inc_read(struct virtual_ring * pvr,size_t len)115 vring_inc_read(struct virtual_ring *pvr, size_t len)
116 {
117 
118 	pvr->pos_read += len;
119 	pvr->len_write -= len;
120 
121 	/* check for wrap-around */
122 	if (pvr->pos_read == pvr->total_size)
123 		pvr->pos_read = 0;
124 }
125 
126 void
vring_inc_write(struct virtual_ring * pvr,size_t len)127 vring_inc_write(struct virtual_ring *pvr, size_t len)
128 {
129 
130 	pvr->len_write += len;
131 }
132 
133 size_t
vring_total_read_len(struct virtual_ring * pvr)134 vring_total_read_len(struct virtual_ring *pvr)
135 {
136 
137 	return (pvr->len_write);
138 }
139 
140 size_t
vring_total_write_len(struct virtual_ring * pvr)141 vring_total_write_len(struct virtual_ring *pvr)
142 {
143 
144 	return (pvr->total_size - pvr->len_write);
145 }
146 
147 size_t
vring_write_linear(struct virtual_ring * pvr,const uint8_t * src,size_t total)148 vring_write_linear(struct virtual_ring *pvr, const uint8_t *src, size_t total)
149 {
150 	uint8_t *buf_ptr;
151 	size_t buf_len;
152 	size_t sum = 0;
153 
154 	while (total != 0) {
155 		vring_get_write(pvr, &buf_ptr, &buf_len);
156 		if (buf_len == 0)
157 			break;
158 		if (buf_len > total)
159 			buf_len = total;
160 		memcpy(buf_ptr, src, buf_len);
161 		vring_inc_write(pvr, buf_len);
162 		src += buf_len;
163 		sum += buf_len;
164 		total -= buf_len;
165 	}
166 	return (sum);
167 }
168 
169 size_t
vring_read_linear(struct virtual_ring * pvr,uint8_t * dst,size_t total)170 vring_read_linear(struct virtual_ring *pvr, uint8_t *dst, size_t total)
171 {
172 	uint8_t *buf_ptr;
173 	size_t buf_len;
174 	size_t sum = 0;
175 
176 	if (total > vring_total_read_len(pvr))
177 		return (0);
178 
179 	while (total != 0) {
180 		vring_get_read(pvr, &buf_ptr, &buf_len);
181 		if (buf_len == 0)
182 			break;
183 		if (buf_len > total)
184 			buf_len = total;
185 		memcpy(dst, buf_ptr, buf_len);
186 		vring_inc_read(pvr, buf_len);
187 		dst += buf_len;
188 		sum += buf_len;
189 		total -= buf_len;
190 	}
191 	return (sum);
192 }
193 
194 size_t
vring_write_zero(struct virtual_ring * pvr,size_t total)195 vring_write_zero(struct virtual_ring *pvr, size_t total)
196 {
197 	uint8_t *buf_ptr;
198 	size_t buf_len;
199 	size_t sum = 0;
200 
201 	while (total != 0) {
202 		vring_get_write(pvr, &buf_ptr, &buf_len);
203 		if (buf_len == 0)
204 			break;
205 		if (buf_len > total)
206 			buf_len = total;
207 		memset(buf_ptr, 0, buf_len);
208 		vring_inc_write(pvr, buf_len);
209 		sum += buf_len;
210 		total -= buf_len;
211 	}
212 	return (sum);
213 }
214