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

博客介绍了offsetof和container_of的使用。offsetof用于获取结构体中特定变量的偏移量,定义在标准库stddef.h中;container_of可通过特定变量地址获取结构体地址,定义在Linux内核的linux/kernel.h中。还介绍了使用结构体初始化寄存器的过程,如使用arm_cpsr结构体初始化寄存器。

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;
};

出现 `Cannot initiate the connection to mirrors.aliyun.com:80` 错误,通常意味着系统无法与阿里云镜像服务器建立连接,可尝试以下解决办法: ### 检查网络连接 使用 `ping` 命令测试网络连通性,例如: ```bash ping mirrors.aliyun.com ``` 若无法 ping 通,需检查网络配置,包括网线连接、路由器设置等。确保网络正常工作,才能顺利连接到镜像服务器。 ### 检查 DNS 配置 DNS 配置错误可能导致无法解析域名。编辑 `/etc/resolv.conf` 文件,添加可靠的 DNS 服务器,如阿里云的 DNS(223.5.5.5 和 223.6.6.6): ```plaintext nameserver 223.5.5.5 nameserver 223.6.6.6 ``` 修改完成后,再次尝试连接。 ### 检查防火墙设置 防火墙可能会阻止对阿里云镜像服务器的访问。检查防火墙规则,确保允许对 `mirrors.aliyun.com` 的 80 端口进行访问。如果使用的是 `ufw` 防火墙,可以添加以下规则: ```bash ufw allow to mirrors.aliyun.com port 80 ``` 添加规则后,重新加载防火墙: ```bash ufw reload ``` ### 更换软件源 如果上述方法都无法解决问题,可以尝试更换其他软件源,如清华大学镜像源。编辑 `/etc/apt/sources.list` 文件,将原有的阿里云源替换为清华大学源: ```plaintext deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main non-free contrib deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main non-free contrib deb http://mirrors.tuna.tsinghua.edu.cn/debian-security/ bookworm-security main deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security/ bookworm-security main deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main non-free contrib deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main non-free contrib deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main non-free contrib deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main non-free contrib ``` 替换完成后,更新软件包列表: ```bash apt update ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值