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>  

 

时间: 2024-09-26 06:48:53

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

编译Linux2.6.32.2内核报错

问题描述 编译Linux2.6.32.2内核报错 joker@Ghost:~/local/linux-2.6.32$ make CHK include/linux/version.h CHK include/linux/utsrelease.h SYMLINK include/asm -> include/asm-x86 CALL scripts/checksyscalls.sh CHK include/linux/compile.h AS arch/x86/kernel/entry_64.o

手把手教你移植linux内核(目标平台S3C2410)

Linux内核的最初版本由Linus Torvalds在1991年发布,后来内核版本不断更新: Linux1.0   1994年3月    仅支持386 Linux1.2   1995年3月    多平台支持 Linux2.0   1996年6月    支持SMP Linux2.2   1999年1月    支持更多的硬件 Linux2.4   2001年1月    支持桌面系统和USB Linux2.6   2003年12月   支持更多平台,使用新的调度器,内核可被抢占,模块子系统和文件系统

《Linux内核修炼之道》——2.2 编译内核

2.2 编译内核 Linux内核修炼之道2.2.1 准备工作 虽然与配置内核相比,编译内核所做的工作要少得多,但是在正式编译之前,我们仍需要做一些必要的准备. 1.需要了解的基础知识 首先我们需要了解系统中与编译过程有关的目录及文件. /boot/vmlinuz-< version >:用于启动的压缩内核镜像. /boot/system.map-< version >:存储内核符号表. /boot/initrd.img-< version >:一个镜像文件,类似ramd

32 64位运行效率-64位,32位windows上的相同程序,运行效率到底区别有多大?

问题描述 64位,32位windows上的相同程序,运行效率到底区别有多大? 64位,32位windows上的相同程序,运行效率到底区别有多大?分别在对应的版本下编译并运行. 解决方案 看什么样的程序.如果是科学计算程序,比如用到很多64位的四则运算,那么显然32位机器要想做同样的工作,需要3.4倍的指令才能完成,那么64位的优势是明显的. 可以举一个例子.你可以运行Windows XP和Windows XP x64版本下的计算器(calc.exe),执行高级运算中的阶乘.计算一个比较大的数,比

浏览器内核转换-程序代码中完成浏览器内核切换成ie

问题描述 程序代码中完成浏览器内核切换成ie 在使用java和jsp制作网站时,遇到了浏览器的问题 问题是需要解决 在代码中完成其他浏览器向ie浏览器内核的转换 不知道这个是不是可以实现 该如何实现

程序-求基于linux内核3.16的netlink实例

问题描述 求基于linux内核3.16的netlink实例 我最近在搞一个基于内核的过滤模块,要用到netlink进行内核于用户程序的交互. 但网上关于netlink的实例基于的内核版本太低了,内核里面关于netlink的函数实现改的我无法下手. 求高手指导!!!感激不尽 解决方案 可以参考一下iproute2(ip和tc命令)和内核的通讯过程.

API Demos 2.3 学习笔记 (3)-- Android应用程序架构

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> Android应用程序以可以分为下三种类型:1.前端Activity(Foreground Activities):通俗一点讲Activity可以理解为一个界面容器,里面装着各种各样的UI组件.例如,上面例子中"Hello World" 显示界面.2.后台服务(Background Services):系统服务(System Service).系统Broadcast(广播信息)与Receiver(广播信息)

Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)

原文:[置顶] Android菜鸟的成长笔记(14)-- Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话来了,当接完电话游戏继续玩),某些应用甚至会保留你离开时候的状态及数据,这些原理是什么?怎么去实现这样的应用?这将是我们这一篇文章要解开的问题. 有一句很常见的代码如下,很多人不知道为什么要加这么一句话 @Override protected void onCreate(Bundle savedIn

Android菜鸟的成长笔记(15)—— Android中的状态保存探究(下)

原文:Android菜鸟的成长笔记(15)-- Android中的状态保存探究(下) 在上一篇中我们简单了解关于Android中状态保存的过程和原理,这一篇中我们来看一下在系统配置改变的情况下保存数据及恢复数据的过程. 下面我们先来看一个现象:(代码在 Android中状态保存探究(上)中) 先启动应用如下: 打印的Log 再翻转屏幕 打印的Log如下 可以看到每翻转一次屏幕实际上系统会停止原理的activity并销毁然后重新启动一次,在这个过程中会调用onSaveInstanceState方法