On Mon, Nov 23, 2020 at 04:32:40PM +0000, Barnabás Pőcze wrote:
[...]
+static int get_gpio_pin_state(struct irq_desc *irq_desc) +{
- struct gpio_chip *gc = irq_data_get_irq_chip_data(&irq_desc->irq_data);
- return gc->get(gc, irq_desc->irq_data.hwirq);
+}
[...]
- ssize_t status = get_gpio_pin_state(irq_desc);
`get_gpio_pin_state()` returns an `int`, so I am not sure why `ssize_t` is used here.
I used `ssize_t` because I found gpiolib-sysfs.c uses `ssize_t`
// drivers/gpio/gpiolib-sysfs.c static ssize_t value_show(struct device *dev, struct device_attribute *attr, char *buf) { struct gpiod_data *data = dev_get_drvdata(dev); struct gpio_desc *desc = data->desc; ssize_t status; mutex_lock(&data->mutex); status = gpiod_get_value_cansleep(desc); ... return status; }
According to the book Advanced Programming in the UNIX Environment by W. Richard Stevens, With the 1990 POSIX.1 standard, the primitive system data type ssize_t was introduced to provide the signed return value...
So ssize_t is fairly common, for example, the read and write syscall return a value of type ssize_t. But I haven't found out why ssize_t is better int.
Sorry if I wasn't clear, what prompted me to ask that question is the following: `gc->get()` returns `int`, `get_gpio_pin_state()` returns `int`, yet you still save the return value of `get_gpio_pin_state()` into a variable with type `ssize_t` for no apparent reason. In the example you cited, `ssize_t` is used because the show() callback of a sysfs attribute must return `ssize_t`, but here, `interrupt_line_active()` returns `bool`, so I don't see any advantage over a plain `int`. Anyways, I believe either one is fine, I just found it odd.
I don't understand why "the show() callback of a sysfs attribute must return `ssize_t`" instead of int. Do you think the rationale behind it is the same for this case? If yes, using "ssize_t" for status could be justified. [...]
Because it was decided that way, `ssize_t` is a better choice for that purpose than plain `int`. You can see it in include/linux/device.h, that both the show() and store() methods must return `ssize_t`.
Could you explain why `ssize_t` is a better choice? AFAIU, ssize_t is used because we can return negative value to indicate an error. If we use ssize_t here, it's a reminder that reading a GPIO pin's status could fail. And ssize_t reminds us it's a operation similar to read or write. So ssize_t is better than int here. And maybe it's the same reason why "it was decided that way".
What I'm arguing here, is that there is no reason to use `ssize_t` in this case. Because `get_gpio_pin_state()` returns `int`. So when you do
ssize_t status = get_gpio_pin_state(...);
then the return value of `get_gpio_pin_state()` (which is an `int`), will be converted to an `ssize_t`, and saved into `status`. I'm arguing that that is unnecessary and a plain `int` would work perfectly well in this case. Anyways, both work fine, I just found the unnecessary use of `ssize_t` here odd.
Regards, Barnabás Pőcze
-- Best regards, Coiby
linux-stable-mirror@lists.linaro.org