Elipsis with array

B

berte

Hello folks,

I investigating kernel sources I coincided with this part:

struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
[0 ... NR_IRQS-1] = {
.status = IRQ_DISABLED,
.chip = &no_irq_chip,
.handle_irq = handle_bad_irq,
.depth = 1,
.lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
#ifdef CONFIG_SMP
.affinity = CPU_MASK_ALL
#endif
}
};

Q1- What is this part "[0 ... NR_IRQS-1]" ?
Q2- Elipsis using with arrays ?

Regards.
 
M

Martien Verbruggen

Hello folks,

I investigating kernel sources I coincided with this part:

struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
[0 ... NR_IRQS-1] = {

That's not standard C, but a gcc extension to initialise a range of
elements to the same value.

Check the gcc manual, under 'C Extensions' - 'Designated Inits'.

Martien
 
J

jacob navia

berte said:
Hello folks,

I investigating kernel sources I coincided with this part:

struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
[0 ... NR_IRQS-1] = {
.status = IRQ_DISABLED,
.chip = &no_irq_chip,
.handle_irq = handle_bad_irq,
.depth = 1,
.lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
#ifdef CONFIG_SMP
.affinity = CPU_MASK_ALL
#endif
}
};

Q1- What is this part "[0 ... NR_IRQS-1]" ?
Q2- Elipsis using with arrays ?

Regards.

This is a gnu extension. In
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC88
you can read:
4.20 Labeled Elements in Initializers

Standard C requires the elements of an initializer to appear in a fixed
order, the same as the order of the elements in the array or structure
being initialized.

In GNU C you can give the elements in any order, specifying the array
indices or structure field names they apply to. This extension is not
implemented in GNU C++.

To specify an array index, write `[index]' or `[index] =' before the
element value. For example,



int a[6] = { [4] 29, [2] = 15 };

is equivalent to



int a[6] = { 0, 0, 15, 0, 29, 0 };

The index values must be constant expressions, even if the array being
initialized is automatic.

To initialize a range of elements to the same value, write `[first ...
last] = value'. For example,



int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };

Note that the length of the array is the highest value specified plus one.

In a structure initializer, specify the name of a field to initialize
with `fieldname:' before the element value. For example, given the
following structure,



struct point { int x, y; };

the following initialization



struct point p = { y: yvalue, x: xvalue };

is equivalent to



struct point p = { xvalue, yvalue };

Another syntax which has the same meaning is `.fieldname ='., as shown here:



struct point p = { .y = yvalue, .x = xvalue };

You can also use an element label (with either the colon syntax or the
period-equal syntax) when initializing a union, to specify which element
of the union should be used. For example,



union foo { int i; double d; };

union foo f = { d: 4 };

will convert 4 to a double to store it in the union using the second
element. By contrast, casting 4 to type union foo would store it into
the union as the integer i, since it is an integer. (See section 4.22
Cast to a Union Type.)

You can combine this technique of naming elements with ordinary C
initialization of successive elements. Each initializer element that
does not have a label applies to the next consecutive element of the
array or structure. For example,



int a[6] = { [1] = v1, v2, [4] = v4 };

is equivalent to



int a[6] = { 0, v1, v2, 0, v4, 0 };

Labeling the elements of an array initializer is especially useful when
the indices are characters or belong to an enum type. For example:



int whitespace[256]
= { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
 
W

Wolfgang Draxinger

berte said:
Hello folks,

I investigating kernel sources I coincided with this part:

struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp =
{ [0 ... NR_IRQS-1] = {
.status = IRQ_DISABLED,
.chip = &no_irq_chip,
.handle_irq = handle_bad_irq,
.depth = 1,
.lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
#ifdef CONFIG_SMP
.affinity = CPU_MASK_ALL
#endif
}
};

Q1- What is this part "[0 ... NR_IRQS-1]" ?
Q2- Elipsis using with arrays ?

Those are GNU extensions to the GCC, to ease the static
initialization of structures. You find them to be used
throughout large parts of the Linux kernel sources and their use
is highly encouraged to enhance readability.

See the documentation here:
http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Designated-Inits.html#Designated-Inits

Wolfgang Draxinger
 
B

berte

Hello folks,
I investigating kernel sources I coincided with this part:
struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
   [0 ... NR_IRQS-1] = {

That's not standard C, but a gcc extension to initialise a range of
elements to the same value.

Check the gcc manual, under 'C Extensions' - 'Designated Inits'.

Martien
--
                        |
Martien Verbruggen      |
                        | The gene pool could use a little chlorine.
                        |

Thank you all. I got it :)

Regards,
berte
 
B

Ben Bacarisse

jacob navia said:
berte said:
I investigating kernel sources I coincided with this part:

struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
[0 ... NR_IRQS-1] = {
.status = IRQ_DISABLED,
.chip = &no_irq_chip,
.handle_irq = handle_bad_irq,
.depth = 1,
.lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
#ifdef CONFIG_SMP
.affinity = CPU_MASK_ALL
#endif
}
};

Q1- What is this part "[0 ... NR_IRQS-1]" ?
Q2- Elipsis using with arrays ?

This is a gnu extension. In
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC88
you can read:
4.20 Labeled Elements in Initializers
<snip quote from old manual>

By quoting such an old manual you obscure what is C99 and what is an
extension. The newer version makes it much clearer.

http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Designated-Inits.html#Designated-Inits
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top