linux内核中Dentry和超级块结构之间的c循环依赖关系



我曾经做过一些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 */
        ...
        ...
};

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

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

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

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

最新更新