Skip to content

Commit f29067d

Browse files
committed
Make Object_Compare functions accept third argument
Update the prototypes of all Object_Compare functions and Vector_sort() function to accept the third argument. The third argument allows passing in extra information or states that could be useful in sorting. The definition of Object_Compare now matches the prototype of the compare function in libc qsort_r(). (Previously many programmers would use global variables to read or store extra information needed in a compare operation. This made the sort function not reentrant. It was the primary motivation for the qsort_r() function.) Currently all Object_Compare functions in htop would simply the third argument. No functions are using it yet, but this may be changed in the future. The callers of Vector_sort() will now by default pass in the reference of the container object (usually named "this") as the third argument to the sort compare functions. Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 3804c55 commit f29067d

File tree

15 files changed

+44
-39
lines changed

15 files changed

+44
-39
lines changed

Action.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ static Htop_Reaction actionFilterByUser(State* st) {
542542
Panel_setHeader(usersPanel, "Show processes of:");
543543
Machine* host = st->host;
544544
UsersTable_foreach(host->usersTable, addUserToVector, usersPanel);
545-
Vector_sort(usersPanel->items, NULL);
545+
Vector_sort(usersPanel->items, NULL, usersPanel);
546546
ListItem* allUsers = ListItem_new("All users", -1);
547547
Panel_insert(usersPanel, 0, (Object*) allUsers);
548548
const ListItem* picked = (ListItem*) Action_pickFromVector(st, usersPanel, 19, false);

EnvScreen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ static void EnvScreen_scan(InfoScreen* this) {
5252
InfoScreen_addLine(this, "Could not read process environment.");
5353
}
5454

55-
Vector_sort(this->lines, NULL);
56-
Vector_sort(panel->items, NULL);
55+
Vector_sort(this->lines, NULL, this);
56+
Vector_sort(panel->items, NULL, this);
5757
Panel_setSelected(panel, idx);
5858
}
5959

ListItem.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void ListItem_append(ListItem* this, const char* text) {
5959
this->value[newLen] = '\0';
6060
}
6161

