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