Invert Process_compare resolution so that superclass matches run first

* This removes duplicated code that adjusts the sort direction from every
  OS-specific folder.
* Most fields in a regular htop screen are OS-independent, so trying
  Process_compare first and only falling back to the OS-specific
  compareByKey function if it's an OS-specific field makes sense.
* This will allow us to override the sortKey in a global way without having
  to edit each OS-specific file.
This commit is contained in:
Hisham Muhammad 2020-12-17 20:09:55 -03:00 committed by BenBE
parent 52fa4e7ee4
commit 3d1703f16f
10 changed files with 51 additions and 90 deletions

View File

@ -555,6 +555,6 @@ long Process_compare(const void* v1, const void* v2) {
case USER:
return SPACESHIP_NULLSTR(p1->user, p2->user);
default:
return SPACESHIP_NUMBER(p1->pid, p2->pid);
return Process_compareByKey(p1, p2, key);
}
}

View File

@ -129,17 +129,20 @@ extern char Process_pidFormat[20];
typedef Process*(*Process_New)(const struct Settings_*);
typedef void (*Process_WriteField)(const Process*, RichString*, ProcessField);
typedef long (*Process_CompareByKey)(const Process*, const Process*, ProcessField);
typedef const char* (*Process_GetCommandStr)(const Process*);
typedef struct ProcessClass_ {
const ObjectClass super;
const Process_WriteField writeField;
const Process_CompareByKey compareByKey;
const Process_GetCommandStr getCommandStr;
} ProcessClass;
#define As_Process(this_) ((const ProcessClass*)((this_)->super.klass))
#define As_Process(this_) ((const ProcessClass*)((this_)->super.klass))
#define Process_getCommand(this_) (As_Process(this_)->getCommandStr ? As_Process(this_)->getCommandStr((const Process*)(this_)) : ((const Process*)(this_))->comm)
#define Process_getCommand(this_) (As_Process(this_)->getCommandStr ? As_Process(this_)->getCommandStr((const Process*)(this_)) : ((const Process*)(this_))->comm)
#define Process_compareByKey(p1_, p2_, key_) (As_Process(p1_)->compareByKey ? (As_Process(p1_)->compareByKey(p1_, p2_, key_)) : SPACESHIP_NUMBER(p1->pid, p2->pid))
static inline pid_t Process_getParentPid(const Process* this) {
return this->tgid == this->pid ? this->ppid : this->tgid;

View File

@ -83,24 +83,16 @@ static void DarwinProcess_writeField(const Process* this, RichString* str, Proce
RichString_appendWide(str, attr, buffer);
}
static long DarwinProcess_compare(const void* v1, const void* v2) {
const DarwinProcess *p1, *p2;
const Settings *settings = ((const Process*)v1)->settings;
static long DarwinProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
const DarwinProcess* p1 = (const DarwinProcess*)v1;
const DarwinProcess* p2 = (const DarwinProcess*)v2;
if (settings->direction == 1) {
p1 = (const DarwinProcess*)v1;
p2 = (const DarwinProcess*)v2;
} else {
p2 = (const DarwinProcess*)v1;
p1 = (const DarwinProcess*)v2;
}
switch ((int) settings->sortKey) {
switch ((int) key) {
// add Platform-specific fields here
case TRANSLATED:
return SPACESHIP_NUMBER(p1->translated, p2->translated);
default:
return Process_compare(v1, v2);
return SPACESHIP_NUMBER(v1->pid, v2->pid);
}
}
@ -409,7 +401,8 @@ const ProcessClass DarwinProcess_class = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = DarwinProcess_compare
.compare = Process_compare
},
.writeField = DarwinProcess_writeField,
.compareByKey = DarwinProcess_compareByKey,
};

View File

