[systemd-devel] string constants vs. #defines [was: Re: [PATCH v4] udev hwdb: Support shipping pre-compiled database in system images]

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Sun Oct 26 07:40:18 PDT 2014


On Sun, Oct 26, 2014 at 07:51:13AM -0400, Martin Pitt wrote:
> Hello all,
> 
> Zbigniew Jędrzejewski-Szmek [2014-10-25 21:51 +0200]:
> > Looks fine and push-worthy. Two minor nitpicks below, feel free
> > to fix up before pushing or ignore as you see fit.
> 
> Michael raised the question about the option name. I'll wait another
> day or two for responses (no time today/flight back home until
> tomorrow).
> 
> > > +static const char hwdb_bin_paths[] =
> > > +    "/etc/udev/hwdb.bin\0"
> > > +    UDEVLIBEXECDIR "/hwdb.bin\0";
> > Actually we don't need to define a variable for this, a #define would be enough.
> 
> Are #defines actually preferred in systemd's code style/conventions?
> Personally I prefer "proper" constants, as they are more type safe and
> less magic, and presumably string constants will end up in the same
> place in the ELF anyway? Using #defines potentially duplicates the
> strings too, although I suspect gcc to be clever enough to
> de-duplicate identical string constants in the source. Is there any
> downside to using static constants?
[I *did* start by saying that it's kind of not terribly important, but...]

When compiled without optimization, gcc will create a spot in .symtab
section for the array variable. It increases the size of .rodata by 13
bytes and .strtab also. With optimization on, the result is identical.
I think there's some advantage to having the same code with and without
optimization, the change in size is less important.

Zbyszek


More information about the systemd-devel mailing list