[Asterisk-Dev] [RFC] strncpy -> ast_copy_string

Tzafrir Cohen tzafrir.cohen at xorcom.com
Sun May 1 23:57:10 MST 2005


On Sun, May 01, 2005 at 06:31:29PM -0700, Kevin P. Fleming wrote:
> The recent UTF-8 discussion brought to light something that I had not 
> realized before... strncpy() zero-fills the destination buffer, which is 
> waste of time (and cache pollution). In Asterisk, we are nearly always 
> copying strings into buffers that are much larger than required, so this 
> extra zero-writing is just wasted CPU cycles.

There is one popular strncpy alternative that doesn't seem to do this
padding: strlcpy

  http://www.courtesan.com/todd/papers/strlcpy.html

[ snip ]

> I do understand that GCC has a highly-optimized strncpy implementation 
> for each architecture it runs on, and I don't doubt that an open-coded 
> version will not be optimized as well. However, we'd be trading a 
> slightly slower implementation against not writing many, many billions 
> of excess zero bytes over the lifetime of an Asterisk instance.
> 
> Thoughts?

Consider using strlcpy? Maybe it has already been optimized?

-- 
Tzafrir Cohen     icq#16849755  +972-50-7952406
tzafrir.cohen at xorcom.com  http://www.xorcom.com



More information about the asterisk-dev mailing list