diff mbox series

[U-Boot,05/45] sf: Avoid allocating memory on every read operation

Message ID 20181001182249.129565-6-sjg@chromium.org
State Accepted
Commit 97f57109bb750d0b2908d6ecd4aef3801baf1cf2
Delegated to: Simon Glass
Headers show
Series Various fixes and improvements | expand

Commit Message

Simon Glass Oct. 1, 2018, 6:22 p.m. UTC
At present spi_flash_cmd_read_ops() allocates and frees a few bytes of
memory every time it is called. It is faster to use the stack for this
and this is now supported by the minimum GCC version required by U-Boot.

Remove the allocation and use a variable-sized array instead.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/mtd/spi/spi_flash.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

Comments

Simon Glass Oct. 9, 2018, 11:55 p.m. UTC | #1
At present spi_flash_cmd_read_ops() allocates and frees a few bytes of
memory every time it is called. It is faster to use the stack for this
and this is now supported by the minimum GCC version required by U-Boot.

Remove the allocation and use a variable-sized array instead.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/mtd/spi/spi_flash.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

Applied to u-boot-dm
diff mbox series

Patch

diff --git a/drivers/mtd/spi/spi_flash.c b/drivers/mtd/spi/spi_flash.c
index c1591242591..7113c855fe4 100644
--- a/drivers/mtd/spi/spi_flash.c
+++ b/drivers/mtd/spi/spi_flash.c
@@ -468,7 +468,7 @@  int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 		size_t len, void *data)
 {
 	struct spi_slave *spi = flash->spi;
-	u8 *cmd, cmdsz;
+	u8 cmdsz;
 	u32 remain_len, read_len, read_addr;
 	int bank_sel = 0;
 	int ret = -1;
@@ -488,11 +488,7 @@  int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 	}
 
 	cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
-	cmd = calloc(1, cmdsz);
-	if (!cmd) {
-		debug("SF: Failed to allocate cmd\n");
-		return -ENOMEM;
-	}
+	u8 cmd[cmdsz];
 
 	cmd[0] = flash->read_cmd;
 	while (len) {
@@ -535,7 +531,6 @@  int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
 	ret = clean_bar(flash);
 #endif
 
-	free(cmd);
 	return ret;
 }