2
* IPRT - Lock Free Circular Buffer
6
* Copyright (C) 2010 Oracle Corporation
8
* This file is part of VirtualBox Open Source Edition (OSE), as
9
* available from http://www.virtualbox.org. This file is free software;
10
* you can redistribute it and/or modify it under the terms of the GNU
11
* General Public License (GPL) as published by the Free Software
12
* Foundation, in version 2 as it comes in the "COPYING" file of the
13
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16
* The contents of this file may alternatively be used under the terms
17
* of the Common Development and Distribution License Version 1.0
18
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19
* VirtualBox OSE distribution, in which case the provisions of the
20
* CDDL are applicable instead of those of the GPL.
22
* You may elect to license modified versions of this file under the
23
* terms and conditions of either the GPL or the CDDL or both.
26
#ifndef ___iprt_circbuf_h
27
#define ___iprt_circbuf_h
29
#include <iprt/types.h>
31
/** @defgroup grp_rt_circbuf RTCircBuf - Lock Free Circular Buffer
34
* Implementation of a lock free circular buffer which could be used in a multi
35
* threaded environment. Note that only the acquire, release and getter
36
* functions are threading aware. So don't use reset if the circular buffer is
44
typedef struct RTCIRCBUF
46
/** The current read position in the buffer. */
48
/** The current write position in the buffer. */
50
/** How much space of the buffer is currently in use. */
51
volatile size_t cbBufUsed;
52
/** How big is the buffer. */
54
/** The buffer itself. */
57
/* Pointer to a circular buffer structure */
58
typedef RTCIRCBUF* PRTCIRCBUF;
61
* Create a circular buffer.
63
* @returns IPRT status code.
65
* @param ppBuf Where to store the buffer.
66
* @param cbSize The size of the new buffer.
68
RTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize);
71
* Destroy the circular buffer.
73
* @param pBuf The buffer to destroy.
75
RTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf);
79
* Reset all position information in the circular buffer.
81
* @note This function is not multi threading aware.
83
* @param pBuf The buffer to reset.
85
RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf);
88
* Returns the current free space of the buffer.
90
* @param pBuf The buffer to query.
92
RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf);
95
* Returns the current used space of the buffer.
97
* @param pBuf The buffer to query.
99
RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf);
102
* Returns the size of the buffer.
104
* @param pBuf The buffer to query.
106
RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf);
109
* Acquire a block of the circular buffer for reading.
111
* @param pBuf The buffer to acquire from.
112
* @param cbReqSize The requested size of the block.
113
* @param ppvStart The resulting memory pointer.
114
* @param pcbSize The resulting size of the memory pointer.
116
RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize);
119
* Release a block which was acquired by RTCircBufAcquireReadBlock.
121
* @param pBuf The buffer to acquire from.
122
* @param cbSize The size of the block.
124
RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize);
127
* Acquire a block of the circular buffer for writing.
129
* @param pBuf The buffer to acquire from.
130
* @param cbReqSize The requested size of the block.
131
* @param ppvStart The resulting memory pointer.
132
* @param pcbSize The resulting size of the memory pointer.
134
RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize);
137
* Release a block which was acquired by RTCircBufAcquireWriteBlock.
139
* @param pBuf The buffer to acquire from.
140
* @param cbSize The size of the block.
142
RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize);
148
#endif /* ___iprt_circbuf_h */