Re: [PATCH v8 11/14] module: Reorder functions

Peter Zijlstra
2021-12-03T10:23:59Z

On Thu, Dec 02, 2021 at 11:32:11PM +0100, Alexander Lobakin wrote:
> +/*
> + * shuffle_text_list()
> + * Use a Fisher Yates algorithm to shuffle a list of text sections.
> + */
> +static void shuffle_text_list(Elf_Shdr **list, int size)
> +{
> + u32 i, j;
> +
> + for (i = size - 1; i > 0; i--) {
> + /*
> + * pick a random index from 0 to i
> + */
> + j = get_random_u32() % (i + 1);
> +
> + swap(list[i], list[j]);
> + }
> +}

I'm sure I've seen pretty much that exact function earlier in this
series; does we really need two of them?

#define shuffle_me_harder(_base, _size, _nr) \
do { \
struct { unsigned char _[_size]; } _t, *_a = (void *)(_base); \
int _i, _j; \
for (_i = (_nr)-1; _i > 0; _i--) { \
_j = get_random_u32() % (_i + 1); \
_t = _a[_i]; \
_a[_i] = _a[_j]; \
_a[_j] = _t; \
} \
} while (0)

#define shuffle_array(_array) shuffle_me_harder(_array, sizeof(_array[0]), sizeof(_array)/sizeof(_array[0]))

Or something like that...