Re: [PATCH 1/3] mem-pool: add convenience functions for xstrdup and xstrndup

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Aug 13, 2020 at 11:02 PM Elijah Newren via GitGitGadget
<gitgitgadget@xxxxxxxxx> wrote:
> fast-import had a special mem_pool_xstrdup() convenience function that I
> want to be able to use from the new merge algorithm I am writing.  Move
> it from fast-import to mem-pool, and also add a mem_pool_xstrndup()
> while at it that I also want to use.
>
> Signed-off-by: Elijah Newren <newren@xxxxxxxxx>
> ---
> diff --git a/mem-pool.c b/mem-pool.c
> @@ -102,6 +102,29 @@ void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
> +char *mem_pool_xstrdup(struct mem_pool *pool, const char *str)
> +{
> +       size_t len = strlen(str) + 1;
> +       char *ret = mem_pool_alloc(pool, len);
> +
> +       if (!ret)
> +               die("Out of memory, mem_pool_xstrdup failed");

Nit: Not worth a re-roll, but I was wondering if it would make sense
to allow these to be localized (and follow current convention of not
capitalizing):

    die(_("mem_pool_xstrdup: out of memory"));

> +       return memcpy(ret, str, len);
> +}
> +
> +char *mem_pool_xstrndup(struct mem_pool *pool, const char *str, size_t len)
> +{
> +       size_t minlen = strnlen(str, len);
> +       char *ret = mem_pool_alloc(pool, minlen+1);
> +
> +       if (!ret)
> +               die("Out of memory, mem_pool_xstrndup failed");

Ditto.

> +
> +       ret[minlen] = '\0';
> +       return memcpy(ret, str, minlen);
> +}



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux