linux内核链表的移植与使用
一、
Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲。由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用。
/***********************************
文件名:kernel link list of linux.h
作者:Bumble Bee
日期:2015-1-31
功能:移植linux内核链表
************************************/ /*链表结点数据结构*/
struct list_head
{
struct list_head *next, *prev;
}; /***********************************
函数名: INIT_LIST_HEAD
参数: 指向list_head结构体的指针
返回值: 无
函数功能:通过将前向指针和后向指
针指向自己来创建一个链表表
头
***********************************/
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
} /***********************************
函数名: __list_add
参数: @new:要插入结点的指针域
@prev:前一个节点的指针域
@next:后一个节点的指针域
返回值: 无
函数功能:在两个已知节点中插入新节点
***********************************/ 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;
}
extern void __list_add(struct list_head *new,
struct list_head *prev, struct list_head *next); /**************************************
函数名: list_add
参数: @new:要插入结点的指针域
@head:要插入链表表头的指针域
返回值: 无
函数功能:在已知链表头部插入新节点
**************************************/ static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
} /**************************************
函数名: list_add_tail
参数: @new:要插入结点的指针域
@head:要插入链表表头的指针域
返回值: 无
函数功能:在已知链表尾部插入新节点
**************************************/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
} /*************************************
函数名: list_for_each
参数: @pos:遍历链表的光标
@head:要遍历链表的表头
返回值: 无
函数功能:实质为一个for循环,遍历链表
*************************************/
#define list_for_each(pos, head) \
for (pos = (head)->next;pos != (head); \
pos = pos->next) /*************************************************
函数名: list_entry
参数: @ptr:节点中list_head的地址
@type:节点的类型
@member:list_head 在结构体中成员的名字
返回值: 节点的地址,已被强制转化为type型指针
函数功能:将节点最低位置假设为0,此时取成员member
的地址即为offset,再用list_head的地址将
offset减去即为节点的地址
**************************************************/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member) #define container_of(ptr, type, member) ({ \
const typeof(((type *))->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); }) #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
二、设计应用程序测试链表
/****************************
文件名:homework.c
作者:Bumble Bee
日期:2015-1-31
功能:测试移植的linux内核链表
*****************************/ #include <stdio.h>
#include "kernel link list of linux.h" struct score
{
int num;
int english;
int math;
struct list_head list;
}; struct score stu1,stu2,stu3,*temp; struct list_head score_head,*pos; int main()
{
INIT_LIST_HEAD(&score_head); //创建链表函数 stu1.num = ;
stu1.english = ;
stu1.math = ;
list_add_tail(&(stu1.list),&(score_head)); stu2.num = ;
stu2.english = ;
stu2.math = ;
list_add_tail(&(stu2.list),&(score_head)); stu3.num = ;
stu3.english = ;
stu3.math = ;
list_add_tail(&(stu3.list),&(score_head)); list_del(&(stu2.list)); list_for_each(pos,&(score_head))
{
temp = list_entry(pos,struct score,list);
printf("No %d,english is %d,math is %d\n",temp->num,temp->english,temp->math);
} return ; }
三、运行结果

linux内核链表的移植与使用的更多相关文章
- [国嵌攻略][108][Linux内核链表]
链表简介 链表是一种常见的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...
- 第32课 Linux内核链表剖析
1. Linux内核链表的位置及依赖 (1)位置:{linux-2.6.39}\\include\linux\list.h (2)依赖 ①#include<linux\types.h> ② ...
- linux内核链表使用
原文链接:http://blog.csdn.net/xnwyd/article/details/7359373 Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p ...
- 数据结构开发(10):Linux内核链表
0.目录 1.老生常谈的两个宏(Linux) 1.1 offsetof 1.2 container_of 2.Linux内核链表剖析 3.小结 1.老生常谈的两个宏(Linux) Linux 内核中常 ...
- linux内核链表剖析
1.移植linux内核链表,使其适用于非GNU编译器 2.分析linux内核中链表的基本实现 移植时的注意事项 清除文件间的依赖 剥离依赖文件中与链表实现相关的代码 清除平台相关的代码(GNU C) ...
- Linux内核链表——看这一篇文章就够了
本文从最基本的内核链表出发,引出初始化INIT_LIST_HEAD函数,然后介绍list_add,通过改变链表位置的问题引出list_for_each函数,然后为了获取容器结构地址,引出offseto ...
- C语言 Linux内核链表(企业级链表)
//Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 深入分析 Linux 内核链表--转
引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...
- Linux 内核链表
一 . Linux内核链表 1 . 内核链表函数 1.INIT_LIST_HEAD:创建链表 2.list_add:在链表头插入节点 3.list_add_tail:在链表尾插入节点 4.list_d ...
随机推荐
- Spine(2D骨骼动画)
点击进入Spine官网 Spine 是一款针对游戏的 2D 骨骼动画编辑工具. Spine 旨在提供更高效和简洁 的工作流程,以创建游戏所需的动画. 功能: 1.摄影表 Dopesheet 在动画制作 ...
- Lucene和jackson冲突
今天在使用lucene的时候,想直接在Controller中返回json对象,于是在Spring中配置了JackSon的converter: <bean id="jacksonMess ...
- Cocos2dx 多线程
多-threaded负荷plist特征.获取知识的必要性: 1.多线程开启:pthread 2.怎样在线程中载入plist 一.多线程开启 当我们想在程序中开多线程中.第一想到的是cocos2d-x有 ...
- 【剑指offer】链表倒数第k个节点
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25662121 在Cracking the Code Interview上做过了一次,这次在 ...
- Android 百度地图 SDK v3.0.0 (一)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37729091 最近公司要把百度地图集成的项目中,于是我就研究了一天百度地图的SD ...
- Java基础知识强化之IO流笔记04:throw和throws的区别
1. throw概述 在功能方法内部出现某种问题,程序不能继续运行,需要进行跳转时,就用throw把异常对象抛出. 2. 案例演示: (1) package com.himi.throwdemo; p ...
- 配置NTP时间服务器
一.安装ntp软件 1.检查是否安装了ntp相关包. rpm -qa | grep ntp 2.安装ntp软件. yum -y install ntp 二.参数讲解 ignore :关闭所有的 NT ...
- HUD 2846 Repository
/* 开始想耍小聪明 直接map搞 代码短 好理解 空间够 恩 很好 就是 map慢死了 T了 */ #include<iostream> #include<cstdio> # ...
- Regex.Escape
C# 字符串变量str 的值为"a\nb"如果直接输出显示的话,就成了:ab需要输出显示为:a\nb问,怎么办?千万别告诉我定义: str=@"a\nb",因为 ...
- 返回到上一页的html代码的几种写法
关键词:返回上一页 html代码超链接返回上一页代码: <a href=”#” onClick=”javascript :history.back(-1);”>返回上一页</a> ...