lib/sort: Move swap, cmp and cmp_r function types for wider use
Subsystems (3)
PATCH 1/3 lib/sort: Move swap, cmp and cmp_r function types for wider use
The function types for swap, cmp and cmp_r functions are already being in use by modules.
Move them to types.h that everybody in kernel will be able to use generic types instead of custom ones.
This adds more sense to the comment in bsearch() later on.
--- include/linux/sort.h | 8 ++++---- include/linux/types.h | 5 +++++ lib/sort.c | 15 +++++---------- 3 files changed, 14 insertions(+), 14 deletions(-)
include/linux/sort.h +4 -4
@@ -5,12 +5,12 @@#include <linux/types.h> void sort_r (void *base,size_t num,size_t size, -int (*cmp)(const void *,const void *,const void *), -void (*swap)(void *,void *,int ), +cmp_r_func_t cmp_func, +swap_func_t swap_func,const void *priv);void sort (void *base,size_t num,size_t size, -int (*cmp)(const void *,const void *), -void (*swap)(void *,void *,int )); +cmp_func_t cmp_func, +swap_func_t swap_func);#endif
include/linux/types.h +5 -0
@@ -225,5 +225,10 @@ struct callback_head {typedef void (*rcu_callback_t )(struct rcu_head *head);typedef void (*call_rcu_func_t )(struct rcu_head *head,rcu_callback_t func); +typedef void (*swap_func_t )(void *a,void *b,int size); + +typedef int (*cmp_r_func_t )(const void *a,const void *b,const void *priv); +typedef int (*cmp_func_t )(const void *a,const void *b); +#endif /* __ASSEMBLY__ */ #endif /* _LINUX_TYPES_H */
lib/sort.c +5 -10
@@ -117,8 +117,6 @@ static void swap_bytes(void *a, void *b, size_t n) }while (n); } -typedef void (*swap_func_t )(void *a,void *b,int size); -/* * The values are arbitrary as long as they can't be confused with * a pointer, but small integers make for the smallest compare
@@ -144,12 +142,9 @@ static void do_swap(void *a, void *b, size_t size, swap_func_t swap_func)swap_func (a, b, (int )size); } -typedef int (*cmp_func_t )(const void *,const void *); -typedef int (*cmp_r_func_t )(const void *,const void *,const void *);#define _CMP_WRAPPER ((cmp_r_func_t)0L) -static int do_cmp (const void *a,const void *b, -cmp_r_func_t cmp,const void *priv) +static int do_cmp (const void *a,const void *b,cmp_r_func_t cmp,const void *priv) {if (cmp == _CMP_WRAPPER)return ((cmp_func_t)(priv))(a, b);
@@ -202,8 +197,8 @@ static size_t parent(size_t i, unsigned int lsbit, size_t size)* it less suitable for kernel use. */ void sort_r (void *base,size_t num,size_t size, -int (*cmp_func)(const void *,const void *,const void *), -void (*swap_func)(void *,void *,int size), +cmp_r_func_t cmp_func, +swap_func_t swap_func,const void *priv) {/* pre-scale counters for performance */
@@ -269,8 +264,8 @@ void sort_r(void *base, size_t num, size_t size,EXPORT_SYMBOL (sort_r);void sort (void *base,size_t num,size_t size, -int (*cmp_func)(const void *,const void *), -void (*swap_func)(void *,void *,int size)) +cmp_func_t cmp_func, +swap_func_t swap_func) {return sort_r (base, num, size, _CMP_WRAPPER, swap_func, cmp_func); }
Signature · 1 line
2.23.0
PATCH 2/3 lib/bsearch: Use generic type for comparator function
Comparator function type, cmp_func_t, is defined in the types.h, use it in bsearch() and, thus, add more sense to the corresponding comment in the code.
--- include/linux/bsearch.h | 2 +- lib/bsearch.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/bsearch.h +1 -1
@@ -5,6 +5,6 @@#include <linux/types.h> void *bsearch (const void *key,const void *base,size_t num,size_t size, -int (*cmp)(const void *key,const void *elt)); +cmp_func_t cmp);#endif /* _LINUX_BSEARCH_H */
lib/bsearch.c +1 -1
@@ -29,7 +29,7 @@* the same comparison function for both sort() and bsearch(). */ void *bsearch (const void *key,const void *base,size_t num,size_t size, -int (*cmp)(const void *key,const void *elt)) +cmp_func_t cmp) {const char *pivot;int result;
Signature · 1 line
2.23.0
PATCH 3/3 tracing: Use generic type for comparator function
In reply to Andy ShevchenkoComparator function type, cmp_func_t, is defined in the types.h, use it in the code.
--- kernel/trace/ftrace.c | 12 ++++++------ kernel/trace/trace_branch.c | 8 ++++---- kernel/trace/trace_stat.c | 6 ++---- kernel/trace/trace_stat.h | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-)
kernel/trace/ftrace.c +6 -6
@@ -462,10 +462,10 @@ static void *function_stat_start(struct tracer_stat *trace)#ifdef CONFIG_FUNCTION_GRAPH_TRACER /* function graph compares on total time */ -static int function_stat_cmp (void *p1,void *p2) +static int function_stat_cmp (const void *p1,const void *p2) { -struct ftrace_profile *a = p1; -struct ftrace_profile *b = p2; +const struct ftrace_profile *a = p1; +const struct ftrace_profile *b = p2;if (a->time < b->time)return -1 ;
@@ -476,10 +476,10 @@ static int function_stat_cmp(void *p1, void *p2) }#else /* not function graph compares against hits */ -static int function_stat_cmp (void *p1,void *p2) +static int function_stat_cmp (const void *p1,const void *p2) { -struct ftrace_profile *a = p1; -struct ftrace_profile *b = p2; +const struct ftrace_profile *a = p1; +const struct ftrace_profile *b = p2;if (a->counter < b->counter)return -1 ;
kernel/trace/trace_branch.c +4 -4
@@ -244,7 +244,7 @@ static int annotated_branch_stat_headers(struct seq_file *m)return 0 ; } -static inline long get_incorrect_percent (struct ftrace_branch_data *p) +static inline long get_incorrect_percent (const struct ftrace_branch_data *p) {long percent;
@@ -332,10 +332,10 @@ annotated_branch_stat_next(void *v, int idx)return p; } -static int annotated_branch_stat_cmp (void *p1,void *p2) +static int annotated_branch_stat_cmp (const void *p1,const void *p2) { -struct ftrace_branch_data *a = p1; -struct ftrace_branch_data *b = p2; +const struct ftrace_branch_data *a = p1; +const struct ftrace_branch_data *b = p2;long percent_a, percent_b;
kernel/trace/trace_stat.c +2 -4
@@ -72,9 +72,7 @@ static void destroy_session(struct stat_session *session)kfree (session); } -typedef int (*cmp_stat_t )(void *,void *); - -static int insert_stat (struct rb_root *root,void *stat,cmp_stat_t cmp) +static int insert_stat (struct rb_root *root,void *stat,cmp_func_t cmp) {struct rb_node **new = &(root->rb_node), *parent =NULL ;struct stat_node *data;
@@ -112,7 +110,7 @@ static int insert_stat(struct rb_root *root, void *stat, cmp_stat_t cmp)* This one will force an insertion as right-most node * in the rbtree. */ -static int dummy_cmp (void *p1,void *p2) +static int dummy_cmp (const void *p1,const void *p2) {return -1 ; }
kernel/trace/trace_stat.h +1 -1
@@ -16,7 +16,7 @@ struct tracer_stat {void *(*stat_start)(struct tracer_stat *trace);void *(*stat_next)(void *prev,int idx);/* Compare two entries for stats sorting */ -int (*stat_cmp)(void *p1,void *p2); +cmp_func_t stat_cmp;/* Print a stat entry */ int (*stat_show)(struct seq_file *s,void *p);/* Release an entry */
Signature · 1 line
2.23.0
Re: [PATCH v1 3/3] tracing: Use generic type for comparator function
On Mon, 7 Oct 2019 16:56:56 +0300
Andy Shevchenko <[email protected]> wrote:
Hi Andy,
FYI, when sending more than one patch, you should have a cover letter.
Andrew,
Do you want to take this series?
-- Steve
Quoted 107 lines
Comparator function type,
cmp_func_t, is defined in thetypes.h, use it in the code.Signed-off-by Andy Shevchenko <[email protected]>--- kernel/trace/ftrace.c | 12 ++++++------ kernel/trace/trace_branch.c | 8 ++++---- kernel/trace/trace_stat.c | 6 ++---- kernel/trace/trace_stat.h | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-)kernel/trace/ftrace.c +6
-6@@ -462,10 +462,10 @@ static void *function_stat_start(struct tracer_stat *trace)#ifdef CONFIG_FUNCTION_GRAPH_TRACER /* function graph compares on total time */ -static int function_stat_cmp (void *p1,void *p2) +static int function_stat_cmp (const void *p1,const void *p2) { -struct ftrace_profile *a = p1; -struct ftrace_profile *b = p2; +const struct ftrace_profile *a = p1; +const struct ftrace_profile *b = p2;if (a->time < b->time)return -1 ;@@ -476,10 +476,10 @@ static int function_stat_cmp(void *p1, void *p2) }#else /* not function graph compares against hits */ -static int function_stat_cmp (void *p1,void *p2) +static int function_stat_cmp (const void *p1,const void *p2) { -struct ftrace_profile *a = p1; -struct ftrace_profile *b = p2; +const struct ftrace_profile *a = p1; +const struct ftrace_profile *b = p2;if (a->counter < b->counter)return -1 ;kernel/trace/trace_branch.c +4
-4@@ -244,7 +244,7 @@ static int annotated_branch_stat_headers(struct seq_file *m)return 0 ; } -static inline long get_incorrect_percent (struct ftrace_branch_data *p) +static inline long get_incorrect_percent (const struct ftrace_branch_data *p) {long percent;@@ -332,10 +332,10 @@ annotated_branch_stat_next(void *v, int idx)return p; } -static int annotated_branch_stat_cmp (void *p1,void *p2) +static int annotated_branch_stat_cmp (const void *p1,const void *p2) { -struct ftrace_branch_data *a = p1; -struct ftrace_branch_data *b = p2; +const struct ftrace_branch_data *a = p1; +const struct ftrace_branch_data *b = p2;long percent_a, percent_b;kernel/trace/trace_stat.c +2
-4@@ -72,9 +72,7 @@ static void destroy_session(struct stat_session *session)kfree (session); } -typedef int (*cmp_stat_t )(void *,void *); - -static int insert_stat (struct rb_root *root,void *stat,cmp_stat_t cmp) +static int insert_stat (struct rb_root *root,void *stat,cmp_func_t cmp) {struct rb_node **new = &(root->rb_node), *parent =NULL ;struct stat_node *data;@@ -112,7 +110,7 @@ static int insert_stat(struct rb_root *root, void *stat, cmp_stat_t cmp)* This one will force an insertion as right-most node * in the rbtree. */ -static int dummy_cmp (void *p1,void *p2) +static int dummy_cmp (const void *p1,const void *p2) {return -1 ; }kernel/trace/trace_stat.h +1
-1@@ -16,7 +16,7 @@ struct tracer_stat {void *(*stat_start)(struct tracer_stat *trace);void *(*stat_next)(void *prev,int idx);/* Compare two entries for stats sorting */ -int (*stat_cmp)(void *p1,void *p2); +cmp_func_t stat_cmp;/* Print a stat entry */ int (*stat_show)(struct seq_file *s,void *p);/* Release an entry */