How to initiate a register by using struction and use the container_of(...)/offsetof(...)

1. About offsetof(...) and container_of(...)

1)offsetof(type, member)
    #define offsetof(type, member) (size_t)&(((type*)0)->member)
    (1)offsetof is defined in the stddef.h of standard library. when use it, we may use the format-->#include <stddef.h>
    (2)offsetof is used to get the step(length) of specific variable in a structure.
    typedef struct offsetof_test{
        int a;
        int b;
        int c;
    }off_test;

 
    offsetof(off_test, a) --> 0
    offsetof(off_test, b) --> 4
    offsetof(off_test, c) --> 8
   
2)container_of(ptr, type, member)
   
    #define container_of(ptr, type, member) (
        {
            const typeof( ((type *)0)->member ) *__mptr = (ptr);
            (type *)( (char *)__mptr - offsetof(type,member) );
        }
    )
    
     (1)container_of is defined in the linux/kernel.h of linux kernel.
     (2)we get the struct address by using a specific varibale address
    
    typedef struct offsetof_test{
        int a;
        int b;
        int c;
    }off_test;
   
    container_of(&a, off_test, a) --> get the struct address by using "a"


2. How to initiate a register by using struction.

1) using arm current program status register to introduce the process
    typedef unsigned int uint32_t;
   
    typedef union arm_cpsr_tag{
        unint32_t all;
        struct {
            uint32_t process_mode   :5;
            uint32_t thumb_state    :1;
            uint32_t intr_fiq       :1;
            uint32_t intr_irq       :1;
            uint32_t resevered      :20;
            uint32_t overflow       :1;
            uint32_t carry          :1;
            uint32_t zero           :1;
            uint32_t negative       :1;
        }b;
    }arm_cpsr;
   
2) initiate the arm_cpsr_var address
   arm_cpsr *arm_cpsr_var = (arm_cpsr*)0x1000 0000;

3) use it
    arm_cpsr_var->b.process_mode =  b10000;(user mode)
    we use the "arm_cpsr_var->all" to represent the whole register.

4) we can also use numbers of sturcts to consist a large struct.

   struct arm_cpsr_group{
 arm_cpsr arm_cpsr_var1;
 arm_cpsr arm_cpsr_var2;
 arm_cpsr arm_cpsr_var3;
 arm_cpsr arm_cpsr_var4;
 arm_cpsr arm_cpsr_var5;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值