Use Linux kernel list in user programs

Source: Internet
Author: User
Tags prefetch
Use Linux kernel list-general Linux technology-Linux programming and kernel information in your program. The following is a detailed description. In our daily programs, we often encounter writing linked lists. For c ++ programs, stl provides two templates: list and vector, which are easy to use. If you restrict the compiler to use only c, many people encapsulate related interfaces by themselves, which takes unnecessary time. The linux Kernel provides the object-oriented linked list: include/linux/list. h.

Reprinted Please retain original (msn and email: jinglexy at yahoo dot com dot cn), Suzhou Koda Technology Research Institute.

Here is the linked list program obtained from kernel 2.6.10 of montavista. There are some differences between kernel 2.6 and kernel 2.4. Only the usage is described here.

First, modify the source program and add the following program after # ifdef _ KERNEL _ and 3 include statements:

(Blue is added)

# Ifdef _ KERNEL __


# Include

# Include

# Include

# Elif 1


# Define prefetch (x) 1

# Define smp_wmb (x) 1


# Define offsetof (TYPE, MEMBER) (size_t) & (TYPE *) 0)-> MEMBER)

# Define container_of (ptr, type, member )({\

Const typeof (type *) 0)-> member) * _ mptr = (ptr );\

(Type *) (char *) _ mptr-offsetof (type, member ));})



The following is a test procedure:

# Include

# Include

# Include

# Include "list. h"



# Define DEBUG do {\

Printf ("************************** \ n ");\

Printf ("list 1: \ n ");\

List_for_each (I, & list1 ){\

Mylist_t * ops = \

List_entry (I ,\

Mylist_t, list );\

Printf ("pf is % d \ n", ops-> pf );\

}\

\

Printf ("list 2: \ n ");\

List_for_each (I, & list2 ){\

Mylist_t * ops = \

List_entry (I ,\

Mylist_t, list );\

Printf ("pf is % d \ n", ops-> pf );\

}\

} While (0 );



# If 1

Typedef struct _ mylist {

Int pf;

Char name [FILENAME_MAX];



Struct list_head list;

} Mylist_t;

# Else

Typedef struct _ mylist {

Struct list_head list;



Int pf;

Char name [FILENAME_MAX];

} Mylist_t;

# Endif



Static LIST_HEAD (list1);/* define 2 list */

Static LIST_HEAD (list2 );



Int main (int argc, char * argv [])

{

Struct list_head * I;

Mylist_t elem1;

Mylist_t elem2;



INIT_LIST_HEAD (& list1 );

INIT_LIST_HEAD (& list2 );

Elem1.pf = 2008;

Elem2.pf = 8002;

List_add (& elem1.list, & list1 );

List_del (& elem1.list );



List_add (& elem1.list, & list1 );

List_add_tail (& elem2.list, & list1 );

DEBUG;



/* Move element to another list */

List_move_tail (& elem2.list, & list2 );

List_splice_init (& list1, & list2);/* merge */

DEBUG;



Return 0;

}

Compile with gcc, run the gcc test. c program, and the result is exactly what we want.
Related Article

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.