Skip to content

Commit 5925766

Browse files
authored
Merge pull request #9 from EricccTaiwan/main
Implement overwrite-on-full behavior
2 parents 4008b10 + 96d1f72 commit 5925766

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

simrupt.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,19 @@ static inline int update_simrupt_data(void)
6161
/* Insert a value into the kfifo buffer */
6262
static void produce_data(unsigned char val)
6363
{
64-
/* Implement a kind of circular FIFO here (skip oldest element if kfifo
65-
* buffer is full).
64+
/* Implement overwrite-on-full FIFO behavior:
65+
* If the kfifo buffer is full, remove the oldest element
66+
* before inserting the new one.
6667
*/
67-
unsigned int len = kfifo_in(&rx_fifo, &val, sizeof(val));
68+
unsigned int len;
69+
if (kfifo_avail(&rx_fifo) < sizeof(val)) {
70+
unsigned char dummy;
71+
len = kfifo_out(&rx_fifo, &dummy, sizeof(dummy));
72+
if (len != sizeof(dummy))
73+
pr_warn("Failed to remove the oldest element (%u bytes)\n", len);
74+
}
75+
76+
len = kfifo_in(&rx_fifo, &val, sizeof(val));
6877
if (unlikely(len < sizeof(val)) && printk_ratelimit())
6978
pr_warn("%s: %zu bytes dropped\n", __func__, sizeof(val) - len);
7079

0 commit comments

Comments
 (0)