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

Re: [PATCH] Use MOVE_ARRAY

From
Jeff King <peff@peff.net>
Date
Jan 22, 2018, 22:44 UTC
Message-ID
<20180122224410.GA21604@sigill.intra.peff.net>
In-Reply-To
<20180122175009.20178-1-szeder.dev@gmail.com>
On Mon, Jan 22, 2018 at 06:50:09PM +0100, SZEDER Gábor wrote:
Show 5 quoted lines
> Use the helper macro MOVE_ARRAY to move arrays.  This is shorter and
> safer, as it automatically infers the size of elements.
> 
> Patch generated by Coccinelle and contrib/coccinelle/array.cocci in
> Travis CI's static analysis build job.
Seems pretty straightforward. One thing I did notice...
Show 13 quoted lines
> diff --git a/cache-tree.c b/cache-tree.c
> index e03e72c34a..18946aa458 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -84,9 +84,8 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
>  	down->namelen = pathlen;
>  
>  	if (pos < it->subtree_nr)
> -		memmove(it->down + pos + 1,
> -			it->down + pos,
> -			sizeof(down) * (it->subtree_nr - pos - 1));
> +		MOVE_ARRAY(it->down + pos + 1, it->down + pos,
> +			   it->subtree_nr - pos - 1);

Most of these are "shift part of the array". I wonder if it would make sense to encapsulate that pattern in a helper, like:

  #define SHIFT_ARRAY(a, nr, pos, slots) \
    MOVE_ARRAY(a + pos + slots, a + pos, nr - pos - slots)
  ...
  SHIFT_ARRAY(it->down, it->subtree_nr, pos, 1);

I'm not sure if that's more readable because it describes a higher-level operation, or if it's less because it adds yet another non-standard helper for the reader to learn.

-Peff
Previous: SZEDER GáborNext: Junio C Hamano
Message 9 of 11 in “describe: use strbuf_add_unique_abbrev() for adding short hashes”
  1. describe: use strbuf_add_unique_abbrev() for adding short hashesRené Scharfe, Jan 15, 2018
  2. Derrick StoleeJan 16, 2018
  3. SZEDER GáborJan 16, 2018
  4. René ScharfeJan 18, 2018
  5. SZEDER GáborJan 18, 2018
  6. Lars SchneiderJan 18, 2018
  7. René ScharfeJan 19, 2018
  8. Use MOVE_ARRAYSZEDER Gábor, Jan 22, 2018
  9. Jeff KingJan 22, 2018
  10. Junio C HamanoJan 22, 2018
  11. Jeff KingJan 22, 2018

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.