Thread (1 message) 1 message, 1 author, 2020-06-14

Re: [PATCH] pkt-line: extract out PACKET_HEADER_SIZE

From: Junio C Hamano <hidden>
Date: 2020-06-14 18:24:19

Junio C Hamano [off-list ref] writes:
quoted
How about (this ugly code):

	packet_trace("0000", sizeof "0000" - 1, 1);
	if (write_in_full(fd, "0000", sizeof "0000" - 1) < 0)
Yeah, that is ugly.  I was thinking more in the direction of
replacing these three-argument write_in_full with something like

#define write_constant(fd, constant_string) \
	write_in_full((fd), (constant_string), strlen(constant_string))

with some preprocessor magic to make the compilation break when the
second parameter to the macro is not a string constant.
There is a bit of subtlety but I did mean C preprocessor macro and
not a helper function with the above.  With use of a macro defined
like above, the programmer can write

	write_constant(fd, "0000");

which would turn into

	write_in_full((fd), "0000", strlen("0000"));
	
Descent compilers know to produce identical code as

	write_in_full((fd), "0000", 4);

when seeing a literal constant string given to strlen().

But a helper function like this:

	static int write_constant(int fd, const char *string) {
		return write_in_full(fd, string, strlen(string));
	}

has less chance of getting the same kind of optimization (the helper
needs to be inlined before the compiler can realize that the
parameter to strlen() is a literal constant whose length can be
computed at the compile time).

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help