【问题标题】:Do you think data structure of tasklet_head is wired?你认为tasklet_head的数据结构是有线的吗?
【发布时间】:2012-11-18 12:47:16
【问题描述】:

我正在阅读tasklet的源代码并尝试理解它。

我认为tasklet_head的数据结构是有线的,你是这样认为的。 为什么第二个元素的数据类型是 struct tasklet_struct ** ,它只会让源代码更复杂,让人困惑,writh? 我的意思是,为什么作者不使用 struct tasklet_struct *tail? 也许作者太聪明了,我无法理解这种简单。 如果是真的,如果你理解了,能否请你指点一下。

398 /*
399  * Tasklets
400  */
401 struct tasklet_head
402 {
403         struct tasklet_struct *head;
404         struct tasklet_struct **tail;
405 };

完整的源代码可以在here.找到

【问题讨论】:

    标签: linux kernel interrupt


    【解决方案1】:

    我会回答手头的编程问题。

    当然 *tail 和 **tail 是完全不同的东西,这就是为什么它是 **tail 而不是 *tail。

    您将持有某种尾引用以避免遍历整个链表以在末尾添加一些东西(对于 O(1) 追加而不是 O(n))。 Tail 可以设置为特定的实例,但这种特定的实施策略是有原因的。而是指向下一个字段,它很简单:

    new_item->next = NULL;
    *tail = new_item;
    tail = &(new_item->next);
    

    希望这会有所帮助。

    【讨论】:

    • 迭代整个链表以在末尾添加一些东西(对于 O(1) 追加而不是 O(n))。 ==>对不起,我不明白为什么我们在末尾添加节点时需要迭代整个链表。我认为复杂性是 O(1) 。如果作者使用 struct tasklet_struct *tail;插入操作可以是这样的: 'if(!tasklet_head->tail) { //插入这个节点 } else {//链表为空,插入修改tasklet_head->head}'
    • @user1296994 抱歉,我的回答措辞不当——我只是为了彻底起见,澄清保留尾部引用的原因。从我的回答来看:“尾巴可能已经设置为一个特定的实例,但是这种特定的实施策略是有原因的。” - 即确切的原因取决于实施者的选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2011-03-31
    • 2016-03-15
    • 2015-08-09
    • 2013-07-30
    • 1970-01-01
    相关资源
    最近更新 更多