Reputation: 1494
From linux kernel code
struct gpio_desc {
struct gpio_chip *chip;
unsigned long flags;
/* flag symbols are bit numbers */
#define FLAG_REQUESTED 0
#define FLAG_IS_OUT 1
#define FLAG_EXPORT 2 /* protected by sysfs_lock */
#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
#define FLAG_ACTIVE_LOW 6 /* value has active low */
#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
/* Connection label */
const char *label;
/* Name of the GPIO */
const char *name;
};
what is the reason to place defines into the body of structure?
Upvotes: 8
Views: 6264
Reputation: 12731
There is no impact. But in the example, for readability sake they maintained/added near to the variable declared.
They want use those "#define" for the variable "unsigned long flags;".
Below is simple example and proves no impact on the class/struct by declaring the "#Define" inside.
//gcc 4.9.3
#include <stdio.h>
struct gpio_desc {
#define FLAG_REQUESTED 0
} test;
int main()
{
printf("%d",FLAG_REQUESTED);
}
The out put is zero.
Upvotes: 0
Reputation: 2853
With #define
it doesn't matter too much where you put them (so long as it is higher up in the file than where it is first used). Most likely those constants are used only within that structure, so it was put there so logically they would be easier to find. They could have been put anywhere above the first place they were used, but they were grouped together because of similar purpose.
Upvotes: 12
Reputation: 8205
It's meaningless, other than to try and define them close to the point of first use.
By the time the compiler actually sees the code, the preprocessor will have stripped those lines out of it.
In this particular example, they would be better off with a typedef'd enum for the flags and using that enum to declare the field.
Upvotes: 4