From 9207401f9711ba026a70d7dd47347d7eab168b3f Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sat, 12 Sep 2020 18:25:25 -0300 Subject: [PATCH 1/4] Clean up some code duplication in the header files PR htop-dev/htop#70 got rid of the infrastructure for generating header files, but it left behind some code duplication. Some of cases are things that belong in the header file and don't need to be repeated in the C file. Other cases are things that belong in the C file and don't need to be in the header file. In this commit I tried to fix all of these that I could find. When given a choice I preferred keeping things out of the header file, unless they were being used by someone else. --- Action.h | 4 ---- Affinity.h | 8 -------- CRT.c | 11 +---------- CRT.h | 27 +-------------------------- ColorsPanel.h | 6 ------ Header.c | 2 -- IncSet.c | 1 - Meter.c | 4 ---- Meter.h | 25 ++----------------------- MetersPanel.h | 8 -------- Panel.c | 2 -- Process.c | 23 ----------------------- Process.h | 4 ---- RichString.c | 2 -- RichString.h | 7 ++----- Settings.c | 2 -- StringUtils.c | 6 ------ TraceScreen.c | 1 - Vector.c | 4 ++-- Vector.h | 4 ---- 20 files changed, 8 insertions(+), 143 deletions(-) diff --git a/Action.h b/Action.h index cf0169c4..8e5cc9b6 100644 --- a/Action.h +++ b/Action.h @@ -37,14 +37,10 @@ typedef struct State_ { Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess); -// ---------------------------------------- - bool Action_setUserOnly(const char* userName, uid_t* userId); Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey); -// ---------------------------------------- - Htop_Reaction Action_follow(State* st); void Action_setBindings(Htop_Action* keys); diff --git a/Affinity.h b/Affinity.h index 6bdd9750..52619457 100644 --- a/Affinity.h +++ b/Affinity.h @@ -8,14 +8,6 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#ifdef HAVE_LIBHWLOC -#if __linux__ -#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_THREAD -#else -#define HTOP_HWLOC_CPUBIND_FLAG HWLOC_CPUBIND_PROCESS -#endif -#endif - #include "Process.h" #include "ProcessList.h" diff --git a/CRT.c b/CRT.c index 2a7c7912..7776c4e7 100644 --- a/CRT.c +++ b/CRT.c @@ -39,10 +39,6 @@ in the source distribution for its full text. #define ColorPairGrayBlack ColorPair(Magenta,Magenta) #define ColorIndexGrayBlack ColorIndex(Magenta,Magenta) -#define KEY_WHEELUP KEY_F(20) -#define KEY_WHEELDOWN KEY_F(21) -#define KEY_RECLICK KEY_F(22) - const char *CRT_treeStrAscii[TREE_STR_COUNT] = { "-", // TREE_STR_HORZ "|", // TREE_STR_VERT @@ -550,12 +546,7 @@ void CRT_restorePrivileges() { #else -/* Turn setuid operations into NOPs */ - -#ifndef CRT_dropPrivileges -#define CRT_dropPrivileges() -#define CRT_restorePrivileges() -#endif +// In this case, the setuid operations are defined as macros in CRT.h #endif diff --git a/CRT.h b/CRT.h index 2e21e91e..80d1eb71 100644 --- a/CRT.h +++ b/CRT.h @@ -7,31 +7,12 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#if HAVE_SETUID_ENABLED -#endif - -#define ColorIndex(i,j) ((7-i)*8+j) - -#define ColorPair(i,j) COLOR_PAIR(ColorIndex(i,j)) - -#define Black COLOR_BLACK -#define Red COLOR_RED -#define Green COLOR_GREEN -#define Yellow COLOR_YELLOW -#define Blue COLOR_BLUE -#define Magenta COLOR_MAGENTA -#define Cyan COLOR_CYAN -#define White COLOR_WHITE - -#define ColorPairGrayBlack ColorPair(Magenta,Magenta) -#define ColorIndexGrayBlack ColorIndex(Magenta,Magenta) +#include #define KEY_WHEELUP KEY_F(20) #define KEY_WHEELDOWN KEY_F(21) #define KEY_RECLICK KEY_F(22) -#include - typedef enum TreeStr_ { TREE_STR_HORZ, TREE_STR_VERT, @@ -158,16 +139,12 @@ extern int CRT_scrollWheelVAmount; extern char* CRT_termType; -// TODO move color scheme to Settings, perhaps? - extern int CRT_colorScheme; extern void *backtraceArray[128]; #if HAVE_SETUID_ENABLED -#define DIE(msg) do { CRT_done(); fprintf(stderr, msg); exit(1); } while(0) - void CRT_dropPrivileges(); void CRT_restorePrivileges(); @@ -183,8 +160,6 @@ void CRT_restorePrivileges(); #endif -// TODO: pass an instance of Settings instead. - void CRT_init(int delay, int colorScheme, bool allowUnicode); void CRT_done(); diff --git a/ColorsPanel.h b/ColorsPanel.h index 9977dc0b..479fad08 100644 --- a/ColorsPanel.h +++ b/ColorsPanel.h @@ -7,12 +7,6 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -// TO ADD A NEW SCHEME: -// * Increment the size of bool check in ColorsPanel.h -// * Add the entry in the ColorSchemeNames array below in the file -// * Add a define in CRT.h that matches the order of the array -// * Add the colors in CRT_setColors - #include "Panel.h" #include "Settings.h" #include "ScreenManager.h" diff --git a/Header.c b/Header.c index 574165a6..d2f3b884 100644 --- a/Header.c +++ b/Header.c @@ -16,8 +16,6 @@ in the source distribution for its full text. #include #include -#define Header_forEachColumn(this_, i_) for (int (i_)=0; (i_) < (this_)->nrColumns; ++(i_)) - Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns) { Header* this = xCalloc(1, sizeof(Header)); this->columns = xCalloc(nrColumns, sizeof(Vector*)); diff --git a/IncSet.c b/IncSet.c index 90ce8b9f..e9e28934 100644 --- a/IncSet.c +++ b/IncSet.c @@ -7,7 +7,6 @@ in the source distribution for its full text. #include "IncSet.h" #include "StringUtils.h" -#include "Panel.h" #include "ListItem.h" #include "CRT.h" #include diff --git a/Meter.c b/Meter.c index 34f428ff..00c49cee 100644 --- a/Meter.c +++ b/Meter.c @@ -11,7 +11,6 @@ in the source distribution for its full text. #include "Object.h" #include "CRT.h" #include "StringUtils.h" -#include "ListItem.h" #include "Settings.h" #include @@ -19,9 +18,6 @@ in the source distribution for its full text. #include #include #include -#include - -#define METER_BUFFER_LEN 256 #define GRAPH_DELAY (DEFAULT_DELAY/2) diff --git a/Meter.h b/Meter.h index 773d94ff..c4d00396 100644 --- a/Meter.h +++ b/Meter.h @@ -7,16 +7,11 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#define METER_BUFFER_LEN 256 - -#define GRAPH_DELAY (DEFAULT_DELAY/2) - -#define GRAPH_HEIGHT 4 /* Unit: rows (lines) */ - #include "ListItem.h" - #include +#define METER_BUFFER_LEN 256 + typedef struct Meter_ Meter; typedef void(*Meter_Init)(Meter*); @@ -108,24 +103,8 @@ void Meter_setMode(Meter* this, int modeIndex); ListItem* Meter_toListItem(Meter* this, bool moving); -/* ---------- TextMeterMode ---------- */ - -/* ---------- BarMeterMode ---------- */ - -/* ---------- GraphMeterMode ---------- */ - -#ifdef HAVE_LIBNCURSESW -#define PIXPERROW_UTF8 4 -#endif - -#define PIXPERROW_ASCII 2 - -/* ---------- LEDMeterMode ---------- */ - extern MeterMode* Meter_modes[]; -/* Blank meter */ - extern int BlankMeter_attributes[]; extern MeterClass BlankMeter_class; diff --git a/MetersPanel.h b/MetersPanel.h index bea9238d..c02ce1cd 100644 --- a/MetersPanel.h +++ b/MetersPanel.h @@ -24,14 +24,6 @@ struct MetersPanel_ { bool moving; }; - -// Note: In code the meters are known to have bar/text/graph "Modes", but in UI -// we call them "Styles". -// We avoid UTF-8 arrows ← → here as they might display full-width on Chinese -// terminals, breaking our aligning. -// In , arrows (U+2019..U+2199) are -// considered "Ambiguous characters". - void MetersPanel_setMoving(MetersPanel* this, bool moving); extern PanelClass MetersPanel_class; diff --git a/Panel.c b/Panel.c index 883b3e62..b77483d9 100644 --- a/Panel.c +++ b/Panel.c @@ -19,8 +19,6 @@ in the source distribution for its full text. #include #include -#define KEY_CTRL(l) ((l)-'A'+1) - PanelClass Panel_class = { .super = { .extends = Class(Object), diff --git a/Process.c b/Process.c index e7ab4929..7a04c0f9 100644 --- a/Process.c +++ b/Process.c @@ -21,7 +21,6 @@ in the source distribution for its full text. #include #include #include -#include #include #include #include @@ -37,30 +36,8 @@ in the source distribution for its full text. #include #endif -#ifdef __ANDROID__ -#define SYS_ioprio_get __NR_ioprio_get -#define SYS_ioprio_set __NR_ioprio_set -#endif - -// On Linux, this works only with glibc 2.1+. On earlier versions -// the behavior is similar to have a hardcoded page size. -#ifndef PAGE_SIZE -#define PAGE_SIZE ( sysconf(_SC_PAGESIZE) ) -#endif -#define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K ) - static int Process_getuid = -1; -#define ONE_K 1024L -#define ONE_M (ONE_K * ONE_K) -#define ONE_G (ONE_M * ONE_K) -#define ONE_T ((long long)ONE_G * ONE_K) - -#define ONE_DECIMAL_K 1000L -#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K) -#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K) -#define ONE_DECIMAL_T ((long long)ONE_DECIMAL_G * ONE_DECIMAL_K) - char Process_pidFormat[20] = "%7d "; static char Process_titleBuffer[20][20]; diff --git a/Process.h b/Process.h index 9aca41c9..ebf5cdf4 100644 --- a/Process.h +++ b/Process.h @@ -8,10 +8,6 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#ifdef MAJOR_IN_MKDEV -#elif defined(MAJOR_IN_SYSMACROS) -#endif - #ifdef __ANDROID__ #define SYS_ioprio_get __NR_ioprio_get #define SYS_ioprio_set __NR_ioprio_set diff --git a/RichString.c b/RichString.c index 95ddf297..b97e34e5 100644 --- a/RichString.c +++ b/RichString.c @@ -12,8 +12,6 @@ in the source distribution for its full text. #include #include -#define RICHSTRING_MAXLEN 350 - #define charBytes(n) (sizeof(CharType) * (n)) static void RichString_extendLen(RichString* this, int len) { diff --git a/RichString.h b/RichString.h index 5431e92a..e6fb4c32 100644 --- a/RichString.h +++ b/RichString.h @@ -7,7 +7,6 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#define RICHSTRING_MAXLEN 350 #include "config.h" #include @@ -50,16 +49,14 @@ in the source distribution for its full text. #define CharType chtype #endif +#define RICHSTRING_MAXLEN 350 + typedef struct RichString_ { int chlen; CharType* chptr; CharType chstr[RICHSTRING_MAXLEN+1]; } RichString; -#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) - #ifdef HAVE_LIBNCURSESW void RichString_setAttrn(RichString* this, int attrs, int start, int finish); diff --git a/Settings.c b/Settings.c index 104d9bb9..c9c75d4a 100644 --- a/Settings.c +++ b/Settings.c @@ -17,8 +17,6 @@ in the source distribution for its full text. #include #include -#define DEFAULT_DELAY 15 - void Settings_delete(Settings* this) { free(this->filename); free(this->fields); diff --git a/StringUtils.c b/StringUtils.c index 6bae8227..e2386e3c 100644 --- a/StringUtils.c +++ b/StringUtils.c @@ -14,12 +14,6 @@ in the source distribution for its full text. #include #include - -/* - * String_startsWith gives better performance if strlen(match) can be computed - * at compile time (e.g. when they are immutable string literals). :) - */ - char* String_cat(const char* s1, const char* s2) { int l1 = strlen(s1); int l2 = strlen(s2); diff --git a/TraceScreen.c b/TraceScreen.c index 99b56a05..0a3485ec 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -8,7 +8,6 @@ in the source distribution for its full text. #include "TraceScreen.h" #include "CRT.h" -#include "InfoScreen.h" #include "ProcessList.h" #include "ListItem.h" #include "IncSet.h" diff --git a/Vector.c b/Vector.c index e7776ea3..e51fd7b1 100644 --- a/Vector.c +++ b/Vector.c @@ -261,7 +261,7 @@ inline Object* Vector_get(Vector* this, int idx) { #else -#define Vector_get(v_, idx_) ((v_)->array[idx_]) +// In this case, Vector_get is defined as a macro in vector.h #endif @@ -274,7 +274,7 @@ inline int Vector_size(Vector* this) { #else -#define Vector_size(v_) ((v_)->items) +// In this case, Vector_size is defined as a macro in vector.h #endif diff --git a/Vector.h b/Vector.h index 32cd809e..209e27cf 100644 --- a/Vector.h +++ b/Vector.h @@ -72,10 +72,6 @@ int Vector_size(Vector* this); #endif -/* - -*/ - void Vector_add(Vector* this, void* data_); int Vector_indexOf(Vector* this, void* search_, Object_Compare compare); From 5ad3c11eaad3f75b09569758e0b67d18933a6278 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sat, 12 Sep 2020 19:16:07 -0300 Subject: [PATCH 2/4] Alignment tweak --- CRT.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CRT.h b/CRT.h index 80d1eb71..8a5d6ac6 100644 --- a/CRT.h +++ b/CRT.h @@ -9,9 +9,9 @@ in the source distribution for its full text. #include -#define KEY_WHEELUP KEY_F(20) +#define KEY_WHEELUP KEY_F(20) #define KEY_WHEELDOWN KEY_F(21) -#define KEY_RECLICK KEY_F(22) +#define KEY_RECLICK KEY_F(22) typedef enum TreeStr_ { TREE_STR_HORZ, From a2ef400e4354125183015700fc859300ec63c477 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sat, 12 Sep 2020 19:18:23 -0300 Subject: [PATCH 3/4] Merge identical ifdefs in Affinity.h --- Affinity.h | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Affinity.h b/Affinity.h index 52619457..20d00470 100644 --- a/Affinity.h +++ b/Affinity.h @@ -24,13 +24,7 @@ void Affinity_delete(Affinity* this); void Affinity_add(Affinity* this, int id); -#ifdef HAVE_LIBHWLOC - -Affinity* Affinity_get(Process* proc, ProcessList* pl); - -bool Affinity_set(Process* proc, Arg arg); - -#elif HAVE_LINUX_AFFINITY +#if defined(HAVE_LIBHWLOC) || defined(HAVE_LINUX_AFFINITY) Affinity* Affinity_get(Process* proc, ProcessList* pl); From b6828d7b8676a8a0e51f8fa2be0dc51977a451e4 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sat, 12 Sep 2020 23:32:31 -0300 Subject: [PATCH 4/4] Remove some unused #includes As suggested by cppclean. --- Action.h | 7 +++---- MainPanel.h | 4 ++-- ScreenManager.h | 1 - 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Action.h b/Action.h index 8e5cc9b6..0ec0537f 100644 --- a/Action.h +++ b/Action.h @@ -7,12 +7,11 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#include "IncSet.h" -#include "Settings.h" #include "Header.h" -#include "UsersTable.h" -#include "ProcessList.h" #include "Panel.h" +#include "ProcessList.h" +#include "Settings.h" +#include "UsersTable.h" typedef enum { HTOP_OK = 0x00, diff --git a/MainPanel.h b/MainPanel.h index 582dd2ec..ca064722 100644 --- a/MainPanel.h +++ b/MainPanel.h @@ -8,9 +8,9 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#include "Panel.h" #include "Action.h" -#include "Settings.h" +#include "IncSet.h" +#include "Panel.h" typedef struct MainPanel_ { Panel super; diff --git a/ScreenManager.h b/ScreenManager.h index 69d983a0..3c19a05d 100644 --- a/ScreenManager.h +++ b/ScreenManager.h @@ -7,7 +7,6 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#include "FunctionBar.h" #include "Vector.h" #include "Header.h" #include "Settings.h"