[glib/wip/smcv/assert-standard-types: 1/2] glib-init: Statically assert more facts about standard types




commit c074bd8e45233e3c4efae818515fdd65ec7967af
Author: Simon McVittie <smcv collabora com>
Date:   Fri Oct 2 15:06:09 2020 +0100

    glib-init: Statically assert more facts about standard types
    
    This is a step towards #1484.
    
    If we have <stdint.h>, we can statically assert that each g(u)intN type
    is of the same size and alignment as the corresponding Standard C
    (u)intN_t type.
    
    Even if we don't, we can make similar assertions about lots of other
    standard types.
    
    Signed-off-by: Simon McVittie <smcv collabora com>

 glib/glib-init.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
---
diff --git a/glib/glib-init.c b/glib/glib-init.c
index ed800dca1..b670e961e 100644
--- a/glib/glib-init.c
+++ b/glib/glib-init.c
@@ -31,6 +31,10 @@
 #include <stdio.h>
 #include <ctype.h>
 
+#if HAVE_STDINT_H
+#include <stdint.h>
+#endif
+
 /* This seems as good a place as any to make static assertions about platform
  * assumptions we make throughout GLib. */
 
@@ -67,6 +71,83 @@ G_STATIC_ASSERT (G_ALIGNOF (TestChar) == G_ALIGNOF (int));
 G_STATIC_ASSERT (G_ALIGNOF (TestShort) == G_ALIGNOF (int));
 G_STATIC_ASSERT (G_ALIGNOF (TestInt) == G_ALIGNOF (int));
 
+G_STATIC_ASSERT (sizeof (gchar) == 1);
+G_STATIC_ASSERT (sizeof (guchar) == 1);
+G_STATIC_ASSERT (sizeof (gint8) * CHAR_BIT == 8);
+G_STATIC_ASSERT (sizeof (guint8) * CHAR_BIT == 8);
+G_STATIC_ASSERT (sizeof (gint16) * CHAR_BIT == 16);
+G_STATIC_ASSERT (sizeof (guint16) * CHAR_BIT == 16);
+G_STATIC_ASSERT (sizeof (gint32) * CHAR_BIT == 32);
+G_STATIC_ASSERT (sizeof (guint32) * CHAR_BIT == 32);
+G_STATIC_ASSERT (sizeof (gint64) * CHAR_BIT == 64);
+G_STATIC_ASSERT (sizeof (guint64) * CHAR_BIT == 64);
+
+G_STATIC_ASSERT (sizeof (void *) == GLIB_SIZEOF_VOID_P);
+G_STATIC_ASSERT (sizeof (gintptr) == sizeof (void *));
+G_STATIC_ASSERT (sizeof (guintptr) == sizeof (void *));
+
+G_STATIC_ASSERT (sizeof (long) == GLIB_SIZEOF_LONG);
+
+G_STATIC_ASSERT (G_HAVE_GINT64 == 1);
+
+G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SIZE_T);
+/* Not a typo: ssize_t is POSIX, not Standard C, but if it exists then
+ * it's the same size as size_t. */
+G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SSIZE_T);
+G_STATIC_ASSERT (sizeof (gsize) == GLIB_SIZEOF_SSIZE_T);
+G_STATIC_ASSERT (sizeof (gsize) == sizeof (size_t));
+/* Again this is size_t not ssize_t, because ssize_t is POSIX, not C99 */
+G_STATIC_ASSERT (sizeof (gssize) == sizeof (size_t));
+G_STATIC_ASSERT (G_ALIGNOF (gsize) == G_ALIGNOF (size_t));
+G_STATIC_ASSERT (G_ALIGNOF (gssize) == G_ALIGNOF (size_t));
+
+/* goffset is always 64-bit, even if off_t is only 32-bit
+ * (compiling without large-file-support on 32-bit) */
+G_STATIC_ASSERT (sizeof (goffset) == sizeof (gint64));
+G_STATIC_ASSERT (G_ALIGNOF (goffset) == G_ALIGNOF (gint64));
+
+G_STATIC_ASSERT (sizeof (gfloat) == sizeof (float));
+G_STATIC_ASSERT (G_ALIGNOF (gfloat) == G_ALIGNOF (float));
+G_STATIC_ASSERT (sizeof (gdouble) == sizeof (double));
+G_STATIC_ASSERT (G_ALIGNOF (gdouble) == G_ALIGNOF (double));
+
+G_STATIC_ASSERT (sizeof (gintptr) == sizeof (intptr_t));
+G_STATIC_ASSERT (sizeof (guintptr) == sizeof (uintptr_t));
+G_STATIC_ASSERT (G_ALIGNOF (gintptr) == G_ALIGNOF (intptr_t));
+G_STATIC_ASSERT (G_ALIGNOF (guintptr) == G_ALIGNOF (uintptr_t));
+
+#if HAVE_STDINT_H
+
+  G_STATIC_ASSERT (sizeof (gint8) == sizeof (int8_t));
+  G_STATIC_ASSERT (sizeof (guint8) == sizeof (uint8_t));
+  G_STATIC_ASSERT (G_ALIGNOF (gint8) == G_ALIGNOF (int8_t));
+  G_STATIC_ASSERT (G_ALIGNOF (guint8) == G_ALIGNOF (uint8_t));
+
+  G_STATIC_ASSERT (sizeof (gint16) == sizeof (int16_t));
+  G_STATIC_ASSERT (sizeof (guint16) == sizeof (uint16_t));
+  G_STATIC_ASSERT (G_ALIGNOF (gint16) == G_ALIGNOF (int16_t));
+  G_STATIC_ASSERT (G_ALIGNOF (guint16) == G_ALIGNOF (uint16_t));
+
+  G_STATIC_ASSERT (sizeof (gint32) == sizeof (int32_t));
+  G_STATIC_ASSERT (sizeof (guint32) == sizeof (uint32_t));
+  G_STATIC_ASSERT (G_ALIGNOF (gint32) == G_ALIGNOF (int32_t));
+  G_STATIC_ASSERT (G_ALIGNOF (guint32) == G_ALIGNOF (uint32_t));
+
+  G_STATIC_ASSERT (sizeof (gint64) == sizeof (int64_t));
+  G_STATIC_ASSERT (sizeof (guint64) == sizeof (uint64_t));
+  G_STATIC_ASSERT (G_ALIGNOF (gint64) == G_ALIGNOF (int64_t));
+  G_STATIC_ASSERT (G_ALIGNOF (guint64) == G_ALIGNOF (uint64_t));
+
+#elif defined(G_OS_WIN32)
+
+  /* Windows has historically been the reason we can't have nice things */
+
+#else
+
+# warning stdint.h not found, please use a C99 compiler
+
+#endif
+
 /**
  * g_mem_gc_friendly:
  *


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