(patch) strdupa for GLib - 2nd try



Apologies if you guys are seeing this twice.

The following patch adds strdupa support to GLib - duplicate a string on
the stack.  As Ulrich Drepper noted yesterday, alloca is many, many times
faster than g_malloc/g_free.

I will commit unless there are objections.

	Jeff



Index: configure.in
===================================================================
RCS file: /debian/home/gnomecvs/glib/configure.in,v
retrieving revision 1.51
diff -u -r1.51 configure.in
--- configure.in	1998/12/26 03:58:06	1.51
+++ configure.in	1998/12/30 17:01:05
@@ -245,9 +245,12 @@
 AC_CHECK_HEADERS(sys/times.h, AC_DEFINE(HAVE_SYS_TIMES_H))
 AC_CHECK_HEADERS(unistd.h, AC_DEFINE(HAVE_UNISTD_H))
 AC_CHECK_HEADERS(values.h, AC_DEFINE(HAVE_VALUES_H))
+AC_CHECK_HEADER(alloca.h, glib_have_alloca_h=yes, glib_have_alloca_h=no)
+AC_CHECK_HEADER(string.h, glib_have_string_h=yes, glib_have_string_h=no)
 
 # Check for some functions
 AC_CHECK_FUNCS(lstat strerror strsignal memmove vsnprintf strcasecmp strncasecmp poll)
+AC_CHECK_FUNC(alloca, glib_have_alloca=yes, glib_have_alloca=no)
 
 # Check for sys_errlist
 AC_MSG_CHECKING(for sys_errlist)
@@ -870,6 +873,8 @@
 
 $glib_vacopy
 
+$glib_alloca
+
 #ifdef	__cplusplus
 #define	G_HAVE_INLINE	1
 #else	/* !__cplusplus */
@@ -1127,6 +1132,19 @@
 if test x$glib_working_wctype = xno; then
   glib_wc="\$glib_wc
 #define G_HAVE_BROKEN_WCTYPE 1"
+fi
+
+if test x$glib_have_alloca = xyes; then
+  glib_alloca='
+#define G_HAVE_ALLOCA 1'
+fi
+if test x$glib_have_alloca_h = xyes; then
+  glib_alloca='
+#define G_HAVE_ALLOCA_H 1'
+fi
+if test x$glib_have_string_h = xyes; then
+  glib_alloca='
+#define G_HAVE_STRING_H 1'
 fi
 
 case x$enable_threads in
Index: glib.h
===================================================================
RCS file: /debian/home/gnomecvs/glib/glib.h,v
retrieving revision 1.98
diff -u -r1.98 glib.h
--- glib.h	1998/12/21 21:42:59	1.98
+++ glib.h	1998/12/30 17:01:06
@@ -67,6 +67,14 @@
 #include "dmalloc.h"
 #endif
 
+/* include alloca.h for g_strdupa, string.h for g_strndupa */
+#if G_HAVE_ALLOCA_H
+#  include <alloca.h>
+#endif
+#if G_HAVE_STRING_H
+#  include <string.h>
+#endif
+
 
 #ifdef NATIVE_WIN32
 
@@ -1454,6 +1462,33 @@
 gchar*	 g_strescape		(gchar	      *string);
 gpointer g_memdup		(gconstpointer mem,
 				 guint	       byte_size);
+
+/* stack-based string duplication routines */
+#if G_HAVE_ALLOCA
+#  ifdef strdupa
+#    define g_strdupa		strdupa
+#  else
+#    define g_strdupa(s)	G_STMT_START { \
+				  const char *__old = (s); \
+				  size_t __len = strlen (__old) + 1; \
+				  char *__new = alloca (__len); \
+				  memcpy (__new, __old, __len); \
+				} G_STMT_END
+#  endif
+
+#  ifdef strndupa
+#    define g_strndupa		strndupa
+#  else
+#    define g_strndupa(s,n)	G_STMT_START { \
+				  const char *__old = (s); \
+				  size_t __len = strnlen (__old, (n)); \
+				  char *__new = alloca (__len + 1); \
+				  __new[__len] = '\0'; \
+				  memcpy (__new, __old, __len); \
+				} G_STMT_END
+#  endif
+#endif
+
 
 /* NULL terminated string arrays.
  * g_strsplit() splits up string into max_tokens tokens at delim and


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]