The classical realization of bidirectional linked list in Linux kernel

Source: Internet
Author: User

Profile

The previous chapter "introduces the bidirectional linked list and gives the C/c++/java three kinds of realization", this chapter continues to discuss the two-way link list, introduces the content is the Linux kernel bidirectional link List's classic realization and the usage. It also involves the two classic macro definitions Offsetof and container_of that are very commonly used in the Linux kernel. The content includes:

1. Two classic macro definitions in Linux

2. The classic realization of bidirectional linked list in Linux

Reprint please indicate the source: http://www.cnblogs.com/skywang12345/p/3562146.html

more content: data structure and algorithm series catalogue

Two classic macro definitions in Linux

If you have checked the Linux kernel source code, then you to Offsetof and container_of these two macros should be not unfamiliar. The two macros were originally written by geeks and later used in the Linux kernel.

1. Offsetof

1.1 Offsetof Introduction

Definition: Offsetof is defined in the include/linux/stddef.h of the Linux kernel.

#define OFFSETOF (Type, member) ((size_t) & ((type *) 0)->member)

description : Gets the offset of the variable members (member) of the struct body (TYPE) in this structure.

(TYPE *) 0) converts 0 to a type pointer, that is, the address of the type's pointer is 0.

(TYPE *) 0)->member access to data members in the structure.

(0) & ((TYPE *)->member) Remove the address of the data member. Because the address of type is 0, the address obtained here is the offset of the relative member in type.

(size_t) (& (((type*) 0)->member) result conversion type. For 32-bit systems, the size_t is the unsigned int type, and for 64-bit systems, size_t is the unsigned long type.

1.2 Offsetof Sample

Code (OFFSET_TEST.C)

 1 #include <stdio.h>
 2 
 3//Get the offset of the variable members (member) of the struct body (TYPE) in this structure.
 4 #define OFFSETOF (type, member) ((size_t) & ((type *) 0)->member)
 5 
 6 struct student
 7 {
 8< C8/>char gender;
 9     int id;     int age;
One     char name[20];
; 
void Main ()     int gender_offset, Id_offset, Age_offset, Name_offset;     gender_offset = offsetof (struct student, gender);     id_offset = offsetof (struct student, id);     age_offset = offsetof (struct student, age);     name_offset = offsetof (struct student, name);     printf ("Gender_offset =%d\n", gender_offset);     printf ("Id_offset =%d\n", id_offset);     printf ("Age_offset =%d\n", age_offset);
Num     printf ("Name_offset =%d\n", name_offset);
27}

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.