记录一个linux内核链表使用示例

56 篇文章 2 订阅

1. 链表文件:

#ifndef __ULIST_H__
#define __ULIST_H__

/*
 * 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)

#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;
}

/**
 * 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.
 */
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.
 */
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)
{
	if (next)
		next->prev = prev;
	if (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.
 */
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
 */
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
 */
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.
 */
#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_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	-	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_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))

/**
 * list_for_each_entry_continue -       iterate over list of given type
 *                      continuing after existing point
 * @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_continue(pos, head, member)			\
	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head);					\
	     pos = list_entry(pos->member.next, typeof(*pos), member))


#endif

2. 使用示例 :

/*************************************************************************
	> File Name: test.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Thu 29 Aug 2019 03:36:06 PM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "klist.h"

///< 图片类型, 一张图片由很多片段(slice)链式存储在内存中
struct image {
	int nr;
	struct list_head slice_list; ///< 所有的片段都链接在以该链表对象为头节点的链表上, 嵌入容器image中
};

///< 片段类型, 代表一张图片在内存中某个片段
struct slice {
	unsigned long start;
	unsigned long end;
	char name[10];
	struct list_head node; ///< 表征一个链表节点对象, 嵌入容器slice中
};

int main()
{
	struct image *pimage = malloc(sizeof(*pimage));
	struct slice *stemp = NULL, *snext = NULL;
	struct list_head* ltemp = NULL, *lnext = NULL;

	INIT_LIST_HEAD(&pimage->slice_list); ///< 初始化链表头
	pimage->nr = 0;

	for (int i = 0; i < 6; i++) {
		stemp = malloc(sizeof(*stemp)); ///< 构造片段对象
		stemp->start = i;
		stemp->end = i + 10;
		snprintf(stemp->name, 9, "slice[%d]", i);

		list_add(&stemp->node, &pimage->slice_list); ///< 添加片段对象到图片slice_list链表中
		pimage->nr++;
	}

	printf("1. iterate list:");
	///< 迭代图片slice_list链表, 获取它每个元素的容器对象(片段)的地址
	list_for_each_entry(stemp, &pimage->slice_list, node) { 
		printf(" %s", stemp->name);
	}
	printf("\n");

	///< 获取slice_list链表的第一个元素的容器对象的地址
	stemp = list_entry(pimage->slice_list.next, struct slice, node);
	printf("2. first entry: %s\n", stemp->name);

	///< 获取slice_list链表的第二个元素的容器对象的地址
	stemp = list_entry(pimage->slice_list.next->next, struct slice, node);
	printf("3. next extry: %s\n", stemp->name);

	///< 迭代图片slist_list链表, 获取它的每个元素的容器对象的地址
	list_for_each_entry_safe(stemp, snext, &pimage->slice_list, node) {
		if (!strcmp("slice[2]", stemp->name)) {
			printf("4. remove entry: %s\n", stemp->name);
			list_del_init(&stemp->node);  ///< 删除满足条件的元素
			free(stemp); 
		}
	}

	printf("5. iterate list:");
	///< 迭代slice_list链表, 获取它的每个元素的地址
	list_for_each(ltemp, &pimage->slice_list) {
		stemp = list_entry(ltemp, struct slice, node); ///< 根据元素的地址, 获得元素对应容器的地址
		printf(" %s", stemp->name);
	}
	printf("\n");

	printf("6. remove all entry:");
	///< 迭代slice_list链表, 获取它的每个元素的地址
	list_for_each(ltemp, &pimage->slice_list) {
	list_for_each_safe(ltemp, lnext, &pimage->slice_list) {
		stemp = list_entry(ltemp, struct slice, node); ///< 根据元素的地址, 获得元素对应容器的地址
		printf(" %s", stemp->name);
		list_del_init(&stemp->node);  ///< 删除一个元素
		free(stemp);
	}
	printf("\n");

	printf("7. test list is empty: %s\n", list_empty(&pimage->slice_list) ? "YES" : "NO"); ///< 判断链表是否为空

	free(pimage);

	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值