mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 3/8] drivers/media: use ARRAY_SIZE
@ 2008-11-09 16:55 Julia Lawall
  2008-11-09 17:14 ` Arjan van de Ven
  0 siblings, 1 reply; 3+ messages in thread
From: Julia Lawall @ 2008-11-09 16:55 UTC (permalink / raw)
  To: mchehab, v4l-dvb-maintainer, video4linux-list, linux-kernel,
	kernel-janitors

From: Julia Lawall <julia@diku.dk>

ARRAY_SIZE is more concise to use when the size of an array is divided by
the size of its type or the size of its first element.

The semantic patch that makes this change is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@i@
@@

#include <linux/kernel.h>

@depends on i using "paren.iso"@
type T;
T[] E;
@@

- (sizeof(E)/sizeof(E[...]))
+ ARRAY_SIZE(E)

@depends on i using "paren.iso"@
type T;
T[] E;
@@

- (sizeof(E)/sizeof(T))
+ ARRAY_SIZE(E)
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
 drivers/media/common/tuners/mxl5005s.c        |    6 +++---
 drivers/media/video/gspca/gspca.c             |    2 +-
 drivers/media/video/gspca/mars.c              |    2 +-
 drivers/media/video/gspca/spca500.c           |    4 ++--
 drivers/media/video/saa7134/saa7134-tvaudio.c |    2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/media/common/tuners/mxl5005s.c b/drivers/media/common/tuners/mxl5005s.c
index a887824..31522d2 100644
--- a/drivers/media/common/tuners/mxl5005s.c
+++ b/drivers/media/common/tuners/mxl5005s.c
@@ -3598,7 +3598,7 @@ static u16 MXL_GetInitRegister(struct dvb_frontend *fe, u8 *RegNum,
 		76, 77, 91, 134, 135, 137, 147,
 		156, 166, 167, 168, 25 };
 
-	*count = sizeof(RegAddr) / sizeof(u8);
+	*count = ARRAY_SIZE(RegAddr);
 
 	status += MXL_BlockInit(fe);
 
@@ -3630,7 +3630,7 @@ static u16 MXL_GetCHRegister(struct dvb_frontend *fe, u8 *RegNum, u8 *RegVal,
 	*/
 #endif
 
-	*count = sizeof(RegAddr) / sizeof(u8);
+	*count = ARRAY_SIZE(RegAddr);
 
 	for (i = 0 ; i < *count; i++) {
 		RegNum[i] = RegAddr[i];
@@ -3648,7 +3648,7 @@ static u16 MXL_GetCHRegister_ZeroIF(struct dvb_frontend *fe, u8 *RegNum,
 
 	u8 RegAddr[] = {43, 136};
 
-	*count = sizeof(RegAddr) / sizeof(u8);
+	*count = ARRAY_SIZE(RegAddr);
 
 	for (i = 0; i < *count; i++) {
 		RegNum[i] = RegAddr[i];
diff --git a/drivers/media/video/gspca/gspca.c b/drivers/media/video/gspca/gspca.c
index e48fbfc..e2bd10f 100644
--- a/drivers/media/video/gspca/gspca.c
+++ b/drivers/media/video/gspca/gspca.c
@@ -728,7 +728,7 @@ static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 			if (fmtdesc->index == index)
 				break;		/* new format */
 			index++;
-			if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
+			if (index >= ARRAY_SIZE(fmt_tb))
 				return -EINVAL;
 		}
 	}
diff --git a/drivers/media/video/gspca/mars.c b/drivers/media/video/gspca/mars.c
index 277ca34..492cdd3 100644
--- a/drivers/media/video/gspca/mars.c
+++ b/drivers/media/video/gspca/mars.c
@@ -123,7 +123,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
 	cam = &gspca_dev->cam;
 	cam->epaddr = 0x01;
 	cam->cam_mode = vga_mode;
-	cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
+	cam->nmodes = ARRAY_SIZE(vga_mode);
 	sd->qindex = 1;			/* set the quantization table */
 	return 0;
 }
diff --git a/drivers/media/video/gspca/spca500.c b/drivers/media/video/gspca/spca500.c
index bca106c..09ff697 100644
--- a/drivers/media/video/gspca/spca500.c
+++ b/drivers/media/video/gspca/spca500.c
@@ -633,10 +633,10 @@ static int sd_config(struct gspca_dev *gspca_dev,
 	sd->subtype = id->driver_info;
 	if (sd->subtype != LogitechClickSmart310) {
 		cam->cam_mode = vga_mode;
-		cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
+		cam->nmodes = ARRAY_SIZE(vga_mode);
 	} else {
 		cam->cam_mode = sif_mode;
-		cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
+		cam->nmodes = ARRAY_SIZE(sif_mode);
 	}
 	sd->qindex = 5;
 	sd->brightness = BRIGHTNESS_DEF;
diff --git a/drivers/media/video/saa7134/saa7134-tvaudio.c b/drivers/media/video/saa7134/saa7134-tvaudio.c
index c5d0b44..76b1640 100644
--- a/drivers/media/video/saa7134/saa7134-tvaudio.c
+++ b/drivers/media/video/saa7134/saa7134-tvaudio.c
@@ -159,7 +159,7 @@ static struct saa7134_tvaudio tvaudio[] = {
 		.mode          = TVAUDIO_FM_MONO,
 	}
 };
-#define TVAUDIO (sizeof(tvaudio)/sizeof(struct saa7134_tvaudio))
+#define TVAUDIO ARRAY_SIZE(tvaudio)
 
 /* ------------------------------------------------------------------ */
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 3/8] drivers/media: use ARRAY_SIZE
  2008-11-09 16:55 [PATCH 3/8] drivers/media: use ARRAY_SIZE Julia Lawall
@ 2008-11-09 17:14 ` Arjan van de Ven
  2008-11-12 23:17   ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Arjan van de Ven @ 2008-11-09 17:14 UTC (permalink / raw)
  To: Julia Lawall
  Cc: mchehab, v4l-dvb-maintainer, video4linux-list, linux-kernel,
	kernel-janitors

