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

Re: [PATCH 1/2] mem-pool: add mem_pool_strfmt()

From
René Scharfe <l.s.r@web.de>
Date
Feb 26, 2024, 18:17 UTC
Message-ID
<1868c502-b915-42fd-8cb2-efe90429f9b7@web.de>
In-Reply-To
<20240226070844.GB780982@coredump.intra.peff.net>
Am 26.02.24 um 08:08 schrieb Jeff King:
Show 42 quoted lines
> 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.

That would allow to keep track of allocations that would otherwise leak. We can achieve that more easily by pushing the pointer to a global array and never freeing it. Hmm.

It would not allow the shortcut of using the vast pool as a scratch space to format the string with a single vsnprintf call in most cases. Or am I missing something?

René
Previous: Jeff KingNext: Jeff King
Message 7 of 10 in “name-rev: use memory pool for name strings”
  1. 0/2 name-rev: use memory pool for name stringsRené Scharfe, Feb 25, 2024
  2. 2/2 name-rev: use mem_pool_strfmt()René Scharfe, Feb 25, 2024
  3. Junio C HamanoFeb 26, 2024
  4. René ScharfeFeb 26, 2024
  5. 1/2 mem-pool: add mem_pool_strfmt()René Scharfe, Feb 25, 2024
  6. Jeff KingFeb 26, 2024
  7. René ScharfeFeb 26, 2024
  8. Jeff KingFeb 27, 2024
  9. René ScharfeFeb 27, 2024
  10. Jeff KingMar 7, 2024

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.