[PATCH v2 03/11] staging: comedi: comedi_buf: factor out common comedi_buf_write_alloc_* code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The only difference between comedi_buf_write_alloc() and the *_strict()
version is that the *_strict() one will only allocate the chunk if it
can completely fulfill the request.

Factor out the common code and add a flag parameter to indicate the 'strict'
usage. Change the exported functions so they are just wrappers around the
common function.

Cleanup the common function a bit and use the comedi_buf_write_n_available()
helper to determine the number of bytes available.

Signed-off-by: H Hartley Sweeten <hsweeten@xxxxxxxxxxxxxxxxxxx>
Cc: Ian Abbott <abbobbi@xxxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
 drivers/staging/comedi/comedi_buf.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)

diff --git a/drivers/staging/comedi/comedi_buf.c b/drivers/staging/comedi/comedi_buf.c
index a6b22c5..56793d0 100644
--- a/drivers/staging/comedi/comedi_buf.c
+++ b/drivers/staging/comedi/comedi_buf.c
@@ -196,37 +196,32 @@ unsigned int comedi_buf_write_n_available(struct comedi_async *async)
 	return nbytes;
 }
 
-/* allocates chunk for the writer from free buffer space */
-unsigned int comedi_buf_write_alloc(struct comedi_async *async,
-				    unsigned int nbytes)
+static unsigned int __comedi_buf_write_alloc(struct comedi_async *async,
+					     unsigned int nbytes, int strict)
 {
-	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
+	unsigned int available = comedi_buf_write_n_available(async);
 
-	if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
-		nbytes = free_end - async->buf_write_alloc_count;
+	if (nbytes > available)
+		nbytes = strict ? 0 : available;
 
 	async->buf_write_alloc_count += nbytes;
-	/* barrier insures the read of buf_read_count above occurs before
-	   we write data to the write-alloc'ed buffer space */
-	smp_mb();
+
 	return nbytes;
 }
+
+/* allocates chunk for the writer from free buffer space */
+unsigned int comedi_buf_write_alloc(struct comedi_async *async,
+				    unsigned int nbytes)
+{
+	return __comedi_buf_write_alloc(async, nbytes, 0);
+}
 EXPORT_SYMBOL(comedi_buf_write_alloc);
 
 /* allocates nothing unless it can completely fulfill the request */
 unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
 					   unsigned int nbytes)
 {
-	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
-
-	if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
-		nbytes = 0;
-
-	async->buf_write_alloc_count += nbytes;
-	/* barrier insures the read of buf_read_count above occurs before
-	   we write data to the write-alloc'ed buffer space */
-	smp_mb();
-	return nbytes;
+	return __comedi_buf_write_alloc(async, nbytes, 1);
 }
 
 /* munging is applied to data by core as it passes between user
-- 
1.8.0

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/devel


[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux