From 9a8ed8b11c3e2031993374e6ae8d236c95bb9742 Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Thu, 11 Jun 2015 12:45:56 +0300 Subject: [PATCH] crush: move safe arithmetic functions to buider.c Given that crush_{addition,multiplication}_is_unsafe() are only used for compiling maps, they have no business in crush.c which is shared with the kernel. Signed-off-by: Ilya Dryomov --- src/crush/builder.c | 20 ++++++++++++++++++++ src/crush/builder.h | 3 +++ src/crush/crush.c | 20 -------------------- src/crush/crush.h | 2 -- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/crush/builder.c b/src/crush/builder.c index 28d957db21f03..1212e4be2d37b 100644 --- a/src/crush/builder.c +++ b/src/crush/builder.c @@ -1465,3 +1465,23 @@ int crush_reweight_bucket(struct crush_map *crush, struct crush_bucket *b) /***************************/ +/* methods to check for safe arithmetic operations */ + +int crush_addition_is_unsafe(__u32 a, __u32 b) +{ + if ((((__u32)(-1)) - b) < a) + return 1; + else + return 0; +} + +int crush_multiplication_is_unsafe(__u32 a, __u32 b) +{ + /* prevent division by zero */ + if (!b) + return 1; + if ((((__u32)(-1)) / b) < a) + return 1; + else + return 0; +} diff --git a/src/crush/builder.h b/src/crush/builder.h index efd7c8aec43d2..b436e3eb3cdf2 100644 --- a/src/crush/builder.h +++ b/src/crush/builder.h @@ -41,4 +41,7 @@ crush_make_straw_bucket(struct crush_map *map, int *items, int *weights); +extern int crush_addition_is_unsafe(__u32 a, __u32 b); +extern int crush_multiplication_is_unsafe(__u32 a, __u32 b); + #endif diff --git a/src/crush/crush.c b/src/crush/crush.c index c0bcdb794355f..c400a410b0d89 100644 --- a/src/crush/crush.c +++ b/src/crush/crush.c @@ -143,23 +143,3 @@ void crush_destroy_rule(struct crush_rule *rule) { kfree(rule); } - -// methods to check for safe arithmetic operations -int crush_addition_is_unsafe(__u32 a, __u32 b) -{ - if ((((__u32)(-1)) - b) < a) - return 1; - else - return 0; -} - -int crush_multiplication_is_unsafe(__u32 a, __u32 b) -{ - // prevent division by zero - if (!b) - return 1; - if ((((__u32)(-1)) / b) < a) - return 1; - else - return 0; -} diff --git a/src/crush/crush.h b/src/crush/crush.h index 5082c03196477..baa8e1b08792a 100644 --- a/src/crush/crush.h +++ b/src/crush/crush.h @@ -222,8 +222,6 @@ struct crush_map { /* crush.c */ extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos); -extern int crush_addition_is_unsafe(__u32 a, __u32 b); -extern int crush_multiplication_is_unsafe(__u32 a, __u32 b); extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b); extern void crush_destroy_bucket_list(struct crush_bucket_list *b); extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b); -- 2.39.5