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:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user