Linux2.6.32内核笔记(5)在应用程序中移植使用内核链表【转】

简介:

转自:http://blog.csdn.net/Deep_l_zh/article/details/48392935

复制代码
版权声明:本文为博主原创文章,未经博主允许不得转载。

    摘要:将内核链表移植到应用程序中,实现创建,添加节点,遍历,删除的操作。

    

    首先复习一下内核链表中经常使用的几个函数,在/include/Linux/list.h中。

    

    创建链表:

[html] view plain copy

    <span style="font-size:18px;">INIT_LIST_HEAD()  
    staticinline void INIT_LIST_HEAD(struct list_head *list)  
    {  
        list->next = list;  
        list->prev = list;  
    }</span>  



    插入节点:

[objc] view plain copy

    <span style="font-size:18px;">list_add()在链表头插入  
    list_add_tail()在链表尾插入  
    staticinline void list_add(struct list_head *new, struct list_head *head)  
    {  
        __list_add(new, head, head->next);  
    }  
    staticinline void list_add_tail(struct list_head *new, struct list_head *head)  
    {  
        __list_add(new, head->prev, head);  
    }</span>  
         



    删除节点:

[objc] view plain copy

    <span style="font-size:18px;">list_del()  
    staticinline void list_del(struct list_head *entry)  
    {  
        __list_del(entry->prev, entry->next);  
        entry->next = LIST_POISON1;  
        entry->prev = LIST_POISON2;  
    }</span>  


    遍历链表:

[objc] view plain copy

    <span style="font-size:18px;">list_for_each()  
    #definelist_for_each(pos, head) \  
    for(pos = (head)->next; prefetch(pos->next), pos != (head); \  
          pos = pos->next)</span>  
       


    取出节点:

[objc] view plain copy

    <span style="font-size:18px;">list_entry()  
    #definelist_entry(ptr, type, member) \  
    container_of(ptr,type, member)</span>  
         


    移植过程中用到的其他函数:

    1.malloc

    函数原型:extern void *malloc(unsigned int num_bytes);

   功能:分配字节长度为num_bytes内存,如果成功则返回指向内存起始地址的指针,否则返回null。

    说明:这里声明为void *表示未确定类型的指针,这样使用的时候就可以强制转换为其他我们需要的任何类型的指针。

    2.memset

    函数原型:void *memset(void *s,int ch,seze_t n);

    功能:将s指向的某一块内存中的前n个字节的内容全部填充为ch。一般用来对新申请的内存做初始化工作,ch一般都是填充0。我们在使用较大的结构体和数组的时候,都会使用其对分配到的内存清零。

    3.sprintf

    函数原型:int sprintf(char *buffer,const char *format,[arugument]…);

    功能:把格式化的数据写入某个字符串中,返回值是字符串的长度。


    移植步骤:

    1.创建list.h

    因为我们要写成一个app,里面用到很多内核链表的函数,都在list.h里面声明的,一开始这里我就偷懒把内核里面的list.h拷贝一份,放到我当前的工作目录下,命名为list.h,后来编译的时候提示找不到list.h里面加进去的那三个头文件,于是我又把position.h,这三个头文件注释掉了,但是提示LIST_POSITION1和LIST_POSITION2没有定义还有别的错误,于是利用grep查找,到源码目录下,把这部分拷贝到我们的list.h前面部分里面来就可以了。完整的list.c附在最后。

[objc] view plain copy

    <span style="font-size:18px;">#ifndef _LINUX_LIST_H  
    #define _LINUX_LIST_H  
       
       
    #include <linux/stddef.h>  
       
    #ifndef ARCH_HAS_PREFETCH  
    #define ARCH_HAS_PREFETCH  
    static inline void prefetch(const voidvoid *x){;}  
    #endif  
       
    #define LIST_POISON1 ((void *) 0x0)   
    #define LIST_POISON2 ((void *) 0x0)  
       
    #define container_of(ptr ,type,member)({              \  
       const typeof( ((type *)0)->member ) *__mptr = (ptr);     \  
       (type *)( (charchar *)__mptr - offsetof(type,member) );})</span>  


    2.创建listapp.c添加头文件

    这里我命名为listapp.c,因为我们要用到很多头文件,这里都添加进去,我添加的如下;

[objc] view plain copy

    <span style="font-size:18px;">#include"list.h"//内核链表操作函数  
    #include<malloc.h>//使用malloc分配内存  
    #include<stdio.h>//sprintf和printf  
    #include<string.h>//memset</span><span style="font-size:14px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">                </span>  

   

     3.创建球员信息结构体

