libiberty, gcc: Add memrchr to libiberty and use it [PR119283].

This adds an implementation of memrchr to libiberty and arranges
to configure gcc to use it, if the host does not have it.

	PR cobol/119283

gcc/ChangeLog:

	* config.in: Regenerate.
	* configure: Regenerate.
	* configure.ac: Check for host memrchr.

include/ChangeLog:

	* libiberty.h (memrchr): New.

libiberty/ChangeLog:

	* Makefile.in: Add memrchr build rules.
	* config.in: Regenerate.
	* configure: Regenerate.
	* configure.ac: Check for memrchr.
	* functions.texi: Document memrchr.
	* memrchr.c: New file.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
This commit is contained in:
Iain Sandoe
2025-03-15 22:30:20 +00:00
parent eb2182479f
commit 9018336252
10 changed files with 87 additions and 10 deletions

View File

@@ -139,8 +139,8 @@ CFILES = alloca.c argv.c asprintf.c atexit.c \
ldirname.c \
lrealpath.c \
make-relative-prefix.c \
make-temp-file.c md5.c memchr.c memcmp.c memcpy.c memmem.c \
memmove.c mempcpy.c memset.c mkstemps.c \
make-temp-file.c md5.c memchr.c memrchr.c memcmp.c memcpy.c \
memmem.c memmove.c mempcpy.c memset.c mkstemps.c \
objalloc.c obstack.c \
partition.c pexecute.c \
pex-common.c pex-djgpp.c pex-msdos.c pex-one.c \
@@ -213,8 +213,8 @@ CONFIGURED_OFILES = ./asprintf.$(objext) ./atexit.$(objext) \
./getcwd.$(objext) ./getpagesize.$(objext) \
./gettimeofday.$(objext) \
./index.$(objext) ./insque.$(objext) \
./memchr.$(objext) ./memcmp.$(objext) ./memcpy.$(objext) \
./memmem.$(objext) ./memmove.$(objext) \
./memchr.$(objext) ./memrchr.$(objext) ./memcmp.$(objext) \
./memcpy.$(objext) ./memmem.$(objext) ./memmove.$(objext) \
./mempcpy.$(objext) ./memset.$(objext) ./mkstemps.$(objext) \
./pex-djgpp.$(objext) ./pex-msdos.$(objext) \
./pex-unix.$(objext) ./pex-win32.$(objext) \
@@ -1025,6 +1025,15 @@ $(CONFIGURED_OFILES): stamp-picdir stamp-noasandir
else true; fi
$(COMPILE.c) $(srcdir)/memchr.c $(OUTPUT_OPTION)
./memrchr.$(objext): $(srcdir)/memrchr.c $(INCDIR)/ansidecl.h
if [ x"$(PICFLAG)" != x ]; then \
$(COMPILE.c) $(PICFLAG) $(srcdir)/memrchr.c -o pic/$@; \
else true; fi
if [ x"$(NOASANFLAG)" != x ]; then \
$(COMPILE.c) $(PICFLAG) $(NOASANFLAG) $(srcdir)/memrchr.c -o noasan/$@; \
else true; fi
$(COMPILE.c) $(srcdir)/memrchr.c $(OUTPUT_OPTION)
./memcmp.$(objext): $(srcdir)/memcmp.c $(INCDIR)/ansidecl.h
if [ x"$(PICFLAG)" != x ]; then \
$(COMPILE.c) $(PICFLAG) $(srcdir)/memcmp.c -o pic/$@; \

View File

@@ -186,6 +186,9 @@
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the `memrchr' function. */
#undef HAVE_MEMRCHR
/* Define to 1 if you have the `memset' function. */
#undef HAVE_MEMSET

5
libiberty/configure vendored
View File

