ReactOS 0.4.16-dev-109-gf4cb10f
circularbuffer.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: Serial port driver
4 * FILE: drivers/dd/serial/circularbuffer.c
5 * PURPOSE: Operations on a circular buffer
6 *
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
8 */
9
10#include "serial.h"
11
12#include <debug.h>
13
18{
19 TRACE_(SERIAL, "InitializeCircularBuffer(pBuffer %p, BufferSize %lu)\n", pBuffer, BufferSize);
22 if (!pBuffer->Buffer)
24 pBuffer->Length = BufferSize;
25 pBuffer->ReadPosition = pBuffer->WritePosition = 0;
26 return STATUS_SUCCESS;
27}
28
32{
33 TRACE_(SERIAL, "FreeCircularBuffer(pBuffer %p)\n", pBuffer);
35 if (pBuffer->Buffer != NULL)
37 return STATUS_SUCCESS;
38}
39
43{
44 TRACE_(SERIAL, "IsCircularBufferEmpty(pBuffer %p)\n", pBuffer);
46 return (pBuffer->ReadPosition == pBuffer->WritePosition);
47}
48
52{
53 TRACE_(SERIAL, "GetNumberOfElementsInCircularBuffer(pBuffer %p)\n", pBuffer);
55 return (pBuffer->WritePosition + pBuffer->Length - pBuffer->ReadPosition) % pBuffer->Length;
56}
57
62{
63 ULONG NextPosition;
64 TRACE_(SERIAL, "PushCircularBufferEntry(pBuffer %p, Entry 0x%x)\n", pBuffer, Entry);
66 ASSERT(pBuffer->Length);
67 NextPosition = (pBuffer->WritePosition + 1) % pBuffer->Length;
68 if (NextPosition == pBuffer->ReadPosition)
70 pBuffer->Buffer[pBuffer->WritePosition] = Entry;
71 pBuffer->WritePosition = NextPosition;
72 return STATUS_SUCCESS;
73}
74
79{
80 TRACE_(SERIAL, "PopCircularBufferEntry(pBuffer %p)\n", pBuffer);
82 ASSERT(pBuffer->Length);
85 *Entry = pBuffer->Buffer[pBuffer->ReadPosition];
86 pBuffer->ReadPosition = (pBuffer->ReadPosition + 1) % pBuffer->Length;
87 return STATUS_SUCCESS;
88}
89
93 IN ULONG NewBufferSize)
94{
95 PUCHAR NewBuffer;
96
97 TRACE_(SERIAL, "IncreaseCircularBufferSize(pBuffer %p, NewBufferSize %lu)\n", pBuffer, NewBufferSize);
99 ASSERT(pBuffer->Length);
100 if (pBuffer->Length > NewBufferSize)
102 else if (pBuffer->Length == NewBufferSize)
103 return STATUS_SUCCESS;
104
105 NewBuffer = (PUCHAR)ExAllocatePoolWithTag(NonPagedPool, NewBufferSize * sizeof(UCHAR), SERIAL_TAG);
106 if (!NewBuffer)
108 RtlCopyMemory(NewBuffer, pBuffer->Buffer, pBuffer->Length * sizeof(UCHAR));
110 pBuffer->Buffer = NewBuffer;
111 pBuffer->Length = NewBufferSize;
112 return STATUS_SUCCESS;
113}
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS PushCircularBufferEntry(IN PCIRCULAR_BUFFER pBuffer, IN UCHAR Entry)
BOOLEAN IsCircularBufferEmpty(IN PCIRCULAR_BUFFER pBuffer)
NTSTATUS InitializeCircularBuffer(IN PCIRCULAR_BUFFER pBuffer, IN ULONG BufferSize)
NTSTATUS PopCircularBufferEntry(IN PCIRCULAR_BUFFER pBuffer, OUT PUCHAR Entry)
NTSTATUS FreeCircularBuffer(IN PCIRCULAR_BUFFER pBuffer)
NTSTATUS IncreaseCircularBufferSize(IN PCIRCULAR_BUFFER pBuffer, IN ULONG NewBufferSize)
ULONG GetNumberOfElementsInCircularBuffer(IN PCIRCULAR_BUFFER pBuffer)
#define BufferSize
Definition: mmc.h:75
#define NULL
Definition: types.h:112
#define TRACE_(x)
Definition: compat.h:76
#define SERIAL_TAG
Definition: serial.h:100
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define NonPagedPool
Definition: env_spec_w32.h:307
if(dx< 0)
Definition: linetemp.h:194
#define ASSERT(a)
Definition: mode.c:44
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define STATUS_ARRAY_BOUNDS_EXCEEDED
Definition: ntstatus.h:376
PVOID pBuffer
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
base of all file and directory entries
Definition: entries.h:83
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
unsigned char UCHAR
Definition: xmlstorage.h:181