在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head。虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作list_head 的通用接口很容易实现代码的重用,有点类似于C++的继承机制 list示例分析
以下提供的源码可直接用于单片机MDK
list.h内容如下
#ifndef __DLIST_H
#define __DLIST_H
/* This file is from Linux Kernel (include/linux/list.h)
* and modified by simply removing hardware prefetching of list items.
* Here by copyright, credits attributed to wherever they belong.
* Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
*/
/*
* 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.
*/
/**
* 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.
*
*/
#if 0//linux内核版本编译不通过
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#else
#ifndef container_of
//作用就是通过一个结构变量中一个成员的地址找到这个结构体变量的首地址
#define container_of(ptr, type, member) ((type*)(((int)ptr) - (int)(&(((type*)0)->member))))
#endif
#endif
/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200)
// 内核标准链表:小结构体
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)
// 初始化小结构体,让其自己形成一个双向循环
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
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;
}
//中间4个永远都是指针,说明内核就是把双向循环的所有指针操作变成了函数。
//内核链表绝对没有前后指针,所有指针操作都封装成了函数
/**
* 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.
*/
// 将新节点new插入到以head为首的链表的开头
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.
*/
// 将新节点new插入到以head为首的链表的末尾
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.
*/
// 将entry指向的节点,从链表中剔除出去
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *)0;
entry->prev = (void *)0;
}
/**
* 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
*/
// 将节点list移动到以head为首的链表的开头
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
*/
// 将节点list移动到以head为首的链表的末尾
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_empty – tests whether a list is empty
* @head: the list to test.
*/
//判断链表是否为空
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice – join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
//在原来链表上添加一个新链表
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, 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);
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.
*/
// 小结构体指针ptr转换成大结构体指针p: p = list_entry(ptr, type, member)
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
// 从头开始往后遍历链表(遍历过程中,不可以将节点删除)
#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 counter.
* @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 counter.
* @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_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @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_entry_safe – iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop counter.
* @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))
#endif
list_test.c内容如下
#if 0
#include "list.h"
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 32
#define MAX_ID_LEN 10
struct list_head myhead;
#define I2C_TYPE 1
#define SPI_TYPE 2
char *dev_name[] = {
"none",
"I2C",
"SPI"
};
struct mylist
{
int type;
char name[MAX_NAME_LEN];
struct list_head list;
};
void display_list(struct list_head *list_head)
{
int i = 0;
struct list_head *p;
struct mylist *entry;
printf("-------list---------\n");
list_for_each(p, list_head)
{
printf("node[%d]\n", i++);
entry = list_entry(p, struct mylist, list);
printf("\ttype: %s\n", dev_name[entry->type]);
printf("\tname: %s\n", entry->name);
}
printf("-------end----------\n");
}
int main(void)
{
struct mylist node1;
struct mylist node2;
INIT_LIST_HEAD(&myhead);
node1.type = I2C_TYPE;
strcpy(node1.name, "yikoulinux");
node2.type = I2C_TYPE;
strcpy(node2.name, "yikoupeng");
list_add(&node1.list, &myhead);
list_add(&node2.list, &myhead);
display_list(&myhead);
list_del(&node1.list);
display_list(&myhead);
return 0;
}
#else 1
#include "list.h"
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 32
#define MAX_ID_LEN 10
struct list_head device_list;
#define I2C_TYPE 1
#define SPI_TYPE 2
char *dev_name[] = {
"none",
"I2C",
"SPI"
};
struct device
{
int type;
char name[MAX_NAME_LEN];
struct list_head list;
};
struct i2c_node
{
int data;
unsigned int reg;
struct device dev;
};
struct spi_node
{
unsigned int reg;
struct device dev;
};
void display_i2c_device(struct device *device)
{
struct i2c_node *i2c_device = container_of(device, struct i2c_node, dev);
printf("\t i2c_device->data: %d\n", i2c_device->data);
printf("\t i2c_device->reg: %#x\n", i2c_device->reg);
}
void display_spi_device(struct device *device)
{
struct spi_node *spi_device = container_of(device, struct spi_node, dev);
printf("\t spi_device->reg: %#x\n", spi_device->reg);
}
void display_device(struct device *device)
{
printf("\t dev.type: %d\n", device->type);
printf("\t dev.type: %s\n", dev_name[device->type]);
printf("\t dev.name: %s\n", device->name);
}
void display_list(struct list_head *list_head)
{
int i = 0;
struct list_head *p;
struct device *entry;
printf("-------list---------\n");
list_for_each(p, list_head)
{
printf("node[%d]\n", i++);
entry = list_entry(p, struct device, list);
switch (entry->type)
{
case I2C_TYPE:
display_i2c_device(entry);
break;
case SPI_TYPE:
display_spi_device(entry);
break;
default:
printf("unknown device type!\n");
break;
}
display_device(entry);
}
printf("-------end----------\n");
}
void i2c_register_device(struct device*dev)
{
struct i2c_node *i2c_device = container_of(dev, struct i2c_node, dev);
i2c_device->dev.type = I2C_TYPE;
strcpy(i2c_device->dev.name, "yikoulinux");
list_add(&dev->list, &device_list);
}
void spi_register_device(struct device*dev)
{
struct spi_node *spi_device = container_of(dev, struct spi_node, dev);
spi_device->dev.type = SPI_TYPE;
strcpy(spi_device->dev.name, "yikoupeng");
list_add(&dev->list, &device_list);
}
void i2c_unregister_device(struct device *dev)
{
struct i2c_node *i2c_device = container_of(dev, struct i2c_node, dev);
list_del(&dev->list);
}
void spi_unregister_device(struct device *dev)
{
struct spi_node *spi_device = container_of(dev, struct spi_node, dev);
list_del(&dev->list);
}
int main(void)
{
struct i2c_node dev1;
struct spi_node dev2;
INIT_LIST_HEAD(&device_list);
dev1.data = 1;
dev1.reg = 0x40009000;
i2c_register_device(&dev1.dev);
dev2.reg = 0x40008000;
spi_register_device(&dev2.dev);
display_list(&device_list);
i2c_unregister_device(&dev1.dev);
spi_unregister_device(&dev2.dev);
display_list(&device_list);
return 0;
}
#endif