mirror of https://github.com/xzeldon/htop.git
Merge pull request #349 from Explorer09/clamp-macro
Introduce CLAMP macro. Unify all MAX(l,MIN(h,x)) uses.
This commit is contained in:
commit
09cf369f2b
19
Meter.c
19
Meter.c
|
@ -114,6 +114,9 @@ typedef struct GraphData_ {
|
||||||
#ifndef MAX
|
#ifndef MAX
|
||||||
#define MAX(a,b) ((a)>(b)?(a):(b))
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef CLAMP
|
||||||
|
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
|
||||||
|
#endif
|
||||||
|
|
||||||
MeterClass Meter_class = {
|
MeterClass Meter_class = {
|
||||||
.super = {
|
.super = {
|
||||||
|
@ -297,8 +300,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
|
||||||
int items = Meter_getItems(this);
|
int items = Meter_getItems(this);
|
||||||
for (int i = 0; i < items; i++) {
|
for (int i = 0; i < items; i++) {
|
||||||
double value = this->values[i];
|
double value = this->values[i];
|
||||||
value = MAX(value, 0);
|
value = CLAMP(value, 0.0, this->total);
|
||||||
value = MIN(value, this->total);
|
|
||||||
if (value > 0) {
|
if (value > 0) {
|
||||||
blockSizes[i] = ceil((value/this->total) * w);
|
blockSizes[i] = ceil((value/this->total) * w);
|
||||||
} else {
|
} else {
|
||||||
|
@ -306,7 +308,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
|
||||||
}
|
}
|
||||||
int nextOffset = offset + blockSizes[i];
|
int nextOffset = offset + blockSizes[i];
|
||||||
// (Control against invalid values)
|
// (Control against invalid values)
|
||||||
nextOffset = MIN(MAX(nextOffset, 0), w);
|
nextOffset = CLAMP(nextOffset, 0, w);
|
||||||
for (int j = offset; j < nextOffset; j++)
|
for (int j = offset; j < nextOffset; j++)
|
||||||
if (bar[j] == ' ') {
|
if (bar[j] == ' ') {
|
||||||
if (CRT_colorScheme == COLORSCHEME_MONOCHROME) {
|
if (CRT_colorScheme == COLORSCHEME_MONOCHROME) {
|
||||||
|
@ -324,8 +326,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
|
||||||
attrset(CRT_colors[Meter_attributes(this)[i]]);
|
attrset(CRT_colors[Meter_attributes(this)[i]]);
|
||||||
mvaddnstr(y, x + offset, bar + offset, blockSizes[i]);
|
mvaddnstr(y, x + offset, bar + offset, blockSizes[i]);
|
||||||
offset += blockSizes[i];
|
offset += blockSizes[i];
|
||||||
offset = MAX(offset, 0);
|
offset = CLAMP(offset, 0, w);
|
||||||
offset = MIN(offset, w);
|
|
||||||
}
|
}
|
||||||
if (offset < w) {
|
if (offset < w) {
|
||||||
attrset(CRT_colors[BAR_SHADOW]);
|
attrset(CRT_colors[BAR_SHADOW]);
|
||||||
|
@ -406,13 +407,13 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
|
||||||
|
|
||||||
for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) {
|
for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) {
|
||||||
const double dot = (1.0 / (GraphMeterMode_pixPerRow * 4));
|
const double dot = (1.0 / (GraphMeterMode_pixPerRow * 4));
|
||||||
int v1 = MIN(GraphMeterMode_pixPerRow * 4, MAX(1, data->values[i] / dot));
|
int v1 = CLAMP(data->values[i] / dot, 1, GraphMeterMode_pixPerRow * 4);
|
||||||
int v2 = MIN(GraphMeterMode_pixPerRow * 4, MAX(1, data->values[i+1] / dot));
|
int v2 = CLAMP(data->values[i+1] / dot, 1, GraphMeterMode_pixPerRow * 4);
|
||||||
|
|
||||||
int colorIdx = GRAPH_1;
|
int colorIdx = GRAPH_1;
|
||||||
for (int line = 0; line < 4; line++) {
|
for (int line = 0; line < 4; line++) {
|
||||||
int line1 = MIN(GraphMeterMode_pixPerRow, MAX(0, v1 - (GraphMeterMode_pixPerRow * (3 - line))));
|
int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (3 - line)), 0, GraphMeterMode_pixPerRow);
|
||||||
int line2 = MIN(GraphMeterMode_pixPerRow, MAX(0, v2 - (GraphMeterMode_pixPerRow * (3 - line))));
|
int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (3 - line)), 0, GraphMeterMode_pixPerRow);
|
||||||
|
|
||||||
attrset(CRT_colors[colorIdx]);
|
attrset(CRT_colors[colorIdx]);
|
||||||
mvaddstr(y+line, x+k, GraphMeterMode_dots[line1 * (GraphMeterMode_pixPerRow + 1) + line2]);
|
mvaddstr(y+line, x+k, GraphMeterMode_dots[line1 * (GraphMeterMode_pixPerRow + 1) + line2]);
|
||||||
|
|
3
Meter.h
3
Meter.h
|
@ -100,6 +100,9 @@ typedef struct GraphData_ {
|
||||||
#ifndef MAX
|
#ifndef MAX
|
||||||
#define MAX(a,b) ((a)>(b)?(a):(b))
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef CLAMP
|
||||||
|
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
|
||||||
|
#endif
|
||||||
|
|
||||||
extern MeterClass Meter_class;
|
extern MeterClass Meter_class;
|
||||||
|
|
||||||
|
|
|
@ -62,10 +62,6 @@ typedef struct RichString_ {
|
||||||
|
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
#ifndef MIN
|
|
||||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define charBytes(n) (sizeof(CharType) * (n))
|
#define charBytes(n) (sizeof(CharType) * (n))
|
||||||
|
|
||||||
static void RichString_extendLen(RichString* this, int len) {
|
static void RichString_extendLen(RichString* this, int len) {
|
||||||
|
|
|
@ -59,10 +59,6 @@ typedef struct RichString_ {
|
||||||
} RichString;
|
} RichString;
|
||||||
|
|
||||||
|
|
||||||
#ifndef MIN
|
|
||||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define charBytes(n) (sizeof(CharType) * (n))
|
#define charBytes(n) (sizeof(CharType) * (n))
|
||||||
|
|
||||||
#define RichString_setLen(this, len) do{ if(len < RICHSTRING_MAXLEN && this->chlen < RICHSTRING_MAXLEN) { RichString_setChar(this,len,0); this->chlen=len; } else RichString_extendLen(this,len); }while(0)
|
#define RichString_setLen(this, len) do{ if(len < RICHSTRING_MAXLEN && this->chlen < RICHSTRING_MAXLEN) { RichString_setChar(this,len,0); this->chlen=len; } else RichString_extendLen(this,len); }while(0)
|
||||||
|
|
|
@ -27,6 +27,10 @@ in the source distribution for its full text.
|
||||||
#include "DarwinProcess.h"
|
#include "DarwinProcess.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, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
|
ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
|
||||||
|
|
||||||
SignalItem Platform_signals[] = {
|
SignalItem Platform_signals[] = {
|
||||||
|
@ -213,7 +217,7 @@ double Platform_setCPUValues(Meter* mtr, int cpu) {
|
||||||
/* Convert to percent and return */
|
/* Convert to percent and return */
|
||||||
total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL];
|
total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL];
|
||||||
|
|
||||||
return MIN(100.0, MAX(0.0, total));
|
return CLAMP(total, 0.0, 100.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform_setMemoryValues(Meter* mtr) {
|
void Platform_setMemoryValues(Meter* mtr) {
|
||||||
|
|
|
@ -16,6 +16,10 @@ in the source distribution for its full text.
|
||||||
#include "BatteryMeter.h"
|
#include "BatteryMeter.h"
|
||||||
#include "DarwinProcess.h"
|
#include "DarwinProcess.h"
|
||||||
|
|
||||||
|
#ifndef CLAMP
|
||||||
|
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
|
||||||
|
#endif
|
||||||
|
|
||||||
extern ProcessField Platform_defaultFields[];
|
extern ProcessField Platform_defaultFields[];
|
||||||
|
|
||||||
extern SignalItem Platform_signals[];
|
extern SignalItem Platform_signals[];
|
||||||
|
|
|
@ -34,6 +34,10 @@ extern ProcessFieldData Process_fields[];
|
||||||
|
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
#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, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
|
ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
|
||||||
|
|
||||||
int Platform_numberOfFields = LAST_PROCESSFIELD;
|
int Platform_numberOfFields = LAST_PROCESSFIELD;
|
||||||
|
@ -171,7 +175,7 @@ double Platform_setCPUValues(Meter* this, int cpu) {
|
||||||
percent = v[0]+v[1]+v[2];
|
percent = v[0]+v[1]+v[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
percent = MIN(100.0, MAX(0.0, percent));
|
percent = CLAMP(percent, 0.0, 100.0);
|
||||||
if (isnan(percent)) percent = 0.0;
|
if (isnan(percent)) percent = 0.0;
|
||||||
return percent;
|
return percent;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,10 @@ in the source distribution for its full text.
|
||||||
extern ProcessFieldData Process_fields[];
|
extern ProcessFieldData Process_fields[];
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef CLAMP
|
||||||
|
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
|
||||||
|
#endif
|
||||||
|
|
||||||
extern ProcessField Platform_defaultFields[];
|
extern ProcessField Platform_defaultFields[];
|
||||||
|
|
||||||
extern int Platform_numberOfFields;
|
extern int Platform_numberOfFields;
|
||||||
|
@ -42,4 +46,6 @@ void Platform_setSwapValues(Meter* this);
|
||||||
|
|
||||||
void Platform_setTasksValues(Meter* this);
|
void Platform_setTasksValues(Meter* this);
|
||||||
|
|
||||||
|
char* Platform_getProcessEnv(pid_t pid);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -84,6 +84,10 @@ typedef struct LinuxProcessList_ {
|
||||||
|
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
#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) {
|
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
|
||||||
LinuxProcessList* this = calloc(1, sizeof(LinuxProcessList));
|
LinuxProcessList* this = calloc(1, sizeof(LinuxProcessList));
|
||||||
ProcessList* pl = &(this->super);
|
ProcessList* pl = &(this->super);
|
||||||
|
@ -540,7 +544,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char*
|
||||||
if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO)
|
if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO)
|
||||||
LinuxProcess_updateIOPriority(lp);
|
LinuxProcess_updateIOPriority(lp);
|
||||||
float percent_cpu = (lp->utime + lp->stime - lasttimes) / period * 100.0;
|
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;
|
if (isnan(proc->percent_cpu)) proc->percent_cpu = 0.0;
|
||||||
proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(pl->totalMem) * 100.0;
|
proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(pl->totalMem) * 100.0;
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,10 @@ typedef struct LinuxProcessList_ {
|
||||||
#endif
|
#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);
|
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);
|
||||||
|
|
||||||
void ProcessList_delete(ProcessList* pl);
|
void ProcessList_delete(ProcessList* pl);
|
||||||
|
|
|
@ -37,6 +37,10 @@ in the source distribution for its full text.
|
||||||
#include "SignalsPanel.h"
|
#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 };
|
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 };
|
//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);
|
Meter_setItems(this, 4);
|
||||||
percent = v[0]+v[1]+v[2]+v[3];
|
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;
|
if (isnan(percent)) percent = 0.0;
|
||||||
return percent;
|
return percent;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,10 @@ in the source distribution for its full text.
|
||||||
#include "LinuxProcess.h"
|
#include "LinuxProcess.h"
|
||||||
#include "SignalsPanel.h"
|
#include "SignalsPanel.h"
|
||||||
|
|
||||||
|
#ifndef CLAMP
|
||||||
|
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
|
||||||
|
#endif
|
||||||
|
|
||||||
extern ProcessField Platform_defaultFields[];
|
extern ProcessField Platform_defaultFields[];
|
||||||
|
|
||||||
extern int Platform_numberOfFields;
|
extern int Platform_numberOfFields;
|
||||||
|
|
|
@ -42,6 +42,10 @@ typedef struct OpenBSDProcessList_ {
|
||||||
|
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
#ifndef CLAMP
|
||||||
|
#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x)))
|
||||||
|
#endif
|
||||||
|
|
||||||
static int pageSizeKb;
|
static int pageSizeKb;
|
||||||
static long fscale;
|
static long fscale;
|
||||||
|
|
||||||
|
@ -229,7 +233,7 @@ void ProcessList_goThroughEntries(ProcessList* this) {
|
||||||
proc->m_size = kproc->p_vm_dsize;
|
proc->m_size = kproc->p_vm_dsize;
|
||||||
proc->m_resident = kproc->p_vm_rssize;
|
proc->m_resident = kproc->p_vm_rssize;
|
||||||
proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(this->totalMem) * 100.0;
|
proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(this->totalMem) * 100.0;
|
||||||
proc->percent_cpu = MAX(MIN(getpcpu(kproc), this->cpuCount*100.0), 0.0);
|
proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->cpuCount*100.0);
|
||||||
//proc->nlwp = kproc->p_numthreads;
|
//proc->nlwp = kproc->p_numthreads;
|
||||||
//proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 10);
|
//proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 10);
|
||||||
proc->nice = kproc->p_nice - 20;
|
proc->nice = kproc->p_nice - 20;
|
||||||
|
|
|
@ -27,6 +27,10 @@ typedef struct OpenBSDProcessList_ {
|
||||||
} OpenBSDProcessList;
|
} OpenBSDProcessList;
|
||||||
|
|
||||||
|
|
||||||
|
#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);
|
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue