硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2196|回复: 4
收起左侧

函数offsetof,简单实用,求结构体中一个成员在该结构体中的偏移量

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106685
QQ
发表于 2019-4-22 15:34:20 | 显示全部楼层 |阅读模式
结构体:
  1. /// OS Runtime Information structure
  2. typedef struct {
  3.   const char                   *os_id;  ///< OS Identification
  4.   uint32_t                    version;  ///< OS Version
  5.   struct {                              ///< Kernel Info
  6.     uint8_t                     state;  ///< State
  7.     volatile uint8_t          blocked;  ///< Blocked
  8.     uint8_t                    pendSV;  ///< Pending SV
  9.     uint8_t                  reserved;
  10.     uint32_t                     tick;  ///< Tick counter
  11.   } kernel;
  12.   int32_t                   tick_irqn;  ///< Tick Timer IRQ Number
  13.   struct {                              ///< Thread Info
  14.     struct {                            ///< Thread Run Info
  15.       osRtxThread_t             *curr;  ///< Current running Thread
  16.       osRtxThread_t             *next;  ///< Next Thread to Run
  17.     } run;
  18.     osRtxObject_t               ready;  ///< Ready List Object
  19.     osRtxThread_t               *idle;  ///< Idle Thread
  20.     osRtxThread_t         *delay_list;  ///< Delay List
  21.     osRtxThread_t          *wait_list;  ///< Wait List (no Timeout)
  22.     osRtxThread_t     *terminate_list;  ///< Terminate Thread List
  23.     struct {                            ///< Thread Round Robin Info
  24.       osRtxThread_t           *thread;  ///< Round Robin Thread
  25.       uint32_t                   tick;  ///< Round Robin Time Tick
  26.       uint32_t                timeout;  ///< Round Robin Timeout
  27.     } robin;
  28.   } thread;
  29.   struct {                              ///< Timer Info
  30.     osRtxTimer_t                *list;  ///< Active Timer List
  31.     osRtxThread_t             *thread;  ///< Timer Thread
  32.     osRtxMessageQueue_t           *mq;  ///< Timer Message Queue
  33.     void                (*tick)(void);  ///< Timer Tick Function
  34.   } timer;
  35.   struct {                              ///< ISR Post Processing Queue
  36.     uint16_t                      max;  ///< Maximum Items
  37.     uint16_t                      cnt;  ///< Item Count
  38.     uint16_t                       in;  ///< Incoming Item Index
  39.     uint16_t                      out;  ///< Outgoing Item Index
  40.     void                       **data;  ///< Queue Data
  41.   } isr_queue;
  42.   struct {                                      ///< ISR Post Processing functions
  43.     void          (*thread)(osRtxThread_t*);    ///< Thread Post Processing function
  44.     void (*event_flags)(osRtxEventFlags_t*);    ///< Event Flags Post Processing function
  45.     void    (*semaphore)(osRtxSemaphore_t*);    ///< Semaphore Post Processing function
  46.     void (*memory_pool)(osRtxMemoryPool_t*);    ///< Memory Pool Post Processing function
  47.     void        (*message)(osRtxMessage_t*);    ///< Message Post Processing function
  48.   } post_process;
  49.   struct {                              ///< Memory Pools (Variable Block Size)
  50.     void                       *stack;  ///< Stack Memory
  51.     void                     *mp_data;  ///< Memory Pool Data Memory
  52.     void                     *mq_data;  ///< Message Queue Data Memory
  53.     void                      *common;  ///< Common Memory
  54.   } mem;
  55.   struct {                              ///< Memory Pools (Fixed Block Size)
  56.     osRtxMpInfo_t              *stack;  ///< Stack for Threads
  57.     osRtxMpInfo_t             *thread;  ///< Thread Control Blocks
  58.     osRtxMpInfo_t              *timer;  ///< Timer Control Blocks
  59.     osRtxMpInfo_t        *event_flags;  ///< Event Flags Control Blocks
  60.     osRtxMpInfo_t              *mutex;  ///< Mutex Control Blocks
  61.     osRtxMpInfo_t          *semaphore;  ///< Semaphore Control Blocks
  62.     osRtxMpInfo_t        *memory_pool;  ///< Memory Pool Control Blocks
  63.     osRtxMpInfo_t      *message_queue;  ///< Message Queue Control Blocks
  64.   } mpi;
  65. } osRtxInfo_t;
复制代码


使用举例:
memset(&osRtxInfo.kernel, 0, sizeof(osRtxInfo) - offsetof(osRtxInfo_t, kernel));

原始定义:

  1. #ifdef __clang__
  2.   #define offsetof(t, d) __builtin_offsetof(t, d)
  3. #else
  4.   /* EDG uses __INTADDR__ to avoid errors when strict */
  5.   #define offsetof(t, memb) ((__CLIBNS size_t)__INTADDR__(&(((t *)0)->memb)))
  6. #endif
复制代码


回复

使用道具 举报

1

主题

369

回帖

372

积分

高级会员

积分
372
发表于 2019-4-22 16:26:09 | 显示全部楼层
将kernel及后面的变量清零,学习了
回复

使用道具 举报

5

主题

179

回帖

194

积分

初级会员

积分
194
发表于 2019-4-23 00:01:33 | 显示全部楼层
offestof 和 containof
回复

使用道具 举报

36

主题

2039

回帖

2147

积分

至尊会员

积分
2147
发表于 2019-4-23 10:40:45 | 显示全部楼层
迄今为止,没使用过这个函数
Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better.
回复

使用道具 举报

1

主题

103

回帖

106

积分

初级会员

积分
106
发表于 2019-6-19 16:13:04 | 显示全部楼层
太深奥了,表示
回复

使用道具 举报

73

主题

1193

回帖

1412

积分

至尊会员

积分
1412
发表于 2019-12-19 20:07:36
上面的结构体是干嘛的,弄这么大来演示这个offset功能吗

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-29 14:57 , Processed in 0.171442 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表