gpt4 book ai didi

c - 了解 glibc 源代码约定

转载 作者:太空狗 更新时间:2023-10-29 17:01:35 25 4
gpt4 key购买 nike

我一直在查看 glibc 的一些源代码,特别是 nptl 代码,我发现它有点难以理解,因为它似乎有我不熟悉的约定。

例如,我正在查看一个非常小的文件 pthread_equal.c有几件事我有疑问:

22 int
23 __pthread_equal (thread1, thread2)
24 pthread_t thread1;
25 pthread_t thread2;
26 {
27 return thread1 == thread2;
28 }
29 strong_alias (__pthread_equal, pthread_equal)
  1. 第 22 行和第 23 行的声明看起来像我理解的内容。它有一个返回类型 int 然后是函数名 __pthread_equal 和参数列表 (thread1, thread2)。但是第 24 行 pthread_t thread1; 和第 25 行 pthread_t thread2; 的声明是干什么用的?看起来这些被声明为全局变量,但我不明白其目的。我在 nptl 目录中的许多文件中都看到过这种模式,但一直无法弄清楚为什么会这样。

  2. 什么是strong_alias?快速 Google 搜索有使用此方法的示例,但我没有找到指向任何文档的链接。

  3. 为什么要在某些名称前加上两个下划线 __ 而在某些名称前加上一个下划线 _?我见过的大多数代码都使用了两个下划线,但我认为我已经看到一些地方使用了一个下划线。例如在 pthreadP.h

    556 /* Old cleanup interfaces, still used in libc.so.  */
    557 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
    558 void (*routine) (void *), void *arg);
    559 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
    560 int execute);
    561 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
    562 void (*routine) (void *), void *arg);
    563 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
    564 int execute);

不可否认,该代码以“旧清理接口(interface)”的注释开头,但无论哪种方式,我都很好奇其中的区别以及为什么有时使用一个下划线,有时使用两个下划线。

欢迎提供有关这些问题的任何信息。

最佳答案

该函数是在不需要 C89 兼容编译器的情况下编写的;它也适用于旧的编译器。那是一个非原型(prototype)函数定义。

int   /* Return type */
function(arg1, arg2) /* Function name and argument names (but no types) */
int arg1; /* Type of arg1 */
char *arg2; /* Type of arg2 */
{
/* Body of function */
}

请注意,参数的定义不必与函数行中的顺序相同(我不得不将代码从这种“K&R”表示法转换为原型(prototype)表示法,因为它们是乱序的!)。另请注意,过去可以简单地编写:

main(argc, argv)
char **argv;
{
...
}

argc 的隐含类型是 int,因为它没有被指定为任何其他类型。 glib 代码不太可能利用该许可证。同样,main() 的返回类型是 int,因为没有给出其他类型。

strong_alias 与隐藏和公开共享库中的符号有关。我没有使用它,所以我不确定所有的后果,但我相信这意味着 __pthread_equal()pthread_equal() 函数的另一个名称.


__pthread_equal() 名称背后的部分原因是,以下划线开头后跟大写字母或另一个下划线的名称由 C 标准“保留给实现”。根据 C 标准,“pthread_equal()”等名称位于用户的 namespace 中。

ISO/IEC 9899:1990(C99 标准)说:

7.1.3 Reserved identifiers

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).

— All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.154)

— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.

No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

154) The list of reserved identifiers with external linkage includes errno, math_errhandling, setjmp, and va_end.

关于c - 了解 glibc 源代码约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9695182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com