[objc] view plain copy

    <span style="font-size:18px;">    structmember  
    {  
        charname[10];  
        intnum;  
        intscore;  
        intassists;  
        structlist_head list;  
    };</span>  

    4.main函数

    主要思想是创建链表,分配内存,插入节点,遍历输出,删除节点。

    编译成功后运行出现如下信息;

   

    可以看到我们的链表操作是成功了,输出信息也与期望值一样,但是最后free的时候出现了core dump,这个问题查了下有几种解释,这里大概是数组操作越界,或者我们修改了mem区的指针信息,导致free释放内存的时候,释放到别的地方去了,这里不做深究了,留待之后结局。

    最后附上list.h和listapp.c的代码,结束,如有不正确的地方还请指出,大家共同进步。

    

list.h如下
[objc] view plain copy

    <span style="font-size:14px;">#ifndef _LINUX_LIST_H  
    #define _LINUX_LIST_H  
      
      
    #include <linux/stddef.h>  
      
    #ifndef ARCH_HAS_PREFETCH  
    #define ARCH_HAS_PREFETCH  
    static inline void prefetch(const voidvoid *x) {;}  
    #endif  
      
    #define LIST_POISON1 ((void *) 0x0)    
    #define LIST_POISON2 ((void *) 0x0)  
      
    #define container_of(ptr ,type,member) ({              \  
        const typeof( ((type *)0)->member ) *__mptr = (ptr);     \  
        (type *)( (charchar *)__mptr - offsetof(type,member) );})  
      
      
    /* 
     * Simple doubly linked list implementation. 
     * 
     * Some of the internal functions ("__xxx") are useful when 
     * manipulating whole lists rather than single entries, as 
     * sometimes we already know the next/prev entries and we can 
     * generate better code by using them directly rather than 
     * using the generic single-entry routines. 
     */  
      
    struct list_head {  
        struct list_head *next, *prev;  
    };  
      
    #define LIST_HEAD_INIT(name) { &(name), &(name) }  
      
    #define LIST_HEAD(name) \  
        struct list_head name = LIST_HEAD_INIT(name)  
      
    static inline void INIT_LIST_HEAD(struct list_head *list)  
    {  
        list->next = list;  
        list->prev = list;  
    }  
      
    /* 
     * Insert a new entry between two known consecutive entries. 
     * 
     * This is only for internal list manipulation where we know 
     * the prev/next entries already! 
     */  
    #ifndef CONFIG_DEBUG_LIST  
    static inline void __list_add(struct list_head *new,  
                      struct list_head *prev,  
                      struct list_head *next)  
    {  
        next->prev = new;  
        new->next = next;  
        new->prev = prev;  
        prev->next = new;  
    }  
    #else  
    extern void __list_add(struct list_head *new,  
                      struct list_head *prev,  
                      struct list_head *next);  
    #endif  
      
    /** 
     * list_add - add a new entry 
     * @new: new entry to be added 
     * @head: list head to add it after 
     * 
     * Insert a new entry after the specified head. 
     * This is good for implementing stacks. 
     */  
    static inline void list_add(struct list_head *new, struct list_head *head)  
    {  
        __list_add(new, head, head->next);  
    }  
      
      
    /** 
     * list_add_tail - add a new entry 
     * @new: new entry to be added 
     * @head: list head to add it before 
     * 
     * Insert a new entry before the specified head. 
     * This is useful for implementing queues. 
     */  
    static inline void list_add_tail(struct list_head *new, struct list_head *head)  
    {  
        __list_add(new, head->prev, head);  
    }  
      
    /* 
     * Delete a list entry by making the prev/next entries 
     * point to each other. 
     * 
     * This is only for internal list manipulation where we know 
     * the prev/next entries already! 
     */  
    static inline void __list_del(struct list_head * prev, struct list_head * next)  
    {  
        next->prev = prev;  
        prev->next = next;  
    }  
      
    /** 
     * list_del - deletes entry from list. 
     * @entry: the element to delete from the list. 
     * Note: list_empty() on entry does not return true after this, the entry is 
     * in an undefined state. 
     */  
    #ifndef CONFIG_DEBUG_LIST  
    static inline void list_del(struct list_head *entry)  
    {  
        __list_del(entry->prev, entry->next);  
        entry->next = LIST_POISON1;  
        entry->prev = LIST_POISON2;  
    }  
    #else  
    extern void list_del(struct list_head *entry);  
    #endif  
      
    /** 
     * list_replace - replace old entry by new one 
     * @old : the element to be replaced 
     * @new : the new element to insert 
     * 
     * If @old was empty, it will be overwritten. 
     */  
    static inline void list_replace(struct list_head *old,  
                    struct list_head *new)  
    {  
        new->next = old->next;  
        new->next->prev = new;  
        new->prev = old->prev;  
        new->prev->next = new;  
    }  
      
    static inline void list_replace_init(struct list_head *old,  
                        struct list_head *new)  
    {  
        list_replace(old, new);  
        INIT_LIST_HEAD(old);  
    }  
      
    /** 
     * list_del_init - deletes entry from list and reinitialize it. 
     * @entry: the element to delete from the list. 
     */  
    static inline void list_del_init(struct list_head *entry)  
    {  
        __list_del(entry->prev, entry->next);  
        INIT_LIST_HEAD(entry);  
    }  
      
    /** 
     * list_move - delete from one list and add as another's head 
     * @list: the entry to move 
     * @head: the head that will precede our entry 
     */  
    static inline void list_move(struct list_head *list, struct list_head *head)  
    {  
        __list_del(list->prev, list->next);  
        list_add(list, head);  
    }  
      
    /** 
     * list_move_tail - delete from one list and add as another's tail 
     * @list: the entry to move 
     * @head: the head that will follow our entry 
     */  
    static inline void list_move_tail(struct list_head *list,  
                      struct list_head *head)  
    {  
        __list_del(list->prev, list->next);  
        list_add_tail(list, head);  
    }  
      
    /** 
     * list_is_last - tests whether @list is the last entry in list @head 
     * @list: the entry to test 
     * @head: the head of the list 
     */  
    static inline int list_is_last(const struct list_head *list,  
                    const struct list_head *head)  
    {  
        return list->next == head;  
    }  
      
    /** 
     * list_empty - tests whether a list is empty 
     * @head: the list to test. 
     */  
    static inline int list_empty(const struct list_head *head)  
    {  
        return head->next == head;  
    }  
      
    /** 
     * list_empty_careful - tests whether a list is empty and not being modified 
     * @head: the list to test 
     * 
     * Description: 
     * tests whether a list is empty _and_ checks that no other CPU might be 
     * in the process of modifying either member (next or prev) 
     * 
     * NOTE: using list_empty_careful() without synchronization 
     * can only be safe if the only activity that can happen 
     * to the list entry is list_del_init(). Eg. it cannot be used 
     * if another CPU could re-list_add() it. 
     */  
    static inline int list_empty_careful(const struct list_head *head)  
    {  
        struct list_head *next = head->next;  
        return (next == head) && (next == head->prev);  
    }  
      
    /** 
     * list_is_singular - tests whether a list has just one entry. 
     * @head: the list to test. 
     */  
    static inline int list_is_singular(const struct list_head *head)  
    {  
        return !list_empty(head) && (head->next == head->prev);  
    }  
      
    static inline void __list_cut_position(struct list_head *list,  
            struct list_head *head, struct list_head *entry)  
    {  
        struct list_head *new_first = entry->next;  
        list->next = head->next;  
        list->next->prev = list;  
        list->prev = entry;  
        entry->next = list;  
        head->next = new_first;  
        new_first->prev = head;  
    }  
      
    /** 
     * list_cut_position - cut a list into two 
     * @list: a new list to add all removed entries 
     * @head: a list with entries 
     * @entry: an entry within head, could be the head itself 
     *  and if so we won't cut the list 
     * 
     * This helper moves the initial part of @head, up to and 
     * including @entry, from @head to @list. You should 
     * pass on @entry an element you know is on @head. @list 
     * should be an empty list or a list you do not care about 
     * losing its data. 
     * 
     */  
    static inline void list_cut_position(struct list_head *list,  
            struct list_head *head, struct list_head *entry)  
    {  
        if (list_empty(head))  
            return;  
        if (list_is_singular(head) &&  
            (head->next != entry && head != entry))  
            return;  
        if (entry == head)  
            INIT_LIST_HEAD(list);  
        else  
            __list_cut_position(list, head, entry);  
    }  
      
    static inline void __list_splice(const struct list_head *list,  
                     struct list_head *prev,  
                     struct list_head *next)  
    {  
        struct list_head *first = list->next;  
        struct list_head *last = list->prev;  
      
        first->prev = prev;  
        prev->next = first;  
      
        last->next = next;  
        next->prev = last;  
    }  
      
    /** 
     * list_splice - join two lists, this is designed for stacks 
     * @list: the new list to add. 
     * @head: the place to add it in the first list. 
     */  
    static inline void list_splice(const struct list_head *list,  
                    struct list_head *head)  
    {  
        if (!list_empty(list))  
            __list_splice(list, head, head->next);  
    }  
      
    /** 
     * list_splice_tail - join two lists, each list being a queue 
     * @list: the new list to add. 
     * @head: the place to add it in the first list. 
     */  
    static inline void list_splice_tail(struct list_head *list,  
                    struct list_head *head)  
    {  
        if (!list_empty(list))  
            __list_splice(list, head->prev, head);  
    }  
      
    /** 
     * list_splice_init - join two lists and reinitialise the emptied list. 
     * @list: the new list to add. 
     * @head: the place to add it in the first list. 
     * 
     * The list at @list is reinitialised 
     */  
    static inline void list_splice_init(struct list_head *list,  
                        struct list_head *head)  
    {  
        if (!list_empty(list)) {  
            __list_splice(list, head, head->next);  
            INIT_LIST_HEAD(list);  
        }  
    }  
      
    /** 
     * list_splice_tail_init - join two lists and reinitialise the emptied list 
     * @list: the new list to add. 
     * @head: the place to add it in the first list. 
     * 
     * Each of the lists is a queue. 
     * The list at @list is reinitialised 
     */  
    static inline void list_splice_tail_init(struct list_head *list,  
                         struct list_head *head)  
    {  
        if (!list_empty(list)) {  
            __list_splice(list, head->prev, head);  
            INIT_LIST_HEAD(list);  
        }  
    }  
      
    /** 
     * list_entry - get the struct for this entry 
     * @ptr:    the &struct list_head pointer. 
     * @type:   the type of the struct this is embedded in. 
     * @member: the name of the list_struct within the struct. 
     */  
    #define list_entry(ptr, type, member) \  
        container_of(ptr, type, member)  
      
    /** 
     * list_first_entry - get the first element from a list 
     * @ptr:    the list head to take the element from. 
     * @type:   the type of the struct this is embedded in. 
     * @member: the name of the list_struct within the struct. 
     * 
     * Note, that list is expected to be not empty. 
     */  
    #define list_first_entry(ptr, type, member) \  
        list_entry((ptr)->next, type, member)  
      
    /** 
     * list_for_each    -   iterate over a list 
     * @pos:    the &struct list_head to use as a loop cursor. 
     * @head:   the head for your list. 
     */  
    #define list_for_each(pos, head) \  
        for (pos = (head)->next; prefetch(pos->next), pos != (head); \  
                pos = pos->next)  
      
    /** 
     * __list_for_each  -   iterate over a list 
     * @pos:    the &struct list_head to use as a loop cursor. 
     * @head:   the head for your list. 
     * 
     * This variant differs from list_for_each() in that it's the 
     * simplest possible list iteration code, no prefetching is done. 
     * Use this for code that knows the list to be very short (empty 
     * or 1 entry) most of the time. 
     */  
    #define __list_for_each(pos, head) \  
        for (pos = (head)->next; pos != (head); pos = pos->next)  
      
    /** 
     * list_for_each_prev   -   iterate over a list backwards 
     * @pos:    the &struct list_head to use as a loop cursor. 
     * @head:   the head for your list. 
     */  
    #define list_for_each_prev(pos, head) \  
        for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \  
                pos = pos->prev)  
      
    /** 
     * list_for_each_safe - iterate over a list safe against removal of list entry 
     * @pos:    the &struct list_head to use as a loop cursor. 
     * @n:      another &struct list_head to use as temporary storage 
     * @head:   the head for your list. 
     */  
    #define list_for_each_safe(pos, n, head) \  
        for (pos = (head)->next, n = pos->next; pos != (head); \  
            pos = n, n = pos->next)  
      
    /** 
     * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry 
     * @pos:    the &struct list_head to use as a loop cursor. 
     * @n:      another &struct list_head to use as temporary storage 
     * @head:   the head for your list. 
     */  
    #define list_for_each_prev_safe(pos, n, head) \  
        for (pos = (head)->prev, n = pos->prev; \  
             prefetch(pos->prev), pos != (head); \  
             pos = n, n = pos->prev)  
      
    /** 
     * list_for_each_entry  -   iterate over list of given type 
     * @pos:    the type * to use as a loop cursor. 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     */  
    #define list_for_each_entry(pos, head, member)              \  
        for (pos = list_entry((head)->next, typeof(*pos), member);   \  
             prefetch(pos->member.next), &pos->member != (head);  \  
             pos = list_entry(pos->member.next, typeof(*pos), member))  
      
    /** 
     * list_for_each_entry_reverse - iterate backwards over list of given type. 
     * @pos:    the type * to use as a loop cursor. 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     */  
    #define list_for_each_entry_reverse(pos, head, member)          \  
        for (pos = list_entry((head)->prev, typeof(*pos), member);   \  
             prefetch(pos->member.prev), &pos->member != (head);  \  
             pos = list_entry(pos->member.prev, typeof(*pos), member))  
      
    /** 
     * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() 
     * @pos:    the type * to use as a start point 
     * @head:   the head of the list 
     * @member: the name of the list_struct within the struct. 
     * 
     * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). 
     */  
    #define list_prepare_entry(pos, head, member) \  
        ((pos) ? : list_entry(head, typeof(*pos), member))  
      
    /** 
     * list_for_each_entry_continue - continue iteration over list of given type 
     * @pos:    the type * to use as a loop cursor. 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     * 
     * Continue to iterate over list of given type, continuing after 
     * the current position. 
     */  
    #define list_for_each_entry_continue(pos, head, member)         \  
        for (pos = list_entry(pos->member.next, typeof(*pos), member);   \  
             prefetch(pos->member.next), &pos->member != (head);  \  
             pos = list_entry(pos->member.next, typeof(*pos), member))  
      
    /** 
     * list_for_each_entry_continue_reverse - iterate backwards from the given point 
     * @pos:    the type * to use as a loop cursor. 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     * 
     * Start to iterate over list of given type backwards, continuing after 
     * the current position. 
     */  
    #define list_for_each_entry_continue_reverse(pos, head, member)     \  
        for (pos = list_entry(pos->member.prev, typeof(*pos), member);   \  
             prefetch(pos->member.prev), &pos->member != (head);  \  
             pos = list_entry(pos->member.prev, typeof(*pos), member))  
      
    /** 
     * list_for_each_entry_from - iterate over list of given type from the current point 
     * @pos:    the type * to use as a loop cursor. 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     * 
     * Iterate over list of given type, continuing from current position. 
     */  
    #define list_for_each_entry_from(pos, head, member)             \  
        for (; prefetch(pos->member.next), &pos->member != (head);    \  
             pos = list_entry(pos->member.next, typeof(*pos), member))  
      
    /** 
     * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 
     * @pos:    the type * to use as a loop cursor. 
     * @n:      another type * to use as temporary storage 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     */  
    #define list_for_each_entry_safe(pos, n, head, member)          \  
        for (pos = list_entry((head)->next, typeof(*pos), member),   \  
            n = list_entry(pos->member.next, typeof(*pos), member);  \  
             &pos->member != (head);                     \  
             pos = n, n = list_entry(n->member.next, typeof(*n), member))  
      
    /** 
     * list_for_each_entry_safe_continue 
     * @pos:    the type * to use as a loop cursor. 
     * @n:      another type * to use as temporary storage 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     * 
     * Iterate over list of given type, continuing after current point, 
     * safe against removal of list entry. 
     */  
    #define list_for_each_entry_safe_continue(pos, n, head, member)         \  
        for (pos = list_entry(pos->member.next, typeof(*pos), member),       \  
            n = list_entry(pos->member.next, typeof(*pos), member);      \  
             &pos->member != (head);                     \  
             pos = n, n = list_entry(n->member.next, typeof(*n), member))  
      
    /** 
     * list_for_each_entry_safe_from 
     * @pos:    the type * to use as a loop cursor. 
     * @n:      another type * to use as temporary storage 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     * 
     * Iterate over list of given type from current point, safe against 
     * removal of list entry. 
     */  
    #define list_for_each_entry_safe_from(pos, n, head, member)             \  
        for (n = list_entry(pos->member.next, typeof(*pos), member);     \  
             &pos->member != (head);                     \  
             pos = n, n = list_entry(n->member.next, typeof(*n), member))  
      
    /** 
     * list_for_each_entry_safe_reverse 
     * @pos:    the type * to use as a loop cursor. 
     * @n:      another type * to use as temporary storage 
     * @head:   the head for your list. 
     * @member: the name of the list_struct within the struct. 
     * 
     * Iterate backwards over list of given type, safe against removal 
     * of list entry. 
     */  
    #define list_for_each_entry_safe_reverse(pos, n, head, member)      \  
        for (pos = list_entry((head)->prev, typeof(*pos), member),   \  
            n = list_entry(pos->member.prev, typeof(*pos), member);  \  
             &pos->member != (head);                     \  
             pos = n, n = list_entry(n->member.prev, typeof(*n), member))  
      
    /* 
     * Double linked lists with a single pointer list head. 
     * Mostly useful for hash tables where the two pointer list head is 
     * too wasteful. 
     * You lose the ability to access the tail in O(1). 
     */  
      
    struct hlist_head {  
        struct hlist_node *first;  
    };  
      
    struct hlist_node {  
        struct hlist_node *next, **pprev;  
    };  
      
    #define HLIST_HEAD_INIT { .first = NULL }  
    #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }  
    #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)  
      
    static inline void INIT_HLIST_NODE(struct hlist_node *h)  
    {  
        h->next = NULL;  
        h->pprev = NULL;  
    }  
      
    static inline int hlist_unhashed(const struct hlist_node *h)  
    {  
        return !h->pprev;  
    }  
      
    static inline int hlist_empty(const struct hlist_head *h)  
    {  
        return !h->first;  
    }  
      
    static inline void __hlist_del(struct hlist_node *n)  
    {  
        struct hlist_node *next = n->next;  
        struct hlist_node **pprev = n->pprev;  
        *pprev = next;  
        if (next)  
            next->pprev = pprev;  
    }  
      
    static inline void hlist_del(struct hlist_node *n)  
    {  
        __hlist_del(n);  
        n->next = LIST_POISON1;  
        n->pprev = LIST_POISON2;  
    }  
      
    static inline void hlist_del_init(struct hlist_node *n)  
    {  
        if (!hlist_unhashed(n)) {  
            __hlist_del(n);  
            INIT_HLIST_NODE(n);  
        }  
    }  
      
    static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)  
    {  
        struct hlist_node *first = h->first;  
        n->next = first;  
        if (first)  
            first->pprev = &n->next;  
        h->first = n;  
        n->pprev = &h->first;  
    }  
      
    /* next must be != NULL */  
    static inline void hlist_add_before(struct hlist_node *n,  
                        struct hlist_node *next)  
    {  
        n->pprev = next->pprev;  
        n->next = next;  
        next->pprev = &n->next;  
        *(n->pprev) = n;  
    }  
      
    static inline void hlist_add_after(struct hlist_node *n,  
                        struct hlist_node *next)  
    {  
        next->next = n->next;  
        n->next = next;  
        next->pprev = &n->next;  
      
        if(next->next)  
            next->next->pprev  = &next->next;  
    }  
      
    /* 
     * Move a list from one list head to another. Fixup the pprev 
     * reference of the first entry if it exists. 
     */  
    static inline void hlist_move_list(struct hlist_head *old,  
                       struct hlist_head *new)  
    {  
        new->first = old->first;  
        if (new->first)  
            new->first->pprev = &new->first;  
        old->first = NULL;  
    }  
      
    #define hlist_entry(ptr, type, member) container_of(ptr,type,member)  
      
    #define hlist_for_each(pos, head) \  
        for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \  
             pos = pos->next)  
      
    #define hlist_for_each_safe(pos, n, head) \  
        for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \  
             pos = n)  
      
    /** 
     * hlist_for_each_entry - iterate over list of given type 
     * @tpos:   the type * to use as a loop cursor. 
     * @pos:    the &struct hlist_node to use as a loop cursor. 
     * @head:   the head for your list. 
     * @member: the name of the hlist_node within the struct. 
     */  
    #define hlist_for_each_entry(tpos, pos, head, member)            \  
        for (pos = (head)->first;                     \  
             pos && ({ prefetch(pos->next); 1;}) &&           \  
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \  
             pos = pos->next)  
      
    /** 
     * hlist_for_each_entry_continue - iterate over a hlist continuing after current point 
     * @tpos:   the type * to use as a loop cursor. 
     * @pos:    the &struct hlist_node to use as a loop cursor. 
     * @member: the name of the hlist_node within the struct. 
     */  
    #define hlist_for_each_entry_continue(tpos, pos, member)         \  
        for (pos = (pos)->next;                       \  
             pos && ({ prefetch(pos->next); 1;}) &&           \  
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \  
             pos = pos->next)  
      
    /** 
     * hlist_for_each_entry_from - iterate over a hlist continuing from current point 
     * @tpos:   the type * to use as a loop cursor. 
     * @pos:    the &struct hlist_node to use as a loop cursor. 
     * @member: the name of the hlist_node within the struct. 
     */  
    #define hlist_for_each_entry_from(tpos, pos, member)             \  
        for (; pos && ({ prefetch(pos->next); 1;}) &&             \  
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \  
             pos = pos->next)  
      
    /** 
     * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry 
     * @tpos:   the type * to use as a loop cursor. 
     * @pos:    the &struct hlist_node to use as a loop cursor. 
     * @n:      another &struct hlist_node to use as temporary storage 
     * @head:   the head for your list. 
     * @member: the name of the hlist_node within the struct. 
     */  
    #define hlist_for_each_entry_safe(tpos, pos, n, head, member)        \  
        for (pos = (head)->first;                     \  
             pos && ({ n = pos->next; 1; }) &&                \  
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \  
             pos = n)  
      
    #endif</span>  



