1:内核常用的辅助接口函数的介绍(struct list_head)
链表是一种常用的组织有序数据的数据结构,它通过指针将一系列数据节点连接成一条数据链,是线性
表的一种重要实现方式。相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可
以随机分配空间,可以高效地在链表中的任意位置实时插入或删除数据。
1:链表的原型//声明,基本添加,删除,查找等操作。
struct list_head{
struct list_head *next;
struct list_head *prev;
}
#define LIST_HEAD_INIT(name) {&(name),&(name)}
#define LIST_HEAD(name) struct list_head name=LIST_HEAD_INIT(name)
所以我们要声明一个链表的时候,则使用
LIST_HEAD(mylist);//声明并定义了一个链表mylist;
同时也可以采用下面的方式;
struct list_head mylist;//先声明
INIT_LIST_HEAD(&mylist);//再初始化
在linux的内核链表中,需要用链表组织起来的数据通常会包含一个struct list_head成员;
例如:
struct data{
struct list_head list;
……
void datas;
};
struct data mydata0={……};
struct data mydata1={……};
通过list_head,将他们连在一起,形成链表;
删除操作:static inline void list_del(struct list_head *entry);
插入操作:
static inline void list_add(struct list_head *new, struct list_head *head);
static inline void list_add_tail(struct list_head *new, struct list_head *head);
链表的遍历:
(1):list_entry(ptr,type,member);通过list_head的指针找到宿主体;
ptr指向该数据中list_head成员的指针;
type是数据项的类型(宿主体类型);
member则是数据项类型定义中list_head成员的变量名;
example:
/*********************************************************************
*
* Filename: pfile.c
* Version: 1.0
* Description: Demo for Linux LIST utility
* Compilation: gcc -D__KERNEL__ -I/usr/src/linux/include pfile.c
* Status: Stable
* Author: Yang Shazhou
* Created at: Thu Jul 15 13:50:33 2004
* Modified at: Thu Jul 15 14:39:03 2004
* Modified by: Yang Shazhou
*
* Copyright (c) 2004 Yang Shazhou, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
********************************************************************/
#include
#include
#include
int main(int argc,char *argv[])
{
LIST_HEAD(list); //定义存放文件内容的链表
FILE *fp;
struct file_store {
char c;
struct list_head node;
} *pstore;
if(argc<2){
printf("usage: pfile <[r]>\n");
return -1;
}
if(!(fp=fopen(argv[1],"rb"))){
printf("fopen(%s) error\n",argv[1]);
return -1;
}
/* 读文件到链表 */
while(1){
if(!(pstore=(struct file_store *)malloc(sizeof(struct file_store))))
break;
pstore->c=fgetc(fp);
printf("input %c\n " ,pstore->c) ;
if(feof(fp)){
free(pstore);
break;
}
list_add_tail(&pstore->node,&list); //将本字符插入链表中
}
fclose(fp);
/* 遍历链表,输出链表中的节点个数,即文件字符数 */
int count=0;
struct list_head *p;
list_for_each(p,&list){
count++;
}
printf("%s has altogether %d character(s)\n",argv[1],count);
/* 根据命令行参数正向/反向遍历链表,输出链表中存放的字符,同时释放各节点 */
/*if(argc>2 && !strcasecmp(argv[2],"r")){
struct list_head *p;
list_for_each_entry_reverse(pstore,&list,node){ //反向遍历,没有保护
p=pstore->node.next;
list_del(&pstore->node);
putchar(pstore->c);
free(pstore);
// 如果没有这一句,将报segmentation fault //
pstore=list_entry(p,struct file_store,node); //取数据项
}
}else */{
struct file_store *pstorenode;
struct list_head *p ,*n;
int k = 0 ;
// list_for_each_entry_safe(pstore,p,&list,node){ //正向遍历,有保护
// list_for_each(p,&list) {
list_for_each_safe(p, n,&list) {
pstorenode = list_entry(p ,struct file_store ,node) ;
putchar(pstorenode->c);
//list_del_init(&(pstorenode->node));
k++ ;
if( k ==7) break ;
free(pstorenode);
}
}
return 0;
}
笔者尝试使用2.6.24的版本测试上述代码,测试无法通过;直接采用低版本的list文件
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#include
#include "linux/poison.h"
#ifndef __KERNEL__
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif /* __KERNEL__ */
/*
* 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.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
#else
extern void list_add(struct list_head *new, struct list_head *head);
#endif
/**
* 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);
}
/**
* 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)
/*
* 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_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_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;
entry->next = entry;
entry->prev = entry;
}
#else
extern void list_del(struct list_head *entry);
#endif
/**
* 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)
{
return head->next == head;
}
/**
* 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; 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; pos != (head); \
pos = 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); \
&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_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)
/*
* 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;
}
#endif
阅读(1256) | 评论(0) | 转发(0) |