[glib] Use static assert over runtime assert if possible



commit 1953b4087f2852e43c0a4dd46bfbd6228135cf70
Author: Christian Persch <chpe gnome org>
Date:   Sun Feb 7 13:39:10 2010 +0100

    Use static assert over runtime assert if possible
    
    We can test the these at compile time. Bug #609231.

 glib/gmappedfile.c |   19 ++++++++++---------
 glib/gvariant.c    |   13 ++++++++-----
 2 files changed, 18 insertions(+), 14 deletions(-)
---
diff --git a/glib/gmappedfile.c b/glib/gmappedfile.c
index 9bf7146..42f1322 100644
--- a/glib/gmappedfile.c
+++ b/glib/gmappedfile.c
@@ -77,6 +77,16 @@ struct _GMappedFile
 #endif
 };
 
+/* Make sure the layout of GMappedFile is the same as GBuffer's */
+G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, contents) ==
+                 G_STRUCT_OFFSET (GBuffer, data));
+G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, length) ==
+                 G_STRUCT_OFFSET (GBuffer, size));
+G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, ref_count) ==
+                 G_STRUCT_OFFSET (GBuffer, ref_count));
+G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, free_func) ==
+                 G_STRUCT_OFFSET (GBuffer, free_func));
+
 static void
 g_mapped_file_destroy (GMappedFile *file)
 {
@@ -129,15 +139,6 @@ g_mapped_file_new (const gchar  *filename,
   g_return_val_if_fail (filename != NULL, NULL);
   g_return_val_if_fail (!error || *error == NULL, NULL);
 
-  g_assert (G_STRUCT_OFFSET (GMappedFile, contents) ==
-            G_STRUCT_OFFSET (GBuffer, data));
-  g_assert (G_STRUCT_OFFSET (GMappedFile, length) ==
-            G_STRUCT_OFFSET (GBuffer, size));
-  g_assert (G_STRUCT_OFFSET (GMappedFile, ref_count) ==
-            G_STRUCT_OFFSET (GBuffer, ref_count));
-  g_assert (G_STRUCT_OFFSET (GMappedFile, free_func) ==
-            G_STRUCT_OFFSET (GBuffer, free_func));
-
   fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
   if (fd == -1)
     {
diff --git a/glib/gvariant.c b/glib/gvariant.c
index 342eeeb..be33fec 100644
--- a/glib/gvariant.c
+++ b/glib/gvariant.c
@@ -1927,6 +1927,8 @@ struct stack_iter
   gsize magic;
 };
 
+G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter));
+
 struct heap_iter
 {
   struct stack_iter iter;
@@ -1993,8 +1995,6 @@ gsize
 g_variant_iter_init (GVariantIter *iter,
                      GVariant     *value)
 {
-  g_assert (sizeof (struct stack_iter) <= sizeof (GVariantIter));
-
   GVSI(iter)->magic = GVSI_MAGIC;
   GVSI(iter)->value = value;
   GVSI(iter)->n = g_variant_n_children (value);
@@ -2183,6 +2183,8 @@ struct stack_builder
   gsize magic;
 };
 
+G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder));
+
 struct heap_builder
 {
   GVariantBuilder builder;
@@ -2371,7 +2373,6 @@ g_variant_builder_init (GVariantBuilder    *builder,
   g_return_if_fail (type != NULL);
   g_return_if_fail (g_variant_type_is_container (type));
 
-  g_assert (sizeof (struct stack_builder) <= sizeof (GVariantBuilder));
   memset (builder, 0, sizeof (GVariantBuilder));
 
   GVSB(builder)->type = g_variant_type_copy (type);
@@ -3220,6 +3221,10 @@ g_variant_valist_new_leaf (const gchar **str,
     }
 }
 
+/* The code below assumes this */
+G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32));
+G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
+
 static void
 g_variant_valist_get_leaf (const gchar **str,
                            GVariant     *value,
@@ -3313,14 +3318,12 @@ g_variant_valist_get_leaf (const gchar **str,
         case 'u':
         case 'h':
         case 'b':
-          g_assert (sizeof (gboolean) == sizeof (guint32));
           *(guint32 *) ptr = 0;
           return;
 
         case 'x':
         case 't':
         case 'd':
-          g_assert (sizeof (gdouble) == sizeof (guint64));
           *(guint64 *) ptr = 0;
           return;
         }



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