1 | /* $Id: circbuf.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Lock Free Circular Buffer
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/circbuf.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | /** @todo r=bird: this is missing docs and magic. */
|
---|
52 | typedef struct RTCIRCBUF
|
---|
53 | {
|
---|
54 | /** The current read position in the buffer. */
|
---|
55 | size_t offRead;
|
---|
56 | /** Is a read block acquired currently? */
|
---|
57 | bool fReading;
|
---|
58 | /** Is a write block acquired currently? */
|
---|
59 | bool fWriting;
|
---|
60 | /** The current write position in the buffer. */
|
---|
61 | size_t offWrite;
|
---|
62 | /** How much space of the buffer is currently in use. */
|
---|
63 | volatile size_t cbUsed;
|
---|
64 | /** How big is the buffer. */
|
---|
65 | size_t cbBuf;
|
---|
66 | /** The buffer itself. */
|
---|
67 | void *pvBuf;
|
---|
68 | } RTCIRCBUF, *PRTCIRCBUF;
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
|
---|
72 | {
|
---|
73 | /* Validate input. */
|
---|
74 | AssertPtrReturn(ppBuf, VERR_INVALID_POINTER);
|
---|
75 | AssertReturn(cbSize > 0, VERR_INVALID_PARAMETER);
|
---|
76 |
|
---|
77 | PRTCIRCBUF pTmpBuf;
|
---|
78 | pTmpBuf = (PRTCIRCBUF)RTMemAllocZ(sizeof(RTCIRCBUF));
|
---|
79 | if (!pTmpBuf)
|
---|
80 | return VERR_NO_MEMORY;
|
---|
81 |
|
---|
82 | pTmpBuf->pvBuf = RTMemAlloc(cbSize);
|
---|
83 | if (pTmpBuf->pvBuf)
|
---|
84 | {
|
---|
85 | pTmpBuf->cbBuf = cbSize;
|
---|
86 | *ppBuf = pTmpBuf;
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 |
|
---|
90 | RTMemFree(pTmpBuf);
|
---|
91 | return VERR_NO_MEMORY;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf)
|
---|
96 | {
|
---|
97 | /* Validate input. */
|
---|
98 | if (!pBuf)
|
---|
99 | return;
|
---|
100 | AssertPtr(pBuf);
|
---|
101 | RTMemFree(pBuf->pvBuf);
|
---|
102 | RTMemFree(pBuf);
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf)
|
---|
107 | {
|
---|
108 | /* Validate input. */
|
---|
109 | AssertPtr(pBuf);
|
---|
110 |
|
---|
111 | pBuf->offRead = 0;
|
---|
112 | pBuf->offWrite = 0;
|
---|
113 | pBuf->cbUsed = 0;
|
---|
114 | pBuf->fReading = false;
|
---|
115 | pBuf->fWriting = false;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf)
|
---|
120 | {
|
---|
121 | /* Validate input. */
|
---|
122 | AssertPtrReturn(pBuf, 0);
|
---|
123 |
|
---|
124 | return pBuf->cbBuf - ASMAtomicReadZ(&pBuf->cbUsed);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf)
|
---|
129 | {
|
---|
130 | /* Validate input. */
|
---|
131 | AssertPtrReturn(pBuf, 0);
|
---|
132 |
|
---|
133 | return ASMAtomicReadZ(&pBuf->cbUsed);
|
---|
134 | }
|
---|
135 |
|
---|
136 | RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf)
|
---|
137 | {
|
---|
138 | /* Validate input. */
|
---|
139 | AssertPtrReturn(pBuf, 0);
|
---|
140 |
|
---|
141 | return pBuf->cbBuf;
|
---|
142 | }
|
---|
143 |
|
---|
144 | RTDECL(bool) RTCircBufIsReading(PRTCIRCBUF pBuf)
|
---|
145 | {
|
---|
146 | /* Validate input. */
|
---|
147 | AssertPtrReturn(pBuf, 0);
|
---|
148 |
|
---|
149 | return ASMAtomicReadBool(&pBuf->fReading);
|
---|
150 | }
|
---|
151 |
|
---|
152 | RTDECL(bool) RTCircBufIsWriting(PRTCIRCBUF pBuf)
|
---|
153 | {
|
---|
154 | /* Validate input. */
|
---|
155 | AssertPtrReturn(pBuf, 0);
|
---|
156 |
|
---|
157 | return ASMAtomicReadBool(&pBuf->fWriting);
|
---|
158 | }
|
---|
159 |
|
---|
160 | RTDECL(size_t) RTCircBufOffsetRead(PRTCIRCBUF pBuf)
|
---|
161 | {
|
---|
162 | /* Validate input. */
|
---|
163 | AssertPtrReturn(pBuf, 0);
|
---|
164 |
|
---|
165 | return ASMAtomicReadZ(&pBuf->offRead);
|
---|
166 | }
|
---|
167 |
|
---|
168 | RTDECL(size_t) RTCircBufOffsetWrite(PRTCIRCBUF pBuf)
|
---|
169 | {
|
---|
170 | /* Validate input. */
|
---|
171 | AssertPtrReturn(pBuf, 0);
|
---|
172 |
|
---|
173 | return ASMAtomicReadZ(&pBuf->offWrite);
|
---|
174 | }
|
---|
175 |
|
---|
176 | RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
|
---|
177 | {
|
---|
178 | /* Validate input. */
|
---|
179 | AssertPtr(pBuf);
|
---|
180 | Assert(cbReqSize > 0);
|
---|
181 | AssertPtr(ppvStart);
|
---|
182 | AssertPtr(pcbSize);
|
---|
183 |
|
---|
184 | *ppvStart = 0;
|
---|
185 | *pcbSize = 0;
|
---|
186 |
|
---|
187 | /* How much is in use? */
|
---|
188 | size_t cbUsed = ASMAtomicReadZ(&pBuf->cbUsed);
|
---|
189 | if (cbUsed > 0)
|
---|
190 | {
|
---|
191 | /* Get the size out of the requested size, the read block till the end
|
---|
192 | * of the buffer & the currently used size. */
|
---|
193 | size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBuf - pBuf->offRead, cbUsed));
|
---|
194 | if (cbSize > 0)
|
---|
195 | {
|
---|
196 | /* Return the pointer address which point to the current read
|
---|
197 | * position. */
|
---|
198 | *ppvStart = (char *)pBuf->pvBuf + pBuf->offRead;
|
---|
199 | *pcbSize = cbSize;
|
---|
200 |
|
---|
201 | ASMAtomicWriteBool(&pBuf->fReading, true);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
|
---|
208 | {
|
---|
209 | /* Validate input. */
|
---|
210 | AssertPtr(pBuf);
|
---|
211 |
|
---|
212 | /* Split at the end of the buffer. */
|
---|
213 | pBuf->offRead = (pBuf->offRead + cbSize) % pBuf->cbBuf;
|
---|
214 |
|
---|
215 | ASMAtomicSubZ(&pBuf->cbUsed, cbSize);
|
---|
216 | ASMAtomicWriteBool(&pBuf->fReading, false);
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
|
---|
221 | {
|
---|
222 | /* Validate input. */
|
---|
223 | AssertPtr(pBuf);
|
---|
224 | Assert(cbReqSize > 0);
|
---|
225 | AssertPtr(ppvStart);
|
---|
226 | AssertPtr(pcbSize);
|
---|
227 |
|
---|
228 | *ppvStart = 0;
|
---|
229 | *pcbSize = 0;
|
---|
230 |
|
---|
231 | /* How much is free? */
|
---|
232 | size_t cbFree = pBuf->cbBuf - ASMAtomicReadZ(&pBuf->cbUsed);
|
---|
233 | if (cbFree > 0)
|
---|
234 | {
|
---|
235 | /* Get the size out of the requested size, then write block till the end
|
---|
236 | * of the buffer & the currently free size. */
|
---|
237 | size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBuf - pBuf->offWrite, cbFree));
|
---|
238 | if (cbSize > 0)
|
---|
239 | {
|
---|
240 | /* Return the pointer address which point to the current write
|
---|
241 | * position. */
|
---|
242 | *ppvStart = (char*)pBuf->pvBuf + pBuf->offWrite;
|
---|
243 | *pcbSize = cbSize;
|
---|
244 |
|
---|
245 | ASMAtomicWriteBool(&pBuf->fWriting, true);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
|
---|
252 | {
|
---|
253 | /* Validate input. */
|
---|
254 | AssertPtr(pBuf);
|
---|
255 |
|
---|
256 | /* Split at the end of the buffer. */
|
---|
257 | pBuf->offWrite = (pBuf->offWrite + cbSize) % pBuf->cbBuf;
|
---|
258 |
|
---|
259 | ASMAtomicAddZ(&pBuf->cbUsed, cbSize);
|
---|
260 | ASMAtomicWriteBool(&pBuf->fWriting, false);
|
---|
261 | }
|
---|
262 |
|
---|