From 3c1e5348d15943d988283e49f84cf6245d45d783 Mon Sep 17 00:00:00 2001 From: Danila Kutenin Date: Sun, 28 Apr 2019 13:04:47 +0300 Subject: [PATCH 1/3] LfAlloc more reliable choice of hint --- contrib/lfalloc/src/lf_allocX64.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/contrib/lfalloc/src/lf_allocX64.h b/contrib/lfalloc/src/lf_allocX64.h index 2c4cf3f1021..63500426163 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); + char* largeBlock; + static constexpr size_t maxAttempts = 10; + 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 From 852ea727fb23b80e708b3cbf61c0fba6e757e733 Mon Sep 17 00:00:00 2001 From: Danila Kutenin Date: Sun, 28 Apr 2019 13:09:01 +0300 Subject: [PATCH 2/3] Exponentially small --- contrib/lfalloc/src/lf_allocX64.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/lfalloc/src/lf_allocX64.h b/contrib/lfalloc/src/lf_allocX64.h index 63500426163..2047d64e4b1 100644 --- a/contrib/lfalloc/src/lf_allocX64.h +++ b/contrib/lfalloc/src/lf_allocX64.h @@ -360,13 +360,13 @@ static char* AllocWithMMap(uintptr_t sz, EMMapMode mode) { static thread_local std::mt19937_64 generator(std::random_device{}()); std::uniform_int_distribution distr(areaStart, areaFinish - sz); char* largeBlock; - static constexpr size_t maxAttempts = 10; + 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); + } 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 From 89b0b3c3421220c4edd70e488897f9e69f6ecea6 Mon Sep 17 00:00:00 2001 From: Danila Kutenin Date: Sun, 28 Apr 2019 13:12:36 +0300 Subject: [PATCH 3/3] Fix --- contrib/lfalloc/src/lf_allocX64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/lfalloc/src/lf_allocX64.h b/contrib/lfalloc/src/lf_allocX64.h index 2047d64e4b1..12190f0712f 100644 --- a/contrib/lfalloc/src/lf_allocX64.h +++ b/contrib/lfalloc/src/lf_allocX64.h @@ -358,7 +358,7 @@ 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 - sz); + std::uniform_int_distribution distr(areaStart, areaFinish - sz - 1); char* largeBlock; static constexpr size_t MaxAttempts = 100; size_t attempt = 0;