From 2e484610296b25f0a04b516bc144a00731d1d845 Mon Sep 17 00:00:00 2001 From: Maxim Levitsky Date: Thu, 27 Sep 2012 12:45:28 +0200 Subject: scatterlist: add sg_nents Useful helper to know the number of entries in scatterlist. Signed-off-by: Maxim Levitsky Cc: Alex Dubov Acked-by: Tejun Heo Cc: Jens Axboe Signed-off-by: Jens Axboe --- lib/scatterlist.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib/scatterlist.c') diff --git a/lib/scatterlist.c b/lib/scatterlist.c index fadae774a20..1bf60efb5e0 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -38,6 +38,28 @@ struct scatterlist *sg_next(struct scatterlist *sg) } EXPORT_SYMBOL(sg_next); +/** + * sg_nents - return total count of entries in scatterlist + * @sg: The scatterlist + * + * Description: + * Allows to know how many entries are in sg, taking into acount + * chaining as well + * + **/ +int sg_nents(struct scatterlist *sg) +{ + int nents = 0; + while (sg) { + nents++; + sg = sg_next(sg); + } + + return nents; +} +EXPORT_SYMBOL(sg_nents); + + /** * sg_last - return the last scatterlist entry in a list * @sgl: First entry in the scatterlist -- cgit v1.2.3-70-g09d2 From 232f1b51062553b7cf49f99719fbd1b8a8d80f29 Mon Sep 17 00:00:00 2001 From: Maxim Levitsky Date: Fri, 28 Sep 2012 10:38:15 +0200 Subject: scatterlist: refactor the sg_nents Replace 'while' with 'for' as suggested by Tejun Heo Signed-off-by: Maxim Levitsky Signed-off-by: Jens Axboe --- lib/scatterlist.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'lib/scatterlist.c') diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 1bf60efb5e0..8cd2ced68f3 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -49,12 +49,9 @@ EXPORT_SYMBOL(sg_next); **/ int sg_nents(struct scatterlist *sg) { - int nents = 0; - while (sg) { + int nents; + for (nents = 0; sg; sg = sg_next(sg)) nents++; - sg = sg_next(sg); - } - return nents; } EXPORT_SYMBOL(sg_nents); -- cgit v1.2.3-70-g09d2