Description: Make ring buffer code overflow free Author: Simon Richter Last-Update: 2013-01-08 Index: langford-0.0.20130228/langford.c =================================================================== --- langford-0.0.20130228.orig/langford.c +++ langford-0.0.20130228/langford.c @@ -55,22 +55,20 @@ static struct task_struct *dev_write_thr /**Figures out elements are used in a circular buffer */ static unsigned int circ_buff_filled(unsigned int start, unsigned int end, unsigned int len) { - int rc; - - rc = end - start; - - if (rc < 0) { - rc = len + rc; - }; - - return rc; + if (start > end) + return len - start + end; + else + return end - start; }; /**Figures out how many elements of a circular buffer are available to be filled */ static unsigned int circ_buff_avail(unsigned int start, unsigned int end, unsigned int len) { - return len - circ_buff_filled(start, end, len) - 1; + if (start > end) + return start - end - 1; + else + return len - end + start - 1; };