[vlc-devel] [PATCH 2/2] rand: only load the random generator algorithm once
Rémi Denis-Courmont
remi at remlab.net
Thu Mar 26 17:20:00 CET 2020
Le torstaina 26. maaliskuuta 2020, 17.33.37 EET Steve Lhomme a écrit :
> According to the documentation:
> Because of the number and type of operations that are required to find,
> load, and initialize an algorithm provider, the BCryptOpenAlgorithmProvider
> function is a relatively time intensive function. Because of this, we
> recommend that you cache any algorithm provider handles that you will use
> more than once, rather than opening and closing the algorithm providers
> over and over.
>
> We don't release it anymore but it should be released when the process ends.
So what happens if LibVLC is loaded/unloaded multiple times in the same
process?
> ---
> src/win32/rand.c | 19 +++++++++++++------
> 1 file changed, 13 insertions(+), 6 deletions(-)
>
> diff --git a/src/win32/rand.c b/src/win32/rand.c
> index 1af02aa5984..949ce866dec 100644
> --- a/src/win32/rand.c
> +++ b/src/win32/rand.c
> @@ -27,14 +27,21 @@
>
> #include <bcrypt.h>
>
> -void vlc_rand_bytes (void *buf, size_t len)
> +static BCRYPT_ALG_HANDLE algo_handle = NULL;
> +
> +static void InitAlgo(void)
> {
> - BCRYPT_ALG_HANDLE algo_handle;
> NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle,
> BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0); - if
> (BCRYPT_SUCCESS(ret))
> - {
> + if (!BCRYPT_SUCCESS(ret))
> + algo_handle = NULL;
> +}
> +
> +void vlc_rand_bytes (void *buf, size_t len)
> +{
> + static vlc_once_t algo_init_once = VLC_STATIC_ONCE;
> + vlc_once( &algo_init_once, InitAlgo );
> +
> + if (likely(algo_handle != NULL))
> BCryptGenRandom(algo_handle, buf, len, 0);
> - BCryptCloseAlgorithmProvider(algo_handle, 0);
> - }
> }
--
Rémi Denis-Courmont
http://www.remlab.net/
More information about the vlc-devel
mailing list