diff options
author | Explorer09 <explorer09@gmail.com> | 2016-01-15 20:26:01 +0800 |
---|---|---|
committer | Explorer09 <explorer09@gmail.com> | 2016-01-15 20:26:01 +0800 |
commit | 6dae8108f8e6a8d3f4ccf3d92f8bb19d3933861a (patch) | |
tree | 7cb08e282337b483de387b68cf7e225eb359853f /linux | |
parent | 195f5edbc8dd79267fa23feb5fda2a8be812abeb (diff) |
Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.
With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
MAX and MIN combination. In theory, this makes the code slightly
smaller, in case that (low) or (high) or both are computed at
runtime, so that compilers cannot optimize them. (The third
comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
results. Unlike MIN & MAX which will force either (low) or (high) to
win. No assertion of ((low)<=(high)) is done in this macro, for now.
This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
Diffstat (limited to 'linux')
-rw-r--r-- | linux/LinuxProcessList.c | 6 | ||||
-rw-r--r-- | linux/LinuxProcessList.h | 4 | ||||
-rw-r--r-- | linux/Platform.c | 6 | ||||
-rw-r--r-- | linux/Platform.h | 4 |
4 files changed, 18 insertions, 2 deletions
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 9387dbd4..19820ada 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -83,6 +83,10 @@ typedef struct LinuxProcessList_ { #endif }*/ + +#ifndef CLAMP +#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +#endif ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { LinuxProcessList* this = calloc(1, sizeof(LinuxProcessList)); @@ -540,7 +544,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO) LinuxProcess_updateIOPriority(lp); float percent_cpu = (lp->utime + lp->stime - lasttimes) / period * 100.0; - proc->percent_cpu = MAX(MIN(percent_cpu, cpus*100.0), 0.0); + proc->percent_cpu = CLAMP(percent_cpu, 0.0, cpus * 100.0); if (isnan(proc->percent_cpu)) proc->percent_cpu = 0.0; proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(pl->totalMem) * 100.0; diff --git a/linux/LinuxProcessList.h b/linux/LinuxProcessList.h index 2a4abff9..97725812 100644 --- a/linux/LinuxProcessList.h +++ b/linux/LinuxProcessList.h @@ -63,6 +63,10 @@ typedef struct LinuxProcessList_ { #define PROC_LINE_LENGTH 512 #endif + +#ifndef CLAMP +#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +#endif ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId); diff --git a/linux/Platform.c b/linux/Platform.c index 81c96e4c..7b8a120b 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -37,6 +37,10 @@ in the source distribution for its full text. #include "SignalsPanel.h" }*/ +#ifndef CLAMP +#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +#endif + ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; //static ProcessField defaultIoFields[] = { PID, IO_PRIORITY, USER, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, COMM, 0 }; @@ -186,7 +190,7 @@ double Platform_setCPUValues(Meter* this, int cpu) { Meter_setItems(this, 4); percent = v[0]+v[1]+v[2]+v[3]; } - percent = MIN(100.0, MAX(0.0, percent)); + percent = CLAMP(percent, 0.0, 100.0); if (isnan(percent)) percent = 0.0; return percent; } diff --git a/linux/Platform.h b/linux/Platform.h index 114b9a20..b0d69fb7 100644 --- a/linux/Platform.h +++ b/linux/Platform.h @@ -15,6 +15,10 @@ in the source distribution for its full text. #include "LinuxProcess.h" #include "SignalsPanel.h" +#ifndef CLAMP +#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) +#endif + extern ProcessField Platform_defaultFields[]; extern int Platform_numberOfFields; |