GLib 宏 g_slice_new 个问题
GLib macro g_slice_new questions
这个问题与用于 C 编程的 GLib 有关。
原代码在这里:
https://github.com/GNOME/glib/blob/master/glib/gslice.h
在glist.h中,我看到了宏_g_list_alloc0,我想知道它是如何implements.So我回溯的。
#define _g_list_alloc0() g_slice_new0 (GList)
接下来,回溯到宏g_slice_new0
#define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type)))
好的,返回
gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
对于G_GNUC_MALLOC,我发现其实是:
#define G_GNUC_MALLOC __attribute__((__malloc__))
#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
我对最后两个宏 G_GNUC_MALLOC 和 G_GNUC_ALLOC_SIZE 感到困惑。
我可以将 G_GNUC_ALLOC_SIZE(1) 和 G_GNUC_MALLOC 替换为:
__attribute__((__alloc_size__(1)))
__attribute__((__malloc__))
所以,替换宏
gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
宏实际上是这样定义的:
gpointer g_slice_allo0 (gsize block_size)
__attribute__((__malloc__)) __attribute__((__alloc_size__(1)))
我的问题:
什么表达式
__attribute__((__malloc__)) __attribute__((__alloc_size__(1)))
有效还是生成?我猜它像
malloc(sizeof())
根据sizeof分配内存。
为什么不只使用 malloc(sizeof()) 而不是这个完整的表达式?
什么是
__attribute__
?它是 glib 的一些保留关键字吗?
gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
表达式的类型是什么?它不是宏或 typedef。
是不是以宏为函数名的函数?
谁能帮我分析一下?
原文link在这里:
https://github.com/GNOME/glib/blob/master/glib/gslice.h
您可以在此处阅读有关属性的信息:https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
malloc
一个"tells the compiler that a function is malloc-like"。 alloc_size
"is used to tell the compiler that the function return value points to memory, where the size is given by one or two of the functions parameters."
都是为了编译器的优化。这些属性不会改变函数的工作方式,它们只是让编译器产生更好的输出。
#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
Expands to the GNU C alloc_size function attribute if the compiler is a new enough gcc. This attribute tells the compiler that the function returns a pointer to memory of a size that is specified by the xth function parameter.
#define G_GNUC_MALLOC __attribute__((__malloc__))
Expands to the GNU C malloc function attribute if the compiler is gcc. Declaring a function as malloc enables better optimization of the function. A function can have the malloc attribute if it returns a pointer which is guaranteed to not alias with any other pointer when the function returns (in practice, this means newly allocated memory).
这个问题与用于 C 编程的 GLib 有关。 原代码在这里: https://github.com/GNOME/glib/blob/master/glib/gslice.h
在glist.h中,我看到了宏_g_list_alloc0,我想知道它是如何implements.So我回溯的。
#define _g_list_alloc0() g_slice_new0 (GList)
接下来,回溯到宏g_slice_new0
#define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type)))
好的,返回
gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
对于G_GNUC_MALLOC,我发现其实是:
#define G_GNUC_MALLOC __attribute__((__malloc__))
#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
我对最后两个宏 G_GNUC_MALLOC 和 G_GNUC_ALLOC_SIZE 感到困惑。
我可以将 G_GNUC_ALLOC_SIZE(1) 和 G_GNUC_MALLOC 替换为:
__attribute__((__alloc_size__(1)))
__attribute__((__malloc__))
所以,替换宏
gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
宏实际上是这样定义的:
gpointer g_slice_allo0 (gsize block_size)
__attribute__((__malloc__)) __attribute__((__alloc_size__(1)))
我的问题: 什么表达式
__attribute__((__malloc__)) __attribute__((__alloc_size__(1)))
有效还是生成?我猜它像
malloc(sizeof())
根据sizeof分配内存。 为什么不只使用 malloc(sizeof()) 而不是这个完整的表达式? 什么是
__attribute__
?它是 glib 的一些保留关键字吗?
gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
表达式的类型是什么?它不是宏或 typedef。 是不是以宏为函数名的函数? 谁能帮我分析一下?
原文link在这里: https://github.com/GNOME/glib/blob/master/glib/gslice.h
您可以在此处阅读有关属性的信息:https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
malloc
一个"tells the compiler that a function is malloc-like"。 alloc_size
"is used to tell the compiler that the function return value points to memory, where the size is given by one or two of the functions parameters."
都是为了编译器的优化。这些属性不会改变函数的工作方式,它们只是让编译器产生更好的输出。
#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
Expands to the GNU C alloc_size function attribute if the compiler is a new enough gcc. This attribute tells the compiler that the function returns a pointer to memory of a size that is specified by the xth function parameter.
#define G_GNUC_MALLOC __attribute__((__malloc__))
Expands to the GNU C malloc function attribute if the compiler is gcc. Declaring a function as malloc enables better optimization of the function. A function can have the malloc attribute if it returns a pointer which is guaranteed to not alias with any other pointer when the function returns (in practice, this means newly allocated memory).