min_heap: Make use of cmp_func_t and swap_func_t types
Subsystems (3)
RFC PATCH 1/1 min_heap: Make use of cmp_func_t and swap_func_t types
The types.h defines standard types for comparator and swap functions. Convert min_heap to use it.
Signed-off-by Andy Shevchenko <[email protected]>
--- include/linux/min_heap.h | 8 ++++---- kernel/events/core.c | 4 ++-- lib/test_min_heap.c | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-)
include/linux/min_heap.h +4 -4
@@ -26,8 +26,8 @@ struct min_heap {*/ struct min_heap_callbacks {int elem_size; -bool (*less)(const void *lhs,const void *rhs); -void (*swp)(void *lhs,void *rhs); +cmp_func_t less; +swap_func_t swp; };/* Sift the element at pos down the heap. */
@@ -55,7 +55,7 @@ void min_heapify(struct min_heap *heap, int pos, }if (smallest == parent)break ; - func->swp (smallest, parent); + func->swp (smallest, parent, func->elem_size);if (smallest == left) pos = (pos *2 ) +1 ;else
@@ -127,7 +127,7 @@ void min_heap_push(struct min_heap *heap, const void *element, parent = data + ((pos -1 ) /2 ) * func->elem_size;if (func->less (parent, child))break ; - func->swp (parent, child); + func->swp (parent, child, func->elem_size); } }
kernel/events/core.c +2 -2
@@ -3639,7 +3639,7 @@ void __perf_event_task_sched_out(struct task_struct *task,perf_cgroup_switch (next); } -static bool perf_less_group_idx (const void *l,const void *r) +static int perf_less_group_idx (const void *l,const void *r) {const struct perf_event *le = *(const struct perf_event **)l;const struct perf_event *re = *(const struct perf_event **)r;
@@ -3647,7 +3647,7 @@ static bool perf_less_group_idx(const void *l, const void *r)return le->group_index < re->group_index; } -static void swap_ptr (void *l,void *r) +static void swap_ptr (void *l,void *r,int size) {void **lp = l, **rp = r;
lib/test_min_heap.c +3 -3
@@ -11,17 +11,17 @@#include <linux/printk.h> #include <linux/random.h> -static __init bool less_than (const void *lhs,const void *rhs) +static __init int less_than (const void *lhs,const void *rhs) {return *(int *)lhs < *(int *)rhs; } -static __init bool greater_than (const void *lhs,const void *rhs) +static __init int greater_than (const void *lhs,const void *rhs) {return *(int *)lhs > *(int *)rhs; } -static __init void swap_ints (void *lhs,void *rhs) +static __init void swap_ints (void *lhs,void *rhs,int size) {int temp = *(int *)lhs;
Signature · 1 line
2.40.0.1.gaa8946217a0b
Ian Rogers
reviewerQuoted 16 lines · Andy Shevchenko
The
types.hdefines standard types for comparator and swap functions. Convertmin_heapto use it.Signed-off-by Andy Shevchenko <[email protected]>--- include/linux/min_heap.h | 8 ++++---- kernel/events/core.c | 4 ++-- lib/test_min_heap.c | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-)include/linux/min_heap.h +0
-0
include/linux/min_heap.h struct min_heap { · line 29
Quoted 6 lines
include/linux/min_heap.h +1
-2@@ -26,8 +26,8 @@ struct min_heap {*/ struct min_heap_callbacks {int elem_size; -bool (*less)(const void *lhs,const void *rhs); -void (*swp)(void *lhs,void *rhs); +cmp_func_t less;
This looks wrong. A cmp_func_t will return -1, for less-than, 0 for equal, +1 for greater-than whilst the less function here is just providing less-than as true/false. This is done to avoid common pitfalls around minimum integer.
include/linux/min_heap.h struct min_heap { · line 30
Quoted 1 line
include/linux/min_heap.h +1
-0+swap_func_t swp;
Why pass a size argument when we're specifying the swap function to use anyway?
Thanks,
Ian
Quoted 71 lines
include/linux/min_heap.h +2
-2};/* Sift the element at pos down the heap. */ @@ -55,7 +55,7 @@ void min_heapify(struct min_heap *heap, int pos, }if (smallest == parent)break ; - func->swp (smallest, parent); + func->swp (smallest, parent, func->elem_size);if (smallest == left) pos = (pos *2 ) +1 ;else @@ -127,7 +127,7 @@ void min_heap_push(struct min_heap *heap, const void *element, parent = data + ((pos -1 ) /2 ) * func->elem_size;if (func->less (parent, child))break ; - func->swp (parent, child); + func->swp (parent, child, func->elem_size); } }kernel/events/core.c +2
-2@@ -3639,7 +3639,7 @@ void __perf_event_task_sched_out(struct task_struct *task,perf_cgroup_switch (next); } -static bool perf_less_group_idx (const void *l,const void *r) +static int perf_less_group_idx (const void *l,const void *r) {const struct perf_event *le = *(const struct perf_event **)l;const struct perf_event *re = *(const struct perf_event **)r;@@ -3647,7 +3647,7 @@ static bool perf_less_group_idx(const void *l, const void *r)return le->group_index < re->group_index; } -static void swap_ptr (void *l,void *r) +static void swap_ptr (void *l,void *r,int size) {void **lp = l, **rp = r;lib/test_min_heap.c +3
-4@@ -11,17 +11,17 @@#include <linux/printk.h> #include <linux/random.h> -static __init bool less_than (const void *lhs,const void *rhs) +static __init int less_than (const void *lhs,const void *rhs) {return *(int *)lhs < *(int *)rhs; } -static __init bool greater_than (const void *lhs,const void *rhs) +static __init int greater_than (const void *lhs,const void *rhs) {return *(int *)lhs > *(int *)rhs; } -static __init void swap_ints (void *lhs,void *rhs) +static __init void swap_ints (void *lhs,void *rhs,int size) {int temp = *(int *)lhs; --2.40.0.1.gaa8946217a0b