【发布时间】:2019-05-16 19:11:52
【问题描述】:
我正在查看 linux 内核中的这个头文件: https://elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/string.h
#ifndef BOOT_STRING_H
#define BOOT_STRING_H
/* Undef any of these macros coming from string_32.h. */
#undef memcpy
#undef memset
#undef memcmp
void *memcpy(void *dst, const void *src, size_t len);
void *memset(void *dst, int c, size_t len);
int memcmp(const void *s1, const void *s2, size_t len);
#define memcpy(d,s,l) __builtin_memcpy(d,s,l)
#define memset(d,c,l) __builtin_memset(d,c,l)
#define memcmp __builtin_memcmp
...
#endif /* BOOT_STRING_H */
我无法弄清楚 memcpy、memset 和 memcmp 上的 #undef + 函数声明 + 宏定义是做什么的。例如,它首先声明了一个函数 memcpy,然后定义了一个宏 memcpy。我不确定这样做的目的是什么。我发现这里定义了这个函数:https://elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/copy.S#L20。如果代码中的某处使用 memcpy(例如这里:https://elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/main.c#L40)使用 memcpy,它使用什么? copy.S 或 __builtin_memcpy 中定义的函数?
【问题讨论】:
-
可能声明的目的是,如果人们绕过以下宏(例如取消定义它们),它仍然可以工作
标签: c linux-kernel