@ -23,9 +23,10 @@ const ProcessClass DragonFlyBSDProcess_class = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = DragonFlyBSDProcess_compare
.compare = Process_compare
},
.writeField = DragonFlyBSDProcess_writeField,
.compareByKey = DragonFlyBSDProcess_compareByKey
};
ProcessFieldData Process_fields[] = {
@ -108,26 +109,18 @@ void DragonFlyBSDProcess_writeField(const Process* this, RichString* str, Proces
RichString_appendWide(str, attr, buffer);
}
long DragonFlyBSDProcess_compare(const void* v1, const void* v2) {
const DragonFlyBSDProcess *p1, *p2;
const Settings *settings = ((const Process*)v1)->settings;
long DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
const DragonFlyBSDProcess* p1 = (const DragonFlyBSDProcess*)v1;
const DragonFlyBSDProcess* p2 = (const DragonFlyBSDProcess*)v2;
if (settings->direction == 1) {
p1 = (const DragonFlyBSDProcess*)v1;
p2 = (const DragonFlyBSDProcess*)v2;
} else {
p2 = (const DragonFlyBSDProcess*)v1;
p1 = (const DragonFlyBSDProcess*)v2;
}
switch ((int) settings->sortKey) {
switch ((int) key) {
// add Platform-specific fields here
case JID:
return SPACESHIP_NUMBER(p1->jid, p2->jid);
case JAIL:
return SPACESHIP_NULLSTR(p1->jname, p2->jname);
default:
return Process_compare(v1, v2);
return SPACESHIP_NULLSTR(v1->pid, v2->pid);
}
}

View File

@ -39,7 +39,7 @@ void Process_delete(Object* cast);
void DragonFlyBSDProcess_writeField(const Process* this, RichString* str, ProcessField field);
long DragonFlyBSDProcess_compare(const void* v1, const void* v2);
long DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key);
bool Process_isThread(const Process* this);

View File

@ -108,19 +108,11 @@ static void FreeBSDProcess_writeField(const Process* this, RichString* str, Proc
RichString_appendWide(str, attr, buffer);
}
static long FreeBSDProcess_compare(const void* v1, const void* v2) {
const FreeBSDProcess *p1, *p2;
const Settings *settings = ((const Process*)v1)->settings;
static long FreeBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
const FreeBSDProcess* p1 = (const FreeBSDProcess*)v1;
const FreeBSDProcess* p2 = (const FreeBSDProcess*)v2;
if (settings->direction == 1) {
p1 = (const FreeBSDProcess*)v1;
p2 = (const FreeBSDProcess*)v2;
} else {
p2 = (const FreeBSDProcess*)v1;
p1 = (const FreeBSDProcess*)v2;
}
switch ((int) settings->sortKey) {
switch ((int) key) {
// add FreeBSD-specific fields here
case JID:
return SPACESHIP_NUMBER(p1->jid, p2->jid);
@ -129,7 +121,7 @@ static long FreeBSDProcess_compare(const void* v1, const void* v2) {
case TTY_NR:
return SPACESHIP_NULLSTR(p1->ttyPath, p2->ttyPath);
default:
return Process_compare(v1, v2);
return SPACESHIP_NUMBER(v1->pid, v2->pid);
}
}
@ -148,7 +140,8 @@ const ProcessClass FreeBSDProcess_class = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = FreeBSDProcess_compare
.compare = Process_compare
},
.writeField = FreeBSDProcess_writeField,
.compareByKey = FreeBSDProcess_compareByKey
};

View File

@ -767,19 +767,11 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
RichString_appendWide(str, attr, buffer);
}
static long LinuxProcess_compare(const void* v1, const void* v2) {
const LinuxProcess *p1, *p2;
const Settings *settings = ((const Process*)v1)->settings;
static long LinuxProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
const LinuxProcess* p1 = (const LinuxProcess*)v1;
const LinuxProcess* p2 = (const LinuxProcess*)v2;
if (settings->direction == 1) {
p1 = (const LinuxProcess*)v1;
p2 = (const LinuxProcess*)v2;
} else {
p2 = (const LinuxProcess*)v1;
p1 = (const LinuxProcess*)v2;
}
switch ((int)settings->sortKey) {
switch ((int) key) {
case M_DRS:
return SPACESHIP_NUMBER(p2->m_drs, p1->m_drs);
case M_DT:
@ -865,7 +857,7 @@ static long LinuxProcess_compare(const void* v1, const void* v2) {
case CWD:
return SPACESHIP_NULLSTR(p1->cwd, p2->cwd);
default:
return Process_compare(v1, v2);
return SPACESHIP_NUMBER(v1->pid, v2->pid);
}
}
@ -878,8 +870,9 @@ const ProcessClass LinuxProcess_class = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = LinuxProcess_compare
.compare = Process_compare
},
.writeField = LinuxProcess_writeField,
.getCommandStr = LinuxProcess_getCommandStr
.getCommandStr = LinuxProcess_getCommandStr,
.compareByKey = LinuxProcess_compareByKey
};

