VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/circbuf.cpp@ 37209

Last change on this file since 37209 was 37209, checked in by vboxsync, 14 years ago

circbuf: Use ASMAtomic*Z instead of ASMAtomic*Size because the latter is causing trouble on recent microsoft compilers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: circbuf.cpp 37209 2011-05-25 09:33:12Z 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.215389.xyz. 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
38RTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
39{
40 /* Validate input. */
41 AssertPtrReturn(ppBuf, VERR_INVALID_POINTER);
42 AssertReturn(cbSize > 0, VERR_INVALID_PARAMETER);
43
44 PRTCIRCBUF pTmpBuf;
45 pTmpBuf = (PRTCIRCBUF)RTMemAllocZ(sizeof(RTCIRCBUF));
46 if (!pTmpBuf)
47 return VERR_NO_MEMORY;
48
49 pTmpBuf->pvBuf = RTMemAlloc(cbSize);
50 if (pTmpBuf->pvBuf)
51 {
52 pTmpBuf->cbBufSize = cbSize;
53 *ppBuf = pTmpBuf;
54 return VINF_SUCCESS;
55 }
56
57 RTMemFree(pTmpBuf);
58 return VERR_NO_MEMORY;
59}
60
61
62RTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf)
63{
64 /* Validate input. */
65 if (!pBuf)
66 return;
67 AssertPtr(pBuf);
68 RTMemFree(pBuf->pvBuf);
69 RTMemFree(pBuf);
70}
71
72
73RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf)
74{
75 /* Validate input. */
76 AssertPtr(pBuf);
77
78 pBuf->uReadPos = 0;
79 pBuf->uWritePos = 0;
80 pBuf->cbBufUsed = 0;
81}
82
83
84RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf)
85{
86 /* Validate input. */
87 AssertPtrReturn(pBuf, 0);
88
89 return pBuf->cbBufSize - ASMAtomicReadZ(&pBuf->cbBufUsed);
90}
91
92
93RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf)
94{
95 /* Validate input. */
96 AssertPtrReturn(pBuf, 0);
97
98 return ASMAtomicReadZ(&pBuf->cbBufUsed);
99}
100
101
102RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf)
103{
104 /* Validate input. */
105 AssertPtrReturn(pBuf, 0);
106
107 return pBuf->cbBufSize;
108}
109
110
111RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
112{
113 /* Validate input. */
114 AssertPtr(pBuf);
115 Assert(cbReqSize > 0);
116 AssertPtr(ppvStart);
117 AssertPtr(pcbSize);
118
119 *ppvStart = 0;
120 *pcbSize = 0;
121
122 /* How much is in use? */
123 size_t cbUsed = ASMAtomicReadZ(&pBuf->cbBufUsed);
124 if (cbUsed > 0)
125 {
126 /* Get the size out of the requested size, the read block till the end
127 * of the buffer & the currently used size. */
128 size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uReadPos, cbUsed));
129 if (cbSize > 0)
130 {
131 /* Return the pointer address which point to the current read
132 * position. */
133 *ppvStart = (char *)pBuf->pvBuf + pBuf->uReadPos;
134 *pcbSize = cbSize;
135 }
136 }
137}
138
139
140RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
141{
142 /* Validate input. */
143 AssertPtr(pBuf);
144
145 /* Split at the end of the buffer. */
146 pBuf->uReadPos = (pBuf->uReadPos + cbSize) % pBuf->cbBufSize;
147
148 ASMAtomicSubZ(&pBuf->cbBufUsed, cbSize);
149}
150
151
152RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
153{
154 /* Validate input. */
155 AssertPtr(pBuf);
156 Assert(cbReqSize > 0);
157 AssertPtr(ppvStart);
158 AssertPtr(pcbSize);
159
160 *ppvStart = 0;
161 *pcbSize = 0;
162
163 /* How much is free? */
164 size_t cbFree = pBuf->cbBufSize - ASMAtomicReadZ(&pBuf->cbBufUsed);
165 if (cbFree > 0)
166 {
167 /* Get the size out of the requested size, the write block till the end
168 * of the buffer & the currently free size. */
169 size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, cbFree));
170 if (cbSize > 0)
171 {
172 /* Return the pointer address which point to the current write
173 * position. */
174 *ppvStart = (char*)pBuf->pvBuf + pBuf->uWritePos;
175 *pcbSize = cbSize;
176 }
177 }
178}
179
180
181RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
182{
183 /* Validate input. */
184 AssertPtr(pBuf);
185
186 /* Split at the end of the buffer. */
187 pBuf->uWritePos = (pBuf->uWritePos + cbSize) % pBuf->cbBufSize;
188
189 size_t cbOldIgnored = 0;
190 ASMAtomicAddZ(&pBuf->cbBufUsed, cbSize);
191}
192
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette