From 65357c8c46154de4e4eca14075bfe5523bb5fc14 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 7 Dec 2020 10:26:01 +0100 Subject: New upstream version 3.0.3 --- AffinityPanel.c | 106 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 42 deletions(-) (limited to 'AffinityPanel.c') diff --git a/AffinityPanel.c b/AffinityPanel.c index d0a9e6d..e491b52 100644 --- a/AffinityPanel.c +++ b/AffinityPanel.c @@ -1,29 +1,41 @@ /* htop - AffinityPanel.c (C) 2004-2011 Hisham H. Muhammad -Released under the GNU GPL, see the COPYING file +Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "AffinityPanel.h" -#include "CRT.h" +#include "config.h" // IWYU pragma: keep -#include "Vector.h" +#include "AffinityPanel.h" #include +#include +#include #include +#include "CRT.h" +#include "FunctionBar.h" +#include "Object.h" +#include "ProvideCurses.h" +#include "RichString.h" +#include "Settings.h" +#include "Vector.h" +#include "XUtils.h" + #ifdef HAVE_LIBHWLOC #include +#include #endif + typedef struct MaskItem_ { Object super; - const char* text; - const char* indent; /* used also as an condition whether this is a tree node */ + char* text; + char* indent; /* used also as an condition whether this is a tree node */ int value; /* tri-state: 0 - off, 1 - some set, 2 - all set */ int sub_tree; /* tri-state: 0 - no sub-tree, 1 - open sub-tree, 2 - closed sub-tree */ - Vector *children; + Vector* children; #ifdef HAVE_LIBHWLOC bool ownCpuset; hwloc_bitmap_t cpuset; @@ -34,9 +46,8 @@ typedef struct MaskItem_ { static void MaskItem_delete(Object* cast) { MaskItem* this = (MaskItem*) cast; - free((void*)this->text); - if (this->indent) - free((void*)this->indent); + free(this->text); + free(this->indent); Vector_delete(this->children); #ifdef HAVE_LIBHWLOC if (this->ownCpuset) @@ -45,16 +56,17 @@ static void MaskItem_delete(Object* cast) { free(this); } -static void MaskItem_display(Object* cast, RichString* out) { - MaskItem* this = (MaskItem*)cast; +static void MaskItem_display(const Object* cast, RichString* out) { + const MaskItem* this = (const MaskItem*)cast; assert (this != NULL); RichString_append(out, CRT_colors[CHECK_BOX], "["); - if (this->value == 2) + if (this->value == 2) { RichString_append(out, CRT_colors[CHECK_MARK], "x"); - else if (this->value == 1) + } else if (this->value == 1) { RichString_append(out, CRT_colors[CHECK_MARK], "o"); - else + } else { RichString_append(out, CRT_colors[CHECK_MARK], " "); + } RichString_append(out, CRT_colors[CHECK_BOX], "]"); RichString_append(out, CRT_colors[CHECK_TEXT], " "); if (this->indent) { @@ -68,7 +80,7 @@ static void MaskItem_display(Object* cast, RichString* out) { RichString_append(out, CRT_colors[CHECK_TEXT], this->text); } -static ObjectClass MaskItem_class = { +static const ObjectClass MaskItem_class = { .display = MaskItem_display, .delete = MaskItem_delete }; @@ -100,11 +112,10 @@ static MaskItem* MaskItem_newSingleton(const char* text, int cpu, bool isSet) { this->ownCpuset = true; this->cpuset = hwloc_bitmap_alloc(); hwloc_bitmap_set(this->cpuset, cpu); - (void)isSet; #else this->cpu = cpu; #endif - this->value = 2 * isSet; + this->value = isSet ? 2 : 0; return this; } @@ -113,11 +124,11 @@ typedef struct AffinityPanel_ { Panel super; ProcessList* pl; bool topoView; - Vector *cpuids; + Vector* cpuids; unsigned width; #ifdef HAVE_LIBHWLOC - MaskItem *topoRoot; + MaskItem* topoRoot; hwloc_const_cpuset_t allCpuset; hwloc_bitmap_t workCpuset; #endif @@ -162,17 +173,18 @@ static void AffinityPanel_update(AffinityPanel* this, bool keepSelected) { Panel* super = (Panel*) this; FunctionBar_setLabel(super->currentBar, KEY_F(3), this->topoView ? "Collapse/Expand" : ""); - FunctionBar_draw(super->currentBar, NULL); + FunctionBar_draw(super->currentBar); int oldSelected = Panel_getSelectedIndex(super); Panel_prune(super); #ifdef HAVE_LIBHWLOC - if (this->topoView) + if (this->topoView) { AffinityPanel_updateTopo(this, this->topoRoot); - else { - for (int i = 0; i < Vector_size(this->cpuids); i++) + } else { + for (int i = 0; i < Vector_size(this->cpuids); i++) { AffinityPanel_updateItem(this, (MaskItem*) Vector_get(this->cpuids, i)); + } } #else Panel_splice(super, this->cpuids); @@ -206,7 +218,7 @@ static HandlerResult AffinityPanel_eventHandler(Panel* super, int ch) { selected->value = 2; } #else - selected->value = 2 * !selected->value; /* toggle between 0 and 2 */ + selected->value = selected->value ? 0 : 2; /* toggle between 0 and 2 */ #endif result = HANDLED; @@ -252,7 +264,7 @@ static HandlerResult AffinityPanel_eventHandler(Panel* super, int ch) { #ifdef HAVE_LIBHWLOC -static MaskItem *AffinityPanel_addObject(AffinityPanel* this, hwloc_obj_t obj, unsigned indent, MaskItem *parent) { +static MaskItem* AffinityPanel_addObject(AffinityPanel* this, hwloc_obj_t obj, unsigned indent, MaskItem* parent) { const char* type_name = hwloc_obj_type_string(obj->type); const char* index_prefix = "#"; unsigned depth = obj->depth; @@ -271,17 +283,20 @@ static MaskItem *AffinityPanel_addObject(AffinityPanel* this, hwloc_obj_t obj, u for (unsigned i = 1; i < depth; i++) { xSnprintf(&indent_buf[off], left, "%s ", (indent & (1u << i)) ? CRT_treeStr[TREE_STR_VERT] : " "); size_t len = strlen(&indent_buf[off]); - off += len, left -= len; + off += len; + left -= len; } xSnprintf(&indent_buf[off], left, "%s", - obj->next_sibling ? CRT_treeStr[TREE_STR_RTEE] : CRT_treeStr[TREE_STR_BEND]); - size_t len = strlen(&indent_buf[off]); - off += len, left -= len; + obj->next_sibling ? CRT_treeStr[TREE_STR_RTEE] : CRT_treeStr[TREE_STR_BEND]); + // Uncomment when further appending to indent_buf + //size_t len = strlen(&indent_buf[off]); + //off += len; + //left -= len; } - xSnprintf(buf, 64, "%s %s%u", type_name, index_prefix, index); + xSnprintf(buf, sizeof(buf), "%s %s%u", type_name, index_prefix, index); - MaskItem *item = MaskItem_newMask(buf, indent_buf, obj->complete_cpuset, false); + MaskItem* item = MaskItem_newMask(buf, indent_buf, obj->complete_cpuset, false); if (parent) Vector_add(parent->children, item); @@ -291,34 +306,38 @@ static MaskItem *AffinityPanel_addObject(AffinityPanel* this, hwloc_obj_t obj, u hwloc_bitmap_and(result, obj->complete_cpuset, this->workCpuset); int weight = hwloc_bitmap_weight(result); hwloc_bitmap_free(result); - if (weight == 0 || weight == (hwloc_bitmap_weight(this->workCpuset) + hwloc_bitmap_weight(obj->complete_cpuset))) + if (weight == 0 || weight == (hwloc_bitmap_weight(this->workCpuset) + hwloc_bitmap_weight(obj->complete_cpuset))) { item->sub_tree = 2; + } } /* "[x] " + "|- " * depth + ("- ")?(if root node) + name */ unsigned width = 4 + 3 * depth + (2 * !depth) + strlen(buf); - if (width > this->width) + if (width > this->width) { this->width = width; + } return item; } -static MaskItem *AffinityPanel_buildTopology(AffinityPanel* this, hwloc_obj_t obj, unsigned indent, MaskItem *parent) { - MaskItem *item = AffinityPanel_addObject(this, obj, indent, parent); +static MaskItem* AffinityPanel_buildTopology(AffinityPanel* this, hwloc_obj_t obj, unsigned indent, MaskItem* parent) { + MaskItem* item = AffinityPanel_addObject(this, obj, indent, parent); if (obj->next_sibling) { indent |= (1u << obj->depth); } else { indent &= ~(1u << obj->depth); } - for (unsigned i = 0; i < obj->arity; i++) + + for (unsigned i = 0; i < obj->arity; i++) { AffinityPanel_buildTopology(this, obj->children[i], indent, item); + } return parent == NULL ? item : NULL; } #endif -PanelClass AffinityPanel_class = { +const PanelClass AffinityPanel_class = { .super = { .extends = Class(Panel), .delete = AffinityPanel_delete @@ -369,8 +388,9 @@ Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity, int* width) { char number[16]; xSnprintf(number, 9, "CPU %d", Settings_cpuId(pl->settings, i)); unsigned cpu_width = 4 + strlen(number); - if (cpu_width > this->width) + if (cpu_width > this->width) { this->width = cpu_width; + } bool isSet = false; if (curCpu < affinity->used && affinity->cpus[curCpu] == i) { @@ -389,8 +409,9 @@ Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity, int* width) { this->topoRoot = AffinityPanel_buildTopology(this, hwloc_get_root_obj(pl->topology), 0, NULL); #endif - if (width) + if (width) { *width = this->width; + } AffinityPanel_update(this, false); @@ -404,13 +425,14 @@ Affinity* AffinityPanel_getAffinity(Panel* super, ProcessList* pl) { #ifdef HAVE_LIBHWLOC int i; hwloc_bitmap_foreach_begin(i, this->workCpuset) - Affinity_add(affinity, i); + Affinity_add(affinity, i); hwloc_bitmap_foreach_end(); #else for (int i = 0; i < this->pl->cpuCount; i++) { MaskItem* item = (MaskItem*)Vector_get(this->cpuids, i); - if (item->value) + if (item->value) { Affinity_add(affinity, item->cpu); + } } #endif -- cgit v1.2.3