Skip to contentSkip to conversation

gpio: altera-a10sr: constify gpio_chip structure

PATCH In review Gustavo A. R. Silva 3 participants 4 messages Jul 11, 2017 – Aug 2, 2017 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: altera-a10sr: constify gpio_chip structure

This structure is only used to copy into another structure, so declare it as const.

This issue was detected using Coccinelle and the following semantic patch:

@r disable optional_qualifier@
identifier i;
position p;
@@
static struct gpio_chip i@p = { ... };

@ok@
identifier r.i;
expression e;
position p;
@@
e = i@p;

@bad@
position p != {r.p,ok.p};
identifier r.i;
struct gpio_chip e;
@@
e@i@p

@depends on !bad disable optional_qualifier@
identifier r.i;
@@
static
+const
struct gpio_chip i = { ... };

In the following log you can see a significant difference in the code size and data segment, hence in the dec segment. This log is the output of the size command, before and after the code change:

before:
text data bss dec hex filename
1954 600 0 2554 9fa drivers/gpio/gpio-altera-a10sr.o

after:
text data bss dec hex filename
1655 512 0 2167 877 drivers/gpio/gpio-altera-a10sr.o

---
 drivers/gpio/gpio-altera-a10sr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-altera-a10sr.c +1 -1
index 16a8951..6b11f13 100644
@@ -71,7 +71,7 @@ static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc,
 	return -EINVAL;
 }
 
-static struct gpio_chip altr_a10sr_gc = {
+static const struct gpio_chip altr_a10sr_gc = {
 	.label = "altr_a10sr_gpio",
 	.owner = THIS_MODULE,
 	.get = altr_a10sr_gpio_get,
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Gustavo A. R. Silva

This structure is only used to copy into another structure, so declare it as const.

<snip>

Quoted 11 lines
---
  drivers/gpio/gpio-altera-a10sr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-altera-a10sr.c +0 -0
index 16a8951..6b11f13 100644

drivers/gpio/gpio-altera-a10sr.c static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc, · line 77

Quoted 8 lines
drivers/gpio/gpio-altera-a10sr.c +1 -1
@@ -71,7 +71,7 @@ static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc,
  	return -EINVAL;
  }
 
-static struct gpio_chip altr_a10sr_gc = {
+static const struct gpio_chip altr_a10sr_gc = {
  	.label = "altr_a10sr_gpio",
  	.owner = THIS_MODULE,
  	.get = altr_a10sr_gpio_get,
#2 lore raw
Quoted 11 lines · Thor Thayer
Quoted 2 lines · Gustavo A. R. Silva

This structure is only used to copy into another structure, so declare it as const.

<snip>

Quoted 5 lines
---
  drivers/gpio/gpio-altera-a10sr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 6 lines
drivers/gpio/gpio-altera-a10sr.c +0 -0
index 16a8951..6b11f13 100644

drivers/gpio/gpio-altera-a10sr.c static int altr_a10sr_gpio_direction_output(struct · line 77

Quoted 8 lines
drivers/gpio/gpio-altera-a10sr.c +1 -0
@@ -71,7 +71,7 @@ static int altr_a10sr_gpio_direction_output(struct 
 gpio_chip *gc,
      return -EINVAL;
  }
  -static struct gpio_chip altr_a10sr_gc = {
+static const struct gpio_chip altr_a10sr_gc = {
      .label = "altr_a10sr_gpio",
      .owner = THIS_MODULE,
      .get = altr_a10sr_gpio_get,

Thank you, Thor.

Signature · 1 line
Gustavo A. R. Silva
#3 lore raw
In reply to Gustavo A. R. Silva
Quoted 40 lines · Gustavo A. R. Silva

This structure is only used to copy into another structure, so declare it as const.

This issue was detected using Coccinelle and the following semantic patch:

@r disable optional_qualifier@
identifier i;
position p;
@@
static struct gpio_chip i@p = { ... };

@ok@
identifier r.i;
expression e;
position p;
@@
e = i@p;

@bad@
position p != {r.p,ok.p};
identifier r.i;
struct gpio_chip e;
@@
e@i@p

@depends on !bad disable optional_qualifier@
identifier r.i;
@@
static
+const
struct gpio_chip i = { ... };

In the following log you can see a significant difference in the code size and data segment, hence in the dec segment. This log is the output of the size command, before and after the code change:

before:
text data bss dec hex filename
1954 600 0 2554 9fa drivers/gpio/gpio-altera-a10sr.o

Quoted 5 lines

after:
text data bss dec hex filename
1655 512 0 2167 877 drivers/gpio/gpio-altera-a10sr.o

Patch applied with Thor's review tag.

Yours,
Linus Walleij