From f3306db9d1cacab0713b67690f4cde47e0c4b228 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Tue, 3 Sep 2024 17:39:17 +1000 Subject: [PATCH] Pass Machine pointer to Settings_new instead of internal detail Be more flexible and consistent with the way other structures are initialised by passing a pointer to struct Machine into the Settings constructor. This would allow other alternative setup of default Meters in the future, e.g. for some nvtop-alike tool sharing code with htop (hypothetically) showing GPUs instead of CPUs by default. Mainly it just makes the code easier to read. --- CommandLine.c | 2 +- Settings.c | 17 +++++++++-------- Settings.h | 3 ++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CommandLine.c b/CommandLine.c index 69b80c1e5..b93a0f969 100644 --- a/CommandLine.c +++ b/CommandLine.c @@ -342,7 +342,7 @@ int CommandLine_run(int argc, char** argv) { Machine* host = Machine_new(ut, flags.userId); ProcessTable* pt = ProcessTable_new(host, flags.pidMatchList); - Settings* settings = Settings_new(host->activeCPUs, dm, dc, ds); + Settings* settings = Settings_new(host, dm, dc, ds); Machine_populateTablesFromSettings(host, settings, &pt->super); Header* header = Header_new(host, 2); diff --git a/Settings.c b/Settings.c index 56649acaf..43aee3784 100644 --- a/Settings.c +++ b/Settings.c @@ -153,7 +153,8 @@ static bool Settings_validateMeters(Settings* this) { return anyMeter; } -static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount) { +static void Settings_defaultMeters(Settings* this, const Machine* host) { + unsigned int initialCpuCount = host->activeCPUs; int sizes[] = { 3, 3 }; if (initialCpuCount > 4 && initialCpuCount <= 128) { @@ -357,7 +358,7 @@ static ScreenSettings* Settings_defaultScreens(Settings* this) { return this->screens[0]; } -static bool Settings_read(Settings* this, const char* fileName, unsigned int initialCpuCount, bool checkWritability) { +static bool Settings_read(Settings* this, const char* fileName, const Machine* host, bool checkWritability) { int fd = -1; const char* fopen_mode = "r+"; if (checkWritability) { @@ -595,7 +596,7 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini } fclose(fp); if (!didReadMeters || !Settings_validateMeters(this)) - Settings_defaultMeters(this, initialCpuCount); + Settings_defaultMeters(this, host); if (!this->nScreens) Settings_defaultScreens(this); return didReadAny; @@ -814,7 +815,7 @@ int Settings_write(const Settings* this, bool onCrash) { return r; } -Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens) { +Settings* Settings_new(const Machine* host, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens) { Settings* this = xCalloc(1, sizeof(Settings)); this->writeConfig = true; @@ -900,9 +901,9 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, H this->changed = false; this->delay = DEFAULT_DELAY; - bool ok = Settings_read(this, this->filename, initialCpuCount, /*checkWritability*/true); + bool ok = Settings_read(this, this->filename, host, /*checkWritability*/true); if (!ok && legacyDotfile) { - ok = Settings_read(this, legacyDotfile, initialCpuCount, this->writeConfig); + ok = Settings_read(this, legacyDotfile, host, this->writeConfig); if (ok && this->writeConfig) { // Transition to new location and delete old configuration file if (Settings_write(this, false) == 0) { @@ -914,10 +915,10 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, H this->screenTabs = true; this->changed = true; - ok = Settings_read(this, SYSCONFDIR "/htoprc", initialCpuCount, /*checkWritability*/false); + ok = Settings_read(this, SYSCONFDIR "/htoprc", host, /*checkWritability*/false); } if (!ok) { - Settings_defaultMeters(this, initialCpuCount); + Settings_defaultMeters(this, host); Settings_defaultScreens(this); } diff --git a/Settings.h b/Settings.h index 9d9e0dba7..5d17fb346 100644 --- a/Settings.h +++ b/Settings.h @@ -23,6 +23,7 @@ in the source distribution for its full text. #define CONFIG_READER_MIN_VERSION 3 struct DynamicScreen_; // IWYU pragma: keep +struct Machine_; // IWYU pragma: keep struct Table_; // IWYU pragma: keep typedef struct { @@ -128,7 +129,7 @@ void Settings_delete(Settings* this); int Settings_write(const Settings* this, bool onCrash); -Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens); +Settings* Settings_new(const struct Machine_* host, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens); ScreenSettings* Settings_newScreen(Settings* this, const ScreenDefaults* defaults);