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,如需转载请自行联系原作者


相关文章
|
16小时前
|
缓存 运维 算法
深入理解Linux内核的虚拟内存管理
【5月更文挑战第6天】 在现代操作系统中,尤其是类Unix系统如Linux中,虚拟内存管理是一项核心功能,它不仅支持了多任务环境,还提供了内存保护和抽象。本文将深入探讨Linux操作系统的虚拟内存子系统,包括分页机制、虚拟地址空间布局、页面置换算法以及内存分配策略。通过对这些概念的剖析,我们旨在为读者揭示Linux如何有效地管理和优化物理内存资源,并确保系统的稳定运行与高效性能。
|
16小时前
|
Linux 调度 开发者
探索Linux内核调度:公平与效率的平衡艺术
【5月更文挑战第6天】 随着多核处理器的普及,操作系统的进程调度策略对系统性能的影响愈加显著。Linux作为广泛应用的开源操作系统,其内核调度器的设计哲学和实现细节一直是系统研究领域的热点。本文将深入分析Linux内核调度器的工作原理,探讨如何在保证公平性和效率之间取得平衡,并考察最新的调度器CFS(Completely Fair Scheduler)如何适应现代硬件架构的需求。
|
1天前
|
存储 运维 Java
Linux笔记02 —— Shell补充
Linux笔记02 —— Shell补充
18 2
|
1天前
|
安全 Linux Shell
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
9 1
|
1天前
|
安全 Linux Android开发
FFmpeg开发笔记(十六)Linux交叉编译Android的OpenSSL库
该文介绍了如何在Linux服务器上交叉编译Android的FFmpeg库以支持HTTPS视频播放。首先,从GitHub下载openssl源码,解压后通过编译脚本`build_openssl.sh`生成64位静态库。接着,更新环境变量加载openssl,并编辑FFmpeg配置脚本`config_ffmpeg_openssl.sh`启用openssl支持。然后,编译安装FFmpeg。最后,将编译好的库文件导入App工程的相应目录,修改视频链接为HTTPS,App即可播放HTTPS在线视频。
FFmpeg开发笔记(十六)Linux交叉编译Android的OpenSSL库
|
5天前
|
Linux Shell 调度
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
|
5天前
|
运维 监控 Linux
提升系统稳定性:Linux内核参数调优实战
【5月更文挑战第1天】 在运维领域,保障服务器的高效稳定运行是核心任务之一。Linux操作系统因其开源、可靠和灵活的特点被广泛应用于服务器中。本文将深入探讨通过调整Linux内核参数来优化系统性能,提升服务器的稳定性和响应能力。文章首先介绍了内核参数调优的必要性和基本原则,然后详细阐述了调优过程中的关键步骤,包括如何监控当前系统状态,确定性能瓶颈,选择合适的参数进行调优,以及调优后的测试与验证。最后,文中提供了一些常见问题的解决策略和调优的最佳实践。
26 5
|
6天前
|
算法 大数据 Linux
深入理解Linux内核的进程调度机制
【4月更文挑战第30天】操作系统的核心职能之一是有效地管理和调度进程,确保系统资源的合理分配和高效利用。在众多操作系统中,Linux因其开源和高度可定制的特点,在进程调度机制上展现出独特优势。本文将深入探讨Linux内核中的进程调度器——完全公平调度器(CFS),分析其设计理念、实现原理及面临的挑战,并探索未来可能的改进方向。
|
6天前
|
算法 Linux 调度
探索Linux内核:进程调度的奥秘
【4月更文挑战第30天】 在多任务操作系统中,进程调度是核心功能之一,它决定了处理器资源的分配。本文深入分析了Linux操作系统的进程调度机制,从调度器的基本原理到复杂的调度策略,以及它们如何影响系统性能和用户体验。通过剖析进程优先级、时间片分配以及实时性要求等方面,揭示了Linux如何在众多运行着的进程中做出快速而公平的决策,确保系统的高效与稳定运行。
|
6天前
|
算法 安全 Linux
深度解析:Linux内核内存管理机制
【4月更文挑战第30天】 在操作系统领域,内存管理是核心功能之一,尤其对于多任务操作系统来说更是如此。本文将深入探讨Linux操作系统的内核内存管理机制,包括物理内存的分配与回收、虚拟内存的映射以及页面替换算法等关键技术。通过对这些技术的详细剖析,我们不仅能够理解操作系统如何高效地利用有限的硬件资源,还能领会到系统设计中的性能与复杂度之间的权衡。