Process Hacker
circbuf_h.h
Go to the documentation of this file.
1 #ifdef T
2 
3 #include <templ.h>
4 
5 typedef struct T___(_PH_CIRCULAR_BUFFER, T)
6 {
7  ULONG Size;
8 #ifdef PH_CIRCULAR_BUFFER_POWER_OF_TWO_SIZE
9  ULONG SizeMinusOne;
10 #endif
11  ULONG Count;
12  LONG Index;
13  T *Data;
14 } T___(PH_CIRCULAR_BUFFER, T), *T___(PPH_CIRCULAR_BUFFER, T);
15 
17 VOID
18 NTAPI
19 T___(PhInitializeCircularBuffer, T)(
20  _Out_ T___(PPH_CIRCULAR_BUFFER, T) Buffer,
21  _In_ ULONG Size
22  );
23 
25 VOID
26 NTAPI
27 T___(PhDeleteCircularBuffer, T)(
28  _Inout_ T___(PPH_CIRCULAR_BUFFER, T) Buffer
29  );
30 
32 VOID
33 NTAPI
34 T___(PhResizeCircularBuffer, T)(
35  _Inout_ T___(PPH_CIRCULAR_BUFFER, T) Buffer,
36  _In_ ULONG NewSize
37  );
38 
40 VOID
41 NTAPI
42 T___(PhClearCircularBuffer, T)(
43  _Inout_ T___(PPH_CIRCULAR_BUFFER, T) Buffer
44  );
45 
47 VOID
48 NTAPI
49 T___(PhCopyCircularBuffer, T)(
50  _Inout_ T___(PPH_CIRCULAR_BUFFER, T) Buffer,
51  _Out_writes_(Count) T *Destination,
52  _In_ ULONG Count
53  );
54 
55 FORCEINLINE T T___(PhGetItemCircularBuffer, T)(
56  _In_ T___(PPH_CIRCULAR_BUFFER, T) Buffer,
57  _In_ LONG Index
58  )
59 {
60 #ifdef PH_CIRCULAR_BUFFER_POWER_OF_TWO_SIZE
61  return Buffer->Data[(Buffer->Index + Index) & Buffer->SizeMinusOne];
62 #else
63  ULONG size;
64 
65  size = Buffer->Size;
66  // Modulo is dividend-based.
67  return Buffer->Data[(((Buffer->Index + Index) % size) + size) % size];
68 #endif
69 }
70 
71 FORCEINLINE VOID T___(PhSetItemCircularBuffer, T)(
72  _Inout_ T___(PPH_CIRCULAR_BUFFER, T) Buffer,
73  _In_ LONG Index,
74  _In_ T Value
75  )
76 {
77 #ifdef PH_CIRCULAR_BUFFER_POWER_OF_TWO_SIZE
78  Buffer->Data[(Buffer->Index + Index) & Buffer->SizeMinusOne] = Value;
79 #else
80  ULONG size;
81 
82  size = Buffer->Size;
83  Buffer->Data[(((Buffer->Index + Index) % size) + size) % size] = Value;
84 #endif
85 }
86 
87 FORCEINLINE VOID T___(PhAddItemCircularBuffer, T)(
88  _Inout_ T___(PPH_CIRCULAR_BUFFER, T) Buffer,
89  _In_ T Value
90  )
91 {
92 #ifdef PH_CIRCULAR_BUFFER_POWER_OF_TWO_SIZE
93  Buffer->Data[Buffer->Index = ((Buffer->Index - 1) & Buffer->SizeMinusOne)] = Value;
94 #else
95  ULONG size;
96 
97  size = Buffer->Size;
98  Buffer->Data[Buffer->Index = (((Buffer->Index - 1) % size) + size) % size] = Value;
99 #endif
100 
101  if (Buffer->Count < Buffer->Size)
102  Buffer->Count++;
103 }
104 
105 FORCEINLINE T T___(PhAddItemCircularBuffer2, T)(
106  _Inout_ T___(PPH_CIRCULAR_BUFFER, T) Buffer,
107  _In_ T Value
108  )
109 {
110  LONG index;
111  T oldValue;
112 
113 #ifdef PH_CIRCULAR_BUFFER_POWER_OF_TWO_SIZE
114  index = ((Buffer->Index - 1) & Buffer->SizeMinusOne);
115 #else
116  ULONG size;
117 
118  size = Buffer->Size;
119  index = (((Buffer->Index - 1) % size) + size) % size;
120 #endif
121 
122  Buffer->Index = index;
123  oldValue = Buffer->Data[index];
124  Buffer->Data[index] = Value;
125 
126  if (Buffer->Count < Buffer->Size)
127  Buffer->Count++;
128 
129  return oldValue;
130 }
131 
132 #endif