public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: Simon Glass <sjg@chromium.org>,
	U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Bin Meng <bmeng.cn@gmail.com>, Tom Rini <trini@konsulko.com>,
	Alexandru Gagniuc <mr.nuke.me@gmail.com>
Subject: Re: [PATCH v3 01/15] lib: Add memdup()
Date: Sun, 1 Aug 2021 23:25:17 +0200	[thread overview]
Message-ID: <bcabb520-7541-400a-9185-b5a80a335ff4@gmx.de> (raw)
In-Reply-To: <20210801150024.v3.1.I1d417387eb1e7273b536017f4a8920fc4e2369a9@changeid>



On 8/1/21 11:00 PM, Simon Glass wrote:
> Add a function to duplicate a memory region, a little like strdup().
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (no changes since v2)
>
> Changes in v2:
> - Add a patch to introduce a memdup() function
>
>   include/linux/string.h | 13 +++++++++++++
>   lib/string.c           | 13 +++++++++++++
>   test/lib/string.c      | 32 ++++++++++++++++++++++++++++++++
>   3 files changed, 58 insertions(+)
>
> diff --git a/include/linux/string.h b/include/linux/string.h
> index dd255f21633..3169c93796e 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -129,6 +129,19 @@ extern void * memchr(const void *,int,__kernel_size_t);
>   void *memchr_inv(const void *, int, size_t);
>   #endif
>
> +/**
> + * memdup() - allocate a buffer and copy in the contents

This partially duplicates realloc(). The only difference is that
realloc() frees the old buffer. But it seems rEALLOc() has too much
logic to easily join the two functions.

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

> + *
> + * Note that this returns a valid pointer even if @len is 0
> + *
> + * @src: data to copy in
> + * @len: number of bytes to copy
> + * @return allocated buffer with the copied contents, or NULL if not enough
> + *	memory is available
> + *
> + */
> +char *memdup(const void *src, size_t len);
> +
>   unsigned long ustrtoul(const char *cp, char **endp, unsigned int base);
>   unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base);
>
> diff --git a/lib/string.c b/lib/string.c
> index ba176fb08f7..78bd65c4136 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -659,6 +659,19 @@ void * memscan(void * addr, int c, size_t size)
>   }
>   #endif
>
> +char *memdup(const void *src, size_t len)
> +{
> +	char *p;
> +
> +	p = malloc(len);
> +	if (!p)
> +		return NULL;
> +
> +	memcpy(p, src, len);
> +
> +	return p;
> +}
> +
>   #ifndef __HAVE_ARCH_STRSTR
>   /**
>    * strstr - Find the first substring in a %NUL terminated string
> diff --git a/test/lib/string.c b/test/lib/string.c
> index 64234bef36c..5dcf4d6db00 100644
> --- a/test/lib/string.c
> +++ b/test/lib/string.c
> @@ -23,6 +23,8 @@
>   /* Allow for copying up to 32 bytes */
>   #define BUFLEN (SWEEP + 33)
>
> +#define TEST_STR	"hello"
> +
>   /**
>    * init_buffer() - initialize buffer
>    *
> @@ -193,3 +195,33 @@ static int lib_memmove(struct unit_test_state *uts)
>   }
>
>   LIB_TEST(lib_memmove, 0);
> +
> +/** lib_memdup() - unit test for memdup() */
> +static int lib_memdup(struct unit_test_state *uts)
> +{
> +	char buf[BUFLEN];
> +	size_t len;
> +	char *p, *q;
> +
> +	/* Zero size should do nothing */
> +	p = memdup(NULL, 0);
> +	ut_assertnonnull(p);
> +	free(p);
> +
> +	p = memdup(buf, 0);
> +	ut_assertnonnull(p);
> +	free(p);
> +
> +	strcpy(buf, TEST_STR);
> +	len = sizeof(TEST_STR);
> +	p = memdup(buf, len);
> +	ut_asserteq_mem(p, buf, len);
> +
> +	q = memdup(p, len);
> +	ut_asserteq_mem(q, buf, len);
> +	free(q);
> +	free(p);
> +
> +	return 0;
> +}
> +LIB_TEST(lib_memdup, 0);
>

  reply	other threads:[~2021-08-01 21:25 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-01 21:00 [PATCH v3 00/15] image: A partial series for the image clean-up Simon Glass
2021-08-01 21:00 ` [PATCH v3 01/15] lib: Add memdup() Simon Glass
2021-08-01 21:25   ` Heinrich Schuchardt [this message]
2021-08-01 21:00 ` [PATCH v3 02/15] Add support for an owned buffer Simon Glass
2021-08-01 21:00 ` [PATCH v3 03/15] compiler: Add a comment to host_build() Simon Glass
2021-08-01 21:00 ` [PATCH v3 04/15] zstd: Create a function for use from U-Boot Simon Glass
2021-08-01 21:00 ` [PATCH v3 05/15] btrfs: Use U-Boot API for decompression Simon Glass
2021-08-01 21:00 ` [PATCH v3 06/15] image: Avoid switch default in image_decomp() Simon Glass
2021-08-01 21:00 ` [PATCH v3 07/15] image: Update zstd to avoid reporting error twice Simon Glass
2021-08-01 21:00 ` [PATCH v3 08/15] gzip: Avoid use of u64 Simon Glass
2021-08-01 21:00 ` [PATCH v3 09/15] image: Update image_decomp() to avoid ifdefs Simon Glass
2021-08-01 21:00 ` [PATCH v3 10/15] image: Split board code out into its own file Simon Glass
2021-08-01 21:00 ` [PATCH v3 11/15] image: Fix up checkpatch warnings in image-board.c Simon Glass
2021-08-01 21:00 ` [PATCH v3 12/15] image: Split host code out into its own file Simon Glass
2021-08-01 21:00 ` [PATCH v3 13/15] image: Create a function to do manual relocation Simon Glass
2021-08-01 21:00 ` [PATCH v3 14/15] image: Avoid #ifdefs for " Simon Glass
2021-08-01 21:00 ` [PATCH v3 15/15] image: Remove ifdefs around image_setup_linux() el at Simon Glass

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=bcabb520-7541-400a-9185-b5a80a335ff4@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=bmeng.cn@gmail.com \
    --cc=mr.nuke.me@gmail.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --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