media: use ARRAY_SIZE
authorJérémy Lefaure <jeremy.lefaure@lse.epita.fr>
Sun, 1 Oct 2017 19:30:41 +0000 (15:30 -0400)
committerMauro Carvalho Chehab <mchehab@s-opensource.com>
Fri, 8 Dec 2017 15:11:00 +0000 (10:11 -0500)
Using the ARRAY_SIZE macro improves the readability of the code. Also,
it is not always useful to use a variable to store this constant
calculated at compile time.

Found with Coccinelle with the following semantic patch:
@r depends on (org || report)@
type T;
T[] E;
position p;
@@
(
 (sizeof(E)@p /sizeof(*E))
|
 (sizeof(E)@p /sizeof(E[...]))
|
 (sizeof(E)@p /sizeof(T))
)

Signed-off-by: Jérémy Lefaure <jeremy.lefaure@lse.epita.fr>
Reviewed-by: Michael Ira Krufky <mkrufky@linuxtv.org>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
drivers/media/common/saa7146/saa7146_video.c
drivers/media/dvb-frontends/cxd2841er.c
drivers/media/pci/saa7146/hexium_gemini.c
drivers/media/pci/saa7146/hexium_orion.c
drivers/media/pci/saa7146/mxb.c
drivers/media/usb/dvb-usb/cxusb.c
drivers/media/usb/dvb-usb/friio-fe.c

index 2b631eaa65b3dac434f74723af92c2400c396613..5dfc1f27d1cf7f5792c0f0ea83653eff4a187a41 100644 (file)
@@ -4,6 +4,7 @@
 #include <media/v4l2-event.h>
 #include <media/v4l2-ctrls.h>
 #include <linux/module.h>
+#include <linux/kernel.h>
 
 static int max_memory = 32;
 
