From 995a26cf19ffa8f9f81dc3f1e2859ff6805ae391 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 5 Jan 2020 22:01:47 +0300 Subject: [PATCH] mdbx-windws: refine/fix handling STATUS_CONFLICTING_ADDRESSES. Change-Id: I501acb2d5d653c74ab210907dd955d7167956af8 --- src/elements/core.c | 6 +++++- src/elements/osal.c | 36 +++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/elements/core.c b/src/elements/core.c index 8ad180ca..467d54c0 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -5135,9 +5135,13 @@ static int mdbx_txn_renew0(MDBX_txn *txn, unsigned flags) { txn->mt_flags |= MDBX_SHRINK_ALLOWED; mdbx_srwlock_AcquireShared(&env->me_remap_guard); } -#endif +#endif /* Windows */ } else { env->me_dxb_mmap.current = size; +#if defined(_WIN32) || defined(_WIN64) + env->me_dxb_mmap.filesize = + (env->me_dxb_mmap.filesize < size) ? size : env->me_dxb_mmap.filesize; +#endif /* Windows */ } #if defined(MDBX_USE_VALGRIND) || defined(__SANITIZE_ADDRESS__) mdbx_txn_valgrind(env, txn); diff --git a/src/elements/osal.c b/src/elements/osal.c index 247c1377..91016304 100644 --- a/src/elements/osal.c +++ b/src/elements/osal.c @@ -1417,12 +1417,12 @@ MDBX_INTERNAL_FUNC int mdbx_mresize(int flags, mdbx_mmap_t *map, size_t size, /* growth rw-section */ SectionSize.QuadPart = size; status = NtExtendSection(map->section, &SectionSize); - if (NT_SUCCESS(status)) { - map->current = size; - if (map->filesize < size) - map->filesize = size; - } - return ntstatus2errcode(status); + if (!NT_SUCCESS(status)) + return ntstatus2errcode(status); + map->current = size; + if (map->filesize < size) + map->filesize = size; + return MDBX_SUCCESS; } if (limit > map->limit) { @@ -1431,11 +1431,10 @@ MDBX_INTERNAL_FUNC int mdbx_mresize(int flags, mdbx_mmap_t *map, size_t size, SIZE_T RegionSize = limit - map->limit; status = NtAllocateVirtualMemory(GetCurrentProcess(), &BaseAddress, 0, &RegionSize, MEM_RESERVE, PAGE_NOACCESS); - if (!NT_SUCCESS(status)) { - if (status == /* STATUS_INVALID_ADDRESS */ 0xC0000141) - status = /* STATUS_CONFLICTING_ADDRESSES */ 0xC0000018; + if (status == /* STATUS_CONFLICTING_ADDRESSES */ 0xC0000018) + return MDBX_RESULT_TRUE; + if (!NT_SUCCESS(status)) return ntstatus2errcode(status); - } status = NtFreeVirtualMemory(GetCurrentProcess(), &BaseAddress, &RegionSize, MEM_RELEASE); @@ -1462,9 +1461,13 @@ MDBX_INTERNAL_FUNC int mdbx_mresize(int flags, mdbx_mmap_t *map, size_t size, bailout: map->address = NULL; map->current = map->limit = 0; - if (ReservedAddress) - (void)NtFreeVirtualMemory(GetCurrentProcess(), &ReservedAddress, - &ReservedSize, MEM_RELEASE); + if (ReservedAddress) { + ReservedSize = 0; + status = NtFreeVirtualMemory(GetCurrentProcess(), &ReservedAddress, + &ReservedSize, MEM_RELEASE); + assert(NT_SUCCESS(status)); + (void)status; + } return err; } @@ -1475,8 +1478,7 @@ MDBX_INTERNAL_FUNC int mdbx_mresize(int flags, mdbx_mmap_t *map, size_t size, &ReservedSize, MEM_RESERVE, PAGE_NOACCESS); if (!NT_SUCCESS(status)) { ReservedAddress = NULL; - if (status != /* STATUS_CONFLICTING_ADDRESSES */ 0xC0000018 && - status != /* STATUS_INVALID_ADDRESS */ 0xC0000141) + if (status != /* STATUS_CONFLICTING_ADDRESSES */ 0xC0000018) goto bailout_ntstatus /* no way to recovery */; /* assume we can change base address if mapping size changed or prev address @@ -1516,6 +1518,7 @@ retry_file_and_section: if (ReservedAddress) { /* release reserved address space */ + ReservedSize = 0; status = NtFreeVirtualMemory(GetCurrentProcess(), &ReservedAddress, &ReservedSize, MEM_RELEASE); ReservedAddress = NULL; @@ -1536,8 +1539,7 @@ retry_mapview:; (flags & MDBX_WRITEMAP) ? PAGE_READWRITE : PAGE_READONLY); if (!NT_SUCCESS(status)) { - if ((status == /* STATUS_CONFLICTING_ADDRESSES */ 0xC0000018 || - status == /* STATUS_INVALID_ADDRESS */ 0xC0000141) && + if (status == /* STATUS_CONFLICTING_ADDRESSES */ 0xC0000018 && map->address) { /* try remap at another base address */ map->address = NULL;