1 | /** @file
|
---|
2 | * IPRT - Lock Free Circular Buffer
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2010-2020 Oracle Corporation
|
---|
7 | *
|
---|
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.
|
---|
15 | *
|
---|
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.
|
---|
21 | *
|
---|
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.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef IPRT_INCLUDED_circbuf_h
|
---|
27 | #define IPRT_INCLUDED_circbuf_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/types.h>
|
---|
33 |
|
---|
34 | /** @defgroup grp_rt_circbuf RTCircBuf - Lock Free Circular Buffer
|
---|
35 | * @ingroup grp_rt
|
---|
36 | *
|
---|
37 | * Implementation of a lock free circular buffer which could be used in a multi
|
---|
38 | * threaded environment. Note that only the acquire, release and getter
|
---|
39 | * functions are threading aware. So don't use reset if the circular buffer is
|
---|
40 | * still in use.
|
---|
41 | *
|
---|
42 | * @{
|
---|
43 | */
|
---|
44 |
|
---|
45 | RT_C_DECLS_BEGIN
|
---|
46 |
|
---|
47 | /** Pointer to a circular buffer (abstract). */
|
---|
48 | typedef struct RTCIRCBUF *PRTCIRCBUF;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Create a circular buffer.
|
---|
52 | *
|
---|
53 | * @returns IPRT status code.
|
---|
54 | *
|
---|
55 | * @param ppBuf Where to store the buffer.
|
---|
56 | * @param cbSize The size of the new buffer.
|
---|
57 | */
|
---|
58 | RTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize);
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Destroy the circular buffer.
|
---|
62 | *
|
---|
63 | * @param pBuf The buffer to destroy. NULL is ignored.
|
---|
64 | */
|
---|
65 | RTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf);
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Reset all position information in the circular buffer.
|
---|
69 | *
|
---|
70 | * @note This function is not multi threading aware.
|
---|
71 | *
|
---|
72 | * @param pBuf The buffer to reset.
|
---|
73 | */
|
---|
74 | RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf);
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Returns the current free space of the buffer.
|
---|
78 | *
|
---|
79 | * @param pBuf The buffer to query.
|
---|
80 | */
|
---|
81 | RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf);
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Returns the current used space of the buffer.
|
---|
85 | *
|
---|
86 | * @param pBuf The buffer to query.
|
---|
87 | */
|
---|
88 | RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf);
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Returns the size of the buffer.
|
---|
92 | *
|
---|
93 | * @param pBuf The buffer to query.
|
---|
94 | */
|
---|
95 | RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf);
|
---|
96 |
|
---|
97 | RTDECL(bool) RTCircBufIsReading(PRTCIRCBUF pBuf);
|
---|
98 | RTDECL(bool) RTCircBufIsWriting(PRTCIRCBUF pBuf);
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns the current read offset (in bytes) within the buffer.
|
---|
102 | *
|
---|
103 | * @param pBuf The buffer to query.
|
---|
104 | */
|
---|
105 | RTDECL(size_t) RTCircBufOffsetRead(PRTCIRCBUF pBuf);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Returns the current write offset (in bytes) within the buffer.
|
---|
109 | *
|
---|
110 | * @param pBuf The buffer to query.
|
---|
111 | */
|
---|
112 | RTDECL(size_t) RTCircBufOffsetWrite(PRTCIRCBUF pBuf);
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Acquire a block of the circular buffer for reading.
|
---|
116 | *
|
---|
117 | * @param pBuf The buffer to acquire from.
|
---|
118 | * @param cbReqSize The requested size of the block.
|
---|
119 | * @param ppvStart The resulting memory pointer.
|
---|
120 | * @param pcbSize The resulting size of the memory pointer.
|
---|
121 | */
|
---|
122 | RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize);
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Release a block which was acquired by RTCircBufAcquireReadBlock.
|
---|
126 | *
|
---|
127 | * @param pBuf The buffer to acquire from.
|
---|
128 | * @param cbSize The size of the block.
|
---|
129 | */
|
---|
130 | RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize);
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Acquire a block of the circular buffer for writing.
|
---|
134 | *
|
---|
135 | * @param pBuf The buffer to acquire from.
|
---|
136 | * @param cbReqSize The requested size of the block.
|
---|
137 | * @param ppvStart The resulting memory pointer.
|
---|
138 | * @param pcbSize The resulting size of the memory pointer.
|
---|
139 | */
|
---|
140 | RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize);
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Release a block which was acquired by RTCircBufAcquireWriteBlock.
|
---|
144 | *
|
---|
145 | * @param pBuf The buffer to acquire from.
|
---|
146 | * @param cbSize The size of the block.
|
---|
147 | */
|
---|
148 | RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize);
|
---|
149 |
|
---|
150 | RT_C_DECLS_END
|
---|
151 |
|
---|
152 | /** @} */
|
---|
153 |
|
---|
154 | #endif /* !IPRT_INCLUDED_circbuf_h */
|
---|
155 |
|
---|