@@ -86,13 +87,11 @@ static struct saa7146_format formats[] = {
    due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
    (like V4L2_PIX_FMT_YUYV) ... 8-( */
 
-static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
-
 struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
 {
-       int i, j = NUM_FORMATS;
+       int i;
 
-       for (i = 0; i < j; i++) {
+       for (i = 0; i < ARRAY_SIZE(formats); i++) {
                if (formats[i].pixelformat == fourcc) {
                        return formats+i;
                }
@@ -524,7 +523,7 @@ static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuf
 
 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
 {
-       if (f->index >= NUM_FORMATS)
+       if (f->index >= ARRAY_SIZE(formats))
                return -EINVAL;
        strlcpy((char *)f->description, formats[f->index].name,
                        sizeof(f->description));
index 48ee9bc00c069d8e329d865077f1136967aa7952..2cb97a3130be7b68ea6fb014b8149687f0c04685 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/math64.h>
 #include <linux/log2.h>
 #include <linux/dynamic_debug.h>
+#include <linux/kernel.h>
 
 #include "dvb_math.h"
 #include "dvb_frontend.h"
@@ -1696,12 +1697,10 @@ static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
                min_index = 0;
                if (delsys == SYS_DVBS) {
                        cn_data = s_cn_data;
-                       max_index = sizeof(s_cn_data) /
-                               sizeof(s_cn_data[0]) - 1;
+                       max_index = ARRAY_SIZE(s_cn_data) - 1;
                } else {
                        cn_data = s2_cn_data;
-                       max_index = sizeof(s2_cn_data) /
-                               sizeof(s2_cn_data[0]) - 1;
+                       max_index = ARRAY_SIZE(s2_cn_data) - 1;
                }
                if (value >= cn_data[min_index].value) {
                        res = cn_data[min_index].cnr_x1000;
index d31a2d4494d1a8c20778db9c7cd0496309dbabf9..39357eddee32ac35a8eeeea89c74bca256aa7d94 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <media/drv-intf/saa7146_vv.h>
 #include <linux/module.h>
+#include <linux/kernel.h>
 
 static int debug;
 module_param(debug, int, 0);
@@ -388,7 +389,7 @@ static struct saa7146_ext_vv vv_data = {
        .inputs = HEXIUM_INPUTS,
        .capabilities = 0,
        .stds = &hexium_standards[0],
-       .num_stds = sizeof(hexium_standards) / sizeof(struct saa7146_standard),
+       .num_stds = ARRAY_SIZE(hexium_standards),
        .std_callback = &std_callback,
 };
 
index 043318aa19e2e82f5878786903d4f88ead3ad3d4..461e421080f38ba66f18ebbb4b335a6645c0bb66 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <media/drv-intf/saa7146_vv.h>
 #include <linux/module.h>
+#include <linux/kernel.h>
 
 static int debug;
 module_param(debug, int, 0);
@@ -460,7 +461,7 @@ static struct saa7146_ext_vv vv_data = {
        .inputs = HEXIUM_INPUTS,
        .capabilities = 0,
        .stds = &hexium_standards[0],
-       .num_stds = sizeof(hexium_standards) / sizeof(struct saa7146_standard),
+       .num_stds = ARRAY_SIZE(hexium_standards),
        .std_callback = &std_callback,
 };
 
index 930218cc2de193d250509b798bd24ba2e8a48ce6..0144f305ea24647db426a12608944c2baaf0dc2e 100644 (file)
@@ -30,6 +30,7 @@
 #include <media/v4l2-common.h>
 #include <media/i2c/saa7115.h>
 #include <linux/module.h>
+#include <linux/kernel.h>
 
 #include "tea6415c.h"
 #include "tea6420.h"
@@ -837,7 +838,7 @@ static struct saa7146_ext_vv vv_data = {
        .inputs         = MXB_INPUTS,
        .capabilities   = V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_AUDIO,
        .stds           = &standard[0],
-       .num_stds       = sizeof(standard)/sizeof(struct saa7146_standard),
+       .num_stds       = ARRAY_SIZE(standard),
        .std_callback   = &std_callback,
 };
 
index 247dc164dd63833193514f39f8e335109383a7da..7109fc7ab74dc5831294fbebf607c394d2982f9f 100644 (file)
@@ -26,6 +26,7 @@
 #include <media/tuner.h>
 #include <linux/vmalloc.h>
 #include <linux/slab.h>
+#include <linux/kernel.h>
 
 #include "cxusb.h"
 
@@ -304,7 +305,7 @@ static int cxusb_aver_power_ctrl(struct dvb_usb_device *d, int onoff)
                        0x0e, 0x2, 0x47, 0x88,
                };
                msleep(20);
-               for (i = 0; i < sizeof(bufs)/sizeof(u8); i += 4/sizeof(u8)) {
+               for (i = 0; i < ARRAY_SIZE(bufs); i += 4 / sizeof(u8)) {
                        ret = cxusb_ctrl_msg(d, CMD_I2C_WRITE,
                                             bufs+i, 4, &buf, 1);
                        if (ret)
index b6046e0e07f6845c150d1894d3ad1c34f04b6b40..b2830c1575485084ecf8ea0f6c6a83992760f3b9 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/init.h>
 #include <linux/string.h>
 #include <linux/slab.h>
+#include <linux/kernel.h>
 
 #include "friio.h"
 
@@ -340,8 +341,6 @@ static u8 init_code[][2] = {
        {0x76, 0x0C},
 };
 
-static const int init_code_len = sizeof(init_code) / sizeof(u8[2]);
-
 static int jdvbt90502_init(struct dvb_frontend *fe)
 {
        int i = -1;
@@ -355,7 +354,7 @@ static int jdvbt90502_init(struct dvb_frontend *fe)
        msg.addr = state->config.demod_address;
        msg.flags = 0;
        msg.len = 2;
-       for (i = 0; i < init_code_len; i++) {
+       for (i = 0; i < ARRAY_SIZE(init_code); i++) {
                msg.buf = init_code[i];
                ret = i2c_transfer(state->i2c, &msg, 1);
                if (ret != 1)