[U-Boot] [PATCH v1] fs: fat: dynamically allocate memory for temporary buffer
Lukasz Majewski
lukma at denx.de
Thu Dec 6 14:42:28 UTC 2018
On Thu, 6 Dec 2018 14:58:52 +0100
Stefan Agner <stefan at agner.ch> wrote:
> From: Stefan Agner <stefan.agner at toradex.com>
>
> Drop the statically allocated get_contents_vfatname_block and
> dynamically allocate a buffer only if required. This saves
> 64KiB of memory.
>
> Signed-off-by: Stefan Agner <stefan.agner at toradex.com>
> ---
>
> fs/fat/fat.c | 20 ++++++++++++++------
> 1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/fs/fat/fat.c b/fs/fat/fat.c
> index 4bc3030ab8..06bcd02835 100644
> --- a/fs/fat/fat.c
> +++ b/fs/fat/fat.c
> @@ -305,9 +305,6 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8
> *buffer, unsigned long size)
> * into 'buffer'.
> * Update the number of bytes read in *gotsize or return -1 on fatal
> errors. */
> -__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
> - __aligned(ARCH_DMA_MINALIGN);
> -
> static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t
> pos, __u8 *buffer, loff_t maxsize, loff_t *gotsize)
> {
> @@ -318,7 +315,7 @@ static int get_contents(fsdata *mydata, dir_entry
> *dentptr, loff_t pos, loff_t actsize;
>
> *gotsize = 0;
> - debug("Filesize: %llu bytes\n", filesize);
> + debug("Filesize: %llu bytes, starting at pos %llu\n",
> filesize, pos);
> if (pos >= filesize) {
> debug("Read position past EOF: %llu\n", pos);
> @@ -350,15 +347,26 @@ static int get_contents(fsdata *mydata,
> dir_entry *dentptr, loff_t pos,
> /* align to beginning of next cluster if any */
> if (pos) {
> + __u8 *tmp_buffer;
> +
> + tmp_buffer = malloc_cache_aligned(MAX_CLUSTSIZE);
> + if (tmp_buffer == NULL) {
> + debug("Error: allocating buffer\n");
> + return -1;
> + }
> +
> actsize = min(filesize, (loff_t)bytesperclust);
> - if (get_cluster(mydata, curclust,
> get_contents_vfatname_block,
> + if (get_cluster(mydata, curclust, tmp_buffer,
> (int)actsize) != 0) {
> printf("Error reading cluster\n");
> + free(tmp_buffer);
> return -1;
> }
> filesize -= actsize;
> actsize -= pos;
> - memcpy(buffer, get_contents_vfatname_block + pos,
> actsize);
> + memcpy(tmp_buffer, buffer + pos, actsize);
> + free(tmp_buffer);
> +
> *gotsize += actsize;
> if (!filesize)
> return 0;
Reviewed-by: Lukasz Majewski <lukma at denx.de>
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181206/7c37379d/attachment.sig>
More information about the U-Boot
mailing list