summaryrefslogtreecommitdiff
path: root/src/basic/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-06-08 20:45:32 +0200
committerLennart Poettering <lennart@poettering.net>2016-06-14 20:01:45 +0200
commitd8cf2ac79b524d7784bccb428295ebc9c5e8548c (patch)
tree43a03ab780ccc0b130dc57322c816f99e3f09754 /src/basic/util.c
parent799ec13412e2f6649fd69ce4f1ca1674a40039b0 (diff)
util: introduce physical_memory_scale() to unify how we scale by physical memory
The various bits of code did the scaling all different, let's unify this, given that the code is not trivial.
Diffstat (limited to 'src/basic/util.c')
-rw-r--r--src/basic/util.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/basic/util.c b/src/basic/util.c
index 88d58cd94a..09d16697b7 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -805,6 +805,33 @@ uint64_t physical_memory(void) {
return MIN(mem, lim);
}
+uint64_t physical_memory_scale(uint64_t v, uint64_t max) {
+ uint64_t p, m, ps, r;
+
+ assert(max > 0);
+
+ /* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success
+ * the result is a multiple of the page size (rounds down). */
+
+ ps = page_size();
+ assert(ps > 0);
+
+ p = physical_memory() / ps;
+ assert(p > 0);
+
+ m = p * v;
+ if (m / p != v)
+ return UINT64_MAX;
+
+ m /= max;
+
+ r = m * ps;
+ if (r / ps != m)
+ return UINT64_MAX;
+
+ return r;
+}
+
int update_reboot_parameter_and_warn(const char *param) {
int r;