public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Stefan Agner <stefan@agner.ch>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v1] fs: fat: dynamically allocate memory for temporary buffer
Date: Thu, 06 Dec 2018 16:46:33 +0100	[thread overview]
Message-ID: <e6bbf417e0a968f7ccb094c8cd8da756@agner.ch> (raw)
In-Reply-To: <20181206135853.9891-1-stefan@agner.ch>

On 06.12.2018 14:58, Stefan Agner wrote:
> From: Stefan Agner <stefan.agner@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@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);

I somehow managed to mix up copy direction here. Will send a v2.

--
Stefan

> +		free(tmp_buffer);
> +
>  		*gotsize += actsize;
>  		if (!filesize)
>  			return 0;

      parent reply	other threads:[~2018-12-06 15:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-06 13:58 [U-Boot] [PATCH v1] fs: fat: dynamically allocate memory for temporary buffer Stefan Agner
2018-12-06 14:42 ` Lukasz Majewski
2018-12-06 15:13 ` Tom Rini
2018-12-06 15:45   ` Stefan Agner
2018-12-06 16:06     ` Tom Rini
2018-12-06 15:46 ` Stefan Agner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e6bbf417e0a968f7ccb094c8cd8da756@agner.ch \
    --to=stefan@agner.ch \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox