summaryrefslogtreecommitdiff
path: root/shared/nm-utils
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-12-23 13:51:21 +0100
committerThomas Haller <thaller@redhat.com>2019-01-09 16:46:41 +0100
commitaab3e14883cf27d24ca64ee6cae68485f97e404c (patch)
treea673b026c27e222d7000db7434ea912de50d24ba /shared/nm-utils
parent6ae04654f79643d758145df34b1011b051b1b329 (diff)
downloadNetworkManager-aab3e14883cf27d24ca64ee6cae68485f97e404c.tar.gz
shared: add nm_utils_getpagesize() and use it in netlink code
Since we already cached the result of getpagesize() in a static variable (at two places), move the code to nm-shared-utils, so it is reusable. Also, use sysconf() instead of getpagesize(), like suggested by `man getpagesize`.
Diffstat (limited to 'shared/nm-utils')
-rw-r--r--shared/nm-utils/nm-shared-utils.c32
-rw-r--r--shared/nm-utils/nm-shared-utils.h4
2 files changed, 36 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index 6131d3e809..9daa7783da 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -2285,3 +2285,35 @@ nm_utils_invoke_on_idle (NMUtilsInvokeOnIdleCallback callback,
data->cancelled_id = 0;
data->idle_id = g_idle_add (_nm_utils_invoke_on_idle_cb_idle, data);
}
+
+/*****************************************************************************/
+
+int
+nm_utils_getpagesize (void)
+{
+ static volatile int val = 0;
+ long l;
+ int v;
+
+ v = g_atomic_int_get (&val);
+
+ if (G_UNLIKELY (v == 0)) {
+ l = sysconf (_SC_PAGESIZE);
+
+ g_return_val_if_fail (l > 0 && l < G_MAXINT, 4*1024);
+
+ v = (int) l;
+ if (!g_atomic_int_compare_and_exchange (&val, 0, v)) {
+ v = g_atomic_int_get (&val);
+ g_return_val_if_fail (v > 0, 4*1024);
+ }
+ }
+
+ nm_assert (v > 0);
+#if NM_MORE_ASSERTS > 5
+ nm_assert (v == getpagesize ());
+ nm_assert (v == sysconf (_SC_PAGESIZE));
+#endif
+
+ return v;
+}
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index f3f3831e49..37d65e877c 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -1086,4 +1086,8 @@ nm_strv_ptrarray_take_gstring (GPtrArray *cmd,
FALSE));
}
+/*****************************************************************************/
+
+int nm_utils_getpagesize (void);
+
#endif /* __NM_SHARED_UTILS_H__ */