See patch.
Uwe.
On 29.06.2007 15:04, Uwe Hermann wrote:
Add an ARRAY_SIZE() macro which returns the size of an array, regardless of the data types of the individual array elements.
The macro is defined in lib.h, so code which uses it must include lib.h.
Signed-off-by: Uwe Hermann uwe@hermann-uwe.de
Acked-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Index: southbridge/amd/cs5536/cs5536.c
--- southbridge/amd/cs5536/cs5536.c (Revision 409) +++ southbridge/amd/cs5536/cs5536.c (Arbeitskopie) @@ -93,8 +93,6 @@ {FLASH_TYPE_NONE, 0, 0}, /* CS3, or Flash Device 3 */ };
-#define FlashInitTableLen (sizeof(FlashInitTable)/sizeof(FlashInitTable[0]))
u32 FlashPort[] = { MDD_LBAR_FLSH0, MDD_LBAR_FLSH1, @@ -149,7 +147,7 @@ int numEnabled = 0;
printk(BIOS_DEBUG, "chipset_flash_setup: Start\n");
- for (i = 0; i < FlashInitTableLen; i++) {
- for (i = 0; i < ARRAY_SIZE(FlashInitTable); i++) { if (FlashInitTable[i].fType != FLASH_TYPE_NONE) { printk(BIOS_DEBUG, "Enable CS%d\n", i); /* we need to configure the memory/IO mask */
Index: include/lib.h
--- include/lib.h (Revision 409) +++ include/lib.h (Arbeitskopie) @@ -21,6 +21,12 @@ #ifndef LIB_H #define LIB_H
+/**
- Return the size of a given array, no matter of which data type
- the individual array elements are.
- */
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
int log2(unsigned int n);
void udelay(unsigned int usecs); Index: superio/winbond/w83627hf/superio.c =================================================================== --- superio/winbond/w83627hf/superio.c (Revision 409) +++ superio/winbond/w83627hf/superio.c (Arbeitskopie) @@ -22,6 +22,7 @@ */
#include <io.h> +#include <lib.h> #include <device/device.h> #include <device/pnp.h> #include <console.h> @@ -99,7 +100,7 @@
};
- for(i = 0; i< sizeof(hwm_reg_values)/sizeof(hwm_reg_values[0]); i+=3 ) {
- for (i = 0; i < ARRAY_SIZE(hwm_reg_values); i += 3) { reg = hwm_reg_values[i]; value = pnp_read_index(base, reg); value &= 0xff & hwm_reg_values[i+1];
@@ -207,8 +208,7 @@
static void phase2_setup_scan_bus(struct device *dev) {
- pnp_enable_devices(dev, &ops,
sizeof(pnp_dev_info)/sizeof(pnp_dev_info[0]), pnp_dev_info);
- pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
}
static struct device_operations ops = { Index: mainboard/artecgroup/dbe61/initram.c =================================================================== --- mainboard/artecgroup/dbe61/initram.c (Revision 409) +++ mainboard/artecgroup/dbe61/initram.c (Arbeitskopie) @@ -120,7 +120,7 @@ static void dbe61_msr_init(void) { int i;
- for(i = 0; i < sizeof(dbe61_msr)/sizeof(dbe61_msr[0]); i++)
- for (i = 0; i < ARRAY_SIZE(dbe61_msr); i++) wrmsr(dbe61_msr[i].reg, dbe61_msr[i].msr);
}
Index: arch/x86/linuxbios_table.c
--- arch/x86/linuxbios_table.c (Revision 409) +++ arch/x86/linuxbios_table.c (Arbeitskopie) @@ -25,6 +25,7 @@ #include <device/device.h> #include <tables.h> #include <mc146818rtc.h> +#include <lib.h> //#include <cpu/cpu.h> //#include <pirq_routing.h> //#include <smp/mpspec.h> @@ -159,7 +160,7 @@ { LB_TAG_ASSEMBLER, (const u8 *)LINUXBIOS_ASSEMBLER }, }; unsigned int i;
- for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
- for(i = 0; i < ARRAY_SIZE(strings); i++) { struct lb_string *rec; size_t len; rec = (struct lb_string *)lb_new_record(header);
Hi,
Just a short comment:
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \ + + sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \ + typeof(&arr[0]))]))*0)
-- Rusty Russell http://lwn.net/Articles/226010/
Rusty, that's a work of art.
However, I would suggest that you never show it to anybody ever again. I'm sure that in fifty years, it will be worth much more. So please keep it tightly under wraps, to keep people from gouging their eyes out^W^W^W^W^W^W^W make a killing in the art market.
-- Linus Torvalds http://lwn.net/Articles/226011/
Maybe we will need something more insane too?
Rudolf
* Rudolf Marek r.marek@assembler.cz [070629 15:36]:
Hi,
Just a short comment:
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) \
- sizeof(typeof(int[1 - 2*!!__builtin_types_compatible_p(typeof(arr), \
typeof(&arr[0]))]))*0)
-- Rusty Russell http://lwn.net/Articles/226010/
Maybe we will need something more insane too?
I suggest we rather expect people to know what they are doing. Feeding a non-array into a macro ARRAY_SIZE is not completely unlikely to happen but adding trickery like the above will not make us any friends among those trying to understand the code.
On Fri, Jun 29, 2007 at 03:19:18PM +0200, Carl-Daniel Hailfinger wrote:
Acked-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Thanks, r413.
Uwe.
* Uwe Hermann uwe@hermann-uwe.de [070629 15:04]:
See patch.
Uwe.
http://www.hermann-uwe.de | http://www.holsham-traders.de http://www.crazy-hacks.org | http://www.unmaintained-free-software.org
Add an ARRAY_SIZE() macro which returns the size of an array, regardless of the data types of the individual array elements.
The macro is defined in lib.h, so code which uses it must include lib.h.
Signed-off-by: Uwe Hermann uwe@hermann-uwe.de
Acked-by: Stefan Reinauer stepan@coresystems.de