Branch data Line data Source code
1 : : // Copyright 2011-2025 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include <zix/ring.h>
5 : :
6 : : #include "errno_status.h"
7 : : #include "zix_config.h"
8 : :
9 : : #include <zix/allocator.h>
10 : : #include <zix/attributes.h>
11 : : #include <zix/status.h>
12 : :
13 : : #if USE_VIRTUALLOCK
14 : : # include <windows.h>
15 : : #elif USE_MLOCK
16 : : # include <sys/mman.h>
17 : : #endif
18 : :
19 : : /*
20 : : Note that for simplicity, only x86 and x64 are supported with MSVC. Hopefully
21 : : stdatomic.h support arrives before anyone cares about running this code on
22 : : Windows on ARM.
23 : : */
24 : : #ifdef _MSC_VER
25 : : # include <intrin.h>
26 : : #endif
27 : :
28 : : #include <stdint.h>
29 : : #include <string.h>
30 : :
31 : : struct ZixRingImpl {
32 : : ZixAllocator* allocator; ///< User allocator
33 : : uint32_t write_head; ///< Read index into buf
34 : : uint32_t read_head; ///< Write index into buf
35 : : uint32_t size; ///< Size (capacity) in bytes
36 : : uint32_t size_mask; ///< Mask for fast modulo
37 : : char* buf; ///< Contents
38 : : };
39 : :
40 : : static inline uint32_t
41 : 2566802 : zix_atomic_load(const uint32_t* const ptr)
42 : : {
43 : : #ifdef _MSC_VER
44 : : const uint32_t val = *ptr;
45 : : _ReadBarrier();
46 : : return val;
47 : : #else
48 : 2566802 : return __atomic_load_n(ptr, __ATOMIC_ACQUIRE);
49 : : #endif
50 : : }
51 : :
52 : : static inline void
53 : 467580 : zix_atomic_store(uint32_t* const ptr, // NOLINT(readability-non-const-parameter)
54 : : const uint32_t val)
55 : : {
56 : : #ifdef _MSC_VER
57 : : _WriteBarrier();
58 : : *ptr = val;
59 : : #else
60 : 467580 : __atomic_store_n(ptr, val, __ATOMIC_RELEASE);
61 : : #endif
62 : 467580 : }
63 : :
64 : : static inline uint32_t
65 : 10 : next_power_of_two(uint32_t size)
66 : : {
67 : : // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
68 : 10 : size--;
69 : 10 : size |= size >> 1U;
70 : 10 : size |= size >> 2U;
71 : 10 : size |= size >> 4U;
72 : 10 : size |= size >> 8U;
73 : 10 : size |= size >> 16U;
74 : 10 : size++;
75 : 10 : return size;
76 : : }
77 : :
78 : : ZixRing*
79 : 18 : zix_ring_new(ZixAllocator* const allocator, const uint32_t size)
80 : : {
81 [ + + + + ]: 18 : if (size < 2U || size > 2147483648U) {
82 : 6 : return NULL;
83 : : }
84 : :
85 : 12 : ZixRing* ring = (ZixRing*)zix_malloc(allocator, sizeof(ZixRing));
86 : :
87 [ + + ]: 12 : if (ring) {
88 : 10 : ring->allocator = allocator;
89 : 10 : ring->write_head = 0;
90 : 10 : ring->read_head = 0;
91 : 10 : ring->size = next_power_of_two(size);
92 : 10 : ring->size_mask = ring->size - 1U;
93 : :
94 [ + + ]: 10 : if (!(ring->buf = (char*)zix_malloc(allocator, ring->size))) {
95 : 2 : zix_free(allocator, ring);
96 : 2 : return NULL;
97 : : }
98 : : }
99 : :
100 : 10 : return ring;
101 : : }
102 : :
103 : : void
104 : 10 : zix_ring_free(ZixRing* const ring)
105 : : {
106 [ + + ]: 10 : if (ring) {
107 : 8 : zix_free(ring->allocator, ring->buf);
108 : 8 : zix_free(ring->allocator, ring);
109 : : }
110 : 10 : }
111 : :
112 : : ZixStatus
113 : 2 : zix_ring_mlock(ZixRing* const ring)
114 : : {
115 : : #if USE_VIRTUALLOCK
116 : : return (VirtualLock(ring, sizeof(ZixRing)) &&
117 : : VirtualLock(ring->buf, ring->size))
118 : : ? ZIX_STATUS_SUCCESS
119 : : : ZIX_STATUS_ERROR;
120 : :
121 : : #elif USE_MLOCK
122 : 4 : return zix_errno_status_if(mlock(ring, sizeof(ZixRing)) +
123 : 2 : mlock(ring->buf, ring->size));
124 : :
125 : : #else
126 : : (void)ring;
127 : : return ZIX_STATUS_NOT_SUPPORTED;
128 : : #endif
129 : : }
130 : :
131 : : ZIX_REALTIME void
132 : 2 : zix_ring_reset(ZixRing* const ring)
133 : : {
134 : 2 : ring->write_head = 0;
135 : 2 : ring->read_head = 0;
136 : 2 : }
137 : :
138 : : /*
139 : : General pattern for public thread-safe functions below: start with a single
140 : : atomic load of the "other's" index, then do whatever work, and finally end
141 : : with a single atomic store to "your" index (if it is changed).
142 : : */
143 : :
144 : : static inline uint32_t
145 : 1283405 : read_space_internal(const ZixRing* const ring,
146 : : const uint32_t r,
147 : : const uint32_t w)
148 : : {
149 : 1283405 : return (w - r) & ring->size_mask;
150 : : }
151 : :
152 : : ZIX_REALTIME uint32_t
153 : 1049608 : zix_ring_read_space(const ZixRing* const ring)
154 : : {
155 : 1049608 : const uint32_t w = zix_atomic_load(&ring->write_head);
156 : :
157 : 1049608 : return read_space_internal(ring, ring->read_head, w);
158 : : }
159 : :
160 : : static inline uint32_t
161 : 1283397 : write_space_internal(const ZixRing* const ring,
162 : : const uint32_t r,
163 : : const uint32_t w)
164 : : {
165 : 1283397 : return (r - w - 1U) & ring->size_mask;
166 : : }
167 : :
168 : : ZIX_REALTIME uint32_t
169 : 1049604 : zix_ring_write_space(const ZixRing* const ring)
170 : : {
171 : 1049604 : const uint32_t r = zix_atomic_load(&ring->read_head);
172 : :
173 : 1049604 : return write_space_internal(ring, r, ring->write_head);
174 : : }
175 : :
176 : : ZIX_REALTIME uint32_t
177 : 8 : zix_ring_capacity(const ZixRing* const ring)
178 : : {
179 : 8 : return ring->size - 1U;
180 : : }
181 : :
182 : : static inline uint32_t
183 : 233793 : peek_internal(const ZixRing* const ring,
184 : : const uint32_t r,
185 : : const uint32_t w,
186 : : const uint32_t size,
187 : : void* const dst)
188 : : {
189 [ + + ]: 233793 : if (read_space_internal(ring, r, w) < size) {
190 : 4 : return 0;
191 : : }
192 : :
193 [ + + ]: 233789 : if (r + size < ring->size) {
194 : 215525 : memcpy(dst, &ring->buf[r], size);
195 : : } else {
196 : 18264 : const uint32_t first_size = ring->size - r;
197 : 18264 : memcpy(dst, &ring->buf[r], first_size);
198 : 18264 : memcpy((char*)dst + first_size, &ring->buf[0], (size_t)size - first_size);
199 : : }
200 : :
201 : 233789 : return size;
202 : : }
203 : :
204 : : ZIX_REALTIME uint32_t
205 : 4 : zix_ring_peek(ZixRing* const ring, void* const dst, const uint32_t size)
206 : : {
207 : 4 : const uint32_t w = zix_atomic_load(&ring->write_head);
208 : :
209 : 4 : return peek_internal(ring, ring->read_head, w, size, dst);
210 : : }
211 : :
212 : : ZIX_REALTIME uint32_t
213 : 233789 : zix_ring_read(ZixRing* const ring, void* const dst, const uint32_t size)
214 : : {
215 : 233789 : const uint32_t w = zix_atomic_load(&ring->write_head);
216 : 233789 : const uint32_t r = ring->read_head;
217 [ + + ]: 233789 : if (!peek_internal(ring, r, w, size, dst)) {
218 : 2 : return 0;
219 : : }
220 : :
221 : 233787 : zix_atomic_store(&ring->read_head, (r + size) & ring->size_mask);
222 : 233787 : return size;
223 : : }
224 : :
225 : : ZIX_REALTIME uint32_t
226 : 4 : zix_ring_skip(ZixRing* const ring, const uint32_t size)
227 : : {
228 : 4 : const uint32_t w = zix_atomic_load(&ring->write_head);
229 : 4 : const uint32_t r = ring->read_head;
230 [ + + ]: 4 : if (read_space_internal(ring, r, w) < size) {
231 : 2 : return 0;
232 : : }
233 : :
234 : 2 : zix_atomic_store(&ring->read_head, (r + size) & ring->size_mask);
235 : 2 : return size;
236 : : }
237 : :
238 : : ZIX_REALTIME ZixRingTransaction
239 : 233793 : zix_ring_begin_write(ZixRing* const ring)
240 : : {
241 : 233793 : const uint32_t r = zix_atomic_load(&ring->read_head);
242 : 233793 : const uint32_t w = ring->write_head;
243 : :
244 : 233793 : const ZixRingTransaction tx = {r, w};
245 : 233793 : return tx;
246 : : }
247 : :
248 : : ZIX_REALTIME ZixStatus
249 : 233793 : zix_ring_amend_write(ZixRing* const ring,
250 : : ZixRingTransaction* const tx,
251 : : const void* const src,
252 : : const uint32_t size)
253 : : {
254 : 233793 : const uint32_t r = tx->read_head;
255 : 233793 : const uint32_t w = tx->write_head;
256 [ + + ]: 233793 : if (write_space_internal(ring, r, w) < size) {
257 : 2 : return ZIX_STATUS_NO_MEM;
258 : : }
259 : :
260 : 233791 : const uint32_t end = w + size;
261 [ + + ]: 233791 : if (end <= ring->size) {
262 : 219177 : memcpy(&ring->buf[w], src, size);
263 : 219177 : tx->write_head = end & ring->size_mask;
264 : : } else {
265 : 14614 : const uint32_t size1 = ring->size - w;
266 : 14614 : const uint32_t size2 = size - size1;
267 : 14614 : memcpy(&ring->buf[w], src, size1);
268 : 14614 : memcpy(&ring->buf[0], (const char*)src + size1, size2);
269 : 14614 : tx->write_head = size2;
270 : : }
271 : :
272 : 233791 : return ZIX_STATUS_SUCCESS;
273 : : }
274 : :
275 : : ZIX_REALTIME ZixStatus
276 : 233791 : zix_ring_commit_write(ZixRing* const ring, const ZixRingTransaction* const tx)
277 : : {
278 : 233791 : zix_atomic_store(&ring->write_head, tx->write_head);
279 : 233791 : return ZIX_STATUS_SUCCESS;
280 : : }
281 : :
282 : : ZIX_REALTIME uint32_t
283 : 233793 : zix_ring_write(ZixRing* const ring, const void* src, const uint32_t size)
284 : : {
285 : 233793 : ZixRingTransaction tx = zix_ring_begin_write(ring);
286 : :
287 [ + + ]: 233793 : if (zix_ring_amend_write(ring, &tx, src, size)) {
288 : 2 : return 0;
289 : : }
290 : :
291 : 233791 : zix_ring_commit_write(ring, &tx);
292 : 233791 : return size;
293 : : }
|