VirtualBox

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

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

circbuf: code cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* $Id: circbuf.cpp 37208 2011-05-25 09:22:18Z 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 size_t cbSize = 0;
90 ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
91 return pBuf->cbBufSize - cbSize;
92}
93
94
95RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf)
96{
97 /* Validate input. */
98 AssertPtrReturn(pBuf, 0);
99
100 size_t cbSize = 0;
101 ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
102 return cbSize;
103}
104
105
106RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf)
107{
108 /* Validate input. */
109 AssertPtrReturn(pBuf, 0);
110
111 return pBuf->cbBufSize;
112}
113
114
115RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
116{
117 /* Validate input. */
118 AssertPtr(pBuf);
119 Assert(cbReqSize > 0);
120 AssertPtr(ppvStart);
121 AssertPtr(pcbSize);
122
123 size_t uUsed = 0;
124 size_t uSize = 0;
125
126 *ppvStart = 0;
127 *pcbSize = 0;
128
129 /* How much is in use? */
130 ASMAtomicReadSize(&pBuf->cbBufUsed, &uUsed);
131 if (uUsed > 0)
132 {
133 /* Get the size out of the requested size, the read block till the end
134 * of the buffer & the currently used size. */
135 uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uReadPos, uUsed));
136 if (uSize > 0)
137 {
138 /* Return the pointer address which point to the current read
139 * position. */
140 *ppvStart = (char *)pBuf->pvBuf + pBuf->uReadPos;
141 *pcbSize = uSize;
142 }
143 }
144}
145
146
147RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
148{
149 /* Validate input. */
150 AssertPtr(pBuf);
151
152 /* Split at the end of the buffer. */
153 pBuf->uReadPos = (pBuf->uReadPos + cbSize) % pBuf->cbBufSize;
154
155 size_t cbOld = 0;
156 ASMAtomicSubSize(&pBuf->cbBufUsed, cbSize, &cbOld);
157}
158
159
160RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
161{
162 /* Validate input. */
163 AssertPtr(pBuf);
164 Assert(cbReqSize > 0);
165 AssertPtr(ppvStart);
166 AssertPtr(pcbSize);
167
168 *ppvStart = 0;
169 *pcbSize = 0;
170
171 /* How much is free? */
172 size_t cbSize = 0;
173 ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
174 size_t uFree = pBuf->cbBufSize - cbSize;
175 if (uFree > 0)
176 {
177 /* Get the size out of the requested size, the write block till the end
178 * of the buffer & the currently free size. */
179 size_t uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, uFree));
180 if (uSize > 0)
181 {
182 /* Return the pointer address which point to the current write
183 * position. */
184 *ppvStart = (char*)pBuf->pvBuf + pBuf->uWritePos;
185 *pcbSize = uSize;
186 }
187 }
188}
189
190
191RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
192{
193 /* Validate input. */
194 AssertPtr(pBuf);
195
196 /* Split at the end of the buffer. */
197 pBuf->uWritePos = (pBuf->uWritePos + cbSize) % pBuf->cbBufSize;
198
199 size_t cbOldIgnored = 0;
200 ASMAtomicAddSize(&pBuf->cbBufUsed, cbSize, &cbOldIgnored);
201}
202
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