From: Junio C Hamano Date: Sun, 14 Jun 2020 18:24:10 GMT Subject: Re: [PATCH] pkt-line: extract out PACKET_HEADER_SIZE Message-ID: In-Reply-To: Junio C Hamano writes: >> 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).