[systemd-devel] [PATCH 2/5] util: CACHED_METHOD macro

Simon Peeters peeters.simon at gmail.com
Fri Jan 3 17:35:24 PST 2014


the CACHED_METHOD macro is used to cache the return value of a method.

example:

CACHED_METHOD(bool, a_cached_method) {
	return some_dificult_and_slow_stuff();
}
---
 src/shared/apparmor-util.c | 16 ++++---------
 src/shared/ima-util.c      | 11 +++------
 src/shared/macro.h         | 16 +++++++++++++
 src/shared/smack-util.c    | 15 +++++-------
 src/shared/util.c          | 60 +++++++++++++---------------------------------
 5 files changed, 46 insertions(+), 72 deletions(-)

diff --git a/src/shared/apparmor-util.c b/src/shared/apparmor-util.c
index 2b85da1..805d624 100644
--- a/src/shared/apparmor-util.c
+++ b/src/shared/apparmor-util.c
@@ -25,17 +25,9 @@
 #include "fileio.h"
 #include "apparmor-util.h"
 
-static int use_apparmor_cached = -1;
+CACHED_METHOD(bool, use_apparmor) {
+        _cleanup_free_ char *p = NULL;
 
-bool use_apparmor(void) {
-
-        if (use_apparmor_cached < 0) {
-                _cleanup_free_ char *p = NULL;
-
-                use_apparmor_cached =
-                        read_one_line_file("/sys/module/apparmor/parameters/enabled", &p) >= 0 &&
-                        parse_boolean(p) > 0;
-        }
-
-        return use_apparmor_cached;
+        return read_one_line_file("/sys/module/apparmor/parameters/enabled", &p) >= 0 &&
+               parse_boolean(p) > 0;
 }
diff --git a/src/shared/ima-util.c b/src/shared/ima-util.c
index 6c1954b..7f78311 100644
--- a/src/shared/ima-util.c
+++ b/src/shared/ima-util.c
@@ -22,13 +22,8 @@
 #include <unistd.h>
 
 #include "ima-util.h"
+#include "macro.h"
 
-static int use_ima_cached = -1;
-
-bool use_ima(void) {
-
-        if (use_ima_cached < 0)
-                use_ima_cached = access("/sys/kernel/security/ima/", F_OK) >= 0;
-
-        return use_ima_cached;
+CACHED_METHOD(bool, use_ima) {
+        return access("/sys/kernel/security/ima/", F_OK) >=0;
 }
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 79bee03..e0c83d4 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -323,4 +323,20 @@ do {                                                                    \
 #endif
 #endif
 
+#define CACHED_METHOD(rettype, name) \
+        _CACHED_METHOD(rettype, name, )
+#define CACHED_METHOD_THREAD(rettype, name) \
+        _CACHED_METHOD(rettype, name, thread_local)
+#define _CACHED_METHOD(rettype, name, mod) \
+        rettype _##name(void); \
+        rettype name(void) { \
+                static mod bool cached = false; \
+                static mod rettype res; \
+                if (_unlikely_(cached == false)) \
+                        res = _##name(); \
+                cached = true; \
+                return res; \
+        } \
+        rettype _##name(void)
+
 #include "log.h"
diff --git a/src/shared/smack-util.c b/src/shared/smack-util.c
index df194e0..5d3a0d8 100644
--- a/src/shared/smack-util.c
+++ b/src/shared/smack-util.c
@@ -27,21 +27,18 @@
 #include <attr/xattr.h>
 #endif
 
+#include "macro.h"
 #include "smack-util.h"
 
-bool use_smack(void) {
 #ifdef HAVE_SMACK
-        static int use_smack_cached = -1;
-
-        if (use_smack_cached < 0)
-                use_smack_cached = access("/sys/fs/smackfs/", F_OK) >= 0;
-
-        return use_smack_cached;
+CACHED_METHOD(bool,use_smack) {
+        return access("/sys/fs/smackfs/", F_OK) >= 0;
+}
 #else
+bool use_smack(void) {
         return false;
-#endif
-
 }
+#endif
 
 int smack_label_path(const char *path, const char *label) {
 #ifdef HAVE_SMACK
diff --git a/src/shared/util.c b/src/shared/util.c
index 50dac70..2350204 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -3198,13 +3198,8 @@ void columns_lines_cache_reset(int signum) {
         cached_lines = 0;
 }
 
-bool on_tty(void) {
-        static int cached_on_tty = -1;
-
-        if (_unlikely_(cached_on_tty < 0))
-                cached_on_tty = isatty(STDOUT_FILENO) > 0;
-
-        return cached_on_tty;
+CACHED_METHOD(bool, on_tty) {
+        return isatty(STDOUT_FILENO) > 0;
 }
 
 int running_in_chroot(void) {
@@ -4569,13 +4564,8 @@ char *strjoin(const char *x, ...) {
         return r;
 }
 
-bool is_main_thread(void) {
-        static thread_local int cached = 0;
-
-        if (_unlikely_(cached == 0))
-                cached = getpid() == gettid() ? 1 : -1;
-
-        return cached > 0;
+CACHED_METHOD_THREAD(bool, is_main_thread) {
+        return getpid() == gettid();
 }
 
 int block_get_whole_disk(dev_t d, dev_t *ret) {
@@ -5395,47 +5385,31 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
         return NULL;
 }
 
-bool is_locale_utf8(void) {
+CACHED_METHOD(bool, is_locale_utf8) {
         const char *set;
-        static int cached_answer = -1;
 
-        if (cached_answer >= 0)
-                goto out;
-
-        if (!setlocale(LC_ALL, "")) {
-                cached_answer = true;
-                goto out;
-        }
+        if (!setlocale(LC_ALL, ""))
+                return true;
 
         set = nl_langinfo(CODESET);
-        if (!set) {
-                cached_answer = true;
-                goto out;
-        }
+        if (!set)
+                return true;
 
-        if (streq(set, "UTF-8")) {
-                cached_answer = true;
-                goto out;
-        }
+        if (streq(set, "UTF-8"))
+                return true;
 
         /* For LC_CTYPE=="C" return true, because CTYPE is effectly
          * unset and everything can do to UTF-8 nowadays. */
         set = setlocale(LC_CTYPE, NULL);
-        if (!set) {
-                cached_answer = true;
-                goto out;
-        }
+        if (!set)
+                return true;
 
         /* Check result, but ignore the result if C was set
          * explicitly. */
-        cached_answer =
-                streq(set, "C") &&
-                !getenv("LC_ALL") &&
-                !getenv("LC_CTYPE") &&
-                !getenv("LANG");
-
-out:
-        return (bool) cached_answer;
+        return streq(set, "C") &&
+               !getenv("LC_ALL") &&
+               !getenv("LC_CTYPE") &&
+               !getenv("LANG");
 }
 
 const char *draw_special_char(DrawSpecialChar ch) {
-- 
1.8.5.2



More information about the systemd-devel mailing list