listapp.c如下

[objc] view plain copy

    <span style="font-size:14px;">#include"list.h"//内核链表操作函数  
    #include<malloc.h>//使用malloc分配内存  
    #include<stdio.h>//sprintf和printf  
    #include<string.h>//memset  
      
    struct member  
    {  
        char name[100];  
        int num;  
        int score;  
        struct list_head list;  
    };  
      
    struct list_head *pos;//遍历指针的pos,不断地指向链表中节点的指针域,需要是list_head指针类型  
    struct list_head member_list;//名为menber_list的链表  
    struct member *tmp;//存放遍历结果,为struct member类型  
    struct member *pmember;//member的成员  
      
    int main(void)  
    {  
        unsigned int i = 0;            //循环变量的声明  
      
        INIT_LIST_HEAD(&member_list); //创建一个链表头,使其前向和后继指针都指向自己,传入参数必须为指针类型,所以取地址  
          
        pmember=malloc(sizeof(struct member)*4);  
        memset(pmember,0,sizeof(struct member)*4);//为member成员分配内存,这里分配四个成员,并且对分配到的内存清零  
          
        /*给球员成员命名,编号,进球数*/  
        sprintf(pmember[1].name,"player %s","xu");  
        sprintf(pmember[2].name,"player %s","zeng");  
        sprintf(pmember[3].name,"player %s","le");  
        sprintf(pmember[4].name,"player %s","suo");  
          
        pmember[1].num=9;  
        pmember[2].num=21;  
        pmember[3].num=10;  
        pmember[4].num=66;  
          
        pmember[1].score=2;  
        pmember[2].score=0;  
        pmember[3].score=1;   
        pmember[4].score=5;   
          
        /*插入节点,list_add第一个参数是成员内部list的指针,第二个是刚才创建的链表头,这样就插入进去了*/  
        for(i=0;i<4;i++)  
        {  
                list_add(&(pmember[i+1].list),&member_list);  
                printf("###num %d player add sucess!###\n",i+1);  
            }  
          
      
        /*遍历链表,并开始输出球员信息*/  
        printf("###start list_for_each player information###\n");  
        list_for_each(pos,&member_list)  
            {  
                tmp=list_entry(pos,struct member,list);//第一个参数为pos,第二个要给进去我们定义的球员信息结构体,最后是结构内部的list名  
                printf("play %d name %s score %d\n",tmp->num,tmp->name,tmp->score);  
            }  
              
        /*最后删除节点*/  
          
        for(i=0;i<4;i++)  
        {  
            list_del(&(pmember[i+1].list));  
            printf("### num %d has deleted###\n",i+1);  
            }  
          
        /*释放分配得内存*/  
        free(pmember);  
          
        }  
    </span>  
