diff --git a/contrib/lfalloc/src/lf_allocX64.h b/contrib/lfalloc/src/lf_allocX64.h index 2c4cf3f1021..12190f0712f 100644 --- a/contrib/lfalloc/src/lf_allocX64.h +++ b/contrib/lfalloc/src/lf_allocX64.h @@ -358,8 +358,15 @@ static char* AllocWithMMap(uintptr_t sz, EMMapMode mode) { } #if defined(USE_LFALLOC_RANDOM_HINT) static thread_local std::mt19937_64 generator(std::random_device{}()); - std::uniform_int_distribution distr(areaStart, areaFinish / 2); - char* largeBlock = (char*)mmap(reinterpret_cast(distr(generator)), sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + std::uniform_int_distribution distr(areaStart, areaFinish - sz - 1); + char* largeBlock; + static constexpr size_t MaxAttempts = 100; + size_t attempt = 0; + do + { + largeBlock = (char*)mmap(reinterpret_cast(distr(generator)), sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + ++attempt; + } while (uintptr_t(((char*)largeBlock - ALLOC_START) + sz) >= areaFinish && attempt < MaxAttempts && munmap(largeBlock, sz) == 0); #else char* largeBlock = (char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); #endif