Reducing the weight of g_return_if_fail()



All the g_return_if_fail() statements tend to increase the size of 
Glib, GTK+, etc, quite a bit.

The attached patch, changes the GCC branch of of the g_return_if_fail()
definition to:

 - Removes the file/line information when we have __PRETTY_FUNC__; it's
   really just useless clutter for the common use of g_return_if_fail()
   to check assertions on entry
   
 - Uses a helper function to avoid hardcoding the format string into
   every function

As an example of the how this helps:

 libgobject gets 10% smaller (saves 20k out of 210k)
 libgtk gets 4% smaller (saves 80k of 2.4M)

Which is a significant saving for such a tiny patch...

Regards,
							Owen

Index: glib/gmessages.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gmessages.c,v
retrieving revision 1.59
diff -u -p -r1.59 gmessages.c
--- glib/gmessages.c	14 Mar 2004 18:59:33 -0000	1.59
+++ glib/gmessages.c	12 Apr 2004 16:08:03 -0000
@@ -542,6 +542,18 @@ g_log (const gchar   *log_domain,
   va_end (args);
 }
 
+void
+g_return_failed_internal (const char *log_domain,
+			  const char *pretty_function,
+			  const char *expression)
+{
+  g_log (log_domain,
+	 G_LOG_LEVEL_CRITICAL,
+	 "%s: assertion `%s' failed",
+	 pretty_function,
+	 expression);
+}
+
 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
 			    (wc == 0x7f) || \
 			    (wc >= 0x80 && wc < 0xa0)))
Index: glib/gmessages.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gmessages.h,v
retrieving revision 1.21
diff -u -p -r1.21 gmessages.h
--- glib/gmessages.h	6 Feb 2003 19:57:14 -0000	1.21
+++ glib/gmessages.h	12 Apr 2004 16:08:03 -0000
@@ -252,29 +252,25 @@ GPrintFunc      g_set_printerr_handler  
 
 #ifdef __GNUC__
 
+void g_return_failed_internal (const char *log_domain,
+			       const char *pretty_function,
+			       const char *expression);
+
 #define g_return_if_fail(expr)		G_STMT_START{			\
      if G_LIKELY(expr) { } else       					\
        {								\
-	 g_log (G_LOG_DOMAIN,						\
-		G_LOG_LEVEL_CRITICAL,					\
-		"file %s: line %d (%s): assertion `%s' failed",		\
-		__FILE__,						\
-		__LINE__,						\
-		__PRETTY_FUNCTION__,					\
-		#expr);							\
+	 g_return_failed_internal (G_LOG_DOMAIN,			\
+		                   __PRETTY_FUNCTION__,		        \
+		                   #expr);				\
 	 return;							\
        };				}G_STMT_END
 
 #define g_return_val_if_fail(expr,val)	G_STMT_START{			\
      if G_LIKELY(expr) { } else						\
        {								\
-	 g_log (G_LOG_DOMAIN,						\
-		G_LOG_LEVEL_CRITICAL,					\
-		"file %s: line %d (%s): assertion `%s' failed",		\
-		__FILE__,						\
-		__LINE__,						\
-		__PRETTY_FUNCTION__,					\
-		#expr);							\
+	 g_return_failed_internal (G_LOG_DOMAIN,			\
+		                   __PRETTY_FUNCTION__,		        \
+		                   #expr);				\
 	 return (val);							\
        };				}G_STMT_END
 

Attachment: signature.asc
Description: This is a digitally signed message part



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