b0207191 发表于 2024-6-29 17:16

请教个linux源码的问题

在看3.x中的sysctl.c这部分代码,有个函数看不懂


/* see if attaching q to p would be an improvement */
static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
{
        struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
        struct ctl_table *next;
        int is_better = 0;
        int not_in_parent = !p->attached_by;

        while ((next = is_branch_in(by, to)) != NULL) {
                if (by == q->attached_by)
                        is_better = 1;
                if (to == p->attached_by)
                        not_in_parent = 1;   《---- 这里为什么不添加个判断 if (is_better && not_in_parent)就break; 提前结束循环
                by = by->child;
                to = next->child;
        }

        if (is_better && not_in_parent) {
                q->attached_by = by;
                q->attached_to = to;
                q->parent = p;
        }
}

另外,网络上关于sysctl这部分代码分析没有看见,有疑问不知道找谁交流,直接问源代码作者吗,

一问十块钱 发表于 2024-6-29 17:46

EraserKing 发表于 2024-6-29 17:51

因为要继续遍历链表到头?下面有赋值

— from Xiaomi 23127PN0CC, Android 14 of S1 Next Goose v2.5.2-play

不要叫水狗做主人 发表于 2024-6-29 17:57

先看最新版本有没有改这边的逻辑吧,说不定别人也觉得可以提前终止循环

精钢魔像 发表于 2024-6-29 18:36

可能是循环里有两层if 不利于优化?

tsubasa9 发表于 2024-6-29 19:06

没看懂你的改法,但 is_better && not_in_parent 显然不是while结束的条件
这里while结束的条件是is_branch_in函数返回值空

posthoc 发表于 2024-6-29 19:54

虽然is_better 和 not_in_parent一旦赋值为1就不会变回去,但是最后的
                q->attached_by = by;
                q->attached_to = to;
用到的是循环到最后的by和to,提前break的话显然语义不同了吧。
页: [1]
查看完整版本: 请教个linux源码的问题