【问题标题】:cpumask array defined with size 0定义为大小为 0 的 cpumask 数组
【发布时间】:2015-12-02 04:41:55
【问题描述】:

我正在研究 Linux 内核(特别是负载平衡区域)。 在内核(sched.h)中有一个结构声明:

结构 sched_group

看起来像这样:

struct sched_group {
    struct sched_group *next;   /* Must be a circular list */
    atomic_t ref;

    unsigned int group_weight;
    struct sched_group_power *sgp;

    /*
     * The CPUs this group covers.
     *
     * NOTE: this field is variable length. (Allocated dynamically
     * by attaching extra space to the end of the structure,
     * depending on how many CPUs the kernel has booted up with)
     */
    unsigned long cpumask[0];
};

我不明白的是使用大小为 0 的 cpumask 数组。 任何解释将不胜感激:)

【问题讨论】:

  • 这是通常称为the struct hack 的GCC 变体。 C 标准不包括这种行为。

标签: c linux linux-kernel


【解决方案1】:

cpumask的大小应该根据不同的CPU数量不同的平台而变化,这就是为什么它不能定义一个固定长度的数组。 GNU C 支持a variable-length object,它应该是结构的最后一个元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2022-11-02
    • 2016-05-07
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多