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

Re: [PATCH 2/3] mem-pool: use more standard initialization and finalization

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 14, 2020, 04:38 UTC
Message-ID
<xmqqpn7tzvtf.fsf@gitster.c.googlers.com>
In-Reply-To
<f13a52055cd975d457e0593cbabb70897e78024b.1597374135.git.gitgitgadget@gmail.com>
"Elijah Newren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 9 quoted lines
> written.  mem_pool_init() does essentially the following (simplified
> for purposes of explanation here):
>
>     void mem_pool_init(struct mem_pool **pool...)
>     {
>         *pool = xcalloc(1, sizeof(*pool));
>
> It seems weird to require that mem_pools can only be accessed through a
> pointer.

Yup, if the _init() were to also allocate, I would expect it to be more like

	struct mem_pool *mem_pool_create(...)
	{
		struct mem_pool *pool = xcalloc(1, sizeof(*pool));
		...
		return pool;
	}

It also is OK to let the caller pass uninitialized region of memory, which is how we usually arrange _init() to work. It seems that that is the approach this patch takes.

Show 20 quoted lines
> -void mem_pool_init(struct mem_pool **mem_pool, size_t initial_size)
> +void mem_pool_init(struct mem_pool *mem_pool, size_t initial_size)
>  {
> -	struct mem_pool *pool;
> -
> -	if (*mem_pool)
> -		return;
> -
> -	pool = xcalloc(1, sizeof(*pool));
> -
> -	pool->block_alloc = BLOCK_GROWTH_SIZE;
> +	mem_pool->mp_block = NULL;
> +	mem_pool->pool_alloc = 0;
> +	mem_pool->block_alloc = BLOCK_GROWTH_SIZE;
>  
>  	if (initial_size > 0)
> -		mem_pool_alloc_block(pool, initial_size, NULL);
> -
> -	*mem_pool = pool;
> +		mem_pool_alloc_block(mem_pool, initial_size, NULL);

It used to be that this function both knew and took control of all the bits in *pool memory by using xcalloc(). Any field the function assigned to of course got explicitly the value the function wanted it to have, and all other fields were left to 0.

It may happen to be still the case (i.e. the assignments we see in this function cover all the fields defined), but don't we need some provision to make sure it will hold to be true in the future?

Starting it with "memset(pool, 0, sizeof(*pool)" would be one way.

You'd standardize to s/mem_pool/pool/ in [3/3]; shouldn't this be written with pool to begin with, instead of reintroducing mem_pool that is of different type from the original?

Show 5 quoted lines
> -	if (!*pool_ptr)
> -		mem_pool_init(pool_ptr, 0);
> +	if (!*pool_ptr) {
> +		*pool_ptr = xmalloc(sizeof(**pool_ptr));
> +		mem_pool_init(*pool_ptr, 0);

This one gives an uninitialized chunk of memory to the _init(); an example of the caller that the earlier comment may matter.

Show 10 quoted lines
> +	istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
>  	if (istate->version == 4) {
> -		mem_pool_init(&istate->ce_mem_pool,
> +		mem_pool_init(istate->ce_mem_pool,
>  				estimate_cache_size_from_compressed(istate->cache_nr));
>  	} else {
> -		mem_pool_init(&istate->ce_mem_pool,
> +		mem_pool_init(istate->ce_mem_pool,
>  				estimate_cache_size(mmap_size, istate->cache_nr));
>  	}
Likewise.
Thanks.
Previous: Elijah Newren via GitGitGadgetNext: Matheus Tavares Bernardino
Message 6 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.