Skip to content

Commit cb24a66

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. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent d389795 commit cb24a66

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/random.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,28 @@ 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+
FreeLibrary(hDll);
206+
}
207+
BCryptGenRandom_is_initialized = 1;
208+
}
209+
if (!pBCryptGenRandom)
210+
return 0;
211+
return (pBCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
194212
}
195213
#endif
196214

0 commit comments

Comments
 (0)