Skip to content

Commit

Permalink
Merge 'RowField' and 'ProcessField' types to 'FieldID'
Browse files Browse the repository at this point in the history
Both types are integer identifiers for fields. As the dynamic columns
and process fields share the same identifier space, it makes no sense to
split the types to two.

This commit also frees up the 'Field' name so it can refer to an object
or class rather than an ID in the future.

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Aug 31, 2023
1 parent 214166a commit 6d091a8
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 81 deletions.
10 changes: 6 additions & 4 deletions Action.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ in the source distribution for its full text.

#include "Action.h"

#include <assert.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdlib.h>
Expand Down Expand Up @@ -158,8 +159,8 @@ static bool collapseIntoParent(Panel* panel) {
return false;
}

Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey) {
ScreenSettings_setSortKey(settings->ss, (RowField) sortKey);
Htop_Reaction Action_setSortKey(Settings* settings, FieldID sortKey) {
ScreenSettings_setSortKey(settings->ss, sortKey);
return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_UPDATE_PANELHDR | HTOP_KEEP_FOLLOWING;
}

Expand All @@ -182,7 +183,7 @@ static Htop_Reaction actionSetSortColumn(State* st) {
Panel_setHeader(sortPanel, "Sort by");
Machine* host = st->host;
Settings* settings = host->settings;
const RowField* fields = settings->ss->fields;
const FieldID* fields = settings->ss->fields;
Hashtable* dynamicColumns = settings->dynamicColumns;
for (int i = 0; fields[i]; i++) {
char* name = NULL;
Expand All @@ -202,7 +203,8 @@ static Htop_Reaction actionSetSortColumn(State* st) {
}
const ListItem* field = (const ListItem*) Action_pickFromVector(st, sortPanel, 14, false);
if (field) {
reaction |= Action_setSortKey(settings, field->key);
assert(sizeof(field->key) <= sizeof(FieldID));
reaction |= Action_setSortKey(settings, (FieldID) field->key);
}
Object_delete(sortPanel);

Expand Down
2 changes: 1 addition & 1 deletion Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Object* Action_pickFromVector(State* st, Panel* list, int x, bool follow);

bool Action_setUserOnly(const char* userName, uid_t* userId);

Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey);
Htop_Reaction Action_setSortKey(Settings* settings, FieldID sortKey);

Htop_Reaction Action_setScreenTab(State* st, int x);

Expand Down
4 changes: 2 additions & 2 deletions ColumnsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static void ColumnsPanel_add(Panel* super, unsigned int key, Hashtable* columns)
void ColumnsPanel_fill(ColumnsPanel* this, ScreenSettings* ss, Hashtable* columns) {
Panel* super = (Panel*) this;
Panel_prune(super);
for (const RowField* fields = ss->fields; *fields; fields++)
for (const FieldID* fields = ss->fields; *fields; fields++)
ColumnsPanel_add(super, *fields, columns);
this->ss = ss;
}
Expand All @@ -165,7 +165,7 @@ void ColumnsPanel_update(Panel* super) {
ColumnsPanel* this = (ColumnsPanel*) super;
int size = Panel_size(super);
*(this->changed) = true;
this->ss->fields = xRealloc(this->ss->fields, sizeof(ProcessField) * (size + 1));
this->ss->fields = xRealloc(this->ss->fields, sizeof(*this->ss->fields) * (size + 1));
this->ss->flags = 0;
for (int i = 0; i < size; i++) {
int key = ((ListItem*) Panel_get(super, i))->key;
Expand Down
2 changes: 1 addition & 1 deletion MainPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static HandlerResult MainPanel_eventHandler(Panel* super, int ch) {
if (EVENT_IS_HEADER_CLICK(ch)) {
int x = EVENT_HEADER_CLICK_GET_X(ch);
int hx = super->scrollH + x + 1;
RowField field = RowField_keyAt(settings, hx);
FieldID field = RowField_keyAt(settings, hx);
if (ss->treeView && ss->treeViewAlwaysByPID) {
ss->treeView = false;
ss->direction = 1;
Expand Down
8 changes: 4 additions & 4 deletions Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,13 @@ static inline char processStateChar(ProcessState state) {
}
}

static void Process_rowWriteField(const Row* super, RichString* str, RowField field) {
static void Process_rowWriteField(const Row* super, RichString* str, FieldID field) {
const Process* this = (const Process*) super;
assert(Object_isA((const Object*) this, (const ObjectClass*) &Process_class));
Process_writeField(this, str, field);
}

void Process_writeField(const Process* this, RichString* str, RowField field) {
void Process_writeField(const Process* this, RichString* str, FieldID field) {
char buffer[256];
size_t n = sizeof(buffer);
int attr = CRT_colors[DEFAULT_COLOR];
Expand Down Expand Up @@ -887,7 +887,7 @@ int Process_compare(const void* v1, const void* v2) {

const ScreenSettings* ss = p1->super.host->settings->ss;

ProcessField key = ScreenSettings_getActiveSortKey(ss);
FieldID key = ScreenSettings_getActiveSortKey(ss);

int result = Process_compareByKey(p1, p2, key);

Expand All @@ -907,7 +907,7 @@ int Process_compareByParent(const Row* r1, const Row* r2) {
return Process_compare(r1, r2);
}

int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key) {
int Process_compareByKey_Base(const Process* p1, const Process* p2, FieldID key) {
int r;

switch (key) {
Expand Down
7 changes: 3 additions & 4 deletions Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,9 @@ typedef struct ProcessFieldData_ {
} ProcessFieldData;

#define LAST_PROCESSFIELD LAST_RESERVED_FIELD
typedef int32_t ProcessField; /* see ReservedField list in RowField.h */

// Implemented in platform-specific code:
void Process_writeField(const Process* row, RichString* str, ProcessField field);
void Process_writeField(const Process* row, RichString* str, FieldID field);
int Process_compare(const void* v1, const void* v2);
int Process_compareByParent(const Row* r1, const Row* v2);
void Process_delete(Object* cast);
Expand All @@ -224,7 +223,7 @@ extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD];
#define Process_uidDigits Row_uidDigits

typedef Process* (*Process_New)(const struct Machine_*);
typedef int (*Process_CompareByKey)(const Process*, const Process*, ProcessField);
typedef int (*Process_CompareByKey)(const Process*, const Process*, FieldID);

typedef struct ProcessClass_ {
const RowClass super;
Expand Down Expand Up @@ -308,7 +307,7 @@ static inline int Process_pidEqualCompare(const void* v1, const void* v2) {
return Row_idEqualCompare(v1, v2);
}

int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key);
int Process_compareByKey_Base(const Process* p1, const Process* p2, FieldID key);

const char* Process_getCommand(const Process* this);

Expand Down
40 changes: 20 additions & 20 deletions Row.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static inline bool Row_isTomb(const Row* this) {
void Row_display(const Object* cast, RichString* out) {
const Row* this = (const Row*) cast;
const Settings* settings = this->host->settings;
const RowField* fields = settings->ss->fields;
const FieldID* fields = settings->ss->fields;

for (int i = 0; fields[i]; i++)
As_Row(this)->writeField(this, out, fields[i]);
Expand Down Expand Up @@ -115,7 +115,7 @@ void Row_resetFieldWidths(void) {
}
}

void Row_updateFieldWidth(RowField key, size_t width) {
void Row_updateFieldWidth(FieldID key, size_t width) {
if (width > UINT8_MAX)
Row_fieldWidths[key] = UINT8_MAX;
else if (width > Row_fieldWidths[key])
Expand All @@ -137,52 +137,52 @@ static const char* alignedTitleDynamicColumn(const Settings* settings, int key,
}

// helper function to fill an aligned title string for a process field
static const char* alignedTitleProcessField(ProcessField field, char* titleBuffer, size_t titleBufferSize) {
const char* title = Process_fields[field].title;
static const char* alignedTitleProcessField(FieldID fieldId, char* titleBuffer, size_t titleBufferSize) {
const char* title = Process_fields[fieldId].title;
if (!title)
return "- ";

if (Process_fields[field].pidColumn) {
if (Process_fields[fieldId].pidColumn) {
xSnprintf(titleBuffer, titleBufferSize, "%*s ", Row_pidDigits, title);
return titleBuffer;
}

if (field == ST_UID) {
if (fieldId == ST_UID) {
xSnprintf(titleBuffer, titleBufferSize, "%*s ", Row_uidDigits, title);
return titleBuffer;
}

if (Process_fields[field].autoWidth) {
if (field == PERCENT_CPU)
xSnprintf(titleBuffer, titleBufferSize, "%*s ", Row_fieldWidths[field], title);
if (Process_fields[fieldId].autoWidth) {
if (fieldId == PERCENT_CPU)
xSnprintf(titleBuffer, titleBufferSize, "%*s ", Row_fieldWidths[fieldId], title);
else
xSnprintf(titleBuffer, titleBufferSize, "%-*.*s ", Row_fieldWidths[field], Row_fieldWidths[field], title);
xSnprintf(titleBuffer, titleBufferSize, "%-*.*s ", Row_fieldWidths[fieldId], Row_fieldWidths[fieldId], title);
return titleBuffer;
}

return title;
}

// helper function to create an aligned title string for a given field
const char* RowField_alignedTitle(const Settings* settings, RowField field) {
const char* RowField_alignedTitle(const Settings* settings, FieldID fieldId) {
static char titleBuffer[UINT8_MAX + sizeof(" ")];
assert(sizeof(titleBuffer) >= DYNAMIC_MAX_COLUMN_WIDTH + sizeof(" "));
assert(sizeof(titleBuffer) >= ROW_MAX_PID_DIGITS + sizeof(" "));
assert(sizeof(titleBuffer) >= ROW_MAX_UID_DIGITS + sizeof(" "));

if (field < LAST_PROCESSFIELD)
return alignedTitleProcessField((ProcessField)field, titleBuffer, sizeof(titleBuffer));
return alignedTitleDynamicColumn(settings, field, titleBuffer, sizeof(titleBuffer));
if (fieldId < LAST_PROCESSFIELD)
return alignedTitleProcessField(fieldId, titleBuffer, sizeof(titleBuffer));
return alignedTitleDynamicColumn(settings, fieldId, titleBuffer, sizeof(titleBuffer));
}

RowField RowField_keyAt(const Settings* settings, int at) {
const RowField* fields = (const RowField*) settings->ss->fields;
RowField field;
FieldID RowField_keyAt(const Settings* settings, int at) {
const FieldID* fields = settings->ss->fields;
FieldID key;
int x = 0;
for (int i = 0; (field = fields[i]); i++) {
int len = strlen(RowField_alignedTitle(settings, field));
for (int i = 0; (key = fields[i]); i++) {
int len = strlen(RowField_alignedTitle(settings, key));
if (at >= x && at <= x + len) {
return field;
return key;
}
x += len;
}
Expand Down
8 changes: 4 additions & 4 deletions Row.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ typedef struct Row_ {
} Row;

typedef Row* (*Row_New)(const struct Machine_*);
typedef void (*Row_WriteField)(const Row*, RichString*, RowField);
typedef void (*Row_WriteField)(const Row*, RichString*, FieldID);
typedef bool (*Row_IsHighlighted)(const Row*);
typedef bool (*Row_IsVisible)(const Row*, const struct Table_*);
typedef bool (*Row_MatchesFilter)(const Row*, const struct Table_*);
Expand Down Expand Up @@ -128,13 +128,13 @@ void Row_toggleTag(Row* this);

void Row_resetFieldWidths(void);

void Row_updateFieldWidth(RowField key, size_t width);
void Row_updateFieldWidth(FieldID key, size_t width);

void Row_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width);

const char* RowField_alignedTitle(const struct Settings_* settings, RowField field);
const char* RowField_alignedTitle(const struct Settings_* settings, FieldID fieldId);

RowField RowField_keyAt(const struct Settings_* settings, int at);
FieldID RowField_keyAt(const struct Settings_* settings, int at);

/* Sets the size of the PID column based on the passed PID */
void Row_setPidColumnWidth(pid_t maxPid);
Expand Down
2 changes: 1 addition & 1 deletion RowField.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ typedef enum ReservedFields_ {

/* Follow ReservedField entries with dynamic fields defined at runtime */
#define ROW_DYNAMIC_FIELDS LAST_RESERVED_FIELD
typedef int32_t RowField;
typedef int32_t FieldID;

#endif
16 changes: 8 additions & 8 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ static void ScreenSettings_readFields(ScreenSettings* ss, Hashtable* columns, co
free(trim);

/* reset default fields */
memset(ss->fields, '\0', LAST_PROCESSFIELD * sizeof(ProcessField));
memset(ss->fields, '\0', LAST_PROCESSFIELD * sizeof(*ss->fields));

for (size_t j = 0, i = 0; ids[i]; i++) {
if (j >= UINT_MAX / sizeof(ProcessField))
if (j >= UINT_MAX / sizeof(*ss->fields))
continue;
if (j >= LAST_PROCESSFIELD) {
ss->fields = xRealloc(ss->fields, (j + 1) * sizeof(ProcessField));
memset(&ss->fields[j], 0, sizeof(ProcessField));
ss->fields = xRealloc(ss->fields, (j + 1) * sizeof(*ss->fields));
memset(&ss->fields[j], 0, sizeof(*ss->fields));
}
int id = toFieldIndex(columns, ids[i]);
if (id >= 0)
Expand Down Expand Up @@ -301,7 +301,7 @@ ScreenSettings* Settings_newScreen(Settings* this, const ScreenDefaults* default
.heading = xStrdup(defaults->name),
.dynamic = NULL,
.table = NULL,
.fields = xCalloc(LAST_PROCESSFIELD, sizeof(ProcessField)),
.fields = xCalloc(LAST_PROCESSFIELD, sizeof(*ss->fields)),
.flags = 0,
.direction = sortDesc ? -1 : 1,
.treeDirection = 1,
Expand All @@ -322,7 +322,7 @@ ScreenSettings* Settings_newDynamicScreen(Settings* this, const char* tab, const
.heading = xStrdup(tab),
.dynamic = xStrdup(screen->name),
.table = table,
.fields = xCalloc(LAST_PROCESSFIELD, sizeof(ProcessField)),
.fields = xCalloc(LAST_PROCESSFIELD, sizeof(*ss->fields)),
.direction = screen->direction,
.treeDirection = 1,
.sortKey = sortKey,
Expand Down Expand Up @@ -557,7 +557,7 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini
return didReadAny;
}

static void writeFields(FILE* fd, const ProcessField* fields, Hashtable* columns, bool byName, char separator) {
static void writeFields(FILE* fd, const FieldID* fields, Hashtable* columns, bool byName, char separator) {
const char* sep = "";
for (unsigned int i = 0; fields[i]; i++) {
if (fields[i] < LAST_PROCESSFIELD && byName) {
Expand Down Expand Up @@ -841,7 +841,7 @@ void ScreenSettings_invertSortOrder(ScreenSettings* this) {
*attr = (*attr == 1) ? -1 : 1;
}

void ScreenSettings_setSortKey(ScreenSettings* this, ProcessField sortKey) {
void ScreenSettings_setSortKey(ScreenSettings* this, FieldID sortKey) {
if (this->treeViewAlwaysByPID || !this->treeView) {
this->sortKey = sortKey;
this->direction = (Process_fields[sortKey].defaultSortDesc) ? -1 : 1;
Expand Down
10 changes: 5 additions & 5 deletions Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ typedef struct ScreenSettings_ {
char* heading; /* user-editable screen name (pretty) */
char* dynamic; /* from DynamicScreen config (fixed) */
struct Table_* table;
RowField* fields;
FieldID* fields;
uint32_t flags;
int direction;
int treeDirection;
RowField sortKey;
RowField treeSortKey;
FieldID sortKey;
FieldID treeSortKey;
bool treeView;
bool treeViewAlwaysByPID;
bool allBranchesCollapsed;
Expand Down Expand Up @@ -111,7 +111,7 @@ typedef struct Settings_ {

#define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromOne ? (cpu)+1 : (cpu))

static inline RowField ScreenSettings_getActiveSortKey(const ScreenSettings* this) {
static inline FieldID ScreenSettings_getActiveSortKey(const ScreenSettings* this) {
return (this->treeView)
? (this->treeViewAlwaysByPID ? 1 : this->treeSortKey)
: this->sortKey;
Expand All @@ -135,7 +135,7 @@ void ScreenSettings_delete(ScreenSettings* this);

void ScreenSettings_invertSortOrder(ScreenSettings* this);

void ScreenSettings_setSortKey(ScreenSettings* this, RowField sortKey);
void ScreenSettings_setSortKey(ScreenSettings* this, FieldID sortKey);

void Settings_enableReadonly(void);

Expand Down
4 changes: 2 additions & 2 deletions Table.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ void Table_printHeader(const Settings* settings, RichString* header) {
RichString_rewind(header, RichString_size(header));

const ScreenSettings* ss = settings->ss;
const RowField* fields = ss->fields;
const FieldID* fields = ss->fields;

RowField key = ScreenSettings_getActiveSortKey(ss);
FieldID key = ScreenSettings_getActiveSortKey(ss);

for (int i = 0; fields[i]; i++) {
int color;
Expand Down
4 changes: 2 additions & 2 deletions darwin/DarwinProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void Process_delete(Object* cast) {
free(this);
}

static void DarwinProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
static void DarwinProcess_rowWriteField(const Row* super, RichString* str, FieldID field) {
const DarwinProcess* dp = (const DarwinProcess*) super;
char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR];
Expand All @@ -87,7 +87,7 @@ static void DarwinProcess_rowWriteField(const Row* super, RichString* str, Proce
RichString_appendWide(str, attr, buffer);
}

static int DarwinProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
static int DarwinProcess_compareByKey(const Process* v1, const Process* v2, FieldID key) {
const DarwinProcess* p1 = (const DarwinProcess*)v1;
const DarwinProcess* p2 = (const DarwinProcess*)v2;

Expand Down
4 changes: 2 additions & 2 deletions dragonflybsd/DragonFlyBSDProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void Process_delete(Object* cast) {
free(this);
}

static void DragonFlyBSDProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) {
static void DragonFlyBSDProcess_rowWriteField(const Row* super, RichString* str, FieldID field) {
const Process* this = (const Process*) super;
const DragonFlyBSDProcess* fp = (const DragonFlyBSDProcess*) this;
char buffer[256]; buffer[255] = '\0';
Expand All @@ -84,7 +84,7 @@ static void DragonFlyBSDProcess_rowWriteField(const Row* super, RichString* str,
RichString_appendWide(str, attr, buffer);
}

static int DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
static int DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, FieldID key) {
const DragonFlyBSDProcess* p1 = (const DragonFlyBSDProcess*)v1;
const DragonFlyBSDProcess* p2 = (const DragonFlyBSDProcess*)v2;

Expand Down
Loading

0 comments on commit 6d091a8

Please sign in to comment.