62-
int ListItem_compare(const void* cast1, const void* cast2) {
62+
int ListItem_compare(const void* cast1, const void* cast2, void* context) {
63+
(void)context;
6364
const ListItem* obj1 = (const ListItem*) cast1;
6465
const ListItem* obj2 = (const ListItem*) cast2;
6566
return strcmp(obj1->value, obj2->value);

ListItem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ListItem* ListItem_new(const char* value, int key);
3232

3333
void ListItem_append(ListItem* this, const char* text);
3434

35-
int ListItem_compare(const void* cast1, const void* cast2);
35+
int ListItem_compare(const void* cast1, const void* cast2, void* context);
3636

3737
static inline const char* ListItem_getRef(const ListItem* this) {
3838
return this->value;

Object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Object_;
1919
typedef struct Object_ Object;
2020

2121
typedef void(*Object_Display)(const Object*, RichString*);
22-
typedef int(*Object_Compare)(const void*, const void*);
22+
typedef int(*Object_Compare)(const void*, const void*, void*);
2323
typedef void(*Object_Delete)(Object*);
2424

2525
#define Object_getClass(obj_) ((const Object*)(obj_))->klass
@@ -28,7 +28,7 @@ typedef void(*Object_Delete)(Object*);
2828
#define Object_delete(obj_) (assert(Object_getClass(obj_)->delete), Object_getClass(obj_)->delete((Object*)(obj_)))
2929
#define Object_displayFn(obj_) Object_getClass(obj_)->display
3030
#define Object_display(obj_, str_) (assert(Object_getClass(obj_)->display), Object_getClass(obj_)->display((const Object*)(obj_), str_))
31-
#define Object_compare(obj_, other_) (assert(Object_getClass(obj_)->compare), Object_getClass(obj_)->compare((const void*)(obj_), other_))
31+
#define Object_compare(obj_, other_, ctx_) (assert(Object_getClass(obj_)->compare), Object_getClass(obj_)->compare((const void*)(obj_), other_, ctx_)
3232

3333
#define Class(class_) ((const ObjectClass*)(&(class_ ## _class)))
3434

OpenFilesScreen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ static void OpenFilesScreen_scan(InfoScreen* super) {
300300
OpenFiles_Data_clear(&pdata->data);
301301
}
302302
free(pdata);
303-
Vector_sort(super->lines, NULL);
304-
Vector_sort(panel->items, NULL);
303+
Vector_sort(super->lines, NULL, super);
304+
Vector_sort(panel->items, NULL, super);
305305
Panel_setSelected(panel, idx);
306306
}
307307

Process.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,8 @@ bool Process_rowSendSignal(Row* super, Arg sgn) {
911911
return Process_sendSignal(this, sgn);
912912
}
913913

914-
int Process_compare(const void* v1, const void* v2) {
914+
int Process_compare(const void* v1, const void* v2, void* context) {
915+
(void)context;
915916
const Process* p1 = (const Process*)v1;
916917
const Process* p2 = (const Process*)v2;
917918

@@ -928,7 +929,7 @@ int Process_compare(const void* v1, const void* v2) {
928929
return (ScreenSettings_getActiveDirection(ss) == 1) ? result : -result;
929930
}
930931

931-
int Process_compareByParent(const Row* r1, const Row* r2) {
932+
int Process_compareByParent(const Row* r1, const Row* r2, void* context) {
932933
int result = SPACESHIP_NUMBER(
933934
r1->isRoot ? 0 : Row_getGroupOrParent(r1),
934935
r2->isRoot ? 0 : Row_getGroupOrParent(r2)
@@ -937,7 +938,7 @@ int Process_compareByParent(const Row* r1, const Row* r2) {
937938
if (result != 0)
938939
return result;
939940

940-
return Process_compare(r1, r2);
941+
return Process_compare(r1, r2, context);
941942
}
942943

943944
int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key) {

Process.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ typedef int32_t ProcessField; /* see ReservedField list in RowField.h */
231231

232232
// Implemented in platform-specific code:
233233
void Process_writeField(const Process* this, RichString* str, ProcessField field);
234-
int Process_compare(const void* v1, const void* v2);
235-
int Process_compareByParent(const Row* r1, const Row* r2);
234+
int Process_compare(const void* v1, const void* v2, void* context);
235+
int Process_compareByParent(const Row* r1, const Row* r2, void* context);
236236
void Process_delete(Object* cast);
237237
extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD];
238238
#define Process_pidDigits Row_pidDigits
@@ -317,8 +317,8 @@ bool Process_rowIsVisible(const Row* super, const struct Table_* table);
317317

318318
bool Process_rowMatchesFilter(const Row* super, const struct Table_* table);
319319

320-
static inline int Process_pidEqualCompare(const void* v1, const void* v2) {
321-
return Row_idEqualCompare(v1, v2);
320+
static inline int Process_pidEqualCompare(const void* v1, const void* v2, void* context) {
321+
return Row_idEqualCompare(v1, v2, context);
322322
}
323323

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

ProcessLocksScreen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ static void ProcessLocksScreen_scan(InfoScreen* this) {
9191
}
9292
}
9393
free(pdata);
94-
Vector_sort(this->lines, NULL);
95-
Vector_sort(panel->items, NULL);
94+
Vector_sort(this->lines, NULL, this);
95+
Vector_sort(panel->items, NULL, this);
9696
Panel_setSelected(panel, idx);
9797
}
9898

Row.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,15 @@ void Row_toggleTag(Row* this) {
530530
this->tag = !this->tag;
531531
}
532532

533-
int Row_compare(const void* v1, const void* v2) {
533+
int Row_compare(const void* v1, const void* v2, void* context) {
534+
(void)context;
534535
const Row* r1 = (const Row*)v1;
535536
const Row* r2 = (const Row*)v2;
536537

537538
return SPACESHIP_NUMBER(r1->id, r2->id);
538539
}
539540

540-
int Row_compareByParent_Base(const void* v1, const void* v2) {
541+
int Row_compareByParent_Base(const void* v1, const void* v2, void* context) {
541542
const Row* r1 = (const Row*)v1;
542543
const Row* r2 = (const Row*)v2;
543544

@@ -549,7 +550,7 @@ int Row_compareByParent_Base(const void* v1, const void* v2) {
549550
if (result != 0)
550551
return result;
551552

552-
return Row_compare(v1, v2);
553+
return Row_compare(v1, v2, context);
553554
}
554555

555556
const RowClass Row_class = {

0 commit comments

Comments
 (0)