复制代码

 






本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/7133261.html,如需转载请自行联系原作者


相关文章
|
1月前
|
缓存 Linux 开发者
Linux内核中的并发控制机制
本文深入探讨了Linux操作系统中用于管理多线程和进程的并发控制的关键技术,包括原子操作、锁机制、自旋锁、互斥量以及信号量。通过详细分析这些技术的原理和应用,旨在为读者提供一个关于如何有效利用Linux内核提供的并发控制工具以优化系统性能和稳定性的综合视角。
|
1月前
|
缓存 负载均衡 算法
深入探索Linux内核的调度机制
本文旨在揭示Linux操作系统核心的心脏——进程调度机制。我们将从Linux内核的架构出发,深入剖析其调度策略、算法以及它们如何共同作用于系统性能优化和资源管理。不同于常规摘要提供文章概览的方式,本摘要将直接带领读者进入Linux调度机制的世界,通过对其工作原理的解析,展现这一复杂系统的精妙设计与实现。
86 8
|
1月前
|
算法 Linux 调度
深入理解Linux内核调度器:从基础到优化####
本文旨在通过剖析Linux操作系统的心脏——内核调度器,为读者揭开其高效管理CPU资源的神秘面纱。不同于传统的摘要概述,本文将直接以一段精简代码片段作为引子,展示一个简化版的任务调度逻辑,随后逐步深入,详细探讨Linux内核调度器的工作原理、关键数据结构、调度算法演变以及性能调优策略,旨在为开发者与系统管理员提供一份实用的技术指南。 ####
73 4
|
20天前
|
算法 Linux
深入探索Linux内核的内存管理机制
本文旨在为读者提供对Linux操作系统内核中内存管理机制的深入理解。通过探讨Linux内核如何高效地分配、回收和优化内存资源,我们揭示了这一复杂系统背后的原理及其对系统性能的影响。不同于常规的摘要,本文将直接进入主题,不包含背景信息或研究目的等标准部分,而是专注于技术细节和实际操作。
|
20天前
|
存储 缓存 网络协议
Linux操作系统的内核优化与性能调优####
本文深入探讨了Linux操作系统内核的优化策略与性能调优方法,旨在为系统管理员和高级用户提供一套实用的指南。通过分析内核参数调整、文件系统选择、内存管理及网络配置等关键方面,本文揭示了如何有效提升Linux系统的稳定性和运行效率。不同于常规摘要仅概述内容的做法,本摘要直接指出文章的核心价值——提供具体可行的优化措施,助力读者实现系统性能的飞跃。 ####
|
21天前
|
监控 算法 Linux
Linux内核锁机制深度剖析与实践优化####
本文作为一篇技术性文章,深入探讨了Linux操作系统内核中锁机制的工作原理、类型及其在并发控制中的应用,旨在为开发者提供关于如何有效利用这些工具来提升系统性能和稳定性的见解。不同于常规摘要的概述性质,本文将直接通过具体案例分析,展示在不同场景下选择合适的锁策略对于解决竞争条件、死锁问题的重要性,以及如何根据实际需求调整锁的粒度以达到最佳效果,为读者呈现一份实用性强的实践指南。 ####
|
21天前
|
缓存 监控 网络协议
Linux操作系统的内核优化与实践####
本文旨在探讨Linux操作系统内核的优化策略与实际应用案例,深入分析内核参数调优、编译选项配置及实时性能监控的方法。通过具体实例讲解如何根据不同应用场景调整内核设置,以提升系统性能和稳定性,为系统管理员和技术爱好者提供实用的优化指南。 ####
|
23天前
|
负载均衡 算法 Linux
深入探索Linux内核调度机制:公平与效率的平衡####
本文旨在剖析Linux操作系统内核中的进程调度机制,特别是其如何通过CFS(完全公平调度器)算法实现多任务环境下资源分配的公平性与系统响应速度之间的微妙平衡。不同于传统摘要的概览性质,本文摘要将直接聚焦于CFS的核心原理、设计目标及面临的挑战,为读者揭开Linux高效调度的秘密。 ####
34 3
|
26天前
|
负载均衡 算法 Linux
深入探索Linux内核调度器:公平与效率的平衡####
本文通过剖析Linux内核调度器的工作机制,揭示了其在多任务处理环境中如何实现时间片轮转、优先级调整及完全公平调度算法(CFS),以达到既公平又高效地分配CPU资源的目标。通过对比FIFO和RR等传统调度策略,本文展示了Linux调度器如何在复杂的计算场景下优化性能,为系统设计师和开发者提供了宝贵的设计思路。 ####
40 6
|
25天前
|
消息中间件 安全 Linux
深入探索Linux操作系统的内核机制
本文旨在为读者提供一个关于Linux操作系统内核机制的全面解析。通过探讨Linux内核的设计哲学、核心组件、以及其如何高效地管理硬件资源和系统操作,本文揭示了Linux之所以成为众多开发者和组织首选操作系统的原因。不同于常规摘要,此处我们不涉及具体代码或技术细节,而是从宏观的角度审视Linux内核的架构和功能,为对Linux感兴趣的读者提供一个高层次的理解框架。