On Sunday 06 January 2008 18:32, Carl-Daniel Hailfinger wrote:
How do we factor out such code? I prefer macros for simple function wrappers and functions for multiline sequences.
As we are working with hardware we should avoid any macros for function replacement. There are ugly side effects. Refer the getCx86()/setCx86() disaster in the kernel for Geode-GX1 chipset programming:
What wrong with that?
#define getCx86(reg) ({ outb((reg), 0x22); inb(0x23); }) #define setCx86(reg, data) do { \ outb((reg), 0x22); \ outb((data), 0x23); \ } while (0)
This works: x = getCx86(0x20); x |= 0x01; setCx86(0x20, x);
This fails badly: setCx86(0x20, getCx86(0x20) | 0x01);
getCx86 and setCx86 defined as inline functions are working in all cases. So: Do not use macros as functions.
Juergen