X Tutup
Skip to content

Commit 93bab28

Browse files
committed
tree-wide: use typesafe_qsort()
1 parent 6058516 commit 93bab28

35 files changed

+231
-362
lines changed

src/analyze/analyze.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
#define SCALE_X (0.1 / 1000.0) /* pixels per us */
4242
#define SCALE_Y (20.0)
4343

44-
#define compare(a, b) (((a) > (b))? 1 : (((b) > (a))? -1 : 0))
45-
4644
#define svg(...) printf(__VA_ARGS__)
4745

4846
#define svg_bar(class, x1, x2, y) \
@@ -191,14 +189,12 @@ static int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char
191189
return 0;
192190
}
193191

194-
static int compare_unit_time(const void *a, const void *b) {
195-
return compare(((struct unit_times *)b)->time,
196-
((struct unit_times *)a)->time);
192+
static int compare_unit_time(const struct unit_times *a, const struct unit_times *b) {
193+
return CMP(b->time, a->time);
197194
}
198195

199-
static int compare_unit_start(const void *a, const void *b) {
200-
return compare(((struct unit_times *)a)->activating,
201-
((struct unit_times *)b)->activating);
196+
static int compare_unit_start(const struct unit_times *a, const struct unit_times *b) {
197+
return CMP(a->activating, b->activating);
202198
}
203199

204200
static void unit_times_free(struct unit_times *t) {
@@ -629,7 +625,7 @@ static int analyze_plot(int argc, char *argv[], void *userdata) {
629625
if (n <= 0)
630626
return n;
631627

632-
qsort(times, n, sizeof(struct unit_times), compare_unit_start);
628+
typesafe_qsort(times, n, compare_unit_start);
633629

634630
width = SCALE_X * (boot->firmware_time + boot->finish_time);
635631
if (width < 800.0)
@@ -854,8 +850,7 @@ static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, cha
854850

855851
static Hashmap *unit_times_hashmap;
856852

857-
static int list_dependencies_compare(const void *_a, const void *_b) {
858-
const char **a = (const char**) _a, **b = (const char**) _b;
853+
static int list_dependencies_compare(char * const *a, char * const *b) {
859854
usec_t usa = 0, usb = 0;
860855
struct unit_times *times;
861856

@@ -866,7 +861,7 @@ static int list_dependencies_compare(const void *_a, const void *_b) {
866861
if (times)
867862
usb = times->activated;
868863

869-
return usb - usa;
864+
return CMP(usb, usa);
870865
}
871866

872867
static bool times_in_range(const struct unit_times *times, const struct boot_times *boot) {
@@ -891,7 +886,7 @@ static int list_dependencies_one(sd_bus *bus, const char *name, unsigned int lev
891886
if (r < 0)
892887
return r;
893888

894-
qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
889+
typesafe_qsort(deps, strv_length(deps), list_dependencies_compare);
895890

896891
r = acquire_boot_times(bus, &boot);
897892
if (r < 0)
@@ -1056,7 +1051,7 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
10561051
if (n <= 0)
10571052
return n;
10581053

1059-
qsort(times, n, sizeof(struct unit_times), compare_unit_time);
1054+
typesafe_qsort(times, n, compare_unit_time);
10601055

10611056
(void) pager_open(arg_no_pager, false);
10621057

src/basic/calendarspec.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,18 @@ CalendarSpec* calendar_spec_free(CalendarSpec *c) {
5757
return mfree(c);
5858
}
5959

60-
static int component_compare(const void *_a, const void *_b) {
61-
CalendarComponent * const *a = _a, * const *b = _b;
62-
63-
if ((*a)->start < (*b)->start)
64-
return -1;
65-
if ((*a)->start > (*b)->start)
66-
return 1;
60+
static int component_compare(CalendarComponent * const *a, CalendarComponent * const *b) {
61+
int r;
6762

68-
if ((*a)->stop < (*b)->stop)
69-
return -1;
70-
if ((*a)->stop > (*b)->stop)
71-
return 1;
63+
r = CMP((*a)->start, (*b)->start);
64+
if (r != 0)
65+
return r;
7266

73-
if ((*a)->repeat < (*b)->repeat)
74-
return -1;
75-
if ((*a)->repeat > (*b)->repeat)
76-
return 1;
67+
r = CMP((*a)->stop, (*b)->stop);
68+
if (r != 0)
69+
return r;
7770

78-
return 0;
71+
return CMP((*a)->repeat, (*b)->repeat);
7972
}
8073

8174
static void normalize_chain(CalendarComponent **c) {
@@ -103,7 +96,7 @@ static void normalize_chain(CalendarComponent **c) {
10396
for (i = *c; i; i = i->next)
10497
*(j++) = i;
10598

106-
qsort(b, n, sizeof(CalendarComponent*), component_compare);
99+
typesafe_qsort(b, n, component_compare);
107100

108101
b[n-1]->next = NULL;
109102
next = b[n-1];

src/basic/conf-files.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,8 @@ static int files_add(
134134
return 0;
135135
}
136136

137-
static int base_cmp(const void *a, const void *b) {
138-
const char *s1, *s2;
139-
140-
s1 = *(char * const *)a;
141-
s2 = *(char * const *)b;
142-
return strcmp(basename(s1), basename(s2));
137+
static int base_cmp(char * const *a, char * const *b) {
138+
return strcmp(basename(*a), basename(*b));
143139
}
144140

145141
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
@@ -176,7 +172,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
176172
if (!files)
177173
return -ENOMEM;
178174

179-
qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
175+
typesafe_qsort(files, hashmap_size(fh), base_cmp);
180176
*strv = files;
181177

182178
return 0;
@@ -200,7 +196,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
200196
for (i = 0; i < strv_length(*strv); i++) {
201197
int c;
202198

203-
c = base_cmp(*strv + i, &path);
199+
c = base_cmp((char* const*) *strv + i, (char* const*) &path);
204200
if (c == 0) {
205201
char **dir;
206202

src/basic/process-util.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,11 +1104,9 @@ void valgrind_summary_hack(void) {
11041104
#endif
11051105
}
11061106

1107-
int pid_compare_func(const void *a, const void *b) {
1108-
const pid_t *p = a, *q = b;
1109-
1107+
int pid_compare_func(const pid_t *a, const pid_t *b) {
11101108
/* Suitable for usage in qsort() */
1111-
return CMP(*p, *q);
1109+
return CMP(*a, *b);
11121110
}
11131111

11141112
int ioprio_parse_priority(const char *s, int *ret) {

src/basic/process-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static inline void* PID_TO_PTR(pid_t pid) {
108108

109109
void valgrind_summary_hack(void);
110110

111-
int pid_compare_func(const void *a, const void *b);
111+
int pid_compare_func(const pid_t *a, const pid_t *b);
112112

113113
static inline bool nice_is_valid(int n) {
114114
return n >= PRIO_MIN && n < PRIO_MAX;

src/basic/strv.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,14 +711,12 @@ bool strv_overlap(char **a, char **b) {
711711
return false;
712712
}
713713

714-
static int str_compare(const void *_a, const void *_b) {
715-
const char **a = (const char**) _a, **b = (const char**) _b;
716-
714+
static int str_compare(char * const *a, char * const *b) {
717715
return strcmp(*a, *b);
718716
}
719717

720718
char **strv_sort(char **l) {
721-
qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
719+
typesafe_qsort(l, strv_length(l), str_compare);
722720
return l;
723721
}
724722

src/busctl/busctl.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,8 @@ static int member_compare_func(const void *a, const void *b) {
739739
return strcmp_ptr(x->name, y->name);
740740
}
741741

742-
static int member_compare_funcp(const void *a, const void *b) {
743-
const Member *const * x = (const Member *const *) a, * const *y = (const Member *const *) b;
744-
745-
return member_compare_func(*x, *y);
742+
static int member_compare_funcp(Member * const *a, Member * const *b) {
743+
return member_compare_func(*a, *b);
746744
}
747745

748746
static void member_free(Member *m) {
@@ -1063,7 +1061,7 @@ static int introspect(int argc, char **argv, void *userdata) {
10631061
if (result_width > 40)
10641062
result_width = 40;
10651063

1066-
qsort(sorted, k, sizeof(Member*), member_compare_funcp);
1064+
typesafe_qsort(sorted, k, member_compare_funcp);
10671065

10681066
if (arg_legend) {
10691067
printf("%-*s %-*s %-*s %-*s %s\n",

src/cgtop/cgtop.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,9 @@ static int refresh(const char *root, Hashmap *a, Hashmap *b, unsigned iteration)
524524
return 0;
525525
}
526526

527-
static int group_compare(const void*a, const void *b) {
528-
const Group *x = *(Group**)a, *y = *(Group**)b;
527+
static int group_compare(Group * const *a, Group * const *b) {
528+
const Group *x = *a, *y = *b;
529+
int r;
529530

530531
if (arg_order != ORDER_TASKS || arg_recursive) {
531532
/* Let's make sure that the parent is always before
@@ -547,29 +548,26 @@ static int group_compare(const void*a, const void *b) {
547548
case ORDER_CPU:
548549
if (arg_cpu_type == CPU_PERCENT) {
549550
if (x->cpu_valid && y->cpu_valid) {
550-
if (x->cpu_fraction > y->cpu_fraction)
551-
return -1;
552-
else if (x->cpu_fraction < y->cpu_fraction)
553-
return 1;
551+
r = CMP(y->cpu_fraction, x->cpu_fraction);
552+
if (r != 0)
553+
return r;
554554
} else if (x->cpu_valid)
555555
return -1;
556556
else if (y->cpu_valid)
557557
return 1;
558558
} else {
559-
if (x->cpu_usage > y->cpu_usage)
560-
return -1;
561-
else if (x->cpu_usage < y->cpu_usage)
562-
return 1;
559+
r = CMP(y->cpu_usage, x->cpu_usage);
560+
if (r != 0)
561+
return r;
563562
}
564563

565564
break;
566565

567566
case ORDER_TASKS:
568567
if (x->n_tasks_valid && y->n_tasks_valid) {
569-
if (x->n_tasks > y->n_tasks)
570-
return -1;
571-
else if (x->n_tasks < y->n_tasks)
572-
return 1;
568+
r = CMP(y->n_tasks, x->n_tasks);
569+
if (r != 0)
570+
return r;
573571
} else if (x->n_tasks_valid)
574572
return -1;
575573
else if (y->n_tasks_valid)
@@ -579,10 +577,9 @@ static int group_compare(const void*a, const void *b) {
579577

580578
case ORDER_MEMORY:
581579
if (x->memory_valid && y->memory_valid) {
582-
if (x->memory > y->memory)
583-
return -1;
584-
else if (x->memory < y->memory)
585-
return 1;
580+
r = CMP(y->memory, x->memory);
581+
if (r != 0)
582+
return r;
586583
} else if (x->memory_valid)
587584
return -1;
588585
else if (y->memory_valid)
@@ -592,10 +589,9 @@ static int group_compare(const void*a, const void *b) {
592589

593590
case ORDER_IO:
594591
if (x->io_valid && y->io_valid) {
595-
if (x->io_input_bps + x->io_output_bps > y->io_input_bps + y->io_output_bps)
596-
return -1;
597-
else if (x->io_input_bps + x->io_output_bps < y->io_input_bps + y->io_output_bps)
598-
return 1;
592+
r = CMP(y->io_input_bps + y->io_output_bps, x->io_input_bps + x->io_output_bps);
593+
if (r != 0)
594+
return r;
599595
} else if (x->io_valid)
600596
return -1;
601597
else if (y->io_valid)
@@ -624,7 +620,7 @@ static void display(Hashmap *a) {
624620
if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid)
625621
array[n++] = g;
626622

627-
qsort_safe(array, n, sizeof(Group*), group_compare);
623+
typesafe_qsort(array, n, group_compare);
628624

629625
/* Find the longest names in one run */
630626
for (j = 0; j < n; j++) {

src/core/job.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,23 +1384,16 @@ void job_add_to_gc_queue(Job *j) {
13841384
j->in_gc_queue = true;
13851385
}
13861386

1387-
static int job_compare(const void *a, const void *b) {
1388-
Job *x = *(Job**) a, *y = *(Job**) b;
1389-
1390-
if (x->id < y->id)
1391-
return -1;
1392-
if (x->id > y->id)
1393-
return 1;
1394-
1395-
return 0;
1387+
static int job_compare(Job * const *a, Job * const *b) {
1388+
return CMP((*a)->id, (*b)->id);
13961389
}
13971390

13981391
static size_t sort_job_list(Job **list, size_t n) {
13991392
Job *previous = NULL;
14001393
size_t a, b;
14011394

14021395
/* Order by numeric IDs */
1403-
qsort_safe(list, n, sizeof(Job*), job_compare);
1396+
typesafe_qsort(list, n, job_compare);
14041397

14051398
/* Filter out duplicates */
14061399
for (a = 0, b = 0; a < n; a++) {

src/core/namespace.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,22 +397,16 @@ static int append_protect_system(MountEntry **p, ProtectSystem protect_system, b
397397
}
398398
}
399399

400-
static int mount_path_compare(const void *a, const void *b) {
401-
const MountEntry *p = a, *q = b;
400+
static int mount_path_compare(const MountEntry *a, const MountEntry *b) {
402401
int d;
403402

404403
/* If the paths are not equal, then order prefixes first */
405-
d = path_compare(mount_entry_path(p), mount_entry_path(q));
404+
d = path_compare(mount_entry_path(a), mount_entry_path(b));
406405
if (d != 0)
407406
return d;
408407

409408
/* If the paths are equal, check the mode */
410-
if (p->mode < q->mode)
411-
return -1;
412-
if (p->mode > q->mode)
413-
return 1;
414-
415-
return 0;
409+
return CMP((int) a->mode, (int) b->mode);
416410
}
417411

418412
static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) {
@@ -1132,7 +1126,7 @@ static void normalize_mounts(const char *root_directory, MountEntry *mounts, siz
11321126
assert(n_mounts);
11331127
assert(mounts || *n_mounts == 0);
11341128

1135-
qsort_safe(mounts, *n_mounts, sizeof(MountEntry), mount_path_compare);
1129+
typesafe_qsort(mounts, *n_mounts, mount_path_compare);
11361130

11371131
drop_duplicates(mounts, n_mounts);
11381132
drop_outside_root(root_directory, mounts, n_mounts);

0 commit comments

Comments
 (0)
X Tutup