]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Containers/Queues/Fifo.c
2 Class for arbitrary sized FIFO queues.
4 The FIFO is empty if both the Read and Write indexes are equal.
5 The FIFO is full if the next write would make the Read and Write indexes equal.
7 Member variable NumElements is the maximum number of elements that can be
9 If NumElements is ZERO, there is an error.
10 NumElements should be in the range 1:N.
12 Members WriteIndex and ReadIndex are indexes into the array implementing the
13 FIFO. They should be in the range 0:(NumElements - 1).
15 One element of the FIFO is always reserved as the "terminator" element. Thus,
16 the capacity of a FIFO is actually NumElements-1.
18 Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>
19 This program and the accompanying materials are licensed and made available
20 under the terms and conditions of the BSD License which accompanies this
21 distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php.
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 #include <Library/BaseLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/MemoryAllocationLib.h>
32 #include <LibConfig.h>
39 #include <Containers/Fifo.h>
41 /** Determine number of items available to read from the FIFO.
43 The number of items are either the number of bytes, or the number of elements
44 depending upon the value of the As enumerator.
46 @param[in] Self Pointer to the FIFO instance.
47 @param[in] As An enumeration variable whose value determines whether the
48 returned value is the number of bytes or the number of elements
49 currently contained by the FIFO.
51 @retval 0 The FIFO is empty.
52 @retval >=0 The number of items contained by the FIFO.
64 if(Self
->ReadIndex
<= Self
->WriteIndex
) {
65 Count
= Self
->WriteIndex
- Self
->ReadIndex
;
68 Count
= Self
->NumElements
- (Self
->ReadIndex
- Self
->WriteIndex
);
71 Count
*= Self
->ElementSize
;
76 /** Determine amount of free space in the FIFO that can be written into.
78 The number of items are either the number of bytes, or the number of elements
79 depending upon the value of the As enumerator.
81 @param[in] Self Pointer to the FIFO instance.
82 @param[in] As An enumeration variable whose value determines whether the
83 returned value is the number of bytes or the number of elements
84 currently available in the FIFO.
86 @retval 0 The FIFO is full.
87 @retval >=0 The number of items which can be accepted by the FIFO.
101 RDex
= Self
->ReadIndex
;
102 WDex
= Self
->WriteIndex
;
105 Count
= (Self
->NumElements
- (WDex
- RDex
)) - 1;
108 Count
= (RDex
- WDex
)-1;
111 Count
*= Self
->ElementSize
;
116 /** Reduce the FIFO contents by NumElem elements.
118 @param[in] Self Pointer to the FIFO instance.
119 @param[in] NumElem Number of elements to delete from the FIFO.
121 @retval 0 FIFO is now empty.
122 @retval N>0 There are still N elements in the FIFO.
123 @retval -1 There are fewer than NumElem elements in the FIFO.
135 assert(Self
!= NULL
);
137 QCount
= FIFO_NumInQueue(Self
, AsElements
);
138 if(NumElem
> QCount
) {
143 RetVal
= (ssize_t
)ModuloAdd(Self
->ReadIndex
, (UINT32
)NumElem
, Self
->NumElements
);
144 Self
->ReadIndex
= (UINT32
)RetVal
;
146 RetVal
= (ssize_t
)(QCount
- NumElem
);
151 /** Test whether the FIFO is empty.
153 @param[in] Self Pointer to the FIFO instance.
155 @retval TRUE The FIFO is empty.
156 @retval FALSE There is data in the FIFO.
165 assert(Self
!= NULL
);
167 return (BOOLEAN
)(Self
->WriteIndex
== Self
->ReadIndex
);
170 /** Test whether the FIFO is full.
172 @param[in] Self Pointer to the FIFO instance.
174 @retval TRUE The FIFO is full.
175 @retval FALSE There is free space in the FIFO.
184 assert(Self
!= NULL
);
186 return (BOOLEAN
)(ModuloIncrement(Self
->WriteIndex
, Self
->NumElements
) == (INT32
)Self
->ReadIndex
);
189 /** Add one or more elements to the FIFO.
191 This function allows one to add one or more elements, as specified by Count,
192 to the FIFO. Each element is of the size specified when the FIFO object
193 was instantiated (FIFO.ElementSize).
195 pElement points to the first byte of the first element to be added.
196 If multiple elements are to be added, the elements are expected to be
197 organized as a packed array.
199 @param[in] Self Pointer to the FIFO instance.
200 @param[in] pElement Pointer to the element(s) to enqueue (add).
201 @param[in] Count Number of elements to add.
203 @retval 0 The FIFO is full.
204 @retval >=0 The number of elements added to the FIFO.
211 const void *pElement
,
218 UINT32 SizeOfElement
;
221 assert(Self
!= NULL
);
222 assert(pElement
!= NULL
);
224 if(FIFO_IsFull(Self
)) { // FIFO is full so can't add to it
225 Count
= 0; // Zero characters added
227 else { // Otherwise, FIFO is not full...
228 Count
= MIN(Count
, Self
->FreeSpace(Self
, AsElements
)); // Smaller of requested or available space
229 SizeOfElement
= Self
->ElementSize
; // Size of Elements, in bytes
230 Windex
= Self
->WriteIndex
; // Index of first writable slot in FIFO
232 ElemPtr
= (uintptr_t)pElement
; // Addr. of element to add, as an integer
233 QPtr
= (uintptr_t)Self
->Queue
+ (SizeOfElement
* Windex
); // Addr. in FIFO to write, as an integer
235 for(i
= 0; i
< Count
; ++i
) { // For Count elements...
236 (void)CopyMem((void *)QPtr
, (const void *)ElemPtr
, SizeOfElement
); // Copy an element into the FIFO
237 Windex
= (UINT32
)ModuloIncrement(Windex
, Self
->NumElements
); // Increment the Write index, wrap if necessary
238 if(Windex
== 0) { // If the index wrapped
239 QPtr
= (uintptr_t)Self
->Queue
; // Go to the beginning
242 QPtr
+= SizeOfElement
; // Otherwise, point to next in FIFO
244 ElemPtr
+= SizeOfElement
; // And also point to next Element to add
246 Self
->WriteIndex
= Windex
; // Finally, save the new Write Index
248 return Count
; // Number of elements added to FIFO
251 /** Read or copy elements from the FIFO.
253 This function allows one to read one or more elements, as specified by Count,
254 from the FIFO. Each element is of the size specified when the FIFO object
255 was instantiated (FIFO.ElementSize).
257 pElement points to the destination of the first byte of the first element
258 to be read. If multiple elements are to be read, the elements are expected
259 to be organized as a packed array.
261 @param[in] Self Pointer to the FIFO instance.
262 @param[out] pElement Pointer to where to store the element(s) read from the FIFO.
263 @param[in] Count Number of elements to dequeue.
264 @param[in] Consume If TRUE, consume read elements. Otherwise, preserve.
266 @retval 0 The FIFO is empty.
267 @retval >=0 The number of elements read from the FIFO.
281 UINT32 SizeOfElement
;
284 assert(Self
!= NULL
);
285 assert(pElement
!= NULL
);
287 if(FIFO_IsEmpty(Self
)) {
291 RDex
= Self
->ReadIndex
; // Get this FIFO's Read Index
292 SizeOfElement
= Self
->ElementSize
; // Get size of this FIFO's elements
293 Count
= MIN(Count
, Self
->Count(Self
, AsElements
)); // Lesser of requested or actual
295 QPtr
= (UINTN
)Self
->Queue
+ (RDex
* SizeOfElement
); // Point to Read location in FIFO
296 for(i
= 0; i
< Count
; ++i
) { // Iterate Count times...
297 (void)CopyMem(pElement
, (const void *)QPtr
, SizeOfElement
); // Copy element from FIFO to caller's buffer
298 RDex
= (UINT32
)ModuloIncrement(RDex
, Self
->NumElements
); // Increment Read Index
299 if(RDex
== 0) { // If the index wrapped
300 QPtr
= (UINTN
)Self
->Queue
; // Point back to beginning of data
303 QPtr
+= SizeOfElement
; // Point to the next element in FIFO
305 pElement
= (char*)pElement
+ SizeOfElement
; // Point to next element in caller's buffer
306 } // Iterate: for loop
307 if(Consume
) { // If caller requests data consumption
308 Self
->ReadIndex
= RDex
; // Set FIFO's Read Index to new Index
311 return Count
; // Return number of elements actually read
314 /** Read elements from the FIFO.
316 Read the specified number of elements from the FIFO, removing each element read.
317 The number of elements actually read from the FIFO is returned. This number can differ
318 from the Count requested if more elements are requested than are in the FIFO.
320 @param[in] Self Pointer to the FIFO instance.
321 @param[out] pElement Pointer to where to store the element read from the FIFO.
322 @param[in] Count Number of elements to dequeue.
324 @retval 0 The FIFO is empty.
325 @retval >=0 The number of elements read from the FIFO.
336 return FIFO_Dequeue(Self
, pElement
, Count
, TRUE
);
339 /** Make a copy of the FIFO's data.
340 The contents of the FIFO is copied out and linearized without affecting the
341 FIFO contents. This function is idempotent.
343 @param[in] Self Pointer to the FIFO instance.
344 @param[out] pElement Pointer to where to store the elements copied from the FIFO.
345 @param[in] Count Number of elements to copy.
347 @retval 0 The FIFO is empty.
348 @retval >=0 The number of elements copied from the FIFO.
359 return FIFO_Dequeue(Self
, pElement
, Count
, FALSE
);
362 /** Get the FIFO's current Read Index.
364 @param[in] Self Pointer to the FIFO instance.
373 assert(Self
!= NULL
);
375 return Self
->ReadIndex
;
378 /** Get the FIFO's current Write Index.
380 @param[in] Self Pointer to the FIFO instance.
382 @return The current value of the FIFO's WriteIndex member is returned.
391 assert(Self
!= NULL
);
393 return Self
->WriteIndex
;
396 /** Cleanly delete a FIFO instance.
398 @param[in] Self Pointer to the FIFO instance.
407 assert(Self
!= NULL
);
409 if(Self
->Queue
!= NULL
) {
410 FreePool(Self
->Queue
);
411 Self
->Queue
= NULL
; // Zombie catcher
416 /** Empty the FIFO, discarding up to NumToFlush elements.
418 @param[in] Self Pointer to the FIFO instance.
419 @param[in] NumToFlush Number of elements to flush from the FIFO.
420 If larger than the number of elements in the
421 FIFO, the FIFO is emptied.
423 @return Returns the number of elements remaining in the FIFO after the flush.
436 assert(Self
!= NULL
);
438 NumInQ
= FIFO_NumInQueue(Self
, AsElements
);
439 if(NumToFlush
>= NumInQ
) {
441 Self
->WriteIndex
= 0;
445 Remainder
= FIFO_Reduce(Self
, NumToFlush
);
450 /** Remove the most recently added element from the FIFO.
452 @param[in] Self Pointer to the FIFO instance.
454 @return Returns the number of elements remaining in the FIFO.
465 assert(Self
!= NULL
);
467 Remainder
= FIFO_NumInQueue(Self
, AsElements
);
469 Self
->WriteIndex
= (UINT32
)ModuloDecrement(Self
->WriteIndex
, Self
->NumElements
);
475 /** Construct a new instance of a FIFO Queue.
477 @param[in] NumElements Number of elements to be contained in the new FIFO.
478 @param[in] ElementSize Size, in bytes, of an element.
480 @retval NULL Unable to create the instance.
481 @retval NonNULL Pointer to the new FIFO instance.
494 if((NumElements
> 2) && (ElementSize
> 0)) {
495 FIFO
= (cFIFO
*)AllocatePool(sizeof(cFIFO
));
497 Queue
= (UINT8
*)AllocateZeroPool(NumElements
* ElementSize
);
499 FIFO
->Write
= FIFO_Enqueue
;
500 FIFO
->Read
= FIFO_Read
;
501 FIFO
->Copy
= FIFO_Copy
;
502 FIFO
->IsEmpty
= FIFO_IsEmpty
;
503 FIFO
->IsFull
= FIFO_IsFull
;
504 FIFO
->Count
= FIFO_NumInQueue
;
505 FIFO
->FreeSpace
= FIFO_FreeSpace
;
506 FIFO
->Flush
= FIFO_Flush
;
507 FIFO
->Truncate
= FIFO_Truncate
;
508 FIFO
->Delete
= FIFO_Delete
;
509 FIFO
->GetRDex
= FIFO_GetRDex
;
510 FIFO
->GetWDex
= FIFO_GetWDex
;
513 FIFO
->ElementSize
= (UINT32
)ElementSize
;
514 FIFO
->NumElements
= (UINT32
)NumElements
;
516 FIFO
->WriteIndex
= 0;