Skip to content

Commit

Permalink
Fix WindowsVirtualMemoryProvider.free
Browse files Browse the repository at this point in the history
  • Loading branch information
pejovica committed Mar 23, 2020
1 parent 5e0ded9 commit d7a85e3
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.c.struct.SizeOf;
import org.graalvm.nativeimage.c.type.CIntPointer;
import org.graalvm.nativeimage.c.type.WordPointer;
import org.graalvm.nativeimage.hosted.Feature;
Expand Down Expand Up @@ -132,7 +133,7 @@ private static int accessAsProt(int access) {
@Override
@Uninterruptible(reason = "May be called from uninterruptible code.", mayBeInlined = true)
public Pointer reserve(UnsignedWord nbytes) {
return MemoryAPI.VirtualAlloc(WordFactory.nullPointer(), nbytes, MemoryAPI.MEM_RESERVE(), MemoryAPI.PAGE_READWRITE());
return MemoryAPI.VirtualAlloc(WordFactory.nullPointer(), nbytes, MemoryAPI.MEM_RESERVE(), MemoryAPI.PAGE_NOACCESS());
}

@Override
Expand All @@ -144,8 +145,7 @@ public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHand
@Override
@Uninterruptible(reason = "May be called from uninterruptible code.", mayBeInlined = true)
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access) {
Pointer addr = MemoryAPI.VirtualAlloc(start, nbytes, MemoryAPI.MEM_COMMIT(), accessAsProt(access));
return addr.isNull() ? WordFactory.nullPointer() : addr;
return MemoryAPI.VirtualAlloc(start, nbytes, MemoryAPI.MEM_COMMIT(), accessAsProt(access));
}

@Override
Expand All @@ -166,6 +166,15 @@ public int uncommit(PointerBase start, UnsignedWord nbytes) {
@Override
@Uninterruptible(reason = "May be called from uninterruptible code.", mayBeInlined = true)
public int free(PointerBase start, UnsignedWord nbytes) {
return MemoryAPI.VirtualFree(start, nbytes, MemoryAPI.MEM_RELEASE());
assert isValid(start) : "Invalid address range start";
int result = MemoryAPI.VirtualFree(start, WordFactory.zero(), MemoryAPI.MEM_RELEASE());
return result != 0 ? 0 : -1;
}

@Uninterruptible(reason = "May be called from uninterruptible code.", mayBeInlined = true)
private static boolean isValid(PointerBase start) {
MemoryAPI.MEMORY_BASIC_INFORMATION memoryInfo = StackValue.get(MemoryAPI.MEMORY_BASIC_INFORMATION.class);
MemoryAPI.VirtualQuery(start, memoryInfo, SizeOf.unsigned(MemoryAPI.MEMORY_BASIC_INFORMATION.class));
return start.equal(memoryInfo.AllocationBase());
}
}

0 comments on commit d7a85e3

Please sign in to comment.