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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With