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`.
This commit is contained in:
Thomas Haller
2018-12-23 13:51:21 +01:00
parent 6ae04654f7
commit aab3e14883
3 changed files with 39 additions and 20 deletions

View File

@@ -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;
}