forked from microsoft/Windows-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuffer.c
More file actions
338 lines (286 loc) · 9.1 KB
/
ringbuffer.c
File metadata and controls
338 lines (286 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*++
Copyright (c) Microsoft Corporation, All Rights Reserved
Module Name:
RingBuffer.c
Abstract:
This file implements the Ring Buffer
Environment:
--*/
#include "internal.h"
VOID
RingBufferInitialize(
_In_ PRING_BUFFER Self,
_In_reads_bytes_(BufferSize)
BYTE* Buffer,
_In_ size_t BufferSize
)
{
Self->Size = BufferSize;
Self->Base = Buffer;
Self->End = Buffer + BufferSize;
Self->Head = Buffer;
Self->Tail = Buffer;
}
VOID
RingBufferGetAvailableSpace(
_In_ PRING_BUFFER Self,
_Out_ size_t *AvailableSpace
)
{
BYTE* headSnapshot = NULL;
BYTE* tailSnapshot = NULL;
BYTE* tailPlusOne = NULL;
ASSERT(AvailableSpace);
//
// Take a snapshot of the head and tail pointers. We will compute the
// available space based on this snapshot. This is safe to do in a
// single-producer, single-consumer model, because -
// * A producer will call GetAvailableSpace() to determine whether
// there is enough space to write the data it is trying to write.
// The only other thread that could modify the amount of space
// available is the consumer thread, which can only increase the
// amount of space available. Hence it is safe for the producer
// to write based on this snapshot.
// * A consumer thread will call GetAvailableSpace() to determine
// whether there is enough data in the buffer for it to read.
// (Available data = Buffer size - Available space). The only
// other thread that could modify the amount of space available
// is the producer thread, which can only decrease the amount of
// space available (thereby increasing the amount of data
// available. Hence it is safe for the consumer to read based on
// this snapshot.
//
headSnapshot = Self->Head;
tailSnapshot = Self->Tail;
//
// In order to distinguish between a full buffer and an empty buffer,
// we always leave the last byte of the buffer unused. So, an empty
// buffer is denoted by -
// tail == head
// ... and a full buffer is denoted by -
// (tail+1) == head
//
tailPlusOne = ((tailSnapshot+1) == Self->End) ? Self->Base : (tailSnapshot+1);
if (tailPlusOne == headSnapshot)
{
//
// Buffer full
//
*AvailableSpace = 0;
}
else if (tailSnapshot == headSnapshot)
{
//
// Buffer empty
// The -1 in the computation below is to account for the fact that
// we always leave the last byte of the ring buffer unused in order
// to distinguish between an empty buffer and a full buffer.
//
*AvailableSpace = Self->Size - 1;
}
else
{
if (tailSnapshot > headSnapshot)
{
//
// Data has not wrapped around the end of the buffer
// The -1 in the computation below is to account for the fact
// that we always leave the last byte of the ring buffer unused
// in order to distinguish between an empty buffer and a full
// buffer.
//
*AvailableSpace = Self->Size - (tailSnapshot - headSnapshot) - 1;
}
else
{
//
// Data has wrapped around the end of the buffer
// The -1 in the computation below is to account for the fact
// that we always leave the last byte of the ring buffer unused
// in order to distinguish between an empty buffer and a full
// buffer.
//
*AvailableSpace = (headSnapshot - tailSnapshot) - 1;
}
}
}
VOID
RingBufferGetAvailableData(
_In_ PRING_BUFFER Self,
_Out_ size_t *AvailableData
)
{
size_t availableSpace;
ASSERT(AvailableData);
RingBufferGetAvailableSpace(Self, &availableSpace);
//
// The -1 in the arithmetic below accounts for the fact that we always
// keep 1 byte of the ring buffer unused in order to distinguish
// between a full buffer and an empty buffer.
//
*AvailableData = Self->Size - availableSpace - 1;
}
NTSTATUS
RingBufferWrite(
_In_ PRING_BUFFER Self,
_In_reads_bytes_(DataSize)
BYTE* Data,
_In_ size_t DataSize
)
{
size_t availableSpace;
size_t bytesToCopy;
size_t spaceFromCurrToEnd;
ASSERT(Data && (0 != DataSize));
if (Self->Tail >= Self->End)
{
return STATUS_INTERNAL_ERROR;
}
//
// Get the amount of space available in the buffer
//
RingBufferGetAvailableSpace(Self, &availableSpace);
//
// If there is not enough space to fit in all the data passed in by the
// caller then copy as much as possible and throw away the rest
//
if (availableSpace < DataSize)
{
bytesToCopy = availableSpace;
}
else
{
bytesToCopy = DataSize;
}
if (bytesToCopy)
{
//
// The buffer has some space at least
//
if ((Self->Tail + bytesToCopy) > Self->End)
{
//
// The data being written will wrap around the end of the buffer.
// So the copy has to be done in two steps -
// * X bytes from current position to end of the buffer
// * the remaining (bytesToCopy - X) from the start of the buffer
//
//
// The first step of the copy ...
//
spaceFromCurrToEnd = Self->End - Self->Tail;
RtlCopyMemory(Self->Tail, Data, spaceFromCurrToEnd);
Data += spaceFromCurrToEnd;
bytesToCopy -= spaceFromCurrToEnd;
//
// The second step of the copy ...
//
RtlCopyMemory(Self->Base, Data, bytesToCopy);
//
// Advance the tail pointer
//
Self->Tail = Self->Base + bytesToCopy;
}
else
{
//
// Data does NOT wrap around the end of the buffer. Just copy it
// over in a single step
//
RtlCopyMemory(Self->Tail, Data, bytesToCopy);
//
// Advance the tail pointer
//
Self->Tail += bytesToCopy;
if (Self->Tail == Self->End)
{
//
// We have exactly reached the end of the buffer. The next
// write should wrap around and start from the beginning.
//
Self->Tail = Self->Base;
}
}
ASSERT(Self->Tail < Self->End);
}
return STATUS_SUCCESS;
}
NTSTATUS
RingBufferRead(
_In_ PRING_BUFFER Self,
_Out_writes_bytes_to_(DataSize, *BytesCopied)
BYTE* Data,
_In_ size_t DataSize,
_Out_ size_t *BytesCopied
)
{
size_t availableData;
size_t dataFromCurrToEnd;
ASSERT(Data && (DataSize != 0));
if (Self->Head >= Self->End)
{
return STATUS_INTERNAL_ERROR;
}
//
// Get the amount of data available in the buffer
//
RingBufferGetAvailableData(Self, &availableData);
if (availableData == 0)
{
*BytesCopied = 0;
return STATUS_SUCCESS;
}
if (DataSize > availableData)
{
DataSize = availableData;
}
*BytesCopied = DataSize;
if ((Self->Head + DataSize) > Self->End)
{
//
// The data requested by the caller is wrapped around the end of the
// buffer. So we'll do the copy in two steps -
// * Copy X bytes from the current position to the end buffer into
// the caller's buffer
// * Copy (DataSize - X) bytes from the beginning to the buffer into
// the caller's buffer
//
//
// The first step of the copy ...
//
dataFromCurrToEnd = Self->End - Self->Head;
RtlCopyMemory(Data, Self->Head, dataFromCurrToEnd);
Data += dataFromCurrToEnd;
DataSize -= dataFromCurrToEnd;
//
// The second step of the copy ...
//
RtlCopyMemory(Data, Self->Base, DataSize);
//
// Advance the head pointer
//
Self->Head = Self->Base + DataSize;
}
else
{
//
// The data in the buffer is NOT wrapped around the end of the buffer.
// Simply copy the data over to the caller's buffer in a single step.
//
RtlCopyMemory(Data, Self->Head, DataSize);
//
// Advance the head pointer
//
Self->Head += DataSize;
if (Self->Head == Self->End)
{
//
// We have exactly reached the end of the buffer. The next
// read should wrap around and start from the beginning.
//
Self->Head = Self->Base;
}
}
ASSERT(Self->Head < Self->End);
return STATUS_SUCCESS;
}