1 | /* $Id: circbuf.cpp 37210 2011-05-25 09:55:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Lock Free Circular Buffer
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/circbuf.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Structures and Typedefs *
|
---|
40 | *******************************************************************************/
|
---|
41 | /** @todo r=bird: this is missing docs and magic. uXPos should be offX.
|
---|
42 | * cbBufSize should be cbBuf. */
|
---|
43 | typedef struct RTCIRCBUF
|
---|
44 | {
|
---|
45 | /** The current read position in the buffer. */
|
---|
46 | size_t uReadPos;
|
---|
47 | /** The current write position in the buffer. */
|
---|
48 | size_t uWritePos;
|
---|
49 | /** How much space of the buffer is currently in use. */
|
---|
50 | volatile size_t cbBufUsed;
|
---|
51 | /** How big is the buffer. */
|
---|
52 | size_t cbBufSize;
|
---|
53 | /** The buffer itself. */
|
---|
54 | void *pvBuf;
|
---|
55 | } RTCIRCBUF;
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | RTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
|
---|
60 | {
|
---|
61 | /* Validate input. */
|
---|
62 | AssertPtrReturn(ppBuf, VERR_INVALID_POINTER);
|
---|
63 | AssertReturn(cbSize > 0, VERR_INVALID_PARAMETER);
|
---|
64 |
|
---|
65 | PRTCIRCBUF pTmpBuf;
|
---|
66 | pTmpBuf = (PRTCIRCBUF)RTMemAllocZ(sizeof(RTCIRCBUF));
|
---|
67 | if (!pTmpBuf)
|
---|
68 | return VERR_NO_MEMORY;
|
---|
69 |
|
---|
70 | pTmpBuf->pvBuf = RTMemAlloc(cbSize);
|
---|
71 | if (pTmpBuf->pvBuf)
|
---|
72 | {
|
---|
73 | pTmpBuf->cbBufSize = cbSize;
|
---|
74 | *ppBuf = pTmpBuf;
|
---|
75 | return VINF_SUCCESS;
|
---|
76 | }
|
---|
77 |
|
---|
78 | RTMemFree(pTmpBuf);
|
---|
79 | return VERR_NO_MEMORY;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | RTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf)
|
---|
84 | {
|
---|
85 | /* Validate input. */
|
---|
86 | if (!pBuf)
|
---|
87 | return;
|
---|
88 | AssertPtr(pBuf);
|
---|
89 | RTMemFree(pBuf->pvBuf);
|
---|
90 | RTMemFree(pBuf);
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf)
|
---|
95 | {
|
---|
96 | /* Validate input. */
|
---|
97 | AssertPtr(pBuf);
|
---|
98 |
|
---|
99 | pBuf->uReadPos = 0;
|
---|
100 | pBuf->uWritePos = 0;
|
---|
101 | pBuf->cbBufUsed = 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf)
|
---|
106 | {
|
---|
107 | /* Validate input. */
|
---|
108 | AssertPtrReturn(pBuf, 0);
|
---|
109 |
|
---|
110 | return pBuf->cbBufSize - ASMAtomicReadZ(&pBuf->cbBufUsed);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf)
|
---|
115 | {
|
---|
116 | /* Validate input. */
|
---|
117 | AssertPtrReturn(pBuf, 0);
|
---|
118 |
|
---|
119 | return ASMAtomicReadZ(&pBuf->cbBufUsed);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf)
|
---|
124 | {
|
---|
125 | /* Validate input. */
|
---|
126 | AssertPtrReturn(pBuf, 0);
|
---|
127 |
|
---|
128 | return pBuf->cbBufSize;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
|
---|
133 | {
|
---|
134 | /* Validate input. */
|
---|
135 | AssertPtr(pBuf);
|
---|
136 | Assert(cbReqSize > 0);
|
---|
137 | AssertPtr(ppvStart);
|
---|
138 | AssertPtr(pcbSize);
|
---|
139 |
|
---|
140 | *ppvStart = 0;
|
---|
141 | *pcbSize = 0;
|
---|
142 |
|
---|
143 | /* How much is in use? */
|
---|
144 | size_t cbUsed = ASMAtomicReadZ(&pBuf->cbBufUsed);
|
---|
145 | if (cbUsed > 0)
|
---|
146 | {
|
---|
147 | /* Get the size out of the requested size, the read block till the end
|
---|
148 | * of the buffer & the currently used size. */
|
---|
149 | size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uReadPos, cbUsed));
|
---|
150 | if (cbSize > 0)
|
---|
151 | {
|
---|
152 | /* Return the pointer address which point to the current read
|
---|
153 | * position. */
|
---|
154 | *ppvStart = (char *)pBuf->pvBuf + pBuf->uReadPos;
|
---|
155 | *pcbSize = cbSize;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
|
---|
162 | {
|
---|
163 | /* Validate input. */
|
---|
164 | AssertPtr(pBuf);
|
---|
165 |
|
---|
166 | /* Split at the end of the buffer. */
|
---|
167 | pBuf->uReadPos = (pBuf->uReadPos + cbSize) % pBuf->cbBufSize;
|
---|
168 |
|
---|
169 | ASMAtomicSubZ(&pBuf->cbBufUsed, cbSize);
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
|
---|
174 | {
|
---|
175 | /* Validate input. */
|
---|
176 | AssertPtr(pBuf);
|
---|
177 | Assert(cbReqSize > 0);
|
---|
178 | AssertPtr(ppvStart);
|
---|
179 | AssertPtr(pcbSize);
|
---|
180 |
|
---|
181 | *ppvStart = 0;
|
---|
182 | *pcbSize = 0;
|
---|
183 |
|
---|
184 | /* How much is free? */
|
---|
185 | size_t cbFree = pBuf->cbBufSize - ASMAtomicReadZ(&pBuf->cbBufUsed);
|
---|
186 | if (cbFree > 0)
|
---|
187 | {
|
---|
188 | /* Get the size out of the requested size, the write block till the end
|
---|
189 | * of the buffer & the currently free size. */
|
---|
190 | size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, cbFree));
|
---|
191 | if (cbSize > 0)
|
---|
192 | {
|
---|
193 | /* Return the pointer address which point to the current write
|
---|
194 | * position. */
|
---|
195 | *ppvStart = (char*)pBuf->pvBuf + pBuf->uWritePos;
|
---|
196 | *pcbSize = cbSize;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
|
---|
203 | {
|
---|
204 | /* Validate input. */
|
---|
205 | AssertPtr(pBuf);
|
---|
206 |
|
---|
207 | /* Split at the end of the buffer. */
|
---|
208 | pBuf->uWritePos = (pBuf->uWritePos + cbSize) % pBuf->cbBufSize;
|
---|
209 |
|
---|
210 | size_t cbOldIgnored = 0;
|
---|
211 | ASMAtomicAddZ(&pBuf->cbBufUsed, cbSize);
|
---|
212 | }
|
---|
213 |
|
---|