Skip to contentSkip to conversation

memchr() vs. memscan()

DISCUSSION Andy Shevchenko 2 participants 2 messages Feb 2, 2023 – Feb 3, 2023

lore
Share & tools
lore
#0 lore raw

Why do we have memchr() and memscan() implementations in lib/string.c? As far as I can see the one may be derived from the other easily.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#1 lore raw
Quoted 2 lines · Andy Shevchenko

Why do we have memchr() and memscan() implementations in lib/string.c? As far as I can see the one may be derived from the other easily.

Well, memchr() is the C standard thing, and the one arches are more likely to (and do) provide custom implementations of. memscan() seems to be to memchr() as strchrnul() is to strchr(), and judging by the number of callers, I don't think any optimizations or a separate C implementation is really worth it. So yes, by all means we can replace its entire body by

return memchr(addr, c, size) ?: addr + size;

That's possibly even a win for the arches that do provide their own memchr(), but again, performance of this one doesn't matter at all. But the tiny kernel folks might appreciate this.

Rasmus