Use static assert over runtime assert if possible

We can test the these at compile time. Bug #609231.
This commit is contained in:
Christian Persch 2010-02-07 13:39:10 +01:00 committed by Ryan Lortie
parent 24aeaebc48
commit 1953b4087f
2 changed files with 18 additions and 14 deletions

View File

@ -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)
{

View File

@ -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;
}