Data structure [Linux kernel Programming] Learning notes (i)

Source: Internet
Author: User

The Linux kernel uses bitmap-related
1, declares a bitmap array, can represent 100 bit, the array name is bitmap

[CPP]View Plaincopy
    1. Declare_bitmap (bitmap,100)

The related macros are defined as follows:

[CPP]View Plaincopy
    1. #define DECLARE_BITMAP (name,bits) unsigned long name[bits_to_longs (bits)]
    2. #define Bits_to_longs (NR) div_round_up (nr,bits_per_byte*sizeof (Long))
    3. #define DIV_ROUND_UP (N,d) (((n) + (d)-1)/(d))
    4. #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
    1. static int test_bit (unsigned int nr,const unsigned long *addr)
    2. {
    3. Return ((1ul<< (Nr%bits_per_long) & (((unsigned*) addr) [Nr/bits_per_long])!=0)
    4. }
    5. #define Bits_per_long 32

Usage Example: Bootmem dispenser ~ ~

The kernel uses a doubly linked list correlation

[CPP]View Plaincopy
    1. struct list_head{
    2. struct List_head *next;
    3. struct List_head *priv;
    4. };


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
    1. #define List_entry (Ptr,type,member) container_of (Ptr,type,member)
[CPP]View Plaincopy
    1. 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
    1. 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
    1. #define CONTAINER_OF (PTR, type, member) ({\
    2. Const typeof (((type *) 0)->member) *__mptr = (PTR); \
    3. (Type *)  ((char *) __mptr-offsetof (Type,member));})
    4. #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
    1. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.