如果你仔细查看文件 drivers/pci/pci.c,你会看到以下代码:
#define PCI_OP(rw,size,type) \
int pci_##rw##_config_##size (struct pci_dev *dev,
int pos, type value) \
{ \
int res; \
unsigned long flags; \
if (PCI_##size##_BAD) return
PCIBIOS_BAD_REGISTER_NUMBER; \
spin_lock_irqsave(&pci_lock, flags); \
res = dev->bus->ops->rw##_##size(dev, pos,
value); \
spin_unlock_irqrestore(&pci_lock, flags); \
return res; \
}
PCI_OP(read, byte, u8 *)
PCI_OP(read, word, u16 *)
PCI_OP(read, dword, u32 *)
PCI_OP(write, byte, u8)
PCI_OP(write, word, u16)
PCI_OP(write, dword, u32)
这段宏技巧通过滥用 C 预处理器 #define PCI_OP(),创建了六个 pci_read_config_* 和 pci_write_config_* 函数。