20150203 【 内核链表 kernel_list.h 】 list_head 使用

本文介绍了Linux内核中的链表数据结构kernel_list.h,包括其独特之处在于全在头文件中实现。通过一个示例展示了如何使用kernel_list.h创建并操作链表,如生成并打印多个学生信息(姓名、年龄),同时提供了链表查找的实践应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内核链表模板【全部在头文件实现,我是第一次看到这种形式】



#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)
*/
//此文件是从Linux内核(包括/ Linux /列表。H)通过简单的移除列表项的硬件预取改性。在著作权,不论他们属于学分归因于Kulesh Shanmugasundaram*/
/*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
 * container_of铸造结构的成员的容置结构
 * @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 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) );})
/*
 * 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, *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 int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值