git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/2] mem-pool: add mem_pool_strfmt()
Date: Mon, 26 Feb 2024 02:08:44 -0500	[thread overview]
Message-ID: <20240226070844.GB780982@coredump.intra.peff.net> (raw)
In-Reply-To: <20240225113947.89357-2-l.s.r@web.de>

On Sun, Feb 25, 2024 at 12:39:44PM +0100, René Scharfe wrote:

> +static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
> +			      va_list ap)
> +{
> +	struct mp_block *block = pool->mp_block;
> +	char *next_free = block ? block->next_free : NULL;
> +	size_t available = block ? block->end - block->next_free : 0;
> +	va_list cp;
> +	int len, len2;
> +	char *ret;
> +
> +	va_copy(cp, ap);
> +	len = vsnprintf(next_free, available, fmt, cp);
> +	va_end(cp);
> +	if (len < 0)
> +		BUG("your vsnprintf is broken (returned %d)", len);
> +
> +	ret = mem_pool_alloc(pool, len + 1);  /* 1 for NUL */
> +
> +	/* Shortcut; relies on mem_pool_alloc() not touching buffer contents. */
> +	if (ret == next_free)
> +		return ret;
> +
> +	len2 = vsnprintf(ret, len + 1, fmt, ap);
> +	if (len2 != len)
> +		BUG("your vsnprintf is broken (returns inconsistent lengths)");
> +	return ret;
> +}

This is pulling heavily from strbuf_vaddf(). This might be a dumb idea,
but... would it be reasonable to instead push a global flag that causes
xmalloc() to use a memory pool instead of the regular heap?

Then you could do something like:

  push_mem_pool(pool);
  str = xstrfmt("%.*s~%d^%d", ...etc...);
  pop_mem_pool(pool);

It's a little more involved at the caller, but it means that it now
works for all allocations, not just this one string helper.

Obviously you'd want it to be a thread-local value to prevent races. But
I still wonder if it could cause havoc when some sub-function makes an
allocation that the caller does not expect.

-Peff

  reply	other threads:[~2024-02-26  7:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-25 11:39 [PATCH 0/2] name-rev: use memory pool for name strings René Scharfe
2024-02-25 11:39 ` [PATCH 1/2] mem-pool: add mem_pool_strfmt() René Scharfe
2024-02-26  7:08   ` Jeff King [this message]
2024-02-26 18:17     ` René Scharfe
2024-02-27  7:53       ` Jeff King
2024-02-27 17:58         ` René Scharfe
2024-03-07  9:58           ` Jeff King
2024-02-25 11:39 ` [PATCH 2/2] name-rev: use mem_pool_strfmt() René Scharfe
2024-02-26  2:05   ` Junio C Hamano
2024-02-26 18:06     ` René Scharfe

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=20240226070844.GB780982@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.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;
as well as URLs for NNTP newsgroup(s).