w1: simplify allocation of w1_master
PATCH 1/1 w1: simplify allocation of w1_master
Convert bus_master to a flexible array member as it's being used as one with the old C89 + 1 pointer trick.
--- drivers/w1/w1_int.c | 4 +--- include/linux/w1.h | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-)
drivers/w1/w1_int.c +1 -3
@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,/* * We are in process context(kernel thread), so can sleep. */ - dev =kzalloc (sizeof (struct w1_master ) +sizeof (struct w1_bus_master ),GFP_KERNEL ); + dev =kzalloc_flex (*dev, bus_master,1 );if (!dev)return NULL ; - dev->bus_master = (struct w1_bus_master *)(dev +1 ); - dev->owner =THIS_MODULE ; dev->max_slave_count = slave_count; dev->slave_count =0 ;
include/linux/w1.h +2 -2
@@ -236,9 +236,9 @@ struct w1_master {struct device_driver *driver;struct device dev; -struct w1_bus_master *bus_master; -u32 seq; + +struct w1_bus_master bus_master[]; };int w1_add_master_device (struct w1_bus_master *master);
--
2.53.0
Krzysztof Kozlowski
maintainerQuoted 32 lines · Rosen Penev
Convert
bus_masterto a flexible array member as it's being used as one with the old C89 + 1 pointer trick.Signed-off-by Rosen Penev <[email protected]>--- drivers/w1/w1_int.c | 4 +--- include/linux/w1.h | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-)drivers/w1/w1_int.c +1
-3@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,/* * We are in process context(kernel thread), so can sleep. */ - dev =kzalloc (sizeof (struct w1_master ) +sizeof (struct w1_bus_master ),GFP_KERNEL ); + dev =kzalloc_flex (*dev, bus_master,1 );if (!dev)return NULL ; - dev->bus_master = (struct w1_bus_master *)(dev +1 ); - dev->owner =THIS_MODULE ; dev->max_slave_count = slave_count; dev->slave_count =0 ;include/linux/w1.h +0
-0
include/linux/w1.h struct w1_master { · line 241
Quoted 8 lines
include/linux/w1.h +2
-2@@ -236,9 +236,9 @@ struct w1_master {struct device_driver *driver;struct device dev; -struct w1_bus_master *bus_master; -u32 seq; + +struct w1_bus_master bus_master[];
Code does not use it as flexible array member, does it? Why this cannot be just two kzalloc's - one for w1_master and then next for bus_master? Or embed it directly (composition) in w1_master?
Best regards,
Krzysztof
Quoted 37 lines · Krzysztof Kozlowski
Quoted 35 lines · Rosen Penev
Convert
bus_masterto a flexible array member as it's being used as one with the old C89 + 1 pointer trick.Signed-off-by Rosen Penev <[email protected]>--- drivers/w1/w1_int.c | 4 +--- include/linux/w1.h | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-)drivers/w1/w1_int.c +1
-3@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,/* * We are in process context(kernel thread), so can sleep. */ - dev =kzalloc (sizeof (struct w1_master ) +sizeof (struct w1_bus_master ),GFP_KERNEL ); + dev =kzalloc_flex (*dev, bus_master,1 );if (!dev)return NULL ; - dev->bus_master = (struct w1_bus_master *)(dev +1 ); - dev->owner =THIS_MODULE ; dev->max_slave_count = slave_count; dev->slave_count =0 ;include/linux/w1.h +0
-0@@ -236,9 +236,9 @@ struct w1_master {struct device_driver *driver;struct device dev;
Quoted 9 lines
include/linux/w1.h struct w1_master { · line 241
Quoted 5 lines
include/linux/w1.h +2
-2-struct w1_bus_master *bus_master; -u32 seq; + +struct w1_bus_master bus_master[];Code does not use it as flexible array member, does it? Why this cannot be just two kzalloc's - one for
w1_masterand then next forbus_master? Or embed it directly (composition) inw1_master?
two kzalloc or kzalloc + kcalloc just does two allocations needlessly.
It can be embedded, yes.
Quoted 3 lines
Best regards,
Krzysztof
Krzysztof Kozlowski
maintainerQuoted 47 lines · Rosen Penev
Quoted 37 lines · Krzysztof Kozlowski
Quoted 35 lines · Rosen Penev
Convert
bus_masterto a flexible array member as it's being used as one with the old C89 + 1 pointer trick.Signed-off-by Rosen Penev <[email protected]>--- drivers/w1/w1_int.c | 4 +--- include/linux/w1.h | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-)drivers/w1/w1_int.c +1
-3@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,/* * We are in process context(kernel thread), so can sleep. */ - dev =kzalloc (sizeof (struct w1_master ) +sizeof (struct w1_bus_master ),GFP_KERNEL ); + dev =kzalloc_flex (*dev, bus_master,1 );if (!dev)return NULL ; - dev->bus_master = (struct w1_bus_master *)(dev +1 ); - dev->owner =THIS_MODULE ; dev->max_slave_count = slave_count; dev->slave_count =0 ;include/linux/w1.h +0
-0@@ -236,9 +236,9 @@ struct w1_master {struct device_driver *driver;struct device dev;Quoted 9 lines
include/linux/w1.h struct w1_master { · line 241
Quoted 5 lines
include/linux/w1.h +2
-2-struct w1_bus_master *bus_master; -u32 seq; + +struct w1_bus_master bus_master[];Code does not use it as flexible array member, does it? Why this cannot be just two kzalloc's - one for
w1_masterand then next forbus_master? Or embed it directly (composition) inw1_master?
Quoted 1 line
two kzalloc or kzalloc + kcalloc just does two allocations needlessly.
Needless is marking something as flexible array while it is not such. You write readable and obvious code, not something to optimize number of allocs.
Quoted 5 lines
It can be embedded, yes.
Quoted 3 lines
Best regards,
Krzysztof
Best regards,
Krzysztof