简体   繁体   English

冲突的函数声明和宏?

[英]Conflicting function declaration and macro?

I am looking at this header file in linux kernel: https://elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/string.h 我在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 */

I can't figure out what does the #undef + function declaration + macro define on memcpy, memset and memcmp do. 我无法弄清楚#undef +函数声明+宏在memcpy,memset和memcmp上的定义是什么。 For example, it first declares a function memcpy and then defines a macro memcpy after that. 例如,它首先声明一个函数memcpy,然后在此之后定义一个宏memcpy。 I am not sure what's the purpose of this. 我不确定这是什么目的。 I find that this function is defined here: https://elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/copy.S#L20 . 我发现这个函数在这里定义: https//elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/copy.S#L20 If somewhere in the code uses memcpy (for example here: https://elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/main.c#L40 ) uses memcpy what does it use? 如果代码中的某个地方使用memcpy(例如这里: https//elixir.bootlin.com/linux/v4.14/source/arch/x86/boot/main.c#L40 )使用memcpy它使用什么? The function defined in copy.S or the __builtin_memcpy? copy.S或__builtin_memcpy中定义的函数?

The function declaration and macro are not conflicting. 函数声明和宏没有冲突。 memcpy() has several definitions in the kernel, which is hinted at by the comment above the #undef blocks -- there is another memcpy() defined in string_32.h . memcpy()具有在内核,其在通过上述的注释暗示几种定义#undef块-有另一个memcpy()中所定义string_32.h

#undef memcpy is cancelling out the #define found in string_32.h so that it will not exist in any file that includes /boot/string.h . #undef memcpy正在取消string_32.h中找到的#define ,这样它就不会存在于包含/boot/string.h的任何文件中。 memcpy() is then declared, and a new macro is built for it. 然后声明memcpy() ,并为它构建一个新的宏。

The #define statement is creating a new macro for memcpy() , as the one from string_32.h no longer exists in this context. #define语句正在为memcpy()创建一个新的宏,因为string_32.h中的一个不再存在于此上下文中。 Kernel developers use macros for a variety of reasons; 内核开发人员出于各种原因使用宏; see answers in this thread for more. 请参阅此主题中的答案以获取更多信

/boot/copy.S is an assembly file . /boot/copy.S是一个程序集文件 You can read a bit about its role here . 你可以在这里阅读一下它的作用。 The memcpy() being used in /boot/main.c is coming from /boot/string.h -- check the include statements. /boot/main.c中使用的memcpy()来自/boot/string.h - 检查include语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM