XFusion API v1.3.0
载入中...
搜索中...
未找到
xf_predef

预定义宏. 如 UNUSED, STR, xf_offsetof, xf_container_of. 更多...

xf_predef 的协作图:

宏定义

#define __FILENAME__
 获取不含路径的文件名。
 
#define NC   (-1)
 空引脚值。当引脚无需使用时填入。
 
#define UNUSED(x)   do { (void)(x); } while(0)
 未使用的变量通过 UNUSED 防止编译器警告。
 
#define CONCAT(a, b)   a##b
 拼接。
 
#define CONCAT3(a, b, c)   a##b##c
 拼接 3 个参数。
 
#define XCONCAT(a, b)   CONCAT(a, b)
 展开拼接。 与 CONCAT 相比,此宏会先展开宏后拼接。
 
#define XCONCAT3(a, b, c)   CONCAT3(a, b, c)
 展开拼接 3 个参数。 与 CONCAT3 相比,此宏会先展开宏后拼接。
 
#define STR(x)   #x
 字符串化。 见:https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
 
#define XSTR(x)   STR(x)
 参数字符串化。 见:https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
 
#define xf_offsetof(type, member)   ((size_t)&((type *)0)->member)
 xf_offsetof - 返回结构成员相对于结构开头的字节偏移量。
 
#define xf_container_of(ptr, type, member)    ((type *)((char *)(ptr) - xf_offsetof(type, member)))
 xf_container_of - 通过结构体成员变量地址获取结构体的地址.
 
#define ARRAY_SIZE(arr)   (sizeof(arr) / sizeof((arr)[0]))
 ARRAY_SIZE - 获取数组 arr 中的元素数量。
 

详细描述

预定义宏. 如 UNUSED, STR, xf_offsetof, xf_container_of.

宏定义说明

◆ __FILENAME__

#define __FILENAME__
值:
(__builtin_strrchr(__FILE__, '/') \
? (__builtin_strrchr(__FILE__, '/') + 1) \
: (__FILE__))

获取不含路径的文件名。

在文件 xf_predef.h39 行定义.

◆ NC

#define NC   (-1)

空引脚值。当引脚无需使用时填入。

在文件 xf_predef.h51 行定义.

◆ UNUSED

#define UNUSED (   x)    do { (void)(x); } while(0)

未使用的变量通过 UNUSED 防止编译器警告。

在文件 xf_predef.h64 行定义.

◆ CONCAT

#define CONCAT (   a,
 
)    a##b

拼接。

在文件 xf_predef.h71 行定义.

◆ CONCAT3

#define CONCAT3 (   a,
  b,
 
)    a##b##c

拼接 3 个参数。

在文件 xf_predef.h78 行定义.

◆ XCONCAT

#define XCONCAT (   a,
 
)    CONCAT(a, b)

展开拼接。 与 CONCAT 相比,此宏会先展开宏后拼接。

在文件 xf_predef.h86 行定义.

◆ XCONCAT3

#define XCONCAT3 (   a,
  b,
 
)    CONCAT3(a, b, c)

展开拼接 3 个参数。 与 CONCAT3 相比,此宏会先展开宏后拼接。

在文件 xf_predef.h94 行定义.

◆ STR

#define STR (   x)    #x

字符串化。 见:https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html

在文件 xf_predef.h102 行定义.

◆ XSTR

#define XSTR (   x)    STR(x)

参数字符串化。 见:https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html

在文件 xf_predef.h110 行定义.

◆ xf_offsetof

#define xf_offsetof (   type,
  member 
)    ((size_t)&((type *)0)->member)

xf_offsetof - 返回结构成员相对于结构开头的字节偏移量。

参数
type结构体的类型名。
member结构中成员的名称。
返回
size_t 结构成员相对于结构开头的字节偏移量。

在文件 xf_predef.h121 行定义.

◆ xf_container_of

#define xf_container_of (   ptr,
  type,
  member 
)     ((type *)((char *)(ptr) - xf_offsetof(type, member)))

xf_container_of - 通过结构体成员变量地址获取结构体的地址.

参数
ptr指向成员的指针。
type结构体类型。
member结构中成员的名称。
返回
结构体的地址。

示例如下:

typedef struct {
int member1;
char member2;
float member3;
} struct_test_t;
int main()
{
struct_test_t stru = {
.member1 = 0x12345678,
.member2 = 0xAB,
.member3 = -123.456f,
};
char *p_member2 = &stru.member2;
struct_test_t *p_stru = xf_container_of(p_member2, struct_test_t, member2);
if ((p_stru->member1 != (int)0x12345678)
|| (p_stru->member2 != (char)0xAB)
|| ((p_stru->member3 - (-123.456f)) > 1e-6f)) {
printf("xf_container_of error!\n");
} else {
printf("xf_container_of ok!\n");
}
}
#define xf_container_of(ptr, type, member)
xf_container_of - 通过结构体成员变量地址获取结构体的地址.
Definition xf_predef.h:161
int main(void)
Definition main.c:28

在文件 xf_predef.h161 行定义.

◆ ARRAY_SIZE

#define ARRAY_SIZE (   arr)    (sizeof(arr) / sizeof((arr)[0]))