After studying the kfifo code of kernel 2.6.26 today, I feel that the code can be implemented smartly. The subscript at the end of the queue header is not restricted by the length of the queue. Even if the subscript at the queue header is greater than the length of the queue, it can also be used, the principle is that not all data is stored in the memory space between the first (FIFO-> out) and the last (FIFO-> In) of the team, instead, data that exceeds the length of the head and tail of the team is placed at the beginning of the buffer of the entire queue,
The blue part is the memory segment where the real data is located, and the white part is actually the logically assumed data location. That is to say, to give the user a real sense of queue-push data from the end, to pull data from the header, you must make FIFO-> out and FIFO-> in only push in one direction. However, because the buffer allocated by FIFO is a finite segment of continuous memory, FIFO-> out and FIFO-> In will "Cross-border" sooner or later. Therefore, this is the case in the Code to deal with the so-called "Cross-border" situation:
First, the operation is performed on the inbound queue to ensure that FIFO> out and FIFO> in are always pushed to the right:
Unsigned int _ k1_o_put (struct kfifo * FIFO, unsigned char * buffer, unsigned int Len)
{
Unsigned int L;
Len = min (Len, FIFO-> size-FIFO-> in + FIFO-> out );
/* First put the data starting from FIFO-> in to buffer end */
L = min (Len, FIFO-> size-(FIFO-> In & (FIFO-> size-1 )));
Memcpy (FIFO-> buffer + (FIFO-> In & (FIFO-> size-1), buffer, L );
/* Then put the rest (if any) at the beginning of the buffer */
Memcpy (FIFO-> buffer, buffer + L, len-L );
FIFO-> in + = Len;
Return Len;
}
It can be seen that if the FIFO-> In is smaller than the buffer-> size, the buffer-> size-FIFO-> in memory space is first completed, and the remaining part is, it is stored at the beginning of the available space to be transferred to the buffer. If FIFO> In is greater than buffer> size, the data to be imported to the queue is directly placed at the beginning of the buffer available space.
The second is the operation of the outbound queue:
Unsigned int _ k1_o_get (struct kfifo * FIFO, unsigned char * buffer, unsigned int Len)
{
Unsigned int L;
Len = min (Len, FIFO-> In-FIFO-> out );
/* First get the data from FIFO-> out until the end of the buffer */
L = min (Len, FIFO-> size-(FIFO-> out & (FIFO-> size-1 )));
Memcpy (buffer, FIFO-> buffer + (FIFO-> out & (FIFO-> size-1), L );
/* Then get the rest (if any) from the beginning of the buffer */
Memcpy (buffer + L, FIFO-> buffer, len-L );
FIFO-> out + = Len;
Return Len;
}
Case 1: FIFO> In is greater than FIFO> size while FIFO> out is smaller than FIFO> size (that is, only FIFO> in "out of bounds "), read the segment from FIFO> out to FIFO> size-1, with the size of L bytes, and then read the rest starting from buffer, the data size is len-l bytes (as shown in figure, that is, the Data Segment A is read before the data segment B is read );
Case 2: FIFO> in and FIFO> out are "out of bounds", then l = min (Len, FIFO-> size-(FIFO-> out & (FIFO-> size-1); this statement takes effect, the result of FIFO-> out & FIFO-> size-1 is the Offset Value of the memory address of the data to be read relative to the buffer start address (as shown in, the left is actually the data a segment in the memory, and the right dotted box is the location of the Logical Data a segment );
Finally, I have some questions. FIFO> out and FIFO> in are unsign int (32bit) types. If the application uses kfifo, in the end, the FIFO-> out or FIFO-> in overflow occurs. How can this problem be solved? This problem is not mentioned in the kfifo code.
Source: http://yiphon.diandian.com/post/2011-09-10/4918347