zoukankan      html  css  js  c++  java
  • Linked Lists in the Linux Kernel

    Hareesh Nagarajan (hnagaraj AT cs uic edu)
    Sat Nov 5 22:47:56 CST 2005

    For a good introduction to the implementation of linked lists API inside the Linux Kernel check this link.

    If you are weary of programming with linked lists inside the Linux kernel, download the list.h file which has been stripped away of kernel specific stuff from this link. Then, all you have to do is, add this file into your userspace code with a:

    #include "list.h"

    and you are all set.

    You can also download the following simple example (which appears below) from this link. The compile flags are the usual:

    gcc link-list-test.c -olink-list-test

    Note: In your kernel code use list_for_each instead of __list_for_each

    The Example

    #include <stdio.h>
    #include <assert.h>
    #include <cstdlib.h>
    
    #include "list.h"
    
    struct foo {
    	int info;
    	struct list_head list_member;
    };
    
    void add_node(int arg, struct list_head *head)
    {
        struct foo *fooPtr = (struct foo *)malloc(sizeof(struct foo));
        assert(fooPtr != NULL);
        
        fooPtr->info = arg;
        INIT_LIST_HEAD(&fooPtr->list_member);
        list_add(&fooPtr->list_member, head);
    }
    
    void display(struct list_head *head)
    {
        struct list_head *iter;
        struct foo *objPtr;
    
        __list_for_each(iter, head) {
            objPtr = list_entry(iter, struct foo, list_member);
            printf("%d ", objPtr->info);
        }
        printf("
    ");
    }
    
    void delete_all(struct list_head *head)
    {
        struct list_head *iter;
        struct foo *objPtr;
        
      redo:
        __list_for_each(iter, head) {
            objPtr = list_entry(iter, struct foo, list_member);
            list_del(&objPtr->list_member);
            free(objPtr);
            goto redo;
        }
    }
    
    int find_first_and_delete(int arg, struct list_head *head)
    {
        struct list_head *iter;
        struct foo *objPtr;
    
        __list_for_each(iter, head) {
            objPtr = list_entry(iter, struct foo, list_member);
            if(objPtr->info == arg) {
                list_del(&objPtr->list_member);
                free(objPtr);
                return 1;
            }
        }
    
        return 0;
    }
    
    main() 
    {
        LIST_HEAD(fooHead);
        
        add_node(10, &fooHead);
        add_node(20, &fooHead);
        add_node(25, &fooHead);
        add_node(30, &fooHead);
        
        display(&fooHead);
        find_first_and_delete(20, &fooHead);
        display(&fooHead);
        delete_all(&fooHead);
        display(&fooHead);
    }
    

    copyright
    https://www.cs.uic.edu/~hnagaraj/articles/linked-list/

    ListExample
    http://www1.cs.columbia.edu/~aya/W3101-01/lectures/linked-list.pdf

    Kernel Linux List - An Efficient Generic Linked List
    http://www.davidespataro.it/kernel-linux-list-efficient-generic-linked-list/

    Doubly linked list
    https://0xax.gitbooks.io/linux-insides/content/DataStructures/linux-datastructures-1.html


  • 相关阅读:
    CF 552(div 3) E Two Teams 线段树,模拟链表
    单词接龙
    书的复制 动规,贪心
    C++文本处理&造轮子
    【NOIP2015】【Luogu2661】信息传递(有向图最小环)
    平时上机练习的注意点(NOIP2018)
    【SHOI2009】【BZOJ2028】会场预约(线段树染色)
    【AHOI2009】【BZOJ1798】Seq 维护序列seq(线段树模板,易错提醒)
    2018 “百度之星”程序设计大赛
    2018 “百度之星”程序设计大赛
  • 原文地址:https://www.cnblogs.com/dong1/p/14976922.html
Copyright © 2011-2022 走看看