ReactOS  0.4.14-dev-98-gb0d4763
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);
20  ASSERT(pBuffer);
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);
34  ASSERT(pBuffer);
35  if (pBuffer->Buffer != NULL)
37  return STATUS_SUCCESS;
38 }
39 
40 BOOLEAN
43 {
44  TRACE_(SERIAL, "IsCircularBufferEmpty(pBuffer %p)\n", pBuffer);
45  ASSERT(pBuffer);
46  return (pBuffer->ReadPosition == pBuffer->WritePosition);
47 }
48 
49 ULONG
52 {
53  TRACE_(SERIAL, "GetNumberOfElementsInCircularBuffer(pBuffer %p)\n", pBuffer);
54  ASSERT(pBuffer);
55  return (pBuffer->WritePosition + pBuffer->Length - pBuffer->ReadPosition) % pBuffer->Length;
56 }
57 
61  IN UCHAR Entry)
62 {
63  ULONG NextPosition;
64  TRACE_(SERIAL, "PushCircularBufferEntry(pBuffer %p, Entry 0x%x)\n", pBuffer, Entry);
65  ASSERT(pBuffer);
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);
81  ASSERT(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);
98  ASSERT(pBuffer);
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 }
#define IN
Definition: typedefs.h:38
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
NTSTATUS FreeCircularBuffer(IN PCIRCULAR_BUFFER pBuffer)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
struct _Entry Entry
Definition: kefuncs.h:640
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
ULONG GetNumberOfElementsInCircularBuffer(IN PCIRCULAR_BUFFER pBuffer)
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
NTSTATUS IncreaseCircularBufferSize(IN PCIRCULAR_BUFFER pBuffer, IN ULONG NewBufferSize)
BOOLEAN IsCircularBufferEmpty(IN PCIRCULAR_BUFFER pBuffer)
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
#define SERIAL_TAG
Definition: serial.h:100
#define TRACE_(x)
Definition: compat.h:66
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define BufferSize
Definition: classpnp.h:419
NTSTATUS PushCircularBufferEntry(IN PCIRCULAR_BUFFER pBuffer, IN UCHAR Entry)
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
unsigned char UCHAR
Definition: xmlstorage.h:181
NTSTATUS PopCircularBufferEntry(IN PCIRCULAR_BUFFER pBuffer, OUT PUCHAR Entry)
NTSTATUS InitializeCircularBuffer(IN PCIRCULAR_BUFFER pBuffer, IN ULONG BufferSize)
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:2966
#define STATUS_ARRAY_BOUNDS_EXCEEDED
Definition: ntstatus.h:362
base of all file and directory entries
Definition: entries.h:82