git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 14, 2020, 04:42 UTC
Message-ID
<CAPig+cQamHAUc9gV=pRAVYfxJKCwiMj=y6nqoN8w2kg6CtOT9Q@mail.gmail.com>
In-Reply-To
<eae8b2923f7834f3b1d030f6eb4dc093b94da91b.1597374135.git.gitgitgadget@gmail.com>

On Thu, Aug 13, 2020 at 11:02 PM Elijah Newren via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 16 quoted lines
> 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@gmail.com>
> ---
> 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"));
Show 10 quoted lines
> +       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);
> +}
Previous: Elijah Newren via GitGitGadgetNext: Elijah Newren via GitGitGadget
Message 3 of 17 in “Extend and add a little more generalization to the mem_pool API”
  1. 0/3 Extend and add a little more generalization to the mem_pool APIElijah Newren via GitGitGadget, Aug 14, 2020
  2. 1/3 mem-pool: add convenience functions for xstrdup and xstrndupElijah Newren via GitGitGadget, Aug 14, 2020
  3. Eric SunshineAug 14, 2020
  4. 3/3 mem-pool: use consistent pool variable nameElijah Newren via GitGitGadget, Aug 14, 2020
  5. 2/3 mem-pool: use more standard initialization and finalizationElijah Newren via GitGitGadget, Aug 14, 2020
  6. Junio C HamanoAug 14, 2020
  7. Matheus Tavares BernardinoAug 14, 2020
  8. 0/3 Extend and add a little more generalization to the mem_pool APIElijah Newren via GitGitGadget, Aug 14, 2020
  9. 1/3 mem-pool: add convenience functions for xstrdup and xstrndupElijah Newren via GitGitGadget, Aug 14, 2020
  10. René ScharfeAug 14, 2020
  11. 2/3 mem-pool: use more standard initialization and finalizationElijah Newren via GitGitGadget, Aug 14, 2020
  12. 3/3 mem-pool: use consistent pool variable nameElijah Newren via GitGitGadget, Aug 14, 2020
  13. 0/3 Extend and add a little more generalization to the mem_pool APIElijah Newren via GitGitGadget, Aug 15, 2020
  14. 3/3 mem-pool: use consistent pool variable nameElijah Newren via GitGitGadget, Aug 15, 2020
  15. 2/3 mem-pool: use more standard initialization and finalizationElijah Newren via GitGitGadget, Aug 15, 2020
  16. 1/3 mem-pool: add convenience functions for strdup and strndupElijah Newren via GitGitGadget, Aug 15, 2020
  17. Junio C HamanoAug 18, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.