Skip to content

Commit edb181c

Browse files
committed
Lazy-link to bcrypt
Let's not make `bcrypt.dl` a link-time bound library. Instead, load the `BCryptGenRandom()` function dynamically. When needed. If needed. This helps reduce the start-up cost of any mimalloc user because the time spent on loading dynamic libraries is non-negligible. Note: In contrast to how `os.c` loads libraries and obtains function addresses, we cannot call `FreeLibrary(hDll)` here because that would unload the `bcrypt` library before we want to use it. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 948a0c4 commit edb181c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/random.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,27 @@ static bool os_random_buf(void* buf, size_t buf_len) {
187187
return (RtlGenRandom(buf, (ULONG)buf_len) != 0);
188188
}
189189
#else
190-
#pragma comment (lib,"bcrypt.lib")
191-
#include <bcrypt.h>
190+
191+
#ifndef BCRYPT_USE_SYSTEM_PREFERRED_RNG
192+
#define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
193+
#endif
194+
195+
typedef LONG (NTAPI *PBCryptGenRandom)(HANDLE, PUCHAR, ULONG, ULONG);
196+
static PBCryptGenRandom pBCryptGenRandom = NULL;
197+
static int BCryptGenRandom_is_initialized = 0;
198+
192199
static bool os_random_buf(void* buf, size_t buf_len) {
193-
return (BCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
200+
if (!BCryptGenRandom_is_initialized) {
201+
HINSTANCE hDll;
202+
hDll = LoadLibrary(TEXT("bcrypt.dll"));
203+
if (hDll != NULL) {
204+
pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom");
205+
}
206+
BCryptGenRandom_is_initialized = 1;
207+
}
208+
if (!pBCryptGenRandom)
209+
return 0;
210+
return (pBCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
194211
}
195212
#endif
196213

0 commit comments

Comments
 (0)