summaryrefslogtreecommitdiff
path: root/shared/nm-default.h
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2016-09-09 14:31:30 +0200
committerThomas Haller <thaller@redhat.com>2016-09-12 13:53:34 +0200
commiteb8da4c2820690a55e1ced721f710eb616fecb60 (patch)
treef86433e8aba40bb8cf35adec4e17dfe83de29cbe /shared/nm-default.h
parentaae26ebed31a8195f8b11b015ae77a45c753e5d3 (diff)
downloadNetworkManager-eb8da4c2820690a55e1ced721f710eb616fecb60.tar.gz
build: disable type checks in G_TYPE_CHECK_INSTANCE_CAST() macros (bgo#771120)
Avoid the pointless overhead. Even glib disables them unless you build with --enable-debug. https://bugzilla.gnome.org/show_bug.cgi?id=771120
Diffstat (limited to 'shared/nm-default.h')
-rw-r--r--shared/nm-default.h61
1 files changed, 57 insertions, 4 deletions
diff --git a/shared/nm-default.h b/shared/nm-default.h
index e3e3ea4414..9e2377cf80 100644
--- a/shared/nm-default.h
+++ b/shared/nm-default.h
@@ -52,15 +52,68 @@
#define NM_VERSION_MAX_ALLOWED NM_VERSION_NEXT_STABLE
#define NM_VERSION_MIN_REQUIRED NM_VERSION_0_9_8
+#ifndef NM_MORE_ASSERTS
+#define NM_MORE_ASSERTS 0
+#endif
+
+#if NM_MORE_ASSERTS == 0
+/* The cast macros like NM_TYPE() are implemented via G_TYPE_CHECK_INSTANCE_CAST()
+ * and _G_TYPE_CIC(). The latter, by default performs runtime checks of the type
+ * by calling g_type_check_instance_cast().
+ * This check has a certain overhead without being helpful.
+ *
+ * Example 1:
+ * static void foo (NMType *obj)
+ * {
+ * access_obj_without_check (obj);
+ * }
+ * foo ((NMType *) obj);
+ * // There is no runtime check and passing an invalid pointer
+ * // leads to a crash.
+ *
+ * Example 2:
+ * static void foo (NMType *obj)
+ * {
+ * access_obj_without_check (obj);
+ * }
+ * foo (NM_TYPE (obj));
+ * // There is a runtime check which prints a g_warning(), but that doesn't
+ * // avoid the crash as NM_TYPE() cannot do anything then passing on the
+ * // invalid pointer.
+ *
+ * Example 3:
+ * static void foo (NMType *obj)
+ * {
+ * g_return_if_fail (NM_IS_TYPE (obj));
+ * access_obj_without_check (obj);
+ * }
+ * foo ((NMType *) obj);
+ * // There is a runtime check which prints a g_critical() which also avoids
+ * // the crash. That is actually helpful to catch bugs and avoid crashes.
+ *
+ * Example 4:
+ * static void foo (NMType *obj)
+ * {
+ * g_return_if_fail (NM_IS_TYPE (obj));
+ * access_obj_without_check (obj);
+ * }
+ * foo (NM_TYPE (obj));
+ * // The runtime check is performed twice, with printing a g_warning() and
+ * // a g_critical() and avoiding the crash.
+ *
+ * Example 3 is how it should be done. Type checks in NM_TYPE() are pointless.
+ * Disable them for our production builds.
+ */
+#ifndef G_DISABLE_CAST_CHECKS
+#define G_DISABLE_CAST_CHECKS
+#endif
+#endif
+
#include <stdlib.h>
#include <glib.h>
/*****************************************************************************/
-#ifndef NM_MORE_ASSERTS
-#define NM_MORE_ASSERTS 0
-#endif
-
#if NM_MORE_ASSERTS == 0
/* glib assertions (g_return_*(), g_assert*()) contain a textual representation