From c5808c56db166528ae7e74cedb51cc466f973b9f Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Wed, 9 Sep 2020 16:56:04 +1000 Subject: Consolidate repeated macro definitions into one header The MIN, MAX, CLAMP, MINIMUM, and MAXIMUM macros appear throughout the codebase with many re-definitions. Make a single copy of each in a common header file, and use the BSD variants of MINIMUM/MAXIMUM due to conflicts in the system headers. --- CPUMeter.c | 7 ------- CPUMeter.h | 7 ------- EnvScreen.c | 2 +- Header.c | 9 +-------- Header.h | 4 ---- Macros.h | 16 ++++++++++++++++ Makefile.am | 2 +- Meter.c | 11 ----------- Meter.h | 10 ---------- Object.h | 1 + Panel.c | 27 ++++++++++----------------- Panel.h | 7 ------- RichString.c | 6 +----- RichString.h | 5 ----- TasksMeter.c | 2 +- darwin/Platform.c | 4 ---- darwin/Platform.h | 4 ---- dragonflybsd/DragonFlyBSDProcessList.c | 2 +- dragonflybsd/Platform.c | 4 ---- dragonflybsd/Platform.h | 4 ---- freebsd/FreeBSDProcessList.c | 2 +- freebsd/Platform.c | 4 ---- freebsd/Platform.h | 4 ---- linux/LinuxProcessList.c | 6 +----- linux/LinuxProcessList.h | 4 ---- linux/Platform.c | 4 ---- linux/Platform.h | 4 ---- openbsd/OpenBSDProcessList.c | 15 --------------- openbsd/OpenBSDProcessList.h | 14 -------------- 29 files changed, 35 insertions(+), 156 deletions(-) create mode 100644 Macros.h diff --git a/CPUMeter.c b/CPUMeter.c index b6e4deab..df5585e9 100644 --- a/CPUMeter.c +++ b/CPUMeter.c @@ -20,13 +20,6 @@ int CPUMeter_attributes[] = { CPU_NICE, CPU_NORMAL, CPU_SYSTEM, CPU_IRQ, CPU_SOFTIRQ, CPU_STEAL, CPU_GUEST, CPU_IOWAIT }; -#ifndef MIN -#define MIN(a,b) ((a)<(b)?(a):(b)) -#endif -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif - static void CPUMeter_init(Meter* this) { int cpu = this->param; if (this->pl->cpuCount > 1) { diff --git a/CPUMeter.h b/CPUMeter.h index 2caa8291..d66eefbb 100644 --- a/CPUMeter.h +++ b/CPUMeter.h @@ -24,13 +24,6 @@ typedef enum { extern int CPUMeter_attributes[]; -#ifndef MIN -#define MIN(a,b) ((a)<(b)?(a):(b)) -#endif -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif - extern MeterClass CPUMeter_class; extern MeterClass AllCPUsMeter_class; diff --git a/EnvScreen.c b/EnvScreen.c index fa6ffdcf..479a45d6 100644 --- a/EnvScreen.c +++ b/EnvScreen.c @@ -37,7 +37,7 @@ void EnvScreen_draw(InfoScreen* this) { void EnvScreen_scan(InfoScreen* this) { Panel* panel = this->display; - int idx = MAX(Panel_getSelectedIndex(panel), 0); + int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0); Panel_prune(panel); diff --git a/Header.c b/Header.c index ffd31a24..574165a6 100644 --- a/Header.c +++ b/Header.c @@ -16,14 +16,7 @@ in the source distribution for its full text. #include #include - -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif - -#ifndef Header_forEachColumn #define Header_forEachColumn(this_, i_) for (int (i_)=0; (i_) < (this_)->nrColumns; ++(i_)) -#endif Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns) { Header* this = xCalloc(1, sizeof(Header)); @@ -196,7 +189,7 @@ int Header_calculateHeight(Header* this) { Meter* meter = (Meter*) Vector_get(meters, i); height += meter->h; } - maxHeight = MAX(maxHeight, height); + maxHeight = MAXIMUM(maxHeight, height); } this->height = maxHeight; this->pad = pad; diff --git a/Header.h b/Header.h index e281a0c2..b221becf 100644 --- a/Header.h +++ b/Header.h @@ -20,10 +20,6 @@ typedef struct Header_ { int height; } Header; -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif - #define Header_forEachColumn(this_, i_) for (int (i_)=0; (i_) < (this_)->nrColumns; ++(i_)) Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns); diff --git a/Macros.h b/Macros.h new file mode 100644 index 00000000..cb84b291 --- /dev/null +++ b/Macros.h @@ -0,0 +1,16 @@ +#ifndef HEADER_Macros +#define HEADER_Macros + +#ifndef MINIMUM +#define MINIMUM(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef MAXIMUM +#define MAXIMUM(a, b) ((a) > (b) ? (a) : (b)) +#endif + +#ifndef CLAMP +#define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low)) +#endif + +#endif diff --git a/Makefile.am b/Makefile.am index fbbf7bef..50fb586b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,7 +34,7 @@ BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h ScreenManager.h Settings.h SignalsPanel.h StringUtils.h SwapMeter.h \ TasksMeter.h UptimeMeter.h TraceScreen.h UsersTable.h Vector.h Process.h \ AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IncSet.h Action.h \ -EnvScreen.h InfoScreen.h XAlloc.h +EnvScreen.h InfoScreen.h XAlloc.h Macros.h # Linux # ----- diff --git a/Meter.c b/Meter.c index ffdfc3e0..34f428ff 100644 --- a/Meter.c +++ b/Meter.c @@ -27,17 +27,6 @@ in the source distribution for its full text. #define GRAPH_HEIGHT 4 /* Unit: rows (lines) */ - -#ifndef MIN -#define MIN(a,b) ((a)<(b)?(a):(b)) -#endif -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - MeterClass Meter_class = { .super = { .extends = Class(Object) diff --git a/Meter.h b/Meter.h index 7936fb78..773d94ff 100644 --- a/Meter.h +++ b/Meter.h @@ -94,16 +94,6 @@ typedef struct GraphData_ { double values[METER_BUFFER_LEN]; } GraphData; -#ifndef MIN -#define MIN(a,b) ((a)<(b)?(a):(b)) -#endif -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - extern MeterClass Meter_class; Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type); diff --git a/Object.h b/Object.h index e73230c0..50a5f12d 100644 --- a/Object.h +++ b/Object.h @@ -10,6 +10,7 @@ in the source distribution for its full text. #include "RichString.h" #include "XAlloc.h" +#include "Macros.h" typedef struct Object_ Object; diff --git a/Panel.c b/Panel.c index 73fa7858..883b3e62 100644 --- a/Panel.c +++ b/Panel.c @@ -19,13 +19,6 @@ in the source distribution for its full text. #include #include -#ifndef MIN -#define MIN(a,b) ((a)<(b)?(a):(b)) -#endif -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif - #define KEY_CTRL(l) ((l)-'A'+1) PanelClass Panel_class = { @@ -233,7 +226,7 @@ void Panel_draw(Panel* this, bool focus) { mvhline(y, x, ' ', this->w); if (scrollH < headerLen) { RichString_printoffnVal(this->header, y, x, scrollH, - MIN(headerLen - scrollH, this->w)); + MINIMUM(headerLen - scrollH, this->w)); } attrset(CRT_colors[RESET_COLOR]); y++; @@ -244,7 +237,7 @@ void Panel_draw(Panel* this, bool focus) { this->scrollV = 0; this->needsRedraw = true; } else if (this->scrollV >= size) { - this->scrollV = MAX(size - 1, 0); + this->scrollV = MAXIMUM(size - 1, 0); this->needsRedraw = true; } // ensure selection is on screen @@ -257,7 +250,7 @@ void Panel_draw(Panel* this, bool focus) { } int first = this->scrollV; - int upTo = MIN(first + h, size); + int upTo = MINIMUM(first + h, size); int selectionColor = focus ? this->selectionColor @@ -271,7 +264,7 @@ void Panel_draw(Panel* this, bool focus) { RichString_begin(item); Object_display(itemObj, &item); int itemLen = RichString_sizeVal(item); - int amt = MIN(itemLen - scrollH, this->w); + int amt = MINIMUM(itemLen - scrollH, this->w); bool selected = (i == this->selected); if (selected) { attrset(selectionColor); @@ -306,13 +299,13 @@ void Panel_draw(Panel* this, bool focus) { mvhline(y+ this->oldSelected - first, x+0, ' ', this->w); if (scrollH < oldLen) RichString_printoffnVal(old, y+this->oldSelected - first, x, - scrollH, MIN(oldLen - scrollH, this->w)); + scrollH, MINIMUM(oldLen - scrollH, this->w)); attrset(selectionColor); mvhline(y+this->selected - first, x+0, ' ', this->w); RichString_setAttr(&new, selectionColor); if (scrollH < newLen) RichString_printoffnVal(new, y+this->selected - first, x, - scrollH, MIN(newLen - scrollH, this->w)); + scrollH, MINIMUM(newLen - scrollH, this->w)); attrset(CRT_colors[RESET_COLOR]); RichString_end(new); RichString_end(old); @@ -347,7 +340,7 @@ bool Panel_onKey(Panel* this, int key) { case KEY_LEFT: case KEY_CTRL('B'): if (this->scrollH > 0) { - this->scrollH -= MAX(CRT_scrollHAmount, 0); + this->scrollH -= MAXIMUM(CRT_scrollHAmount, 0); this->needsRedraw = true; } break; @@ -358,12 +351,12 @@ bool Panel_onKey(Panel* this, int key) { break; case KEY_PPAGE: this->selected -= (this->h - 1); - this->scrollV = MAX(0, this->scrollV - this->h + 1); + this->scrollV = MAXIMUM(0, this->scrollV - this->h + 1); this->needsRedraw = true; break; case KEY_NPAGE: this->selected += (this->h - 1); - this->scrollV = MAX(0, MIN(Vector_size(this->items) - this->h, + this->scrollV = MAXIMUM(0, MINIMUM(Vector_size(this->items) - this->h, this->scrollV + this->h - 1)); this->needsRedraw = true; break; @@ -395,7 +388,7 @@ bool Panel_onKey(Panel* this, int key) { break; case KEY_CTRL('E'): case '$': - this->scrollH = MAX(this->selectedLen - this->w, 0); + this->scrollH = MAXIMUM(this->selectedLen - this->w, 0); this->needsRedraw = true; break; default: diff --git a/Panel.h b/Panel.h index 17b65c95..480e2c0f 100644 --- a/Panel.h +++ b/Panel.h @@ -59,13 +59,6 @@ struct Panel_ { #define Panel_setDefaultBar(this_) do{ (this_)->currentBar = (this_)->defaultBar; }while(0) -#ifndef MIN -#define MIN(a,b) ((a)<(b)?(a):(b)) -#endif -#ifndef MAX -#define MAX(a,b) ((a)>(b)?(a):(b)) -#endif - #define KEY_CTRL(l) ((l)-'A'+1) extern PanelClass Panel_class; diff --git a/RichString.c b/RichString.c index 702ab329..95ddf297 100644 --- a/RichString.c +++ b/RichString.c @@ -7,17 +7,13 @@ in the source distribution for its full text. #include "RichString.h" #include "XAlloc.h" +#include "Macros.h" #include #include #define RICHSTRING_MAXLEN 350 - -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - #define charBytes(n) (sizeof(CharType) * (n)) static void RichString_extendLen(RichString* this, int len) { diff --git a/RichString.h b/RichString.h index 59b77496..5431e92a 100644 --- a/RichString.h +++ b/RichString.h @@ -56,11 +56,6 @@ typedef struct RichString_ { CharType chstr[RICHSTRING_MAXLEN+1]; } RichString; - -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - #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) diff --git a/TasksMeter.c b/TasksMeter.c index 49816bdb..0b37ab27 100644 --- a/TasksMeter.c +++ b/TasksMeter.c @@ -20,7 +20,7 @@ static void TasksMeter_updateValues(Meter* this, char* buffer, int len) { this->values[0] = pl->kernelThreads; this->values[1] = pl->userlandThreads; this->values[2] = pl->totalTasks - pl->kernelThreads - pl->userlandThreads; - this->values[3] = MIN(pl->runningTasks, pl->cpuCount); + this->values[3] = MINIMUM(pl->runningTasks, pl->cpuCount); if (pl->totalTasks > this->total) { this->total = pl->totalTasks; } diff --git a/darwin/Platform.c b/darwin/Platform.c index f8007d82..a8ca45b2 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -22,10 +22,6 @@ in the source distribution for its full text. #include -#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 }; const SignalItem Platform_signals[] = { diff --git a/darwin/Platform.h b/darwin/Platform.h index 5ce23684..7dd4ae60 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -14,10 +14,6 @@ in the source distribution for its full text. #include "BatteryMeter.h" #include "DarwinProcess.h" -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - extern ProcessField Platform_defaultFields[]; extern const SignalItem Platform_signals[]; diff --git a/dragonflybsd/DragonFlyBSDProcessList.c b/dragonflybsd/DragonFlyBSDProcessList.c index d77f12eb..08303ba6 100644 --- a/dragonflybsd/DragonFlyBSDProcessList.c +++ b/dragonflybsd/DragonFlyBSDProcessList.c @@ -97,7 +97,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui sysctl(MIB_kern_cp_times, 2, dfpl->cp_times_o, &len, NULL, 0); } - pl->cpuCount = MAX(cpus, 1); + pl->cpuCount = MAXIMUM(cpus, 1); if (cpus == 1 ) { dfpl->cpus = xRealloc(dfpl->cpus, sizeof(CPUData)); diff --git a/dragonflybsd/Platform.c b/dragonflybsd/Platform.c index 84f13707..3b8e1a0d 100644 --- a/dragonflybsd/Platform.c +++ b/dragonflybsd/Platform.c @@ -28,10 +28,6 @@ in the source distribution for its full text. #include -#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 }; int Platform_numberOfFields = LAST_PROCESSFIELD; diff --git a/dragonflybsd/Platform.h b/dragonflybsd/Platform.h index c6792a28..83c14f51 100644 --- a/dragonflybsd/Platform.h +++ b/dragonflybsd/Platform.h @@ -14,10 +14,6 @@ in the source distribution for its full text. extern ProcessFieldData Process_fields[]; -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - extern ProcessField Platform_defaultFields[]; extern int Platform_numberOfFields; diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c index 344c3314..a4deaad9 100644 --- a/freebsd/FreeBSDProcessList.c +++ b/freebsd/FreeBSDProcessList.c @@ -111,7 +111,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui sysctl(MIB_kern_cp_times, 2, fpl->cp_times_o, &len, NULL, 0); } - pl->cpuCount = MAX(cpus, 1); + pl->cpuCount = MAXIMUM(cpus, 1); if (cpus == 1 ) { fpl->cpus = xRealloc(fpl->cpus, sizeof(CPUData)); diff --git a/freebsd/Platform.c b/freebsd/Platform.c index 58a9bf1b..c51b37cb 100644 --- a/freebsd/Platform.c +++ b/freebsd/Platform.c @@ -29,10 +29,6 @@ in the source distribution for its full text. #include -#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 }; int Platform_numberOfFields = LAST_PROCESSFIELD; diff --git a/freebsd/Platform.h b/freebsd/Platform.h index fd704539..1a180551 100644 --- a/freebsd/Platform.h +++ b/freebsd/Platform.h @@ -13,10 +13,6 @@ in the source distribution for its full text. extern ProcessFieldData Process_fields[]; -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - extern ProcessField Platform_defaultFields[]; extern int Platform_numberOfFields; diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index a6fdada8..c9303a04 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -42,10 +42,6 @@ in the source distribution for its full text. #include #endif -#ifndef CLAMP -#define CLAMP(x,low,high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) -#endif - static ssize_t xread(int fd, void *buf, size_t count) { // Read some bytes. Retry on EINTR and when we don't get as many bytes as we requested. size_t alreadyRead = 0; @@ -194,7 +190,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui fclose(file); - pl->cpuCount = MAX(cpus - 1, 1); + pl->cpuCount = MAXIMUM(cpus - 1, 1); this->cpus = xCalloc(cpus, sizeof(CPUData)); for (int i = 0; i < cpus; i++) { diff --git a/linux/LinuxProcessList.h b/linux/LinuxProcessList.h index d689d4ac..aa7317ab 100644 --- a/linux/LinuxProcessList.h +++ b/linux/LinuxProcessList.h @@ -92,10 +92,6 @@ typedef struct LinuxProcessList_ { #define PROC_LINE_LENGTH 4096 #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); void ProcessList_delete(ProcessList* pl); diff --git a/linux/Platform.c b/linux/Platform.c index d48dc764..ffe5c5c2 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -33,10 +33,6 @@ in the source distribution for its full text. #include -#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, (int)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 }; diff --git a/linux/Platform.h b/linux/Platform.h index 922a3392..9f0ee7fd 100644 --- a/linux/Platform.h +++ b/linux/Platform.h @@ -13,10 +13,6 @@ 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; diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 0d1a7226..71cd5627 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -27,21 +27,6 @@ in the source distribution for its full text. #include #include -/* - * avoid relying on or conflicting with MIN() and MAX() in sys/param.h - */ -#ifndef MINIMUM -#define MINIMUM(x, y) ((x) > (y) ? (y) : (x)) -#endif - -#ifndef MAXIMUM -#define MAXIMUM(x, y) ((x) > (y) ? (x) : (y)) -#endif - -#ifndef CLAMP -#define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low)) -#endif - static long fscale; ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { diff --git a/openbsd/OpenBSDProcessList.h b/openbsd/OpenBSDProcessList.h index 038a263d..d22243f7 100644 --- a/openbsd/OpenBSDProcessList.h +++ b/openbsd/OpenBSDProcessList.h @@ -38,20 +38,6 @@ typedef struct OpenBSDProcessList_ { } OpenBSDProcessList; -/* - * avoid relying on or conflicting with MIN() and MAX() in sys/param.h - */ -#ifndef MINIMUM -#define MINIMUM(x, y) ((x) > (y) ? (y) : (x)) -#endif - -#ifndef MAXIMUM -#define MAXIMUM(x, y) ((x) > (y) ? (x) : (y)) -#endif - -#ifndef CLAMP -#define CLAMP(x, low, high) (((x) > (high)) ? (high) : MAXIMUM(x, low)) -#endif ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId); -- cgit v1.2.3