加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么这样的结构体包含两个仅包含一个元素的数组字段?

发布时间:2020-12-16 05:40:58 所属栏目:百科 来源:网络整理
导读:参见英文答案 One element array in struct2 请注意:这个问题不是( One element array in struct)的重复 以下代码摘自Linux内核源码(版本:3.14) struct files_struct{ atomic_t count; struct fdtable __rcu *fdt; struct fdtable fdtab; spinlock_t file_
参见英文答案 > One element array in struct2
请注意:这个问题不是( One element array in struct)的重复

以下代码摘自Linux内核源码(版本:3.14)

struct files_struct
{
    atomic_t count;
    struct fdtable __rcu *fdt;
    struct fdtable fdtab;

    spinlock_t file_lock ____cacheline_aligned_in_smp;
    int next_fd;
    unsigned long close_on_exec_init[1];
    unsigned long open_fds_init[1];
    struct file __rcu * fd_array[NR_OPEN_DEFAULT];
};

我只是想知道为什么close_on_exec_init和open_fds_init被定义为包含一个元素的数组,而不是仅仅被定义为unsigned long close_on_exec_init;和unsigned long open_fds_init ;.

解决方法

这些字段是一个优化,所以Linux不需要为一个不超过BITS_PER_LONG打开的文件描述符的典型进程执行尽可能多的分配.

当分配了file_struct时,close_on_exec_init字段为fdt-> close_on_exec提供初始存储. (请参阅fs / file.c中的dup_fd.)

如果相应的文件描述符设置了“close-on-exec”标志,则设置fdt-> close_on_exec的每个位.因此,Linux只需要为fdt-> close_on_exec分配额外的空间,如果进程具有比无符号长度的位数更多的打开文件描述符.

open_fds_init字段为fdt-> open_fds字段提供相同的功能. fd_array字段为fdt-> fd字段提供相同的功能. (请注意,fd_array的大小为BITS_PER_LONG.)

close_on_exec_init和open_fds_init字段以前的类型为struct embedded_fd_set,但是在this commit中更改为裸阵列.提交消息不能解释为什么作者选择使用单元素数组而不是使用裸标量.也许作者(David Howells)只是想避免使用&操作符.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读