冲突的函数声明和宏?

dar*_*ord 5 c linux-kernel

我在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 */
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚#undef +函数声明+宏在memcpy,memset和memcmp上的定义是什么.例如,它首先声明一个函数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中定义的函数?

bur*_*ino 0

函数声明和宏不冲突。 memcpy()内核中有多个定义,块上方的注释暗示了这一点——在string_32.h#undef中还有另一个memcpy()定义。

#undef memcpy正在取消string_32.h#define中找到的内容,以便它不会存在于包含/boot/string.h的任何文件中。 然后声明它,并为其构建一个新的宏。memcpy()

#define语句正在创建一个新的宏,因为string_32.hmemcpy()中的宏在此上下文中不再存在。内核开发人员出于多种原因使用宏;有关更多信息,请参阅此线程中的答案。

/boot/copy.S是一个汇编文件您可以在此处阅读有关其作用的一些信息。/boot/main.cmemcpy()中使用的内容来自/boot/string.h ——检查 include 语句。