[glib] gsocket: add a wrapper around g_set_error() to avoid extra work



commit 1ab3e3ed3e0c50cc8e747a9617f9412af8a15bdd
Author: Dan Winship <danw gnome org>
Date:   Tue Aug 25 10:18:06 2015 -0400

    gsocket: add a wrapper around g_set_error() to avoid extra work
    
    If @error is NULL then we don't even need to evaluate the remaining
    arguments. And if errno is EWOULDBLOCK, then no one should see the
    error message anyway, so don't bother g_strdup_printf'ing up a pretty
    one.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=752769

 gio/gsocket.c |   54 ++++++++++++++++++++++++++----------------------------
 1 files changed, 26 insertions(+), 28 deletions(-)
---
diff --git a/gio/gsocket.c b/gio/gsocket.c
index 938f6bd..3abb077 100644
--- a/gio/gsocket.c
+++ b/gio/gsocket.c
@@ -244,6 +244,24 @@ socket_strerror (int err)
 #endif
 }
 
+/* Wrapper around g_set_error() to avoid doing excess work */
+#define socket_set_error_lazy(err, errsv, fmt)                          \
+  G_STMT_START {                                                        \
+    GError **__err = (err);                                             \
+    int __errsv = (errsv);                                              \
+                                                                        \
+    if (__err)                                                          \
+      {                                                                 \
+        int __code = socket_io_error_from_errno (__errsv);              \
+        const char *__strerr = socket_strerror (__errsv);               \
+                                                                        \
+        if (__code == G_IO_ERROR_WOULD_BLOCK)                           \
+          g_set_error_literal (__err, G_IO_ERROR, __code, __strerr);    \
+        else                                                            \
+          g_set_error (__err, G_IO_ERROR, __code, fmt, __strerr);       \
+      }                                                                 \
+  } G_STMT_END
+
 #ifdef G_OS_WIN32
 #define win32_unset_event_mask(_socket, _mask) _win32_unset_event_mask (_socket, _mask)
 static void
@@ -2261,9 +2279,7 @@ g_socket_accept (GSocket       *socket,
                 }
             }
 
-         g_set_error (error, G_IO_ERROR,
-                      socket_io_error_from_errno (errsv),
-                      _("Error accepting connection: %s"), socket_strerror (errsv));
+         socket_set_error_lazy (error, errsv, _("Error accepting connection: %s"));
          return NULL;
        }
       break;
@@ -2637,9 +2653,7 @@ g_socket_receive_with_blocking (GSocket       *socket,
 
          win32_unset_event_mask (socket, FD_READ);
 
-         g_set_error (error, G_IO_ERROR,
-                      socket_io_error_from_errno (errsv),
-                      _("Error receiving data: %s"), socket_strerror (errsv));
+         socket_set_error_lazy (error, errsv, _("Error receiving data: %s"));
          return -1;
        }
 
@@ -2812,9 +2826,7 @@ g_socket_send_with_blocking (GSocket       *socket,
                 }
             }
 
-         g_set_error (error, G_IO_ERROR,
-                      socket_io_error_from_errno (errsv),
-                      _("Error sending data: %s"), socket_strerror (errsv));
+         socket_set_error_lazy (error, errsv, _("Error sending data: %s"));
          return -1;
        }
       break;
@@ -3899,10 +3911,7 @@ g_socket_send_message (GSocket                *socket,
                 continue;
               }
 
-           g_set_error (error, G_IO_ERROR,
-                        socket_io_error_from_errno (errsv),
-                        _("Error sending message: %s"), socket_strerror (errsv));
-
+           socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
            return -1;
          }
        break;
@@ -3982,10 +3991,7 @@ g_socket_send_message (GSocket                *socket,
                   }
               }
 
-           g_set_error (error, G_IO_ERROR,
-                        socket_io_error_from_errno (errsv),
-                        _("Error sending message: %s"), socket_strerror (errsv));
-
+           socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
            return -1;
          }
        break;
@@ -4225,9 +4231,7 @@ g_socket_send_messages_with_blocking (GSocket        *socket,
                 break;
               }
 
-            g_set_error (error, G_IO_ERROR,
-                         socket_io_error_from_errno (errsv),
-                         _("Error sending message: %s"), socket_strerror (errsv));
+            socket_set_error_lazy (error, errsv, _("Error sending message: %s"));
 
             /* we have to iterate over all messages below now, because we don't
              * know where between num_sent and num_messages the error occured */
@@ -4554,10 +4558,7 @@ g_socket_receive_message (GSocket                 *socket,
                continue;
              }
 
-           g_set_error (error, G_IO_ERROR,
-                        socket_io_error_from_errno (errsv),
-                        _("Error receiving message: %s"), socket_strerror (errsv));
-
+           socket_set_error_lazy (error, errsv, _("Error receiving message: %s"));
            return -1;
          }
        break;
@@ -4692,10 +4693,7 @@ g_socket_receive_message (GSocket                 *socket,
                   }
               }
 
-           g_set_error (error, G_IO_ERROR,
-                        socket_io_error_from_errno (errsv),
-                        _("Error receiving message: %s"), socket_strerror (errsv));
-
+           socket_set_error_lazy (error, errsv, _("Error receiving message: %s"));
            return -1;
          }
        win32_unset_event_mask (socket, FD_READ);


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