【问题标题】:circular dependency between Dentry and superblock structure in linux kernelLinux内核中Dentry和超级块结构之间的循环依赖
【发布时间】:2014-05-30 07:40:48
【问题描述】:

我曾经做过一些oo编程。现在我正在阅读 C 中的 linux 内核代码。 我发现:

struct super_block {
        ...
        ...
        unsigned long            s_flags;          /* mount flags */
        unsigned long            s_magic;          /* filesystem's magic number */
        struct dentry            *s_root;          /* directory mount point */
        struct rw_semaphore      s_umount;         /* unmount semaphore */
        ...
        ...
        }

struct dentry {
        ...
        ...
        struct dentry_operations *d_op;        /* dentry operations table */
        struct super_block       *d_sb;        /* superblock of file */
        unsigned int             d_flags;      /* dentry flags */
        int                      d_mounted;    /* is this a mount point? */
        void                     *d_fsdata;    /* filesystem-specific data */
        ...
        ...
};

我们可以看到 super_block 结构体有一个 struct dentry 属性,而 struct dentry 有一个 super_block 属性。它会导致循环依赖吗?非常感谢

如果是,内存管理是如何工作的?例如,如果一个 dentry 对象被删除,super_block 将指向一个无效的位置。我的意思是如何管理他们的生命周期。

【问题讨论】:

  • 数据结构包含循环没有问题。只需要知道哪个部分拥有,哪个部分只是引用。如果这不明确,可能会变得很困难。

标签: c linux kernel superblock


【解决方案1】:

首先,两个结构之间存在循环依赖,但是-

当有前向声明时(例如 struct dentry; 在结构 super_block 的定义之前,反之亦然),这没有问题,因为两个结构都使用指向其他结构的指针,并且指针的大小无论如何都是已知的。使用每个结构的字段都需要事先定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2020-12-20
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多