Linux内核链表list_head扩展---klist(六)

2014-11-24 08:54:32 ? 作者: ? 浏览: 6
list_next);
366----------------------


总结:


按面向对象的思想


klist是一个链表操作类,klist->k_list是链表入口,get/put是节点操作的方法。


knode是链表节点,knode->n_klist包括节点状态、链表入口, kref节点引用次数, n_node是节点链表入口


klist_iter是迭代器,是作为klist链表中查找特定对象的辅助结构体。查找特定的n_node


----------------------


结合内核驱动模型说明下klist的用法,实现代码在/drivers/base/core.c。


----------------------


klist的应用1178/**
1179 * device_for_each_child - device child iterator.
1180 * @parent: parent struct device.
1181 * @data: data for the callback.
1182 * @fn: function to be called for each device.
1183 *
1184 * Iterate over @parent's child devices, and call @fn for each,
1185 * passing it @data.
1186 *
1187 * We check the return of @fn each time. If it returns anything
1188 * other than 0, we break out and return that value.
1189 */
1190int device_for_each_child(struct device *parent, void *data,
1191 int (*fn)(struct device *dev, void *data))
1192{
1193 struct klist_iter i;
1194 struct device *child;
1195 int error = 0;
1196
1197 if (!parent->p)
1198 return 0;
1199
/* 用链表头初始化迭代器 */
1200 klist_iter_init(&parent->p->klist_children, &i);
/* 使用迭代器的方法查找下一个设备 */
1201 while ((child = next_device(&i)) && !error)
1202 error = fn(child, data);
/* 结束迭代必须调用 */
1203 klist_iter_exit(&i);
1204 return error;
1205}
/*-------------------------------------------------------------------------------*/
/* 内核驱动中用于查找下一设备 */
1122static struct device *next_device(struct klist_iter *i)
1123{
/* 使用迭代查找下一链表节点 */
1124 struct klist_node *n = klist_next(i);
1125 struct device *dev = NULL;
1126 struct device_private *p;
1127
1128 if (n) {
/* 根据节点入口获取该节点上的设备 */
1129 p = to_device_private_parent(n);
1130 dev = p->device;
1131 }
1132 return dev;
1133}
/*-------------------------------------------------------------------------------*/
/* 其中device_private是设备私有数据结构,一下代码不难看出
* 想要由链表节点迭代查找设备非常容易
*/
66/**
67 * struct device_private - structure to hold the private to the driver core portions of the device structure.
68 *
69 * @klist_children - klist containing all children of this device
70 * @knode_parent - node in sibling list
71 * @knode_driver - node in driver list
72 * @knode_bus - node in bus list
73 * @driver_data - private pointer for driver specific info. Will turn into a
74 * list soon.
75 * @device - pointer back to the struct class that this structure is
76 * associated with.
77 *
78 * Nothing outside of the driver core should ever touch these fields.
79 */
80struct device_private {
81 struct klist klist_children;
82 struct klist_node knode_parent;
83 struct klist_node knode_driver;
84 struct klist_node knode_bus;
85 void *driver_data;
86 struct device *device;
87};
88#define to_device_private_parent(obj) \
89 container_of(obj, struct device_private, knode_parent)
90#define to_device_private_driver(obj) \
91 container_of(obj, struct device_private, knode_driver)
92#define to_device_private_bus(obj) \
93 container_of(obj, struct device_private, knode_bus)
94


-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: