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

Re: [PATCH 2/2] replace-object.c: remove the_repository from prepare_replace_object

From
Junio C Hamano <gitster@pobox.com>
Date
May 10, 2018, 10:23 UTC
Message-ID
<xmqqtvrfizce.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20180509234059.52156-2-sbeller@google.com>
Stefan Beller <sbeller@google.com> writes:
Show 7 quoted lines
> This was missed in 5982da9d2ce (replace-object: allow
> prepare_replace_object to handle arbitrary repositories, 2018-04-11)
>
> Technically the code works correctly as the replace_map is the same
> size in different repositories, however it is hard to read. So convert
> the code to the familiar pattern of dereferencing the pointer that we
> assign in the sizeof itself.
;-)
We say
	ptr = xmalloc(sizeof(*ptr))
is better because 
	ptr = xmalloc(sizeof(typeof(*ptr)))

is easy to go stale unless we actually use typeof and instead say a concrete type like "struct oidmap".

This one was doing
	ptr = xmalloc(sizeof(*another_ptr))

and it was OK because ptr and another_ptr happened to be of the same type. I wonder if we are making it safer, or making it more obscure to seasoned C programmers, if we introduced a pair of helper macros, perhaps like these:

	#define ALLOCATE(ptr) (ptr) = xmalloc(sizeof(*(ptr)))
	#define CALLOCATE(ptr,cnt) (ptr) = xcalloc((cnt), sizeof(*(ptr)))
The change looks obviously good.  Will queue.
Thanks.
Previous: Stefan BellerNext: Jeff King
Message 3 of 8 in “object.c: free replace map in raw_object_store_clear”
  1. 1/2 object.c: free replace map in raw_object_store_clearStefan Beller, May 9, 2018
  2. 2/2 replace-object.c: remove the_repository from prepare_replace_objectStefan Beller, May 9, 2018
  3. Junio C HamanoMay 10, 2018
  4. Jeff KingMay 10, 2018
  5. Derrick StoleeMay 10, 2018
  6. Stefan BellerMay 10, 2018
  7. Junio C HamanoMay 10, 2018
  8. Stefan BellerMay 10, 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.