[U-Boot] a fair bit of code that could use ARRAY_SIZE() macro

Robert P. J. Day rpjday at crashcourse.ca
Fri Aug 26 16:19:09 CEST 2016


  a few years back, i wrote a number of scripts that ran through the
kernel source tree and located places that were good candidates for
"cleaning" or simplification. one of those cleanups was to replace
occurrences of the general form:

  (sizeof(x) / sizeof((x)[0]))

with:

  ARRAY_SIZE(x)

where that macro was defined in include/linux/kernel.h.

  i just did a quick grep to find similar stuff in the u-boot tree ...
first, here are the same defines in u-boot:

  $ grep -r "define ARRAY_SIZE" *
  common/env_flags.c:#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  include/linux/kernel.h:#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  tools/mxsimage.h:#define ARRAY_SIZE(x)		(sizeof(x) / sizeof((x)[0]))
  tools/imagetool.h:#define ARRAY_SIZE(x)		(sizeof(x) / sizeof((x)[0]))
  $

doing a fairly unintelligent search for that pattern gives me:

  $ grep -r "sizeof.*/.*sizeof" * | wc -l
  109
  $

and, sure, there are going to be some false positives in there, but
quite a lot of the real thing, eg.

  drivers/spi/cf_spi.c:	pbrcnt = sizeof(prescaler) / sizeof(int);
  drivers/spi/cf_spi.c:	brcnt = sizeof(scaler) / sizeof(int);

so a couple questions. first, is this worth doing? it, of course,
should make no functional difference, simply aesthetics, so this may
be one of those things where people conclude, "nah, it's just code
churn." OTOH, that test is fairly widespread in the u-boot code base:

  $ grep -rw ARRAY_SIZE * | wc -l
  1396
  $

and, second, that macro in the kernel is defined as:

  #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))

is that additional "+ __must_be_array(arr))" significant? if the
kernel is doing that, would it be wise for u-boot to do the same?

  thoughts?

rday

-- 

========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================



More information about the U-Boot mailing list