Linux内核链表的移植与使用
生活随笔
收集整理的這篇文章主要介紹了
Linux内核链表的移植与使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、Linux內(nèi)核鏈表為雙向循環(huán)鏈表,和數(shù)據(jù)結(jié)構(gòu)中所學(xué)鏈表類似,具體不再細(xì)講。由于在內(nèi)核中所實(shí)現(xiàn)的函數(shù)十分經(jīng)典,所以移植出來方便后期應(yīng)用程序中的使用。
/**************************************** 文件名:kernel_link_list_of_linux.h 作者:Bumble Bee 日期:2015-1-31 功能:移植linux內(nèi)核鏈表 *****************************************//*鏈表結(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu)*/ struct list__head {struct list_head *next, *prev; };/**************************************** 函數(shù)名: INIT_LIST_HEAD 參數(shù): 指向list_head結(jié)構(gòu)體的指針 返回值: 無 函數(shù)功能:通過將前向指針和后向指針指向自己來創(chuàng)建一個(gè)鏈表表頭 *****************************************/ static inline void INIT_LIST_HEAD(struct list_head *list) {list->next = list;list->prev = list; }/*************************************** 函數(shù)名: __list_add 參數(shù): @new:要插入結(jié)點(diǎn)的指針域@prev: 前一個(gè)節(jié)點(diǎn)的指針域@next: 后一個(gè)節(jié)點(diǎn)的指針域 返回值:無 函數(shù)功能:在兩個(gè)已知節(jié)點(diǎn)中插入新節(jié)點(diǎn) ***************************************/ 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);/************************************** 函數(shù)名: list_add 參數(shù): @new: 要插入結(jié)點(diǎn)的指針域@head: 要插入鏈表表頭的指針域 返回值: 無 函數(shù)功能: 在已知鏈表頭部插入新節(jié)點(diǎn) **************************************/ static inline void list_add(struct list_head *new, struct list_head *head) {__list_add(new, head, head->next); }/************************************* 函數(shù)名: list_add_tail 參數(shù): @new: 要插入結(jié)點(diǎn)的指針域@head: 要插入鏈表表頭的指針域 返回值:無 函數(shù)功能: 在已知鏈表尾部插入新結(jié)點(diǎn) **************************************/ static inline void list_add_tail(struct list_head *new, struct list_head *head) {__list_add(new, head->prev, head); }/************************************* 函數(shù)名: list_for_each 參數(shù): @pos: 遍歷鏈表的光標(biāo)@head: 要遍歷鏈表的表頭 返回值:無 函數(shù)功能:實(shí)際為一個(gè)for循環(huán),遍歷鏈表 **************************************/ #define list_for_each(pos, head) \for (pos = (head)->next; pos != (head); \pos = pos->next;/************************************* 函數(shù)名: list_entry 參數(shù): @ptr: 節(jié)點(diǎn)中l(wèi)ist_head的地址@type: 節(jié)點(diǎn)的類型@member: list_head 在結(jié)構(gòu)體中成員的名字 返回值:節(jié)點(diǎn)的地址,已被強(qiáng)制轉(zhuǎn)化為type型指針 函數(shù)功能: 將節(jié)點(diǎn)最低位置假設(shè)為0,此時(shí)取成員member的地址即為offset,再用list_head的地址將offset減去即為節(jié)點(diǎn)的地址 **************************************/ #define list_entry(ptr, type, member) \container_of(ptr, type, member)#define container_of(ptr, type, member) ({ \const typeof(((type *)0)->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); }二、設(shè)計(jì)應(yīng)用程序測試鏈表
/********************************** 文件名: homework.c 作者: Bumble Bee 日期: 2015-1-31 功能:測試移植的linux內(nèi)核鏈表 ***********************************/ #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); //創(chuàng)建鏈表函數(shù)stu1.num = 1;stu1.english = 0;stu1.math = 0;list_add_tail(&(stu1.list), &(score_head));stu2.num = 2;stu2.english = 1;stu2.math = 1;list_add_tail(&(stu2.list), &(score__head));stu3.num = 3;stu3.english = 2;stu3.math = 2;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 0; }三、運(yùn)行結(jié)果
轉(zhuǎn)自:https://www.cnblogs.com/51qianrushi/p/4294406.html
總結(jié)
以上是生活随笔為你收集整理的Linux内核链表的移植与使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用top命令监控linux系统cpu变
- 下一篇: Linux iptables用法与NAT