os_stddef.h 源码
2026/7/20大约 2 分钟源码参考内核宏定义头文件
os_stddef.h — 标准宏定义头文件
源文件路径: kernel/include/os_stddef.h
引用章节: 基础知识库-通用宏
完整源码
#ifndef __OS_STDDEF_H__
#define __OS_STDDEF_H__
#include <os_types.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ==================== 编译器适配宏 ==================== */
#if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM Compiler */
#include <stdarg.h>
#define OS_SECTION(x) __attribute__((section(x)))
#define OS_ALIGN(n) __attribute__((aligned(n)))
#define OS_UNUSED __attribute__((unused))
#define OS_USED __attribute__((used))
#define OS_WEAK __attribute__((weak))
#define OS_INLINE static __inline
#elif defined(__GNUC__) /* GCC */
#include <stdarg.h>
#define OS_SECTION(x) __attribute__((section(x)))
#define OS_ALIGN(n) __attribute__((aligned(n)))
#define OS_UNUSED __attribute__((unused))
#define OS_USED __attribute__((used))
#define OS_WEAK __attribute__((weak))
#define OS_INLINE static __inline
#else
#error "Not supported the tool chain."
#endif
/* ==================== 自动初始化级别 ==================== */
#define OS_INIT_LEVEL_PRE_KERNEL_1 1 /* 内核启动前第1阶段 */
#define OS_INIT_LEVEL_PRE_KERNEL_2 2 /* 内核启动前第2阶段 */
#define OS_INIT_LEVEL_POST_KERNEL 3 /* 内核启动后第1阶段 */
#define OS_INIT_LEVEL_PRE_DEVICE 4 /* 设备初始化前 */
#define OS_INIT_LEVEL_DEVICE 5 /* 设备初始化 */
#define OS_INIT_LEVEL_COMPONENT 6 /* 组件初始化 */
#define OS_INIT_LEVEL_APPLICATION 7 /* 应用初始化 */
#ifdef OS_USING_SMP
#define OS_INIT_LEVEL_SMP 8 /* SMP 初始化 */
#endif
#define OS_INIT_SUBLEVEL_HIGH 1
#define OS_INIT_SUBLEVEL_MIDDLE 2
#define OS_INIT_SUBLEVEL_LOW 3
/* 自动初始化注册宏 */
typedef os_err_t (*init_call_fn_t)(void);
struct _init_call_entry
{
const char *name;
init_call_fn_t func;
};
typedef struct _init_call_entry init_call_entry_t;
#define OS_INIT_CALL(fn, level, sublevel) \
OS_USED static const init_call_entry_t _os_init_call_##fn \
OS_SECTION(".init_call." #level "." #sublevel) = {#fn, fn}
/* ==================== 通用宏 ==================== */
#define OS_NULL ((void *)0)
#define OS_FALSE 0
#define OS_TRUE 1
#define OS_ALIGN_UP(size, align) (((size) + (align)-1) & ~((align)-1))
#define OS_ALIGN_DOWN(size, align) ((size) & ~((align)-1))
#define OS_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define OS_WAIT_FOREVER OS_TICK_MAX
#define OS_NO_WAIT 0U
#define OS_UNREFERENCE(x) ((void)(x))
#define OS_BIT_SET(value, bit) ((value) |= (1 << (bit)))
#define OS_BIT_CLR(value, bit) ((value) &= ~(1 << (bit)))
#define OS_BIT_GET(value, bit) (((value) & (1 << (bit))) >> (bit))
#define os_container_of(ptr, type, member) \
((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
#define os_offsetof(type, member) ((os_size_t) & ((type *)0)->member)
#ifdef __cplusplus
}
#endif
#endif /* __OS_STDDEF_H__ */关键说明
| 宏 | 功能 | 说明 |
|---|---|---|
OS_INIT_CALL | 自动初始化注册 | 通过 section 属性将函数指针放入指定段,内核启动时自动调用 |
OS_ALIGN_UP | 向上对齐 | OS_ALIGN_UP(13, 4) → 16 |
os_container_of | 反向获取结构体 | 从成员指针反推容器结构体指针 |
OS_WAIT_FOREVER | 永久等待 | 值为 OS_TICK_MAX,用于 IPC 超时参数 |