public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] memchr.3: Deprecate rawmemchr(3)
@ 2023-01-05 19:02 Alejandro Colomar
  2023-01-06 12:57 ` Wilco Dijkstra
  0 siblings, 1 reply; 2+ messages in thread
From: Alejandro Colomar @ 2023-01-05 19:02 UTC (permalink / raw)
  To: linux-man; +Cc: Alejandro Colomar, libc-alpha, Wilco Dijkstra

It is not optimized, and it calls either strlen(3) or memchr(3), so the
caller can do it directly, and it will be better.

Suggested-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 man3/memchr.3 | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/man3/memchr.3 b/man3/memchr.3
index 68873964e..e03001bec 100644
--- a/man3/memchr.3
+++ b/man3/memchr.3
@@ -22,7 +22,8 @@ .SH SYNOPSIS
 .PP
 .BI "void *memchr(const void " s [. n "], int " c ", size_t " n );
 .BI "void *memrchr(const void " s [. n "], int " c ", size_t " n );
-.BI "void *rawmemchr(const void " s [. n "], int " c );
+.PP
+.BI "[[deprecated]] void *rawmemchr(const void " s [. n "], int " c );
 .fi
 .PP
 .RS -4
@@ -66,26 +67,21 @@ .SH DESCRIPTION
 The
 .BR rawmemchr ()
 function is similar to
-.BR memchr ():
-it assumes (i.e., the programmer knows for certain)
+.BR memchr (),
+but it assumes
+(i.e., the programmer knows for certain)
 that an instance of
 .I c
 lies somewhere in the memory area starting at the location pointed to by
-.IR s ,
-and so performs an optimized search for
-.I c
-(i.e., no use of a count argument to limit the range of the search).
+.IR s .
 If an instance of
 .I c
-is not found, the results are unpredictable.
-The following call is a fast means of locating a string's
-terminating null byte:
-.PP
-.in +4n
-.EX
-char *p = rawmemchr(s,\ \(aq\e0\(aq);
-.EE
-.in
+is not found, the behavior is undefined.
+Use either
+.BR strlen (3)
+or
+.BR memchr (3)
+instead.
 .SH RETURN VALUE
 The
 .BR memchr ()
-- 
2.39.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] memchr.3: Deprecate rawmemchr(3)
  2023-01-05 19:02 [PATCH] memchr.3: Deprecate rawmemchr(3) Alejandro Colomar
@ 2023-01-06 12:57 ` Wilco Dijkstra
  0 siblings, 0 replies; 2+ messages in thread
From: Wilco Dijkstra @ 2023-01-06 12:57 UTC (permalink / raw)
  To: Alejandro Colomar, linux-man; +Cc: Alejandro Colomar, libc-alpha

Hi Alex,

> It is not optimized, and it calls either strlen(3) or memchr(3), so the
> caller can do it directly, and it will be better.
>
> Suggested-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
> Signed-off-by: Alejandro Colomar <alx@kernel.org>

This looks good to me. Btw in the codesearch there are about 800
uses, the majority looks like prototypes etc, so there are few actual
uses. Interestingly GLIBC still contains this:

#ifdef _LIBC
      p = __rawmemchr (p, '\0');
#else
      p = strchr (p, '\0');
#endif

The strchr (p, 0) is optimized by compilers into strlen (since that's
obviously the right optimization) so adding rawmemchr was not
only wasted effort, but it made things slower as well. So we should
remove these uses from GLIBC.

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-06 12:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-05 19:02 [PATCH] memchr.3: Deprecate rawmemchr(3) Alejandro Colomar
2023-01-06 12:57 ` Wilco Dijkstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).