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