[systemd-devel] [PATCH v2] core: avoid needless strdup by working with strings directly

Lennart Poettering lennart at poettering.net
Tue Aug 21 08:09:15 PDT 2012


On Thu, 16.08.12 10:23, Shawn Landden (shawnlandden at gmail.com) wrote:

> config_parse_join_controllers() not converted cause that is a const
> char *

Umm, so I am not really a fan of changing strings and then back. I mean,
sure in this case this would work. But there are so many other cases
where it doesn't, that I don't even want to start with this style
here. If people see this in the sources, they might blindly think that
this is OK everywhere else, too, but it isn't. For example, if this was
argv[] we are talking of then patching it would be immediately visible
outside of the process in "ps". This would also break in threaded
situations. And it would also break if the string passed is const and in
a RO section in the binary.

This is usually not an inner loop that is executed more than a few
times, so maybe this is not the place to optimize anyway?

That said, an acceptable solution would be to allocate this dynamically
on the stack. Since this is executed in a loop alloca() would not work,
but C99 variable size arrays would. This would get rid of the heap
allocation, and replace it with a cheap stack allocation, but would
still require copying memory.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list