diff --git a/src/staticdata.c b/src/staticdata.c index 65f5692205704..f8d1c1388a303 100644 --- a/src/staticdata.c +++ b/src/staticdata.c @@ -3534,23 +3534,37 @@ JL_DLLEXPORT void jl_image_unpack_zstd(void *handle, jl_image_buf_t *image) image->size = ZSTD_getFrameContentSize(data, *plen); size_t page_size = jl_getpagesize(); /* jl_page_size is not set yet when loading sysimg */ size_t aligned_size = LLT_ALIGN(image->size, page_size); + int fail = 0; #if defined(_OS_WINDOWS_) size_t large_page_size = GetLargePageMinimum(); - if (image->size > 4 * large_page_size) { + image->data = NULL; + if (large_page_size > 0 && image->size > 4 * large_page_size) { size_t aligned_size = LLT_ALIGN(image->size, large_page_size); image->data = (char *)VirtualAlloc( NULL, aligned_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE); } - else { + if (!image->data) { + /* Try small pages if large pages failed. */ image->data = (char *)VirtualAlloc(NULL, aligned_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); } + fail = !image->data; +#else + image->data = (char *)mmap(NULL, aligned_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + fail = image->data == (void *)-1; +#endif + if (fail) { + const char *err; +#if defined(_OS_WINDOWS_) + char err_buf[256]; + win32_formatmessage(GetLastError(), err_buf, sizeof(err_buf)); + err = err_buf; #else - image->data = - (char *)mmap(NULL, aligned_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + err = strerror(errno); #endif - if (!image->data || image->data == (void *)-1) { - jl_printf(JL_STDERR, "ERROR: failed to allocate space for system image\n"); + jl_printf(JL_STDERR, "ERROR: failed to allocate memory for system image: %s\n", + err); jl_exit(1); }