Allocate all sub-driver instance data at once. The amount of data that
have to be allocated is known up front, so is the size of the data, so
there is no need to call malloc() in a loop, mallocate all data at once.
The upside is, less heap fragmentation and fewer malloc() calls overall,
and a faster boot time.
The downside is, if some of the clock fail to register, then the clock
driver cannot free parts of the bulk allocated sub-driver instance data.
Such a failure can only occur if clk_register() were to fail, and if that
happens, the system has more significant problems. Worse, if a core clock
driver fails to probe, the system has even bigger problem.
Reviewed-by: Peng Fan <peng.fan@nxp.com>
Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Signed-off-by: Peng Fan <peng.fan@nxp.com>
static int scmi_clk_probe(struct udevice *dev)
{
- struct clk_scmi *clk_scmi;
+ struct clk_scmi *clk_scmi_bulk, *clk_scmi;
struct scmi_clock_priv *priv = dev_get_priv(dev);
size_t num_clocks, i;
int ret;
return ret;
}
+ clk_scmi_bulk = kzalloc(num_clocks * sizeof(*clk_scmi), GFP_KERNEL);
+ if (!clk_scmi_bulk)
+ return -ENOMEM;
+
for (i = 0; i < num_clocks; i++) {
char *clock_name;
u32 attributes;
if (!scmi_clk_get_attibute(dev, i, &clock_name, &attributes)) {
- clk_scmi = kzalloc(sizeof(*clk_scmi), GFP_KERNEL);
- if (!clk_scmi || !clock_name)
+ clk_scmi = clk_scmi_bulk + i;
+ if (!clock_name)
ret = -ENOMEM;
else
ret = clk_register(&clk_scmi->clk, dev->driver->name,
clock_name, dev->name);
if (ret) {
- free(clk_scmi);
free(clock_name);
return ret;
}