From 5c058287d58fb2ed843d9d17544decfc9dd29575 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 14 Jan 2018 18:36:25 +0300 Subject: [PATCH] mdbx-windows: check ability of address space for growth before umnap. Change-Id: Ibb62dae26ebab8f3efdbf6aed1b26d88ed63a2b7 --- src/osal.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/osal.c b/src/osal.c index 8466b8f7..b47d342a 100644 --- a/src/osal.c +++ b/src/osal.c @@ -97,12 +97,12 @@ extern NTSTATUS NTAPI NtUnmapViewOfSection(IN HANDLE ProcessHandle, extern NTSTATUS NTAPI NtClose(HANDLE Handle); extern NTSTATUS NTAPI NtAllocateVirtualMemory( - IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG ZeroBits, - IN OUT PULONG RegionSize, IN ULONG AllocationType, IN ULONG Protect); + IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, + IN OUT PSIZE_T RegionSize, IN ULONG AllocationType, IN ULONG Protect); extern NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *BaseAddress, - IN OUT PULONG RegionSize, + IN OUT PSIZE_T RegionSize, IN ULONG FreeType); #ifndef FILE_PROVIDER_CURRENT_VERSION @@ -954,6 +954,21 @@ int mdbx_mresize(int flags, mdbx_mmap_t *map, size_t size, size_t limit) { return ntstatus2errcode(status); } + if (limit > map->length) { + /* check ability of address space for growth before umnap */ + PVOID BaseAddress = (PBYTE)map->address + map->length; + SIZE_T RegionSize = limit - map->length; + status = NtAllocateVirtualMemory(GetCurrentProcess(), &BaseAddress, 0, + &RegionSize, MEM_RESERVE, PAGE_NOACCESS); + if (!NT_SUCCESS(status)) + return ntstatus2errcode(status); + + status = NtFreeVirtualMemory(GetCurrentProcess(), &BaseAddress, &RegionSize, + MEM_RELEASE); + if (!NT_SUCCESS(status)) + return ntstatus2errcode(status); + } + /* Windows unable: * - shrinking a mapped file; * - change size of mapped view;