Skip to content

Commit 7a0247a

Browse files
committed
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 <[email protected]>
1 parent 39fbe3f commit 7a0247a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

compat/mimalloc/random.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,15 @@ static bool os_random_buf(void* buf, size_t buf_len) {
185185
return (RtlGenRandom(buf, (ULONG)buf_len) != 0);
186186
}
187187
#else
188-
#pragma comment (lib,"bcrypt.lib")
189-
#include <bcrypt.h>
188+
#include "compat/win32/lazyload.h"
189+
#ifndef BCRYPT_USE_SYSTEM_PREFERRED_RNG
190+
#define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
191+
#endif
192+
190193
static bool os_random_buf(void* buf, size_t buf_len) {
194+
DECLARE_PROC_ADDR(bcrypt, LONG, NTAPI, BCryptGenRandom, HANDLE, PUCHAR, ULONG, ULONG);
195+
if (!INIT_PROC_ADDR(BCryptGenRandom))
196+
return 0;
191197
return (BCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
192198
}
193199
#endif

0 commit comments

Comments
 (0)