mdbx-windows: check ability of address space for growth before umnap.

Change-Id: Ibb62dae26ebab8f3efdbf6aed1b26d88ed63a2b7
This commit is contained in:
Leonid Yuriev 2018-01-14 18:36:25 +03:00 committed by Leo Yuriev
parent 373d4ad81d
commit 5c058287d5

View File

@ -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;