Skip to contentSkip to conversation

iio: buffer: align the size of scan bytes to size of the largest element

PATCH New Lars Möllendorf 1 participant 1 message Jan 24, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: buffer: align the size of scan bytes to size of the largest element

commit 883f61653069 ("iio: buffer: align the size of scan bytes to size of the largest element")

Previous versions of iio_compute_scan_bytes only aligned each element to its own length (i.e. its own natural alignment). Because multiple consecutive sets of scan elements are buffered this does not work in case the computed scan bytes do not align with the natural alignment of the first scan element in the set.

This commit fixes this by aligning the scan bytes to the natural alignment of the largest scan element in the set.

Fixes 959d2952d124 ("staging:iio: make iio_sw_buffer_preenable much more general.")
---
 drivers/iio/industrialio-buffer.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iio/industrialio-buffer.c +5 -1
index 0f6f63b20263..3534f981e561 100644
@@ -516,7 +516,7 @@ static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
 {
 	const struct iio_chan_spec *ch;
 	unsigned bytes = 0;
-	int length, i;
+	int length, i, largest = 0;
 
 	/* How much space will the demuxed element take? */
 	for_each_set_bit(i, mask,
@@ -529,6 +529,7 @@ static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
 			length = ch->scan_type.storagebits / 8;
 		bytes = ALIGN(bytes, length);
 		bytes += length;
+		largest = max(largest, length);
 	}
 	if (timestamp) {
 		ch = iio_find_channel_from_si(indio_dev,
@@ -540,7 +541,10 @@ static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
 			length = ch->scan_type.storagebits / 8;
 		bytes = ALIGN(bytes, length);
 		bytes += length;
+		largest = max(largest, length);
 	}
+
+	bytes = ALIGN(bytes, largest);
 	return bytes;
 }
 

--
2.23.0