Skip to contentSkip to conversation

rtc: armada38x: zalloc + calloc to single allocation

PATCH Applied Rosen Penev 3 participants 4 messages Mar 4 – Mar 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtc: armada38x: zalloc + calloc to single allocation

Use a flexible array member to simplify allocation.

---
 drivers/rtc/rtc-armada38x.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/rtc/rtc-armada38x.c +2 -7
index 713fa0d077cd..245290ae1a8d 100644
@@ -72,8 +72,8 @@ struct armada38x_rtc {
 	spinlock_t	    lock;
 	int		    irq;
 	bool		    initialized;
-	struct value_to_freq *val_to_freq;
 	const struct armada38x_rtc_data *data;
+	struct value_to_freq val_to_freq[];
 };
 
 #define ALARM1	0
@@ -490,18 +490,13 @@ static __init int armada38x_rtc_probe(struct platform_device *pdev)
 {
 	struct armada38x_rtc *rtc;
 
-	rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
+	rtc = devm_kzalloc(&pdev->dev, struct_size(rtc, val_to_freq, SAMPLE_NR),
 			    GFP_KERNEL);
 	if (!rtc)
 		return -ENOMEM;
 
 	rtc->data = of_device_get_match_data(&pdev->dev);
 
-	rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
-				sizeof(struct value_to_freq), GFP_KERNEL);
-	if (!rtc->val_to_freq)
-		return -ENOMEM;
-
 	spin_lock_init(&rtc->lock);
 
 	rtc->regs = devm_platform_ioremap_resource_byname(pdev, "rtc");
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Rosen Penev

Use a flexible array member to simplify allocation.

I must admit that I didn't know struct_size(), it is a nice helper!

Thanks,

Gregory

Quoted 42 lines
---
 drivers/rtc/rtc-armada38x.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/rtc/rtc-armada38x.c +2 -7
index 713fa0d077cd..245290ae1a8d 100644
@@ -72,8 +72,8 @@ struct armada38x_rtc {
 	spinlock_t	    lock;
 	int		    irq;
 	bool		    initialized;
-	struct value_to_freq *val_to_freq;
 	const struct armada38x_rtc_data *data;
+	struct value_to_freq val_to_freq[];
 };
 
 #define ALARM1	0
@@ -490,18 +490,13 @@ static __init int armada38x_rtc_probe(struct platform_device *pdev)
 {
 	struct armada38x_rtc *rtc;
 
-	rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
+	rtc = devm_kzalloc(&pdev->dev, struct_size(rtc, val_to_freq, SAMPLE_NR),
 			    GFP_KERNEL);
 	if (!rtc)
 		return -ENOMEM;
 
 	rtc->data = of_device_get_match_data(&pdev->dev);
 
-	rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
-				sizeof(struct value_to_freq), GFP_KERNEL);
-	if (!rtc->val_to_freq)
-		return -ENOMEM;
-
 	spin_lock_init(&rtc->lock);
 
 	rtc->regs = devm_platform_ioremap_resource_byname(pdev, "rtc");
Signature · 1 line
2.53.0
Signature · 3 lines
Grégory CLEMENT, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw
Quoted 7 lines · Gregory CLEMENT
Quoted 2 lines · Rosen Penev

Use a flexible array member to simplify allocation.

I must admit that I didn't know struct_size(), it is a nice helper!

There's kzalloc_flex that's even nicer. Has no devm version yet.

Quoted 53 lines

Thanks,

Gregory

Quoted 40 lines
---
 drivers/rtc/rtc-armada38x.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/rtc/rtc-armada38x.c +2 -8
index 713fa0d077cd..245290ae1a8d 100644
@@ -72,8 +72,8 @@ struct armada38x_rtc {
      spinlock_t          lock;
      int                 irq;
      bool                initialized;
-     struct value_to_freq *val_to_freq;
      const struct armada38x_rtc_data *data;
+     struct value_to_freq val_to_freq[];
 };
 
 #define ALARM1       0
@@ -490,18 +490,13 @@ static __init int armada38x_rtc_probe(struct platform_device *pdev)
 {
      struct armada38x_rtc *rtc;
 
-     rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
+     rtc = devm_kzalloc(&pdev->dev, struct_size(rtc, val_to_freq, SAMPLE_NR),
                          GFP_KERNEL);
      if (!rtc)
              return -ENOMEM;
 
      rtc->data = of_device_get_match_data(&pdev->dev);
 
-     rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
-                             sizeof(struct value_to_freq), GFP_KERNEL);
-     if (!rtc->val_to_freq)
-             return -ENOMEM;
-
      spin_lock_init(&rtc->lock);
 
      rtc->regs = devm_platform_ioremap_resource_byname(pdev, "rtc");
--
Quoted 2 lines

2.53.0

--
Grégory CLEMENT, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com