[PATCH 01/01] staging: replace open-coded ARRAY_SIZEs
Jim Cromie
jim.cromie at gmail.com
Wed Apr 11 02:14:37 PDT 2012
hi Dan,
On Wed, Apr 11, 2012 at 12:54 AM, Dan Carpenter
<dan.carpenter at oracle.com> wrote:
> On Tue, Apr 10, 2012 at 04:06:41PM -0600, Jim Cromie wrote:
>> --- a/drivers/staging/media/as102/as102_usb_drv.c
>> +++ b/drivers/staging/media/as102/as102_usb_drv.c
>> @@ -367,7 +367,7 @@ static int as102_usb_probe(struct usb_interface *intf,
>> ENTER();
>>
>> /* This should never actually happen */
>> - if ((sizeof(as102_usb_id_table) / sizeof(struct usb_device_id)) !=
>> + if (ARRAY_SIZE(as102_usb_id_table) !=
>> (sizeof(as102_device_names) / sizeof(const char *))) {
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> I wonder why it missed this one?
>
> If you wanted to send a follow on patch you could just change this
> to:
> BUILD_BUG_ON(ARRAY_SIZE(as102_usb_id_table) !=
> ARRAY_SIZE(as102_device_names));
>
>> pr_err("Device names table invalid size");
>> return -EINVAL;
>
> regards,
> dan carpenter
>
My larger goal was to find use-cases for this:
(sent to LKML a day or 2 ago)
+/**
+ * BUILD_BUG_DECL - check declared objects
+ * @name: distinguishes multiple uses at same scope.
+ * @cond: false expr, typically like sizeof(a) != sizeof(b)
+ *
+ * This works at file-scope too, and supports checks like:
+ * BUILD_BUG_DECL(foo, sizeof(a) != sizeof(b));
+ * BUILD_BUG_DECL(id_strings, ARRAY_SIZE(id_strings) != ARRAY_SIZE(id_vals));
+ */
+#define BUILD_BUG_DECL(name, cond) \
+ static __initdata struct { \
+ int BUILD_BUG_DECL_ ## name[1 - 2*!!(cond)]; \
+ } BUILD_BUG_DECL_ ##name[0] __attribute__((unused))
+
It lets you check array consistency at compile-time,
rather than waiting til run-time.
Julia Lawall worked up a spatch script that patched
a bunch of cases, all for file-scope static arrays.
As such, it didnt find your case.
Glancing at the context you quoted, it might be useful there ?
Are there others too ?
thanks
Jim
More information about the devel
mailing list