From: "René Scharfe" <l.s.r@web.de>
To: Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org
Cc: Matheus Tavares <matheus.bernardino@usp.br>,
Eric Sunshine <sunshine@sunshineco.com>,
Elijah Newren <newren@gmail.com>
Subject: Re: [PATCH v2 1/3] mem-pool: add convenience functions for xstrdup and xstrndup
Date: Fri, 14 Aug 2020 10:21:22 +0200 [thread overview]
Message-ID: <e7ef099b-5d96-ff67-5d4e-b6fa306adb38@web.de> (raw)
In-Reply-To: <6d679c5b46541208f5fa714174555ff0dc756b9a.1597384820.git.gitgitgadget@gmail.com>
Am 14.08.20 um 08:00 schrieb Elijah Newren via GitGitGadget:
> From: Elijah Newren <newren@gmail.com>
>
> 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>
> ---
> fast-import.c | 12 ++----------
> mem-pool.c | 23 +++++++++++++++++++++++
> mem-pool.h | 6 ++++++
> 3 files changed, 31 insertions(+), 10 deletions(-)
>
> diff --git a/fast-import.c b/fast-import.c
> index ce47794db6..dd5b563950 100644
> --- a/fast-import.c
> +++ b/fast-import.c
> @@ -526,14 +526,6 @@ static unsigned int hc_str(const char *s, size_t len)
> return r;
> }
>
> -static char *pool_strdup(const char *s)
> -{
> - size_t len = strlen(s) + 1;
> - char *r = mem_pool_alloc(&fi_mem_pool, len);
> - memcpy(r, s, len);
> - return r;
> -}
Note: No "x" in the name and it doesn't handle mem_pool_alloc()
returning NULL.
> -
> static void insert_mark(struct mark_set *s, uintmax_t idnum, struct object_entry *oe)
> {
> while ((idnum >> s->shift) >= 1024) {
> @@ -615,7 +607,7 @@ static struct branch *new_branch(const char *name)
> die("Branch name doesn't conform to GIT standards: %s", name);
>
> b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
> - b->name = pool_strdup(name);
> + b->name = mem_pool_xstrdup(&fi_mem_pool, name);
> b->table_next_branch = branch_table[hc];
> b->branch_tree.versions[0].mode = S_IFDIR;
> b->branch_tree.versions[1].mode = S_IFDIR;
> @@ -2806,7 +2798,7 @@ static void parse_new_tag(const char *arg)
>
> t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
> memset(t, 0, sizeof(struct tag));
> - t->name = pool_strdup(arg);
> + t->name = mem_pool_xstrdup(&fi_mem_pool, arg);
> if (last_tag)
> last_tag->next_tag = t;
> else
> diff --git a/mem-pool.c b/mem-pool.c
> index a2841a4a9a..33fda1c411 100644
> --- 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)
> return r;
> }
>
> +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(_("mem_pool_xstrdup: out of memory"));
Can mem_pool_alloc() actually return NULL? It will rather die because
it uses xmalloc(), right? So that check is unnecessary.
And since "mem_pool_" already implies that these functions won't return
if an allocation fails, no extra "x" is needed in their name.
> +
> + 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);
Hmm, this would be our first caller of strnlen(). wrapper.c::xstrndup()
uses memchr() instead. It was added in 2008, and strnlen() is in
POSIX.1-2008, so back then it made sense. Perhaps there are still
systems out there without one?
> + char *ret = mem_pool_alloc(pool, minlen+1);
> +
> + if (!ret)
> + die(_("mem_pool_xstrndup: out of memory"));
The same comments as on mem_pool_xstrdup() apply here.
> +
> + ret[minlen] = '\0';
> + return memcpy(ret, str, minlen);
> +}
> +
> int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
> {
> struct mp_block *p;
> diff --git a/mem-pool.h b/mem-pool.h
> index 999d3c3a52..fcaa2d462b 100644
> --- a/mem-pool.h
> +++ b/mem-pool.h
> @@ -41,6 +41,12 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len);
> */
> void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
>
> +/*
> + * Allocate memory from the memory pool and copy str into it.
> + */
> +char *mem_pool_xstrdup(struct mem_pool *pool, const char *str);
> +char *mem_pool_xstrndup(struct mem_pool *pool, const char *str, size_t len);
> +
> /*
> * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
> * pool will be empty and not contain any memory. It still needs to be free'd
>
next prev parent reply other threads:[~2020-08-14 8:21 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-14 3:02 [PATCH 0/3] Extend and add a little more generalization to the mem_pool API Elijah Newren via GitGitGadget
2020-08-14 3:02 ` [PATCH 1/3] mem-pool: add convenience functions for xstrdup and xstrndup Elijah Newren via GitGitGadget
2020-08-14 4:42 ` Eric Sunshine
2020-08-14 3:02 ` [PATCH 2/3] mem-pool: use more standard initialization and finalization Elijah Newren via GitGitGadget
2020-08-14 4:38 ` Junio C Hamano
2020-08-14 3:02 ` [PATCH 3/3] mem-pool: use consistent pool variable name Elijah Newren via GitGitGadget
2020-08-14 3:51 ` [PATCH 0/3] Extend and add a little more generalization to the mem_pool API Matheus Tavares Bernardino
2020-08-14 6:00 ` [PATCH v2 " Elijah Newren via GitGitGadget
2020-08-14 6:00 ` [PATCH v2 1/3] mem-pool: add convenience functions for xstrdup and xstrndup Elijah Newren via GitGitGadget
2020-08-14 8:21 ` René Scharfe [this message]
2020-08-14 6:00 ` [PATCH v2 2/3] mem-pool: use more standard initialization and finalization Elijah Newren via GitGitGadget
2020-08-14 6:00 ` [PATCH v2 3/3] mem-pool: use consistent pool variable name Elijah Newren via GitGitGadget
2020-08-15 17:37 ` [PATCH v3 0/3] Extend and add a little more generalization to the mem_pool API Elijah Newren via GitGitGadget
2020-08-15 17:37 ` [PATCH v3 1/3] mem-pool: add convenience functions for strdup and strndup Elijah Newren via GitGitGadget
2020-08-15 17:37 ` [PATCH v3 2/3] mem-pool: use more standard initialization and finalization Elijah Newren via GitGitGadget
2020-08-15 17:37 ` [PATCH v3 3/3] mem-pool: use consistent pool variable name Elijah Newren via GitGitGadget
2020-08-18 19:27 ` [PATCH v3 0/3] Extend and add a little more generalization to the mem_pool API Junio C Hamano
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e7ef099b-5d96-ff67-5d4e-b6fa306adb38@web.de \
--to=l.s.r@web.de \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=matheus.bernardino@usp.br \
--cc=newren@gmail.com \
--cc=sunshine@sunshineco.com \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).