@@ -6210,6 +6210,7 @@ funcs="$funcs gettimeofday"
funcs="$funcs index"
funcs="$funcs insque"
funcs="$funcs memchr"
funcs="$funcs memrchr"
funcs="$funcs memcmp"
funcs="$funcs memcpy"
funcs="$funcs memmem"
@@ -6276,7 +6277,7 @@ if test "x" = "y"; then
ffs __fsetlocking \
getcwd getpagesize getrlimit getrusage getsysinfo gettimeofday \
index insque \
memchr memcmp memcpy memmem memmove memset mkstemps \
memchr memrchr memcmp memcpy memmem memmove memset mkstemps \
on_exit \
pipe2 posix_spawn posix_spawnp psignal \
pstat_getdynamic pstat_getstatic putenv \
@@ -6691,7 +6692,7 @@ esac
for f in atexit basename bcmp bcopy bsearch bzero calloc clock ffs \
getcwd getpagesize getrusage gettimeofday \
index insque memchr memcmp memcpy memmove memset psignal \
index insque memchr memrchr memcmp memcpy memmove memset psignal \
putenv random rename rindex sbrk setenv stpcpy strcasecmp \
strchr strdup strerror strncasecmp strrchr strstr strtod \
strtol strtoul sysconf times tmpnam vfprintf vprintf \

View File

@@ -370,6 +370,7 @@ funcs="$funcs gettimeofday"
funcs="$funcs index"
funcs="$funcs insque"
funcs="$funcs memchr"
funcs="$funcs memrchr"
funcs="$funcs memcmp"
funcs="$funcs memcpy"
funcs="$funcs memmem"
@@ -436,7 +437,7 @@ if test "x" = "y"; then
ffs __fsetlocking \
getcwd getpagesize getrlimit getrusage getsysinfo gettimeofday \
index insque \
memchr memcmp memcpy memmem memmove memset mkstemps \
memchr memrchr memcmp memcpy memmem memmove memset mkstemps \
on_exit \
pipe2 posix_spawn posix_spawnp psignal \
pstat_getdynamic pstat_getstatic putenv \
@@ -555,7 +556,7 @@ if test -n "${with_target_subdir}"; then
for f in atexit basename bcmp bcopy bsearch bzero calloc clock ffs \
getcwd getpagesize getrusage gettimeofday \
index insque memchr memcmp memcpy memmove memset psignal \
index insque memchr memrchr memcmp memcpy memmove memset psignal \
putenv random rename rindex sbrk setenv stpcpy strcasecmp \
strchr strdup strerror strncasecmp strrchr strstr strtod \
strtol strtoul sysconf times tmpnam vfprintf vprintf \

View File

@@ -749,6 +749,20 @@ returned.
@end deftypefn
@c memrchr.c:3
@deftypefn Supplemental void* memrchr (const void *@var{s}, int @var{c}, @
size_t @var{n})
This function searches memory for the character @var{c} in reverse order,
starting at @code{*@var{s}+@var{n}-1} . The search only ends with
the first occurrence of @var{c}, or when the start us reached; in particular,
a null character does not terminate the search. If the character @var{c} is
found within @var{length} characters of @code{*@var{s}}, a pointer
to the character is returned. If @var{c} is not found, then @code{NULL} is
returned.
@end deftypefn
@c memcmp.c:6
@deftypefn Supplemental int memcmp (const void *@var{x}, const void *@var{y}, @
size_t @var{count})

33
libiberty/memrchr.c Normal file
View File

@@ -0,0 +1,33 @@
/*
@deftypefn Supplemental void* memrchr (const void *@var{s}, int @var{c}, @
size_t @var{n})
This function searches memory for the character @var{c} in reverse order,
starting at @code{*@var{s}+@var{n}-1} . The search only ends with
the first occurrence of @var{c}, or when the start us reached; in particular,
a null character does not terminate the search. If the character @var{c} is
found within @var{length} characters of @code{*@var{s}}, a pointer
to the character is returned. If @var{c} is not found, then @code{NULL} is
returned.
@end deftypefn
*/
#include <ansidecl.h>
#include <stddef.h>
void *
memrchr (const void *src_void, int c, size_t length)
{
if (length == 0)
return NULL;
const unsigned char *p = (const unsigned char*)src_void;
p += length;
while (*--p != (unsigned char)c)
if (src_void == p)
return NULL;
return (void *)p;
}