From 23040d68fc02f826c13533c1cd2dcf233ca341ec Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 2 Feb 2017 19:03:38 +0100 Subject: [PATCH] shared: add NM_CACHED_QUARK() and NM_CACHED_QUARK_FCN() macros NM_CACHED_QUARK_FCN() is a replacement for G_DEFINE_QUARK(). G_DEFINE_QUARK() is mostly used to define GError quarks. As such, it always appends _quark() to the function name, which is unfavorable because it makes it harder to grep for the definition of the function. In general I think that macros that defined symbols by concatenating something should be avoided because that makes it harder to locate where the symbol was defined. --- shared/nm-utils/nm-macros-internal.h | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 70476b7ed0..6dc31f1739 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -341,6 +341,40 @@ _NM_IN_STRSET_streq (const char *x, const char *s) /*****************************************************************************/ +/* NM_CACHED_QUARK() returns the GQuark for @string, but caches + * it in a static variable to speed up future lookups. + * + * @string must be a string literal. + */ +#define NM_CACHED_QUARK(string) \ + ({ \ + static GQuark _nm_cached_quark = 0; \ + \ + (G_LIKELY (_nm_cached_quark != 0) \ + ? _nm_cached_quark \ + : (_nm_cached_quark = g_quark_from_static_string (""string""))); \ + }) + +/* NM_CACHED_QUARK_FCN() is essentially the same as G_DEFINE_QUARK + * with two differences: + * - @string must be a quited string-literal + * - @fcn must be the full function name, while G_DEFINE_QUARK() appends + * "_quark" to the function name. + * Both properties of G_DEFINE_QUARK() are non favorable, because you can no + * longer grep for string/fcn -- unless you are aware that you are searching + * for G_DEFINE_QUARK() and omit quotes / append _quark(). With NM_CACHED_QUARK_FCN(), + * ctags/cscope can locate the use of @fcn (though it doesn't recognize that + * NM_CACHED_QUARK_FCN() defines it). + */ +#define NM_CACHED_QUARK_FCN(string, fcn) \ +GQuark \ +fcn (void) \ +{ \ + return NM_CACHED_QUARK (string); \ +} + +/*****************************************************************************/ + #define nm_streq(s1, s2) (strcmp (s1, s2) == 0) #define nm_streq0(s1, s2) (g_strcmp0 (s1, s2) == 0)