utils: add round_up/down() helper functions
This commit is contained in:
@@ -165,6 +165,28 @@ START_TEST(test_untoh)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/*******************************************************************************
|
||||
* round_up/down
|
||||
*/
|
||||
|
||||
START_TEST(test_round)
|
||||
{
|
||||
ck_assert_int_eq(round_up(0, 4), 0);
|
||||
ck_assert_int_eq(round_up(1, 4), 4);
|
||||
ck_assert_int_eq(round_up(2, 4), 4);
|
||||
ck_assert_int_eq(round_up(3, 4), 4);
|
||||
ck_assert_int_eq(round_up(4, 4), 4);
|
||||
ck_assert_int_eq(round_up(5, 4), 8);
|
||||
|
||||
ck_assert_int_eq(round_down(0, 4), 0);
|
||||
ck_assert_int_eq(round_down(1, 4), 0);
|
||||
ck_assert_int_eq(round_down(2, 4), 0);
|
||||
ck_assert_int_eq(round_down(3, 4), 0);
|
||||
ck_assert_int_eq(round_down(4, 4), 4);
|
||||
ck_assert_int_eq(round_down(5, 4), 4);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/*******************************************************************************
|
||||
* memxor
|
||||
*/
|
||||
@@ -416,6 +438,10 @@ Suite *utils_suite_create()
|
||||
tcase_add_test(tc, test_untoh);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
tc = tcase_create("round");
|
||||
tcase_add_test(tc, test_round);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
tc = tcase_create("memxor");
|
||||
tcase_add_test(tc, test_memxor);
|
||||
tcase_add_test(tc, test_memxor_aligned);
|
||||
|
||||
@@ -670,6 +670,29 @@ static inline u_int64_t untoh64(void *network)
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Round up size to be multiple of alignement
|
||||
*/
|
||||
static inline size_t round_up(size_t size, int alignement)
|
||||
{
|
||||
int remainder;
|
||||
|
||||
remainder = size % alignement;
|
||||
if (remainder)
|
||||
{
|
||||
size += alignement - remainder;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Round down size to be a multiple of alignement
|
||||
*/
|
||||
static inline size_t round_down(size_t size, int alignement)
|
||||
{
|
||||
return size - (size % alignement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Special type to count references
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user