[glib: 1/6] gerror: Simplify error construction




commit b715e4c9d00a05b7edfb93692e6930563349bac2
Author: Krzesimir Nowak <qdlacz gmail com>
Date:   Thu Dec 26 16:23:23 2019 +0100

    gerror: Simplify error construction
    
    Factor out the GError creation to a common function. When adding a
    support for extended error types, this will limit the number of places
    where these errors are allocated.

 glib/gerror.c | 46 +++++++++++++++++++---------------------------
 1 file changed, 19 insertions(+), 27 deletions(-)
---
diff --git a/glib/gerror.c b/glib/gerror.c
index 86167d7e5..129a82f2b 100644
--- a/glib/gerror.c
+++ b/glib/gerror.c
@@ -382,6 +382,20 @@
 #include "gstrfuncs.h"
 #include "gtestutils.h"
 
+static GError *
+g_error_new_steal (GQuark  domain,
+                   gint    code,
+                   gchar  *message)
+{
+  GError *error = g_slice_new (GError);
+
+  error->domain = domain;
+  error->code = code;
+  error->message = message;
+
+  return error;
+}
+
 /**
  * g_error_new_valist:
  * @domain: error domain
@@ -402,8 +416,6 @@ g_error_new_valist (GQuark       domain,
                     const gchar *format,
                     va_list      args)
 {
-  GError *error;
-
   /* Historically, GError allowed this (although it was never meant to work),
    * and it has significant use in the wild, which g_return_val_if_fail
    * would break. It should maybe g_return_val_if_fail in GLib 4.
@@ -412,13 +424,7 @@ g_error_new_valist (GQuark       domain,
   g_warn_if_fail (domain != 0);
   g_warn_if_fail (format != NULL);
 
-  error = g_slice_new (GError);
-
-  error->domain = domain;
-  error->code = code;
-  error->message = g_strdup_vprintf (format, args);
-
-  return error;
+  return g_error_new_steal (domain, code, g_strdup_vprintf (format, args));
 }
 
 /**
@@ -470,18 +476,10 @@ g_error_new_literal (GQuark         domain,
                      gint           code,
                      const gchar   *message)
 {
-  GError* err;
-
   g_return_val_if_fail (message != NULL, NULL);
   g_return_val_if_fail (domain != 0, NULL);
 
-  err = g_slice_new (GError);
-
-  err->domain = domain;
-  err->code = code;
-  err->message = g_strdup (message);
-
-  return err;
+  return g_error_new_steal (domain, code, g_strdup (message));
 }
 
 /**
@@ -511,20 +509,14 @@ g_error_free (GError *error)
 GError*
 g_error_copy (const GError *error)
 {
-  GError *copy;
- 
   g_return_val_if_fail (error != NULL, NULL);
   /* See g_error_new_valist for why these don't return */
   g_warn_if_fail (error->domain != 0);
   g_warn_if_fail (error->message != NULL);
 
-  copy = g_slice_new (GError);
-
-  *copy = *error;
-
-  copy->message = g_strdup (error->message);
-
-  return copy;
+  return g_error_new_steal (error->domain,
+                            error->code,
+                            g_strdup (error->message));
 }
 
 /**


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