View File

@ -212,25 +212,17 @@ static void OpenBSDProcess_writeField(const Process* this, RichString* str, Proc
RichString_appendWide(str, attr, buffer);
}
static long OpenBSDProcess_compare(const void* v1, const void* v2) {
const OpenBSDProcess *p1, *p2;
const Settings *settings = ((const Process*)v1)->settings;
if (settings->direction == 1) {
p1 = (const OpenBSDProcess*)v1;
p2 = (const OpenBSDProcess*)v2;
} else {
p2 = (const OpenBSDProcess*)v1;
p1 = (const OpenBSDProcess*)v2;
}
static long OpenBSDProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
const OpenBSDProcess* p1 = (const OpenBSDProcess*)v1;
const OpenBSDProcess* p2 = (const OpenBSDProcess*)v2;
// remove if actually used
(void)p1; (void)p2;
switch (settings->sortKey) {
switch ((int) key) {
// add OpenBSD-specific fields here
default:
return Process_compare(v1, v2);
return SPACESHIP_NUMBER(v1->pid, v2->pid);
}
}
@ -239,9 +231,10 @@ const ProcessClass OpenBSDProcess_class = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = OpenBSDProcess_compare
.compare = Process_compare
},
.writeField = OpenBSDProcess_writeField,
.compareByKey = OpenBSDProcess_compareByKey
};
bool Process_isThread(const Process* this) {

View File

@ -23,9 +23,10 @@ const ProcessClass SolarisProcess_class = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = SolarisProcess_compare
.compare = Process_compare
},
.writeField = SolarisProcess_writeField,
.compareByKey = SolarisProcess_compareByKey
};
ProcessFieldData Process_fields[] = {
@ -117,19 +118,11 @@ void SolarisProcess_writeField(const Process* this, RichString* str, ProcessFiel
RichString_appendWide(str, attr, buffer);
}
long SolarisProcess_compare(const void* v1, const void* v2) {
const SolarisProcess *p1, *p2;
const Settings* settings = ((const Process*)v1)->settings;
long SolarisProcess_compareByKey(const void* v1, const void* v2, ProcessField key) {
const SolarisProcess* p1 = (const SolarisProcess*)v1;
const SolarisProcess* p2 = (const SolarisProcess*)v2;
if (settings->direction == 1) {
p1 = (const SolarisProcess*)v1;
p2 = (const SolarisProcess*)v2;
} else {
p2 = (const SolarisProcess*)v1;
p1 = (const SolarisProcess*)v2;
}
switch ((int) settings->sortKey) {
switch ((int) key) {
case ZONEID:
return SPACESHIP_NUMBER(p1->zoneid, p2->zoneid);
case PROJID:
@ -149,7 +142,7 @@ long SolarisProcess_compare(const void* v1, const void* v2) {
case LWPID:
return SPACESHIP_NUMBER(p1->lwpid, p2->lwpid);
default:
return Process_compare(v1, v2);
return SPACESHIP_NUMBER(v1->pid, v2->pid);
}
}

View File

@ -56,7 +56,7 @@ void Process_delete(Object* cast);
void SolarisProcess_writeField(const Process* this, RichString* str, ProcessField field);
long SolarisProcess_compare(const void* v1, const void* v2);
long SolarisProcess_compareByKey(const Process* v1, const Process* v2, ProcessField field);
bool Process_isThread(const Process* this);