On Sun, 9 Nov 2008 17:55:03 +0100 (CET)
Julia Lawall <julia@diku.dk> wrote:

> From: Julia Lawall <julia@diku.dk>
> 
> ARRAY_SIZE is more concise to use when the size of an array is
> divided by the size of its type or the size of its first element.

Hi,
looking at your patch, I don't think I agree it's just blindly the
right thing to do.

> -	*count = sizeof(RegAddr) / sizeof(u8);
> +	*count = ARRAY_SIZE(RegAddr);

really. ARRAY_SIZE doesn't appear to be an improvement here..


-- 
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 3/8] drivers/media: use ARRAY_SIZE
  2008-11-09 17:14 ` Arjan van de Ven
@ 2008-11-12 23:17   ` Andrew Morton
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2008-11-12 23:17 UTC (permalink / raw)
  To: Arjan van de Ven
  Cc: julia, mchehab, v4l-dvb-maintainer, video4linux-list,
	linux-kernel, kernel-janitors

On Sun, 9 Nov 2008 09:14:27 -0800
Arjan van de Ven <arjan@infradead.org> wrote:

> On Sun, 9 Nov 2008 17:55:03 +0100 (CET)
> Julia Lawall <julia@diku.dk> wrote:
> 
> > From: Julia Lawall <julia@diku.dk>
> > 
> > ARRAY_SIZE is more concise to use when the size of an array is
> > divided by the size of its type or the size of its first element.
> 
> Hi,
> looking at your patch, I don't think I agree it's just blindly the
> right thing to do.
> 
> > -	*count = sizeof(RegAddr) / sizeof(u8);
> > +	*count = ARRAY_SIZE(RegAddr);

It looks OK to me?

	u8 RegAddr[] = {
		11, 12, 13, 22, 32, 43, 44, 53, 56, 59, 73,
		76, 77, 91, 134, 135, 137, 147,
		156, 166, 167, 168, 25 };

	*count = sizeof(RegAddr) / sizeof(u8);


> really. ARRAY_SIZE doesn't appear to be an improvement here..

It's a pretty typical usage of ARRAY_SIZE.  The benefits are, as usual:

- the ARRAY_SIZE construct *tells* the reader what the code is trying
  to do.  Rather than the reader having to work it out and then say "oh
  yeah, that's what it's doing".

- a reviewer doesn't have to go back and double-check that correct
  type was used.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-11-12 23:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-09 16:55 [PATCH 3/8] drivers/media: use ARRAY_SIZE Julia Lawall
2008-11-09 17:14 ` Arjan van de Ven
2008-11-12 23:17   ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®