This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[RFA] lmemmem.patch
- From: dje at google dot com (Doug Evans)
- To: gcc-patches at gcc dot gnu dot org
- Date: Mon, 24 Mar 2008 10:29:36 -0700 (PDT)
- Subject: [RFA] lmemmem.patch
Hi. This patch adds a wrapper around memmem().
It's used in a forthcoming patch to gdb.
2008-03-24 Doug Evans <dje@google.com>
* include/libiberty.h: (lmemmem): Declare.
* libiberty/Makefile.in (CFILES): Add lmemmem.c.
(REQUIRED_OFILES): Add lmemmem.o.
(lmemmem.o): New rule.
* libiberty/configure.ac (funcs): Add memmem.
(AC_CHECK_FUNCS): Add memmem.
* libiberty/config.in: Regenerate.
* libiberty/configure: Regenerate.
* libiberty/lmemmem.c: New file.
Index: include/libiberty.h
===================================================================
RCS file: /cvs/src/src/include/libiberty.h,v
retrieving revision 1.57
diff -u -p -u -p -r1.57 libiberty.h
--- ./include/libiberty.h 6 Sep 2007 17:22:36 -0000 1.57
+++ ./include/libiberty.h 24 Mar 2008 16:34:12 -0000
@@ -1,6 +1,6 @@
/* Function declarations for libiberty.
- Copyright 2001, 2002, 2005, 2007 Free Software Foundation, Inc.
+ Copyright 2001, 2002, 2005, 2007, 2008 Free Software Foundation, Inc.
Note - certain prototypes declared in this header file are for
functions whoes implementation copyright does not belong to the
@@ -166,6 +166,10 @@ extern char *libiberty_concat_ptr;
(libiberty_concat_ptr = (char *) alloca (concat_length ACONCAT_PARAMS + 1), \
concat_copy2 ACONCAT_PARAMS)
+/* A well-defined memmem () that is always compiled in. */
+
+extern void * lmemmem (const void *, size_t, const void *, size_t);
+
/* Check whether two file descriptors refer to the same file. */
extern int fdmatch (int fd1, int fd2);
Index: libiberty/Makefile.in
===================================================================
RCS file: /cvs/src/src/libiberty/Makefile.in,v
retrieving revision 1.89
diff -u -p -u -p -r1.89 Makefile.in
--- ./libiberty/Makefile.in 25 Jul 2007 06:36:27 -0000 1.89
+++ ./libiberty/Makefile.in 24 Mar 2008 16:34:13 -0000
@@ -2,7 +2,7 @@
# Originally written by K. Richard Pixley <rich@cygnus.com>.
#
# Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-# 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
+# 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
# Foundation
#
# This file is part of the libiberty library.
@@ -133,6 +133,7 @@ CFILES = alloca.c argv.c asprintf.c atex
hashtab.c hex.c \
index.c insque.c \
lbasename.c \
+ lmemmem.c \
lrealpath.c \
make-relative-prefix.c \
make-temp-file.c md5.c memchr.c memcmp.c memcpy.c memmove.c \
@@ -164,7 +165,7 @@ REQUIRED_OFILES = ./regex.o ./cplus-dem.
./fnmatch.o ./fopen_unlocked.o \
./getopt.o ./getopt1.o ./getpwd.o ./getruntime.o \
./hashtab.o ./hex.o \
- ./lbasename.o ./lrealpath.o \
+ ./lbasename.o ./lmemmem.o ./lrealpath.o \
./make-relative-prefix.o ./make-temp-file.o \
./objalloc.o ./obstack.o \
./partition.o ./pexecute.o ./physmem.o \
@@ -748,6 +749,12 @@ $(CONFIGURED_OFILES): stamp-picdir
else true; fi
$(COMPILE.c) $(srcdir)/lbasename.c $(OUTPUT_OPTION)
+./lmemmem.o: $(srcdir)/lmemmem.c stamp-h
+ if [ x"$(PICFLAG)" != x ]; then \
+ $(COMPILE.c) $(PICFLAG) $(srcdir)/lmemmem.c -o pic/$@; \
+ else true; fi
+ $(COMPILE.c) $(srcdir)/lmemmem.c $(OUTPUT_OPTION)
+
./lrealpath.o: $(srcdir)/lrealpath.c stamp-h $(INCDIR)/ansidecl.h \
$(INCDIR)/libiberty.h
if [ x"$(PICFLAG)" != x ]; then \
Index: libiberty/config.in
===================================================================
RCS file: /cvs/src/src/libiberty/config.in,v
retrieving revision 1.38
diff -u -p -u -p -r1.38 config.in
--- ./libiberty/config.in 22 Jul 2005 03:16:32 -0000 1.38
+++ ./libiberty/config.in 24 Mar 2008 16:34:13 -0000
@@ -139,6 +139,9 @@
/* Define to 1 if you have the `memcpy' function. */
#undef HAVE_MEMCPY
+/* Define to 1 if you have the `memmem' function. */
+#undef HAVE_MEMMEM
+
/* Define to 1 if you have the `memmove' function. */
#undef HAVE_MEMMOVE
Index: libiberty/configure
===================================================================
RCS file: /cvs/src/src/libiberty/configure,v
retrieving revision 1.89
diff -u -p -u -p -r1.89 configure
--- ./libiberty/configure 17 Jul 2007 18:05:02 -0000 1.89
+++ ./libiberty/configure 24 Mar 2008 16:34:13 -0000
@@ -5062,6 +5062,7 @@ vars="sys_errlist sys_nerr sys_siglist"
checkfuncs="getrusage on_exit psignal strerror strsignal sysconf times sbrk gettimeofday"
checkfuncs="$checkfuncs realpath canonicalize_file_name pstat_getstatic pstat_getdynamic sysmp"
checkfuncs="$checkfuncs getsysinfo table sysctl wait3 wait4 __fsetlocking"
+checkfuncs="$checkfuncs memmem"
# These are neither executed nor required, but they help keep
# autoheader happy without adding a bunch of text to acconfig.h.
Index: libiberty/configure.ac
===================================================================
RCS file: /cvs/src/src/libiberty/configure.ac,v
retrieving revision 1.37
diff -u -p -u -p -r1.37 configure.ac
--- ./libiberty/configure.ac 17 Jul 2007 18:05:02 -0000 1.37
+++ ./libiberty/configure.ac 24 Mar 2008 16:34:13 -0000
@@ -356,6 +356,7 @@ vars="sys_errlist sys_nerr sys_siglist"
checkfuncs="getrusage on_exit psignal strerror strsignal sysconf times sbrk gettimeofday"
checkfuncs="$checkfuncs realpath canonicalize_file_name pstat_getstatic pstat_getdynamic sysmp"
checkfuncs="$checkfuncs getsysinfo table sysctl wait3 wait4 __fsetlocking"
+checkfuncs="$checkfuncs memmem"
# These are neither executed nor required, but they help keep
# autoheader happy without adding a bunch of text to acconfig.h.
Index: libiberty/lmemmem.c
===================================================================
RCS file: libiberty/lmemmem.c
diff -N libiberty/lmemmem.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ./libiberty/lmemmem.c 24 Mar 2008 16:34:13 -0000
@@ -0,0 +1,58 @@
+/* lmemmem -- search for a sequence of bytes
+ This function is in the public domain. */
+
+/*
+
+@deftypefn Supplemental void *lmemmem (const void *@var{haystack}, size_t @var{haystacklen},
+ const void *@var{needle}, size_t @var{needlelen})
+
+Search the area of memory at @var{haystack} for the bytes at @var{needle},
+and return the first occurrence.
+Returns a pointer to the beginning of the string or NULL if not found.
+
+@end deftypefn
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#ifdef HAVE_STRING_H
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE /* memmem */
+#endif
+#include <string.h>
+#endif
+#include <sys/types.h> /* size_t */
+#include "libiberty.h"
+
+void*
+lmemmem (const void *haystack, size_t haystacklen,
+ const void *needle, size_t needlelen)
+{
+#ifdef HAVE_MEMMEM
+ return memmem (haystack, haystacklen, needle, needlelen);
+#else
+ size_t i,j;
+ const char *h = (const char *) haystack;
+ const char *n = (const char *) needle;
+
+ if (needlelen > haystacklen)
+ return NULL;
+ if (needlelen == 0)
+ return (void *) haystack; /* this is what glibc memmem does */
+
+ for (i = 0; i <= haystacklen - needlelen; ++i)
+ {
+ for (j = 0; j < needlelen; ++j)
+ {
+ if (h[i + j] != n[j])
+ break;
+ }
+ if (j == needlelen)
+ return (void*) (h + i);
+ }
+
+ return NULL;
+#endif
+}