From 0ec60aa323a2fd33a2d056a5b4046632acdd2cef Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 12 Aug 2022 13:49:22 +0200 Subject: [PATCH] mimalloc: avoid having to link to `bcrypt` just for mimalloc Instead, load the `BCryptGenRandom()` function dynamically. When needed. If needed. This is necessary because the start-up cost of Git processes spent on loading dynamic libraries is non-negligible. Signed-off-by: Johannes Schindelin --- compat/mimalloc/random.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/compat/mimalloc/random.c b/compat/mimalloc/random.c index 21d052770d..4e334d3dd7 100644 --- a/compat/mimalloc/random.c +++ b/compat/mimalloc/random.c @@ -185,9 +185,15 @@ static bool os_random_buf(void* buf, size_t buf_len) { return (RtlGenRandom(buf, (ULONG)buf_len) != 0); } #else -#pragma comment (lib,"bcrypt.lib") -#include +#include "compat/win32/lazyload.h" +#ifndef BCRYPT_USE_SYSTEM_PREFERRED_RNG +#define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002 +#endif + static bool os_random_buf(void* buf, size_t buf_len) { + DECLARE_PROC_ADDR(bcrypt, LONG, NTAPI, BCryptGenRandom, HANDLE, PUCHAR, ULONG, ULONG); + if (!INIT_PROC_ADDR(BCryptGenRandom)) + return 0; return (BCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0); } #endif