1 | /* $Id: circbuf.cpp 33286 2010-10-21 09:48:00Z 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 | * Public Functions *
|
---|
39 | ******************************************************************************/
|
---|
40 |
|
---|
41 | RTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
|
---|
42 | {
|
---|
43 | /* Validate input. */
|
---|
44 | AssertPtrReturn(ppBuf, VERR_INVALID_POINTER);
|
---|
45 | AssertReturn(cbSize > 0, VERR_INVALID_PARAMETER);
|
---|
46 |
|
---|
47 | PRTCIRCBUF pTmpBuf;
|
---|
48 | pTmpBuf = (PRTCIRCBUF)RTMemAllocZ(sizeof(RTCIRCBUF));
|
---|
49 | if (!pTmpBuf)
|
---|
50 | return VERR_NO_MEMORY;
|
---|
51 |
|
---|
52 | int rc = VINF_SUCCESS;
|
---|
53 | do
|
---|
54 | {
|
---|
55 | pTmpBuf->pvBuf = RTMemAlloc(cbSize);
|
---|
56 | if (!pTmpBuf)
|
---|
57 | {
|
---|
58 | rc = VERR_NO_MEMORY;
|
---|
59 | break;
|
---|
60 | }
|
---|
61 |
|
---|
62 | pTmpBuf->cbBufSize = cbSize;
|
---|
63 | *ppBuf = pTmpBuf;
|
---|
64 | }while (0);
|
---|
65 |
|
---|
66 | if (RT_FAILURE(rc))
|
---|
67 | RTMemFree(pTmpBuf);
|
---|
68 |
|
---|
69 | return rc;
|
---|
70 | }
|
---|
71 |
|
---|
72 | RTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf)
|
---|
73 | {
|
---|
74 | /* Validate input. */
|
---|
75 | AssertPtrNull(pBuf);
|
---|
76 |
|
---|
77 | if (pBuf)
|
---|
78 | {
|
---|
79 | if (pBuf->pvBuf)
|
---|
80 | RTMemFree(pBuf->pvBuf);
|
---|
81 | RTMemFree(pBuf);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf)
|
---|
86 | {
|
---|
87 | /* Validate input. */
|
---|
88 | AssertPtr(pBuf);
|
---|
89 |
|
---|
90 | pBuf->uReadPos = 0;
|
---|
91 | pBuf->uWritePos = 0;
|
---|
92 | pBuf->cbBufUsed = 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf)
|
---|
96 | {
|
---|
97 | /* Validate input. */
|
---|
98 | AssertPtrReturn(pBuf, 0);
|
---|
99 |
|
---|
100 | size_t cbSize = 0;
|
---|
101 | ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
|
---|
102 | return pBuf->cbBufSize - cbSize;
|
---|
103 | }
|
---|
104 |
|
---|
105 | RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf)
|
---|
106 | {
|
---|
107 | /* Validate input. */
|
---|
108 | AssertPtrReturn(pBuf, 0);
|
---|
109 |
|
---|
110 | size_t cbSize = 0;
|
---|
111 | ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
|
---|
112 | return cbSize;
|
---|
113 | }
|
---|
114 |
|
---|
115 | RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf)
|
---|
116 | {
|
---|
117 | /* Validate input. */
|
---|
118 | AssertPtrReturn(pBuf, 0);
|
---|
119 |
|
---|
120 | return pBuf->cbBufSize;
|
---|
121 | }
|
---|
122 |
|
---|
123 | RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
|
---|
124 | {
|
---|
125 | /* Validate input. */
|
---|
126 | AssertPtr(pBuf);
|
---|
127 | Assert(cbReqSize > 0);
|
---|
128 | AssertPtr(ppvStart);
|
---|
129 | AssertPtr(pcbSize);
|
---|
130 |
|
---|
131 | size_t uUsed = 0;
|
---|
132 | size_t uSize = 0;
|
---|
133 |
|
---|
134 | *ppvStart = 0;
|
---|
135 | *pcbSize = 0;
|
---|
136 |
|
---|
137 | /* How much is in use? */
|
---|
138 | ASMAtomicReadSize(&pBuf->cbBufUsed, &uUsed);
|
---|
139 | if (uUsed > 0)
|
---|
140 | {
|
---|
141 | /* Get the size out of the requested size, the read block till the end
|
---|
142 | * of the buffer & the currently used size. */
|
---|
143 | uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uReadPos, uUsed));
|
---|
144 | if (uSize > 0)
|
---|
145 | {
|
---|
146 | /* Return the pointer address which point to the current read
|
---|
147 | * position. */
|
---|
148 | *ppvStart = (char*)pBuf->pvBuf + pBuf->uReadPos;
|
---|
149 | *pcbSize = uSize;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
|
---|
155 | {
|
---|
156 | /* Validate input. */
|
---|
157 | AssertPtr(pBuf);
|
---|
158 |
|
---|
159 | /* Split at the end of the buffer. */
|
---|
160 | pBuf->uReadPos = (pBuf->uReadPos + cbSize) % pBuf->cbBufSize;
|
---|
161 |
|
---|
162 | size_t cbOld = 0;
|
---|
163 | ASMAtomicSubSize(&pBuf->cbBufUsed, cbSize, &cbOld);
|
---|
164 | }
|
---|
165 |
|
---|
166 | RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
|
---|
167 | {
|
---|
168 | /* Validate input. */
|
---|
169 | AssertPtr(pBuf);
|
---|
170 | Assert(cbReqSize > 0);
|
---|
171 | AssertPtr(ppvStart);
|
---|
172 | AssertPtr(pcbSize);
|
---|
173 |
|
---|
174 | size_t uFree;
|
---|
175 | size_t uSize;
|
---|
176 |
|
---|
177 | *ppvStart = 0;
|
---|
178 | *pcbSize = 0;
|
---|
179 |
|
---|
180 | /* How much is free? */
|
---|
181 | size_t cbSize = 0;
|
---|
182 | ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
|
---|
183 | uFree = pBuf->cbBufSize - cbSize;
|
---|
184 | if (uFree > 0)
|
---|
185 | {
|
---|
186 | /* Get the size out of the requested size, the write block till the end
|
---|
187 | * of the buffer & the currently free size. */
|
---|
188 | uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, uFree));
|
---|
189 | if (uSize > 0)
|
---|
190 | {
|
---|
191 | /* Return the pointer address which point to the current write
|
---|
192 | * position. */
|
---|
193 | *ppvStart = (char*)pBuf->pvBuf + pBuf->uWritePos;
|
---|
194 | *pcbSize = uSize;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
|
---|
200 | {
|
---|
201 | /* Validate input. */
|
---|
202 | AssertPtr(pBuf);
|
---|
203 |
|
---|
204 | /* Split at the end of the buffer. */
|
---|
205 | pBuf->uWritePos = (pBuf->uWritePos + cbSize) % pBuf->cbBufSize;
|
---|
206 |
|
---|
207 | size_t cbOld = 0;
|
---|
208 | ASMAtomicAddSize(&pBuf->cbBufUsed, cbSize, &cbOld);
|
---|
209 | }
|
---|
210 |
|
---|