The Linux kernel uses bitmap-related
1, declares a bitmap array, can represent 100 bit, the array name is bitmap
[CPP]View Plaincopy
- Declare_bitmap (bitmap,100)
The related macros are defined as follows:
[CPP]View Plaincopy
- #define DECLARE_BITMAP (name,bits) unsigned long name[bits_to_longs (bits)]
- #define Bits_to_longs (NR) div_round_up (nr,bits_per_byte*sizeof (Long))
- #define DIV_ROUND_UP (N,d) (((n) + (d)-1)/(d))
- #define Bits_per_byte 8
2, use the bitmap array defined above
Detects if a bitmap is 1 and uses the Test_bit () function, such as detecting the first bit, calls Test_bit (0,bitmap)
[CPP]View Plaincopy
- static int test_bit (unsigned int nr,const unsigned long *addr)
- {
- Return ((1ul<< (Nr%bits_per_long) & (((unsigned*) addr) [Nr/bits_per_long])!=0)
- }
- #define Bits_per_long 32
Usage Example: Bootmem dispenser ~ ~
The kernel uses a doubly linked list correlation
[CPP]View Plaincopy
- struct list_head{
- struct List_head *next;
- struct List_head *priv;
- };
The List_entry () function can return a structure containing a doubly linked list, pointer ptr to the member member in the struct type, and return the start address of the struct type by ptr
[CPP]View Plaincopy
- #define List_entry (Ptr,type,member) container_of (Ptr,type,member)
[CPP]View Plaincopy
- List_add (struct list_head *new,struct list_head *head)
The function inserts a new node after the head node of the specified list,
[CPP]View Plaincopy
- List_for_each_entry (Pos,head,member)
POS is a pointer to the object containing the List_head node, which can be seen as the value returned by List_entry (), where the head is the beginning of the traversal,
[CPP]View Plaincopy
- #define CONTAINER_OF (PTR, type, member) ({\
- Const typeof (((type *) 0)->member) *__mptr = (PTR); \
- (Type *) ((char *) __mptr-offsetof (Type,member));})
- #define OFFSETOF (Type, MEMBER) ((size_t) & ((TYPE *) 0)->member)
The first step is to define a temporary data type (obtained by typeof (((type *) 0)->member) with the same pointer variable __mptr as PTR and use it to save the PTR value. The second step, with (char *) __mptr minus member in the structure of the offset, the resulting value is the entire structure of the first address of the variable (the return value of the entire macro is the first address).
TypeOf's explanation is as follows:
[CPP]View Plaincopy
- Const typeof (((type *) 0)->member) *__mptr = (PTR);
is to define a pointer called __mptr. The pointer to the content is immutable, and the type that is pointed to is a type of member in the struct body of type.
Two take the reverse!! The role of
A: Make sure the result is 0 or 1
Data structure [Linux kernel Programming] Learning notes (i)